Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752480AbdGaPfp (ORCPT ); Mon, 31 Jul 2017 11:35:45 -0400 Received: from mx1.redhat.com ([209.132.183.28]:56290 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752283AbdGaPfo (ORCPT ); Mon, 31 Jul 2017 11:35:44 -0400 DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com A56646E406 Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=pbonzini@redhat.com Subject: Re: [tip:locking/urgent] locking/static_key: Fix concurrent static_key_slow_inc() To: Peter Zijlstra 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 , Dima Zavin References: <1466527937-69798-1-git-send-email-pbonzini@redhat.com> <20170731093612.7v23kxkk47i56io6@hirez.programming.kicks-ass.net> <671056c7-5fc7-60c0-4035-b11d43d95bf1@redhat.com> <20170731133349.gphnv5pqkn2xibwf@hirez.programming.kicks-ass.net> From: Paolo Bonzini Message-ID: <645f28cb-56c8-7298-568f-357a1b8929b9@redhat.com> Date: Mon, 31 Jul 2017 17:35:40 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 MIME-Version: 1.0 In-Reply-To: <20170731133349.gphnv5pqkn2xibwf@hirez.programming.kicks-ass.net> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Mon, 31 Jul 2017 15:35:44 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2161 Lines: 54 On 31/07/2017 15:33, Peter Zijlstra wrote: > On Mon, Jul 31, 2017 at 03:04:06PM +0200, Paolo Bonzini wrote: >> - 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. I see. Then yes, I agree a smp_wmb would be nicer here. >> 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. > >> - 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. Yes, they should be fixed and net/ can then use static_key_enable. Paolo