From patchwork Wed Oct 19 14:08:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Uros Bizjak X-Patchwork-Id: 5600 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp367325wrs; Wed, 19 Oct 2022 07:46:49 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5hkYKn3Q9pkvN20O5e+/jrT36Kl7Fv58neGHl+mInAPmCNa+j920ui1CD3eO4yFs77e0aP X-Received: by 2002:a17:906:4a09:b0:78d:b6bb:ff5f with SMTP id w9-20020a1709064a0900b0078db6bbff5fmr6779831eju.538.1666190809571; Wed, 19 Oct 2022 07:46:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666190809; cv=none; d=google.com; s=arc-20160816; b=qkv6ez3oJACNcsXlTsCERHfXDI3n6aSL04oAuYwiPzLJVd5l3bGQII1E55hkIL8fss GiC5pOblfGRgzTz6HrT8HOvUHWk99wXydh3oFNWui52KhaPBNT6TLXzZKdwROkh/rtmD WWaU8Ow3M4pNxpSuHA3ev8JriW19uBHEVXzqwXC391uj1pk6IvL6nB8fV4/cwgft13hZ OXOuq+0jF8nA6/YsYdHewMs6Nnx4z7tjprr3g/+gW+coUhFxqQt4P4qr3jBVpWZD/owm yuzLYxajB70hFgP+ySTwOA7hVG2kNUf7BcB9qgd8y/R9iLONni1mW68UhVlsFnQoF1Cc 519g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=A41cmwk6vsfiz18uL26ZksSgKHfvB8mOhlbn7RWroYI=; b=pAQEmHhWAslvpopbZDHmxLL2Jdv8CaueRrALEf4zaJrCFpov7UYvOaAbyO5CwIu1+r A0btwoJ1j6/W6i47/1d01HoUqJ1e3N6eoAlar1q1uGyGfEOpBBdYLlSvUq4aaBb8SqJK udWgGkcwS0qqQRSbV8GVlx0ytiNcgBGCCwA1ECeMmLLEnERlwNtNjAZAZBOZIa3Wi0+t K1JzIt0MhVQX0hLY1v8/C6O9IzvZK1KdgmCN+UwNr8d2on5hExYsveDt9SVxkHYY9/Zm IYzB/pZlJ665zRayWC6P7gTO9GtBOCk/umdlHEb8evu2TmQLOeA3zc1GUTiD3NgGDTn4 xJUw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=BUGjpY4d; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y9-20020a50e609000000b004573275623fsi12080166edm.423.2022.10.19.07.46.24; Wed, 19 Oct 2022 07:46:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=BUGjpY4d; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232083AbiJSOgJ (ORCPT + 99 others); Wed, 19 Oct 2022 10:36:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55370 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229755AbiJSOfr (ORCPT ); Wed, 19 Oct 2022 10:35:47 -0400 Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8498010CFBD for ; Wed, 19 Oct 2022 07:19:52 -0700 (PDT) Received: by mail-wr1-x430.google.com with SMTP id v1so128169wrt.11 for ; Wed, 19 Oct 2022 07:19:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=A41cmwk6vsfiz18uL26ZksSgKHfvB8mOhlbn7RWroYI=; b=BUGjpY4dFx+tjDzcbqisTqhazVC3FPj/37XmkDVOCQooeKNHql7fJDqPBnfXWy0KZB pvWPDQWEIrkzAdErpRcUPyVTNdC1inAoh8G6LmWvUyZjtIQjbTZ89YajEDu+YUxze9Kq TyIOLsZzmAp5XCHwbw+U5NkhiG26chITfQM3kErJ9qGIhIaI0y+8lzf59ScSiB8hlNjp mQODSfGqWKj8ai+74pMSY+6k4S9qyHyAIOHphWaSGMpW9I8VlgdbH/IIst3OSpj6f0Bv QQW1L4n9snVB/NmmaBVvgSCiDLbIbYOuVDeG0ugdNefybxadBtjOFK7mufwjHLY1Eig2 h/+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=A41cmwk6vsfiz18uL26ZksSgKHfvB8mOhlbn7RWroYI=; b=I4xp1ym7cW69+FK9sBtbRG+FIsZBgDXs+ILu0gqv8gXogRhmA+AcZzPPYk4Gki6gzJ 7FGfpC39J0jSqoF8rUAuh4XqlTGrXBP5fiKji4we7r25KknGk8gMk8qwHNQU+7u+MaqG 7hPk8/ULZO2B/Sc+JXZyhqt4cPXNX70XZRLLu3QqPUkLlCcRaR05eaJXOCYlk4mst4IO yRj7d+xInMNKG1FzY+HRWHkLi+hJFGX6vBsl9F7hniJIJndD068ZUjOvxJ/ZyVp+r/w0 meAKBySHoPl4DVk6PbgLXqzHuckxaAPjC9CGreKmuUEt7kebWGJlnd88ucytaMBmfVBJ 68Hg== X-Gm-Message-State: ACrzQf2qS9pdDuYHqAoFYHN9LIrUrhlxaXBKyZMyFum47bk1UBqD80fi 39LwpWeLwqtGydzom2GicoReWmbNMNBxvA== X-Received: by 2002:a17:907:6d08:b0:78e:e87:5c06 with SMTP id sa8-20020a1709076d0800b0078e0e875c06mr6683796ejc.511.1666188553676; Wed, 19 Oct 2022 07:09:13 -0700 (PDT) Received: from localhost.localdomain ([46.248.82.114]) by smtp.gmail.com with ESMTPSA id c21-20020a17090603d500b00764a76d5888sm8899287eja.27.2022.10.19.07.09.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Oct 2022 07:09:13 -0700 (PDT) From: Uros Bizjak To: linux-kernel@vger.kernel.org Cc: Uros Bizjak , Peter Zijlstra , Josh Poimboeuf , Jason Baron , Steven Rostedt , Ard Biesheuvel Subject: [PATCH] jump_label: use atomic_try_cmpxchg in static_key_slow_inc_cpuslocked Date: Wed, 19 Oct 2022 16:08:50 +0200 Message-Id: <20221019140850.3395-1-ubizjak@gmail.com> X-Mailer: git-send-email 2.37.3 MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747127693839824243?= X-GMAIL-MSGID: =?utf-8?q?1747127693839824243?= Use atomic_try_cmpxchg instead of atomic_cmpxchg (*ptr, old, new) == old in static_key_slow_inc_cpuslocked. x86 CMPXCHG instruction returns success in ZF flag, so this change saves a compare after cmpxchg (and related move instruction in front of cmpxchg). Also, atomic_try_cmpxchg implicitly assigns old *ptr value to "old" when cmpxchg fails, enabling further code simplifications. No functional change intended. Cc: Peter Zijlstra Cc: Josh Poimboeuf Cc: Jason Baron Cc: Steven Rostedt Cc: Ard Biesheuvel Signed-off-by: Uros Bizjak --- kernel/jump_label.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/kernel/jump_label.c b/kernel/jump_label.c index 714ac4c3b556..4d6c6f5f60db 100644 --- a/kernel/jump_label.c +++ b/kernel/jump_label.c @@ -115,8 +115,6 @@ EXPORT_SYMBOL_GPL(static_key_count); void static_key_slow_inc_cpuslocked(struct static_key *key) { - int v, v1; - STATIC_KEY_CHECK_USE(key); lockdep_assert_cpus_held(); @@ -132,11 +130,9 @@ void static_key_slow_inc_cpuslocked(struct static_key *key) * so it counts as "enabled" in jump_label_update(). Note that * atomic_inc_unless_negative() checks >= 0, so roll our own. */ - for (v = atomic_read(&key->enabled); v > 0; v = v1) { - v1 = atomic_cmpxchg(&key->enabled, v, v + 1); - if (likely(v1 == v)) + for (int v = atomic_read(&key->enabled); v > 0; ) + if (likely(atomic_try_cmpxchg(&key->enabled, &v, v + 1))) return; - } jump_label_lock(); if (atomic_read(&key->enabled) == 0) {