From: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
To: John Ogness <john.ogness@linutronix.de>
Cc: Petr Mladek <pmladek@suse.com>,
Sergey Senozhatsky <senozhatsky@chromium.org>,
Steven Rostedt <rostedt@goodmis.org>,
Thomas Gleixner <tglx@linutronix.de>,
linux-kernel@vger.kernel.org, Kees Cook <keescook@chromium.org>,
Luis Chamberlain <mcgrof@kernel.org>,
Andrew Morton <akpm@linux-foundation.org>,
Peter Zijlstra <peterz@infradead.org>,
Josh Poimboeuf <jpoimboe@kernel.org>,
Arnd Bergmann <arnd@arndb.de>,
"Guilherme G. Piccoli" <gpiccoli@igalia.com>
Subject: Re: [PATCH printk v2 09/11] panic: Add atomic write enforcement to oops
Date: Wed, 20 Sep 2023 17:45:32 +0300 [thread overview]
Message-ID: <ZQsFjN5euQgt5CI7@smile.fi.intel.com> (raw)
In-Reply-To: <871qetsz8j.fsf@jogness.linutronix.de>
On Wed, Sep 20, 2023 at 04:26:12PM +0206, John Ogness wrote:
> On 2023-09-20, Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote:
> > On Wed, Sep 20, 2023 at 01:14:54AM +0206, John Ogness wrote:
...
> >> + if (atomic_read(&oops_cpu) == smp_processor_id()) {
> >> + oops_nesting--;
> >> + if (oops_nesting == 0) {
> >> + atomic_set(&oops_cpu, -1);
> >
> > Between read and set the variable can change, can't it?
>
> CPU migration is disabled. @oops_cpu contains the CPU ID of the only CPU
> that is printing the oops. (Perhaps the variable should be called
> "oops_printing_cpu"?)
>
> If this matches smp_processor_id(), then the current CPU is the only one
> that is allowed to change it back to -1. So no, if the first condition
> is true, it cannot change before atomic_set(). And if the second
> condition is true, this is the only CPU+context that is allowed to
> change it back to -1;
>
> > If not, why this variable is atomic then? Or, why it's not a problem?
> > If the latter is the case, perhaps a comment to explain this?
>
> If not atomic, it will be a data race since one CPU might be changing
> @oops_cpu and another is reading it. For type "int" such a data race
> would be fine because it doesn't matter which side of the race the
> reader was on, both values will not match the current CPU ID.
>
> The reason that I didn't implement it using cmpxchg(),
> data_race(READ_ONCE()), and WRITE_ONCE() is because I once learned that
> you should never mix cmpxchg() with READ_ONCE()/WRITE_ONCE() because
> there are architectures that do not support cmpxchg() as an atomic
> instruction. The answer was always: "use atomic_t instead... that is
> what it is for".
>
> But AFAICT for this case it would be fine because obviously cmpxchg()
> will not race with itself. And successfully reading a matching CPU ID
> means there cannot be any cmpxchg() in progress. And writing only occurs
> after seeing a matching CPU ID.
>
> So I can change it from atomic_t to int. Although I do feel like that
> might require explanation about why the data race is safe.
Either way a comment is needed, but I think the usage of atomic above
is a bit confusing as you see I immediately rose the concern.
> Or perhaps it is enough just to have something like this:
>
> /**
> * oops_printing_cpu - The ID of the CPU responsible for printing the
> * OOPS message(s) to the consoles.
> *
> * This is atomic_t because multiple CPUs can read this variable
> * simultaneously when exiting OOPS while another CPU can be
> * modifying this variable to begin or end its printing duties.
> */
> static atomic_t oops_printing_cpu = ATOMIC_INIT(-1);
--
With Best Regards,
Andy Shevchenko
next prev parent reply other threads:[~2023-09-20 14:47 UTC|newest]
Thread overview: 42+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-09-19 23:08 [PATCH printk v2 00/11] wire up nbcon atomic printing John Ogness
2023-09-19 23:08 ` [PATCH printk v2 01/11] printk: Make console_is_usable() available to nbcon John Ogness
2023-09-22 8:33 ` Petr Mladek
2023-09-19 23:08 ` [PATCH printk v2 02/11] printk: Let console_is_usable() handle nbcon John Ogness
2023-09-22 8:37 ` Petr Mladek
2023-09-19 23:08 ` [PATCH printk v2 03/11] printk: Add @flags argument for console_is_usable() John Ogness
2023-09-22 8:41 ` Petr Mladek
2023-09-19 23:08 ` [PATCH printk v2 04/11] printk: nbcon: Provide functions to mark atomic write sections John Ogness
2023-09-22 9:33 ` Petr Mladek
2023-09-25 9:25 ` John Ogness
2023-09-25 16:04 ` Petr Mladek
2023-10-05 12:51 ` John Ogness
2023-10-06 12:51 ` panic context: was: " Petr Mladek
2023-10-06 12:53 ` Petr Mladek
2023-10-08 10:13 ` John Ogness
2023-10-09 15:32 ` Petr Mladek
2023-10-10 16:02 ` John Ogness
2023-10-16 8:58 ` Dave Young
2023-10-16 10:09 ` John Ogness
2023-10-06 15:52 ` Petr Mladek
2023-09-19 23:08 ` [PATCH printk v2 05/11] printk: nbcon: Provide function for atomic flushing John Ogness
2023-09-22 12:32 ` Petr Mladek
2023-09-25 11:11 ` John Ogness
2023-09-19 23:08 ` [PATCH printk v2 06/11] printk: nbcon: Wire up nbcon console " John Ogness
2023-09-22 17:41 ` Petr Mladek
2023-09-25 13:37 ` John Ogness
2023-09-26 12:14 ` Petr Mladek
2023-10-05 13:59 ` John Ogness
2023-09-19 23:08 ` [PATCH printk v2 07/11] printk: nbcon: Wire up nbcon into console_flush_all() John Ogness
2023-09-26 11:34 ` Petr Mladek
2023-09-19 23:08 ` [PATCH printk v2 08/11] panic: Add atomic write enforcement to warn/panic John Ogness
2023-09-27 12:02 ` Petr Mladek
2023-09-19 23:08 ` [PATCH printk v2 09/11] panic: Add atomic write enforcement to oops John Ogness
2023-09-19 23:36 ` John Ogness
2023-09-20 13:28 ` Andy Shevchenko
2023-09-20 14:20 ` John Ogness
2023-09-20 14:45 ` Andy Shevchenko [this message]
2023-09-27 13:15 ` Petr Mladek
2023-09-19 23:08 ` [PATCH printk v2 10/11] rcu: Add atomic write enforcement for rcu stalls John Ogness
2023-09-27 15:00 ` Petr Mladek
2023-09-19 23:08 ` [PATCH printk v2 11/11] lockdep: Add atomic write enforcement for lockdep splats John Ogness
2023-09-29 8:31 ` Petr Mladek
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=ZQsFjN5euQgt5CI7@smile.fi.intel.com \
--to=andriy.shevchenko@linux.intel.com \
--cc=akpm@linux-foundation.org \
--cc=arnd@arndb.de \
--cc=gpiccoli@igalia.com \
--cc=john.ogness@linutronix.de \
--cc=jpoimboe@kernel.org \
--cc=keescook@chromium.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mcgrof@kernel.org \
--cc=peterz@infradead.org \
--cc=pmladek@suse.com \
--cc=rostedt@goodmis.org \
--cc=senozhatsky@chromium.org \
--cc=tglx@linutronix.de \
/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