Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp5652328rwl; Tue, 11 Apr 2023 08:14:21 -0700 (PDT) X-Google-Smtp-Source: AKy350bNfdVcLv2u4NqxPoEpDA6DOTwMXNGHaXefCpGew7YPttAq95csWz7xCxFAXvSykuoDBg3t X-Received: by 2002:aa7:cd16:0:b0:504:8bdb:1d30 with SMTP id b22-20020aa7cd16000000b005048bdb1d30mr8618935edw.23.1681226061156; Tue, 11 Apr 2023 08:14:21 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1681226061; cv=pass; d=google.com; s=arc-20160816; b=tbCKsFpDeld+ISnIZC4+tMs6pbdG/GMZJP6Fyjn9+f+XSSxDtDSJFDrvP2NUbpdgUH adUZ7FBsS3z0jnZRSivodc36CI0RsorppEICjCaPVRXpgQuloToKUQ52OzGMSJKIrvjv gNx1KW1LXTSY07212z8i8H5W4aVMqhuMzNdjakxkYYSQSHIhIdP+UsxkiG9dGzOCyN1P aTjX5ghjDRwEoXDpCTeHUPGsbEV//QDnCaE4J4ci1PQtjnCc0nUyVm1qmN7btOCBvZNm W1AnV0BmVyXC4dpmHgRJMCDl+PBDRFbM7auASN0rNjqFDlz4Tvf35XTNmDbbR7kNGJhQ 5fag== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:content-transfer-encoding :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:dkim-signature; bh=xGnXTW5VC6tcdOlTLMFIueElZmHVbODoCfPsVjIEEzE=; b=zgP+5rY66oc7imBWlSY70K9IDsHJPXgdwJocRIcLOJKUK+1Jf5XAoE5Joiw2MkkrFE cJpCLwRSJF1g8WWqSx1zd48JKfjl612Q2NZZfwOtib0rVJ5+zXrXjBBoR+NOjvNi+Nfg 7a84lYKlEV/o6905+0v2A+w9I6GwCje2bIAhVPvAj8fku8Nf1AXXhk2zLsHdKCRO1UnT 0IesL1irHnIRPdGeVfXq0Ovzvn8FwQE/y9eRmaQpP+LG1jnFsRNLFJZeNm6TGASbs9kf LvIMXrdW9Hk6+cP9OEFzeuq1Tjf6SZj0YkF+ic7vXoSkU3wI4y9+G19JBjGCsmV8CYRL 9JVQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2023-03-30 header.b="j/RlKkrH"; dkim=pass header.i=@oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=TlhpPo6X; arc=pass (i=1 spf=pass spfdomain=oracle.com dkim=pass dkdomain=oracle.com dmarc=pass fromdomain=oracle.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i15-20020aa7c70f000000b005021cd581a0si11541913edq.402.2023.04.11.08.13.55; Tue, 11 Apr 2023 08:14:21 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2023-03-30 header.b="j/RlKkrH"; dkim=pass header.i=@oracle.onmicrosoft.com header.s=selector2-oracle-onmicrosoft-com header.b=TlhpPo6X; arc=pass (i=1 spf=pass spfdomain=oracle.com dkim=pass dkdomain=oracle.com dmarc=pass fromdomain=oracle.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231176AbjDKPN0 (ORCPT + 99 others); Tue, 11 Apr 2023 11:13:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44490 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230522AbjDKPMb (ORCPT ); Tue, 11 Apr 2023 11:12:31 -0400 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ED02E3ABF; Tue, 11 Apr 2023 08:12:08 -0700 (PDT) Received: from pps.filterd (m0246632.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 33BEx9qC019840; Tue, 11 Apr 2023 15:11:48 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2023-03-30; bh=xGnXTW5VC6tcdOlTLMFIueElZmHVbODoCfPsVjIEEzE=; b=j/RlKkrHH7sNp+/3UQ44q5OZO+DdoxeIV54nII3jH97c7IS/ELtt/PRymm65aVmng4rE Y7uohZNpca3Lh4ZmDWJvncLO0V6wwefKIsXWZUQyruTLQyjzCAAPuYLzQzQm9NOn2eBi 7bGZ5NUbJ2JdNAWr5GKKrqN+IaJAWJIF+FeUc1/hAzuqwm/gtV2qWkomDHSU3GC8BNOR DmZ8vM327ovEy43dG2B+9GP+WFxBlCXdozx789ec5TNhc0J2l8Iw3Zl+X8DHJfczJL9k X6LgcqrsaDA/peg91p2vKzuKKh6QihadEe5XHthz/Br6cd7pEXYEwTibiM7ymf4Io9IR hw== Received: from phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta02.appoci.oracle.com [147.154.114.232]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3pu0b2wpqr-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 11 Apr 2023 15:11:48 +0000 Received: from pps.filterd (phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (8.17.1.19/8.17.1.19) with ESMTP id 33BEUkXk009948; Tue, 11 Apr 2023 15:11:47 GMT Received: from nam12-mw2-obe.outbound.protection.outlook.com (mail-mw2nam12lp2048.outbound.protection.outlook.com [104.47.66.48]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 3puw870rxu-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 11 Apr 2023 15:11:47 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=fe1slJqt53lwF0RhWr73NtF1t+eV8EAZ/kVZuP162RiXpO4UmMDaIYalGk5ArFpOCxJcYUvunOs+53zTVFQRe51S4XxWuB1/BVB7yadFxfhbGeZH7sqPNnlvDo5bJV5Ff9KbkktABf3/Vh/BUuKfG0ATYSoe9mQakJIsFP34c/z+uKHIeP/IkOQ+PnIUuDz3JzuvYR4WkvDOEb9uSHfPUnXNmuhg3yX3FpANV7/fH8mD+i/ESyRuylWsq3k6TucgE78WP5WGhOasdKg4RizIFLRbVQb/bnmaaf9orLQ7Q3A/4jFZ2+aadQEM+cdNBNkh76XQ4dZGVU8rswgVMZgHxg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=xGnXTW5VC6tcdOlTLMFIueElZmHVbODoCfPsVjIEEzE=; b=duyV/r6R2wwNGQGQiCFDL72cIicq17Ru/MLWAwo14DcyuenE943dgBHgl8v5qJU/1nZ3wbeOUv3mRaVnH1nDzecv7Xr78yhvTGOO28ncW34aLtrbPnt8GJYu8gg0Wi3HJ5/7I4/8wh/qd/ik/o8/aN3kZtgZfeAQuYVE+vwUzLs1YvTSHey2UJG5i6N+xygrMZWCs/7K8WleqzbCYV8Px4+gKAvE90rxh3Y1T50uqqgSXCfz0F5kqlH80hURi/x04hKZLqoLFQHebIfKvqg0K8+i7wRBfcYNFNbEzmgp4O1i6qZpufqN4wXwedwKdWQRmvCMewvMo0axFOSHGV8r/Q== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=xGnXTW5VC6tcdOlTLMFIueElZmHVbODoCfPsVjIEEzE=; b=TlhpPo6XiQfIvvNw6hIo0PdcSzHpZB4/hJGDdXNtBAhbU7/m6HQXCGFTS7kClVpV0dkR7USd3g0gERk5sTOCcR5O1fUuuxrJXAbJEtUDgP6yfbjqv42cS2hWOHK5O8C1mAiW5on20JASNLrzjQJQd1d40kQAe/H93fuJIhtKmdQ= Received: from SN6PR10MB3022.namprd10.prod.outlook.com (2603:10b6:805:d8::25) by CY5PR10MB6120.namprd10.prod.outlook.com (2603:10b6:930:34::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6156.27; Tue, 11 Apr 2023 15:11:45 +0000 Received: from SN6PR10MB3022.namprd10.prod.outlook.com ([fe80::8bb9:2bb7:3930:b5da]) by SN6PR10MB3022.namprd10.prod.outlook.com ([fe80::8bb9:2bb7:3930:b5da%7]) with mapi id 15.20.6277.035; Tue, 11 Apr 2023 15:11:44 +0000 From: "Liam R. Howlett" 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 Subject: [PATCH 6.1 10/14] maple_tree: fix freeing of nodes in rcu mode Date: Tue, 11 Apr 2023 11:10:51 -0400 Message-Id: <20230411151055.2910579-11-Liam.Howlett@oracle.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230411151055.2910579-1-Liam.Howlett@oracle.com> References: <20230411151055.2910579-1-Liam.Howlett@oracle.com> Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: YT4PR01CA0042.CANPRD01.PROD.OUTLOOK.COM (2603:10b6:b01:fe::22) To SN6PR10MB3022.namprd10.prod.outlook.com (2603:10b6:805:d8::25) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SN6PR10MB3022:EE_|CY5PR10MB6120:EE_ X-MS-Office365-Filtering-Correlation-Id: 22ea3d3a-f7f3-455b-c1c7-08db3a9f1021 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR10MB3022.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230028)(39860400002)(396003)(376002)(136003)(366004)(346002)(451199021)(38100700002)(36756003)(86362001)(316002)(26005)(6506007)(8936002)(5660300002)(6512007)(186003)(41300700001)(478600001)(6666004)(1076003)(2906002)(107886003)(66556008)(66946007)(54906003)(8676002)(6486002)(966005)(2616005)(83380400001)(4326008)(66476007);DIR:OUT;SFP:1101; X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 22ea3d3a-f7f3-455b-c1c7-08db3a9f1021 X-MS-Exchange-CrossTenant-AuthSource: SN6PR10MB3022.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 11 Apr 2023 15:11:44.9563 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: VEtO3mikCSYF3tkFNtY/ldYXhjCisG6JPfLfn2w8zqVsxy2SvIk2X9V3/QaOer+V+MjeCNaoAFz63VC+qIRofw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY5PR10MB6120 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-04-11_10,2023-04-11_02,2023-02-09_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 mlxlogscore=999 phishscore=0 mlxscore=0 malwarescore=0 suspectscore=0 bulkscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2303200000 definitions=main-2304110138 X-Proofpoint-GUID: q_cYDzAUWgXFOG4g9smWkTKZqEC0NWrl X-Proofpoint-ORIG-GUID: q_cYDzAUWgXFOG4g9smWkTKZqEC0NWrl X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H2, SPF_HELO_NONE,SPF_NONE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- lib/maple_tree.c | 73 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 62 insertions(+), 11 deletions(-) diff --git a/lib/maple_tree.c b/lib/maple_tree.c index 6fcf08dbdbf9..0f0a2d4850e8 100644 --- a/lib/maple_tree.c +++ b/lib/maple_tree.c @@ -892,6 +892,44 @@ static inline void ma_set_meta(struct maple_node *mn, enum maple_type mt, meta->end = end; } +/* + * 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 @@ -5439,20 +5477,22 @@ static inline int mas_rev_alloc(struct ma_state *mas, unsigned long min, * 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); @@ -5471,14 +5511,13 @@ unsigned char mas_dead_leaves(struct ma_state *mas, void __rcu **slots) 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)); @@ -5542,11 +5581,14 @@ static inline void __rcu **mas_destroy_descend(struct ma_state *mas, 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; @@ -5566,13 +5608,18 @@ static void mt_destroy_walk(struct maple_enode *enode, unsigned char ma_flags, 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 { @@ -5580,7 +5627,8 @@ static void mt_destroy_walk(struct maple_enode *enode, unsigned char ma_flags, 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; @@ -5604,7 +5652,8 @@ static void mt_destroy_walk(struct maple_enode *enode, unsigned char ma_flags, } 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); @@ -5614,6 +5663,8 @@ static void mt_destroy_walk(struct maple_enode *enode, unsigned char ma_flags, free_leaf: if (free) mt_free_rcu(&node->rcu); + else + mas_clear_meta(&mas, node, node->type); } /* -- 2.39.2