futex: lockless get_futex_key
v6 cover futex: lockless get_futex_key
This is the continuation of the original patch, except with Ingo's feedback about the ordering comments we have and how we reference them throughout the code. Please consider for v4.6.
Thanks!
Changes from v5:
- Added patch 1 (Ingo).
Changes from v4:
- Integrated feedback from Hugh, specifically:
* check for page->mapping change with page lock held when detecting shm race.
* move inode assign, if the mapping still matches what we expect should we have a valid inode.
* move iput out of rcu lock.
Changes from v3:
- Redo mapping sanity check, now do not halt the kernel.
Changes from v2:
- Minor adjustments by peterz.
- Applies on top of -next-20160118
Changes from v1:
- Remove unnecesary mb, as atomic_inc returning does what we need.
- Fix bogus mapping load.
- Minor code cleanups/comments.
Davidlohr Bueso (2):
futex: Rename barrier references in ordering guarantees
futex: Remove requirement for lock_page in get_futex_key
kernel/futex.c | 131 ++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 107 insertions(+), 24 deletions(-)
Signature · 1 line
2.1.4
PATCH v6 1/2 futex: Rename barrier references in ordering guarantees
Ingo suggested we rename how we reference barriers A and B regarding futex ordering guarantees. This patch replaces, for both barriers, MB (A) with smp_mb(); (A), such that:
- We explicitly state that the barriers are SMP, and
- We standardize how we reference these across futex.c helping readers follow what barrier does what and where.
--- kernel/futex.c | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-)
kernel/futex.c +17 -17
@@ -124,16 +124,16 @@* futex_wait(futex, val); * * waiters++; (a) -* mb(); (A) <-- paired with -. -* | -* lock(hash_bucket(futex)); | -* | -* uval = *futex; | -* | *futex = newval; -* | sys_futex(WAKE, futex); -* | futex_wake(futex); -* | -* `-------> mb(); (B) +* smp_mb(); (A) <-- paired with -. +* | +* lock(hash_bucket(futex)); | +* | +* uval = *futex; | +* | *futex = newval; +* | sys_futex(WAKE, futex); +* | futex_wake(futex); +* | +* `--------> smp_mb(); (B) * if (uval == val) * queue(); * unlock(hash_bucket(futex));
@@ -334,7 +334,7 @@ static inline void futex_get_mm(union futex_key *key)/* * Ensure futex_get_mm() implies a full barrier such that * get_futex_key() implies a full barrier. This is relied upon -* as full barrier (B), see the ordering comment above. +* as smp_mb(); (B), see the ordering comment above. */ smp_mb__after_atomic (); }
@@ -407,10 +407,10 @@ static void get_futex_key_refs(union futex_key *key)switch (key->both.offset & (FUT_OFF_INODE |FUT_OFF_MMSHARED )) {case FUT_OFF_INODE : -ihold (key->shared.inode);/* implies MB (B) */ +ihold (key->shared.inode);/* implies smp_mb(); (B) */ break ;case FUT_OFF_MMSHARED : -futex_get_mm (key);/* implies MB (B) */ +futex_get_mm (key);/* implies smp_mb(); (B) */ break ;default :/*
@@ -418,7 +418,7 @@ static void get_futex_key_refs(union futex_key *key)* mm, therefore the only purpose of calling get_futex_key_refs * is because we need the barrier for the lockless waiter check. */ -smp_mb ();/* explicit MB (B) */ +smp_mb ();/* explicit smp_mb(); (B) */ } }
@@ -497,7 +497,7 @@ get_futex_key(u32 __user *uaddr, int fshared, union futex_key *key, int rw)if (!fshared) { key->private.mm = mm; key->private.address = address; -get_futex_key_refs (key);/* implies MB (B) */ +get_futex_key_refs (key);/* implies smp_mb(); (B) */ return 0 ; }
@@ -572,7 +572,7 @@ again: key->shared.pgoff =basepage_index (page); } -get_futex_key_refs (key);/* implies MB (B) */ +get_futex_key_refs (key);/* implies smp_mb(); (B) */ out:unlock_page (page);
@@ -1864,7 +1864,7 @@ static inline struct futex_hash_bucket *queue_lock(struct futex_q *q) q->lock_ptr = &hb->lock; -spin_lock (&hb->lock);/* implies MB (A) */ +spin_lock (&hb->lock);/* implies smp_mb(); (A) */ return hb; }
Signature · 1 line
2.1.4
tip-bot for Davidlohr Bueso
merge bot[tip:locking/core] futex: Rename barrier references in ordering guarantees
futex: Rename barrier references in ordering guarantees
Commit-ID: 8ad7b378d0d016309014cae0f640434bca7b5e11 Gitweb: http://git.kernel.org/tip/8ad7b378d0d016309014cae0f640434bca7b5e11 Author: Davidlohr Bueso <[email protected]> AuthorDate: Tue, 9 Feb 2016 11:15:13 -0800 Committer: Ingo Molnar <[email protected]> CommitDate: Wed, 17 Feb 2016 10:42:17 +0100
futex: Rename barrier references in ordering guarantees
Ingo suggested we rename how we reference barriers A and B regarding futex ordering guarantees. This patch replaces, for both barriers, MB (A) with smp_mb(); (A), such that:
- We explicitly state that the barriers are SMP, and
- We standardize how we reference these across futex.c helping readers follow what barrier does what and where.
--- kernel/futex.c | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-)
kernel/futex.c +17 -17
@@ -124,16 +124,16 @@* futex_wait(futex, val); * * waiters++; (a) -* mb(); (A) <-- paired with -. -* | -* lock(hash_bucket(futex)); | -* | -* uval = *futex; | -* | *futex = newval; -* | sys_futex(WAKE, futex); -* | futex_wake(futex); -* | -* `-------> mb(); (B) +* smp_mb(); (A) <-- paired with -. +* | +* lock(hash_bucket(futex)); | +* | +* uval = *futex; | +* | *futex = newval; +* | sys_futex(WAKE, futex); +* | futex_wake(futex); +* | +* `--------> smp_mb(); (B) * if (uval == val) * queue(); * unlock(hash_bucket(futex));
@@ -334,7 +334,7 @@ static inline void futex_get_mm(union futex_key *key)/* * Ensure futex_get_mm() implies a full barrier such that * get_futex_key() implies a full barrier. This is relied upon -* as full barrier (B), see the ordering comment above. +* as smp_mb(); (B), see the ordering comment above. */ smp_mb__after_atomic (); }
@@ -407,10 +407,10 @@ static void get_futex_key_refs(union futex_key *key)switch (key->both.offset & (FUT_OFF_INODE |FUT_OFF_MMSHARED )) {case FUT_OFF_INODE : -ihold (key->shared.inode);/* implies MB (B) */ +ihold (key->shared.inode);/* implies smp_mb(); (B) */ break ;case FUT_OFF_MMSHARED : -futex_get_mm (key);/* implies MB (B) */ +futex_get_mm (key);/* implies smp_mb(); (B) */ break ;default :/*
@@ -418,7 +418,7 @@ static void get_futex_key_refs(union futex_key *key)* mm, therefore the only purpose of calling get_futex_key_refs * is because we need the barrier for the lockless waiter check. */ -smp_mb ();/* explicit MB (B) */ +smp_mb ();/* explicit smp_mb(); (B) */ } }
@@ -497,7 +497,7 @@ get_futex_key(u32 __user *uaddr, int fshared, union futex_key *key, int rw)if (!fshared) { key->private.mm = mm; key->private.address = address; -get_futex_key_refs (key);/* implies MB (B) */ +get_futex_key_refs (key);/* implies smp_mb(); (B) */ return 0 ; }
@@ -572,7 +572,7 @@ again: key->shared.pgoff =basepage_index (page); } -get_futex_key_refs (key);/* implies MB (B) */ +get_futex_key_refs (key);/* implies smp_mb(); (B) */ out:unlock_page (page);
@@ -1864,7 +1864,7 @@ static inline struct futex_hash_bucket *queue_lock(struct futex_q *q) q->lock_ptr = &hb->lock; -spin_lock (&hb->lock);/* implies MB (A) */ +spin_lock (&hb->lock);/* implies smp_mb(); (A) */ return hb; }
PATCH v6 2/2 futex: Remove requirement for lock_page in get_futex_key
In reply to Davidlohr BuesoFrom: Mel Gorman <[email protected]>
When dealing with key handling for shared futexes, we can drastically reduce the usage/need of the page lock. 1) For anonymous pages, the associated futex object is the mm_struct which does not require the page lock. 2) For inode based, keys, we can check under RCU read lock if the page mapping is still valid and take reference to the inode. This just leaves one rare race that requires the page lock in the slow path when examining the swapcache.
Additionally realtime users currently have a problem with the page lock being contended for unbounded periods of time during futex operations.
Task A
get_futex_key()
lock_page()
---> preemptedNow any other task trying to lock that page will have to wait until task A gets scheduled back in, which is an unbound time.
With this patch, we pretty much have a lockless futex_get_key().
Experiments show that this patch can boost/speedup the hashing of shared futexes with the perf futex benchmarks (which is good for measuring such change) by up to 45% when there are high (> 100) thread counts on a 60 core Westmere. Lower counts are pretty much in the noise range or less than 10%, but mid range can be seen at over 30% overall throughput (hash ops/sec). This makes anon-mem shared futexes much closer to its private counterpart.
--- kernel/futex.c | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 91 insertions(+), 8 deletions(-)
kernel/futex.c +91 -8
@@ -520,7 +520,20 @@ again:else err =0 ; -lock_page (page); +/* +* The treatment of mapping from this point on is critical. The page +* lock protects many things but in this context the page lock +* stabilizes mapping, prevents inode freeing in the shared +* file-backed region case and guards against movement to swap cache. +* +* Strictly speaking the page lock is not needed in all cases being +* considered here and page lock forces unnecessarily serialization +* From this point on, mapping will be re-verified if necessary and +* page lock will be acquired only if it is unavoidable +*/ + page =compound_head (page); + mapping =READ_ONCE (page->mapping); +/* * If page->mapping is NULL, then it cannot be a PageAnon * page; but it might be the ZERO_PAGE or in the gate area or
@@ -536,19 +549,31 @@ again:* shmem_writepage move it from filecache to swapcache beneath us: * an unlikely race, but we do need to retry for page->mapping. */ - mapping =compound_head (page)->mapping; -if (!mapping) { -int shmem_swizzled =PageSwapCache (page); +if (unlikely (!mapping)) { +int shmem_swizzled; + +/* +* Page lock is required to identify which special case above +* applies. If this is really a shmem page then the page lock +* will prevent unexpected transitions. +*/ +lock_page (page); + shmem_swizzled =PageSwapCache (page) || page->mapping;unlock_page (page);put_page (page); +if (shmem_swizzled) goto again; +return -EFAULT ; }/* * Private mappings are handled in a simple way. * +* If the futex key is stored on an anonymous page, then the associated +* object is the mm which is implicitly pinned by the calling process. +* * NOTE: When userspace waits on a MAP_SHARED mapping, even if * it's a read-only handle, it's expected that futexes attach to * the object not the particular process.
@@ -566,16 +591,74 @@ again: key->both.offset |=FUT_OFF_MMSHARED ;/* ref taken on mm */ key->private.mm = mm; key->private.address = address; + +get_futex_key_refs (key);/* implies smp_mb(); (B) */ + }else { +struct inode *inode; + +/* +* The associated futex object in this case is the inode and +* the page->mapping must be traversed. Ordinarily this should +* be stabilised under page lock but it's not strictly +* necessary in this case as we just want to pin the inode, not +* update the radix tree or anything like that. +* +* The RCU read lock is taken as the inode is finally freed +* under RCU. If the mapping still matches expectations then the +* mapping->host can be safely accessed as being a valid inode. +*/ +rcu_read_lock (); + +if (READ_ONCE (page->mapping) != mapping) { +rcu_read_unlock (); +put_page (page); + + goto again; + } + + inode =READ_ONCE (mapping->host); +if (!inode) { +rcu_read_unlock (); +put_page (page); + + goto again; + } + +/* +* Take a reference unless it is about to be freed. Previously +* this reference was taken by ihold under the page lock +* pinning the inode in place so i_lock was unnecessary. The +* only way for this check to fail is if the inode was +* truncated in parallel so warn for now if this happens. +* +* We are not calling into get_futex_key_refs() in file-backed +* cases, therefore a successful atomic_inc return below will +* guarantee that get_futex_key() will still imply smp_mb(); (B). +*/ +if (WARN_ON_ONCE (!atomic_inc_not_zero (&inode->i_count))) { +rcu_read_unlock (); +put_page (page); + + goto again; + } + +/* Should be impossible but lets be paranoid for now */ +if (WARN_ON_ONCE (inode->i_mapping != mapping)) { + err = -EFAULT ; +rcu_read_unlock (); +iput (inode); + + goto out; + } + key->both.offset |=FUT_OFF_INODE ;/* inode-based key */ - key->shared.inode = mapping->host; + key->shared.inode = inode; key->shared.pgoff =basepage_index (page); +rcu_read_unlock (); } -get_futex_key_refs (key);/* implies smp_mb(); (B) */ - out: -unlock_page (page);put_page (page);return err; }
Signature · 1 line
2.1.4
tip-bot for Mel Gorman
merge bot[tip:locking/core] futex: Remove requirement for lock_page() in get_futex_key()
futex: Remove requirement for lock_page() in get_futex_key()
Commit-ID: 65d8fc777f6dcfee12785c057a6b57f679641c90 Gitweb: http://git.kernel.org/tip/65d8fc777f6dcfee12785c057a6b57f679641c90 Author: Mel Gorman <[email protected]> AuthorDate: Tue, 9 Feb 2016 11:15:14 -0800 Committer: Ingo Molnar <[email protected]> CommitDate: Wed, 17 Feb 2016 10:42:17 +0100
futex: Remove requirement for lock_page() in get_futex_key()
When dealing with key handling for shared futexes, we can drastically reduce the usage/need of the page lock. 1) For anonymous pages, the associated futex object is the mm_struct which does not require the page lock. 2) For inode based, keys, we can check under RCU read lock if the page mapping is still valid and take reference to the inode. This just leaves one rare race that requires the page lock in the slow path when examining the swapcache.
Additionally realtime users currently have a problem with the page lock being contended for unbounded periods of time during futex operations.
Task A
get_futex_key()
lock_page()
---> preemptedNow any other task trying to lock that page will have to wait until task A gets scheduled back in, which is an unbound time.
With this patch, we pretty much have a lockless futex_get_key().
Experiments show that this patch can boost/speedup the hashing of shared futexes with the perf futex benchmarks (which is good for measuring such change) by up to 45% when there are high (> 100) thread counts on a 60 core Westmere. Lower counts are pretty much in the noise range or less than 10%, but mid range can be seen at over 30% overall throughput (hash ops/sec). This makes anon-mem shared futexes much closer to its private counterpart.
--- kernel/futex.c | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 91 insertions(+), 8 deletions(-)
kernel/futex.c +91 -8
@@ -520,7 +520,20 @@ again:else err =0 ; -lock_page (page); +/* +* The treatment of mapping from this point on is critical. The page +* lock protects many things but in this context the page lock +* stabilizes mapping, prevents inode freeing in the shared +* file-backed region case and guards against movement to swap cache. +* +* Strictly speaking the page lock is not needed in all cases being +* considered here and page lock forces unnecessarily serialization +* From this point on, mapping will be re-verified if necessary and +* page lock will be acquired only if it is unavoidable +*/ + page =compound_head (page); + mapping =READ_ONCE (page->mapping); +/* * If page->mapping is NULL, then it cannot be a PageAnon * page; but it might be the ZERO_PAGE or in the gate area or
@@ -536,19 +549,31 @@ again:* shmem_writepage move it from filecache to swapcache beneath us: * an unlikely race, but we do need to retry for page->mapping. */ - mapping =compound_head (page)->mapping; -if (!mapping) { -int shmem_swizzled =PageSwapCache (page); +if (unlikely (!mapping)) { +int shmem_swizzled; + +/* +* Page lock is required to identify which special case above +* applies. If this is really a shmem page then the page lock +* will prevent unexpected transitions. +*/ +lock_page (page); + shmem_swizzled =PageSwapCache (page) || page->mapping;unlock_page (page);put_page (page); +if (shmem_swizzled) goto again; +return -EFAULT ; }/* * Private mappings are handled in a simple way. * +* If the futex key is stored on an anonymous page, then the associated +* object is the mm which is implicitly pinned by the calling process. +* * NOTE: When userspace waits on a MAP_SHARED mapping, even if * it's a read-only handle, it's expected that futexes attach to * the object not the particular process.
@@ -566,16 +591,74 @@ again: key->both.offset |=FUT_OFF_MMSHARED ;/* ref taken on mm */ key->private.mm = mm; key->private.address = address; + +get_futex_key_refs (key);/* implies smp_mb(); (B) */ + }else { +struct inode *inode; + +/* +* The associated futex object in this case is the inode and +* the page->mapping must be traversed. Ordinarily this should +* be stabilised under page lock but it's not strictly +* necessary in this case as we just want to pin the inode, not +* update the radix tree or anything like that. +* +* The RCU read lock is taken as the inode is finally freed +* under RCU. If the mapping still matches expectations then the +* mapping->host can be safely accessed as being a valid inode. +*/ +rcu_read_lock (); + +if (READ_ONCE (page->mapping) != mapping) { +rcu_read_unlock (); +put_page (page); + + goto again; + } + + inode =READ_ONCE (mapping->host); +if (!inode) { +rcu_read_unlock (); +put_page (page); + + goto again; + } + +/* +* Take a reference unless it is about to be freed. Previously +* this reference was taken by ihold under the page lock +* pinning the inode in place so i_lock was unnecessary. The +* only way for this check to fail is if the inode was +* truncated in parallel so warn for now if this happens. +* +* We are not calling into get_futex_key_refs() in file-backed +* cases, therefore a successful atomic_inc return below will +* guarantee that get_futex_key() will still imply smp_mb(); (B). +*/ +if (WARN_ON_ONCE (!atomic_inc_not_zero (&inode->i_count))) { +rcu_read_unlock (); +put_page (page); + + goto again; + } + +/* Should be impossible but lets be paranoid for now */ +if (WARN_ON_ONCE (inode->i_mapping != mapping)) { + err = -EFAULT ; +rcu_read_unlock (); +iput (inode); + + goto out; + } + key->both.offset |=FUT_OFF_INODE ;/* inode-based key */ - key->shared.inode = mapping->host; + key->shared.inode = inode; key->shared.pgoff =basepage_index (page); +rcu_read_unlock (); } -get_futex_key_refs (key);/* implies smp_mb(); (B) */ - out: -unlock_page (page);put_page (page);return err; }