From patchwork Tue Aug 13 18:26:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Lameter via B4 Relay X-Patchwork-Id: 13762395 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 08880C52D7B for ; Tue, 13 Aug 2024 18:26:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9987D6B009F; Tue, 13 Aug 2024 14:26:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9489A6B00A0; Tue, 13 Aug 2024 14:26:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7EA5A6B00A1; Tue, 13 Aug 2024 14:26:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 608DA6B009F for ; Tue, 13 Aug 2024 14:26:41 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id D6A58140995 for ; Tue, 13 Aug 2024 18:26:40 +0000 (UTC) X-FDA: 82448052960.30.61253B0 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf27.hostedemail.com (Postfix) with ESMTP id 2D36540008 for ; Tue, 13 Aug 2024 18:26:36 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=MEehcSPu; spf=pass (imf27.hostedemail.com: domain of devnull+cl.gentwo.org@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=devnull+cl.gentwo.org@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723573528; h=from:from:sender:reply-to: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:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=IUlgUM/k+WLUrPh6rOC2bMGI70Eqa0v5fMhivxOvkUM=; b=5HMc+Xt0qLAuD2WaHsc5jKa28iOzqcz4yTUWo8FRZpWu9bX3v49vziS6pYQ22mIBKr5fG6 5A4FRebYc2wjjsxCI5tjAUdTFpT3XsjYi73IRoS2RJmqx732s2/u9lwsKjJGD92ButyRU5 HXZyv5BCeLwjt4HMRq16qMrqpinBEsE= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723573528; a=rsa-sha256; cv=none; b=6Hc0f2Uc+GY5YRD4DscaMq03ka2ChXEwENKJbvyBv2FJRYmLKPtEuhp5bJR7ZIJvtIlTlK aDFrSbpT1KAU4haJrWwbSkSrVsI6Kfs95a2pmhcvClhBV4NimEdl+gjWAuN6cujgnZW0MR lEkD7yM3JXreBGTrw1KwA5Ga4Dj66A4= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=MEehcSPu; spf=pass (imf27.hostedemail.com: domain of devnull+cl.gentwo.org@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=devnull+cl.gentwo.org@kernel.org; dmarc=pass (policy=none) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 4C9D8CE0C6E; Tue, 13 Aug 2024 18:26:33 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPS id 71E48C32782; Tue, 13 Aug 2024 18:26:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723573592; bh=PIiGnmzc+A6sckKGHJxXz1bfdoMHMEy2aBNf2hkDvwU=; h=From:Date:Subject:To:Cc:Reply-To:From; b=MEehcSPuG7zpZzGxfzwM8P2k8fThb0XZgr/bk+7kCaDyOC8lVQ5zIdWWB7eLwz+2V fschJdbPUGscswYRZVMmbKwFUE7mW3pCwVUgufXp2AExQRcdRIX5ZKImS+QTVA0e21 5v2AJju9NktE9i5KkJ6SEifGeEXV7mnuTlc2qsVF8LERV2D/M/eXqtIhPVEcwaupTH HTNvlSjROLefZNDdeMfqfzqyHwpgPz3kfxT9Y0y0sTbzNngXEjXFm4JkV2ZL0Q48lH pAG3PYKiq4m2s6TXQT8TbLrKfzQ/J0+UD+FKADRdIBq0IML/1qoF+JpsaPSb4Y2eda Q0mnKxt8sGdYQ== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 661BCC52D7B; Tue, 13 Aug 2024 18:26:32 +0000 (UTC) From: Christoph Lameter via B4 Relay Date: Tue, 13 Aug 2024 11:26:17 -0700 Subject: [PATCH RFC] Avoid memory barrier in read_seqcount() through load acquire MIME-Version: 1.0 Message-Id: <20240813-seq_optimize-v1-1-84d57182e6a7@gentwo.org> X-B4-Tracking: v=1; b=H4sIAEilu2YC/6tWKk4tykwtVrJSqFYqSi3LLM7MzwNyDHUUlJIzE vPSU3UzU4B8JSMDIxMDC0Nj3eLUwvj8gpLM3MyqVF0zi2QTCzNLs2RzSwsloJaCotS0zAqwcdG xtbUAbA2//14AAAA= To: Catalin Marinas , Will Deacon , Peter Zijlstra , Ingo Molnar , Waiman Long , Boqun Feng Cc: Linus Torvalds , linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, "Christoph Lameter (Ampere)" X-Mailer: b4 0.15-dev-37811 X-Developer-Signature: v=1; a=ed25519-sha256; t=1723573592; l=4580; i=cl@gentwo.org; s=20240811; h=from:subject:message-id; bh=3Nopmj43IyKUcz7M69aLBmi74hfW/6pSaf4yxvR7/js=; b=orwijZITJDz6E0KNga7pCM4Wi5DczM6Xc1j7fdfFedU1/4pZqMoo3LPIQ2AWagSJhdaNGyKoB Cce6zJOs4Y8B/NK8tdDYEhFbS+c7qm+ZZOFwalWJiYhiPkhn2rVgfn3 X-Developer-Key: i=cl@gentwo.org; a=ed25519; pk=I7gqGwDi9drzCReFIuf2k9de1FI1BGibsshXI0DIvq8= X-Endpoint-Received: by B4 Relay for cl@gentwo.org/20240811 with auth_id=194 X-Original-From: "Christoph Lameter (Ampere)" Reply-To: cl@gentwo.org X-Rspamd-Queue-Id: 2D36540008 X-Stat-Signature: woc4karsoabuapo1oneggrt3xiutwn8k X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1723573596-850841 X-HE-Meta: U2FsdGVkX1/Y+fW9WKkOJL3mIkIXgjaHsX8Y77GAKH4cEqC6zk912RWvOhh4eOlEZIcMEypm0zX1xG4MGH5QoqcRWqM/f6IAhQchIqy5XFCwYM1OY5OOwz/ZkOgLGTXgqmqRyTCj/iORrbZnK1UFRynBO5/9czpoRHjsGMRodXIwdxNksS/kerRpOPnWnELBtdhE7bqyMnie7f37H8f6XsFTbJ3rba7BOSFloge7aaWJyZiq6gIjcj/b1L6ivqLhUYR6xn9LjOzKUpGjgWW/klYJnxwzhiAL+frKDz8kk0MFf0AVJpLEfXq6liWIP6dNUNo6tYjJ04UkoO/EZQ2meStzNxBEYmc/IPnRFmR4W2kvVsr/TrH03TAg3k8V1WW6k1SlREl8BKJJMBR42iRlcLLjpmvszL+sKSbKIXFdjQ2YyYA3chtPUcwJheZaOWOW6mRp3VLPvH4Sk5+E4GAgsnkNwrJgZShQ6tOXFO67k7kV92/BrvI+ro9tvPpzX9w4qa24QITW0htgzwhioIrmZUzzZktH7Kp9ZFqW2dxCykBIL51QZ1YOQDQZ6kUB3RGYenCmvfUrCAUM3DGpK9JYvmuQwk1FtA8nSk8ytVyvNWKe1185tfsOqaTVdjMtYh8wLxhJyZuLDcMVlhAdKfui6k1n8kgz8MBRovRJxNKWtSXcJiMfEb9U+QWjkJ8Jv1Pji6yqswA07JBOHCexdoUDLvhZLDsf0voG6NSyDTi1FG4U6X1oW2IM1fyakdiE8jygZZT4T9yPsWS9wjFrwuR0lThmKMtSXXypWcamriYprSnDvib4VFQo+XWsHjdG3j5mAm8+z6I3T0vQjl2c3KVHLWxpPQK5mtmEZj86nqpDQ4veaR0l/B71Zrzy6/sif2z3AfmIcephjukrw3bDSsXuT58c71sNo4b6i74K3vS/sf9Gnaxmi3NjRIPNsRKeRSXxD1/C3ggqgRW3gvmSuVC ByIumd97 PtX742EWqceeod1MNSAPHP6lyZ5631Npkt08xqHKHIYfTxXChZDjCj626zMu1GBYJl/apW5el4MLJIochSTU0iJTYhw6bnGF7CDGBov5RWRCTdFazHGLj1bTiMvmq1IkiSoc1EqmaOy7fp+f3NhrHMa+nuaOZVcJNutuYplNvHcBXBfidXo6jy+s69UP8VWLyZxKh3+040qg+Ums2uLW59gXqWa++zio8SFGgogSWsZkpZuHu3zHf1cxqr+GJ7aML2irZ6F4QuPJ/0zeEwoLlPOmc0k3pZZknxkngWkqKsXghC3Pr6/9BlzWchWEB9KzgKvOY370hdFqNrW45d6SsmDCusZPdd3F9WFop/F7NO2DUFSBvDQta05V6ZZfug3hSu4uUTUwHv2iv0NJscwU5MKHN1JvkqLufjXZcnldVW7m+jo1WjMml1h8YYQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: "Christoph Lameter (Ampere)" Some architectures support load acquire which can save us a memory barrier and save some cycles. A typical sequence do { seq = read_seqcount_begin(&s); } while (read_seqcount_retry(&s, seq); requires 13 cycles on ARM64 for an empty loop. Two read memory barriers are needed. One for each of the seqcount_* functions. We can replace the first read barrier with a load acquire of the seqcount which saves us one barrier. On ARM64 doing so reduces the cycle count from 13 to 8. Signed-off-by: Christoph Lameter (Ampere) --- arch/Kconfig | 5 +++++ arch/arm64/Kconfig | 1 + include/linux/seqlock.h | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+) --- base-commit: 6b4aa469f04999c3f244515fa7491b4d093c5167 change-id: 20240813-seq_optimize-68c48696c798 Best regards, diff --git a/arch/Kconfig b/arch/Kconfig index 975dd22a2dbd..3f8867110a57 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -1600,6 +1600,11 @@ config ARCH_HAS_KERNEL_FPU_SUPPORT Architectures that select this option can run floating-point code in the kernel, as described in Documentation/core-api/floating-point.rst. +config ARCH_HAS_ACQUIRE_RELEASE + bool + help + Architectures that support acquire / release can avoid memory fences + source "kernel/gcov/Kconfig" source "scripts/gcc-plugins/Kconfig" diff --git a/arch/arm64/Kconfig b/arch/arm64/Kconfig index a2f8ff354ca6..19e34fff145f 100644 --- a/arch/arm64/Kconfig +++ b/arch/arm64/Kconfig @@ -39,6 +39,7 @@ config ARM64 select ARCH_HAS_PTE_DEVMAP select ARCH_HAS_PTE_SPECIAL select ARCH_HAS_HW_PTE_YOUNG + select ARCH_HAS_ACQUIRE_RELEASE select ARCH_HAS_SETUP_DMA_OPS select ARCH_HAS_SET_DIRECT_MAP select ARCH_HAS_SET_MEMORY diff --git a/include/linux/seqlock.h b/include/linux/seqlock.h index d90d8ee29d81..353fcf32b800 100644 --- a/include/linux/seqlock.h +++ b/include/linux/seqlock.h @@ -176,6 +176,28 @@ __seqprop_##lockname##_sequence(const seqcount_##lockname##_t *s) \ return seq; \ } \ \ +static __always_inline unsigned \ +__seqprop_##lockname##_sequence_acquire(const seqcount_##lockname##_t *s) \ +{ \ + unsigned seq = smp_load_acquire(&s->seqcount.sequence); \ + \ + if (!IS_ENABLED(CONFIG_PREEMPT_RT)) \ + return seq; \ + \ + if (preemptible && unlikely(seq & 1)) { \ + __SEQ_LOCK(lockbase##_lock(s->lock)); \ + __SEQ_LOCK(lockbase##_unlock(s->lock)); \ + \ + /* \ + * Re-read the sequence counter since the (possibly \ + * preempted) writer made progress. \ + */ \ + seq = smp_load_acquire(&s->seqcount.sequence); \ + } \ + \ + return seq; \ +} \ + \ static __always_inline bool \ __seqprop_##lockname##_preemptible(const seqcount_##lockname##_t *s) \ { \ @@ -211,6 +233,11 @@ static inline unsigned __seqprop_sequence(const seqcount_t *s) return READ_ONCE(s->sequence); } +static inline unsigned __seqprop_sequence_acquire(const seqcount_t *s) +{ + return smp_load_acquire(&s->sequence); +} + static inline bool __seqprop_preemptible(const seqcount_t *s) { return false; @@ -259,6 +286,7 @@ SEQCOUNT_LOCKNAME(mutex, struct mutex, true, mutex) #define seqprop_ptr(s) __seqprop(s, ptr)(s) #define seqprop_const_ptr(s) __seqprop(s, const_ptr)(s) #define seqprop_sequence(s) __seqprop(s, sequence)(s) +#define seqprop_sequence_acquire(s) __seqprop(s, sequence_acquire)(s) #define seqprop_preemptible(s) __seqprop(s, preemptible)(s) #define seqprop_assert(s) __seqprop(s, assert)(s) @@ -293,6 +321,18 @@ SEQCOUNT_LOCKNAME(mutex, struct mutex, true, mutex) * * Return: count to be passed to read_seqcount_retry() */ +#ifdef CONFIG_ARCH_HAS_ACQUIRE_RELEASE +#define raw_read_seqcount_begin(s) \ +({ \ + unsigned _seq; \ + \ + while ((_seq = seqprop_sequence_acquire(s)) & 1) \ + cpu_relax(); \ + \ + kcsan_atomic_next(KCSAN_SEQLOCK_REGION_MAX); \ + _seq; \ +}) +#else #define raw_read_seqcount_begin(s) \ ({ \ unsigned _seq = __read_seqcount_begin(s); \ @@ -300,6 +340,7 @@ SEQCOUNT_LOCKNAME(mutex, struct mutex, true, mutex) smp_rmb(); \ _seq; \ }) +#endif /** * read_seqcount_begin() - begin a seqcount_t read critical section