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 1106BC369CB for ; Thu, 17 Apr 2025 18:19:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 679A1280072; Thu, 17 Apr 2025 14:19:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6271A280070; Thu, 17 Apr 2025 14:19:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4C838280072; Thu, 17 Apr 2025 14:19:02 -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 2738C280070 for ; Thu, 17 Apr 2025 14:19:02 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 0DC1C160337 for ; Thu, 17 Apr 2025 18:19:02 +0000 (UTC) X-FDA: 83344347324.09.4B92705 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10olkn2031.outbound.protection.outlook.com [40.92.42.31]) by imf08.hostedemail.com (Postfix) with ESMTP id 1D1E3160005 for ; Thu, 17 Apr 2025 18:18:58 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=outlook.com header.s=selector1 header.b=HKA654X8; dmarc=pass (policy=none) header.from=outlook.com; arc=pass ("microsoft.com:s=arcselector10001:i=1"); spf=pass (imf08.hostedemail.com: domain of MartyKareem@outlook.com designates 40.92.42.31 as permitted sender) smtp.mailfrom=MartyKareem@outlook.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1744913939; a=rsa-sha256; cv=pass; b=QuS4g3FLq++Scgp6KJUNYe40Oa2mXCGSAb+f5RUMO8wvVXeFe9oAaoGiNYKz47gTZBIv/n PWbilXT/XZyPgLoN4/40KweyF6msFb1/ng304dDDqbVJAV0iIvBEOYi2gkO8IN4LdU9h/0 typHI/OBqwpuoHZHZLU9g7KQR7ezLrY= ARC-Authentication-Results: i=2; imf08.hostedemail.com; dkim=pass header.d=outlook.com header.s=selector1 header.b=HKA654X8; dmarc=pass (policy=none) header.from=outlook.com; arc=pass ("microsoft.com:s=arcselector10001:i=1"); spf=pass (imf08.hostedemail.com: domain of MartyKareem@outlook.com designates 40.92.42.31 as permitted sender) smtp.mailfrom=MartyKareem@outlook.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1744913939; 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:references:dkim-signature; bh=evnX8/HbExGr1bQtrPZk3rNNmFOnTnMkrXpyi/HN0Rw=; b=zDeXFuxi3hkjXC8T6VrxRWAZTGPZIH844NyQvMyIjMl++iLalIVq+1I3tjRSsHIf6RH4YF fGmhYvjxyEaoQrIK9yWEKBCutyFDO2vfxJyqAWCkaFqVjClXd2wUuYxulPC2tButjDGdsK BQGOp8FeTv1R5O0KJuFO7T9BWaAgqFw= ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=ADusY2ehZ76SX+gY8rypmBg7U4Myus9Y0uQHZ15VjgEarwibbZO+12n/iuZVxSaSmba4i/4/cFGf09i3jgq5acOfhPXr3P7M+9IuN03u1N/bFqAzTf7csuyqdVH+fDCQUpCMY9IE3FNS5L5SXYChB9TE8uP9BvRNgJOE13cmWHa258hdv3QH2GdqY3ij+sndp0Y/47KDMr3jR3ExMP9TQ5t0WydomQXuNZ8XXGAMmnrOIKdgNnIaykpDhBCj7KWWwGUfXxnEKO1L+L2LJqPaOHZZ3B2TcrDpLh9ZrhBr8TSYka2GwaTMcZ+QlqFMP93JkuciVewhVP9J8nwKoq6ohw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=evnX8/HbExGr1bQtrPZk3rNNmFOnTnMkrXpyi/HN0Rw=; b=s2kQnAMTkvbwazCSRiUBdUAOHx4/lVsrNEdVLma4jIIMj8dwV64lQF4C3UsQtKWMnHweJb3Qt94bjRUK8aMC2SZxQqwnrk7uKEHkszo0G9o+NRcu3VYpjMyY2PNCepPyHqMdKznADIM/ZhGTjCQyhC6WWbewvCWYBw2RwImJtxixRbZXS71MIizrFGpeXwj99fMnpNESpizNkkC1CTD8aoWQc0PrAIflEE42Ja2v3cQG8kBTiZDjHC2JNCQfikGoMWpgB4qBk8XhL7zC93UwydUTHYLA61U7pAVPxyTSKck+emtTtRVJKiHJeKQcXTSgwUWn87nTNuhjvCpDyYJeAA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=evnX8/HbExGr1bQtrPZk3rNNmFOnTnMkrXpyi/HN0Rw=; b=HKA654X8qp5mMZqO9VRQUfVdhZJq8aRHNm7CFlvj05JN216JueRs72yTo8SBXscKYCkhDBT1igeGR0jwMvSkc5IgL2pJPHac1XlhEbgqR4nkP1kQIGvnpA+R3nyxF2md1rO7XdsddFD99O2cMussNmypp1E2VhmcOZtH/S8kG6d6oyn4QyKy0tJZzdOnPD7+IKg38mh6au3/vNkGCZbGPctatsFBHweDWL3VjdnqIVu3CIPoPVK8jbA3QVbj1/GjVszb9/OcxACOUo/Gy+v5WJGNyRAUkqN1FxpgaoY33wlydwj9ZzadPktShimMTpuPJckFebf5dz1e67N50H836g== Received: from LV3P220MB1815.NAMP220.PROD.OUTLOOK.COM (2603:10b6:408:1d6::19) by SJ0P220MB0461.NAMP220.PROD.OUTLOOK.COM (2603:10b6:a03:3ac::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8655.21; Thu, 17 Apr 2025 18:18:55 +0000 Received: from LV3P220MB1815.NAMP220.PROD.OUTLOOK.COM ([fe80::6590:3e70:cb56:d25c]) by LV3P220MB1815.NAMP220.PROD.OUTLOOK.COM ([fe80::6590:3e70:cb56:d25c%4]) with mapi id 15.20.8632.030; Thu, 17 Apr 2025 18:18:55 +0000 From: Marty Kareem To: Peter Xu CC: "linux-mm@kvack.org" , "linux-kernel@vger.kernel.org" , "akpm@linux-foundation.org" , "shuah@kernel.org" Subject: Re: [PATCH v2] mm/selftest: Fix race condition in userfaultfd dynamic address allocation Thread-Topic: [PATCH v2] mm/selftest: Fix race condition in userfaultfd dynamic address allocation Thread-Index: AQHbn5BYchljhlDG60CnvXtzsPyaybOoSuuu Date: Thu, 17 Apr 2025 18:18:55 +0000 Message-ID: References: In-Reply-To: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: msip_labels: x-ms-publictraffictype: Email x-ms-traffictypediagnostic: LV3P220MB1815:EE_|SJ0P220MB0461:EE_ x-ms-office365-filtering-correlation-id: 3fa33fb6-7105-4d60-a41d-08dd7ddc5077 x-microsoft-antispam: BCL:0;ARA:14566002|461199028|15080799006|19110799003|8062599003|15030799003|8060799006|102099032|13041999003|3412199025|41001999003|440099028; x-microsoft-antispam-message-info: =?us-ascii?Q?MXwHrmWYusQKsdYr4qMzPyP4OzJ228n03NatRS7m14bsvKe48E3ATucnLsrc?= =?us-ascii?Q?Ejq7RFiXRzJVTRrRd4b5taVUonWSW3jIfCF7BPHNeJTUkRwiyWU9nNbUbbdE?= =?us-ascii?Q?YB2/nbegbC0LizutYNKOvEiHFVi2B2ezAtryGLLtZbcTmQ7m+irXgHulrLU/?= =?us-ascii?Q?vC+93HzHgQsn7atMYnWlLPKGUNsOoed+UgYcYcpuOztaYRbDsNW16FOPDD9l?= =?us-ascii?Q?jF80kgUeSJbgMvoCbm2GIgRS7FLnrmKICRPsFrExJWhHjcOi7m4BNdXB19Ai?= =?us-ascii?Q?UxwisstRDpD1An6p8Smzxnk5Zq1CilKZwFtM2uWdn/MezoCWJOZzZicb3AY4?= =?us-ascii?Q?/B6hxMb/uLD5p6/rO/rwIowwZC4KKm6coOKpZ/9ruMoZtm3GDHHwfvOqPg4E?= =?us-ascii?Q?q+pQkSb/lV3ArzWynjBMEFQBsbZpUcD2NWbQQtm9EzvMYrGjax7POaAPNs8p?= =?us-ascii?Q?g4378ZMBcUsIald5RR3GK6WxtSoCSXO4Im8EycSXBhzOorIa5SyugNpH0Rx8?= =?us-ascii?Q?wIU+HfVGa1i3TQi8UiAXbkyg3vuaQAikT/+jHUX43tfrF5zRrd84CAPLpnR+?= =?us-ascii?Q?JiOLZNAKT3VBJ2mPPd7Ni5/KhGV2+6NYRi/G9egQ0wJeZA6NRX1c7woFKiKf?= =?us-ascii?Q?La4DVDUAun9g1fiisngeSInG6sU1LiOji1DBsEFLdR2ShZMC6Q5iIrd9URdB?= =?us-ascii?Q?m4wmi/3IZXSWIa9OaFUzsCUc8VR6eMQJKRYA55cvAeNT7NU7ex/AsEcbivOU?= =?us-ascii?Q?Aa93bJAM85/epRUWf/Ng+e/iGeseO9KBoLp9Bx0Xh/XibwTYiQCFqabQEwkm?= =?us-ascii?Q?q4krQcIPnOIrHWZ5aEkbjeCR714mlhR2eSeq6Ff1XSW7MFD3+RrM5b7Wl9E8?= =?us-ascii?Q?/ZB2cLtbpGUXiGTM/a9gNr8/LXpW8+PyZLQbqs+ekrFqItjSf0kchbivxupT?= =?us-ascii?Q?bc2LYqefUWr5EzykDf9TO/q5uq9JtcN98gbqjvp0EJzRrox8ABtqsbnl21Xp?= =?us-ascii?Q?NAkHm0intRbTwMkMgRLWoSH2sOcTCkTnCgW4djK6pTN9fJuR/PL/V4p8Yu+l?= =?us-ascii?Q?o1WjrBl9/rQAVidohNQOc+PIG15h3Gpc9Q4bURgIlNosux83Pbc18lin+sMO?= =?us-ascii?Q?WZgQvsiDkQoQUrFFuRRAjG19pLETbS3+SeWIpZervmJ8wih33F6c57f1Sji9?= =?us-ascii?Q?OgT2mtNn8+I+kSUG?= x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?us-ascii?Q?LqrE3y2sPANXWkA2MlOyTpIVku3spQYXKdZP4uJAlGWVCjf+g0tqzhhheyA5?= =?us-ascii?Q?K09W0tp9KWC+m7wj60zNeuNldp25Fjdfwp++rjNZmOZiXuCPC3PssO/9q3OL?= =?us-ascii?Q?H38FOigSDzh8rjgsXp4zGzSNb4hzrV8uJnQJ1s7t+fl4FJdB5bziTH24qPNi?= =?us-ascii?Q?TugjrKGNmVuW24yAZQskk9Rni5ftjH8RqwSRAuBZjAsCFNkBd7xagjCGpLk9?= =?us-ascii?Q?Sni6Z33cjXOM3gS7QBgGJJOSdXS0And4t7kIob0qoZpFv8g9rbof6jG+Vkot?= =?us-ascii?Q?n8rgOioUwgCebkMrggjAOz3iaK7aHBk9IJpUY4jtN8ARyUXFHZxjeYB8DY68?= =?us-ascii?Q?cTYJx60dUIUMzFsMMhlmjjwPwqdZvewuwf3/CDyVBr1ivfO6rOySjADph3zp?= =?us-ascii?Q?/Ee+xfljbHiVdGH0uTqf5sMmKKpauy/LAUoeGMtf6JdzJ/+la/NnOGgF0iUe?= =?us-ascii?Q?tVBTkYiXNnl29oW31jTj4XaaAkk18QS5XiA79YZCcI4T8EzFR38C9CJB2Ph/?= =?us-ascii?Q?SOXpdOfAP+s133/e4o50kiYtijrCEx4gLAoGHraJXIpA6VLSPfGyLdO0sLLW?= =?us-ascii?Q?NuMROhJBV3ecYffeNougmHwTS49skvQlPlyxYY/1HC5AtGelFs42ntiqfIKw?= =?us-ascii?Q?Z7s1tOFxl9Hul5jYDTrtbcJI5MIupdauToF0UmQBI2IWbYDi4pQDyeT4h7PG?= =?us-ascii?Q?eHPLnzAo1/++dRBSU3GiywrR6m9Qz0QFQlYTO2aX3otqtYsJmhjvs7ctS3UF?= =?us-ascii?Q?/M9p1g6FBbjRSAiL1jnLkVZTNqYRDkirKt+2YgAJhLchDBHEtTVEdhGqDFBf?= =?us-ascii?Q?9B6vRaIP8tOSFmDewGNF1O2cdbIHTTSZcnMASfVsOOAfqK69zBmpXkQhBsDd?= =?us-ascii?Q?Qil7hbukPxF/0wqhG4cr95M/JqR3lN5E6wTDGv5hpe1L7yDc6vD8VKSsLWOj?= =?us-ascii?Q?HDBF+y7YyScTUUtYakpuNFR8/nCjy0dnLXTr3TmnwszH82M1gFSX3UF5Kvzo?= =?us-ascii?Q?y5t6ngtAH670geaR4e8XVDLsXGTROfCJIWhc1Vz2W2c/CIY7XjMQvodf+3b+?= =?us-ascii?Q?YwdLG3gxxWRsFPTauh79H7vnMYXTNcU8ma674zMciCz3+T9v8snf7XNdNDDg?= =?us-ascii?Q?kbva8vd6a1bY3zeH3/1Ncsc8ViFPs08gxCRui+lceYFvHWRG9pbXg7nxxId9?= =?us-ascii?Q?Zht9mTZIT+jTA7Sej/7iWuzkkQvATBfZzt69WMarAhANp8B9uloxP5pmWBk?= =?us-ascii?Q?=3D?= Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: LV3P220MB1815.NAMP220.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-CrossTenant-Network-Message-Id: 3fa33fb6-7105-4d60-a41d-08dd7ddc5077 X-MS-Exchange-CrossTenant-originalarrivaltime: 17 Apr 2025 18:18:55.2688 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-rms-persistedconsumerorg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0P220MB0461 X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: 1D1E3160005 X-Stat-Signature: ry9jmn4t3mjki9xj9fa8xuobmmifw6jw X-Rspam-User: X-HE-Tag: 1744913938-326778 X-HE-Meta: U2FsdGVkX198ZZEM+kO+ysyqSCe9EkS3kbWb1vdf+FnnKMficLmwKoSM/mDYyX1Rf3rVzNM4RnvkmXJ+xyPKerrvUZI97q9NIMUQPl97DFviLqO9OU2vNaQ0Nj+yZ01MHP8JAKPARxNUW/jHeYBtwc2pzrnsCaR4HjTuFrhb9IVooLAQAj1AHnjqg+c3dA2BUTErdEvYCAB00uMKNevIuYEeHr+GF0N+KqRAo/rQIWrjuSRfLg7f+G9go7H3vT9YSJ1o3itoTGtHhv5ct/XYrL6QKLFBiBiPO+m5fo/XGlY45HM2oeY5DpOUENiAUBVDfumOgPOynTBiAaTMSnRT7zAZgfHRG7TqTOsCEeOVPs1fZZHgWVCzi7fUK4Bkj8JdKIFPdK8YbPTEOmBrcSznBPoU1utAwchebNVZ0BJRKGg65a86UT4ETub14+gA94fxUAPZwCc1lnHVFOOdH7/o1S+xTq1lYziisshevC2WDXhLBQvxnZ6uo0sj/9szFlHh7trSkkLLx++H5w57y50wx6/rCM2xLQf7E7lMomaXyECtrPgQ3J2Nn6WF/yoVS8dN/Hzd85wZXcOlOsl0g4wyHFY93zElZn1friUJ1erRH4oj726KDlZ30NB80luAvy67KGzpA7iBzt7rauNNLy7224O9tKT/lcvY4ueyh5fbVvxAkB+QNcjYdLoVA4lQdJIkS8+AD5vwD+S71YtIHIGe/1ThcNbpGfc1BYDS9pqNFWuq+o2JqVJy4pM64ogB+e9TfJwgZbgUCHYZPpZTSrX6hYoLsAwWmncZM+VeZAGSK1+cYlHOQn6HUJEVshaNvtZxQeuH6ZvT6wEOs3dU7uaxMJ8BP9gJ9SpK+fPtkb5JzFBS8jQ424op+uS5yrqkP23r5jJPpuJ/mUOxxMC3jw4uYh+S+leAbXRb9ogYkrDNeiMWWf2hmBE8WBud9f4Xg3XdMxoqaX3UCk6exz8aGpP +IbcFa87 LS0R2cKY+kcX4zSNbYKwB6M2mKCkdehFe3GZPF3ILzvjTIVCXmGQ0WEliOOEVpxiLLr2r5TJuse6TLSxt44Ts9kCcA32FKnyu/bjKuXPcC5QH50GT4j2Np9OLUYOl+2cI3e+qzJaIeGVG5+bhalAM4ao6z8hMspoDOXdAZY+aAEDJBHARLlKzDlQNFxRp0m0aMmAxnBcrJGfIm55p/drc+Go7z/9hV9q+1YiMLw/KFh8mpenqkB3ejEGWQpSrwSQjTRk0I76UQHGKkwS5XlMG8jxDnLaD7zMf6ClZCzrbE2GpFzignaIKfirmIOHY5sthrp0mGgapPI2UCjhT8sUE+rmFLTdq8t0AaK3QFfbGV/Ikmp7l2RsAAD1Z/WhaHjPdTkG0AUC4s+9d1irCH1KbjnmITTRFBSTYI5Q0/lHKHMZC2C+MsYyoLlUU3iKZ8Wa/LuRit7J5OnbTLnsGwlyMKpbgeYbraG8BfmHDsoj4UGitoZ8QeV3aEzjBiCC6vBV4vUqBPKuOG8AQHkEDp3SSkLMRANYn/W+YH/EY2omiBpZgJqiFGmVmPEZawZ/rKai1GKpTZH4BIIi+hHjJ5yML8K+MGUq6WP+O3QH/qvXDbVFNP/XnvA+Rpp9g4A== 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: Hello, I'm writing to follow up on the patch I submitted regarding improved dynami= c address allocation in userfaultfd tests. The patch aims to prevent race c= onditions by keeping temporary PROT_NONE reservations active until they can= be atomically replaced with MAP_FIXED mappings. I sent the patch through git send and I was wondering if you've had a chanc= e to review this submission and if you have any feedback or questions about= the implementation. As mentioned in the original submission, this approach= makes the tests more reliable, especially when running in parallel, with m= inimal performance impact. Thank you for your time. I look forward to hearing from you. Best regards, Marty Kareem ________________________________________ From: Marty Kareem Sent: Thursday, March 27, 2025 11:20 PM To: Peter Xu Cc: linux-mm@kvack.org; linux-kernel@vger.kernel.org; akpm@linux-foundation= .org; shuah@kernel.org; MrMartyK Subject: [PATCH v2] mm/selftest: Fix race condition in userfaultfd dynamic = address allocation This patch improves the dynamic address allocation in userfaultfd tests to prevent potential race conditions. Instead of unmapping the PROT_NONE reservation before mapping to that area, we now keep the temporary reservation active until we can atomically replace it with MAP_FIXED. Key changes: 1. Keep PROT_NONE reservation active until ready to use 2. Use MAP_FIXED to atomically replace reservation 3. Remove MAP_FIXED_NOREPLACE conditionals since atomic replacement works on all kernel versions 4. Simplify overall implementation while maintaining robustness This approach prevents race conditions where another thread might grab the memory area between unmapping and remapping, making the tests more reliable especially when running in parallel. Performance impact is minimal (approximately 1.3x overhead vs static addressing) while significantly improving reliability. --- tools/testing/selftests/mm/uffd-common.c | 113 ++++++++--------------- 1 file changed, 39 insertions(+), 74 deletions(-) diff --git a/tools/testing/selftests/mm/uffd-common.c b/tools/testing/selft= ests/mm/uffd-common.c index 56a69c6cc7c4..fab3b79abc15 100644 --- a/tools/testing/selftests/mm/uffd-common.c +++ b/tools/testing/selftests/mm/uffd-common.c @@ -123,54 +123,22 @@ static void shmem_release_pages(char *rel_area) } /** - * Structure to hold the reservation and aligned address information - * This helps prevent race conditions by keeping the original reservation - * active until it can be atomically replaced with the real mapping. - */ -struct addr_mapping { - void *reservation; /* The original memory reservation */ - void *aligned_addr; /* The aligned address for actual use */ - size_t size; /* Size of the reservation */ -}; - -/** - * Find a suitable virtual address area of the requested size and alignmen= t + * Find a suitable virtual address area of the requested size * - * This function obtains a hint from the OS about where a good place to ma= p - * memory might be, creates a temporary reservation to hold the space, and - * calculates an aligned address within that reservation. + * This function creates a temporary reservation with PROT_NONE to hold + * the address space. This reservation prevents other threads from taking + * the address range until we can atomically replace it with our real mapp= ing. * - * IMPORTANT: The caller must eventually unmap the reservation when done - * or replace it with MAP_FIXED to prevent memory leaks. + * IMPORTANT: The caller must eventually replace this reservation with MAP= _FIXED + * or munmap it to prevent memory leaks. * - * @param mapping Pointer to addr_mapping struct that will receive the = results * @param size Size of the memory area needed - * @param alignment Alignment requirement (typically huge page size) - * @return 0 on success, -1 on failure + * @return Reserved memory area or NULL on failure */ -static int find_suitable_area(struct addr_mapping *mapping, size_t size, s= ize_t alignment) +static void *find_suitable_area(size_t size) { - void *addr; - uintptr_t aligned_addr; - - if (!mapping) - return -1; - - /* First create a reservation with PROT_NONE to hold the address sp= ace */ - addr =3D mmap(NULL, size, PROT_NONE, - MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); - if (addr =3D=3D MAP_FAILED) - return -1; - - /* Calculate an aligned address within this reservation */ - aligned_addr =3D ((uintptr_t)addr + alignment - 1) & ~(alignment - = 1); - - /* Store both the reservation and the aligned address */ - mapping->reservation =3D addr; - mapping->aligned_addr =3D (void *)aligned_addr; - mapping->size =3D size; - - return 0; + /* Create a reservation with PROT_NONE to hold the address space */ + return mmap(NULL, size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1,= 0); } static int shmem_allocate_area(void **alloc_area, bool is_src) @@ -179,13 +147,12 @@ static int shmem_allocate_area(void **alloc_area, boo= l is_src) size_t bytes =3D nr_pages * page_size, hpage_size =3D read_pmd_page= size(); unsigned long offset =3D is_src ? 0 : bytes; int mem_fd =3D uffd_mem_fd_create(bytes * 2, false); - struct addr_mapping addr_map =3D {0}; - struct addr_mapping alias_map =3D {0}; - int ret; + void *reserved_area =3D NULL; + void *reserved_alias =3D NULL; - /* Get a suitable address space with reservation */ - ret =3D find_suitable_area(&addr_map, bytes, hpage_size); - if (ret < 0) { + /* Get a suitable address reservation */ + reserved_area =3D find_suitable_area(bytes); + if (reserved_area =3D=3D MAP_FAILED) { /* Couldn't get a reservation, but we can still try without= hints */ *alloc_area =3D mmap(NULL, bytes, PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, offset); @@ -195,21 +162,22 @@ static int shmem_allocate_area(void **alloc_area, boo= l is_src) return -errno; } } else { - void *target_addr =3D addr_map.aligned_addr; + void *target_addr =3D reserved_area; /* If this is dst area, add offset to prevent overlap with = src area */ if (!is_src) { + /* Unmap the original reservation since we're using= a different address */ + munmap(reserved_area, bytes); + /* Calculate new address with the same spacing as o= riginal code */ /* src map + alias + interleaved hpages */ - uintptr_t new_addr =3D (uintptr_t)target_addr + - 2 * (bytes + hpage_size); - - /* Unmap the original reservation since we're using= a different address */ - munmap(addr_map.reservation, addr_map.size); + target_addr =3D (char *)reserved_area + 2 * (bytes = + hpage_size); /* Create a new reservation at the offset location = */ - ret =3D find_suitable_area(&addr_map, bytes, hpage_= size); - if (ret < 0) { + reserved_area =3D mmap(target_addr, bytes, PROT_NON= E, + MAP_PRIVATE | MAP_ANONYMOUS, = -1, 0); + + if (reserved_area =3D=3D MAP_FAILED) { /* Fallback to non-fixed mapping if we can'= t reserve space */ *alloc_area =3D mmap(NULL, bytes, PROT_READ= | PROT_WRITE, MAP_SHARED, mem_fd, offset)= ; @@ -220,7 +188,7 @@ static int shmem_allocate_area(void **alloc_area, bool = is_src) } } else { /* Use our new reservation */ - target_addr =3D addr_map.aligned_addr; + target_addr =3D reserved_area; } } @@ -233,14 +201,11 @@ static int shmem_allocate_area(void **alloc_area, boo= l is_src) *alloc_area =3D mmap(target_addr, bytes, PROT_READ | PROT_W= RITE, MAP_SHARED | MAP_FIXED, mem_fd, offset); - /* Check if the mapping succeeded at our target address */ - if (*alloc_area =3D=3D MAP_FAILED || *alloc_area !=3D targe= t_addr) { + if (*alloc_area =3D=3D MAP_FAILED) { /* If fixed mapping failed, clean up and try anywhe= re */ - if (*alloc_area !=3D MAP_FAILED) - munmap(*alloc_area, bytes); - - /* Clean up the reservation if it's still around */ - munmap(addr_map.reservation, addr_map.size); + /* Explicitly munmap the reservation since our map = failed */ + if (reserved_area !=3D MAP_FAILED) + munmap(reserved_area, bytes); /* Fall back to letting the kernel choose an addres= s */ *alloc_area =3D mmap(NULL, bytes, PROT_READ | PROT_= WRITE, @@ -254,12 +219,12 @@ static int shmem_allocate_area(void **alloc_area, boo= l is_src) } /* Calculate a good spot for the alias mapping with space to preven= t merging */ - ret =3D find_suitable_area(&alias_map, bytes, hpage_size); - if (ret < 0) { + void *p_alias =3D (char *)((uintptr_t)*alloc_area + bytes + hpage_s= ize); + + /* Reserve space for alias mapping */ + reserved_alias =3D find_suitable_area(bytes); + if (reserved_alias =3D=3D MAP_FAILED) { /* Fallback to using an offset from the first mapping */ - void *p_alias =3D (char *)((uintptr_t)*alloc_area + bytes += hpage_size); - - /* No reservation, map directly */ area_alias =3D mmap(p_alias, bytes, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, mem_fd, offset); @@ -270,14 +235,14 @@ static int shmem_allocate_area(void **alloc_area, boo= l is_src) } } else { /* Use our reservation for the alias mapping */ - area_alias =3D mmap(alias_map.aligned_addr, bytes, PROT_REA= D | PROT_WRITE, + area_alias =3D mmap(reserved_alias, bytes, PROT_READ | PROT= _WRITE, MAP_SHARED | MAP_FIXED, mem_fd, offset); - /* Whether it succeeded or failed, we need to clean up the = reservation */ - if (area_alias !=3D alias_map.aligned_addr) - munmap(alias_map.reservation, alias_map.size); - + /* If mapping failed, try without specific address */ if (area_alias =3D=3D MAP_FAILED) { + /* Clean up the reservation since it didn't get rep= laced */ + munmap(reserved_alias, bytes); + /* If fixed mapping failed, try anywhere */ area_alias =3D mmap(NULL, bytes, PROT_READ | PROT_W= RITE, MAP_SHARED, mem_fd, offset); -- 2.43.0