Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp1105326rdh; Fri, 24 Nov 2023 05:29:55 -0800 (PST) X-Google-Smtp-Source: AGHT+IHI7Aeqd8eiVchaLB2mIh6zZVDH5m5vsjg6AZgiap0F2luQ8Qd5F0VzDzy4UFH5FYKn8BcN X-Received: by 2002:aa7:98c2:0:b0:6b4:ac0e:2f70 with SMTP id e2-20020aa798c2000000b006b4ac0e2f70mr3315541pfm.29.1700832595124; Fri, 24 Nov 2023 05:29:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700832595; cv=none; d=google.com; s=arc-20160816; b=I5vXkGkkbrqVCVLvvHwWoOi24zv5mT7DfH6C97xLflWJVBjgCAVz+Vl8vEmeovH78c 4FcboZy93S6Gzv7QvM0udS+C6kjN6bQsNU7zh0IKnZ6BUC3f7sHQ9yd3CdJs/Upm8Ug1 +YBTe9re0AA6VV+OVw6anZ5KmJExGHzHFOh0bJAlnrEIsqImdEp6Ni/RbsuwvlTXp0JH j4fMF5KzUPG6fl43jQJjz1+eCUDK8llRZ0PxpXztgN4TW+n0BPu8tclENVstDqNw1nkT /vjOhZb7UNg/j3E7JTL6J1yvOcL37oWnCtzd7rY3d8z5KeE2CbSkKUBJm/8aEQe8TGNY OJaw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=uVx5jgkX7VDXu2BOJ112MWVTaXFaq3iLz1p8EjK2pug=; fh=UVV9UP3jB+jR+DJP/Pn6IwXb1P9vDh5E3FRL6G+mlkU=; b=lXINxmrnczI/d5/Y838bc1Rf811XbK5FIIEzLg5+PG2ypCLPKVzGImoJqRJIVQzSrQ JtkALw6r+lenCaYeFjgjgezIOwwKYOi+uhD6TrO1gQDP5DNmiUzUP2EaDSQFlAcKAlt/ QJjgAeCSXFm2Nd/4flgozYInMPgdsGMBSsyqfaQpYdfSRTndR/pq02Pqc+J40gWicG80 RixftY3ajeBgY/4Kp11TufpSCR1XqXMp2jRabi5qKGUlNUU1I15cQWG3jTay9Nff1IvR vK7PCrDkXc7Pj+clZsInMHOyX0X8qD82GHxSZeso89Wrx/c//JoTseRR+Qr2CzvcfaTW dqtQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=TiePEiML; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id d19-20020a63fd13000000b005b928e39429si3565834pgh.259.2023.11.24.05.29.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Nov 2023 05:29:55 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=TiePEiML; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id E72BE8030A56; Fri, 24 Nov 2023 05:29:51 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345295AbjKXN33 (ORCPT + 99 others); Fri, 24 Nov 2023 08:29:29 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58270 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235292AbjKXN2h (ORCPT ); Fri, 24 Nov 2023 08:28:37 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D26EB213A for ; Fri, 24 Nov 2023 05:27:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700832461; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=uVx5jgkX7VDXu2BOJ112MWVTaXFaq3iLz1p8EjK2pug=; b=TiePEiMLX8kRem+4vM/XteE8B3g580adOI01NCZjErMBifJmMhxl5rTjFWFDkK4dEdcB+I 9QujlixgjEb+wcWOoOXeBOH789xzWDV/gbB8RXHc88HLiK819wT5QlzV2ZOOam5HGanfX1 Aohc8fxxDfB9PiKyUkL6eCoatGEWqtM= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-77-x3WGX4woMCePcALEhUGVJg-1; Fri, 24 Nov 2023 08:27:37 -0500 X-MC-Unique: x3WGX4woMCePcALEhUGVJg-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E919E185A781; Fri, 24 Nov 2023 13:27:36 +0000 (UTC) Received: from t14s.fritz.box (unknown [10.39.194.71]) by smtp.corp.redhat.com (Postfix) with ESMTP id 887C32166B2A; Fri, 24 Nov 2023 13:27:33 +0000 (UTC) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, David Hildenbrand , Andrew Morton , Linus Torvalds , Ryan Roberts , Matthew Wilcox , Hugh Dickins , Yin Fengwei , Yang Shi , Ying Huang , Zi Yan , Peter Zijlstra , Ingo Molnar , Will Deacon , Waiman Long , "Paul E. McKenney" Subject: [PATCH WIP v1 18/20] atomic_seqcount: use atomic add-return instead of atomic cmpxchg on 64bit Date: Fri, 24 Nov 2023 14:26:23 +0100 Message-ID: <20231124132626.235350-19-david@redhat.com> In-Reply-To: <20231124132626.235350-1-david@redhat.com> References: <20231124132626.235350-1-david@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.6 X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Fri, 24 Nov 2023 05:29:52 -0800 (PST) Turns out that it can be beneficial on some HW to use an add-return instead of and atomic cmpxchg. However, we have to deal with more possible races now: in the worst case, each and every CPU might try becoming the exclusive writer at the same time, so we need the same number of bits as for the shared writer case. In case we detect that we didn't end up being the exclusive writer, simply back off and convert to a shared writer. Only implement this optimization on 64bit, where we can steal more bits from the actual sequence without sorrow. Signed-off-by: David Hildenbrand --- include/linux/atomic_seqcount.h | 43 +++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/include/linux/atomic_seqcount.h b/include/linux/atomic_seqcount.h index 00286a9da221..9cd40903863d 100644 --- a/include/linux/atomic_seqcount.h +++ b/include/linux/atomic_seqcount.h @@ -42,9 +42,10 @@ typedef struct raw_atomic_seqcount { #define ATOMIC_SEQCOUNT_SHARED_WRITERS_MAX 0x0000000000008000ul #define ATOMIC_SEQCOUNT_SHARED_WRITERS_MASK 0x000000000000fffful #define ATOMIC_SEQCOUNT_EXCLUSIVE_WRITER 0x0000000000010000ul -#define ATOMIC_SEQCOUNT_WRITERS_MASK 0x000000000001fffful -/* We have 48bit for the actual sequence. */ -#define ATOMIC_SEQCOUNT_SEQUENCE_STEP 0x0000000000020000ul +#define ATOMIC_SEQCOUNT_EXCLUSIVE_WRITERS_MASK 0x00000000ffff0000ul +#define ATOMIC_SEQCOUNT_WRITERS_MASK 0x00000000fffffffful +/* We have 32bit for the actual sequence. */ +#define ATOMIC_SEQCOUNT_SEQUENCE_STEP 0x0000000100000000ul #else /* CONFIG_64BIT */ @@ -53,6 +54,7 @@ typedef struct raw_atomic_seqcount { #define ATOMIC_SEQCOUNT_SHARED_WRITERS_MAX 0x00000040ul #define ATOMIC_SEQCOUNT_SHARED_WRITERS_MASK 0x0000007ful #define ATOMIC_SEQCOUNT_EXCLUSIVE_WRITER 0x00000080ul +#define ATOMIC_SEQCOUNT_EXCLUSIVE_WRITERS_MASK 0x00000080ul #define ATOMIC_SEQCOUNT_WRITERS_MASK 0x000000fful /* We have 24bit for the actual sequence. */ #define ATOMIC_SEQCOUNT_SEQUENCE_STEP 0x00000100ul @@ -144,7 +146,7 @@ static inline bool raw_read_atomic_seqcount_retry(raw_atomic_seqcount_t *s, static inline bool raw_write_atomic_seqcount_begin(raw_atomic_seqcount_t *s, bool try_exclusive) { - unsigned long seqcount, seqcount_new; + unsigned long __maybe_unused seqcount, seqcount_new; BUILD_BUG_ON(IS_ENABLED(CONFIG_PREEMPT_RT)); #ifdef CONFIG_DEBUG_ATOMIC_SEQCOUNT @@ -160,6 +162,32 @@ static inline bool raw_write_atomic_seqcount_begin(raw_atomic_seqcount_t *s, if (unlikely(seqcount & ATOMIC_SEQCOUNT_WRITERS_MASK)) goto shared; +#ifdef CONFIG_64BIT + BUILD_BUG_ON(__builtin_popcount(ATOMIC_SEQCOUNT_EXCLUSIVE_WRITERS_MASK) != + __builtin_popcount(ATOMIC_SEQCOUNT_SHARED_WRITERS_MASK)); + + /* See comment for atomic_long_try_cmpxchg() below. */ + seqcount = atomic_long_add_return(ATOMIC_SEQCOUNT_EXCLUSIVE_WRITER, + &s->sequence); + if (likely((seqcount & ATOMIC_SEQCOUNT_WRITERS_MASK) == + ATOMIC_SEQCOUNT_EXCLUSIVE_WRITER)) + return true; + + /* + * Whoops, we raced with another writer. Back off, converting ourselves + * to a shared writer and wait for any exclusive writers. + */ + atomic_long_add(ATOMIC_SEQCOUNT_SHARED_WRITER - ATOMIC_SEQCOUNT_EXCLUSIVE_WRITER, + &s->sequence); + /* + * No need for __smp_mb__after_atomic(): the reader side already + * realizes that it has to retry and the memory barrier from + * atomic_long_add_return() is sufficient for that. + */ + while (atomic_long_read(&s->sequence) & ATOMIC_SEQCOUNT_EXCLUSIVE_WRITERS_MASK) + cpu_relax(); + return false; +#else seqcount_new = seqcount | ATOMIC_SEQCOUNT_EXCLUSIVE_WRITER; /* * Store the sequence before any store in the critical section. Further, @@ -168,6 +196,7 @@ static inline bool raw_write_atomic_seqcount_begin(raw_atomic_seqcount_t *s, */ if (atomic_long_try_cmpxchg(&s->sequence, &seqcount, seqcount_new)) return true; +#endif shared: /* * Indicate that there is a shared writer, and spin until the exclusive @@ -185,10 +214,10 @@ static inline bool raw_write_atomic_seqcount_begin(raw_atomic_seqcount_t *s, DEBUG_LOCKS_WARN_ON((seqcount & ATOMIC_SEQCOUNT_SHARED_WRITERS_MASK) > ATOMIC_SEQCOUNT_SHARED_WRITERS_MAX); #endif /* CONFIG_DEBUG_ATOMIC_SEQCOUNT */ - if (likely(!(seqcount & ATOMIC_SEQCOUNT_EXCLUSIVE_WRITER))) + if (likely(!(seqcount & ATOMIC_SEQCOUNT_EXCLUSIVE_WRITERS_MASK))) return false; - while (atomic_long_read(&s->sequence) & ATOMIC_SEQCOUNT_EXCLUSIVE_WRITER) + while (atomic_long_read(&s->sequence) & ATOMIC_SEQCOUNT_EXCLUSIVE_WRITERS_MASK) cpu_relax(); return false; } @@ -209,7 +238,7 @@ static inline void raw_write_atomic_seqcount_end(raw_atomic_seqcount_t *s, if (likely(exclusive)) { #ifdef CONFIG_DEBUG_ATOMIC_SEQCOUNT DEBUG_LOCKS_WARN_ON(!(atomic_long_read(&s->sequence) & - ATOMIC_SEQCOUNT_EXCLUSIVE_WRITER)); + ATOMIC_SEQCOUNT_EXCLUSIVE_WRITERS_MASK)); #endif /* CONFIG_DEBUG_ATOMIC_SEQCOUNT */ val -= ATOMIC_SEQCOUNT_EXCLUSIVE_WRITER; } else { -- 2.41.0