All of lore.kernel.org
 help / color / mirror / Atom feed
From: Ingo Molnar <mingo@kernel.org>
To: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Christian Brauner <christian.brauner@ubuntu.com>,
	Oleg Nesterov <oleg@redhat.com>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	Peter Zijlstra <peterz@infradead.org>,
	Andrew Morton <akpm@linux-foundation.org>
Subject: Re: [GIT PULL] sigqueue cache fix
Date: Mon, 28 Jun 2021 07:30:03 +0200	[thread overview]
Message-ID: <YNleW59jE1rj0Tq8@gmail.com> (raw)
In-Reply-To: <YNlcgryyawTxPz//@gmail.com>


* Ingo Molnar <mingo@kernel.org> wrote:

> 
> * Ingo Molnar <mingo@kernel.org> wrote:
> 
> >  - Producer <-> consumer: this is the most interesting race, and I think 
> >    it's unsafe in theory, because the producer doesn't make sure that any 
> >    previous writes to the actual queue entry (struct sigqueue *q) have 
> >    reached storage before the new 'free' entry is advertised to consumers.
> > 
> >    So in principle CPU#0 could see a new sigqueue entry and use it, before 
> >    it's fully freed.
> > 
> >    In *practice* it's probably safe by accident (or by undocumented 
> >    intent), because there's an atomic op we have shortly before putting the 
> >    queue entry into the sigqueue_cache, in __sigqueue_free():
> > 
> >          if (atomic_dec_and_test(&q->user->sigpending))
> >                 free_uid(q->user);
> > 
> >    And atomic_dec_and_test() implies a full barrier - although I haven't 
> >    found the place where we document it and 
> >    Documentation/memory-ordering.txt is silent on it. We should probably 
> >    fix that too.
> > 
> > At minimum the patch adding the ->sigqueue_cache should include a 
> > well-documented race analysis firmly documenting the implicit barrier after 
> > the atomic_dec_and_test().
> 
> I just realized that even with that implicit full barrier it's not safe: 
> the producer uses q->user after the atomic_dec_and_test(). That access is 
> not serialized with the later write to ->sigqueue_cache - and another CPU 
> might see that entry and use the ->sigqueue_cache and corrupt q->user ...
> 
> So I think this code might have a real race on LL/SC platforms and we'll 
> need an smp_mb() in sigqueue_cache_or_free()?

pps. free_uid() happens to have an implicit barrier via 
refcount_dec_and_lock_irqsave():

  void free_uid(struct user_struct *up)
  {
        unsigned long flags;

        if (!up)
                return;

        if (refcount_dec_and_lock_irqsave(&up->__count, &uidhash_lock, &flags))
                free_user(up, flags);


So the q->user read in __sigqueue_free() appears to be implicitly 
serialized by free_uid() with the later write of 'q' to ->sigqueue_cache.

This needs to be robustly documented though.

Thanks,

	Ingo

  reply	other threads:[~2021-06-28  5:30 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-24  7:13 [GIT PULL] sigqueue cache fix Ingo Molnar
2021-06-24 16:29 ` Linus Torvalds
2021-06-27 18:52   ` Linus Torvalds
2021-06-27 20:40     ` Linus Torvalds
2021-06-28  5:14       ` Ingo Molnar
2021-06-28  5:22         ` Ingo Molnar
2021-06-28  5:30           ` Ingo Molnar [this message]
2021-06-28 17:06         ` Linus Torvalds
2021-06-28 18:46           ` Ingo Molnar
2021-06-28 19:02             ` Linus Torvalds
2021-07-07  9:47               ` Thomas Gleixner
2021-06-24 16:34 ` pr-tracker-bot

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=YNleW59jE1rj0Tq8@gmail.com \
    --to=mingo@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=christian.brauner@ubuntu.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=oleg@redhat.com \
    --cc=peterz@infradead.org \
    --cc=tglx@linutronix.de \
    --cc=torvalds@linux-foundation.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.