mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Frederic Weisbecker <fweisbec@gmail.com>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: Steven Rostedt <rostedt@goodmis.org>,
	LKML <linux-kernel@vger.kernel.org>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	"H. Peter Anvin" <hpa@linux.intel.com>,
	Ingo Molnar <mingo@kernel.org>,
	Peter Zijlstra <peterz@infradead.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	"Liu, Chuansheng" <chuansheng.liu@intel.com>
Subject: Re: [PATCH] bug: Use xchg() to update WARN_ON_ONCE() static variable
Date: Tue, 15 Oct 2013 22:21:36 +0200	[thread overview]
Message-ID: <20131015202134.GB3269@localhost.localdomain> (raw)
In-Reply-To: <20131015131259.3051743890ce9b1b9dae9763@linux-foundation.org>

On Tue, Oct 15, 2013 at 01:12:59PM -0700, Andrew Morton wrote:
> On Tue, 15 Oct 2013 15:58:06 -0400 Steven Rostedt <rostedt@goodmis.org> wrote:
> 
> > The WARN_ON_ONCE() code is to trigger a waring only once when some
> > condition happens. But due to the way it is written it is racy.
> > 
> > 	if (unlikely(condition)) {
> > 		if (WARN(!__warned))
> > 			__warned = true;
> > 	}
> > 
> > The problem is that multiple CPUs could hit the same warning and
> > produce multiple output dumps of the same warning, or an interrupt could
> > happen and hit the same warning and do the warning in the middle of a
> > previous one, especially since the WARN() does a dump of the current
> > stack.
> > 
> > Even more of a problem, a recent WARN_ON_ONCE() that was in the page
> > fault handler triggered and the stack dump of the WARN() caused the
> > same WARN_ON_ONCE() get hit again. Since the __warned = true is not
> > updated until after the WARN() is completed, each WARN() triggered
> > another page fault causing the stack to be filled and crashed the box.
> > 
> > The point of WARN_ON() is to warn the user and not to crash the box.
> > 
> > The easy fix is to update the __warned variable with a xchg(). This way
> > only one WARN_ON_ONCE() will actually happen, and prevents any issues
> > of the WARN() causing the same WARN() to be hit and crash the system.
> 
> printk_once() has the same issue, and probably other places.
> 
> Is there some sneaky way of doing this operation as a common thing,
> rather than open-coding it everywhere?  Something like
> 
> #define ONCE() ({
> 	static int state;
> 	int ret;
> 
> 	ret = !xchg(&state, 1);
> 	ret;
> })

Oh, I have a pending patchset that I worked on a few weeks ago which does that.
I did not post it because it made WARN_ONCE using the unlikely text section, but
the diffstat was nice.

I'm going to post that as RFC just in case.

  reply	other threads:[~2013-10-15 20:21 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-10-15 19:58 Steven Rostedt
2013-10-15 20:12 ` Andrew Morton
2013-10-15 20:21   ` Frederic Weisbecker [this message]
2013-10-15 20:36     ` Steven Rostedt
2013-10-15 20:41       ` Frederic Weisbecker
2013-10-15 20:35   ` Steven Rostedt
2013-10-15 20:18 ` Frederic Weisbecker
2013-10-15 20:25   ` Steven Rostedt
2013-10-15 20:37     ` Frederic Weisbecker

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=20131015202134.GB3269@localhost.localdomain \
    --to=fweisbec@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=chuansheng.liu@intel.com \
    --cc=hpa@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®