linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: Paolo Bonzini <pbonzini@redhat.com>
Cc: torvalds@linux-foundation.org, linux-kernel@vger.kernel.org,
	tglx@linutronix.de, mingo@kernel.org, dvyukov@google.com,
	hpa@zytor.com, linux-tip-commits@vger.kernel.org,
	Steven Rostedt <rostedt@goodmis.org>,
	Dima Zavin <dmitriyz@waymo.com>
Subject: Re: [tip:locking/urgent] locking/static_key: Fix concurrent static_key_slow_inc()
Date: Mon, 31 Jul 2017 15:33:49 +0200	[thread overview]
Message-ID: <20170731133349.gphnv5pqkn2xibwf@hirez.programming.kicks-ass.net> (raw)
In-Reply-To: <671056c7-5fc7-60c0-4035-b11d43d95bf1@redhat.com>

On Mon, Jul 31, 2017 at 03:04:06PM +0200, Paolo Bonzini wrote:
> - key->enabled cannot go from 0 to nonzero outside jump_label_mutex.
> For this reason the atomic_try_cmpxchg is unnecessary.

Agreed, the only reason was the implied barrier.

> - the (implied) smp_mb before jump_label_update is not interesting, but
> I don't think it is useful because: 1) during the jump_label_update
> there is no correspondence between what static_key_enabled returns and
> what the text look like; 2) what would it even be pairing with?

Ah, indeed. So I was worried about the text changes escaping upwards,
but you're right in that there's no harm in that because there's nothing
that cares.

Another inc would see 0 and still serialize on the mutex.

> - the smp_mb (though it could be a smp_wmb or atomic_set_release)
> initially triggered my paranoia indeed.  But even then, I can't see why
> you would need it because there's nothing it pairs with.

So this one would pair with the mb implied by the cmpxchg loop for
inc-if-positive. The ordering being that if we see a positive v, we must
then also see all the text modifications.

So if jump_label_update() were to not fully serialize things, it would
be possible for the v=1 store to appear before the last text changes.
And in that case we'd allow the fast path to complete
static_key_slow_inc() before it was in fact done with changing all text.

Now, I suspect (but did not audit) that anything that changes text must
in fact serialize world, but I wasn't sure.

> Rather, it's *any use of key->enabled outside jump_label_lock*
> (meaning: any use of static_key_enabled and static_key_count outside
> the core jump_label.c code) that should be handled with care.

> And indeed, while there aren't many, I think two of them are wrong (and
> not fixed by your patch):
> 
> - include/linux/cpuset.h defines nr_cpusets which uses static_key_count.
>  It makes no sense to call it outside cpuset_mutex, and indeed that's
> how kernel/cgroup/cpuset.c uses it (nr_cpusets <- generate_sched_domains
> <- rebuild_sched_domains_locked).  The function should be moved inside
> kernel/cgroup/cpuset.c since the mutex is static.

Dima was poking at that code.

> - net/ipv4/udp.c and net/ipv6/udp.c want to implement a "once-only"
> increment of the static key.  It's racy and maybe we should provide a
> new API static_key_enable_forever:
> 
> 	void static_key_enable_forever(struct static_key *key)
> 	{
> 	        STATIC_KEY_CHECK_USE();
> 	        if (atomic_read(&key->enabled) > 0)
> 	                return;
> 
> 	        cpus_read_lock();
> 	        jump_label_lock();
> 	        if (atomic_read(&key->enabled) == 0) {
> 	                atomic_set(&key->enabled, -1);
> 	                jump_label_update(key);
> 	                atomic_set(&key->enabled, 1);
> 	        }
> 	        jump_label_unlock();
> 	        cpus_read_unlock();
> 	}
> 	EXPORT_SYMBOL_GPL(static_key_enable_forever);
> 
> I can prepare a patch if you agree.

Isn't that what we have static_key_enable() for? Which btw also uses
static_key_count() outside of the mutex.

  reply	other threads:[~2017-07-31 13:34 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-06-21 16:52 [PATCH] static_key: fix concurrent static_key_slow_inc Paolo Bonzini
2016-06-21 19:20 ` Peter Zijlstra
2016-06-22  8:50 ` Christian Borntraeger
2016-06-22  9:52   ` Paolo Bonzini
2016-06-24  8:59 ` [tip:locking/urgent] locking/static_key: Fix concurrent static_key_slow_inc() tip-bot for Paolo Bonzini
2017-07-31  9:36   ` Peter Zijlstra
2017-07-31 13:04     ` Paolo Bonzini
2017-07-31 13:33       ` Peter Zijlstra [this message]
2017-07-31 15:35         ` Paolo Bonzini
2017-07-31 15:45           ` Peter Zijlstra
2017-07-31 19:21             ` Paolo Bonzini
2017-07-31 17:15         ` Dima Zavin

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=20170731133349.gphnv5pqkn2xibwf@hirez.programming.kicks-ass.net \
    --to=peterz@infradead.org \
    --cc=dmitriyz@waymo.com \
    --cc=dvyukov@google.com \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-tip-commits@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=pbonzini@redhat.com \
    --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;
as well as URLs for NNTP newsgroup(s).