From patchwork Tue Aug 13 04:29:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13761249 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 0EE15C52D7C for ; Tue, 13 Aug 2024 04:30:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 89D2E6B00A4; Tue, 13 Aug 2024 00:30:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 789386B00A5; Tue, 13 Aug 2024 00:30:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 58D2D6B00A6; Tue, 13 Aug 2024 00:30:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 34CB36B00A4 for ; Tue, 13 Aug 2024 00:30:09 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id F12281A05E4 for ; Tue, 13 Aug 2024 04:30:08 +0000 (UTC) X-FDA: 82445944896.03.BE35610 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf02.hostedemail.com (Postfix) with ESMTP id 20C9980027 for ; Tue, 13 Aug 2024 04:30:05 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=nyi4ZzFt; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf02.hostedemail.com: domain of andrii@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=andrii@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723523355; a=rsa-sha256; cv=none; b=L3Q668tDM9MXrCxDlSdnBOedgNm0+I1z78zaVR2+NKCpql3d2Gy7kkke+mmOb1vC4bW6PY YXTLkzlgug8SDVxBPo6Wyd7GPwa/s0K3pENHJ96rsrkiBxB/1zJMGLb4e/zX3b04aX4JqW husThFGbkLcWS86Y+4pImtOUw6LyUD4= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=nyi4ZzFt; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf02.hostedemail.com: domain of andrii@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=andrii@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723523355; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=hCGVwTt9bzFS2vsQe5QWlZJE2SQYwI4THELaVkQI2Lw=; b=fY0NbP1Mos8UTg9g7IYZWxv2/jckDg7/2Sn934Ee0mxzE1D4KRxL7lP+PXdhLFLna5OFW0 Gxg+fQ0RWOQlH46wJzCYRLQThDb0RcpTdWsX+RxoFQDYNWvH9N42cscA2t6XQ/90j6kNRJ hyPYyDYf2/Pk8EAmzTIt1UhANic9buM= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id D7A72CE1179; Tue, 13 Aug 2024 04:30:00 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E9C2EC4AF0E; Tue, 13 Aug 2024 04:29:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1723523400; bh=iIO+4imlGFWJODra3HvN51TRltaxN3vJ0lLIJ7O8eDQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nyi4ZzFtnEAKbgF77exAYbwH1JsjVlsy90cUdbmKcEkFiUDkFs7z+WNMHwv52zCQs RYZnm0SPIyUnbXEBgAAfgiLeilZyo+U9A/ZkLVTHGCuWzvl/JyN5GDGL9wrZBTAszT JxinIH2E9S72z3nYli0ti618m+TKCvsomeB+oeRn8jpQ3Hz3AlsvNoh+xwkhjqMeEd g9PDl+ERgtJaZSDu7PY34GWvX0XBXEts9uwIBdCcwZIP+FQ69ct0JoOsxsg7rJfyIs HCCyxlizGciad8le7WUAhSzKGHiUduThHlpeVKwJtvyMDQV4THSw0uUYbUpYbmTb4w HW43b2PqOU8hg== From: Andrii Nakryiko To: linux-trace-kernel@vger.kernel.org, peterz@infradead.org, oleg@redhat.com Cc: rostedt@goodmis.org, mhiramat@kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, jolsa@kernel.org, paulmck@kernel.org, willy@infradead.org, surenb@google.com, akpm@linux-foundation.org, linux-mm@kvack.org, Andrii Nakryiko Subject: [PATCH v3 06/13] rbtree: provide rb_find_rcu() / rb_find_add_rcu() Date: Mon, 12 Aug 2024 21:29:10 -0700 Message-ID: <20240813042917.506057-7-andrii@kernel.org> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20240813042917.506057-1-andrii@kernel.org> References: <20240813042917.506057-1-andrii@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 20C9980027 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: rorb5s4t5cqzdit4qz633dnb9mfsdduc X-HE-Tag: 1723523405-977787 X-HE-Meta: U2FsdGVkX1/4wVUhRvuR0DRaURrInB6J0Q7S6eCUis6/Xon7HJMoL858YCuSjbVniavTmKbPlghtYNXdHXEkBEZSyMjkipluCsd+0ezVx5vjQLja7V529fCEBz0VWFAaiSNOhOj8NPfTCqYvVwjgr/k6mp/5OBQov21Ly/So4leRp1IHeb/vBYemUD2PGpwFmJl1+ZWXKEaNtvuYAnAjfg+eOVuvBdwAI0zHaFCCnDfKUj76e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No/QWg9lbw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000001, 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: Peter Zijlstra Much like latch_tree, add two RCU methods for the regular RB-tree, which can be used in conjunction with a seqcount to provide lockless lookups. Signed-off-by: Peter Zijlstra (Intel) Reviewed-by: Masami Hiramatsu (Google) --- include/linux/rbtree.h | 67 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/include/linux/rbtree.h b/include/linux/rbtree.h index f7edca369eda..7c173aa64e1e 100644 --- a/include/linux/rbtree.h +++ b/include/linux/rbtree.h @@ -244,6 +244,42 @@ rb_find_add(struct rb_node *node, struct rb_root *tree, return NULL; } +/** + * rb_find_add_rcu() - find equivalent @node in @tree, or add @node + * @node: node to look-for / insert + * @tree: tree to search / modify + * @cmp: operator defining the node order + * + * Adds a Store-Release for link_node. + * + * Returns the rb_node matching @node, or NULL when no match is found and @node + * is inserted. + */ +static __always_inline struct rb_node * +rb_find_add_rcu(struct rb_node *node, struct rb_root *tree, + int (*cmp)(struct rb_node *, const struct rb_node *)) +{ + struct rb_node **link = &tree->rb_node; + struct rb_node *parent = NULL; + int c; + + while (*link) { + parent = *link; + c = cmp(node, parent); + + if (c < 0) + link = &parent->rb_left; + else if (c > 0) + link = &parent->rb_right; + else + return parent; + } + + rb_link_node_rcu(node, parent, link); + rb_insert_color(node, tree); + return NULL; +} + /** * rb_find() - find @key in tree @tree * @key: key to match @@ -272,6 +308,37 @@ rb_find(const void *key, const struct rb_root *tree, return NULL; } +/** + * rb_find_rcu() - find @key in tree @tree + * @key: key to match + * @tree: tree to search + * @cmp: operator defining the node order + * + * Notably, tree descent vs concurrent tree rotations is unsound and can result + * in false-negatives. + * + * Returns the rb_node matching @key or NULL. + */ +static __always_inline struct rb_node * +rb_find_rcu(const void *key, const struct rb_root *tree, + int (*cmp)(const void *key, const struct rb_node *)) +{ + struct rb_node *node = tree->rb_node; + + while (node) { + int c = cmp(key, node); + + if (c < 0) + node = rcu_dereference_raw(node->rb_left); + else if (c > 0) + node = rcu_dereference_raw(node->rb_right); + else + return node; + } + + return NULL; +} + /** * rb_find_first() - find the first @key in @tree * @key: key to match