From patchwork Mon Aug 12 19:05:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sid Kumar X-Patchwork-Id: 13760952 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 E0F59C3DA7F for ; Mon, 12 Aug 2024 19:06:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4DDA46B00C0; Mon, 12 Aug 2024 15:06:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 441506B00C1; Mon, 12 Aug 2024 15:06:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 294EA6B00C2; Mon, 12 Aug 2024 15:06:01 -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 06D2B6B00C0 for ; Mon, 12 Aug 2024 15:06:00 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 9F80C80366 for ; Mon, 12 Aug 2024 19:06:00 +0000 (UTC) X-FDA: 82444523280.09.27E92D3 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by imf17.hostedemail.com (Postfix) with ESMTP id B8C0C4002A for ; Mon, 12 Aug 2024 19:05:57 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b=Ybjwyw+R; spf=pass (imf17.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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723489522; a=rsa-sha256; cv=none; b=iKp0wEfvzWNa/FyugyJ0CX6Uf1UXmuVoKy1ALn2CK7LeicNy3NjYlrJMWqbvdr/IRD3g0P SYVyjSJfSvrPIjiVrQphyTAWYqFhWVuIcA4cfJOJljCqkvgeFVyn+/YZtJkUwTKUf0j8fn JiG5/S12YLumG/NfNPNXybRn8FnmzjE= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b=Ybjwyw+R; spf=pass (imf17.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=1723489522; 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=hzJpDomWqI4XAKH5M6JOzCbongljBXfxlpCrhMR2RbI=; b=leUte/HAv4bPTfzZVWJQapD4KeMP4CDmN97ks+ajMyXTB3sWmbg1cbIlamSUL/RnGLKtVR 9phmiSA6jQxRCI/qFIQQMKmLHmSONIEP9HDYKBNB4NwZTJ+iEOInvkMXZTkjb/OpCbOzFh cOT2UoqWCC8+Hd8UdHXBejUx5MmEsNE= 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 47CHXVMA007029; Mon, 12 Aug 2024 19:05:50 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=hzJpDomWqI4XAK H5M6JOzCbongljBXfxlpCrhMR2RbI=; b=Ybjwyw+RMoaTJfi0g6NaelslofkTpP yt7GSIAJN4TuOVEhY2INVgr8SSWDE+ypgAUZbca18RG1gPY3f+RmZN4KcimsX90i fnE75pYtKIvWTKoF3WVvgFKrX4Cz3M9P8PI2j5sgdLt2P0pQ54BswD+DLmGVL+ZX +UfQnLbsA7EvtMKEvHy2psvMZlXXICzMTru15xdOeMGzvRJkWOBz4NHhaikOThj6 SzFut2jr+ZVDkuN72s5/6IQ7fyA11dot/aplvfLel2Ik+wCaa70eMXlPY9PYzl0M Tc5cVvjzqIBtZPRi40X8PCwnXKbdaf4SlKI6DwsjR7IvtOEXNiNpo2MA== Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.appoci.oracle.com [138.1.37.129]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 40wxmcud7h-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 12 Aug 2024 19:05:50 +0000 (GMT) Received: from pps.filterd (phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (8.17.1.19/8.17.1.19) with ESMTP id 47CIhSbd017606; Mon, 12 Aug 2024 19:05:49 GMT Received: from pps.reinject (localhost [127.0.0.1]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 40wxn8dfcp-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 12 Aug 2024 19:05:48 +0000 Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 47CJ22Kf022973; Mon, 12 Aug 2024 19:05:48 GMT Received: from sidkumar-mac.us.oracle.com (dhcp-10-65-176-210.vpn.oracle.com [10.65.176.210]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTP id 40wxn8dfa3-1; Mon, 12 Aug 2024 19:05:48 +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 v2 1/2] maple_tree: reset mas->index and mas->last on write retries Date: Mon, 12 Aug 2024 15:05:42 -0400 Message-ID: <20240812190543.71967-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-12_12,2024-08-12_02,2024-05-17_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxscore=0 adultscore=0 spamscore=0 phishscore=0 malwarescore=0 suspectscore=0 bulkscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2407110000 definitions=main-2408120141 X-Proofpoint-ORIG-GUID: 6I9s2c9p3OEU8v8FvF9ViXXLFMoqIjAi X-Proofpoint-GUID: 6I9s2c9p3OEU8v8FvF9ViXXLFMoqIjAi X-Stat-Signature: nzf4ijhiji7iirz4notkagc41inh9sa8 X-Rspamd-Queue-Id: B8C0C4002A X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1723489557-743732 X-HE-Meta: U2FsdGVkX19F2AOa9J7to1u0LO5tN4vG17Bpj8FjXE2AY8LQP7k9nxzbBysq8gRGINFVMG1CrSpSdgLDscCdGSF8Swau9HE9nzpPveibVkkYUMmTedwEazKhMYENcCrytYn+fW0mD6tXs7x7eBKZFvmZn+mSSVsyJ5nc4T/NOcNk3C5h8LvPhk7VWnePJKXBvRzxY+HBjKHWTrQ10ZHWYSZg7R5QdRMMxZYp9Nl4C/iI0losBml+FJQcIvcVwOn0k21hN08KVuX1ZZKbppZMwF06Q/cyZT59tUkoibEd2rZx6slcb1KQyVQ+B3nfuBGceRrj/sFr8He+RlFPUGgj59WmyDtjxwEHkbXOH+hijX+zSPyXJK1TO8hX36/6mwkP5VU3yRtLnmnVhPg/T86oww2Ip3Mk7Z0BFz12F9N3JEXxwFBzPk1Sfo2VLiJr+GExPDWQrncXtEtSd9BNXC1BuoL5xgH9fwmNrVzzSz7KvCByDhEXJyA8W3YXwHCOOGd1PSSWwwUHNZhg99kA4OOnsCcVa+yeAUuUktkUJE7Ey5dxdle61qcmQNZTsu8K5Uaqo7JDr3dP3HDw7gV5R6S74NQJFDQu7GbjW/mtZLEsb04AHrNYYAlbVamJRdDGDc2dTg06QFCU/9Aan6XgcxZE5o0Ol1CG+2xofKHZ3gxQN9ZO3OmnK09KqIDUftr4AsKo4BqpX7zV5iJydoqmBEpcQLtRIDemU6QqBwkMZSHVOhLrI9SwHSHUcA+KQ1d4Geb4hcN1OJi1HFCinSNFK12oDgssFWsXaKBFFEDJngLs8595Frh9wy3B3xb2zr/Mny1KtiO8QfER8GsDmiGs+fNzSS95+p+FNt9CJNfI8O3iMTeeTc7wsI7zErqM4TYEFTdg4jq02XK6He/H64Z/QkfOkv4dZL3MXBTm48VijEK79EZ9ozIJ13LeWnOPVy9FXy/WYnLn6OMBmEcJTZDTBDm fa+1R95G GFpSHtfFFSUas0SfGnR9XJBprBH73FEdjTx0yz46hf6JXzAogqTOTm1SmLTSWw8+roF8KAZb8fMeatJZ/JWUbo2NxI4RLcGCmh87a4ElTTDrxgIRuH5pi5OEJJ9e3W6qvLBHvmr6EjOk7Faw91G2ru9lfw7bXTRi/5tNxh3LJ76lVYAp7JB9I3HNgEr1PwR5GQKvbgzWqZiPrJRcsEp+39KspBEk1sxC1paKiLo9NTu0TCjeKUXHCTEX4nP0nZOKojhCS 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 Reviewed-by: Liam R. Howlett --- v1 -> v2: - re-initialize index and last in the mas_nomem() if statement so fast path is not effected in mas_erase(). - use __mas_set_range() rather than set mas->index and mas->last directly. lib/maple_tree.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/maple_tree.c b/lib/maple_tree.c index aa3a5df15b8e..b547ff211ac7 100644 --- a/lib/maple_tree.c +++ b/lib/maple_tree.c @@ -5451,14 +5451,19 @@ 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_set_range(mas, index, last); goto retry; + } if (unlikely(mas_is_err(mas))) return xa_err(mas->node); @@ -6245,23 +6250,26 @@ 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: 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); mas_wr_store_entry(&wr_mas); - if (mas_nomem(mas, GFP_KERNEL)) + if (mas_nomem(mas, GFP_KERNEL)) { + /* in case the range of entry changed when unlocked */ + mas->index = mas->last = index; goto write_retry; + } return entry; } From patchwork Mon Aug 12 19:05:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sid Kumar X-Patchwork-Id: 13760953 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 834A5C531DE for ; Mon, 12 Aug 2024 19:06:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AF9246B00C1; Mon, 12 Aug 2024 15:06:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A82B96B00C2; Mon, 12 Aug 2024 15:06:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 838E86B00C3; Mon, 12 Aug 2024 15:06:02 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 5F97E6B00C1 for ; Mon, 12 Aug 2024 15:06:02 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 1F905160344 for ; Mon, 12 Aug 2024 19:06:02 +0000 (UTC) X-FDA: 82444523364.22.2C6DA48 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) by imf06.hostedemail.com (Postfix) with ESMTP id 0F11218000E for ; Mon, 12 Aug 2024 19:05:59 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b=culyO0NO; dmarc=pass (policy=reject) header.from=oracle.com; spf=pass (imf06.hostedemail.com: domain of sidhartha.kumar@oracle.com designates 205.220.165.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=1723489482; 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=8QTi9OJ0WYuil/jQ98RXaB1n034ZwVOed6J+lTgWExk=; b=bk1v/J+7k0Qx+jq9xHhdkc5zU+UWbcufr+XeqnylEzpNlPkpLnucV3x4FIV7yIMu+BE/6q YavGiEPXBeN1WCr6RYModgC1hpLT8Qyms4mA0itB/nwxkMU0NUrDQMkMLWnxEk6bpaF9ze ZnBuBfZfsxxb8waYhBMjo95aOb7D4pw= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1723489482; a=rsa-sha256; cv=none; b=bwj0oHSE+i40LdNEw4ZDu28kEV/SkqTTDRlAh66PmSGo7lBW0949MATOuGKgw9N5Kkl/pK jGqKmt124DlIIALW8U67wi9xpK1CkoDSi9ikCK69IBkIt/76mO+vgdvfJW1YT2ffbhlVoA 9dnygFOc8ypzaletylSemY0/aZUrpCg= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b=culyO0NO; dmarc=pass (policy=reject) header.from=oracle.com; spf=pass (imf06.hostedemail.com: domain of sidhartha.kumar@oracle.com designates 205.220.165.32 as permitted sender) smtp.mailfrom=sidhartha.kumar@oracle.com Received: from pps.filterd (m0246629.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 47CHXnhS018534; Mon, 12 Aug 2024 19:05:51 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=8 QTi9OJ0WYuil/jQ98RXaB1n034ZwVOed6J+lTgWExk=; b=culyO0NOP0oVmsnsn cbPrM9zUWvS1ZtsMK6xES9k8eLzGvfUbwlH9QkQMEe61bf7c5uNw3va2wpfoB0cw pHQrOwNbGwSOO9CtZ28bVk4PuKqhQ+aYqc2TR8HxkdD7F4ulCfYN3aBVzYzgObc4 8T3pRh8iunpYJMfNt5WldhuaXasZB0Qj1DuGkezQ79r7CJfBe2JkLaXX8EJkkMMZ uXZMZe0EpzFdBRVY6mS0vlGo9LN8FvDq51MLMJuzcytXPkitPyV3RVwOzQD94fXL 5wVB09Pka3B654l4HGcyodrV1nNl0r6VI+c2ZUjFtVEJmz9XY8sz0IkyDP6puEVB mv+Jw== Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.appoci.oracle.com [138.1.37.129]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 40x0393ck9-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 12 Aug 2024 19:05:50 +0000 (GMT) Received: from pps.filterd (phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (8.17.1.19/8.17.1.19) with ESMTP id 47CJ3jNO017630; Mon, 12 Aug 2024 19:05:50 GMT Received: from pps.reinject (localhost [127.0.0.1]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 40wxn8dfdg-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 12 Aug 2024 19:05:50 +0000 Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 47CJ22Kh022973; Mon, 12 Aug 2024 19:05:49 GMT Received: from sidkumar-mac.us.oracle.com (dhcp-10-65-176-210.vpn.oracle.com [10.65.176.210]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTP id 40wxn8dfa3-2; Mon, 12 Aug 2024 19:05:49 +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 v2 2/2] maple_tree: add test to replicate low memory race conditions Date: Mon, 12 Aug 2024 15:05:43 -0400 Message-ID: <20240812190543.71967-2-sidhartha.kumar@oracle.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240812190543.71967-1-sidhartha.kumar@oracle.com> References: <20240812190543.71967-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-12_12,2024-08-12_02,2024-05-17_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxscore=0 adultscore=0 spamscore=0 phishscore=0 malwarescore=0 suspectscore=0 bulkscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2407110000 definitions=main-2408120141 X-Proofpoint-ORIG-GUID: rAZAEemYjuAn6o_hPAuZF05o8qpaOcbe X-Proofpoint-GUID: rAZAEemYjuAn6o_hPAuZF05o8qpaOcbe X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 0F11218000E X-Stat-Signature: 46zjqdaxa36ge31itbikwibt891gbgne X-Rspam-User: X-HE-Tag: 1723489559-816553 X-HE-Meta: U2FsdGVkX1+AJc8/ZLrXLkCa8zri8gCtDgydI0etupdYIhgu2q4YLlTLg/niBWvnRAF2+8nlMHA2vYWrGiOBP19x0YJf5sWE9kJoJP2mqB76qQy49YsJTb7KTpqFSTxLqvDzebShVZooWnRNeGSNmRDgQiUtxvj4OHuHmF/OrzcWQvfhXC7Zz7eBQgDk2S5kvzd/X3909Y+TyZwuv6tDsKPjVvSwDLNGupOcWtB2DbSHMs8ZI9wOVH9NdSD6BLYPKSvqrEWv3N2Ja3rQeo5j+22GQMMO60paxRhESIcm+bbFNQx4uUMjUppEb/w97OJ2lfvaAkTcHWQ0IKDQ/dNUuOm7jxJjhdZ9CzaUIpqe20zyObNfjLAp3aTbv8dT0alppAJwSZAbbt8/5W2Mr0TkTB42s/V6nmqPloh8+ixUIUrGAdUcYVV45t3LE4r4p8/RiDAlSSc2okuZLVaNmaH6nsO43xe67xOjGysGNvECic3iKhOkDzhSyjJB7k+85oC4eJHkeDgjTqZ1bo2xcEJEuXRkwy8nb4Rm4Z+katpo2AvSMaRjMd8TLRh0TRBU/2u/CN/qnPD24phHmD9rvzwm6MKg+En1v6mvrITHwr0pKnw0d2ZG6Z3ni3rSPHQU5xp/nqk098uqhdtbYgG5esKAFcjT3VaTyJlHgX5/wQ6zhcYCDpK+CpgdO5SsEKeN8tx8qzywrjUR2JBr2JiL4vlt/0b7aMUPDP5qiuPuxx/sGspJj1ijUFehqeQWPxAOpPHuknsBLYiV3XVTlGoGHo3YcFV8yG5bwOKfxXvYV3gVyZM0Goo2tnx6eHNHcOL+Tz4Dc9TRtxvg59EGRrSt58zQuN5il+Psdwkcxht/AFKj5tRflNM3cyi75HY9mjHDaoIPlwYm4EHP8zsaQagp2uq30PTbgnI5ggs7zGPdbUWiDFWcLjxzI+JXIZZwdnhGyzzNNV087/pP5x9i7Lz3lVr yYs3mzto qmB+/fb6wMf5AepJcCQ9CA3TqUxhMyBQdxl4SgPpEN+l297jqiJC54YBLWUSke6mlZk8MjmXyN0gAArUuIpkqzbOCuqguYeGKlhb2KBd8FHJmPzeAp+ZcZ/Wr1as8ULkNZd6XHiH8AdeiK5Jtxs5M8rNYmRhRWzdmiWpFPmi91r/VLgBBrNNFEGNR/p9jAMwFTJXmd3kgkp6FXdcczZ1TSuspWWAhfTvdKEyjlDRaF0+4rACSSdQw0TzY2ccjn+0urvup 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. Signed-off-by: Sidhartha Kumar Reviewed-by: Liam R. Howlett --- v1 -> v2: - change test name to check_nomem_writer_race() - move test down in farmer_tests() - remove mas_destroy() from check_nomem_writer_race() as its not needed - remove using mas.index and mas.last directly through mas_set_range() and MA_STATE macros. - remove uneeded mas_reset() in check_nomem_writer_race(). lib/maple_tree.c | 13 +++++++ tools/testing/radix-tree/maple.c | 63 ++++++++++++++++++++++++++++++++ tools/testing/shared/linux.c | 26 ++++++++++++- 3 files changed, 101 insertions(+), 1 deletion(-) diff --git a/lib/maple_tree.c b/lib/maple_tree.c index b547ff211ac7..14d7864b8d53 100644 --- a/lib/maple_tree.c +++ b/lib/maple_tree.c @@ -7005,6 +7005,19 @@ 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..ef5b83cf94ea 100644 --- a/tools/testing/radix-tree/maple.c +++ b/tools/testing/radix-tree/maple.c @@ -36224,6 +36224,65 @@ static noinline void __init check_mtree_dup(struct maple_tree *mt) extern void test_kmem_cache_bulk(void); +/* callback function used for check_nomem_writer_race() */ +static void writer2(void *maple_tree) +{ + struct maple_tree *mt = (struct maple_tree *)maple_tree; + MA_STATE(mas, mt, 6, 10); + + mtree_lock(mas.tree); + mas_store(&mas, xa_mk_value(0xC)); + mas_destroy(&mas); + mtree_unlock(mas.tree); +} + +/* + * check_nomem_writer_race() - test a possible race in the mas_nomem() path + * @mt: The tree to build. + * + * There is a possible race condition in low memory conditions when mas_nomem() + * gives up its lock. A second writer can chagne the entry that the primary + * writer executing the mas_nomem() path is modifying. This test recreates this + * scenario to ensure we are handling it correctly. + */ +static void check_nomem_writer_race(struct maple_tree *mt) +{ + MA_STATE(mas, mt, 0, 5); + + 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_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); + + mas_set_range(&mas, 0, 5); + mtree_lock(mt); + mas_store_gfp(&mas, NULL, GFP_KERNEL); + + /* ensure write made by writer 2 is retained */ + check_load(mt, 6, xa_mk_value(0xC)); + + mt_set_private(NULL); + mt_set_callback(NULL); + mtree_unlock(mt); +} + void farmer_tests(void) { struct maple_node *node; @@ -36257,6 +36316,10 @@ void farmer_tests(void) check_dfs_preorder(&tree); mtree_destroy(&tree); + mt_init_flags(&tree, MT_FLAGS_ALLOC_RANGE | MT_FLAGS_USE_RCU); + check_nomem_writer_race(&tree); + mtree_destroy(&tree); + mt_init_flags(&tree, MT_FLAGS_ALLOC_RANGE); check_prealloc(&tree); mtree_destroy(&tree); 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; }