From: Peter Zijlstra <peterz@infradead.org>
To: Ingo Molnar <mingo@kernel.org>
Cc: Linus Torvalds <torvalds@linux-foundation.org>,
Waiman Long <Waiman.Long@hp.com>, Ingo Molnar <mingo@elte.hu>,
Andrew Morton <akpm@linux-foundation.org>,
Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
Rik van Riel <riel@redhat.com>,
Peter Hurley <peter@hurleysoftware.com>,
Davidlohr Bueso <davidlohr.bueso@hp.com>,
Alex Shi <alex.shi@intel.com>,
Tim Chen <tim.c.chen@linux.intel.com>,
Andrea Arcangeli <aarcange@redhat.com>,
Matthew R Wilcox <matthew.r.wilcox@intel.com>,
Dave Hansen <dave.hansen@intel.com>,
Michel Lespinasse <walken@google.com>,
Andi Kleen <andi@firstfloor.org>,
"Chandramouleeswaran, Aswin" <aswin@hp.com>,
"Norton, Scott J" <scott.norton@hp.com>
Subject: Re: [PATCH, v2] anon_vmas: Convert the rwsem to an rwlock_t
Date: Mon, 30 Sep 2013 13:00:47 +0200 [thread overview]
Message-ID: <20130930110047.GY3081@twins.programming.kicks-ass.net> (raw)
In-Reply-To: <20130928195207.GA31245@gmail.com>
On Sat, Sep 28, 2013 at 09:52:07PM +0200, Ingo Molnar wrote:
> Index: tip/mm/rmap.c
> ===================================================================
> --- tip.orig/mm/rmap.c
> +++ tip/mm/rmap.c
> @@ -98,12 +98,12 @@ static inline void anon_vma_free(struct
> * page_lock_anon_vma_read() VS put_anon_vma()
> * down_read_trylock() atomic_dec_and_test()
> * LOCK MB
> - * atomic_read() rwsem_is_locked()
> + * atomic_read() rwlock_is_locked()
> *
> * LOCK should suffice since the actual taking of the lock must
> * happen _before_ what follows.
> */
> - if (rwsem_is_locked(&anon_vma->root->rwsem)) {
> + if (!write_can_lock(&anon_vma->root->rwlock)) {
> anon_vma_lock_write(anon_vma);
> anon_vma_unlock_write(anon_vma);
> }
> @@ -457,14 +457,14 @@ struct anon_vma *page_lock_anon_vma_read
>
> anon_vma = (struct anon_vma *) (anon_mapping - PAGE_MAPPING_ANON);
> root_anon_vma = ACCESS_ONCE(anon_vma->root);
> - if (down_read_trylock(&root_anon_vma->rwsem)) {
> + if (read_trylock(&root_anon_vma->rwlock)) {
> /*
> * If the page is still mapped, then this anon_vma is still
> * its anon_vma, and holding the mutex ensures that it will
> * not go away, see anon_vma_free().
> */
> if (!page_mapped(page)) {
> - up_read(&root_anon_vma->rwsem);
> + read_unlock(&root_anon_vma->rwlock);
> anon_vma = NULL;
> }
> goto out;
> @@ -1293,7 +1293,7 @@ out_mlock:
> /*
> * We need mmap_sem locking, Otherwise VM_LOCKED check makes
> * unstable result and race. Plus, We can't wait here because
> - * we now hold anon_vma->rwsem or mapping->i_mmap_mutex.
> + * we now hold anon_vma->rwlock or mapping->i_mmap_mutex.
> * if trylock failed, the page remain in evictable lru and later
> * vmscan could retry to move the page to unevictable lru if the
> * page is actually mlocked.
You can remove all that -- all that trickery was only needed because the
lock could sleep;
---
--- a/mm/rmap.c
+++ b/mm/rmap.c
@@ -85,29 +85,6 @@ static inline struct anon_vma *anon_vma_
static inline void anon_vma_free(struct anon_vma *anon_vma)
{
VM_BUG_ON(atomic_read(&anon_vma->refcount));
-
- /*
- * Synchronize against page_lock_anon_vma_read() such that
- * we can safely hold the lock without the anon_vma getting
- * freed.
- *
- * Relies on the full mb implied by the atomic_dec_and_test() from
- * put_anon_vma() against the acquire barrier implied by
- * down_read_trylock() from page_lock_anon_vma_read(). This orders:
- *
- * page_lock_anon_vma_read() VS put_anon_vma()
- * down_read_trylock() atomic_dec_and_test()
- * LOCK MB
- * atomic_read() rwlock_is_locked()
- *
- * LOCK should suffice since the actual taking of the lock must
- * happen _before_ what follows.
- */
- if (!write_can_lock(&anon_vma->root->rwlock)) {
- anon_vma_lock_write(anon_vma);
- anon_vma_unlock_write(anon_vma);
- }
-
kmem_cache_free(anon_vma_cachep, anon_vma);
}
@@ -437,15 +414,10 @@ struct anon_vma *page_get_anon_vma(struc
/*
* Similar to page_get_anon_vma() except it locks the anon_vma.
- *
- * Its a little more complex as it tries to keep the fast path to a single
- * atomic op -- the trylock. If we fail the trylock, we fall back to getting a
- * reference like with page_get_anon_vma() and then block on the mutex.
*/
struct anon_vma *page_lock_anon_vma_read(struct page *page)
{
struct anon_vma *anon_vma = NULL;
- struct anon_vma *root_anon_vma;
unsigned long anon_mapping;
rcu_read_lock();
@@ -456,51 +428,22 @@ struct anon_vma *page_lock_anon_vma_read
goto out;
anon_vma = (struct anon_vma *) (anon_mapping - PAGE_MAPPING_ANON);
- root_anon_vma = ACCESS_ONCE(anon_vma->root);
- if (read_trylock(&root_anon_vma->rwlock)) {
- /*
- * If the page is still mapped, then this anon_vma is still
- * its anon_vma, and holding the mutex ensures that it will
- * not go away, see anon_vma_free().
- */
- if (!page_mapped(page)) {
- read_unlock(&root_anon_vma->rwlock);
- anon_vma = NULL;
- }
- goto out;
- }
-
- /* trylock failed, we got to sleep */
- if (!atomic_inc_not_zero(&anon_vma->refcount)) {
- anon_vma = NULL;
- goto out;
- }
-
- if (!page_mapped(page)) {
- put_anon_vma(anon_vma);
- anon_vma = NULL;
- goto out;
- }
-
- /* we pinned the anon_vma, its safe to sleep */
- rcu_read_unlock();
anon_vma_lock_read(anon_vma);
- if (atomic_dec_and_test(&anon_vma->refcount)) {
- /*
- * Oops, we held the last refcount, release the lock
- * and bail -- can't simply use put_anon_vma() because
- * we'll deadlock on the anon_vma_lock_write() recursion.
- */
+ /*
+ * If this page is still mapped, then its anon_vma cannot have been
+ * freed. But if it has been unmapped, we have no security against the
+ * anon_vma structure being freed and reused (for another anon_vma:
+ * SLAB_DESTROY_BY_RCU guarantees that - so the atomic_inc_not_zero()
+ * above cannot corrupt).
+ */
+ if (!page_mapped(page)) {
anon_vma_unlock_read(anon_vma);
- __put_anon_vma(anon_vma);
anon_vma = NULL;
}
-
- return anon_vma;
-
out:
rcu_read_unlock();
+
return anon_vma;
}
next prev parent reply other threads:[~2013-09-30 11:01 UTC|newest]
Thread overview: 55+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-09-27 19:00 [PATCH] rwsem: reduce spinlock contention in wakeup code path Waiman Long
2013-09-27 19:28 ` Linus Torvalds
2013-09-27 19:39 ` Davidlohr Bueso
2013-09-27 21:49 ` Tim Chen
2013-09-28 6:45 ` Ingo Molnar
2013-09-28 7:41 ` Ingo Molnar
2013-09-28 18:55 ` Linus Torvalds
2013-09-28 19:13 ` Andi Kleen
2013-09-28 19:22 ` Linus Torvalds
2013-09-28 19:21 ` Ingo Molnar
2013-09-28 19:33 ` Linus Torvalds
2013-09-28 19:39 ` Ingo Molnar
2013-09-30 10:44 ` Peter Zijlstra
2013-09-30 16:13 ` Linus Torvalds
2013-09-30 16:41 ` Peter Zijlstra
2013-10-01 7:28 ` Ingo Molnar
2013-10-01 8:09 ` Peter Zijlstra
2013-10-01 8:25 ` Ingo Molnar
2013-09-30 15:58 ` Waiman Long
2013-10-01 7:33 ` Ingo Molnar
2013-10-01 20:03 ` Waiman Long
2013-09-28 19:37 ` [PATCH] anon_vmas: Convert the rwsem to an rwlock_t Ingo Molnar
2013-09-28 19:43 ` Linus Torvalds
2013-09-28 19:46 ` Ingo Molnar
2013-09-28 19:52 ` [PATCH, v2] " Ingo Molnar
2013-09-30 11:00 ` Peter Zijlstra [this message]
2013-09-30 11:44 ` Peter Zijlstra
2013-09-30 17:03 ` Andrew Morton
2013-09-30 17:25 ` Linus Torvalds
2013-09-30 17:10 ` Tim Chen
2013-09-30 18:14 ` Peter Zijlstra
2013-09-30 19:23 ` Tim Chen
2013-09-30 19:35 ` Waiman Long
2013-09-30 19:47 ` Tim Chen
2013-09-30 22:03 ` Tim Chen
2013-10-01 2:41 ` Waiman Long
2013-09-30 8:52 ` [PATCH] " Andrea Arcangeli
2013-09-30 14:40 ` Jerome Glisse
2013-09-30 16:26 ` Linus Torvalds
2013-09-30 19:16 ` Andrea Arcangeli
2013-09-30 18:21 ` Andi Kleen
2013-09-30 19:04 ` Jerome Glisse
2013-09-29 23:06 ` [PATCH] rwsem: reduce spinlock contention in wakeup code path Davidlohr Bueso
2013-09-29 23:26 ` Linus Torvalds
2013-09-30 0:40 ` Davidlohr Bueso
2013-09-30 0:51 ` Linus Torvalds
2013-09-30 6:57 ` Ingo Molnar
2013-09-30 1:11 ` Michel Lespinasse
2013-09-30 7:05 ` Ingo Molnar
2013-09-30 16:03 ` Waiman Long
2013-09-30 10:46 ` Peter Zijlstra
2013-10-01 7:48 ` Ingo Molnar
2013-10-01 8:10 ` Peter Zijlstra
2013-09-27 19:32 ` Peter Hurley
2013-09-28 0:46 ` Waiman Long
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20130930110047.GY3081@twins.programming.kicks-ass.net \
--to=peterz@infradead.org \
--cc=Waiman.Long@hp.com \
--cc=aarcange@redhat.com \
--cc=akpm@linux-foundation.org \
--cc=alex.shi@intel.com \
--cc=andi@firstfloor.org \
--cc=aswin@hp.com \
--cc=dave.hansen@intel.com \
--cc=davidlohr.bueso@hp.com \
--cc=linux-kernel@vger.kernel.org \
--cc=matthew.r.wilcox@intel.com \
--cc=mingo@elte.hu \
--cc=mingo@kernel.org \
--cc=peter@hurleysoftware.com \
--cc=riel@redhat.com \
--cc=scott.norton@hp.com \
--cc=tim.c.chen@linux.intel.com \
--cc=torvalds@linux-foundation.org \
--cc=walken@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Powered by JetHome