From patchwork Fri Nov 22 17:44:14 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13883494 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id B88C1E69185 for ; Fri, 22 Nov 2024 17:44:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 47A146B0085; Fri, 22 Nov 2024 12:44:22 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 451816B0088; Fri, 22 Nov 2024 12:44:22 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3406B6B0089; Fri, 22 Nov 2024 12:44:22 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 125A26B0085 for ; Fri, 22 Nov 2024 12:44:22 -0500 (EST) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id BCD9841BBB for ; Fri, 22 Nov 2024 17:44:21 +0000 (UTC) X-FDA: 82814454072.04.866790C Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf16.hostedemail.com (Postfix) with ESMTP id 04FDB180008 for ; Fri, 22 Nov 2024 17:43:25 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="pe/V5VD4"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf16.hostedemail.com: domain of 388JAZwYKCLwuwtgpdiqqing.eqonkpwz-oomxcem.qti@flex--surenb.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=388JAZwYKCLwuwtgpdiqqing.eqonkpwz-oomxcem.qti@flex--surenb.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1732297392; a=rsa-sha256; cv=none; b=uM3u5EccVE6OVYW/ecKzhXy7xDOu9eD/qSr1SIoL3zOuvqXMZ4oibJC13NMxmylAVGFdCV qdGfFqRGb5w54g7OxSHwdiKY9XcuEa6BbZ/6rgZpCMgRcbU8tTmnTAJ8mRjmsJ6xuI2ese rnkROAnkko+jDxTs7K5h7fOvETUc/Zo= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="pe/V5VD4"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf16.hostedemail.com: domain of 388JAZwYKCLwuwtgpdiqqing.eqonkpwz-oomxcem.qti@flex--surenb.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=388JAZwYKCLwuwtgpdiqqing.eqonkpwz-oomxcem.qti@flex--surenb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1732297392; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=yj1n0zU6JR6TtWJJSwgCR8Y16uC2MOhbrp4MIHnT7BQ=; b=x0tZRIzIIRJ1hLMQoV0YCRpSTm47DjCi4NTgzxix2Ifv9Lh1lzME0AgofB5gwBzhe4wY/T KDoZd0KqnaWFJRiGTBPeoJsRxzCizl2YHJ4w02J4jnicUzQXUc5f2Gcc3Bt0olwfNpfeJG v7OZUhcVJ/UszK69lqmOKGeGZ3DzGI8= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6eebd4e7cc5so27414187b3.0 for ; Fri, 22 Nov 2024 09:44:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1732297459; x=1732902259; darn=kvack.org; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=yj1n0zU6JR6TtWJJSwgCR8Y16uC2MOhbrp4MIHnT7BQ=; b=pe/V5VD4Pjace2HZ+llmbPJDHuen70L1+7tnvocZstJUrOeAreiFMdKQMciOp/kMTJ rFQWxFPPnflqBH31ayw7VkxcoaXnT9x4ytQ2dRkudLWw+3iM8AuEF2oT2C7LtbltMPJP aw8TN7p12/pt8LYjZ5PuPa31UAqwEw3uo+ap4AYpCZSi1UfU3mRM4EX+KsIGl1Xvu8YY mvV6k6kqkd9YnFWr8xY+8f0O0Fy+uJlA1Hn9w6fqsbR76dVR0Vtp1OBfSMb87uRloIC6 CrI5wZjdP3W0TF+lXJgj7RPRcFBVgqtK1skzQb9yzyNg3OLQUlqHkKXMEg0/n1Ft6wVd uLoQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732297459; x=1732902259; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=yj1n0zU6JR6TtWJJSwgCR8Y16uC2MOhbrp4MIHnT7BQ=; b=ENxTEr5h0QbEJ3334OPntMIpHhOSRL1bL0aoGRsklDH2NJGkA1bkAUF5cH1M/VZGX5 MtvegJSbFFw0TKuFrPXrMNXNtLXSo/KxfIFZG1QvhaVgoZtbMcAUubb7QP4s1GzxKtcC tHaE+E6Z1O99E+EAHS2MEabWfkZMzxWfR8pzYfMBa5KFsWJFAMB3DlbC14hUAxEx6vvr wTU0h8WpZlyNnLU/vV2maec5qnWLBr1VnIR1CdHMbduwBgwLjZbLDVHQcRZa7ijNsKCZ RXJIz9z+TMicoPhJCG1k5007YWLR6l3OlpPT9+3yRW1L18ggno+xmWEqxcfjT/K4kTd/ tUFQ== X-Forwarded-Encrypted: i=1; AJvYcCX/PfFxtP5PGFxSZnxywGnVsTYvEpOTHP0Lxs/1t1uYoEnxoHu3r5AtqruOqZtgQEK5HRyK8j2XFw==@kvack.org X-Gm-Message-State: AOJu0YwuNsjmnNBWN2hFoqWI/S8e9pnrkY6eK66uevPTT8rUuRM3rR3g nUMIu6bXfk9ytN4tspPCy7K0CtvAzfa93V5rMXc4/IUeW6j/aN7bp0pJwUopMaSCConB8EnP4hs inA== X-Google-Smtp-Source: AGHT+IGq3cGWzdvvhpU8u0wQHDoiOGsV0+vlaDIGvQqvSQKVuhJtv4ZEC6vOgRsc5zcknFaXDhykLQhGUlY= X-Received: from surenb-desktop.mtv.corp.google.com ([2a00:79e0:2e3f:8:8f07:6a96:7af9:8fe6]) (user=surenb job=sendgmr) by 2002:a05:690c:dc3:b0:620:32ea:e1d4 with SMTP id 00721157ae682-6eee07b3563mr576407b3.0.1732297459074; Fri, 22 Nov 2024 09:44:19 -0800 (PST) Date: Fri, 22 Nov 2024 09:44:14 -0800 Mime-Version: 1.0 X-Mailer: git-send-email 2.47.0.371.ga323438b13-goog Message-ID: <20241122174416.1367052-1-surenb@google.com> Subject: [PATCH v3 1/3] seqlock: add raw_seqcount_try_begin From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: peterz@infradead.org, andrii@kernel.org, jannh@google.com, Liam.Howlett@oracle.com, lorenzo.stoakes@oracle.com, vbabka@suse.cz, mhocko@kernel.org, shakeel.butt@linux.dev, hannes@cmpxchg.org, david@redhat.com, willy@infradead.org, brauner@kernel.org, oleg@redhat.com, arnd@arndb.de, richard.weiyang@gmail.com, zhangpeng.00@bytedance.com, linmiaohe@huawei.com, viro@zeniv.linux.org.uk, hca@linux.ibm.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, surenb@google.com, "Liam R. Howlett" X-Rspam-User: X-Rspamd-Queue-Id: 04FDB180008 X-Rspamd-Server: rspam01 X-Stat-Signature: bwiwqt9ibm4zx3ectanyk8ok8x6c5f74 X-HE-Tag: 1732297405-811707 X-HE-Meta: U2FsdGVkX19RD8ZGxlFrZTrecEuOyr4HviXZV2SJ/DCd7ZMVAGTMqasjeGM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VYQ2GaLS2IWcZBAwOEinyILFQT/zcFJRNkfTPIT2nEzPGBJfYuBotOxRNOqEr+JG0gMKyRrTnFLY8WRdVksRibrHOBY23FWe8psWAxIJgRoLXTto1SQLJm8ClJFqD4c2KIiSjojyXYll2nySuhuMJl+lO98Cm2bE6Lrxs9WtKX9MOhd/adSpLYmWG4UgL4OTaNRgMv/+ZsVqhkLizGjEAjrA85GmdLfvXjhDmxMuWsjMnskmNgiAtnfXNbrKWM+ZLApyvZIBBzPiAJR5Ctnz46Mp/hKZRZFLm1ia9HnEfbS7bJVqfpn3DIxhybiQb4F0vqSdI7UUrsmB1T/YIoUHDQkvRRjQuehsWyCsrI6PsD+P9MigqEJefsRaYuksiztk/A2cpBl3PRYshkYj6V2OF/D3/ApmgePWIbDN1iqVjxMvoPYdnMYlgSOKpxilVkSOCVC1pbFBvkWoV5If52EUZUlx0F9gUwXtJrqpPilfUvbMOzXYy/eWZxNA/SfXGA3SIKhUUlJ/ohSRAXpGhbuS/vPBsZdD62ASbgCLBOZQjC+VRNOu60cuJEwcP1q/wzZga8pA6ZBWPLaXjisb1R+n/BkttTexg53AQthPn10+20tz0g3BEW4+X9E7f X-Bogosity: Ham, tests=bogofilter, spamicity=0.000720, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Add raw_seqcount_try_begin() to opens a read critical section of the given seqcount_t if the counter is even. This enables eliding the critical section entirely if the counter is odd, instead of doing the speculation knowing it will fail. Suggested-by: Peter Zijlstra Signed-off-by: Suren Baghdasaryan Reviewed-by: David Hildenbrand Reviewed-by: Liam R. Howlett --- Applies over Linus' ToT Changes since v2 [1] - Added SOB, per David Hildenbrand - Added SOB, per Liam Howlett [1] https://lore.kernel.org/all/20241121162826.987947-1-surenb@google.com/ include/linux/seqlock.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) base-commit: 28eb75e178d389d325f1666e422bc13bbbb9804c diff --git a/include/linux/seqlock.h b/include/linux/seqlock.h index 5298765d6ca4..22c2c48b4265 100644 --- a/include/linux/seqlock.h +++ b/include/linux/seqlock.h @@ -318,6 +318,28 @@ SEQCOUNT_LOCKNAME(mutex, struct mutex, true, mutex) __seq; \ }) +/** + * raw_seqcount_try_begin() - begin a seqcount_t read critical section + * w/o lockdep and w/o counter stabilization + * @s: Pointer to seqcount_t or any of the seqcount_LOCKNAME_t variants + * + * Similar to raw_seqcount_begin(), except it enables eliding the critical + * section entirely if odd, instead of doing the speculation knowing it will + * fail. + * + * Useful when counter stabilization is more or less equivalent to taking + * the lock and there is a slowpath that does that. + * + * If true, start will be set to the (even) sequence count read. + * + * Return: true when a read critical section is started. + */ +#define raw_seqcount_try_begin(s, start) \ +({ \ + start = raw_read_seqcount(s); \ + !(start & 1); \ +}) + /** * raw_seqcount_begin() - begin a seqcount_t read critical section w/o * lockdep and w/o counter stabilization