lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <45ecc4be-8a81-48b2-a51e-d5ca1aaf62d2@lucifer.local>
Date: Mon, 8 Sep 2025 05:29:49 +0100
From: Lorenzo Stoakes <lorenzo.stoakes@...cle.com>
To: Yajun Deng <yajun.deng@...ux.dev>
Cc: akpm@...ux-foundation.org, david@...hat.com, riel@...riel.com,
        Liam.Howlett@...cle.com, vbabka@...e.cz, harry.yoo@...cle.com,
        linux-mm@...ck.org, linux-kernel@...r.kernel.org
Subject: Re: [PATCH] mm/rmap: make num_children and num_active_vmas update in
 internally

I guess I wasn't clear enough. NAK to this whole concept. AFAICT there's
nothing of value here and you're fiddling with enormously sensitive
code.

Your first patch is incredibly broken and you've obviously not done any
testing whatsoever. This is not encouraging for code this sensitive, so If
you _insist_ on sending a respin anyway, send it as an RFC please (as this
patch should have been!)

Thanks.

On Sat, Sep 06, 2025 at 02:50:17PM +0000, Yajun Deng wrote:
> September 5, 2025 at 10:58 PM, "Lorenzo Stoakes" <lorenzo.stoakes@...cle.com mailto:lorenzo.stoakes@...cle.com?to=%22Lorenzo%20Stoakes%22%20%3Clorenzo.stoakes%40oracle.com%3E > wrote:
>
>
> >
> > On Fri, Sep 05, 2025 at 01:20:19PM +0000, Yajun Deng wrote:
> >
> > >
> > > If the anon_vma_alloc() is called, the num_children of the parent of
> > >  the anon_vma will be updated. But this operation occurs outside of
> > >  anon_vma_alloc().
> > >
> > I don't like that what is supposed to be an allocation function is now also
> > doing additional work.
> >
> > And there's literally only one place where this matters, since
> > __anon_vma_prepare() doesn't have a parent, it's ltierally only anon_vma_fork().
> >
> > And there are only 2 callers, so I don't see the purpose in doing this?
> >
>
> Yes, it is an allocation function. Some code doing like it.
> alloc_fair_sched_group() and alloc_rt_sched_group() are allocation functions.
> They are also pass the parent parameter to them.
>
> The purpose of this is to unify the code. They are essentially the same.
> One has itself as its parent, while another has a real parent.

To unify code that doesn't need unifying and moving logic specific to parts of
the code to a general place for no reason?

No, please just drop this idea altogether thanks.

>
> > >
> > > The num_active_vmas are also updated outside of anon_vma.
> > >
> > I don't know what 'outside of anon_vma' means?
> >
> It means num_active_vmas should be updated in a function.
>
> > >
> > > Pass the parent of anon_vma to the anon_vma_alloc() and update the
> > >  num_children inside it.
> > >
> > >  Introduce anon_vma_attach() and anon_vma_detach() to update
> > >  num_active_vmas with the anon_vma.
> > >
> > I really dislike this naming - VMA detachment means something entirely
> > different, you're not really 'attaching' or 'detaching' anything you're just
> > changing a single stat of the ones you'd need to change were you truly doing so
> > etc. etc.
> >
> > It's misleading.
> >
>
> I will use vma_attach_anon() and vma_detach_anon().

No. Sigh. The problem with patches like these is you end up
writing-the-code-via-review which is really not time efficient for
maintainers.

I'm really not sure there's any value here, but vma_add_anon_child(),
vma_remove_anon_child() would be _much_ better.

- No confusion vs. VMA attach/detach concept.
- Doesn't pretend to do everything needed to add a new child.

>
> > > Signed-off-by: Yajun Deng <yajun.deng@...ux.dev>
> > >
> > Did you test this at all? It causes an immediate kernel panic for me when I run
> > it in a VM:
> >
> > In exit_mmap() -> free->pgtables() -> unlink_anon_vmas()
> >
> > I haven't really dug into why but yeah, this is broken.
> >
>
> I will fix it.

Please make sure to at least run your code locally before sending a patch,
thanks.

>
> > >
> > > ---
> > >  mm/rmap.c | 63 ++++++++++++++++++++++++++++---------------------------
> > >  1 file changed, 32 insertions(+), 31 deletions(-)
> > >
> > >  diff --git a/mm/rmap.c b/mm/rmap.c
> > >  index 34333ae3bd80..2a28edfa5734 100644
> > >  --- a/mm/rmap.c
> > >  +++ b/mm/rmap.c
> > >  @@ -86,15 +86,21 @@
> > >  static struct kmem_cache *anon_vma_cachep;
> > >  static struct kmem_cache *anon_vma_chain_cachep;
> > >
> > >  -static inline struct anon_vma *anon_vma_alloc(void)
> > >  +static inline struct anon_vma *anon_vma_alloc(struct anon_vma *parent)
> > >
> > I really dislike this, we only allocate with a parent in the fork case, this is
> > just not a win imo + adds confusion.
> >
>
> I will add a wapper function.

