How to limit CPU count or disable CPUs in a multi core server in RHEL 7 / CentOS 7

There are three different methods using which the number of CPU can be limited in Red Hat Enterprise Linux 7.

Method 1: maxcpus

Using maxcpus parameter : Add kernel parameter maxcpus=N in /boot/grub2/grub.cfg.

IMPORTANT NOTE:
It is not possible to disable CPU0 on Red Hat Enterprise Linux systems.

After enabling above parameter it is not possible to HOT plug more CPU's Online.
maxcpus=    [SMP] Maximum number of processors that an SMP kernel should make use of.  maxcpus=n : n >= 0 limits the kernel to using 'n' processors.
n=0 is a special case, it is equivalent to "nosmp", which also disables the IO APIC.

Provide the CPU count which you want to use in your system by using maxcpus variable under 'GRUB_CMDLINE_LINUX' in "/etc/sysconfig/grub" as shown below I am limiting the CPU count to 6

GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="novga panic=1 numa=off crashkernel=auto noht rhgb quiet console=tty0 maxcpus=6"
GRUB_DISABLE_RECOVERY="true"

Next regenerate the grub2 configuration file using

# grub2-mkconfig -o /boot/grub2/grub.cfg
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-3.10.0-693.5.2.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-693.5.2.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-1334797d644549a8aa195f756eaab1e1
Found initrd image: /boot/initramfs-0-rescue-1334797d644549a8aa195f756eaab1e1.img
done

Validate your changes

# grep maxcpus /boot/grub2/grub.cfg
linux16 /vmlinuz-3.10.0-693.5.2.el7.x86_64 root=/dev/mapper/os-root ro novga panic=1 numa=off crashkernel=auto rhgb quiet console=tty0 maxcpus=6
linux16 /vmlinuz-0-rescue-1334797d644549a8aa195f756eaab1e1 root=/dev/mapper/os-root ro novga panic=1 numa=off crashkernel=auto rhgb quiet console=tty0 maxcpus=6

Due to a known BUG in systemd kernel adds an udev rule that automatically sets CPUs to "online" state after they appear in the system hence that must be disabled for maxcpus to work
This rule can be disabled using the below command in /usr/lib/udev/rules.d/40-redhat.rules file

# sed -i 's/^(SUBSYSTEM=="cpu".*TEST=="online".*ATTR{online}="1")/#1/' /usr/lib/udev/rules.d/40-redhat.rules

Here we are commenting out below line which enables CPU during the reboot of the system

#SUBSYSTEM=="cpu", ACTION=="add", TEST=="online", ATTR{online}=="0", ATTR{online}="1"

Next rebuild the initramfs

# dracut -f

Next reboot the blade to make the changes affect and validate the number of cpus once the node comes up

# cat /proc/cmdline
BOOT_IMAGE=/vmlinuz-3.10.0-693.5.2.el7.x86_64 root=/dev/mapper/os-root ro novga panic=1 numa=off crashkernel=auto rhgb quiet console=tty0 maxcpus=6

# grep processor /proc/cpuinfo | wc -l
6

# lscpu | grep -i numa
NUMA node(s):          1
NUMA node0 CPU(s):     0-5

As you see now we have only 6 cpu cores.

Method 2: nr_cpus

Using nr_cpus parameter : Add kernel parameter nr_cpus=N in /boot/grub2/grub.cfg

IMPORTANT NOTE:
It is not possible to disable CPU0 on Red Hat Enterprise Linux systems.

After enabling above parameter it is not possible to HOT plug more CPU's Online.
nr_cpus=    [SMP] Maximum number of processors that an SMP kernel could support.  nr_cpus=n : n >= 1 limits the kernel to supporting 'n' processors.

Provide the CPU count which you want to use in your system by using maxcpus variable under GRUB_CMDLINE_LINUX in "/etc/sysconfig/grub" as shown below I am limiting the CPU count to 5

GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="novga panic=1 numa=off crashkernel=auto rhgb quiet console=tty0 nr_cpus=5"
GRUB_DISABLE_RECOVERY="true"

Next regenerate the grub2 configuration file using

# grub2-mkconfig -o /boot/grub2/grub.cfg
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-3.10.0-693.5.2.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-693.5.2.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-1be8540dbd0449f4b6c1a94f585eb350
Found initrd image: /boot/initramfs-0-rescue-1be8540dbd0449f4b6c1a94f585eb350.img
done

Validate your changes

# grep nr_cpus /boot/grub2/grub.cfg
linux16 /vmlinuz-3.10.0-693.5.2.el7.x86_64 root=/dev/mapper/os-root ro novga panic=1 numa=off crashkernel=auto noht rhgb quiet console=tty0 nr_cpus=5
linux16 /vmlinuz-0-rescue-1be8540dbd0449f4b6c1a94f585eb350 root=/dev/mapper/os-root ro novga panic=1 numa=off crashkernel=auto rhgb quiet console=tty0 nr_cpus=5

Before going for reboot lets check the available cores

# grep processor /proc/cpuinfo | wc -l
16

Next reboot the server to activate the changes
Once the system comes up validate the cpu count active on the system

# grep processor /proc/cpuinfo
processor       : 0
processor       : 1
processor       : 2
processor       : 3
processor       : 4

Method 3

Manually disable individual CPU using "/sys/devices/system/cpu/cpu<id>/online" file

IMPORTANT NOTE: 

This method is temporary and will not be persistent across reboot.
Below article contains more details on the steps to use this method

I hope the article was useful.