From patchwork Thu Aug 8 16:29:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sidhartha Kumar X-Patchwork-Id: 13757920 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 C0E67C3DA4A for ; Thu, 8 Aug 2024 16:30:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 27E916B008A; Thu, 8 Aug 2024 12:30:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 22F236B008C; Thu, 8 Aug 2024 12:30:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0F6C96B0093; Thu, 8 Aug 2024 12:30:16 -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 E43FA6B008A for ; Thu, 8 Aug 2024 12:30:15 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 0E34B161525 for ; Thu, 8 Aug 2024 16:30:14 +0000 (UTC) X-FDA: 82429615590.27.585E790 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by imf08.hostedemail.com (Postfix) with ESMTP id BD112160019 for ; Thu, 8 Aug 2024 16:30:12 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b="hMq/0yWk"; dmarc=pass (policy=reject) header.from=oracle.com; spf=pass (imf08.hostedemail.com: domain of sidhartha.kumar@oracle.com designates 205.220.177.32 as permitted sender) smtp.mailfrom=sidhartha.kumar@oracle.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723134560; a=rsa-sha256; cv=none; b=ByFMzgPzIQQtfgbizmDsnR0E1tc42p4uddeQtLkAQcEof/okfHBRTU6RJEGyRWSH/CwErg 8ad4b25QdC5+aZS3lEHf64nCowckRo2B2HoNnBhB23n8DJy7vkfaZpl527LDXhr2VQl11j eNhEeGjHKvPCI2/rasjFvCvKtNFdkQ0= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b="hMq/0yWk"; dmarc=pass (policy=reject) header.from=oracle.com; spf=pass (imf08.hostedemail.com: domain of sidhartha.kumar@oracle.com designates 205.220.177.32 as permitted sender) smtp.mailfrom=sidhartha.kumar@oracle.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723134560; 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:references:dkim-signature; bh=MCKv0iZLaIKB2UxF0JuAuzv2IhnYhIEWfUHEcWlI/e0=; b=JtbqNooQaUK4Gb90W5KfQxjnDlSKA5WUEjELUfN7aTPWDh+2LvNn9jZSZnImJ3LIAsEGOQ emkrsfSK2CEIdYdRV8Hrt64/VWa6oAzleOeAEf2lIvtP0BCdPRov6Jn5Nj6+cOjFeo20yn XH/aZKvsmO2OA2xe9CWuj2MYrV8PrvE= Received: from pps.filterd (m0246632.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 478AMWmg014888; Thu, 8 Aug 2024 16:30:06 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h= from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; s=corp-2023-11-20; bh=MCKv0iZLaIKB2U xF0JuAuzv2IhnYhIEWfUHEcWlI/e0=; b=hMq/0yWkdLVu68vKITOB4PFk1ove1U mXaVtx4QKrgbHNZ5gAt4Az5MCnoO8oVChKNuOW4+u/TSzmg7Vi7JNkvtaP8m2zGL xl5y5+GrbOADOCthWTIYRtvJ7xY0cMk1Jxg3T2AgHtxZ7XMJ6dhjo4TNBoGkgvA9 slDnu/OSdF1o6Z9/uk8OktfG5MLV5leCqvLF7LA3QBqYADDA7ioiUmuCRDP0sC6J 2sxNWeSdHsKvdYcpjmYm+qdII/KNQWByU++rBXEawgrZDee0I/5dd3GNfMYO0xTq ktYw1btlB/9XyKtiH/F44IwHrB5UISwC4Je7BNhFPnz8TRCymnK9vM4w== Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.appoci.oracle.com [147.154.18.20]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 40sc5tj6ja-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 08 Aug 2024 16:30:06 +0000 (GMT) Received: from pps.filterd (iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (8.17.1.19/8.17.1.19) with ESMTP id 478GAa0a039522; Thu, 8 Aug 2024 16:30:05 GMT Received: from pps.reinject (localhost [127.0.0.1]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 40sb0cvkqf-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 08 Aug 2024 16:30:05 +0000 Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 478GU5On039245; Thu, 8 Aug 2024 16:30:05 GMT Received: from sidkumar-mac.us.oracle.com (dhcp-10-65-170-228.vpn.oracle.com [10.65.170.228]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTP id 40sb0cvkpn-1; Thu, 08 Aug 2024 16:30:05 +0000 From: Sidhartha Kumar To: linux-kernel@vger.kernel.org, maple-tree@lists.infradead.org Cc: linux-mm@kvack.org, akpm@linux-foundation.org, liam.howlett@oracle.com, willy@infradead.org, Sidhartha Kumar Subject: [PATCH 1/2] maple_tree: add test to replicate low memory race conditions Date: Thu, 8 Aug 2024 12:29:59 -0400 Message-ID: <20240808163000.25053-1-sidhartha.kumar@oracle.com> X-Mailer: git-send-email 2.46.0 MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.680,FMLib:17.12.28.16 definitions=2024-08-08_16,2024-08-07_01,2024-05-17_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 suspectscore=0 adultscore=0 bulkscore=0 malwarescore=0 mlxlogscore=999 mlxscore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2407110000 definitions=main-2408080116 X-Proofpoint-ORIG-GUID: dwviSdDfYBhYQgD9tRC6g2YMHe7I4dHP X-Proofpoint-GUID: dwviSdDfYBhYQgD9tRC6g2YMHe7I4dHP X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: BD112160019 X-Stat-Signature: f934ge1ed6yxjcbicejrwmo7pdm7obbe X-Rspam-User: X-HE-Tag: 1723134612-892081 X-HE-Meta: U2FsdGVkX19cSZtstE0UOKANY2DbuurcnIHHC1XhUHNrsIt7gnIHDQqqEM1ZGSCb38PbzKrhpBbtIFOe85JEKAiYA/zh+m7AVa12Ten9TKrpBBaVduSqTi8Jrqf8PmmLXUDFLgQFaBVH0ixsgVahgbVcM/Al4xZi/I78jnLBmiyoyrcPs+FnhaI8JeDo5qniCWQhSEEUXxYWehgw96dIpu7kwgtCl07L8pfAiGsJT7CKjQDhwK/vF0gnE0voKWEj/0qLZG3lQHgo73ktuiSHHvDYNIJ/kP4Q35ot9q8iYscC9F11n11drZAqSFnE4xKRc5nYVBs0SBaL2l7IpkjJ9CFr/8hGDh+Ks3/ElM9kTXCPWwEWDwzuYCl9uOfpXc1mSYnpwxW0yBt10DkG17cdUL8xT7azsttia0AerjK7+vfoUdybbtvgwsQeMrXzyf4VeXJVyMWFY67B6URgd+g3KYDvtXIK5C7eXS3G+kK0uN7+AVYxdoHyH0DW/i5y8j9Saz+dtd6gzb5blO0v1YIIaucD0YVPpc0oLBBtBNUSeXjJwXEb9fOzX7ZbcVF0gEMQWMj6uHXBkVo3OF/WCmd8UIfQrWuGXEbEZuCLFX/3jyCzEC2ZCpPQvICH4J8jBEypZYaN3qSq3N/x1CyovI9ajWRzkyAihlIene+spQS9mNlSGE4lFbANcDvH77zJA0VAbou333ezYkIihp/7B0Gtf9qLSXBrjadnIbwJjPXxoMeP5LqryTLy/wdcXL2Ktm0HZazcRcQFCRN1VncLO5wHkrhQvMzk1S/90G7bxWNU+IBGl/n13OlflU1oIZPGz1RUWGZJSFYXb5TN2InVoh26cj54MvXvxS1VuhhKLRipctbIgNX9NXe6HrSbY4JSJjKorhAZSSmmug43XFwsLDZCK4ZlfPZXSKaVBuKI72qnmq+zT4RjG14ZahT7E+PgcvlIevZxdVMMIhhdGbhd//4 qhHGozw5 L64uK5ukH6J9UBWJOxlIZAu84jHvG8dJ++LkSP3BFpaBPpqmaF3kvEVT295y0Lp4OgxoxJlzP0mC8BvFCMgvRjB2VRz4Q3KLxNPxhO45gSUbYntvFxK22epNx+m4i62FrRezt1tzFXzQ+j63Tn0zZ4W3VvmaOW4YK/z7A/J/e/W3p1RPf1lvgFkXB0cHyKz6yTT61s7aXfOX8QDz/AE+AB/p57piCDLMgZQW4wiTRm3vG7lQFihdGh+MchAm/wTpwBgIwxyzl5DspbtY= 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: Add new callback fields to the userspace implementation of struct kmem_cache. This allows for executing callback functions in order to further test low memory scenarios where node allocation is retried. This callback can help test race conditions by calling a function when a low memory event is tested". This exposes a race condition that is addressed in a subsequent patch. Signed-off-by: Sidhartha Kumar --- lib/maple_tree.c | 12 +++++++ tools/testing/radix-tree/maple.c | 60 ++++++++++++++++++++++++++++++++ tools/testing/shared/linux.c | 26 +++++++++++++- 3 files changed, 97 insertions(+), 1 deletion(-) diff --git a/lib/maple_tree.c b/lib/maple_tree.c index aa3a5df15b8e..65fba37ef999 100644 --- a/lib/maple_tree.c +++ b/lib/maple_tree.c @@ -6997,6 +6997,18 @@ void mt_set_non_kernel(unsigned int val) kmem_cache_set_non_kernel(maple_node_cache, val); } +extern void kmem_cache_set_callback(struct kmem_cache *cachep, void (*callback)(void *)); +void mt_set_callback(void (*callback)(void *)) +{ + kmem_cache_set_callback(maple_node_cache, callback); +} + +extern void kmem_cache_set_private(struct kmem_cache *cachep, void *private); +void mt_set_private(void *private) +{ + kmem_cache_set_private(maple_node_cache, private); +} + extern unsigned long kmem_cache_get_alloc(struct kmem_cache *); unsigned long mt_get_alloc_size(void) { diff --git a/tools/testing/radix-tree/maple.c b/tools/testing/radix-tree/maple.c index cd1cf05503b4..0e699feb71b8 100644 --- a/tools/testing/radix-tree/maple.c +++ b/tools/testing/radix-tree/maple.c @@ -36224,6 +36224,61 @@ static noinline void __init check_mtree_dup(struct maple_tree *mt) extern void test_kmem_cache_bulk(void); +static void writer2(void *maple_tree) +{ + struct maple_tree *mt = (struct maple_tree *)maple_tree; + MA_STATE(mas, mt, 0, 0); + + mtree_lock(mas.tree); + __mas_set_range(&mas, 6, 10); + mas_store(&mas, xa_mk_value(0xC)); + mas_destroy(&mas); + mtree_unlock(mas.tree); +} + +static void check_data_race(struct maple_tree *mt) +{ + MA_STATE(mas, mt, 0, 0); + + mt_set_non_kernel(0); + /* setup root with 2 values with NULL in between */ + mtree_store_range(mt, 0, 5, xa_mk_value(0xA), GFP_KERNEL); + mtree_store_range(mt, 6, 10, NULL, GFP_KERNEL); + mtree_store_range(mt, 11, 15, xa_mk_value(0xB), GFP_KERNEL); + + /* setup writer 2 that will trigger the race condition */ + mt_set_private(mt); + mt_set_callback(writer2); + + mtree_lock(mt); + /* erase 0-5 */ + mas_reset(&mas); + mas.index = 0; + mas.last = 5; + mas_erase(&mas); + + /* index 6-10 should retain the value from writer 2*/ + check_load(mt, 6, xa_mk_value(0xC)); + mtree_unlock(mt); + + /* test for the same race but with mas_store_gfp */ + mtree_store_range(mt, 0, 5, xa_mk_value(0xA), GFP_KERNEL); + mtree_store_range(mt, 6, 10, NULL, GFP_KERNEL); + + mtree_lock(mt); + mas_reset(&mas); + mas.index = 0; + mas.last = 5; + mas_store_gfp(&mas, NULL, GFP_KERNEL); + + check_load(mt, 6, xa_mk_value(0xC)); + + mt_set_private(NULL); + mt_set_callback(NULL); + mas_destroy(&mas); + mtree_unlock(mt); +} + void farmer_tests(void) { struct maple_node *node; @@ -36243,6 +36298,11 @@ void farmer_tests(void) node->mr64.pivot[2] = 0; tree.ma_root = mt_mk_node(node, maple_leaf_64); mt_dump(&tree, mt_dump_dec); + mtree_destroy(&tree); + + mt_init_flags(&tree, MT_FLAGS_ALLOC_RANGE | MT_FLAGS_USE_RCU); + check_data_race(&tree); + mtree_destroy(&tree); node->parent = ma_parent_ptr(node); ma_free_rcu(node); diff --git a/tools/testing/shared/linux.c b/tools/testing/shared/linux.c index 4eb442206d01..17263696b5d8 100644 --- a/tools/testing/shared/linux.c +++ b/tools/testing/shared/linux.c @@ -26,8 +26,21 @@ struct kmem_cache { unsigned int non_kernel; unsigned long nr_allocated; unsigned long nr_tallocated; + bool exec_callback; + void (*callback)(void *); + void *private; }; +void kmem_cache_set_callback(struct kmem_cache *cachep, void (*callback)(void *)) +{ + cachep->callback = callback; +} + +void kmem_cache_set_private(struct kmem_cache *cachep, void *private) +{ + cachep->private = private; +} + void kmem_cache_set_non_kernel(struct kmem_cache *cachep, unsigned int val) { cachep->non_kernel = val; @@ -58,9 +71,17 @@ void *kmem_cache_alloc_lru(struct kmem_cache *cachep, struct list_lru *lru, { void *p; + if (cachep->exec_callback) { + if (cachep->callback) + cachep->callback(cachep->private); + cachep->exec_callback = false; + } + if (!(gfp & __GFP_DIRECT_RECLAIM)) { - if (!cachep->non_kernel) + if (!cachep->non_kernel) { + cachep->exec_callback = true; return NULL; + } cachep->non_kernel--; } @@ -223,6 +244,9 @@ kmem_cache_create(const char *name, unsigned int size, unsigned int align, ret->objs = NULL; ret->ctor = ctor; ret->non_kernel = 0; + ret->exec_callback = false; + ret->callback = NULL; + ret->private = NULL; return ret; } From patchwork Thu Aug 8 16:30:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sidhartha Kumar X-Patchwork-Id: 13757921 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 A5322C52D7B for ; Thu, 8 Aug 2024 16:30:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CDF496B008C; Thu, 8 Aug 2024 12:30:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C8F646B0093; Thu, 8 Aug 2024 12:30:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B08CC6B0095; Thu, 8 Aug 2024 12:30:16 -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 95EBE6B008C for ; Thu, 8 Aug 2024 12:30:16 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 67CD51415DB for ; Thu, 8 Aug 2024 16:30:14 +0000 (UTC) X-FDA: 82429615548.28.DA433DE Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by imf01.hostedemail.com (Postfix) with ESMTP id E2A1F40013 for ; Thu, 8 Aug 2024 16:30:11 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b=R9Cj3YEA; spf=pass (imf01.hostedemail.com: domain of sidhartha.kumar@oracle.com designates 205.220.177.32 as permitted sender) smtp.mailfrom=sidhartha.kumar@oracle.com; dmarc=pass (policy=reject) header.from=oracle.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1723134546; 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=I2cunVBKYInUBWGR+biooL7s+jlkeRxlVBahI3pFvBw=; b=4wtBP4RHBtNatxRNFh4LdHPJgLhe2jHj3vzkxW7on7SzL7Gv7C1bb7xuCCI/vHPv02QvCx cqaUWKRN26PqhwvF/f9cCC79YN9dTZWSiKQRwmxw8WlpWEVQEa7T+LQ1LDfmzEzC2FsvSe zK7CH6HJF1jzp3rY8c7z84c9/D1uB2E= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723134546; a=rsa-sha256; cv=none; b=eukCRKvK5l6lnBhkbiy4a1eV7pwa7N4MVfrtuSq16ucQk6a7zE4Bf0YTYRdnkM2omYSsit KEw4W6slglTRSrib4yysgnS+elWK6Mn+Ds7+k1yJeDDsGu9b6MEfDu+3Oz+3KhWk4R7+Az 9XT3qJtb1B3ALESwIH7m7/cvWvqiZSU= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b=R9Cj3YEA; spf=pass (imf01.hostedemail.com: domain of sidhartha.kumar@oracle.com designates 205.220.177.32 as permitted sender) smtp.mailfrom=sidhartha.kumar@oracle.com; dmarc=pass (policy=reject) header.from=oracle.com Received: from pps.filterd (m0246630.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 478AMYqo024560; Thu, 8 Aug 2024 16:30:07 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 :mime-version:content-transfer-encoding; s=corp-2023-11-20; bh=I 2cunVBKYInUBWGR+biooL7s+jlkeRxlVBahI3pFvBw=; b=R9Cj3YEAMetjVgQs6 tWw51Yuh5aTZUgnweOHGJW++VrIIaJPBjEVDLAieMlD8StiqZxFYhSB1MNOciYUj Q377WaN6BaZTXRJapniCM7cdmwQWRXkXPnjMvpXJ6vPg6cnCLh+OhO9uEdMpOvcO ltNnks5FhK9arjbDW7yb3yFypBHpsPPueKS/eST/CxMuQ3kCx63eG8wggnhNtBjq iphHq29PUJ+vXxyM4XE6K08YOoOgDOAHMBhsfdcZF/2E1P0KwpY556egh5Zdau6R 51+ijE0jfk5gwrV6QjWOv6WU4IL1y0tBkCC8iSgQZTc7Y2g90TuA4JcLLSB9wgsf g0fNg== Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.appoci.oracle.com [147.154.18.20]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 40sayea4u3-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 08 Aug 2024 16:30:07 +0000 (GMT) Received: from pps.filterd (iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (8.17.1.19/8.17.1.19) with ESMTP id 478G4LYm039532; Thu, 8 Aug 2024 16:30:06 GMT Received: from pps.reinject (localhost [127.0.0.1]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 40sb0cvkr7-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 08 Aug 2024 16:30:06 +0000 Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 478GU5Op039245; Thu, 8 Aug 2024 16:30:05 GMT Received: from sidkumar-mac.us.oracle.com (dhcp-10-65-170-228.vpn.oracle.com [10.65.170.228]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTP id 40sb0cvkpn-2; Thu, 08 Aug 2024 16:30:05 +0000 From: Sidhartha Kumar To: linux-kernel@vger.kernel.org, maple-tree@lists.infradead.org Cc: linux-mm@kvack.org, akpm@linux-foundation.org, liam.howlett@oracle.com, willy@infradead.org, Sidhartha Kumar Subject: [PATCH 2/2] maple_tree: reset mas->index and mas->last on write retries Date: Thu, 8 Aug 2024 12:30:00 -0400 Message-ID: <20240808163000.25053-2-sidhartha.kumar@oracle.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240808163000.25053-1-sidhartha.kumar@oracle.com> References: <20240808163000.25053-1-sidhartha.kumar@oracle.com> MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.680,FMLib:17.12.28.16 definitions=2024-08-08_16,2024-08-07_01,2024-05-17_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 phishscore=0 suspectscore=0 adultscore=0 bulkscore=0 malwarescore=0 mlxlogscore=999 mlxscore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2407110000 definitions=main-2408080116 X-Proofpoint-ORIG-GUID: gJa0QCKoVr-mSRtHz71HGb5V55626mTL X-Proofpoint-GUID: gJa0QCKoVr-mSRtHz71HGb5V55626mTL X-Stat-Signature: jn1ksrjcyydxt6hzq1s9bog3yjcb9dfp X-Rspamd-Queue-Id: E2A1F40013 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1723134611-961678 X-HE-Meta: U2FsdGVkX19/M3TC3N0KCBYeu5GvJG3IrfjV613VP20CzniTkLBqYctTh07w0+MEpZHJzCGsWFqM6efMArKMVg1ki7zIvIRIarVx5oo00SaQ9UTsN25+qJWLCNGVuJbAKwLbB/pc7b7NV8sAd715LXWZFz1GoqM4DqKv2rnTWJURJmyurfgQOt3FDcifTyk9HjO4posKyDdVikNYeyGzoKrEAE5lDKTS+QT6m0vEHngDYhXNwyZD/ZPHVQ6QL1rY24aleY3VOw8bX42mAm9L9S+Kp4w8TCo/ReiV+5eFrNcO0Ny/FsrVgsTDE+pFH33Www/+NPyvdMbvb5VzNZi0s1xNNm0xF6m5ws65xmIatlZ619QibZDdvRnTM2bhGyHwjpXJPSAIsX+G1hwhtMKo+HQAuyld6zoJDQeeM10dRU4LpMf7IoCd5L4cCx8Qs2Mrd9knATUsX6s+9ivdAfekAqubWzVKYqvH7Cetfg6vAQmnlwPFpXh0mWGzhQ7Esfnx/tLRxUSyFbF8ABc6oi3PEFNWQZbMI6Erem6SsBObAOmQ5zfV6Y7fJ+3kqacQr42tKDo73YnXTHhk/yM3+4uTp4yMcY3VeZJJRmK/d3P6XzuMIo6eC2RJGjmpgQW7p/PYxIOcerN1vj0y/4EYcHGZ2eZRlK6a4yvP3gwkB0NrS2ySCbR0iPqYA7dSdrdXh34u6iwTLec7MuhIaReLOA8bbOLboLp7c3fY0C/kw/uCkZxql8Mx/k/8gZ9iTki+Wsa+ERNNtwTQmHHBAmaJAdbwsLLQ/wQ7QQeqIFAIzMhHOFLLWbXva4Sg1nSaGIJfST8L4UYjmhXxe3m82+3/dDSvxcXDxfN0Bgcjp/Xxf4EqwmdV/APVqym+z0OI6zFP8Kb56GFjGugfWG3FhOdE+sLyWrGgZvYIJQUSTS/RoWIxadpRx5xr4P0lYKw986SeoDNEub2m2hNLpVu0y5ldRgK G+sTeCJM 1zk8BnG+kA4iDtdWm531Zoux+H3pjIc7jkMArWPMtJwqvhdXN+tsONMv2v/dEcMpsZqpZbWpcT1HCIS8zJ1gkZ1GawsxiiIkNSpXj18cDUQjyHV06vNgo/5NfNM8D4K/ZJWbSvCWhWhixz46dNOibxdHlJgutfOg9MskyEnn9VD+MelQIALVW2YkcqO5aD7YDxxTK6OE1nj7oD0jxcY6/qCXYaW2pWBxrgvp06aRL1lw10BAMIJv9YDhpNR3eL/geO3GTwEprL9QhQgY= 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: The following scenario can result in a race condition: Consider a node with the following indices and values a<------->b<----------->c<--------->d 0xA NULL 0xB CPU 1 CPU 2 --------- --------- mas_set_range(a,b) mas_erase() -> range is expanded (a,c) because of null expansion mas_nomem() mas_unlock() mas_store_range(b,c,0xC) The node now looks like: a<------->b<----------->c<--------->d 0xA 0xC 0xB mas_lock() mas_erase() <------ range of erase is still (a,c) The node is now NULL from (a,c) but the write from CPU 2 should have been retained and range (b,c) should still have 0xC as its value. We can fix this by re-intializing to the original index and last. This does not need a cc: Stable as there are no users of the maple tree which use internal locking and this condition is only possible with internal locking. Signed-off-by: Sidhartha Kumar --- lib/maple_tree.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/maple_tree.c b/lib/maple_tree.c index 65fba37ef999..6ba95a278326 100644 --- a/lib/maple_tree.c +++ b/lib/maple_tree.c @@ -5451,14 +5451,21 @@ EXPORT_SYMBOL_GPL(mas_store); */ int mas_store_gfp(struct ma_state *mas, void *entry, gfp_t gfp) { + unsigned long index = mas->index; + unsigned long last = mas->last; MA_WR_STATE(wr_mas, mas, entry); mas_wr_store_setup(&wr_mas); trace_ma_write(__func__, mas, 0, entry); retry: mas_wr_store_entry(&wr_mas); - if (unlikely(mas_nomem(mas, gfp))) + if (unlikely(mas_nomem(mas, gfp))) { + if (!entry) { + mas->index = index; + mas->last = last; + } goto retry; + } if (unlikely(mas_is_err(mas))) return xa_err(mas->node); @@ -6245,17 +6252,19 @@ EXPORT_SYMBOL_GPL(mas_find_range_rev); void *mas_erase(struct ma_state *mas) { void *entry; + unsigned long index = mas->index; MA_WR_STATE(wr_mas, mas, NULL); if (!mas_is_active(mas) || !mas_is_start(mas)) mas->status = ma_start; - /* Retry unnecessary when holding the write lock. */ +write_retry: + /* reset mas->index and mas->last in case range of entry changed */ + mas->index = mas->last = index; entry = mas_state_walk(mas); if (!entry) return NULL; -write_retry: /* Must reset to ensure spanning writes of last slot are detected */ mas_reset(mas); mas_wr_store_setup(&wr_mas);