Received: by 2002:a05:6a10:6006:0:0:0:0 with SMTP id w6csp845691pxa; Thu, 27 Aug 2020 18:08:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwNa5UAjnGjC/3kFIfrYatVQV/cnaIN93EaUMTw9WC0R6mXGkXFc8uDcmi58ATtv0k9I8Gn X-Received: by 2002:a17:906:1b:: with SMTP id 27mr23519400eja.517.1598576897908; Thu, 27 Aug 2020 18:08:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598576897; cv=none; d=google.com; s=arc-20160816; b=UkWu2sqmSDZmtn5H8QYiXtMnsyZnGJqPNPsfK8ki/CDgWyHjbidwRdQCwJe7ZDzhE8 ycbX4jGL62utYd9AKI3A9n6+3xhSMlYwcf71hIt11k72NCmi8l7pg+UnT7fcYJTtAgyP lXM92YlG8ysEDU5cR8bQyJNYPVGePfEi42tXIOPuSSIAo1XNcn+vNHUGfLrqnAOI2BxE B85sfALa1PQxF/xQyaZob0UXP+OCSoBFHYS33fhVyLrlkpoK9YsEe0sbVG1dQhFxLu9C bNBon0WbVvYeVKYtUu1Zm4aLuJCzJQ24r4G3JIpOUNBKpfqIz1cypK687RDlwBbpliFe txLA== 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 :references:in-reply-to:message-id:date:subject:cc:to:dkim-signature :dkim-signature:from; bh=5IsFEQ2Eog5P+tP1GO7w5qYzNlrjSXGVQKMj3BCXPpI=; b=kLQFq85pTSTlZgXw14MiYfh6XiBUebMVw1cxveoXXjd2lHtUII01yLDf8WzwHLuQvd sQKu93hYhwI0rRowcYZZTcIZ9XXIW2rou/LuS0zTfhrcDzeI5xLcIX1RappfAzyICVxa /lSdfEjnmLDxjp6RmxyD1eQOAtXDWULKaMvJ4OAA4HvluXUW+mzcX+dOU5lo6IW3BWg7 vqVd4Vowt5Bkyv0Ted8M0p8LejVxXsA2XZarfu1wUsJ8xDY/GmPgw+l9YTfXHmWYDnXX OQ59HsDJE7k7SzHKI0ruEoypyUrd1m59oY7XE2jsUCopd8L7epj5Q2sFyUy4Ymzkfbkd xJMA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=TjFgHUnh; dkim=neutral (no key) header.i=@vger.kernel.org; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ec15si2328690ejb.236.2020.08.27.18.07.55; Thu, 27 Aug 2020 18:08:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=TjFgHUnh; dkim=neutral (no key) header.i=@vger.kernel.org; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728182AbgH1BHH (ORCPT + 99 others); Thu, 27 Aug 2020 21:07:07 -0400 Received: from Galois.linutronix.de ([193.142.43.55]:41876 "EHLO galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726147AbgH1BHF (ORCPT ); Thu, 27 Aug 2020 21:07:05 -0400 From: "Ahmed S. Darwish" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1598576823; 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=5IsFEQ2Eog5P+tP1GO7w5qYzNlrjSXGVQKMj3BCXPpI=; b=TjFgHUnh91oXg8QkHtGOBI8E6nRwxWRVh1CeJ1lkf5Tyvio04ULx654s6OROxuobtXVjAM 6V90iaHLrYfWfCt6gI5jtcdCK71I5kmr2sut4PZT7++CzqqEyjmqt1n0FTsF3FYK22h5D0 iKQZDQAlxINNNeWSbal9IhGYBKcCCqqiRWoV2AhFlnzspHfAEzOofr6wwbnmaC4GxSbWg/ S8/L78Rh8fvGxg5aVWjHtstKCKe05Ha17a2gyl2nkK78xwWEJlYr41dljPh43nRgPN/Hhj myAF7VvuHQJWsdlcrAyXOT5YWWhB3N+QnvuD+oX/mwoTZC5OrmtUKTWaiddjIg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1598576823; 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=5IsFEQ2Eog5P+tP1GO7w5qYzNlrjSXGVQKMj3BCXPpI=; b=a4TSuQahA4c8tb+Y3+kNa+5/7mWU4NSgttfbyqS4LiJDGWsZL3+jpPYX+GWby1f+5Wb7o5 LSzeczoTNZMPakDw== To: Peter Zijlstra , Ingo Molnar , Will Deacon Cc: Thomas Gleixner , "Sebastian A. Siewior" , "Paul E. McKenney" , Steven Rostedt , LKML , "Ahmed S. Darwish" Subject: [PATCH v1 3/5] seqlock: seqcount_t: Implement all read APIs as statement expressions Date: Fri, 28 Aug 2020 03:07:08 +0200 Message-Id: <20200828010710.5407-4-a.darwish@linutronix.de> In-Reply-To: <20200828010710.5407-1-a.darwish@linutronix.de> References: <20200519214547.352050-1-a.darwish@linutronix.de> <20200828010710.5407-1-a.darwish@linutronix.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The sequence counters read APIs are implemented as CPP macros, so they can take either seqcount_t or any of the seqcount_LOCKTYPE_t variants. Such macros then get *directly* transformed to internal C functions that only take plain seqcount_t. Further commits need access to seqcount_LOCKTYPE_t inside of the actual read APIs code. Thus transform all of the seqcount read APIs to pure GCC statement expressions instead. This will not break type-safety: all of the transformed APIs resolve to a _Generic() selection that does not have a "default" case. This will also not affect the transformed APIs readability: previously added kernel-doc above all of seqlock.h functions makes the expectations quite clear for call-site developers. Signed-off-by: Ahmed S. Darwish --- include/linux/seqlock.h | 98 ++++++++++++++++++++--------------------- 1 file changed, 49 insertions(+), 49 deletions(-) diff --git a/include/linux/seqlock.h b/include/linux/seqlock.h index 5470e9cd52ce..d114a9f4e9d9 100644 --- a/include/linux/seqlock.h +++ b/include/linux/seqlock.h @@ -182,6 +182,12 @@ __seqcount_##locktype##_ptr(seqcount_##locktype##_t *s) \ return &s->seqcount; \ } \ \ +static __always_inline unsigned \ +__seqcount_##locktype##_sequence(const seqcount_##locktype##_t *s) \ +{ \ + return READ_ONCE(s->seqcount.sequence); \ +} \ + \ static __always_inline bool \ __seqcount_##locktype##_preemptible(const seqcount_##locktype##_t *s) \ { \ @@ -203,6 +209,11 @@ static inline seqcount_t *__seqcount_t_ptr(seqcount_t *s) return s; } +static inline unsigned __seqcount_t_sequence(const seqcount_t *s) +{ + return READ_ONCE(s->sequence); +} + static inline bool __seqcount_t_preemptible(const seqcount_t *s) { return false; @@ -248,6 +259,7 @@ SEQCOUNT_LOCKTYPE(ww_mutex, struct ww_mutex, true, &s->lock->base) __seqprop_case((s), ww_mutex, prop)) #define __seqcount_ptr(s) __seqprop(s, ptr) +#define __seqcount_sequence(s) __seqprop(s, sequence) #define __seqcount_lock_preemptible(s) __seqprop(s, preemptible) #define __seqcount_assert_lock_held(s) __seqprop(s, assert) @@ -266,21 +278,19 @@ SEQCOUNT_LOCKTYPE(ww_mutex, struct ww_mutex, true, &s->lock->base) * Return: count to be passed to read_seqcount_retry() */ #define __read_seqcount_begin(s) \ - __read_seqcount_t_begin(__seqcount_ptr(s)) - -static inline unsigned __read_seqcount_t_begin(const seqcount_t *s) -{ - unsigned ret; - -repeat: - ret = READ_ONCE(s->sequence); - if (unlikely(ret & 1)) { - cpu_relax(); - goto repeat; - } - kcsan_atomic_next(KCSAN_SEQLOCK_REGION_MAX); - return ret; -} +({ \ + unsigned seq; \ + \ + do { \ + seq = __seqcount_sequence(s); \ + if (likely(! (seq & 1))) \ + break; \ + cpu_relax(); \ + } while (true); \ + \ + kcsan_atomic_next(KCSAN_SEQLOCK_REGION_MAX); \ + seq; \ +}) /** * raw_read_seqcount_begin() - begin a seqcount_t read section w/o lockdep @@ -289,14 +299,12 @@ static inline unsigned __read_seqcount_t_begin(const seqcount_t *s) * Return: count to be passed to read_seqcount_retry() */ #define raw_read_seqcount_begin(s) \ - raw_read_seqcount_t_begin(__seqcount_ptr(s)) - -static inline unsigned raw_read_seqcount_t_begin(const seqcount_t *s) -{ - unsigned ret = __read_seqcount_t_begin(s); - smp_rmb(); - return ret; -} +({ \ + unsigned seq = __read_seqcount_begin(s); \ + \ + smp_rmb(); \ + seq; \ +}) /** * read_seqcount_begin() - begin a seqcount_t read critical section @@ -305,13 +313,10 @@ static inline unsigned raw_read_seqcount_t_begin(const seqcount_t *s) * Return: count to be passed to read_seqcount_retry() */ #define read_seqcount_begin(s) \ - read_seqcount_t_begin(__seqcount_ptr(s)) - -static inline unsigned read_seqcount_t_begin(const seqcount_t *s) -{ - seqcount_lockdep_reader_access(s); - return raw_read_seqcount_t_begin(s); -} +({ \ + seqcount_lockdep_reader_access(__seqcount_ptr(s)); \ + raw_read_seqcount_begin(s); \ +}) /** * raw_read_seqcount() - read the raw seqcount_t counter value @@ -325,15 +330,13 @@ static inline unsigned read_seqcount_t_begin(const seqcount_t *s) * Return: count to be passed to read_seqcount_retry() */ #define raw_read_seqcount(s) \ - raw_read_seqcount_t(__seqcount_ptr(s)) - -static inline unsigned raw_read_seqcount_t(const seqcount_t *s) -{ - unsigned ret = READ_ONCE(s->sequence); - smp_rmb(); - kcsan_atomic_next(KCSAN_SEQLOCK_REGION_MAX); - return ret; -} +({ \ + unsigned seq = __seqcount_sequence(s); \ + \ + smp_rmb(); \ + kcsan_atomic_next(KCSAN_SEQLOCK_REGION_MAX); \ + seq; \ +}) /** * raw_seqcount_begin() - begin a seqcount_t read critical section w/o @@ -353,16 +356,13 @@ static inline unsigned raw_read_seqcount_t(const seqcount_t *s) * Return: count to be passed to read_seqcount_retry() */ #define raw_seqcount_begin(s) \ - raw_seqcount_t_begin(__seqcount_ptr(s)) - -static inline unsigned raw_seqcount_t_begin(const seqcount_t *s) -{ - /* - * If the counter is odd, let read_seqcount_retry() fail - * by decrementing the counter. - */ - return raw_read_seqcount_t(s) & ~1; -} +({ \ + /* \ + * If the counter is odd, let read_seqcount_retry() fail \ + * by decrementing the counter. \ + */ \ + raw_read_seqcount(s) & ~1; \ +}) /** * __read_seqcount_retry() - end a seqcount_t read section w/o barrier -- 2.28.0