Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp3492666pxb; Mon, 16 Nov 2020 16:43:00 -0800 (PST) X-Google-Smtp-Source: ABdhPJwdpexv2OyVuMO5AOW04fUP8nDZaTZgt75cqYczvvy5mAPV1BZSjN9RQDILCZ89AB3AvA+T X-Received: by 2002:a17:906:cc8c:: with SMTP id oq12mr18208868ejb.177.1605573780240; Mon, 16 Nov 2020 16:43:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605573780; cv=none; d=google.com; s=arc-20160816; b=qWZJfTxiiJquhpFLG9pB4bmTFqkGQ67DnlylmeuQWTlfXVIOAT7TDJFkSBH0uUa96K +DZg97uqcbXEPcvSuYREay3ZI4jx7VxZVKi/hmQesHUbVIMJhM9xV3rH/q3r6rQ8hiEf XjzKBXZaDCjX3kLDoMiuqU2P2+RKukgTsbirjGNYUIQTOoWH+odkujMl41EUlrBnojLu VGI5dir95eU6s/xxAOb28C0Nh/dyeMrJQvuUxjqpeHm66JbgVGO+Kn88ih1vR4aK7CKO PEgtKzSFN6wJAAzfwhruuURxUNaN3LWYT1Zz40T0BoBs45SQ/aUIfqdrAJ5dVO2NTvfB iWoQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=7OjiiSYBZE59o/3EQRZgAkIU1e/Datx8loUOgmDRaWc=; b=WzHNInYvViVUOhPZi3BE4cbjJoUFt2Jt1pYmn/orS3sKZjgbnyXjdIP0aPBQKzC8AY rWwb15zqLWZ0PmeHLFmSXyLH68eHFBwbaWQdDFL7N2fVzoO2GATWIz94DWAxW8LkAbNB qU+uFpOqskvjeaErLfoL86PFUURTSp4+YuS3Y14nmUATaU0SP/xWmGp32WfQPCxdxSSe tU8EiD/W9XXvxFFvDVaEfD1GL15b8wU8AwG/jP4oPRPOoJGHUChVOetjS4ljrIbaoHy8 W8ade1jIUeNg2VPP0RoP3Go+q+XsIjKpnhWm6eEMnkNB7bXjTaRXF8Ni0qXSxveFx0FF IILg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=dMzWcpXt; 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=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i24si12247536eja.728.2020.11.16.16.42.36; Mon, 16 Nov 2020 16:43:00 -0800 (PST) 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=@kernel.org header.s=default header.b=dMzWcpXt; 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=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731575AbgKQAk6 (ORCPT + 99 others); Mon, 16 Nov 2020 19:40:58 -0500 Received: from mail.kernel.org ([198.145.29.99]:50142 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730753AbgKQAk4 (ORCPT ); Mon, 16 Nov 2020 19:40:56 -0500 Received: from paulmck-ThinkPad-P72.home (50-39-104-11.bvtn.or.frontiernet.net [50.39.104.11]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 5149224687; Tue, 17 Nov 2020 00:40:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1605573655; bh=kH2jF7WYpdfhM0FDOGaIFr4kh0dpQOsDj5XywSAn/m8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dMzWcpXtqM3d6MTcQBQ8UQDKGyIYuaVyu/qJNoXDY4bmcwkgGmIabi+cdMQlAYt8l 4Bou/nMkbNTWJSL2MF+bX8zdfP8tly0fQa4o+DPa+0Tm5LVw6P+gybGZDjG9vwvLKQ sISVSdKiK+YSHShgW82LZi5ElNDGrKFs9yDDyEHo= From: paulmck@kernel.org To: rcu@vger.kernel.org Cc: linux-kernel@vger.kernel.org, kernel-team@fb.com, mingo@kernel.org, jiangshanlai@gmail.com, akpm@linux-foundation.org, mathieu.desnoyers@efficios.com, josh@joshtriplett.org, tglx@linutronix.de, peterz@infradead.org, rostedt@goodmis.org, dhowells@redhat.com, edumazet@google.com, fweisbec@gmail.com, oleg@redhat.com, joel@joelfernandes.org, "Paul E. McKenney" Subject: [PATCH RFC tip/core/rcu 4/5] srcu: Provide polling interfaces for Tiny SRCU grace periods Date: Mon, 16 Nov 2020 16:40:51 -0800 Message-Id: <20201117004052.14758-4-paulmck@kernel.org> X-Mailer: git-send-email 2.9.5 In-Reply-To: <20201117004017.GA7444@paulmck-ThinkPad-P72> References: <20201117004017.GA7444@paulmck-ThinkPad-P72> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: "Paul E. McKenney" There is a need for a polling interface for SRCU grace periods, so this commit supplies get_state_synchronize_srcu(), start_poll_synchronize_srcu(), and poll_state_synchronize_srcu() for this purpose. The first can be used if future grace periods are inevitable (perhaps due to a later call_srcu() invocation), the second if future grace periods might not otherwise happen, and the third to check if a grace period has elapsed since the corresponding call to either of the first two. As with get_state_synchronize_rcu() and cond_synchronize_rcu(), the return value from either get_state_synchronize_srcu() or start_poll_synchronize_srcu() must be passed in to a later call to poll_state_synchronize_srcu(). Link: https://lore.kernel.org/rcu/20201112201547.GF3365678@moria.home.lan/ Reported-by: Kent Overstreet [ paulmck: Add EXPORT_SYMBOL_GPL() per kernel test robot feedback. ] Signed-off-by: Paul E. McKenney --- include/linux/rcupdate.h | 2 ++ include/linux/srcu.h | 3 +++ include/linux/srcutiny.h | 1 + kernel/rcu/srcutiny.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 56 insertions(+), 2 deletions(-) diff --git a/include/linux/rcupdate.h b/include/linux/rcupdate.h index de08264..e09c0d8 100644 --- a/include/linux/rcupdate.h +++ b/include/linux/rcupdate.h @@ -33,6 +33,8 @@ #define ULONG_CMP_GE(a, b) (ULONG_MAX / 2 >= (a) - (b)) #define ULONG_CMP_LT(a, b) (ULONG_MAX / 2 < (a) - (b)) #define ulong2long(a) (*(long *)(&(a))) +#define USHORT_CMP_GE(a, b) (USHRT_MAX / 2 >= (unsigned short)((a) - (b))) +#define USHORT_CMP_LT(a, b) (USHRT_MAX / 2 < (unsigned short)((a) - (b))) /* Exported common interfaces */ void call_rcu(struct rcu_head *head, rcu_callback_t func); diff --git a/include/linux/srcu.h b/include/linux/srcu.h index e432cc9..a0895bb 100644 --- a/include/linux/srcu.h +++ b/include/linux/srcu.h @@ -60,6 +60,9 @@ void cleanup_srcu_struct(struct srcu_struct *ssp); int __srcu_read_lock(struct srcu_struct *ssp) __acquires(ssp); void __srcu_read_unlock(struct srcu_struct *ssp, int idx) __releases(ssp); void synchronize_srcu(struct srcu_struct *ssp); +unsigned long get_state_synchronize_srcu(struct srcu_struct *ssp); +unsigned long start_poll_synchronize_srcu(struct srcu_struct *ssp); +bool poll_state_synchronize_srcu(struct srcu_struct *ssp, unsigned long cookie); #ifdef CONFIG_DEBUG_LOCK_ALLOC diff --git a/include/linux/srcutiny.h b/include/linux/srcutiny.h index fed4a2d..e9bd6fb 100644 --- a/include/linux/srcutiny.h +++ b/include/linux/srcutiny.h @@ -16,6 +16,7 @@ struct srcu_struct { short srcu_lock_nesting[2]; /* srcu_read_lock() nesting depth. */ unsigned short srcu_idx; /* Current reader array element in bit 0x2. */ + unsigned short srcu_idx_max; /* Furthest future srcu_idx request. */ u8 srcu_gp_running; /* GP workqueue running? */ u8 srcu_gp_waiting; /* GP waiting for readers? */ struct swait_queue_head srcu_wq; diff --git a/kernel/rcu/srcutiny.c b/kernel/rcu/srcutiny.c index 3bac1db..b405811 100644 --- a/kernel/rcu/srcutiny.c +++ b/kernel/rcu/srcutiny.c @@ -34,6 +34,7 @@ static int init_srcu_struct_fields(struct srcu_struct *ssp) ssp->srcu_gp_running = false; ssp->srcu_gp_waiting = false; ssp->srcu_idx = 0; + ssp->srcu_idx_max = 0; INIT_WORK(&ssp->srcu_work, srcu_drive_gp); INIT_LIST_HEAD(&ssp->srcu_work.entry); return 0; @@ -114,7 +115,7 @@ void srcu_drive_gp(struct work_struct *wp) struct srcu_struct *ssp; ssp = container_of(wp, struct srcu_struct, srcu_work); - if (ssp->srcu_gp_running || !READ_ONCE(ssp->srcu_cb_head)) + if (ssp->srcu_gp_running || USHORT_CMP_GE(ssp->srcu_idx, READ_ONCE(ssp->srcu_idx_max))) return; /* Already running or nothing to do. */ /* Remove recently arrived callbacks and wait for readers. */ @@ -147,14 +148,19 @@ void srcu_drive_gp(struct work_struct *wp) * straighten that out. */ WRITE_ONCE(ssp->srcu_gp_running, false); - if (READ_ONCE(ssp->srcu_cb_head)) + if (USHORT_CMP_GE(ssp->srcu_idx, READ_ONCE(ssp->srcu_idx_max))) schedule_work(&ssp->srcu_work); } EXPORT_SYMBOL_GPL(srcu_drive_gp); static void srcu_gp_start_if_needed(struct srcu_struct *ssp) { + unsigned short cookie; + if (!READ_ONCE(ssp->srcu_gp_running)) { + cookie = get_state_synchronize_srcu(ssp); + if (USHORT_CMP_LT(READ_ONCE(ssp->srcu_idx_max), cookie)) + WRITE_ONCE(ssp->srcu_idx_max, cookie); if (likely(srcu_init_done)) schedule_work(&ssp->srcu_work); else if (list_empty(&ssp->srcu_work.entry)) @@ -196,6 +202,48 @@ void synchronize_srcu(struct srcu_struct *ssp) } EXPORT_SYMBOL_GPL(synchronize_srcu); +/* + * get_state_synchronize_srcu - Provide an end-of-grace-period cookie + */ +unsigned long get_state_synchronize_srcu(struct srcu_struct *ssp) +{ + unsigned long ret; + + barrier(); + ret = (READ_ONCE(ssp->srcu_idx) + 3) & ~0x1; + barrier(); + return ret & USHRT_MAX; +} +EXPORT_SYMBOL_GPL(get_state_synchronize_srcu); + +/* + * start_poll_synchronize_srcu - Provide cookie and start grace period + * + * The difference between this and get_state_synchronize_srcu() is that + * this function ensures that the poll_state_synchronize_srcu() will + * eventually return the value true. + */ +unsigned long start_poll_synchronize_srcu(struct srcu_struct *ssp) +{ + unsigned long ret = get_state_synchronize_srcu(ssp); + + srcu_gp_start_if_needed(ssp); + return ret; +} +EXPORT_SYMBOL_GPL(start_poll_synchronize_srcu); + +/* + * poll_state_synchronize_srcu - Has cookie's grace period ended? + */ +bool poll_state_synchronize_srcu(struct srcu_struct *ssp, unsigned long cookie) +{ + bool ret = USHORT_CMP_GE(READ_ONCE(ssp->srcu_idx), cookie); + + barrier(); + return ret; +} +EXPORT_SYMBOL_GPL(poll_state_synchronize_srcu); + /* Lockdep diagnostics. */ void __init rcu_scheduler_starting(void) { -- 2.9.5