No, just drop this idea altogether thanks.

>
> > >
> > > {
> > >  struct anon_vma *anon_vma;
> > >
> > >  anon_vma = kmem_cache_alloc(anon_vma_cachep, GFP_KERNEL);
> > >  - if (anon_vma) {
> > >  - atomic_set(&anon_vma->refcount, 1);
> > >  - anon_vma->num_children = 0;
> > >  - anon_vma->num_active_vmas = 0;
> > >  + if (!anon_vma)
> > >  + return NULL;
> > >  +
> > >  + atomic_set(&anon_vma->refcount, 1);
> > >  + anon_vma->num_children = 0;
> > >  + anon_vma->num_active_vmas = 0;
> > >  + if (parent) {
> > >  + anon_vma->parent = parent;
> > >  + anon_vma->root = parent->root;
> > >
> > You are accessing parent fields without a lock. This is not good.

Ahem ^

> >
> > >
> > > + } else {
> > >  anon_vma->parent = anon_vma;
> > >  /*
> > >  * Initialise the anon_vma root to point to itself. If called
> > >  @@ -102,6 +108,7 @@ static inline struct anon_vma *anon_vma_alloc(void)
> > >  */
> > >  anon_vma->root = anon_vma;
> > >  }
> > >  + anon_vma->parent->num_children++;
> > >
> > This is even even even more not good, because you're accessing the parent
> > without a lock, which is just completely broken.
> >
> > I note below where you're doing this.
> >
>
> I will add anon_vma_lock_write() and anon_vma_unlock_write().

No, please don't... things are locked by callers at the appropriate time so
you'll just deadlock like this.

Your issue is you're trying to 'unify' code in a way that isn't workable.

> > >
> > > return anon_vma;
> > >  }
> > >  @@ -146,6 +153,19 @@ static void anon_vma_chain_free(struct anon_vma_chain *anon_vma_chain)
> > >  kmem_cache_free(anon_vma_chain_cachep, anon_vma_chain);
> > >  }
> > >
> > >  +static inline void anon_vma_attach(struct vm_area_struct *vma,
> > >  + struct anon_vma *anon_vma)
> > >  +{
> > >  + vma->anon_vma = anon_vma;
> > >  + vma->anon_vma->num_active_vmas++;
> > >  +}
> > >
> > Yeah I hate the naming, you should have lock asserts here, I don't love that
> > we're pairing the vma and anon_vma like this, and again I just really question
> > the value of this.
> >
> anon_vma_chain_link() also didn't have lock assert.

I'm not sure what your point is?

Presumably you doing this patch is based on an assumption code needs
improvement, and then you question a suggestion based upon... an assumption
everything is currently perfect?

>
> > >
> > > +
> > >  +static inline void anon_vma_detach(struct vm_area_struct *vma)
> > >  +{
> > >  + vma->anon_vma->num_active_vmas--;
> > >  + vma->anon_vma = NULL;
> > >  +}
> > >  +
> > >  static void anon_vma_chain_link(struct vm_area_struct *vma,
> > >  struct anon_vma_chain *avc,
> > >  struct anon_vma *anon_vma)
> > >  @@ -198,10 +218,9 @@ int __anon_vma_prepare(struct vm_area_struct *vma)
> > >  anon_vma = find_mergeable_anon_vma(vma);
> > >  allocated = NULL;
> > >  if (!anon_vma) {
> > >  - anon_vma = anon_vma_alloc();
> > >  + anon_vma = anon_vma_alloc(NULL);
> > >
> > This 'arbitrary parameter which is NULL' is also pretty horrible.
> >
>
> I will add a wapper function.

No, just do not do this at all, thanks.

>
> > >
> > > if (unlikely(!anon_vma))
> > >  goto out_enomem_free_avc;
> > >  - anon_vma->num_children++; /* self-parent link for new root */
> > >  allocated = anon_vma;
> > >  }
> > >
> > >  @@ -209,9 +228,8 @@ int __anon_vma_prepare(struct vm_area_struct *vma)
> > >  /* page_table_lock to protect against threads */
> > >  spin_lock(&mm->page_table_lock);
> > >  if (likely(!vma->anon_vma)) {
> > >  - vma->anon_vma = anon_vma;
> > >  + anon_vma_attach(vma, anon_vma);
> > >  anon_vma_chain_link(vma, avc, anon_vma);
> > >  - anon_vma->num_active_vmas++;
> > >  allocated = NULL;
> > >  avc = NULL;
> > >  }
> > >  @@ -306,10 +324,8 @@ int anon_vma_clone(struct vm_area_struct *dst, struct vm_area_struct *src)
> > >  if (!dst->anon_vma && src->anon_vma &&
> > >  anon_vma->num_children < 2 &&
> > >  anon_vma->num_active_vmas == 0)
> > >  - dst->anon_vma = anon_vma;
> > >  + anon_vma_attach(dst, anon_vma);
> > >  }
> > >  - if (dst->anon_vma)
> > >  - dst->anon_vma->num_active_vmas++;
> > >
> > You're now losing the cases where we didn't reuse an anon_vma but dst->anon_vma
> > != NULL? This is just broken isn't it?
> >
>
> Yes, it is.

