Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp3110506rwe; Mon, 29 Aug 2022 06:06:35 -0700 (PDT) X-Google-Smtp-Source: AA6agR53CIqy2MhVKHpAc625nGjaCry8coAoMAATGM567n0+eAOtKqF3ou5sQbjPo9BNltevBUw9 X-Received: by 2002:a17:907:2d12:b0:731:6a4e:ceb0 with SMTP id gs18-20020a1709072d1200b007316a4eceb0mr13777707ejc.115.1661778394714; Mon, 29 Aug 2022 06:06:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661778394; cv=none; d=google.com; s=arc-20160816; b=teWYx6PAlKrtf/35fwsOnXQpG1SkJom3dGQzOdW/M+oj1Nrv+iIi82M/w9NU2Q9LaD p6OZo4APvJdCJJAk+TXFcvTsxRynEJQ3mc6DZkUcgsQ06ti1JVjm4TVD9SHEH7yzcEJo ZRg8zaV0i4kXdQW/2qMztZUzZGdzRLT/hTXc1oQow/i5tidCxR8lWVL/a0QFzjqgIvMr 9boV56QpFbjnwWr6vtU0R3ziEE5WQgdhEW0IIROFFnIkDFGppiTv4RMTpOJzgWQZJ8zZ SALMjcpZgdfeHg01wrrtdXFqnKsO3YV/LKcnsuQQeyP82UNiU3J3TyKnxWg+hJAQMzXM ZV0Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=o7nFvD+bAzzd8hTxdy6J5SFI8QAvwQGuiGUQWboy0hk=; b=fcZoa2WW4Jy6cC4A+vdCIs6+fnrCSF/geWTZXC7789ISoIHNuHtXp6QzcbmYCxjGvW QIw3DImd5kC7JafGqoTECI7gyWcn7c5mkYPU+UzOZ6zvcsAtKfaV9dN12xriwnawTBjV qVc3XuqU7qs3Wj9pb3L/UwwyCwxF621V4vlA+1ePvi3t3SOYGkyLDtiLS+jKZqB7Z5iF snI2bTbnVI4I/gp2WDaYgM9CCsEIMNifAuIgYO7MWlFln9WUEqcK1xzgkMCf4z0Var0M Hmk5Jw4L4dyXO8WbfkszJ7+DaAvWwSIgUt2C0pOwxhmkLQlppeZEi1EDC7A9Rnh1oQdK sR/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=DUROuHk1; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o13-20020a170906774d00b00730a3b5af1fsi5832869ejn.338.2022.08.29.06.06.08; Mon, 29 Aug 2022 06:06:34 -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=@google.com header.s=20210112 header.b=DUROuHk1; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229598AbiH2M5k (ORCPT + 99 others); Mon, 29 Aug 2022 08:57:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54136 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229873AbiH2M4c (ORCPT ); Mon, 29 Aug 2022 08:56:32 -0400 Received: from mail-ej1-x64a.google.com (mail-ej1-x64a.google.com [IPv6:2a00:1450:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 39F56E0A9 for ; Mon, 29 Aug 2022 05:48:27 -0700 (PDT) Received: by mail-ej1-x64a.google.com with SMTP id hr32-20020a1709073fa000b00730a39f36ddso2230051ejc.5 for ; Mon, 29 Aug 2022 05:48:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc; bh=o7nFvD+bAzzd8hTxdy6J5SFI8QAvwQGuiGUQWboy0hk=; b=DUROuHk1Z9qzwl+b2DTEg2S7HJMexhpmqANb/PJZ7O33R9XhSPct7AwehP14PQc/2o wK/bi8a2CwUsCNYLZ60BVrbEBT9p31oxiK+AfnIEOciyJytERkocF14TQxcpALfKW3+m b2Ub+eMgVCVEVNppFz86zBqIyKvR0lUxlYrSs0Pqqu53c88/O9WnN5AbglZY4E2lNY3X UurK/UKzohtpKpt81PvQ4NXYcynS9qosWESRGDtGbt4p3CdpxU/ZwTvmCC71hiCrb5Iz oBnMf/JVbgAxkVDKJuMfwanruY91nDmZy+mUGQ78tN/tN+0RJuWqqSZWWdmcn4+YhWS4 IZ8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc; bh=o7nFvD+bAzzd8hTxdy6J5SFI8QAvwQGuiGUQWboy0hk=; b=cBAn06AKoe0c99jaYp80TY8fCexd7SlbPZ9+gEGmncmcPzVY6JtiiwHqnkWfZRVhPt TgDsjmkqtPxnE6PhtxsIIitQUzlCFPxxAklRHPGIvBP1uyBw9QsVjDDLXkPPip+T3Vb3 SIloZVdbX5fN8ln9UOTldzqHi0SkncYDPwORGDbz26hZ0VTTBOLTsn/xzxAJpdN8zyjc beEwhKUMqkcsRsnMHZKn6pXcUN0V5bGToCsoTvZ4cxseBlYVP2PEB8ywT4AZd5+L9lk3 9R3tDG9v16O3D7qPJimLbLL7OEbidiBdP93xX2jfHnBka5jft1TzCEeXysU6CyEJbJVk ISKQ== X-Gm-Message-State: ACgBeo24Yc8H2WxdZtNAW1hfd9KeoZQSgZqe1bdDaP/i7XX1vq06epu6 gGD/8eX34e6i5ZdRI0TVI+gu+Jf3Ng== X-Received: from elver.muc.corp.google.com ([2a00:79e0:9c:201:196d:4fc7:fa9c:62e3]) (user=elver job=sendgmr) by 2002:a17:907:75c6:b0:741:75a0:b82b with SMTP id jl6-20020a17090775c600b0074175a0b82bmr4672915ejc.465.1661777305817; Mon, 29 Aug 2022 05:48:25 -0700 (PDT) Date: Mon, 29 Aug 2022 14:47:15 +0200 In-Reply-To: <20220829124719.675715-1-elver@google.com> Mime-Version: 1.0 References: <20220829124719.675715-1-elver@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220829124719.675715-11-elver@google.com> Subject: [PATCH v4 10/14] locking/percpu-rwsem: Add percpu_is_write_locked() and percpu_is_read_locked() From: Marco Elver To: elver@google.com, Peter Zijlstra , Frederic Weisbecker , Ingo Molnar Cc: Thomas Gleixner , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Dmitry Vyukov , Michael Ellerman , linuxppc-dev@lists.ozlabs.org, linux-perf-users@vger.kernel.org, x86@kernel.org, linux-sh@vger.kernel.org, kasan-dev@googlegroups.com, linux-kernel@vger.kernel.org, Ian Rogers Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=unavailable 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 Implement simple accessors to probe percpu-rwsem's locked state: percpu_is_write_locked(), percpu_is_read_locked(). Signed-off-by: Marco Elver Reviewed-by: Dmitry Vyukov Acked-by: Ian Rogers --- v4: * Due to spurious read_count increments in __percpu_down_read_trylock() if sem->block != 0, check that !sem->block (reported by Peter). v2: * New patch. --- include/linux/percpu-rwsem.h | 6 ++++++ kernel/locking/percpu-rwsem.c | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/include/linux/percpu-rwsem.h b/include/linux/percpu-rwsem.h index 5fda40f97fe9..36b942b67b7d 100644 --- a/include/linux/percpu-rwsem.h +++ b/include/linux/percpu-rwsem.h @@ -121,9 +121,15 @@ static inline void percpu_up_read(struct percpu_rw_semaphore *sem) preempt_enable(); } +extern bool percpu_is_read_locked(struct percpu_rw_semaphore *); extern void percpu_down_write(struct percpu_rw_semaphore *); extern void percpu_up_write(struct percpu_rw_semaphore *); +static inline bool percpu_is_write_locked(struct percpu_rw_semaphore *sem) +{ + return atomic_read(&sem->block); +} + extern int __percpu_init_rwsem(struct percpu_rw_semaphore *, const char *, struct lock_class_key *); diff --git a/kernel/locking/percpu-rwsem.c b/kernel/locking/percpu-rwsem.c index 5fe4c5495ba3..185bd1c906b0 100644 --- a/kernel/locking/percpu-rwsem.c +++ b/kernel/locking/percpu-rwsem.c @@ -192,6 +192,12 @@ EXPORT_SYMBOL_GPL(__percpu_down_read); __sum; \ }) +bool percpu_is_read_locked(struct percpu_rw_semaphore *sem) +{ + return per_cpu_sum(*sem->read_count) != 0 && !atomic_read(&sem->block); +} +EXPORT_SYMBOL_GPL(percpu_is_read_locked); + /* * Return true if the modular sum of the sem->read_count per-CPU variable is * zero. If this sum is zero, then it is stable due to the fact that if any -- 2.37.2.672.g94769d06f0-goog