Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp1585105img; Tue, 19 Mar 2019 10:48:18 -0700 (PDT) X-Google-Smtp-Source: APXvYqzzMC+pPbDDjYfO5sGTftoZYZA0L7X05Lgdw1SEZimS72686ror2WfHVHatalkt3Ru5btPx X-Received: by 2002:a17:902:9a95:: with SMTP id w21mr3781274plp.118.1553017698721; Tue, 19 Mar 2019 10:48:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553017698; cv=none; d=google.com; s=arc-20160816; b=QQAXEXTNiJkf9Fil6DVjWVYzy05wHeynZ6T7vyy2UFedx0+egHAAghpGHZ/mo8MqRg 3XHTMXBRCpMmYTvWXmZO4e2Ef5LHEhv4FCGcCTRb6/oxBuNyU9+hd4RTUVVHHslFaTSy ER8lCyK0P5QkH/l+jK9QuDnCSMCylgrrw/D58ZRHC96Zz5opW3PXa2qY2l3OhqCXocvP pEQW9z26zx4FZ/WlXYQhzdO1yFaJCXOGPEH0XgUi5Pzm9SSWexfLTbsg5NAs0sNhKyzR FJCBXLL5U1H6+MUYdf4zUQq1O8pPklqutuRRIqJ/FM054+36VoebOKTrrPyPa/maNxkf yYnQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :organization:references:in-reply-to:message-id:subject:cc:to:from :date:dkim-signature; bh=kxyE3mIu+XEHYHbUsnJwC2/Ztt/4Hmrkgoi4jlEiiVs=; b=YHf/wRmHUtTauCkvBdnauJFB4ABgennWspsGBs8/ED9wSwLC/BGEVNYVYJYkMsK2Jt bBXmi/A3gb9e5EBktwKBVkx+iybmmyqKMUHE3ykInV8DoCukQgXdk9bzMNMJdgZ0mf7G NsM7CGfjADCmYkINN/GfmuPocL4oHzYzwoQCfU+4tGFibxNk0Y6Gw6uiMWiD+aXPM55i 9Sli4M0nYKdry8Yr+kn1Chn4KNtyUQstinvZTxUcXIOOoX0tgbxEs2Rfwx2CxHHWYFxU vSsp5TgIn0gqMygE0ou+aal5RkTUsCAPsP471scquiLWcqL7obW5C/olljObFHf3bc8F wFbA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@netronome-com.20150623.gappssmtp.com header.s=20150623 header.b=wcfJm9wr; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t8si10078720pfh.229.2019.03.19.10.48.02; Tue, 19 Mar 2019 10:48:18 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@netronome-com.20150623.gappssmtp.com header.s=20150623 header.b=wcfJm9wr; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727416AbfCSRrF (ORCPT + 99 others); Tue, 19 Mar 2019 13:47:05 -0400 Received: from mail-qt1-f194.google.com ([209.85.160.194]:43925 "EHLO mail-qt1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726944AbfCSRrE (ORCPT ); Tue, 19 Mar 2019 13:47:04 -0400 Received: by mail-qt1-f194.google.com with SMTP id v32so23135463qtc.10 for ; Tue, 19 Mar 2019 10:47:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=netronome-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:in-reply-to:references :organization:mime-version:content-transfer-encoding; bh=kxyE3mIu+XEHYHbUsnJwC2/Ztt/4Hmrkgoi4jlEiiVs=; b=wcfJm9wr5WYvX7sHAtrZSIF3n0P/XTwLHRyS3aZrp3H/yjbN81u3+nKLY6uIvJ0Bm3 aoHrfDe3AllpM4zBTuo6TXkhvJD0vvCezWUZhxUufFFpBPmtrzSpMCA9cLr1SH49Te1K 8M7isGiUcp1dXRQ9n23oljlOGhTM+xy6DjKWeM198+E1pITDkCbRrDuRs5uvVzh9pGLq Y/SPXNU6M1fs2bBlolO8qJSNOgEJ9wMhsten72koHScDA3Deq7SH1Ok1jz1Yn/8LFANC pJa/jYlPADruPYVv7hGdYXV5p+EOiqwQdRaaApIZx/Sed86sD4ZOaXI6ArmZrwMEpnBT B8og== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:in-reply-to :references:organization:mime-version:content-transfer-encoding; bh=kxyE3mIu+XEHYHbUsnJwC2/Ztt/4Hmrkgoi4jlEiiVs=; b=Qi0fVfZwg893zaF78hKHLZWsIZCFu6brJXOyroBGaGupD4mhycGljC5/sI2YTx0yZ7 ixVH7Czy52J7kbATK/GopvBpB89fhCV+A4Qylcwd/TKRqE2EnkNO7CAm66wW8M4E7HYO Sx7tdVtv46Scjb7GzDPmuqHpjTr2aXZO5YRvuiC/0K3AkPV64Ua1rGIXM6j3pALlt+aM r3KukO21eP7rKsVW5FGVfILVtcQonZYlCSD1873nh/WFrA3kjPL1zs+XIo6dXo4YA4u7 KJa2YTZC/09eYDx/2j4Kr9JId/Y+Nxbx2msWObbS8prfQl+INKrJb+xueI2YdV+MukG8 xpIg== X-Gm-Message-State: APjAAAWquQywR3sbn1HrecoRf4ojIzgspMajdMK+Sk+Bs4uZnkJxM1tl sgwxujlJ0yRha0QL3u+nhalPlQ== X-Received: by 2002:a0c:d2d4:: with SMTP id x20mr2950908qvh.227.1553017623464; Tue, 19 Mar 2019 10:47:03 -0700 (PDT) Received: from cakuba.netronome.com ([66.60.152.14]) by smtp.gmail.com with ESMTPSA id l2sm1046205qtc.47.2019.03.19.10.47.02 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 19 Mar 2019 10:47:03 -0700 (PDT) Date: Tue, 19 Mar 2019 10:46:57 -0700 From: Jakub Kicinski To: Peter Zijlstra Cc: pbonzini@redhat.com, ard.biesheuvel@linaro.org, tglx@linutronix.de, mingo@kernel.org, linux-kernel@vger.kernel.org, oss-drivers@netronome.com Subject: Re: [PATCH] locking/static_key: Fix false positive warnings on concurrent dec/inc Message-ID: <20190319104657.6fa99d82@cakuba.netronome.com> In-Reply-To: <20190319121856.GE5996@hirez.programming.kicks-ass.net> References: <20190318215814.3724-1-jakub.kicinski@netronome.com> <20190319121856.GE5996@hirez.programming.kicks-ass.net> Organization: Netronome Systems, Ltd. MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Thanks for looking at the patch! On Tue, 19 Mar 2019 13:18:56 +0100, Peter Zijlstra wrote: > On Mon, Mar 18, 2019 at 02:58:14PM -0700, Jakub Kicinski wrote: > > Even though the atomic_dec_and_mutex_lock() in > > __static_key_slow_dec_cpuslocked() can never see a negative > > value in key->enabled the subsequent sanity check is re-reading > > key->enabled, which may have been set to -1 in the meantime by > > static_key_slow_inc_cpuslocked(). > > A little extra detail might not hurt, or a diagram or something. Like this: CPU A CPU B __static_key_slow_dec_cpuslocked(): static_key_slow_inc_cpuslocked(): # enabled = 1 atomic_dec_and_mutex_lock() # enabled = 0 atomic_read() == 0 atomic_set(-1) # enabled = -1 val = atomic_read() # Oops - val == -1! ? The test case is TCP's clean_acked_data_enable() / clean_acked_data_disable() as tickled by ktls (net/ktls). Which should probably use the delayed version in the first place, hopefully I can get to adding delayed version of static branches and converting at some point.. > > Instead of using -1 as a "enable in progress" constant use > > -0xffff, this way we can still treat smaller negative values > > as errors. > > Those offset games always hurt my brain, but see below. > > > Fixes: 4c5ea0a9cd02 ("locking/static_key: Fix concurrent static_key_slow_inc()") > > Signed-off-by: Jakub Kicinski > > --- > > kernel/jump_label.c | 21 ++++++++++----------- > > 1 file changed, 10 insertions(+), 11 deletions(-) > > > > diff --git a/kernel/jump_label.c b/kernel/jump_label.c > > index bad96b476eb6..4a227e70a8f3 100644 > > --- a/kernel/jump_label.c > > +++ b/kernel/jump_label.c > > @@ -89,7 +89,7 @@ static void jump_label_update(struct static_key *key); > > int static_key_count(struct static_key *key) > > { > > /* > > - * -1 means the first static_key_slow_inc() is in progress. > > + * -0xffff means the first static_key_slow_inc() is in progress. > > * static_key_enabled() must return true, so return 1 here. > > */ > > int n = atomic_read(&key->enabled); > > @@ -125,7 +125,10 @@ void static_key_slow_inc_cpuslocked(struct static_key *key) > > > > jump_label_lock(); > > if (atomic_read(&key->enabled) == 0) { > > - atomic_set(&key->enabled, -1); > > + /* Use a large enough negative number so we can still > > + * catch underflow bugs in static_key_slow_dec(). > > + */ > > Broken comment style. Ah, sorry, netdev. > > + atomic_set(&key->enabled, -0xffff); > > jump_label_update(key); > > /* > > * Ensure that if the above cmpxchg loop observes our positive > > @@ -158,7 +161,7 @@ void static_key_enable_cpuslocked(struct static_key *key) > > > > jump_label_lock(); > > if (atomic_read(&key->enabled) == 0) { > > - atomic_set(&key->enabled, -1); > > + atomic_set(&key->enabled, -0xffff); > > jump_label_update(key); > > /* > > * See static_key_slow_inc(). > > @@ -208,15 +211,11 @@ static void __static_key_slow_dec_cpuslocked(struct static_key *key, > > { > > lockdep_assert_cpus_held(); > > > > - /* > > - * The negative count check is valid even when a negative > > - * key->enabled is in use by static_key_slow_inc(); a > > - * __static_key_slow_dec() before the first static_key_slow_inc() > > - * returns is unbalanced, because all other static_key_slow_inc() > > - * instances block while the update is in progress. > > - */ > > if (!atomic_dec_and_mutex_lock(&key->enabled, &jump_label_mutex)) { > > - WARN(atomic_read(&key->enabled) < 0, > > + int v; > > + > > + v = atomic_read(&key->enabled); > > + WARN(v < 0 && v != -0xffff, > > "jump label: negative count!\n"); > > return; > > } > > > Alternatively we could implement atomic_dec_and_mutex_lock_return(). > > I think I like that better, something like: That indeed looks far cleanest, thanks! Tested-by: Jakub Kicinski > kernel/jump_label.c | 21 +++++++++++++-------- > 1 file changed, 13 insertions(+), 8 deletions(-) > > diff --git a/kernel/jump_label.c b/kernel/jump_label.c > index bad96b476eb6..a799b1ac6b2f 100644 > --- a/kernel/jump_label.c > +++ b/kernel/jump_label.c > @@ -206,6 +206,8 @@ static void __static_key_slow_dec_cpuslocked(struct static_key *key, > unsigned long rate_limit, > struct delayed_work *work) > { > + int val; > + > lockdep_assert_cpus_held(); > > /* > @@ -215,17 +217,20 @@ static void __static_key_slow_dec_cpuslocked(struct static_key *key, > * returns is unbalanced, because all other static_key_slow_inc() > * instances block while the update is in progress. > */ > - if (!atomic_dec_and_mutex_lock(&key->enabled, &jump_label_mutex)) { > - WARN(atomic_read(&key->enabled) < 0, > - "jump label: negative count!\n"); > + val = atomic_fetch_add_unless(&key->enabled, -1, 1); > + if (val != 1) { > + WARN(val < 0, "jump label: negative count!\n"); > return; > } > > - if (rate_limit) { > - atomic_inc(&key->enabled); > - schedule_delayed_work(work, rate_limit); > - } else { > - jump_label_update(key); > + jump_label_lock(); > + if (atomic_dec_and_test(&key->enabled)) { > + if (rate_limit) { > + atomic_inc(&key->enabled); > + schedule_delayed_work(work, rate_limit); > + } else { > + jump_label_update(key); > + } > } > jump_label_unlock(); > }