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+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amicity=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);