From patchwork Tue Sep 3 17:46:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13789158 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 6F043CD37AA for ; Tue, 3 Sep 2024 17:46:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 047D88D01BD; Tue, 3 Sep 2024 13:46:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F3A9F8D018A; Tue, 3 Sep 2024 13:46:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E01B68D01BD; Tue, 3 Sep 2024 13:46:31 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id C08928D018A for ; Tue, 3 Sep 2024 13:46:31 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 057251207D2 for ; Tue, 3 Sep 2024 17:46:31 +0000 (UTC) X-FDA: 82524156582.08.60C7306 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf21.hostedemail.com (Postfix) with ESMTP id 4BBD91C0004 for ; Tue, 3 Sep 2024 17:46:29 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=ePac60MH; spf=pass (imf21.hostedemail.com: domain of andrii@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=andrii@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1725385494; 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=Uk348Kjht7XGQIw8421veAwyJRnWtKPa6jJWqAdo+Tc=; b=oI4O9WG1czioI9J64fnvPd4hoTyccbHb9aJ6ObZT2gG4b/j14gx33bLTvlicRhtGEUbY9s 1XR2mqjLps//kletBWr439T4NxYokNPzCwW/nCunQGdqiXDPyzrow3w+m8/gvW4VAXTe6Q SzRCnVxQTMVq7UaovQnmTosYWhtftWE= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1725385494; a=rsa-sha256; cv=none; b=LDxo4RIzUtmqqq/ql6B3kkoiujhvbnnm35i2pQibyvaOcMt9RbQdaKZ9+9/Jr8Tx7zsEn7 sbkU6GQqrVJdKDdS5fsc0vcaBzT6fUTQQ6D/YrF4SQgP9Z9nooxJRclGfC4Z4Q4tidrAHf sNO692vitiPp6GkOvGKCwlvgi1FEKjY= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=ePac60MH; spf=pass (imf21.hostedemail.com: domain of andrii@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=andrii@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id A1FC35C5A44; Tue, 3 Sep 2024 17:46:24 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6E8DAC4CEC6; Tue, 3 Sep 2024 17:46:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1725385587; bh=fbkVIaD1WSV5k2O1l+qbLGEuln2w8HvLuX0+cvs5jtI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ePac60MHZQZol0UGw03YynNTwklAOaXlOvgQPfdFV66MYzxkSzn+Zmk4d1UTS198j zU8y2voSoi8FHy8gTAz9OTJ/tflW4leFmuRPC95QNYsdovmI8L/kSz/oHSbwosTt4l 90yxAjvX0bAlHHDCz/NCCWumyGr9+oyKrPSrqZY5wXnN3jRekx9+V7cdxUl5sXMLkN qChy2ahmkKbxyNBiybflTFGhG9xQqB2nQW0rsB/wTcKY0458tc/hGBHh9VnRe/+5Oq UuYFJ19TN2rpx1y9fim9ueGgCZ24e5P73q8VPvfJvBuUviAuEuoLvMxFS4Z8cCJcb7 /vMnWH9OEYinw== 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 v5 6/8] rbtree: provide rb_find_rcu() / rb_find_add_rcu() Date: Tue, 3 Sep 2024 10:46:01 -0700 Message-ID: <20240903174603.3554182-7-andrii@kernel.org> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20240903174603.3554182-1-andrii@kernel.org> References: <20240903174603.3554182-1-andrii@kernel.org> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 4BBD91C0004 X-Stat-Signature: qkbaaxgb8zb8ykx7zr95u951xxido4u5 X-HE-Tag: 1725385589-543928 X-HE-Meta: U2FsdGVkX18dlPe+12eXZ2v/EtPuQmfC+H+F5E3BoMPrIgjuPzeyJd1LvF0GQNQ2xibgmrpghpbdsRFkLINakQ7HNg8s5stWu86Fzs1tXDbAgt+py/4pyZlDSPCSb2vRpPJTKzRCC76ubF8e3QL0va2CYhp2ylt1xkhLSpNZpGvH8c9ShALslGYysIi6fR/J72xnx4ZpI6tyhKzXwhosVccMXUTqkOc2lh84f2aNVxdNKZ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ft8SKtzhDUb5GTzGYfdME1sF1BGQtBY= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, 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. Reviewed-by: Oleg Nesterov 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