From mboxrd@z Thu Jan 1 00:00:00 1970 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 1C68BC7619A for ; Wed, 12 Apr 2023 08:13:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B1472280005; Wed, 12 Apr 2023 04:13:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AC5B1280003; Wed, 12 Apr 2023 04:13:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 98E72280005; Wed, 12 Apr 2023 04:13:35 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 8A591280003 for ; Wed, 12 Apr 2023 04:13:35 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 6B120120A99 for ; Wed, 12 Apr 2023 08:13:35 +0000 (UTC) X-FDA: 80672024790.10.9A21A85 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf20.hostedemail.com (Postfix) with ESMTP id A20DE1C001A for ; Wed, 12 Apr 2023 08:13:33 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=linuxfoundation.org header.s=korg header.b=MIeyG2VO; spf=pass (imf20.hostedemail.com: domain of gregkh@linuxfoundation.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=gregkh@linuxfoundation.org; dmarc=pass (policy=none) header.from=linuxfoundation.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1681287213; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:dkim-signature; bh=uODA4+pPvhKVJsH4Lm6fkwPWm7za4ZxCvBsDcBKSL0M=; b=kVfxoLKEUi7X2hLCyw1IdWNs+TPjgnO8zZ8cBDlKFImdCb2WWHgKECso51kQkik++NdNij lsvvu8fh6SwzIsEwAw/0MJ2ZdwDQ8YCiKtLGiXnkZcvLzusgrC3ss3AKLn9p5VeTZGuhA2 bg24xpU67fHHaCI7ctotuXcpMleBllk= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=linuxfoundation.org header.s=korg header.b=MIeyG2VO; spf=pass (imf20.hostedemail.com: domain of gregkh@linuxfoundation.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=gregkh@linuxfoundation.org; dmarc=pass (policy=none) header.from=linuxfoundation.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1681287213; a=rsa-sha256; cv=none; b=mNNA3erogpkxu1MSmAY73u4VdqcyiuxffTUsEmqg4dJZWdxXNqCkna6y02n2uYKmHFz0Tg 0KpXo2vkQjCkcrCCIjE5I9qi3dPxvu7lY5iKq8/1za6WqrvWGlNRBQoFfMkFOLh5ZxSRUQ daUjfp5UhPm08Hau6sUgLtDtTtp/G20= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id CF29A62F53; Wed, 12 Apr 2023 08:13:32 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id E3EB8C433D2; Wed, 12 Apr 2023 08:13:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1681287212; bh=+ZzZhbiaE8bl42Q0sXioqftJ30IOkGYNQGhi1ZYKmx8=; h=Subject:To:Cc:From:Date:In-Reply-To:From; b=MIeyG2VOA7W+s7zeLoQzzGJ5d5AWRT8rmdxpuqS/LA+Jb72uz1Y/gS6/yHE9HY+yA qO6gHkNJjQSxlJ7qLLM1q8BJ041vZjc7fVXR/Nmpmq7EHkO8b92cJNnFDjdY+RR+Xu TiJdGqodYf+DURchCnAZjb0LPAz4kCK59i5oCM3s= Subject: Patch "maple_tree: fix freeing of nodes in rcu mode" has been added to the 6.1-stable tree To: Liam.Howlett@Oracle.com,Liam.Howlett@oracle.com,gregkh@linuxfoundation.org,linux-mm@kvack.org,maple-tree@lists.infradead.org Cc: From: Date: Wed, 12 Apr 2023 10:13:21 +0200 In-Reply-To: <20230411151055.2910579-11-Liam.Howlett@oracle.com> Message-ID: <2023041221-spendable-barbed-69a4@gregkh> MIME-Version: 1.0 Content-Type: text/plain; charset=ANSI_X3.4-1968 Content-Transfer-Encoding: 8bit X-stable: commit X-Patchwork-Hint: ignore X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: u7p8u9g9xqnjg698b4qbrynhb776fmy3 X-Rspamd-Queue-Id: A20DE1C001A X-HE-Tag: 1681287213-241417 X-HE-Meta: U2FsdGVkX1//v/IgVBfJb821qU0PRPF2qGcIRTsn7MTVwuc/9iDI4Trwe+PNwZ5LhitPYsWbSFEUbcdwTzKwc69ANlCK9XowfWadCfvoy3YnH7RYVM59JcuTv4K7UqZCGPjnFiOExwZZ2ZEdbJLxkNoWwkjcb1qXrz5feut7uVqRSA8zFRdOuUC9MvypLDTmKjkxiC6MpQpLlOV7fVfToXweQWGFxREV95FIYqU0icxn2q9NpUKxUNxmggIVgKimbMAxfa+iW7tgCq2U0aYcSrIVbq9O7A24Cnfdr/2hXnFiYiMkLERLmbrnTb1IgZE7R8BCQvkKRVlKcFNElBUCf6ws+mIg862sU9zv9xwslHmVizkWKShmQIYXLb5tuQG7OT/C8oz0NxxKpG2Xtk645d4CHK2D8mbaHu0btJJeOW4c2/+b5SwwVphUvmz+tkSsZWsDmk0TTj4aEE02Z8hO/VoDHxivVW5RLa/UmX/IL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PNAsH9IIq8p8zaSiaIKJXcUAGGV/16pArTZJEpqRxGkJoEJv/KUpZpaw5W+1i1xuGNJY1Me2d2iO68rMcZmR4A== 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: This is a note to let you know that I've just added the patch titled maple_tree: fix freeing of nodes in rcu mode to the 6.1-stable tree which can be found at: http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary The filename of the patch is: maple_tree-fix-freeing-of-nodes-in-rcu-mode.patch and it can be found in the queue-6.1 subdirectory. If you, or anyone else, feels it should not be added to the stable tree, please let know about it. >From stable-owner@vger.kernel.org Tue Apr 11 17:13:39 2023 From: "Liam R. Howlett" Date: Tue, 11 Apr 2023 11:10:51 -0400 Subject: maple_tree: fix freeing of nodes in rcu mode To: Greg Kroah-Hartman , stable@vger.kernel.org Cc: maple-tree@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, "Liam R. Howlett" , Stable@vger.kernel.org, Liam Howlett Message-ID: <20230411151055.2910579-11-Liam.Howlett@oracle.com> From: "Liam R. Howlett" commit 2e5b4921f8efc9e845f4f04741797d16f36847eb upstream. The walk to destroy the nodes was not always setting the node type and would result in a destroy method potentially using the values as nodes. Avoid this by setting the correct node types. This is necessary for the RCU mode of the maple tree. Link: https://lkml.kernel.org/r/20230227173632.3292573-4-surenb@google.com Cc: Fixes: 54a611b60590 ("Maple Tree: add new data structure") Signed-off-by: Liam Howlett Signed-off-by: Greg Kroah-Hartman --- lib/maple_tree.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 62 insertions(+), 11 deletions(-) --- a/lib/maple_tree.c +++ b/lib/maple_tree.c @@ -893,6 +893,44 @@ static inline void ma_set_meta(struct ma } /* + * mas_clear_meta() - clear the metadata information of a node, if it exists + * @mas: The maple state + * @mn: The maple node + * @mt: The maple node type + * @offset: The offset of the highest sub-gap in this node. + * @end: The end of the data in this node. + */ +static inline void mas_clear_meta(struct ma_state *mas, struct maple_node *mn, + enum maple_type mt) +{ + struct maple_metadata *meta; + unsigned long *pivots; + void __rcu **slots; + void *next; + + switch (mt) { + case maple_range_64: + pivots = mn->mr64.pivot; + if (unlikely(pivots[MAPLE_RANGE64_SLOTS - 2])) { + slots = mn->mr64.slot; + next = mas_slot_locked(mas, slots, + MAPLE_RANGE64_SLOTS - 1); + if (unlikely((mte_to_node(next) && mte_node_type(next)))) + return; /* The last slot is a node, no metadata */ + } + fallthrough; + case maple_arange_64: + meta = ma_meta(mn, mt); + break; + default: + return; + } + + meta->gap = 0; + meta->end = 0; +} + +/* * ma_meta_end() - Get the data end of a node from the metadata * @mn: The maple node * @mt: The maple node type @@ -5433,20 +5471,22 @@ no_gap: * mas_dead_leaves() - Mark all leaves of a node as dead. * @mas: The maple state * @slots: Pointer to the slot array + * @type: The maple node type * * Must hold the write lock. * * Return: The number of leaves marked as dead. */ static inline -unsigned char mas_dead_leaves(struct ma_state *mas, void __rcu **slots) +unsigned char mas_dead_leaves(struct ma_state *mas, void __rcu **slots, + enum maple_type mt) { struct maple_node *node; enum maple_type type; void *entry; int offset; - for (offset = 0; offset < mt_slot_count(mas->node); offset++) { + for (offset = 0; offset < mt_slots[mt]; offset++) { entry = mas_slot_locked(mas, slots, offset); type = mte_node_type(entry); node = mte_to_node(entry); @@ -5465,14 +5505,13 @@ unsigned char mas_dead_leaves(struct ma_ static void __rcu **mas_dead_walk(struct ma_state *mas, unsigned char offset) { - struct maple_node *node, *next; + struct maple_node *next; void __rcu **slots = NULL; next = mas_mn(mas); do { - mas->node = ma_enode_ptr(next); - node = mas_mn(mas); - slots = ma_slots(node, node->type); + mas->node = mt_mk_node(next, next->type); + slots = ma_slots(next, next->type); next = mas_slot_locked(mas, slots, offset); offset = 0; } while (!ma_is_leaf(next->type)); @@ -5536,11 +5575,14 @@ static inline void __rcu **mas_destroy_d node = mas_mn(mas); slots = ma_slots(node, mte_node_type(mas->node)); next = mas_slot_locked(mas, slots, 0); - if ((mte_dead_node(next))) + if ((mte_dead_node(next))) { + mte_to_node(next)->type = mte_node_type(next); next = mas_slot_locked(mas, slots, 1); + } mte_set_node_dead(mas->node); node->type = mte_node_type(mas->node); + mas_clear_meta(mas, node, node->type); node->piv_parent = prev; node->parent_slot = offset; offset = 0; @@ -5560,13 +5602,18 @@ static void mt_destroy_walk(struct maple MA_STATE(mas, &mt, 0, 0); - if (mte_is_leaf(enode)) + mas.node = enode; + if (mte_is_leaf(enode)) { + node->type = mte_node_type(enode); goto free_leaf; + } + ma_flags &= ~MT_FLAGS_LOCK_MASK; mt_init_flags(&mt, ma_flags); mas_lock(&mas); - mas.node = start = enode; + mte_to_node(enode)->ma_flags = ma_flags; + start = enode; slots = mas_destroy_descend(&mas, start, 0); node = mas_mn(&mas); do { @@ -5574,7 +5621,8 @@ static void mt_destroy_walk(struct maple unsigned char offset; struct maple_enode *parent, *tmp; - node->slot_len = mas_dead_leaves(&mas, slots); + node->type = mte_node_type(mas.node); + node->slot_len = mas_dead_leaves(&mas, slots, node->type); if (free) mt_free_bulk(node->slot_len, slots); offset = node->parent_slot + 1; @@ -5598,7 +5646,8 @@ next: } while (start != mas.node); node = mas_mn(&mas); - node->slot_len = mas_dead_leaves(&mas, slots); + node->type = mte_node_type(mas.node); + node->slot_len = mas_dead_leaves(&mas, slots, node->type); if (free) mt_free_bulk(node->slot_len, slots); @@ -5608,6 +5657,8 @@ start_slots_free: free_leaf: if (free) mt_free_rcu(&node->rcu); + else + mas_clear_meta(&mas, node, node->type); } /* Patches currently in stable-queue which might be from stable-owner@vger.kernel.org are queue-6.1/maple_tree-fix-potential-rcu-issue.patch queue-6.1/maple_tree-add-smp_rmb-to-dead-node-detection.patch queue-6.1/maple_tree-add-rcu-lock-checking-to-rcu-callback-functions.patch queue-6.1/maple_tree-fix-handle-of-invalidated-state-in-mas_wr_store_setup.patch queue-6.1/maple_tree-reduce-user-error-potential.patch queue-6.1/maple_tree-fix-mas_prev-and-mas_find-state-handling.patch queue-6.1/maple_tree-remove-gfp_zero-from-kmem_cache_alloc-and-kmem_cache_alloc_bulk.patch queue-6.1/maple_tree-be-more-cautious-about-dead-nodes.patch queue-6.1/mm-enable-maple-tree-rcu-mode-by-default.patch queue-6.1/maple_tree-detect-dead-nodes-in-mas_start.patch queue-6.1/maple_tree-fix-freeing-of-nodes-in-rcu-mode.patch queue-6.1/maple_tree-remove-extra-smp_wmb-from-mas_dead_leaves.patch queue-6.1/maple_tree-refine-ma_state-init-from-mas_start.patch