...

> > >
> > > unlock_anon_vma_root(root);
> > >  return 0;
> > >
> > >  @@ -356,31 +372,22 @@ int anon_vma_fork(struct vm_area_struct *vma, struct vm_area_struct *pvma)
> > >  return 0;
> > >
> > >  /* Then add our own anon_vma. */
> > >  - anon_vma = anon_vma_alloc();
> > >  + anon_vma = anon_vma_alloc(pvma->anon_vma);
> > >  if (!anon_vma)
> > >  goto out_error;
> > >  - anon_vma->num_active_vmas++;
> > >  avc = anon_vma_chain_alloc(GFP_KERNEL);
> > >  if (!avc)
> > >  goto out_error_free_anon_vma;
> > >
> > >  - /*
> > >  - * The root anon_vma's rwsem is the lock actually used when we
> > >  - * lock any of the anon_vmas in this anon_vma tree.
> > >  - */
> > >
> > Please please PLEASE do not delete comments like this.
> >
>
> Okay.
> > >
> > > - anon_vma->root = pvma->anon_vma->root;
> > >  - anon_vma->parent = pvma->anon_vma;
> > >
> > Yeah this is just not worth it in my opinion. You're putting code specific to
> > forking in anon_vma_alloc(), which means you've made the code harder to
> > understand.
> >
>
> This won't make the code harder to understand,they are essentially the same.
> One has itself as its parent, while another has a real parent.

They're NOT essentially the same. We have specific logic relating to the
parent on fork, and specific logic relating to anon_vma preparation.

It feels like you've looked at it and seen seemingly duplicated code and wanted
to find a way to write a function to share the code, but then done so without
any understanding of what is going on here.

Arbitrarily trying to squeeze logic into one function is _not_ a win.

Please just drop this parent stuff altogether, thanks.

>
> > >
> > > /*
> > >  * With refcounts, an anon_vma can stay around longer than the
> > >  * process it belongs to. The root anon_vma needs to be pinned until
> > >  * this anon_vma is freed, because the lock lives in the root.
> > >  */
> > >  get_anon_vma(anon_vma->root);
> > >  - /* Mark this anon_vma as the one where our new (COWed) pages go. */
> > >
> > Again, please do not remove comments like this.
> >
>
> Okay.
> > >
> > > - vma->anon_vma = anon_vma;
> > >  + anon_vma_attach(vma, anon_vma);
> > >  anon_vma_lock_write(anon_vma);
> > >  anon_vma_chain_link(vma, avc, anon_vma);
> > >  - anon_vma->parent->num_children++;
> > >
> > So now we're updating things not under the lock?... this is extremely broken.
> >
>
> Yes, it's a mistake.
> > >
> > > anon_vma_unlock_write(anon_vma);
> > >
> > >  return 0;
> > >  @@ -419,15 +426,9 @@ void unlink_anon_vmas(struct vm_area_struct *vma)
> > >  list_del(&avc->same_vma);
> > >  anon_vma_chain_free(avc);
> > >  }
> > >  - if (vma->anon_vma) {
> > >  - vma->anon_vma->num_active_vmas--;
> > >  + if (vma->anon_vma)
> > >  + anon_vma_detach(vma);
> > >
> > >  - /*
> > >  - * vma would still be needed after unlink, and anon_vma will be prepared
> > >  - * when handle fault.
> > >  - */
> > >
> > You are removing key documentation of behaviour, please do not do that.
> >
>
> Okay.
> > >
> > > - vma->anon_vma = NULL;
> > >  - }
> > >  unlock_anon_vma_root(root);
> > >
> > >  /*
> > >  --
> > >  2.25.1
> > >
> > Overall this patch is really quite broken, but I don't think the general concept
> > _as implemented here_ really gives much value.
> >
> > I _like_ the idea of pairing adjustment of these kinds of anon_vma fields like
> > num_children, num_active_vmas etc. with operations, but I think there's probably
> > too many fiddly cases + a need for hellish lock stuff for there to be really
> > anything too viable here.
> >
>
> Sorry for the V1, I will send V2.

To reiterate -

I don't think there's any value in this patch at all and this code is _so_
sensitive that I'm not really hugely interested in seeing refactoring here
without very careful consideration.

I'm not convinced you're giving it that.

So I'd rather you didn't send a v2. But if you insist on doing that, send it as
an RFC please.

This patch _very clearly_ should have been an RFC as it is broken in multiple
fundamental ways.

Thanks, Lorenzo

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