Disable THP and THP defrag on CentOS 7 EC2 instance

I want to disable transparent_hugepage (THP) on a CentOS 7 EC2 instance, which is enabled by default:

# cat /sys/kernel/mm/transparent_hugepage/enabled
[always] madvise never
# cat /sys/kernel/mm/transparent_hugepage/defrag
[always] madvise never

This setting can be manually changed:

# echo never > /sys/kernel/mm/transparent_hugepage/enabled
# echo never > /sys/kernel/mm/transparent_hugepage/defrag
# cat /sys/kernel/mm/transparent_hugepage/enabled
always madvise [never]
# cat /sys/kernel/mm/transparent_hugepage/defrag
always madvise [never]

... but the changes are lost after reboot.

I tried to put the echo never [...] instruction into my rc.local and cloud.cfg files, but it didn't work.

I also tried to append the setting transparent_hugepage=never to the kernel line of /etc/grub.conf (as explained there), but it didn't work better.

So... how can I disable THP on CentOS 7 running on an AWS EC2 instance ?

edit: changed title... I need to disable THP and THP defrag


The solution is in tuned, as pointed out by @michael-hampton. The tricky part is that the vm plugin can only configure the /sys/kernel/mm/transparent_hugepage/enabled setting.

To disable the /sys/kernel/mm/transparent_hugepage/defrag setting too, I had to create a script that is called by the profile on start.

At the end, the complete solution is:

step 1: Create the directory to hold the custom profile:

mkdir /etc/tuned/custom

step 2: Create the profile /etc/tuned/custom/tuned.conf:

[main]
include=virtual-guest

[vm]
transparent_hugepages=never

[script]
script=script.sh

Note that this profile inherits from virtual-guest, which was my active profile, actually looking appropriate for virtualized server (EC2). You can view your active profile with the command tuned-adm active. If you're curious, you can check out the content of the predefined profiles in /usr/lib/tuned/

step 3: Create the script /etc/tuned/custom/script.sh:

#!/bin/sh

. /usr/lib/tuned/functions

start() {
    echo never > /sys/kernel/mm/transparent_hugepage/defrag
    return 0
}

stop() {
    return 0
}

process $@

Make it executable:

sudo chmod 755 /etc/tuned/custom/script.sh

step 4: Activate the new profile:

tuned-adm profile custom

Now you should get:

# cat /sys/kernel/mm/transparent_hugepage/enabled
always madvise [never]
# cat /sys/kernel/mm/transparent_hugepage/defrag
always madvise [never]

It will persist after reboot.


In addition to setting the grub command line, you also need to configure tuned. But not using the instructions you linked to, as they are so full of errors it would take half a day just to explain them all.

Create a custom tuned profile (which I'll call custom), and then set the profile. You will base it on an existing profile, such as virtual-guest if you are running in a virtual machine (EC2 is, of course), or throughput-performance if you are on a physical machine.

Create the directory to hold the custom profile:

mkdir /etc/tuned/custom

Create the custom profile /etc/tuned/custom/tuned.conf, for example:

[main]
include=virtual-guest

[vm]
transparent_hugepages=never

Now set the profile:

tuned-adm profile custom

Also try this

nano /etc/init.d/disable-transparent-hugepages

#!/bin/sh
### BEGIN INIT INFO
# Provides:          disable-transparent-hugepages
# Required-Start:    $local_fs
# Required-Stop:
# X-Start-Before:    mongod mongodb-mms-automation-agent
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Disable Linux transparent huge pages
# Description:       Disable Linux transparent huge pages, to improve
#                    database performance.
### END INIT INFO

case $1 in
  start)
    if [ -d /sys/kernel/mm/transparent_hugepage ]; then
      thp_path=/sys/kernel/mm/transparent_hugepage
    elif [ -d /sys/kernel/mm/redhat_transparent_hugepage ]; then
      thp_path=/sys/kernel/mm/redhat_transparent_hugepage
    else
      return 0
    fi

    echo 'never' > ${thp_path}/enabled
    echo 'never' > ${thp_path}/defrag

    unset thp_path
    ;;
esac

sudo chmod 755 /etc/init.d/disable-transparent-hugepages

sudo chkconfig --add disable-transparent-hugepages