hugetlbfs: Disable PMD sharing for large systems
Subsystems (4)
Waiman Long
reviewercover hugetlbfs: Disable PMD sharing for large systems
A customer with large SMP systems (up to 16 sockets) with application that uses large amount of static hugepages (~500-1500GB) are experiencing random multisecond delays. These delays was caused by the long time it took to scan the VMA interval tree with mmap_sem held.
To fix this problem while perserving existing behavior as much as possible, we need to allow timeout in down_write() and disabling PMD sharing when it is taking too long to do so. Since a transaction can involving touching multiple huge pages, timing out for each of the huge page interactions does not completely solve the problem. So a threshold is set to completely disable PMD sharing if too many timeouts happen.
The first 4 patches of this 5-patch series adds a new down_write_timedlock() API which accepts a timeout argument and return true is locking is successful or false otherwise. It works more or less than a down_write_trylock() but the calling thread may sleep.
The last patch implements the timeout mechanism as described above. With the patched kernel installed, the customer confirmed that the problem was gone.
Waiman Long (5):
locking/rwsem: Add down_write_timedlock() locking/rwsem: Enable timeout check when spinning on owner locking/osq: Allow early break from OSQ locking/rwsem: Enable timeout check when staying in the OSQ
hugetlbfs: Limit wait time when trying to share huge PMD
include/linux/fs.h | 7 ++ include/linux/osq_lock.h | 13 +-- include/linux/rwsem.h | 4 +- kernel/locking/lock_events_list.h | 1 + kernel/locking/mutex.c | 2 +- kernel/locking/osq_lock.c | 12 +- kernel/locking/rwsem.c | 183 +++++++++++++++++++++++++----- mm/hugetlb.c | 24 +++- 8 files changed, 201 insertions(+), 45 deletions(-)
Signature · 1 line
2.18.1
Waiman Long
reviewerPATCH 1/5 locking/rwsem: Add down_write_timedlock()
There are cases where a task wants to acquire a rwsem but doesn't want to wait for an indefinite period of time. Instead, a task may want an alternative way of dealing with the inability to acquire the lock after a certain period of time. There are also cases where waiting indefinitely can potentially lead to deadlock. Doing it by using a trylock loop is inelegant as it increases cacheline contention and is difficult to control the actual wait time.
To address this dilemma, a new down_write_timedlock() variant is introduced which allows an additional ktime_t timeout argument (currently in ns) relative to now. With this new API, a task can now wait for a given period of time and bail out when the lock cannot be acquired within the given period.
In reality, the actual wait time is likely to be longer than the given time. Timeout checking isn't done when doing optimistic spinning. Therefore a short timeout smaller than the scheduling period may be less accurate.
From the lockdep perspective, down_write_timedlock() is treated similar to down_write_trylock().
A similar down_read_timedlock() may be added later on when the need arises.
--- include/linux/rwsem.h | 4 +- kernel/locking/lock_events_list.h | 1 + kernel/locking/rwsem.c | 85 +++++++++++++++++++++++++++++-- 3 files changed, 85 insertions(+), 5 deletions(-)
include/linux/rwsem.h +3 -1
@@ -15,6 +15,7 @@#include <linux/list.h> #include <linux/spinlock.h> #include <linux/atomic.h> +#include <linux/ktime.h> #include <linux/err.h> #ifdef CONFIG_RWSEM_SPIN_ON_OWNER #include <linux/osq_lock.h>
@@ -139,9 +140,10 @@ extern void down_write(struct rw_semaphore *sem);extern int __must_check down_write_killable (struct rw_semaphore *sem);/* -* trylock for writing -- returns 1 if successful, 0 if contention +* trylock or timedlock for writing -- returns 1 if successful, 0 if failed */ extern int down_write_trylock (struct rw_semaphore *sem); +extern int down_write_timedlock (struct rw_semaphore *sem,ktime_t timeout);/* * release a read lock
kernel/locking/lock_events_list.h +1 -0
@@ -69,3 +69,4 @@ LOCK_EVENT(rwsem_rlock_handoff) /* # of read lock handoffs */ LOCK_EVENT(rwsem_wlock) /* # of write locks acquired */ LOCK_EVENT(rwsem_wlock_fail) /* # of failed write lock acquisitions */ LOCK_EVENT(rwsem_wlock_handoff) /* # of write lock handoffs */ +LOCK_EVENT(rwsem_wlock_timeout) /* # of write lock timeouts */
kernel/locking/rwsem.c +81 -4
151 lines not on this page · raw message
Signature · 1 line
2.18.1
Waiman Long
reviewerPATCH 2/5 locking/rwsem: Enable timeout check when spinning on owner
In reply to Waiman LongWhen a task is optimistically spinning on the owner, it may do it for a long time if there is no other running task available in the run queue. That can be long past the given timeout value.
To prevent that from happening, the rwsem_optimistic_spin() is now modified to check for the timeout value, if specified, to see if it should abort early.
--- kernel/locking/rwsem.c | 67 ++++++++++++++++++++++++++++-------------- 1 file changed, 45 insertions(+), 22 deletions(-)
kernel/locking/rwsem.c +45 -22
@@ -716,11 +716,13 @@ rwsem_owner_state(struct task_struct *owner, unsigned long flags, unsigned long } static noinline enum owner_state -rwsem_spin_on_owner (struct rw_semaphore *sem,unsigned long nonspinnable) +rwsem_spin_on_owner (struct rw_semaphore *sem,unsigned long nonspinnable, +ktime_t timeout) {struct task_struct *new, *owner;unsigned long flags, new_flags;enum owner_state state; +int loopcnt =0 ; owner =rwsem_owner_flags (sem, &flags); state =rwsem_owner_state (owner, flags, nonspinnable);
@@ -749,16 +751,22 @@ rwsem_spin_on_owner(struct rw_semaphore *sem, unsigned long nonspinnable)*/ barrier (); -if (need_resched () || !owner_on_cpu (owner)) { - state =OWNER_NONSPINNABLE ; -break ; - } +if (need_resched () || !owner_on_cpu (owner)) + goto stop_optspin; + +if (timeout && !(++loopcnt &0xf ) && + (sched_clock () >=ktime_to_ns (timeout))) + goto stop_optspin;cpu_relax (); }rcu_read_unlock ();return state; + +stop_optspin: +rcu_read_unlock (); +return OWNER_NONSPINNABLE ; }/*
@@ -786,12 +794,13 @@ static inline u64 rwsem_rspin_threshold(struct rw_semaphore *sem)return sched_clock () + delta; } -static bool rwsem_optimistic_spin (struct rw_semaphore *sem,bool wlock) +static bool rwsem_optimistic_spin (struct rw_semaphore *sem,bool wlock, +ktime_t timeout) {bool taken = false;int prev_owner_state =OWNER_NULL ;int loop =0 ; -u64 rspin_threshold =0 ; +u64 rspin_threshold =0 , curtime;unsigned long nonspinnable = wlock ?RWSEM_WR_NONSPINNABLE :RWSEM_RD_NONSPINNABLE ;
@@ -801,6 +810,8 @@ static bool rwsem_optimistic_spin(struct rw_semaphore *sem, bool wlock)if (!osq_lock (&sem->osq)) goto done; + curtime = timeout ?sched_clock () :0 ; +/* * Optimistically spin on the owner field and attempt to acquire the * lock whenever the owner changes. Spinning will be stopped when:
@@ -810,7 +821,7 @@ static bool rwsem_optimistic_spin(struct rw_semaphore *sem, bool wlock)for (;;) {enum owner_state owner_state; - owner_state =rwsem_spin_on_owner (sem, nonspinnable); + owner_state =rwsem_spin_on_owner (sem, nonspinnable, timeout);if (!(owner_state &OWNER_SPINNABLE ))break ;
@@ -823,6 +834,21 @@ static bool rwsem_optimistic_spin(struct rw_semaphore *sem, bool wlock)if (taken)break ; +/* +* Check current time once every 16 iterations when +* 1) spinning on reader-owned rwsem; or +* 2) a timeout value is specified. +* +* This is to avoid calling sched_clock() too frequently +* so as to reduce the average latency between the times +* when the lock becomes free and when the spinner is +* ready to do a trylock. +*/ +if ((wlock && (owner_state ==OWNER_READER )) || timeout) { +if (!(++loop &0xf )) + curtime =sched_clock (); + } +/* * Time-based reader-owned rwsem optimistic spinning */
@@ -838,23 +864,18 @@ static bool rwsem_optimistic_spin(struct rw_semaphore *sem, bool wlock)if (rwsem_test_oflags (sem, nonspinnable))break ; rspin_threshold =rwsem_rspin_threshold (sem); - loop =0 ; } -/* -* Check time threshold once every 16 iterations to -* avoid calling sched_clock() too frequently so -* as to reduce the average latency between the times -* when the lock becomes free and when the spinner -* is ready to do a trylock. -*/ -else if (!(++loop &0xf ) && (sched_clock () > rspin_threshold)) { +else if (curtime > rspin_threshold) {rwsem_set_nonspinnable (sem);lockevent_inc (rwsem_opt_nospin);break ; } } +if (timeout && (ns_to_ktime (curtime) >= timeout)) +break ; +/* * An RT task cannot do optimistic spinning if it cannot * be sure the lock holder is running or live-lock may
@@ -968,7 +989,8 @@ static inline bool rwsem_can_spin_on_owner(struct rw_semaphore *sem,return false; } -static inline bool rwsem_optimistic_spin (struct rw_semaphore *sem,bool wlock) +static inline bool rwsem_optimistic_spin (struct rw_semaphore *sem,bool wlock, +ktime_t timeout) {return false; }
@@ -982,7 +1004,8 @@ static inline bool rwsem_reader_phase_trylock(struct rw_semaphore *sem, }static inline int -rwsem_spin_on_owner (struct rw_semaphore *sem,unsigned long nonspinnable) +rwsem_spin_on_owner (struct rw_semaphore *sem,unsigned long nonspinnable, +ktime_t timeout) {return 0 ; }
@@ -1036,7 +1059,7 @@ rwsem_down_read_slowpath(struct rw_semaphore *sem, int state)*/ atomic_long_add (-RWSEM_READER_BIAS , &sem->count); adjustment =0 ; -if (rwsem_optimistic_spin (sem, false)) { +if (rwsem_optimistic_spin (sem, false,0 )) {/* rwsem_optimistic_spin() implies ACQUIRE on success */ /* * Wake up other readers in the wait list if the front
@@ -1175,7 +1198,7 @@ rwsem_down_write_slowpath(struct rw_semaphore *sem, int state, ktime_t timeout)/* do optimistic spinning and steal lock if possible */ if (rwsem_can_spin_on_owner (sem,RWSEM_WR_NONSPINNABLE ) && -rwsem_optimistic_spin (sem, true)) { +rwsem_optimistic_spin (sem, true, timeout)) {/* rwsem_optimistic_spin() implies ACQUIRE on success */ return sem; }
@@ -1255,7 +1278,7 @@ rwsem_down_write_slowpath(struct rw_semaphore *sem, int state, ktime_t timeout)* without sleeping. */ if ((wstate ==WRITER_HANDOFF ) && - (rwsem_spin_on_owner (sem,0 ) ==OWNER_NULL )) + (rwsem_spin_on_owner (sem,0 ,0 ) ==OWNER_NULL )) goto trylock_again;/* Block until there are no active lockers. */
Signature · 1 line
2.18.1
Waiman Long
reviewerPATCH 3/5 locking/osq: Allow early break from OSQ
In reply to Waiman LongThe current osq_lock() function will spin until it gets the lock or when its time slice has been used up. There may be other reasons that a task may want to back out from the OSQ before getting the lock. This patch extends the osq_lock() function by adding two new arguments - a break function pointer and its argument. That break function will be called, if defined, in each iteration of the loop to see if it should break out early.
The optimistic_spin_node structure in osq_lock.h isn't needed by callers, so it is moved into osq_lock.c.
--- include/linux/osq_lock.h | 13 ++----------- kernel/locking/mutex.c | 2 +- kernel/locking/osq_lock.c | 12 +++++++++++- kernel/locking/rwsem.c | 2 +- 4 files changed, 15 insertions(+), 14 deletions(-)
include/linux/osq_lock.h +2 -11
@@ -2,16 +2,6 @@#ifndef __LINUX_OSQ_LOCK_H#define __LINUX_OSQ_LOCK_H -/* -* An MCS like lock especially tailored for optimistic spinning for sleeping -* lock implementations (mutex, rwsem, etc). -*/ -struct optimistic_spin_node { -struct optimistic_spin_node *next, *prev; -int locked;/* 1 if lock acquired */ -int cpu;/* encoded CPU # + 1 value */ -}; - struct optimistic_spin_queue {/* * Stores an encoded value of the CPU # of the tail node in the queue.
@@ -30,7 +20,8 @@ static inline void osq_lock_init(struct optimistic_spin_queue *lock)atomic_set (&lock->tail,OSQ_UNLOCKED_VAL ); } -extern bool osq_lock (struct optimistic_spin_queue *lock); +extern bool osq_lock (struct optimistic_spin_queue *lock, +bool (*break_fn)(void *),void *break_arg);extern void osq_unlock (struct optimistic_spin_queue *lock);static inline bool osq_is_locked (struct optimistic_spin_queue *lock)
kernel/locking/mutex.c +1 -1
@@ -654,7 +654,7 @@ mutex_optimistic_spin(struct mutex *lock, struct ww_acquire_ctx *ww_ctx,* acquire the mutex all at once, the spinners need to take a * MCS (queued) lock first before spinning on the owner field. */ -if (!osq_lock (&lock->osq)) +if (!osq_lock (&lock->osq,NULL ,NULL )) goto fail; }
kernel/locking/osq_lock.c +11 -1
@@ -11,6 +11,12 @@* called from interrupt context and we have preemption disabled while * spinning. */ +struct optimistic_spin_node { +struct optimistic_spin_node *next, *prev; +int locked;/* 1 if lock acquired */ +int cpu;/* encoded CPU # + 1 value */ +}; + static DEFINE_PER_CPU_SHARED_ALIGNED(struct optimistic_spin_node, osq_node);/*
@@ -87,7 +93,8 @@ osq_wait_next(struct optimistic_spin_queue *lock,return next; } -bool osq_lock (struct optimistic_spin_queue *lock) +bool osq_lock (struct optimistic_spin_queue *lock, +bool (*break_fn)(void *),void *break_arg) {struct optimistic_spin_node *node =this_cpu_ptr (&osq_node);struct optimistic_spin_node *prev, *next;
@@ -143,6 +150,9 @@ bool osq_lock(struct optimistic_spin_queue *lock)if (need_resched () ||vcpu_is_preempted (node_cpu (node->prev))) goto unqueue; +if (unlikely (break_fn) &&break_fn (break_arg)) + goto unqueue; +cpu_relax (); }return true;
kernel/locking/rwsem.c +1 -1
@@ -807,7 +807,7 @@ static bool rwsem_optimistic_spin(struct rw_semaphore *sem, bool wlock,preempt_disable ();/* sem->wait_lock should not be held when doing optimistic spinning */ -if (!osq_lock (&sem->osq)) +if (!osq_lock (&sem->osq,NULL ,NULL )) goto done; curtime = timeout ?sched_clock () :0 ;
Signature · 1 line
2.18.1
Waiman Long
reviewerPATCH 4/5 locking/rwsem: Enable timeout check when staying in the OSQ
In reply to Waiman LongUse the break function allowed by the new osq_lock() to enable early break from the OSQ when a timeout value is specified and expiration time has been reached.
--- kernel/locking/rwsem.c | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-)
kernel/locking/rwsem.c +31 -4
@@ -794,23 +794,50 @@ static inline u64 rwsem_rspin_threshold(struct rw_semaphore *sem)return sched_clock () + delta; } +struct rwsem_break_arg { +u64 timeout; +int loopcnt; +}; + +static bool rwsem_osq_break (void *brk_arg) +{ +struct rwsem_break_arg *arg = brk_arg; + + arg->loopcnt++; +/* +* Check sched_clock() only once every 256 iterations. +*/ +if (!(arg->loopcnt++ &0xff ) && (sched_clock () >= arg->timeout)) +return true; +return false; +} +static bool rwsem_optimistic_spin (struct rw_semaphore *sem,bool wlock,ktime_t timeout) { -bool taken = false; +bool taken = false, locked;int prev_owner_state =OWNER_NULL ;int loop =0 ;u64 rspin_threshold =0 , curtime; +struct rwsem_break_arg break_arg;unsigned long nonspinnable = wlock ?RWSEM_WR_NONSPINNABLE :RWSEM_RD_NONSPINNABLE ;preempt_disable ();/* sem->wait_lock should not be held when doing optimistic spinning */ -if (!osq_lock (&sem->osq,NULL ,NULL )) - goto done; +if (timeout) { + break_arg.timeout =ktime_to_ns (timeout); + break_arg.loopcnt =0 ; + locked =osq_lock (&sem->osq, rwsem_osq_break, &break_arg); + curtime =sched_clock (); + }else { + locked =osq_lock (&sem->osq,NULL ,NULL ); + curtime =0 ; + } - curtime = timeout ?sched_clock () :0 ; +if (!locked) + goto done;/* * Optimistically spin on the owner field and attempt to acquire the
Signature · 1 line
2.18.1
Waiman Long
reviewerPATCH 5/5 hugetlbfs: Limit wait time when trying to share huge PMD
In reply to Waiman LongWhen allocating a large amount of static hugepages (~500-1500GB) on a system with large number of CPUs (4, 8 or even 16 sockets), performance degradation (random multi-second delays) was observed when thousands of processes are trying to fault in the data into the huge pages. The likelihood of the delay increases with the number of sockets and hence the CPUs a system has. This only happens in the initial setup phase and will be gone after all the necessary data are faulted in.
These random delays, however, are deemed unacceptable. The cause of that delay is the long wait time in acquiring the mmap_sem when trying to share the huge PMDs.
To remove the unacceptable delays, we have to limit the amount of wait time on the mmap_sem. So the new down_write_timedlock() function is used to acquire the write lock on the mmap_sem with a timeout value of 10ms which should not cause a perceivable delay. If timeout happens, the task will abandon its effort to share the PMD and allocate its own copy instead.
When too many timeouts happens (threshold currently set at 256), the system may be too large for PMD sharing to be useful without undue delay. So the sharing will be disabled in this case.
--- include/linux/fs.h | 7 +++++++ mm/hugetlb.c | 24 +++++++++++++++++++++--- 2 files changed, 28 insertions(+), 3 deletions(-)
include/linux/fs.h +7 -0
@@ -40,6 +40,7 @@#include <linux/fs_types.h> #include <linux/build_bug.h> #include <linux/stddef.h> +#include <linux/ktime.h> #include <asm/byteorder.h> #include <uapi/linux/fs.h>
@@ -519,6 +520,12 @@ static inline void i_mmap_lock_write(struct address_space *mapping)down_write (&mapping->i_mmap_rwsem); } +static inline bool i_mmap_timedlock_write (struct address_space *mapping, +ktime_t timeout) +{ +return down_write_timedlock (&mapping->i_mmap_rwsem, timeout); +} +static inline void i_mmap_unlock_write (struct address_space *mapping) {up_write (&mapping->i_mmap_rwsem);
mm/hugetlb.c +21 -3
@@ -4750,6 +4750,8 @@ void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma, } } +#define PMD_SHARE_DISABLE_THRESHOLD (1 << 8) +/* * Search for a shareable pmd page for hugetlb. In any case calls pmd_alloc() * and returns the corresponding pte. While this is not necessary for the
@@ -4770,11 +4772,24 @@ pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud)pte_t *spte =NULL ;pte_t *pte;spinlock_t *ptl; +static atomic_t timeout_cnt; -if (!vma_shareable (vma, addr)) -return (pte_t *)pmd_alloc (mm, pud, addr); +/* +* Don't share if it is not sharable or locking attempt timed out +* after 10ms. After 256 timeouts, PMD sharing will be permanently +* disabled as it is just too slow. +*/ +if (!vma_shareable (vma, addr) || + (atomic_read (&timeout_cnt) >=PMD_SHARE_DISABLE_THRESHOLD )) + goto out_no_share; + +if (!i_mmap_timedlock_write (mapping,ms_to_ktime (10 ))) { +if (atomic_inc_return (&timeout_cnt) == +PMD_SHARE_DISABLE_THRESHOLD ) +pr_info ("Hugetlbfs PMD sharing disabled because of timeouts!\n" ); + goto out_no_share; + } -i_mmap_lock_write (mapping); vma_interval_tree_foreach(svma, &mapping->i_mmap, idx, idx) {if (svma == vma)continue ;
@@ -4806,6 +4821,9 @@ pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud) pte = (pte_t *)pmd_alloc (mm, pud, addr);i_mmap_unlock_write (mapping);return pte; + +out_no_share: +return (pte_t *)pmd_alloc (mm, pud, addr); }/*
Signature · 1 line
2.18.1
Re: [PATCH 5/5] hugetlbfs: Limit wait time when trying to share huge PMD
Quoted 7 lines · Waiman Long
When allocating a large amount of static hugepages (~500-1500GB) on a system with large number of CPUs (4, 8 or even 16 sockets), performance degradation (random multi-second delays) was observed when thousands of processes are trying to fault in the data into the huge pages. The likelihood of the delay increases with the number of sockets and hence the CPUs a system has. This only happens in the initial setup phase and will be gone after all the necessary data are faulted in.
Can;t the application just specify MAP_POPULATE?
Waiman Long
reviewerRe: [PATCH 5/5] hugetlbfs: Limit wait time when trying to share huge PMD
Quoted 9 lines · Matthew Wilcox
Quoted 7 lines · Waiman Long
When allocating a large amount of static hugepages (~500-1500GB) on a system with large number of CPUs (4, 8 or even 16 sockets), performance degradation (random multi-second delays) was observed when thousands of processes are trying to fault in the data into the huge pages. The likelihood of the delay increases with the number of sockets and hence the CPUs a system has. This only happens in the initial setup phase and will be gone after all the necessary data are faulted in.
Can;t the application just specify
MAP_POPULATE?
Originally, I thought that this happened in the startup phase when the pages were faulted in. The problem persists after steady state had been reached though. Every time you have a new user process created, it will have its own page table. It is the sharing of the of huge page shared memory that is causing problem. Of course, it depends on how the application is written.
Anyway, MAP_POPULATE will not be useful in this case.
Thanks,
Longman
Re: [PATCH 5/5] hugetlbfs: Limit wait time when trying to share huge PMD
Quoted 9 lines · Waiman Long
Quoted 8 lines · Matthew Wilcox
Quoted 7 lines · Waiman Long
When allocating a large amount of static hugepages (~500-1500GB) on a system with large number of CPUs (4, 8 or even 16 sockets), performance degradation (random multi-second delays) was observed when thousands of processes are trying to fault in the data into the huge pages. The likelihood of the delay increases with the number of sockets and hence the CPUs a system has. This only happens in the initial setup phase and will be gone after all the necessary data are faulted in.
Quoted 6 lines
Quoted 1 line
Can;t the application just specify
MAP_POPULATE?Originally, I thought that this happened in the startup phase when the pages were faulted in. The problem persists after steady state had been reached though. Every time you have a new user process created, it will have its own page table.
This is still at fault time. Although, for the particular application it may be after the 'startup phase'.
Quoted 3 lines
It is the sharing of the of huge page shared
memory that is causing problem. Of course, it depends on how the application is written.
It may be the case that some applications would find the delays acceptable for the benefit of shared pmds once they reach steady state. As you say, of course this depends on how the application is written.
I know that Oracle DB would not like it if PMD sharing is disabled for them. Based on what I know of their model, all processes which share PMDs perform faults (write or read) during the startup phase. This is in environments as big or bigger than you describe above. I have never looked at/for delays in these environments around pmd sharing (page faults), but that does not mean they do not exist. I will try to get the DB group to give me access to one of their large environments for analysis.
We may want to consider making the timeout value and disable threshold user configurable.
Signature · 1 line
Mike Kravetz
Waiman Long
reviewerRe: [PATCH 5/5] hugetlbfs: Limit wait time when trying to share huge PMD
Quoted 33 lines · Mike Kravetz
Quoted 9 lines · Waiman Long
Quoted 8 lines · Matthew Wilcox
Quoted 7 lines · Waiman Long
When allocating a large amount of static hugepages (~500-1500GB) on a system with large number of CPUs (4, 8 or even 16 sockets), performance degradation (random multi-second delays) was observed when thousands of processes are trying to fault in the data into the huge pages. The likelihood of the delay increases with the number of sockets and hence the CPUs a system has. This only happens in the initial setup phase and will be gone after all the necessary data are faulted in.
Quoted 5 lines
Quoted 1 line
Can;t the application just specify
MAP_POPULATE?Originally, I thought that this happened in the startup phase when the pages were faulted in. The problem persists after steady state had been reached though. Every time you have a new user process created, it will have its own page table.
This is still at fault time. Although, for the particular application it may be after the 'startup phase'.
Quoted 3 lines
It is the sharing of the of huge page shared
memory that is causing problem. Of course, it depends on how the application is written.It may be the case that some applications would find the delays acceptable for the benefit of shared pmds once they reach steady state. As you say, of course this depends on how the application is written.
I know that Oracle DB would not like it if PMD sharing is disabled for them. Based on what I know of their model, all processes which share PMDs perform faults (write or read) during the startup phase. This is in environments as big or bigger than you describe above. I have never looked at/for delays in these environments around pmd sharing (page faults), but that does not mean they do not exist. I will try to get the DB group to give me access to one of their large environments for analysis.
Quoted 2 lines
We may want to consider making the timeout value and disable threshold user configurable.
Making it configurable is certainly doable. They can be sysctl parameters so that the users can reenable PMD sharing by making those parameters larger.
Cheers,
Longman
Re: [PATCH 5/5] hugetlbfs: Limit wait time when trying to share huge PMD
Quoted 38 lines · Waiman Long
Quoted 33 lines · Mike Kravetz
Quoted 9 lines · Waiman Long
Quoted 8 lines · Matthew Wilcox
Quoted 7 lines · Waiman Long
When allocating a large amount of static hugepages (~500-1500GB) on a system with large number of CPUs (4, 8 or even 16 sockets), performance degradation (random multi-second delays) was observed when thousands of processes are trying to fault in the data into the huge pages. The likelihood of the delay increases with the number of sockets and hence the CPUs a system has. This only happens in the initial setup phase and will be gone after all the necessary data are faulted in.
Quoted 5 lines
Quoted 1 line
Can;t the application just specify
MAP_POPULATE?Originally, I thought that this happened in the startup phase when the pages were faulted in. The problem persists after steady state had been reached though. Every time you have a new user process created, it will have its own page table.
This is still at fault time. Although, for the particular application it may be after the 'startup phase'.
Quoted 3 lines
It is the sharing of the of huge page shared
memory that is causing problem. Of course, it depends on how the application is written.It may be the case that some applications would find the delays acceptable for the benefit of shared pmds once they reach steady state. As you say, of course this depends on how the application is written.
I know that Oracle DB would not like it if PMD sharing is disabled for them. Based on what I know of their model, all processes which share PMDs perform faults (write or read) during the startup phase. This is in environments as big or bigger than you describe above. I have never looked at/for delays in these environments around pmd sharing (page faults), but that does not mean they do not exist. I will try to get the DB group to give me access to one of their large environments for analysis.
Quoted 6 lines
Quoted 2 lines
We may want to consider making the timeout value and disable threshold user configurable.
Making it configurable is certainly doable. They can be sysctl parameters so that the users can reenable PMD sharing by making those parameters larger.
It could be a Kconfig option, so people don’t need to change the setting every time after reinstalling the system. There are times people don’t care too much about those random multi-second delays. For example, running a debug kernel.
Waiman Long
reviewerRe: [PATCH 5/5] hugetlbfs: Limit wait time when trying to share huge PMD
In reply to Waiman LongQuoted 34 lines · Waiman Long
Quoted 33 lines · Mike Kravetz
Quoted 9 lines · Waiman Long
Quoted 8 lines · Matthew Wilcox
Quoted 7 lines · Waiman Long
When allocating a large amount of static hugepages (~500-1500GB) on a system with large number of CPUs (4, 8 or even 16 sockets), performance degradation (random multi-second delays) was observed when thousands of processes are trying to fault in the data into the huge pages. The likelihood of the delay increases with the number of sockets and hence the CPUs a system has. This only happens in the initial setup phase and will be gone after all the necessary data are faulted in.
Quoted 5 lines
Quoted 1 line
Can;t the application just specify
MAP_POPULATE?Originally, I thought that this happened in the startup phase when the pages were faulted in. The problem persists after steady state had been reached though. Every time you have a new user process created, it will have its own page table.
This is still at fault time. Although, for the particular application it may be after the 'startup phase'.
Quoted 3 lines
It is the sharing of the of huge page shared
memory that is causing problem. Of course, it depends on how the application is written.It may be the case that some applications would find the delays acceptable for the benefit of shared pmds once they reach steady state. As you say, of course this depends on how the application is written.
I know that Oracle DB would not like it if PMD sharing is disabled for them. Based on what I know of their model, all processes which share PMDs perform faults (write or read) during the startup phase. This is in environments as big or bigger than you describe above. I have never looked at/for delays in these environments around pmd sharing (page faults), but that does not mean they do not exist. I will try to get the DB group to give me access to one of their large environments for analysis.
Quoted 5 lines
Quoted 2 lines
We may want to consider making the timeout value and disable threshold user configurable.
Making it configurable is certainly doable. They can be sysctl parameters so that the users can reenable PMD sharing by making those parameters larger.
I suspect that the customer's application may be generating a new process with its own address space for each transaction. That will be causing a lot of PMD sharing operations when hundreds of threads are pounding it simultaneously. I had inserted some instrumentation code to a test kernel that the customers used for testing, the number of timeouts after a certain time went up more than 20k.
On the other hands, if the application is structured in such a way that there is limited number of separate address spaces with worker threads processing the transaction, PMD sharing will be less of a problem. It will be hard to convince users to make such a structural changes to their application.
Cheers,
Longman
Re: [PATCH 5/5] hugetlbfs: Limit wait time when trying to share huge PMD
In reply to Waiman LongQuoted 73 lines · Waiman Long
When allocating a large amount of static hugepages (~500-1500GB) on a system with large number of CPUs (4, 8 or even 16 sockets), performance degradation (random multi-second delays) was observed when thousands of processes are trying to fault in the data into the huge pages. The likelihood of the delay increases with the number of sockets and hence the CPUs a system has. This only happens in the initial setup phase and will be gone after all the necessary data are faulted in.
These random delays, however, are deemed unacceptable. The cause of that delay is the long wait time in acquiring the
mmap_semwhen trying to share the huge PMDs.To remove the unacceptable delays, we have to limit the amount of wait time on the
mmap_sem. So the newdown_write_timedlock()function is used to acquire the write lock on themmap_semwith a timeout value of 10ms which should not cause a perceivable delay. If timeout happens, the task will abandon its effort to share the PMD and allocate its own copy instead.When too many timeouts happens (threshold currently set at 256), the system may be too large for PMD sharing to be useful without undue delay. So the sharing will be disabled in this case.
Signed-off-by Waiman Long <[email protected]>---
include/linux/fs.h| 7 +++++++mm/hugetlb.c| 24 +++++++++++++++++++++---2 files changed, 28 insertions(+), 3 deletions(-)include/linux/fs.h +7
-0@@ -40,6 +40,7 @@#include <linux/fs_types.h> #include <linux/build_bug.h> #include <linux/stddef.h> +#include <linux/ktime.h> #include <asm/byteorder.h> #include <uapi/linux/fs.h> @@ -519,6 +520,12 @@ static inline void i_mmap_lock_write(struct address_space *mapping)down_write (&mapping->i_mmap_rwsem); } +static inline bool i_mmap_timedlock_write (struct address_space *mapping, +ktime_t timeout) +{ +return down_write_timedlock (&mapping->i_mmap_rwsem, timeout); +} +static inline void i_mmap_unlock_write (struct address_space *mapping) {up_write (&mapping->i_mmap_rwsem);mm/hugetlb.c +2
-0@@ -4750,6 +4750,8 @@ void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma, } } +#define PMD_SHARE_DISABLE_THRESHOLD (1 << 8) +/* * Search for a shareable pmd page for hugetlb. In any case calls pmd_alloc() * and returns the corresponding pte. While this is not necessary for the @@ -4770,11 +4772,24 @@ pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud)pte_t *spte =NULL ;
mm/hugetlb.c pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud) · line 4780
Quoted 10 lines
mm/hugetlb.c +5
-2pte_t *pte;spinlock_t *ptl; +static atomic_t timeout_cnt; -if (!vma_shareable (vma, addr)) -return (pte_t *)pmd_alloc (mm, pud, addr); +/* +* Don't share if it is not sharable or locking attempt timed out +* after 10ms. After 256 timeouts, PMD sharing will be permanently +* disabled as it is just too slow.
It looks like this kind of policy interacts with kernel debug options like KASAN (which is going to slow the system down anyway) could introduce tricky issues due to different timings on a debug kernel.
Quoted 30 lines
mm/hugetlb.c +14
-1+*/ +if (!vma_shareable (vma, addr) || + (atomic_read (&timeout_cnt) >=PMD_SHARE_DISABLE_THRESHOLD )) + goto out_no_share; + +if (!i_mmap_timedlock_write (mapping,ms_to_ktime (10 ))) { +if (atomic_inc_return (&timeout_cnt) == +PMD_SHARE_DISABLE_THRESHOLD ) +pr_info ("Hugetlbfs PMD sharing disabled because of timeouts!\n" ); + goto out_no_share; + } -i_mmap_lock_write (mapping); vma_interval_tree_foreach(svma, &mapping->i_mmap, idx, idx) {if (svma == vma)continue ;@@ -4806,6 +4821,9 @@ pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud) pte = (pte_t *)pmd_alloc (mm, pud, addr);i_mmap_unlock_write (mapping);return pte; + +out_no_share: +return (pte_t *)pmd_alloc (mm, pud, addr); }/* Signature · 1 line
2.18.1
Waiman Long
reviewerRe: [PATCH 5/5] hugetlbfs: Limit wait time when trying to share huge PMD
Quoted 76 lines · Qian Cai
Quoted 76 lines · Waiman Long
When allocating a large amount of static hugepages (~500-1500GB) on a system with large number of CPUs (4, 8 or even 16 sockets), performance degradation (random multi-second delays) was observed when thousands of processes are trying to fault in the data into the huge pages. The likelihood of the delay increases with the number of sockets and hence the CPUs a system has. This only happens in the initial setup phase and will be gone after all the necessary data are faulted in.
These random delays, however, are deemed unacceptable. The cause of that delay is the long wait time in acquiring the
mmap_semwhen trying to share the huge PMDs.To remove the unacceptable delays, we have to limit the amount of wait time on the
mmap_sem. So the newdown_write_timedlock()function is used to acquire the write lock on themmap_semwith a timeout value of 10ms which should not cause a perceivable delay. If timeout happens, the task will abandon its effort to share the PMD and allocate its own copy instead.When too many timeouts happens (threshold currently set at 256), the system may be too large for PMD sharing to be useful without undue delay. So the sharing will be disabled in this case.
Signed-off-by Waiman Long <[email protected]>---
include/linux/fs.h| 7 +++++++mm/hugetlb.c| 24 +++++++++++++++++++++---2 files changed, 28 insertions(+), 3 deletions(-)include/linux/fs.h +7
-0@@ -40,6 +40,7 @@#include <linux/fs_types.h> #include <linux/build_bug.h> #include <linux/stddef.h> +#include <linux/ktime.h> #include <asm/byteorder.h> #include <uapi/linux/fs.h> @@ -519,6 +520,12 @@ static inline void i_mmap_lock_write(struct address_space *mapping)down_write (&mapping->i_mmap_rwsem); } +static inline bool i_mmap_timedlock_write (struct address_space *mapping, +ktime_t timeout) +{ +return down_write_timedlock (&mapping->i_mmap_rwsem, timeout); +} +static inline void i_mmap_unlock_write (struct address_space *mapping) {up_write (&mapping->i_mmap_rwsem);mm/hugetlb.c +3
-0@@ -4750,6 +4750,8 @@ void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma, } } +#define PMD_SHARE_DISABLE_THRESHOLD (1 << 8) +/* * Search for a shareable pmd page for hugetlb. In any case calls pmd_alloc() * and returns the corresponding pte. While this is not necessary for the @@ -4770,11 +4772,24 @@ pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud)pte_t *spte =NULL ;pte_t *pte;spinlock_t *ptl; +static atomic_t timeout_cnt;
Quoted 8 lines
mm/hugetlb.c pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud) · line 4780
Quoted 6 lines
mm/hugetlb.c +4
-2-if (!vma_shareable (vma, addr)) -return (pte_t *)pmd_alloc (mm, pud, addr); +/* +* Don't share if it is not sharable or locking attempt timed out +* after 10ms. After 256 timeouts, PMD sharing will be permanently +* disabled as it is just too slow. It looks like this kind of policy interacts with kernel debug options like KASAN (which is going to slow the system down anyway) could introduce tricky issues due to different timings on a debug kernel.
With respect to lockdep, down_write_timedlock() works like a trylock. So a lot of checking will be skipped. Also the lockdep code won't be run until the lock is acquired. So its execution time has no effect on the timeout.
Cheers,
Longman
Re: [PATCH 5/5] hugetlbfs: Limit wait time when trying to share huge PMD
Quoted 90 lines · Waiman Long
Quoted 76 lines · Qian Cai
Quoted 76 lines · Waiman Long
When allocating a large amount of static hugepages (~500-1500GB) on a system with large number of CPUs (4, 8 or even 16 sockets), performance degradation (random multi-second delays) was observed when thousands of processes are trying to fault in the data into the huge pages. The likelihood of the delay increases with the number of sockets and hence the CPUs a system has. This only happens in the initial setup phase and will be gone after all the necessary data are faulted in.
These random delays, however, are deemed unacceptable. The cause of that delay is the long wait time in acquiring the
mmap_semwhen trying to share the huge PMDs.To remove the unacceptable delays, we have to limit the amount of wait time on the
mmap_sem. So the newdown_write_timedlock()function is used to acquire the write lock on themmap_semwith a timeout value of 10ms which should not cause a perceivable delay. If timeout happens, the task will abandon its effort to share the PMD and allocate its own copy instead.When too many timeouts happens (threshold currently set at 256), the system may be too large for PMD sharing to be useful without undue delay. So the sharing will be disabled in this case.
Signed-off-by Waiman Long <[email protected]>---
include/linux/fs.h| 7 +++++++mm/hugetlb.c| 24 +++++++++++++++++++++---2 files changed, 28 insertions(+), 3 deletions(-)include/linux/fs.h +7
-0@@ -40,6 +40,7 @@#include <linux/fs_types.h> #include <linux/build_bug.h> #include <linux/stddef.h> +#include <linux/ktime.h> #include <asm/byteorder.h> #include <uapi/linux/fs.h> @@ -519,6 +520,12 @@ static inline void i_mmap_lock_write(struct address_space *mapping)down_write (&mapping->i_mmap_rwsem); } +static inline bool i_mmap_timedlock_write (struct address_space *mapping, +ktime_t timeout) +{ +return down_write_timedlock (&mapping->i_mmap_rwsem, timeout); +} +static inline void i_mmap_unlock_write (struct address_space *mapping) {up_write (&mapping->i_mmap_rwsem);mm/hugetlb.c +3
-0@@ -4750,6 +4750,8 @@ void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma, } } +#define PMD_SHARE_DISABLE_THRESHOLD (1 << 8) +/* * Search for a shareable pmd page for hugetlb. In any case calls pmd_alloc() * and returns the corresponding pte. While this is not necessary for the @@ -4770,11 +4772,24 @@ pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud)pte_t *spte =NULL ;pte_t *pte;spinlock_t *ptl; +static atomic_t timeout_cnt;Quoted 8 lines
mm/hugetlb.c pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud) · line 4780
Quoted 6 lines
mm/hugetlb.c +4
-2-if (!vma_shareable (vma, addr)) -return (pte_t *)pmd_alloc (mm, pud, addr); +/* +* Don't share if it is not sharable or locking attempt timed out +* after 10ms. After 256 timeouts, PMD sharing will be permanently +* disabled as it is just too slow. It looks like this kind of policy interacts with kernel debug options like KASAN (which is going to slow the system down anyway) could introduce tricky issues due to different timings on a debug kernel.
Quoted 4 lines
With respect to lockdep,
down_write_timedlock()works like a trylock. So a lot of checking will be skipped. Also the lockdep code won't be run until the lock is acquired. So its execution time has no effect on the timeout.
No only lockdep, but also things like KASAN, debug_pagealloc, page_poison, kmemleak, debug objects etc that all going to slow down things in huge_pmd_share(), and make it tricky to get a right timeout value for those debug kernels without changing the previous behavior.
Waiman Long
reviewerRe: [PATCH 5/5] hugetlbfs: Limit wait time when trying to share huge PMD
Quoted 87 lines · Qian Cai
Quoted 87 lines · Waiman Long
Quoted 83 lines · Qian Cai
Quoted 71 lines · Waiman Long
When allocating a large amount of static hugepages (~500-1500GB) on a system with large number of CPUs (4, 8 or even 16 sockets), performance degradation (random multi-second delays) was observed when thousands of processes are trying to fault in the data into the huge pages. The likelihood of the delay increases with the number of sockets and hence the CPUs a system has. This only happens in the initial setup phase and will be gone after all the necessary data are faulted in.
These random delays, however, are deemed unacceptable. The cause of that delay is the long wait time in acquiring the
mmap_semwhen trying to share the huge PMDs.To remove the unacceptable delays, we have to limit the amount of wait time on the
mmap_sem. So the newdown_write_timedlock()function is used to acquire the write lock on themmap_semwith a timeout value of 10ms which should not cause a perceivable delay. If timeout happens, the task will abandon its effort to share the PMD and allocate its own copy instead.When too many timeouts happens (threshold currently set at 256), the system may be too large for PMD sharing to be useful without undue delay. So the sharing will be disabled in this case.
Signed-off-by Waiman Long <[email protected]>---
include/linux/fs.h| 7 +++++++mm/hugetlb.c| 24 +++++++++++++++++++++---2 files changed, 28 insertions(+), 3 deletions(-)include/linux/fs.h +7
-0@@ -40,6 +40,7 @@#include <linux/fs_types.h> #include <linux/build_bug.h> #include <linux/stddef.h> +#include <linux/ktime.h> #include <asm/byteorder.h> #include <uapi/linux/fs.h> @@ -519,6 +520,12 @@ static inline void i_mmap_lock_write(struct address_space *mapping)down_write (&mapping->i_mmap_rwsem); } +static inline bool i_mmap_timedlock_write (struct address_space *mapping, +ktime_t timeout) +{ +return down_write_timedlock (&mapping->i_mmap_rwsem, timeout); +} +static inline void i_mmap_unlock_write (struct address_space *mapping) {up_write (&mapping->i_mmap_rwsem);mm/hugetlb.c +2
-0@@ -4750,6 +4750,8 @@ void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma, } } +#define PMD_SHARE_DISABLE_THRESHOLD (1 << 8) +/* * Search for a shareable pmd page for hugetlb. In any case calls pmd_alloc() * and returns the corresponding pte. While this is not necessary for the @@ -4770,11 +4772,24 @@ pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud)pte_t *spte =NULL ;mm/hugetlb.c pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud) · line 4780
Quoted 10 lines
mm/hugetlb.c +5
-2pte_t *pte;spinlock_t *ptl; +static atomic_t timeout_cnt; -if (!vma_shareable (vma, addr)) -return (pte_t *)pmd_alloc (mm, pud, addr); +/* +* Don't share if it is not sharable or locking attempt timed out +* after 10ms. After 256 timeouts, PMD sharing will be permanently +* disabled as it is just too slow. It looks like this kind of policy interacts with kernel debug options like KASAN (which is going to slow the system down anyway) could introduce tricky issues due to different timings on a debug kernel.
Quoted 7 lines
Quoted 4 lines
With respect to lockdep,
down_write_timedlock()works like a trylock. So a lot of checking will be skipped. Also the lockdep code won't be run until the lock is acquired. So its execution time has no effect on the timeout.No only lockdep, but also things like KASAN,
debug_pagealloc,page_poison, kmemleak, debug objects etc that all going to slow down things inhuge_pmd_share(), and make it tricky to get a right timeout value for those debug kernels without changing the previous behavior.
Right, I understand that. I will move to use a sysctl parameters for the timeout and then set its default value to either 10ms or 20ms if some debug options are detected. Usually the slower than should not be more than 2X.
Cheers,
Longman
Re: [PATCH 5/5] hugetlbfs: Limit wait time when trying to share huge PMD
Quoted 96 lines · Waiman Long
Quoted 91 lines · Qian Cai
Quoted 91 lines · Waiman Long
Quoted 75 lines · Qian Cai
Quoted 75 lines · Waiman Long
When allocating a large amount of static hugepages (~500-1500GB) on a system with large number of CPUs (4, 8 or even 16 sockets), performance degradation (random multi-second delays) was observed when thousands of processes are trying to fault in the data into the huge pages. The likelihood of the delay increases with the number of sockets and hence the CPUs a system has. This only happens in the initial setup phase and will be gone after all the necessary data are faulted in.
These random delays, however, are deemed unacceptable. The cause of that delay is the long wait time in acquiring the
mmap_semwhen trying to share the huge PMDs.To remove the unacceptable delays, we have to limit the amount of wait time on the
mmap_sem. So the newdown_write_timedlock()function is used to acquire the write lock on themmap_semwith a timeout value of 10ms which should not cause a perceivable delay. If timeout happens, the task will abandon its effort to share the PMD and allocate its own copy instead.When too many timeouts happens (threshold currently set at 256), the system may be too large for PMD sharing to be useful without undue delay. So the sharing will be disabled in this case.
Signed-off-by Waiman Long <[email protected]>---
include/linux/fs.h| 7 +++++++mm/hugetlb.c| 24 +++++++++++++++++++++---2 files changed, 28 insertions(+), 3 deletions(-)include/linux/fs.h +7
-0@@ -40,6 +40,7 @@#include <linux/fs_types.h> #include <linux/build_bug.h> #include <linux/stddef.h> +#include <linux/ktime.h> #include <asm/byteorder.h> #include <uapi/linux/fs.h> @@ -519,6 +520,12 @@ static inline void i_mmap_lock_write(struct address_space *mapping)down_write (&mapping->i_mmap_rwsem); } +static inline bool i_mmap_timedlock_write (struct address_space *mapping, +ktime_t timeout) +{ +return down_write_timedlock (&mapping->i_mmap_rwsem, timeout); +} +static inline void i_mmap_unlock_write (struct address_space *mapping) {up_write (&mapping->i_mmap_rwsem);mm/hugetlb.c +3
-0@@ -4750,6 +4750,8 @@ void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma, } } +#define PMD_SHARE_DISABLE_THRESHOLD (1 << 8) +/* * Search for a shareable pmd page for hugetlb. In any case calls pmd_alloc() * and returns the corresponding pte. While this is not necessary for the @@ -4770,11 +4772,24 @@ pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud)pte_t *spte =NULL ;pte_t *pte;spinlock_t *ptl; +static atomic_t timeout_cnt;Quoted 8 lines
mm/hugetlb.c pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud) · line 4780
Quoted 6 lines
mm/hugetlb.c +4
-2-if (!vma_shareable (vma, addr)) -return (pte_t *)pmd_alloc (mm, pud, addr); +/* +* Don't share if it is not sharable or locking attempt timed out +* after 10ms. After 256 timeouts, PMD sharing will be permanently +* disabled as it is just too slow. It looks like this kind of policy interacts with kernel debug options like KASAN (which is going to slow the system down anyway) could introduce tricky issues due to different timings on a debug kernel.
With respect to lockdep,
down_write_timedlock()works like a trylock. So a lot of checking will be skipped. Also the lockdep code won't be run until the lock is acquired. So its execution time has no effect on the timeout.
Quoted 8 lines
Quoted 3 lines
No only lockdep, but also things like KASAN,
debug_pagealloc,page_poison, kmemleak, debug objects etc that all going to slow down things inhuge_pmd_share(), and make it tricky to get a right timeout value for those debug kernels without changing the previous behavior.Right, I understand that. I will move to use a sysctl parameters for the timeout and then set its default value to either 10ms or 20ms if some debug options are detected. Usually the slower than should not be more than 2X.
That 2X is another magic number which has no testing data back for it. We need a way to disable timeout completely in Kconfig, so it can ship in the part of a debug kernel package.
Re: [PATCH 5/5] hugetlbfs: Limit wait time when trying to share huge PMD
In reply to Waiman LongQuoted 6 lines · Waiman Long
To remove the unacceptable delays, we have to limit the amount of wait time on the
mmap_sem. So the newdown_write_timedlock()function is used to acquire the write lock on themmap_semwith a timeout value of 10ms which should not cause a perceivable delay. If timeout happens, the task will abandon its effort to share the PMD and allocate its own copy instead.
If you do a v2, this is *NOT* the mmap_sem. It's the i_mmap_rwsem which protects a very different data structure from the mmap_sem.
Quoted 5 lines
include/linux/fs.h +5
-0+static inline bool i_mmap_timedlock_write (struct address_space *mapping, +ktime_t timeout) +{ +return down_write_timedlock (&mapping->i_mmap_rwsem, timeout); +}
Waiman Long
reviewerRe: [PATCH 5/5] hugetlbfs: Limit wait time when trying to share huge PMD
Quoted 10 lines · Matthew Wilcox
Quoted 6 lines · Waiman Long
To remove the unacceptable delays, we have to limit the amount of wait time on the
mmap_sem. So the newdown_write_timedlock()function is used to acquire the write lock on themmap_semwith a timeout value of 10ms which should not cause a perceivable delay. If timeout happens, the task will abandon its effort to share the PMD and allocate its own copy instead.If you do a v2, this is *NOT* the
mmap_sem. It's thei_mmap_rwsemwhich protects a very different data structure from themmap_sem.
Thanks for reminder. I should have read the code more carefully.
Cheers,
Longman
Re: [PATCH 5/5] hugetlbfs: Limit wait time when trying to share huge PMD
In reply to Waiman LongQuoted 19 lines · Waiman Long
When allocating a large amount of static hugepages (~500-1500GB) on a system with large number of CPUs (4, 8 or even 16 sockets), performance degradation (random multi-second delays) was observed when thousands of processes are trying to fault in the data into the huge pages. The likelihood of the delay increases with the number of sockets and hence the CPUs a system has. This only happens in the initial setup phase and will be gone after all the necessary data are faulted in.
These random delays, however, are deemed unacceptable. The cause of that delay is the long wait time in acquiring the
mmap_semwhen trying to share the huge PMDs.To remove the unacceptable delays, we have to limit the amount of wait time on the
mmap_sem. So the newdown_write_timedlock()function is used to acquire the write lock on themmap_semwith a timeout value of 10ms which should not cause a perceivable delay. If timeout happens, the task will abandon its effort to share the PMD and allocate its own copy instead.
<snip>
Quoted 28 lines
mm/hugetlb.c +10
-2@@ -4750,6 +4750,8 @@ void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma, } } +#define PMD_SHARE_DISABLE_THRESHOLD (1 << 8) +/* * Search for a shareable pmd page for hugetlb. In any case calls pmd_alloc() * and returns the corresponding pte. While this is not necessary for the @@ -4770,11 +4772,24 @@ pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud)pte_t *spte =NULL ;pte_t *pte;spinlock_t *ptl; +static atomic_t timeout_cnt; -if (!vma_shareable (vma, addr)) -return (pte_t *)pmd_alloc (mm, pud, addr); +/* +* Don't share if it is not sharable or locking attempt timed out +* after 10ms. After 256 timeouts, PMD sharing will be permanently +* disabled as it is just too slow. +*/ +if (!vma_shareable (vma, addr) || + (atomic_read (&timeout_cnt) >=PMD_SHARE_DISABLE_THRESHOLD ))
mm/hugetlb.c pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud) · line 4777
Quoted 10 lines
mm/hugetlb.c +8
-1+ goto out_no_share; + +if (!i_mmap_timedlock_write (mapping,ms_to_ktime (10 ))) { +if (atomic_inc_return (&timeout_cnt) == +PMD_SHARE_DISABLE_THRESHOLD ) +pr_info ("Hugetlbfs PMD sharing disabled because of timeouts!\n" ); + goto out_no_share; + } -i_mmap_lock_write (mapping);
All this got me wondering if we really need to take i_mmap_rwsem in write mode here. We are not changing the tree, only traversing it looking for a suitable vma.
Unless I am missing something, the hugetlb code only ever takes the semaphore in write mode; never read. Could this have been the result of changing the tree semaphore to read/write? Instead of analyzing all the code, the easiest and safest thing would have been to take all accesses in write mode.
I can investigate more, but wanted to ask the question in case someone already knows.
At one time, I thought it was safe to acquire the semaphore in read mode for huge_pmd_share, but write mode for huge_pmd_unshare. See commit b43a99900559. This was reverted along with another patch for other reasons.
If we change change from write to read mode, this may have significant impact on the stalls.
Signature · 1 line
Mike Kravetz
Re: [PATCH 5/5] hugetlbfs: Limit wait time when trying to share huge PMD
Quoted 8 lines · Mike Kravetz
All this got me wondering if we really need to take
i_mmap_rwsemin write mode here. We are not changing the tree, only traversing it looking for a suitable vma.Unless I am missing something, the hugetlb code only ever takes the semaphore in write mode; never read. Could this have been the result of changing the tree semaphore to read/write? Instead of analyzing all the code, the easiest and safest thing would have been to take all accesses in write mode.
I was wondering the same thing. It was changed here:
commit 83cde9e8ba95d180eaefefe834958fbf7008cf39
Author: Davidlohr Bueso <[email protected]> Date: Fri Dec 12 16:54:21 2014 -0800
mm: use new helper functions around the i_mmap_mutex
Convert all open coded mutex_lock/unlock calls to the i_mmap_[lock/unlock]_write() helpers.
and a subsequent patch said:
This conversion is straightforward. For now, all users take the write lock.
There were subsequent patches which changed a few places c8475d144abb1e62958cc5ec281d2a9e161c1946 1acf2e040721564d579297646862b8ea3dd4511b d28eb9c861f41aa2af4cfcc5eeeddff42b13d31e 874bfcaf79e39135cd31e1cfc9265cf5222d1ec3 3dec0ba0be6a532cac949e02b853021bf6d57dad
but I don't know why this one wasn't changed.
(I was also wondering about caching a potentially sharable page table in the address_space to avoid having to walk the VMA tree at all if that one happened to be sharable).
Re: [PATCH 5/5] hugetlbfs: Limit wait time when trying to share huge PMD
Quoted 26 lines · Matthew Wilcox
Quoted 8 lines · Mike Kravetz
All this got me wondering if we really need to take
i_mmap_rwsemin write mode here. We are not changing the tree, only traversing it looking for a suitable vma.Unless I am missing something, the hugetlb code only ever takes the semaphore in write mode; never read. Could this have been the result of changing the tree semaphore to read/write? Instead of analyzing all the code, the easiest and safest thing would have been to take all accesses in write mode.
I was wondering the same thing. It was changed here:
commit
83cde9e8ba95d180eaefefe834958fbf7008cf39Author: Davidlohr Bueso <[email protected]> Date: Fri Dec 12 16:54:21 2014 -0800mm: use new helper functions around the
i_mmap_mutexConvert all open coded mutex_lock/unlock calls to the i_mmap_[lock/unlock]_write() helpers.
and a subsequent patch said:
This conversion is straightforward. For now, all users take the write lock.
Quoted 8 lines
There were subsequent patches which changed a few places
c8475d144abb1e62958cc5ec281d2a9e161c19461acf2e040721564d579297646862b8ea3dd4511bd28eb9c861f41aa2af4cfcc5eeeddff42b13d31e874bfcaf79e39135cd31e1cfc9265cf5222d1ec33dec0ba0be6a532cac949e02b853021bf6d57dadbut I don't know why this one wasn't changed.
I cannot recall why huge_pmd_share() was not changed along with the other callers that don't modify the interval tree. By looking at the function, I agree that this could be shared, in fact this lock is much less involved than it's anon_vma counterpart, last I checked (perhaps with the exception of take_rmap_locks().
Quoted 4 lines
(I was also wondering about caching a potentially sharable page table in the
address_spaceto avoid having to walk the VMA tree at all if that one happened to be sharable).
I also think that the right solution is within the mm instead of adding a new api to rwsem and the extra complexity/overhead to osq _just_ for this case. We've managed to not need timeout extensions in our locking primitives thus far, which is a good thing imo.
Thanks,
Davidlohr
Waiman Long
reviewerRe: [PATCH 5/5] hugetlbfs: Limit wait time when trying to share huge PMD
Quoted 7 lines · Davidlohr Bueso
I also think that the right solution is within the mm instead of adding a new api to rwsem and the extra complexity/overhead to osq _just_ for this
case. We've managed to not need timeout extensions in our locking primitives
thus far, which is a good thing imo.
Adding a variant with timeout can be useful in resolving some potential deadlock issues found by lockdep. Anyway, there were talk about merging rt-mutex and regular mutex in the LPC last week. So we will need to have mutex_lock() variant with timeout for that to happen.
Cheers,
Longman
Waiman Long
reviewerRe: [PATCH 5/5] hugetlbfs: Limit wait time when trying to share huge PMD
In reply to Mike KravetzQuoted 71 lines · Mike Kravetz
Quoted 19 lines · Waiman Long
When allocating a large amount of static hugepages (~500-1500GB) on a system with large number of CPUs (4, 8 or even 16 sockets), performance degradation (random multi-second delays) was observed when thousands of processes are trying to fault in the data into the huge pages. The likelihood of the delay increases with the number of sockets and hence the CPUs a system has. This only happens in the initial setup phase and will be gone after all the necessary data are faulted in.
These random delays, however, are deemed unacceptable. The cause of that delay is the long wait time in acquiring the
mmap_semwhen trying to share the huge PMDs.To remove the unacceptable delays, we have to limit the amount of wait time on the
mmap_sem. So the newdown_write_timedlock()function is used to acquire the write lock on themmap_semwith a timeout value of 10ms which should not cause a perceivable delay. If timeout happens, the task will abandon its effort to share the PMD and allocate its own copy instead.<snip>
Quoted 28 lines
mm/hugetlb.c +10
-2@@ -4750,6 +4750,8 @@ void adjust_range_if_pmd_sharing_possible(struct vm_area_struct *vma, } } +#define PMD_SHARE_DISABLE_THRESHOLD (1 << 8) +/* * Search for a shareable pmd page for hugetlb. In any case calls pmd_alloc() * and returns the corresponding pte. While this is not necessary for the @@ -4770,11 +4772,24 @@ pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud)pte_t *spte =NULL ;pte_t *pte;spinlock_t *ptl; +static atomic_t timeout_cnt; -if (!vma_shareable (vma, addr)) -return (pte_t *)pmd_alloc (mm, pud, addr); +/* +* Don't share if it is not sharable or locking attempt timed out +* after 10ms. After 256 timeouts, PMD sharing will be permanently +* disabled as it is just too slow. +*/ +if (!vma_shareable (vma, addr) || + (atomic_read (&timeout_cnt) >=PMD_SHARE_DISABLE_THRESHOLD ))mm/hugetlb.c pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud) · line 4777
Quoted 10 lines
mm/hugetlb.c +8
-1+ goto out_no_share; + +if (!i_mmap_timedlock_write (mapping,ms_to_ktime (10 ))) { +if (atomic_inc_return (&timeout_cnt) == +PMD_SHARE_DISABLE_THRESHOLD ) +pr_info ("Hugetlbfs PMD sharing disabled because of timeouts!\n" ); + goto out_no_share; + } -i_mmap_lock_write (mapping);All this got me wondering if we really need to take
i_mmap_rwsemin write mode here. We are not changing the tree, only traversing it looking for a suitable vma.Unless I am missing something, the hugetlb code only ever takes the semaphore in write mode; never read. Could this have been the result of changing the tree semaphore to read/write? Instead of analyzing all the code, the easiest and safest thing would have been to take all accesses in write mode.
I can investigate more, but wanted to ask the question in case someone already knows.
Quoted 6 lines
At one time, I thought it was safe to acquire the semaphore in read mode for
huge_pmd_share, but write mode forhuge_pmd_unshare. See commitb43a99900559. This was reverted along with another patch for other reasons.If we change change from write to read mode, this may have significant impact on the stalls.
If we can take the rwsem in read mode, that should solve the problem AFAICS. As I don't have a full understanding of the history of that code, I didn't try to do that in my patch.
Cheers,
Longman
Re: [PATCH 5/5] hugetlbfs: Limit wait time when trying to share huge PMD
Quoted 3 lines · Waiman Long
If we can take the rwsem in read mode, that should solve the problem AFAICS. As I don't have a full understanding of the history of that code, I didn't try to do that in my patch.
Do you still have access to an environment that creates the long stalls? If so, can you try the simple change of taking the semaphore in read mode in huge_pmd_share.
Signature · 1 line
Mike Kravetz
Waiman Long
reviewerRe: [PATCH 5/5] hugetlbfs: Limit wait time when trying to share huge PMD
Quoted 8 lines · Mike Kravetz
Quoted 3 lines · Waiman Long
If we can take the rwsem in read mode, that should solve the problem AFAICS. As I don't have a full understanding of the history of that code, I didn't try to do that in my patch.
Do you still have access to an environment that creates the long stalls? If so, can you try the simple change of taking the semaphore in read mode in
huge_pmd_share.
That is what I am planning to do. I don't have an environment to reproduce the problem myself. I have to create a test kernel and ask the customer to try it out.
Cheers,
Longman
Re: [PATCH 5/5] hugetlbfs: Limit wait time when trying to share huge PMD
In reply to Waiman LongOn Wed, 11 Sep 2019 16:05:37 +0100
Quoted 19 lines
(quoted diff) +8
-2+#define PMD_SHARE_DISABLE_THRESHOLD (1 << 8) + /* * Search for a shareable pmd page for hugetlb. In any case calls pmd_alloc() * and returns the corresponding pte. While this is not necessary for the@@ -4770,11 +4772,24 @@ pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud) pte_t *spte = NULL; pte_t *pte; spinlock_t *ptl; + static atomic_t timeout_cnt; - if (!vma_shareable(vma, addr)) - return (pte_t *)pmd_alloc(mm, pud, addr); + /* + * Don't share if it is not sharable or locking attempt timed out + * after 10ms. After 256 timeouts, PMD sharing will be permanently + * disabled as it is just too slow. + */
Review pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud) · line 4791
Quoted 10 lines
(quoted diff) +10
-0+ if (!vma_shareable(vma, addr) || + (atomic_read(&timeout_cnt) >= PMD_SHARE_DISABLE_THRESHOLD)) + goto out_no_share; + + if (!i_mmap_timedlock_write(mapping, ms_to_ktime(10))) { + if (atomic_inc_return(&timeout_cnt) == + PMD_SHARE_DISABLE_THRESHOLD) + pr_info("Hugetlbfs PMD sharing disabled because of timeouts!\n"); + goto out_no_share; + }
atomic_dec_if_positive(&timeout_cnt);
The logic to permanently disable pmd sharing does not make much sense without anything like atomic_dec that would have been in their places, with 256 timeouts put aside.
Quoted 13 lines
(quoted diff) +3
-1- i_mmap_lock_write(mapping); vma_interval_tree_foreach(svma, &mapping->i_mmap, idx, idx) { if (svma == vma) continue;@@ -4806,6 +4821,9 @@ pte_t *huge_pmd_share(struct mm_struct *mm, unsigned long addr, pud_t *pud) pte = (pte_t *)pmd_alloc(mm, pud, addr); i_mmap_unlock_write(mapping); return pte; + +out_no_share: + return (pte_t *)pmd_alloc(mm, pud, addr); }
Quoted 12 lines · Waiman Long
A customer with large SMP systems (up to 16 sockets) with application that uses large amount of static hugepages (~500-1500GB) are experiencing random multisecond delays. These delays was caused by the long time it took to scan the VMA interval tree with
mmap_semheld.To fix this problem while perserving existing behavior as much as possible, we need to allow timeout in
down_write()and disabling PMD sharing when it is taking too long to do so. Since a transaction can involving touching multiple huge pages, timing out for each of the huge page interactions does not completely solve the problem. So a threshold is set to completely disable PMD sharing if too many timeouts happen.
Quoted 4 lines
The first 4 patches of this 5-patch series adds a new
down_write_timedlock()API which accepts a timeout argument and return true is locking is successful or false otherwise. It works more or less than adown_write_trylock()but the calling thread may sleep.
Just on general principle, this is a non-starter. If a lock is being held too long, then whatever the lock is protecting needs fixing. Adding timeouts to locks and sysctls to tune them is not a viable solution to address latencies caused by algorithm scalability issues.
Cheers,
Dave.
Signature · 2 lines
Dave Chinner [email protected]
Peter Zijlstra
maintainerQuoted 18 lines · Dave Chinner
Quoted 12 lines · Waiman Long
A customer with large SMP systems (up to 16 sockets) with application that uses large amount of static hugepages (~500-1500GB) are experiencing random multisecond delays. These delays was caused by the long time it took to scan the VMA interval tree with
mmap_semheld.To fix this problem while perserving existing behavior as much as possible, we need to allow timeout in
down_write()and disabling PMD sharing when it is taking too long to do so. Since a transaction can involving touching multiple huge pages, timing out for each of the huge page interactions does not completely solve the problem. So a threshold is set to completely disable PMD sharing if too many timeouts happen.Quoted 4 lines
The first 4 patches of this 5-patch series adds a new
down_write_timedlock()API which accepts a timeout argument and return true is locking is successful or false otherwise. It works more or less than adown_write_trylock()but the calling thread may sleep.
Quoted 5 lines
Just on general principle, this is a non-starter. If a lock is being held too long, then whatever the lock is protecting needs fixing. Adding timeouts to locks and sysctls to tune them is not a viable solution to address latencies caused by algorithm scalability issues.
I'm very much agreeing here. Lock functions with timeouts are a sign of horrific design.