Re: [PATCH -v2] rmap: make anon_vma_prepare link in all theanon_vmas of a mergeable VMA

From: Peter Zijlstra
Date: Mon Apr 12 2010 - 15:30:30 EST


On Mon, 2010-04-12 at 20:40 +0200, Peter Zijlstra wrote:

Hmm, if interleaved like so

> struct anon_vma *page_lock_anon_vma(struct page *page)
> {
> struct anon_vma *anon_vma;
> unsigned long anon_mapping;

page_remove_rmap()
anon_vma_unlink()
anon_vma_free()

So that the below will all observe the old page->mapping:

> rcu_read_lock();
> again:
> anon_mapping = (unsigned long)rcu_dereference(page->mapping);
> if ((anon_mapping & PAGE_MAPPING_FLAGS) != PAGE_MAPPING_ANON)
> goto out;
> anon_vma = (struct anon_vma *)(anon_mapping - PAGE_MAPPING_ANON);
>
> /*
> * The RCU read lock ensures we can safely dereference anon_vma
> * since it ensures the backing slab won't go away. It will however
> * not guarantee it's the right object.
> *
> * First take the anon_vma->lock, this will, per anon_vma_unlink()
> * avoid this anon_vma from being freed if it is a valid object.
> */
> spin_lock(&anon_vma->lock);
>
> /*
> * Secondly, we have to re-read page->mapping, so ensure it
> * has not changed, rely on spin_lock() being at least a
> * compiler barrier to force the re-read.
> */
> if (unlikely(page_rmapping(page) != anon_vma)) {
> spin_unlock(&anon_vma->lock);
> goto again;
> }

page_add_anon_rmap(), so that the page_mapped() test below would be
positive,

> /*
> * Ensure we read page->mapping before page->_mapcount,
> * orders against atomic_add_negative() in page_remove_rmap().
> */
> smp_rmb();
>
> /*
> * Finally check that the page is still mapped,
> * if not, this can't possibly be the right anon_vma.
> */
> if (!page_mapped(page))
> goto unlock;

We could here return a non-valid and already freed anon_vma.

> return anon_vma;
>
> unlock:
> spin_unlock(&anon_vma->lock);
> out:
> rcu_read_unlock();
> return NULL;
> }
>
>

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/