From patchwork Thu Aug 29 18:37:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 13783567 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 95F03C87FD2 for ; Thu, 29 Aug 2024 18:38:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C26936B008C; Thu, 29 Aug 2024 14:38:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BAD886B0098; Thu, 29 Aug 2024 14:38:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A018C6B00B7; Thu, 29 Aug 2024 14:38:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 7C7796B008C for ; Thu, 29 Aug 2024 14:38:09 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 2FDBEA0816 for ; Thu, 29 Aug 2024 18:38:09 +0000 (UTC) X-FDA: 82506142698.11.D56EFF7 Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf22.hostedemail.com (Postfix) with ESMTP id 8945EC0006 for ; Thu, 29 Aug 2024 18:38:07 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=JzC1CmdK; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf22.hostedemail.com: domain of andrii@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=andrii@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724956621; a=rsa-sha256; cv=none; b=TL1+Tgg76Y5AEi0AuAd6//EPs9u6scnRUNCJJ0s8qwkIcbNttP2+dSKr60yIM+/1WqiPKy aB/X9zQ7OvTmzc/xMkujSSsyGk8I5Wb9jtc7OhzW/hvh1tHMA5oKPL/Kzx11Fj+1INDYa4 14PQeuAXiQQdZlEMDPh+tbJ2QD6j30s= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=JzC1CmdK; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf22.hostedemail.com: domain of andrii@kernel.org designates 147.75.193.91 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=1724956621; 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=09XjGE5FVJQxTGHNOsClqyUdib/RLxkQkXa9cYXocRGkS1joxkuNtNg+e8wiD0UYyiPlT4 N5osv0HNiV3UxJDe9eE/+TPwMvsRupqIiR04ZpBR8cD2/257UANVTZEMsxMollf7tPj23I +hu2FT4wBl+U/GdH9UTs6JH1r4RD8n0= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id A441DA42811; Thu, 29 Aug 2024 18:37:59 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0DB41C4CEC8; Thu, 29 Aug 2024 18:38:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1724956686; bh=iIO+4imlGFWJODra3HvN51TRltaxN3vJ0lLIJ7O8eDQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JzC1CmdKA1dtEn7aGy1jjW/ec/JkAE54yAYkcV7++RQ8t92nsCNYrmWjTv5EAce7s yziefBlq6hxVldBtXB+7OKy7SGFy0H/BKTt7LyNloWdF/FxvOQe4YPAHuciZta8xmo hNN8v5AtrC45em3nFrg143zZISbYTM7V8ypcuYxl3xFF0RNlNuVWhFSWS8mx1JLRYL 8ttljUgfpwxp5E9rn/hUY3uSv4eOT2RNYFphSR4IaMcMZgHhEd07n605BPDs9uQ75D Dc9aJe8SJJesglvAjesr+/i3sZBzpAmaZyvulNcBOWBpbBxAJhc7tGiW1DpBfj2ppr ODT29am/jlW5w== 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 v4 6/8] rbtree: provide rb_find_rcu() / rb_find_add_rcu() Date: Thu, 29 Aug 2024 11:37:39 -0700 Message-ID: <20240829183741.3331213-7-andrii@kernel.org> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20240829183741.3331213-1-andrii@kernel.org> References: <20240829183741.3331213-1-andrii@kernel.org> MIME-Version: 1.0 X-Rspamd-Queue-Id: 8945EC0006 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: m1j6wczqa6dmsj33xt1jhgr4zcyj5i1m X-HE-Tag: 1724956687-65828 X-HE-Meta: U2FsdGVkX1+phtYLaGjhorEa0ksUojqfW3wwq7MJCCRC8ftnhZTCInCE1J7V3xt4RkZo/jOPrjtnGdnRjbj/7O0rxWYpURg1wSXw51zvNmbDFpEUSPlzwHkc0U6y1aEXWMAWL5U11EV9okUqnmEa+tJBqul9Y4hIU4SegTZ+WsiVvMF0gnp4N5PBAfe4Dc7LME6HeZIQVhBYEXACqk0laOMM99di9XurUZhDRQJIM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vaqpWB2fvzy50bbkT6bwl8Ncg== 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