From patchwork Wed Oct 23 16:24:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 838943 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 14B071CF299; Wed, 23 Oct 2024 16:25:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=205.220.165.32 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729700735; cv=fail; b=GkkKnlerjcseJYCtetKFFrr8TqHXz8lcrBOcXrDqlW6M41FGHj+AWPjp5vHXUrSwjsQ2/cb60QdjR0G+PT6aNWkVHzJlGMn30K3YZLhOn1PVtLUVP7HIXqs6YlgoSHWchyEmWlMM8RMB0T10yGNG/WvTMwfnsvh/qbGD8jfkjhg= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729700735; c=relaxed/simple; bh=Mmi4UeKbWikDYyMi8VghSLQVvjn+6yQRCp1LV6Xhqs4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=PN17ASPAD4uOqcFWLAt9YPz5X74+FQZqrlxr2PQY9lWlQ5RVUM/46lTgfkIBQH3dlylDfy6jMUtycszosVAkFjWjlq5iSrujDpy+9U7TttRWMPEBvjZlTRI3ffhwWdH3hDJ/ifMbaBY/EUfkE5s5MoIXDNFPlPss54mDzCnvrJg= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com; spf=pass smtp.mailfrom=oracle.com; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b=lMZc2EdF; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=t0xUX0/S; arc=fail smtp.client-ip=205.220.165.32 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oracle.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="lMZc2EdF"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="t0xUX0/S" Received: from pps.filterd (m0246617.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 49NFfcea025333; Wed, 23 Oct 2024 16:25:02 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=cc :content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s= corp-2023-11-20; bh=hFCM0e78+b+/G/MXToFjpQ50kDjpN2kBLlXHoOncL1g=; b= lMZc2EdF5VrT/GfHJax7Hi3pr+1xPROZ/tWuZJv1RwtT/nG1RJTV96Bc+RyogCbI mAoZsqGW1LI7hIWV1LBD7Q/6hUFX/56wsD5CdYm1ERaaj87zaCpUNRjRXpn9y8sC g6gaXItd0C3LyLc1OCiAjNVC468DL2qCvJLkHdkec7qlMChNJhuz0xKKNm+TsGHR pyE7Z8ygXEmX6UXimetwen0e3Y9Jxjj/zYQm6uHjnexxIHTgwgcT117aE6iuVi8t dHpqzMccCSEXcnEVyzKRowIegEF567rX5CS8ipj2afc5tqGBvEiM0E7x5PeWC7nF I1D/8o3+9/PTibAUDMvKTA== Received: from iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta01.appoci.oracle.com [130.35.100.223]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42c5asgj4p-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 23 Oct 2024 16:25:01 +0000 (GMT) Received: from pps.filterd (iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 49NG1Eni027453; Wed, 23 Oct 2024 16:25:00 GMT Received: from nam10-dm6-obe.outbound.protection.outlook.com (mail-dm6nam10lp2044.outbound.protection.outlook.com [104.47.58.44]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 42emh328xd-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 23 Oct 2024 16:25:00 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=tQoZoMsXzmxg4OJyNoJHAaDtF/jBRSEXDS3Y2nsPsbdOKq/kE4/bb5Mr+ck9i1qb964ihsazLnOTsy1Y6KgxoWyCBt32t9SqVYXmJTIIhyZEpwKe8RhXdmOJ/HX2hcuMKLYjsdUYREaULbcpC+WsL+CqKpX8Qsqr1v2ytROuMsBdFOhGm5EVEawTzIdBNOHxs48nwweAVDZVX4h0CiF1Sx/fIUV0Y/7098NiGK4uOdegCz5Vd4KXLSm4w42N/RL1fVe1vitqxlaARb4rkHSyKogdCSmoxQfPK/I6VIc6C3rvUyGl5JDXmATPt9tGh02HfUwbpmpZZ5jHTQg1oWssKQ== 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=hFCM0e78+b+/G/MXToFjpQ50kDjpN2kBLlXHoOncL1g=; b=m1oo5LIT/Qefib8zeO5eulRCPAifBLK5uzlacAR/7/pDM8Liv8jcJ1HN4Dgx0U/6viWA3Zm2X/srm6bfVgePRpPdoUWPeTDYJ8/xTEMtSG0u6gpS2GNWo6Yxp6pg4UYMuIguIRbp5ZqRp0ZS6aFcQGAiU/FyldJ3s0RBWe2qrAyWvJ0gtth2ktkYYXd48vwayp7fGxj7OH5YUlhQ9DO/GgdK1LubsFQn09KSuOMLDqtid5iVFMCvOQkmBPxirdtxLo9HmrFK6N6OJlphqyzwiT0dgWmcpYd7wxj4kTUXLv8jK2IeLSOKnwCgHSONopW+d+pdq4P2QR/KWA89l7jdBA== 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=hFCM0e78+b+/G/MXToFjpQ50kDjpN2kBLlXHoOncL1g=; b=t0xUX0/SJ3FEzwiXEoBhgN54Cb1Gy1mr0YdJ13f1V8mkggzYvx0aOilmaLEDLE9tYhmeGjjXsLMM54HhXOWyYBNasFQZbaLjQYIwHhT3OzRKjDvPyGlIAvnm/vraC4wffdPKAAY23WVAMguLAwfWOt6LN/LcZ0wpw4vUGzWQrh8= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by CH3PR10MB6836.namprd10.prod.outlook.com (2603:10b6:610:14f::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8093.16; Wed, 23 Oct 2024 16:24:56 +0000 Received: from BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9]) by BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9%6]) with mapi id 15.20.8069.024; Wed, 23 Oct 2024 16:24:56 +0000 From: Lorenzo Stoakes To: Andrew Morton Cc: Suren Baghdasaryan , "Liam R . Howlett" , Matthew Wilcox , Vlastimil Babka , "Paul E . McKenney" , Jann Horn , David Hildenbrand , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Muchun Song , Richard Henderson , Matt Turner , Thomas Bogendoerfer , "James E . J . Bottomley" , Helge Deller , Chris Zankel , Max Filippov , Arnd Bergmann , linux-alpha@vger.kernel.org, linux-mips@vger.kernel.org, linux-parisc@vger.kernel.org, linux-arch@vger.kernel.org, Shuah Khan , Christian Brauner , linux-kselftest@vger.kernel.org, Sidhartha Kumar , Jeff Xu , Christoph Hellwig , linux-api@vger.kernel.org, John Hubbard Subject: [PATCH v3 1/5] mm: pagewalk: add the ability to install PTEs Date: Wed, 23 Oct 2024 17:24:38 +0100 Message-ID: <9be732fd0e897453116b433fe2f468ef7795602e.1729699916.git.lorenzo.stoakes@oracle.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: References: X-ClientProxiedBy: LO2P265CA0384.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:a3::36) To BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR10MB3366:EE_|CH3PR10MB6836:EE_ X-MS-Office365-Filtering-Correlation-Id: 393368a7-2130-4048-c51d-08dcf37f3b08 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|10070799003|376014|7416014|1800799024|366016; X-Microsoft-Antispam-Message-Info: rWA8e+nnwvMJnTur8Y54LLOK62gKpX2Mh7J+ObxdvAmcZWNK9kxQurIpdZNCFoFqjvcpqPZ0fBX+3Mgtmv5sq1zIB/MHYEolNVknMUkhp1umIVnUdE+ZGDrGn0PVpGy4f16f0/vE5BspwbZCdzpPq2c9xPhH+VJohpB2fuaXCHrisJP1KtdN8MKsLmdl+s307gEelv+nVyKSt2OPlg9BHCvn7MOF4b/dd+cWcs+/u+Vf1c+wxqTsxJDbmlHDxrXbw9Bqa6n/mhkGa9utn1VE3XyT8sQ4TKjRxfVTvRWXfCPGyeQ/RXhTwr0GH2JdQf8Sa/hSHYWaO6FAda/HxqOV7GkLtU7uXOfoRxrQlFEQ/TT18bQ2Ix+Q9nPNUsRIRScIGU5R0oP1YWzvKcJ/ny62sQChKpK2ltksSiOzUWW75ZkTrop028YgfpKeFKbyOLT2d/dy5f81s5eCEANoXnRNp7jYoLacBzUkyQKbnxM2GnWXmKeeuvOK0C/CzcZ96SSoPYiHQMpXn2sB7+Gg3UeqhdFSc47pMXwCE5/K96f6GVVayMTpi1aTkv1NAnW0a/NjkClOrVX8J4vfPa65A5i6bKNUFI91LDOrE8TeeGpDK+iHt9DASkVkMooWiMHLc7PAdsooeM8pQWoisZhuJ6e/ifyIg8bclYthOsVMIiZMCj3P0kSVe8Gv9r75epZ8ty9ymVIKFedvnfVwFll9Z7sFM9ei+TzSHF/LseExQDZr1tPP1K7CWT5vSD9hHTKVTpkpPHzVwyeoYlgfoKaSCPhAlZYvIRoFn/a4vRCXTguXDiAW+BjxGNfWtuqdc0xsZtLOu3FWRtub29I4hhEfNElLmUsg3I2HF7I3bHqW+Puj+woJj/xM7bG0xEzmej/b3M+QMj8ZlhK+1KH6LM5xRFTWOsK8fk0642zIE79OMptmxl9KDwxNXLyO2MDaG1lb17ohts+dT9vn29jvDaM0WDefx61t5rTkEMsQZqyY2z2PcCcatldNboL5VAAeyAlWykBsbvT2+P1f1oBx/HfOrTgB3i/cJhUTnQ+hMp8lUoRp8U2Vzn8MmkV9SClV8JSe/9B2gyWaxTOXJdyJ/GX9IzEqM99VBq2cNlXsXAF5fvCLIdcnx/wZ5DXn7pB973Gf7zugC7ONszO8dv0GSslq298O+ijT0z+uDA7IMgcYPAnkYZQXdO58meCQzuOThIeesor1ZWpjUHNfmENTq7xx1sKDNivoBdg3y9b4NK03Kdsbxui4ecaKxj/Gfb3OcjXjvKaO+x0y6D39t31k69I6MPYAgcydmexkLN4Jp25Qz8Lh0HD3WAhYTm7E3MZ01x2a5QxE X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:BYAPR10MB3366.namprd10.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230040)(10070799003)(376014)(7416014)(1800799024)(366016); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: pVotCpcuM2ze6cNCVL3vT6lhTwbP98D5DAhFk8zoDCH6WRYvG+IXbauEQObCWaWqkL2QPAPJNnnR/1/ofIiidqm8Y8vHGu3H/+Ts3HXtArHYTAasLwhT+j28M5LIUEN0NnCY7oDadopuWN9lN8NT8eAg43F0GFTJCBP7cM20Y/AxvF0KjhzSgPjSin+k0kc4omFExxbShW2FW1TmnqmXbwZMCOTMX6GmPk2C5N4vf1EyyScnYjWmpTzaVy9sDGapPRtQKbQWzuEAp1h8ZuIQOfnH3l+WPDedLX7D22MTZymOtuqBYQ5OLaZQoEM3gbvK+XuvGVXZT5I9EDUD8croE+24/0e2Ymipg0ydwak34Nmvk8/lmUHEZ0r1sGAyML3XZ5/4xqOqwtOBeTAntl3pidy8q1uWazPkZwUgIEap4l4z58ACvPhdWr9NmZ9cBiH9tjvBOl4l9nNYlOfQaMJZjUGL8574a0vA0iDkhlnwmYctEa+iNITWSnARPBHNziyVhPbK7weFlKgalurB9qyN9EjJ5hIMiYiYjo+WoYHwPo+iT08Ix/CccJ4Yibc6USIvfJY1b09CHN/ocy7M/QcHdJbPZJkDOsWkwf+bpgyz8gGrYq24CzMNBnvCv1Eb5AwGfYv9YtBhez13i5nKUBev6OXx2bjx6ntFR1WTrXW/ry/NldpU7UX1EJnyEov7oCWSozN76y9/HfVo9/fjTmaBKMXZSx05gVQkfmG1vMVQgbB65vNHGBkwre020mdMyYb5sXGnw4mNxQkrfodD05sPnJZcVfmfyzBMjmNtUeR/BszVBwPUHcSQc6gcOl6LuVzf1SP4frUKZmXhzJFrJiVmVAJ8CLbJZq1oQ6eJAzMq0MeceN3969NCSSv6tw+go7qf/mgbCjboMUFhqDIZOSxitX4Cz9g4gNDZ5TH3Kg6A7mBJjIh0yfZFzzBizt2GdYlgae5hgyGHNgmC7Rs8lOJwWqFLam0i7jS/U50EiZwMbyNrTscChki91cL/3FzWS1PkT8kzx3qSbuk7up3KrPiDTi76n/AdF7JefTclilIUK4uCbv0JixTg0lO0lPbSv6XNtIKUu2t87QSiz6UI4LqbIkz2LAixB7SH8HdUUq9ZgzJmAB48EbnSWdbZODS8TSN3dcaNd2Ld9Zp3A8FT+ahUv6pPJuAxy5JPAJeT3Lm4z/svtb02gZMMA9vkudHmKmQ30BA8zYGnDEAsySdPEAsg5+t8OR+Au7Q55hX+/+d0aTkhONLbXaMp0vp5didHNQrLrXzH4ELtIJmiPQrJ+aEm8HNQIdj3mSXUCKEF8w8fRIqTO7XNPP7W0M1acfXQzmVBad1upQfOuZDR0WqzVg4amfsYjWLYGIdEkH6UR98KhiFSLS/45ogIrGLIZgROENRS+pZlMPFvsNS+Xha0dUL5MwZoeAIR3NxR8AVe9WGhH10bvRxVphV4vNnlvGNT8VoWzXGGhYdpnlaw8Dk00ZQgyxpJ1jmnXhGV5MWWULE3BRqdYVeHKJC7fpXCt4NiR9mdBtP0IS7OlA5uA+ncRVEyLSCQcJPJCvUUoYb83YuxZ0Y3ODww/DLIaS6HhiQiXA9SHuorrM9rvDb2dDCftJk7OAuJoZ+ptJtHQo7padG5RueMbD/vTzYzl7/91aIGTsoLrm3Gu/LisV3sq/t1b46MkQ== X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: VHXByoypFYz39JXeTwmTT49Oq6AnMNRXy1hFV4RyGhKAZHb+ZH1kxUeVa/tj+ScneWJt3uVw26e3x2FO11+WoCTdn2nlQ/SSL5GooVzsD4uKN1hCONg9vz9aoV6nsAgP+RRJb+wE7I3D5w12l0YAHFk6pKJ8XL5qyS0ES0Y+X08HFqS1BwNPuDEWq1APonQ4NorDfPnYE6FcRK9Kc3zKV+xSV1ECH3kwvxnob2lG279m9W7ni3SHDANXwFUom2H9SBz7F6WqIfzDjsiPgKw2o91dGa/xAxLHH0P+VSWveY6L56H9GsZQ9KIUdU/IINLDX8tPeXyoA2SH58qeKm9HAFVN5U/M5e+HpkNuymeD0N1YTmuaZyeE4YvQPaos8pC/5/Ag15zUD2T5ui4icmWwfAzFJKsl/qwPSAmaYD4Zxt/Ur8IeRiTf+0oc/o6M7JOcZCA8Wz+9Pt6rvZXaIxSsdK/jIXZ2DCPkJPpR1Wd9R9PIfTEHh1E30HsgwfxpsbaesB2gh6lyoGfq9EEVqoVnJu8dqt+wZhYiWBo66PXOrJK9ag9xRg3UiEfA9vENYfihPj9cnLFQiO1nyUATnQmHlEVW439qlu3iKzIH3MRpMY4= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 393368a7-2130-4048-c51d-08dcf37f3b08 X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB3366.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Oct 2024 16:24:56.5287 (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: vXJDUY3tewWyajAVpUS9ZYHBkERSFi2SURoTZw7J6zzQ9JyJeHi4812BDKGYnPZyv8O5kbi/KJ3Uw68DVV0dBko6++TKsxQFPH8JZrjT5cY= X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH3PR10MB6836 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1051,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-10-23_13,2024-10-23_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 phishscore=0 malwarescore=0 adultscore=0 mlxscore=0 mlxlogscore=999 spamscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2410230095 X-Proofpoint-GUID: nSb4NVsdwXyavRW_ddHmBWl7L7GTbHXn X-Proofpoint-ORIG-GUID: nSb4NVsdwXyavRW_ddHmBWl7L7GTbHXn The existing generic pagewalk logic permits the walking of page tables, invoking callbacks at individual page table levels via user-provided mm_walk_ops callbacks. This is useful for traversing existing page table entries, but precludes the ability to establish new ones. Existing mechanism for performing a walk which also installs page table entries if necessary are heavily duplicated throughout the kernel, each with semantic differences from one another and largely unavailable for use elsewhere. Rather than add yet another implementation, we extend the generic pagewalk logic to enable the installation of page table entries by adding a new install_pte() callback in mm_walk_ops. If this is specified, then upon encountering a missing page table entry, we allocate and install a new one and continue the traversal. If a THP huge page is encountered at either the PMD or PUD level we split it only if there are ops->pte_entry() (or ops->pmd_entry at PUD level), otherwise if there is only an ops->install_pte(), we avoid the unnecessary split. We do not support hugetlb at this stage. If this function returns an error, or an allocation fails during the operation, we abort the operation altogether. It is up to the caller to deal appropriately with partially populated page table ranges. If install_pte() is defined, the semantics of pte_entry() change - this callback is then only invoked if the entry already exists. This is a useful property, as it allows a caller to handle existing PTEs while installing new ones where necessary in the specified range. If install_pte() is not defined, then there is no functional difference to this patch, so all existing logic will work precisely as it did before. As we only permit the installation of PTEs where a mapping does not already exist there is no need for TLB management, however we do invoke update_mmu_cache() for architectures which require manual maintenance of mappings for other CPUs. We explicitly do not allow the existing page walk API to expose this feature as it is dangerous and intended for internal mm use only. Therefore we provide a new walk_page_range_mm() function exposed only to mm/internal.h. Reviewed-by: Jann Horn Signed-off-by: Lorenzo Stoakes Tested-by: Jarkko Sakkinen --- include/linux/pagewalk.h | 18 +++- mm/internal.h | 6 ++ mm/pagewalk.c | 227 +++++++++++++++++++++++++++------------ 3 files changed, 182 insertions(+), 69 deletions(-) diff --git a/include/linux/pagewalk.h b/include/linux/pagewalk.h index f5eb5a32aeed..9700a29f8afb 100644 --- a/include/linux/pagewalk.h +++ b/include/linux/pagewalk.h @@ -25,12 +25,15 @@ enum page_walk_lock { * this handler is required to be able to handle * pmd_trans_huge() pmds. They may simply choose to * split_huge_page() instead of handling it explicitly. - * @pte_entry: if set, called for each PTE (lowest-level) entry, - * including empty ones + * @pte_entry: if set, called for each PTE (lowest-level) entry + * including empty ones, except if @install_pte is set. + * If @install_pte is set, @pte_entry is called only for + * existing PTEs. * @pte_hole: if set, called for each hole at all levels, * depth is -1 if not known, 0:PGD, 1:P4D, 2:PUD, 3:PMD. * Any folded depths (where PTRS_PER_P?D is equal to 1) - * are skipped. + * are skipped. If @install_pte is specified, this will + * not trigger for any populated ranges. * @hugetlb_entry: if set, called for each hugetlb entry. This hook * function is called with the vma lock held, in order to * protect against a concurrent freeing of the pte_t* or @@ -51,6 +54,13 @@ enum page_walk_lock { * @pre_vma: if set, called before starting walk on a non-null vma. * @post_vma: if set, called after a walk on a non-null vma, provided * that @pre_vma and the vma walk succeeded. + * @install_pte: if set, missing page table entries are installed and + * thus all levels are always walked in the specified + * range. This callback is then invoked at the PTE level + * (having split any THP pages prior), providing the PTE to + * install. If allocations fail, the walk is aborted. This + * operation is only available for userland memory. Not + * usable for hugetlb ranges. * * p?d_entry callbacks are called even if those levels are folded on a * particular architecture/configuration. @@ -76,6 +86,8 @@ struct mm_walk_ops { int (*pre_vma)(unsigned long start, unsigned long end, struct mm_walk *walk); void (*post_vma)(struct mm_walk *walk); + int (*install_pte)(unsigned long addr, unsigned long next, + pte_t *ptep, struct mm_walk *walk); enum page_walk_lock walk_lock; }; diff --git a/mm/internal.h b/mm/internal.h index 508f7802dd2b..fb1fb0c984e4 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -1451,4 +1452,9 @@ static inline void accept_page(struct page *page) } #endif /* CONFIG_UNACCEPTED_MEMORY */ +/* pagewalk.c */ +int walk_page_range_mm(struct mm_struct *mm, unsigned long start, + unsigned long end, const struct mm_walk_ops *ops, + void *private); + #endif /* __MM_INTERNAL_H */ diff --git a/mm/pagewalk.c b/mm/pagewalk.c index 5f9f01532e67..f3cbad384344 100644 --- a/mm/pagewalk.c +++ b/mm/pagewalk.c @@ -3,9 +3,14 @@ #include #include #include +#include #include #include +#include + +#include "internal.h" + /* * We want to know the real level where a entry is located ignoring any * folding of levels which may be happening. For example if p4d is folded then @@ -29,9 +34,23 @@ static int walk_pte_range_inner(pte_t *pte, unsigned long addr, int err = 0; for (;;) { - err = ops->pte_entry(pte, addr, addr + PAGE_SIZE, walk); - if (err) - break; + if (ops->install_pte && pte_none(ptep_get(pte))) { + pte_t new_pte; + + err = ops->install_pte(addr, addr + PAGE_SIZE, &new_pte, + walk); + if (err) + break; + + set_pte_at(walk->mm, addr, pte, new_pte); + /* Non-present before, so for arches that need it. */ + if (!WARN_ON_ONCE(walk->no_vma)) + update_mmu_cache(walk->vma, addr, pte); + } else { + err = ops->pte_entry(pte, addr, addr + PAGE_SIZE, walk); + if (err) + break; + } if (addr >= end - PAGE_SIZE) break; addr += PAGE_SIZE; @@ -89,11 +108,14 @@ static int walk_pmd_range(pud_t *pud, unsigned long addr, unsigned long end, again: next = pmd_addr_end(addr, end); if (pmd_none(*pmd)) { - if (ops->pte_hole) + if (ops->install_pte) + err = __pte_alloc(walk->mm, pmd); + else if (ops->pte_hole) err = ops->pte_hole(addr, next, depth, walk); if (err) break; - continue; + if (!ops->install_pte) + continue; } walk->action = ACTION_SUBTREE; @@ -109,18 +131,19 @@ static int walk_pmd_range(pud_t *pud, unsigned long addr, unsigned long end, if (walk->action == ACTION_AGAIN) goto again; - - /* - * Check this here so we only break down trans_huge - * pages when we _need_ to - */ - if ((!walk->vma && (pmd_leaf(*pmd) || !pmd_present(*pmd))) || - walk->action == ACTION_CONTINUE || - !(ops->pte_entry)) + if (walk->action == ACTION_CONTINUE) continue; + if (!ops->install_pte && !ops->pte_entry) + continue; /* Nothing to do. */ + if (!ops->pte_entry && ops->install_pte && + pmd_present(*pmd) && + (pmd_trans_huge(*pmd) || pmd_devmap(*pmd))) + continue; /* Avoid unnecessary split. */ if (walk->vma) split_huge_pmd(walk->vma, pmd, addr); + else if (pmd_leaf(*pmd) || !pmd_present(*pmd)) + continue; /* Nothing to do. */ err = walk_pte_range(pmd, addr, next, walk); if (err) @@ -148,11 +171,14 @@ static int walk_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end, again: next = pud_addr_end(addr, end); if (pud_none(*pud)) { - if (ops->pte_hole) + if (ops->install_pte) + err = __pmd_alloc(walk->mm, pud, addr); + else if (ops->pte_hole) err = ops->pte_hole(addr, next, depth, walk); if (err) break; - continue; + if (!ops->install_pte) + continue; } walk->action = ACTION_SUBTREE; @@ -164,14 +190,20 @@ static int walk_pud_range(p4d_t *p4d, unsigned long addr, unsigned long end, if (walk->action == ACTION_AGAIN) goto again; - - if ((!walk->vma && (pud_leaf(*pud) || !pud_present(*pud))) || - walk->action == ACTION_CONTINUE || - !(ops->pmd_entry || ops->pte_entry)) + if (walk->action == ACTION_CONTINUE) continue; + if (!ops->install_pte && !ops->pte_entry && !ops->pmd_entry) + continue; /* Nothing to do. */ + if (!ops->pmd_entry && !ops->pte_entry && ops->install_pte && + pud_present(*pud) && + (pud_trans_huge(*pud) || pud_devmap(*pud))) + continue; /* Avoid unnecessary split. */ if (walk->vma) split_huge_pud(walk->vma, pud, addr); + else if (pud_leaf(*pud) || !pud_present(*pud)) + continue; /* Nothing to do. */ + if (pud_none(*pud)) goto again; @@ -196,18 +228,22 @@ static int walk_p4d_range(pgd_t *pgd, unsigned long addr, unsigned long end, do { next = p4d_addr_end(addr, end); if (p4d_none_or_clear_bad(p4d)) { - if (ops->pte_hole) + if (ops->install_pte) + err = __pud_alloc(walk->mm, p4d, addr); + else if (ops->pte_hole) err = ops->pte_hole(addr, next, depth, walk); if (err) break; - continue; + if (!ops->install_pte) + continue; } if (ops->p4d_entry) { err = ops->p4d_entry(p4d, addr, next, walk); if (err) break; } - if (ops->pud_entry || ops->pmd_entry || ops->pte_entry) + if (ops->pud_entry || ops->pmd_entry || ops->pte_entry || + ops->install_pte) err = walk_pud_range(p4d, addr, next, walk); if (err) break; @@ -231,18 +267,22 @@ static int walk_pgd_range(unsigned long addr, unsigned long end, do { next = pgd_addr_end(addr, end); if (pgd_none_or_clear_bad(pgd)) { - if (ops->pte_hole) + if (ops->install_pte) + err = __p4d_alloc(walk->mm, pgd, addr); + else if (ops->pte_hole) err = ops->pte_hole(addr, next, 0, walk); if (err) break; - continue; + if (!ops->install_pte) + continue; } if (ops->pgd_entry) { err = ops->pgd_entry(pgd, addr, next, walk); if (err) break; } - if (ops->p4d_entry || ops->pud_entry || ops->pmd_entry || ops->pte_entry) + if (ops->p4d_entry || ops->pud_entry || ops->pmd_entry || + ops->pte_entry || ops->install_pte) err = walk_p4d_range(pgd, addr, next, walk); if (err) break; @@ -334,6 +374,11 @@ static int __walk_page_range(unsigned long start, unsigned long end, int err = 0; struct vm_area_struct *vma = walk->vma; const struct mm_walk_ops *ops = walk->ops; + bool is_hugetlb = is_vm_hugetlb_page(vma); + + /* We do not support hugetlb PTE installation. */ + if (ops->install_pte && is_hugetlb) + return -EINVAL; if (ops->pre_vma) { err = ops->pre_vma(start, end, walk); @@ -341,7 +386,7 @@ static int __walk_page_range(unsigned long start, unsigned long end, return err; } - if (is_vm_hugetlb_page(vma)) { + if (is_hugetlb) { if (ops->hugetlb_entry) err = walk_hugetlb_range(start, end, walk); } else @@ -380,47 +425,14 @@ static inline void process_vma_walk_lock(struct vm_area_struct *vma, #endif } -/** - * walk_page_range - walk page table with caller specific callbacks - * @mm: mm_struct representing the target process of page table walk - * @start: start address of the virtual address range - * @end: end address of the virtual address range - * @ops: operation to call during the walk - * @private: private data for callbacks' usage - * - * Recursively walk the page table tree of the process represented by @mm - * within the virtual address range [@start, @end). During walking, we can do - * some caller-specific works for each entry, by setting up pmd_entry(), - * pte_entry(), and/or hugetlb_entry(). If you don't set up for some of these - * callbacks, the associated entries/pages are just ignored. - * The return values of these callbacks are commonly defined like below: - * - * - 0 : succeeded to handle the current entry, and if you don't reach the - * end address yet, continue to walk. - * - >0 : succeeded to handle the current entry, and return to the caller - * with caller specific value. - * - <0 : failed to handle the current entry, and return to the caller - * with error code. - * - * Before starting to walk page table, some callers want to check whether - * they really want to walk over the current vma, typically by checking - * its vm_flags. walk_page_test() and @ops->test_walk() are used for this - * purpose. - * - * If operations need to be staged before and committed after a vma is walked, - * there are two callbacks, pre_vma() and post_vma(). Note that post_vma(), - * since it is intended to handle commit-type operations, can't return any - * errors. - * - * struct mm_walk keeps current values of some common data like vma and pmd, - * which are useful for the access from callbacks. If you want to pass some - * caller-specific data to callbacks, @private should be helpful. +/* + * See the comment for walk_page_range(), this performs the heavy lifting of the + * operation, only sets no restrictions on how the walk proceeds. * - * Locking: - * Callers of walk_page_range() and walk_page_vma() should hold @mm->mmap_lock, - * because these function traverse vma list and/or access to vma's data. + * We usually restrict the ability to install PTEs, but this functionality is + * available to internal memory management code and provided in mm/internal.h. */ -int walk_page_range(struct mm_struct *mm, unsigned long start, +int walk_page_range_mm(struct mm_struct *mm, unsigned long start, unsigned long end, const struct mm_walk_ops *ops, void *private) { @@ -479,6 +491,80 @@ int walk_page_range(struct mm_struct *mm, unsigned long start, return err; } +/* + * Determine if the walk operations specified are permitted to be used for a + * page table walk. + * + * This check is performed on all functions which are parameterised by walk + * operations and exposed in include/linux/pagewalk.h. + * + * Internal memory management code can use the walk_page_range_mm() function to + * be able to use all page walking operations. + */ +static bool check_ops_valid(const struct mm_walk_ops *ops) +{ + /* + * The installation of PTEs is solely under the control of memory + * management logic and subject to many subtle locking, security and + * cache considerations so we cannot permit other users to do so, and + * certainly not for exported symbols. + */ + if (ops->install_pte) + return false; + + return true; +} + +/** + * walk_page_range - walk page table with caller specific callbacks + * @mm: mm_struct representing the target process of page table walk + * @start: start address of the virtual address range + * @end: end address of the virtual address range + * @ops: operation to call during the walk + * @private: private data for callbacks' usage + * + * Recursively walk the page table tree of the process represented by @mm + * within the virtual address range [@start, @end). During walking, we can do + * some caller-specific works for each entry, by setting up pmd_entry(), + * pte_entry(), and/or hugetlb_entry(). If you don't set up for some of these + * callbacks, the associated entries/pages are just ignored. + * The return values of these callbacks are commonly defined like below: + * + * - 0 : succeeded to handle the current entry, and if you don't reach the + * end address yet, continue to walk. + * - >0 : succeeded to handle the current entry, and return to the caller + * with caller specific value. + * - <0 : failed to handle the current entry, and return to the caller + * with error code. + * + * Before starting to walk page table, some callers want to check whether + * they really want to walk over the current vma, typically by checking + * its vm_flags. walk_page_test() and @ops->test_walk() are used for this + * purpose. + * + * If operations need to be staged before and committed after a vma is walked, + * there are two callbacks, pre_vma() and post_vma(). Note that post_vma(), + * since it is intended to handle commit-type operations, can't return any + * errors. + * + * struct mm_walk keeps current values of some common data like vma and pmd, + * which are useful for the access from callbacks. If you want to pass some + * caller-specific data to callbacks, @private should be helpful. + * + * Locking: + * Callers of walk_page_range() and walk_page_vma() should hold @mm->mmap_lock, + * because these function traverse vma list and/or access to vma's data. + */ +int walk_page_range(struct mm_struct *mm, unsigned long start, + unsigned long end, const struct mm_walk_ops *ops, + void *private) +{ + if (!check_ops_valid(ops)) + return -EINVAL; + + return walk_page_range_mm(mm, start, end, ops, private); +} + /** * walk_page_range_novma - walk a range of pagetables not backed by a vma * @mm: mm_struct representing the target process of page table walk @@ -494,7 +580,7 @@ int walk_page_range(struct mm_struct *mm, unsigned long start, * walking the kernel pages tables or page tables for firmware. * * Note: Be careful to walk the kernel pages tables, the caller may be need to - * take other effective approache (mmap lock may be insufficient) to prevent + * take other effective approaches (mmap lock may be insufficient) to prevent * the intermediate kernel page tables belonging to the specified address range * from being freed (e.g. memory hot-remove). */ @@ -513,6 +599,8 @@ int walk_page_range_novma(struct mm_struct *mm, unsigned long start, if (start >= end || !walk.mm) return -EINVAL; + if (!check_ops_valid(ops)) + return -EINVAL; /* * 1) For walking the user virtual address space: @@ -556,6 +644,8 @@ int walk_page_range_vma(struct vm_area_struct *vma, unsigned long start, return -EINVAL; if (start < vma->vm_start || end > vma->vm_end) return -EINVAL; + if (!check_ops_valid(ops)) + return -EINVAL; process_mm_walk_lock(walk.mm, ops->walk_lock); process_vma_walk_lock(vma, ops->walk_lock); @@ -574,6 +664,8 @@ int walk_page_vma(struct vm_area_struct *vma, const struct mm_walk_ops *ops, if (!walk.mm) return -EINVAL; + if (!check_ops_valid(ops)) + return -EINVAL; process_mm_walk_lock(walk.mm, ops->walk_lock); process_vma_walk_lock(vma, ops->walk_lock); @@ -623,6 +715,9 @@ int walk_page_mapping(struct address_space *mapping, pgoff_t first_index, unsigned long start_addr, end_addr; int err = 0; + if (!check_ops_valid(ops)) + return -EINVAL; + lockdep_assert_held(&mapping->i_mmap_rwsem); vma_interval_tree_foreach(vma, &mapping->i_mmap, first_index, first_index + nr - 1) { From patchwork Wed Oct 23 16:24:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 837922 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 54BD81CEEBD; Wed, 23 Oct 2024 16:25:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=205.220.165.32 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729700736; cv=fail; b=HrYr5rhJTNBoZYLUQnK47B40tiDXwv7URSkWKr5X7VRTIF7FtFYHw3Epq8XbqMYovAuhZR2gtnmod+UDnAPNlvG96nAfRiLllqIV54dBoJFI3bdoq1wKgRVeCMuPUEgEM5clGKyPoV14xmyUbWNErL7gubLx4RGo8InHk6i73eM= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729700736; c=relaxed/simple; bh=dzW7dqkkFK9SqZeQ/MXLjAOKLEhiNtQVc3EBs65U1B8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=jKy7eNBYjl6h/qctJR/yxZlm+dkfN7ifh75urK5sRNuvRTMb4XI4HZLqQSM7dFd3AFFiWhMLBxSXohh9DipcKYGfiCRT0jRIEbF9rktViIbSEA18jyEbSTseV+CNQ1vXKlMaqaEnCCQV6S1wHjlioUDLraGByPC8FjRU3cEwRLw= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com; spf=pass smtp.mailfrom=oracle.com; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b=n/YigyHe; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=il+Yr2wK; arc=fail smtp.client-ip=205.220.165.32 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oracle.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="n/YigyHe"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="il+Yr2wK" Received: from pps.filterd (m0333521.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 49NFfdNc012074; Wed, 23 Oct 2024 16:25:05 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=cc :content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s= corp-2023-11-20; bh=vgEdRzLyuHzc2anBjPO5zTNYgSoqG4gc63FTV4U/xUA=; b= n/YigyHeHBmU0VBwkUHxF61nZrAa5YKBaJCjyY9FxypkN1DplIpDT8Lu5Tpvrkzj c6fC32RbyvhTL8H+YEpGmtyt3dn+5qE4NXzi37Kt0K1lzH0J1F6zNprCnrcQ5oKY HRMKVZPLJfzg4581SSzjfyd07CNQmBnujCCV0ht2jZKfXjUPorBJIcJwnBquDB4w AfdV0BtRzBKWgn364sj7tIbSyoD9Xhhf/zZaayYL81YI18/U3lHUiaY8g8wy61hR wIa0rLDaGYYQZhyCT5akaD1tIb0ksOxaI3hQZsRjB5Ih6DHyBRDETF0SezB/U99I dCXHbuL4SpogNZTnVl4qzw== Received: from iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta01.appoci.oracle.com [130.35.100.223]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42c53urht2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 23 Oct 2024 16:25:05 +0000 (GMT) Received: from pps.filterd (iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 49NFc3C7027408; Wed, 23 Oct 2024 16:25:04 GMT Received: from nam10-dm6-obe.outbound.protection.outlook.com (mail-dm6nam10lp2046.outbound.protection.outlook.com [104.47.58.46]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 42emh3290n-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 23 Oct 2024 16:25:03 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=wL4T7bcg7vvyYohT0I3q2j3qOV1sEqjt8KIICAYnmxqovP+iy4e1unetTPmyKCFxAxbqnBkkQtN9s9NLGsDomUbkDyBwMdxG3dzcuZn6e8/pfQfbD70MBej1zjnVQsmMYRCFJ5TaYH6QeRRHeilQIb9B84LGRx7OELFTjkMTxGi/O2nxcLV/jd/eacNCWbzXloabT51EVoWBZ1UjTd9smj6bzDYvfnw0Ms1HfsTnTX4+IYJw4AzruPOC063CRWvT+RVKM42DBTP+/CRe3SdSWgssEi+xKEdxhhNaBEqIAOE+GQrCTiDhY8RZL6rYbb/O+EbawoosMXrZUpzLRkaGSA== 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=vgEdRzLyuHzc2anBjPO5zTNYgSoqG4gc63FTV4U/xUA=; b=PRCj+QnLwufyIjD/WV3tzq37rdlSZY6NEKKQg8yWUt2eqIB9IUvnPx5BwJZTX2COABoIfrXwGTENJb116SevEOHW2B9PC4ecq+PffhFwAmRE7E5JVrQrROUfAc1OL199Dk5asE4OHcw/BpQ3C7rS1VV/qCIP5u1+fB2q+L7+l/dqTjvKDuHvvwBzfTuCwclO3YvEjTdoG5PvUnofW7wxxSc1EiVr8NhAmXb+foGWXjydlszuuOCP0AbDQ8C1060oJd1QXrZSJrT4vDl3mH2VkHM+DjizgPf7VHFB+0MtZI1gcULVraz645/SQJg/YrESmLNj3DwLx/9tzOcruEHHcA== 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=vgEdRzLyuHzc2anBjPO5zTNYgSoqG4gc63FTV4U/xUA=; b=il+Yr2wKdX3gnLiD6jmSWWK7vZTjCRqYmpyfOawlCnfyKVp3VeypB9ZpMU7W3aXqS1SMkaG5YGItC7R7k/KxpVupVw7cklVt7UQt3hPVgq3dEBh6kaFxABJLCryokd0qU95zBxVAvd+Zw4xSgY2KIsRdmRVmxhQmxVEHohT8zKk= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by CH3PR10MB6836.namprd10.prod.outlook.com (2603:10b6:610:14f::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8093.16; Wed, 23 Oct 2024 16:25:00 +0000 Received: from BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9]) by BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9%6]) with mapi id 15.20.8069.024; Wed, 23 Oct 2024 16:25:00 +0000 From: Lorenzo Stoakes To: Andrew Morton Cc: Suren Baghdasaryan , "Liam R . Howlett" , Matthew Wilcox , Vlastimil Babka , "Paul E . McKenney" , Jann Horn , David Hildenbrand , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Muchun Song , Richard Henderson , Matt Turner , Thomas Bogendoerfer , "James E . J . Bottomley" , Helge Deller , Chris Zankel , Max Filippov , Arnd Bergmann , linux-alpha@vger.kernel.org, linux-mips@vger.kernel.org, linux-parisc@vger.kernel.org, linux-arch@vger.kernel.org, Shuah Khan , Christian Brauner , linux-kselftest@vger.kernel.org, Sidhartha Kumar , Jeff Xu , Christoph Hellwig , linux-api@vger.kernel.org, John Hubbard Subject: [PATCH v3 2/5] mm: add PTE_MARKER_GUARD PTE marker Date: Wed, 23 Oct 2024 17:24:39 +0100 Message-ID: <7f81287792eef318a7a711d358c00e2e48216f15.1729699916.git.lorenzo.stoakes@oracle.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: References: X-ClientProxiedBy: LO2P265CA0070.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:60::34) To BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR10MB3366:EE_|CH3PR10MB6836:EE_ X-MS-Office365-Filtering-Correlation-Id: 465955cd-aa31-4d31-2167-08dcf37f3dd2 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|10070799003|376014|7416014|1800799024|366016; X-Microsoft-Antispam-Message-Info: /mq3qnNUhDJN5w2OjdtaVQWFJYr6bOdnB9gihGjSm69eQCEAAGWEwTOrEGnioJnYWKB4MKhohSOKeVqoGR2HpLWAgthR6uZp9YTL+Fc2z+MjCcwjjyNEdcOsIY4H5UShdD/iNLXLejDGr0ukFuvgm8xCh7SpgYiyFB+/1Uxmab7MQF5QP7/9/wV+iEs+0FnAUKLotwBpX0IfjWKrBxVUV0hDPVLuuzpogvoSDhMp8X5xbTXMh9TyMxmnq3ZrldIuVvlndW4QTiOQwEc8CQqCC9mGYLMPsOoQPNXYAnVAIY+5MiVux8ejUtKWNEDOwu54Ho6/cTlw/ZIjhS1UdcD83m96ikWR2fo3zYaUTZo78w0lHvnQ8GMDkue7GxqUpV0iUFYol9QIurNa/A14vDWvqSY8vuSKkZCM04SOoT8wT2fdo2OBoE/TMinWCNce/WospcSb6i7WE9vA/m7YVmxf9wtFXN1Q+C/uPTfuWjnAJj23OoittMoTLDDlCEIAeeRL90gSWEYtMpD0SVjlqxY0ssoFu+Rpw75kBDiuPsNHGUTxA+cfYGe3HRdC8xFRPlcmGAn1yENjqQyQNQdNteLpBNd8Rdcrfv/it28sr4NQAFGiL3NdCis95dfcF7PuPN5aVv2MMnRPcDoIok7fQmd6oEy39ZtG0pVLl53lM1TMyeViZxpjCf0jADKUcnmY+TkLUUhXW6EVNWqMW465mGRzX91VEuMCvcfk+nu2MpGZCVaztGeC3xwxCutXBNPqbdI9Texnw0giaJkCCD2+GPCVBVJE3X8v/uV+H5STP4KjKLwTwvFWcKjj+AGQMt39nTzRBhX5xFoY5JDePUdkBSOUWmBp4CIbCUpYkvi64qc6LyR/W9a8aWPs2xIJ2silrBqut7TcZRt6g83qcYez1P2EyGwAt+ZFbDd/0x+JwPWHhS6pFrNBN8/olrlQazTHmXxeTz7HhSt9LZq1eWp6/uRbKomSxtyicdLUiqea9SvWoPcEmpCubukdXSpXTegqS4aUS/myUJYMVgxx+oQ0BP/LB3MpTsgtpH4qVsDekJMqBEIOBarULrS1542c2nlcox6GoC1osnZBgAnkNJHPMs0l3KUnfsliBZRQm9T5eSkR2TuCCNG/soUKCKaRxJdYAA6ogwgNcZl5uTG55SRGAP1CHAoagx8dTVrQH4NtOye6/a+FKz9GasqPRxOy6OlfV/eFK8SN+BqSIN/vZO86OKR3LjbeoQJ1lwZ9gRkxiFZl/pQeIfyaKXdakSQS0AIxSnq4Ht65wKGPj4vx2xMgP2Ng2wKMkZQA+aL0L/+zrsbnOtU= X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:BYAPR10MB3366.namprd10.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230040)(10070799003)(376014)(7416014)(1800799024)(366016); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 9yJzjUE+p/vbsLN0GRGeYY5NaTBzXUhnk1VZt6Q4xtgB/UTljJO0XHXrr4OXH18d6IKWQphxccFX1cJUMgfdF/os+vixwNOofIwMdcOyXv5AC3DVjDevSZpZS+ygjhLXQCaAKdDfEjWND+/ZzFatAR7IUwOLDNpbomVxRihCQCvsbPTwTHkASKh/3pMKJakV15N5H1KMqI7adR21LUc9f33xO6MKgs6z8MWOVTnlhs6iNkZGy06wTCF5lXWWu3RZNmApQEn16S0L4mz+ZFppUtcQw8UDX4jeYNVJRWkCRsHJ7WcM4N7WTISnT+MAJH646PSb+wJ8aGwfZGAapiJgv7n4yHw6tlwXYd1i3foD0x/AM8tDO40bMzbphg3NR85/pqmURW6Z7h8ost/EShhUKeUWPsvCyiMPKBlJX+XOUVfZLV5TUF62izffR6wwTfTnYN49lr7POMYza4RUaISGcgU2PoYPJV8oZhL+1tvuP/oAsV3jV0ND+bt/GpW1ZyhuRS3C3BKPc/YFkVtTykYfHh5GS3P/LbVxMDFFqa72IdoOBDFXBZ+iCVVkEL7+mMHfuR+l/WuLaPaaYlP7pmBmNccRLrx2EtFCAFVdN14ojBx6lW+73QqmklNQqFJT0zmOEdZDO/7KCCJ1NlnftYIs0+dsxhIBQFeplJvVbcgEZmWiIfvmDPk+NKA7OzD2kIW6aMkW5MCjMz+9WaapCSe9T+GTUAkwwOEX2TWkU4EfRgXz42zQuqVMqaEGfj4AtHYOQAt34Yrh04+CT894BSudogeIjcw0uF3/d+vbNdmrFdZUI9Q0/ByefEgb9wSFhdpsIuBSamWgHaVsUmPXyCibcz3xt9cZjXVpJ89j829qmPbZQ8S7RJxoVNwPz2Rwii1fZK/Qd5KPjhrz2O9rRj+81EQxC1ZGnqx2fLfXUqddU9m9+Ke8HyN4gB99VlCt2JTwxjNwLNyzXpt31AuSB9aCl5Nz/ZWaWp+a8WBko/L/+7TsNO+TTYDcKSJsYtty817jCd1VUZ9+FAthJzRp0KjJYh4W9+fBZIjugCeIP1rZGkCQ4HohchGGPOHVhWYjFcDJzE+FO0RJT/96Bc4IjM4d/Xp/jWATdLGxObkTuBo67djEGHyBifbIt3OPC3U9Srb6Se/x8SWQuCV96JeFDCa7r5juYktCvZDefMR5VrBM2I1p4lIYg5Mx6EREYWmBF3erZVrtHAFJA0i5zOdom4o5f9BAMtldYbAm3rlGxu08NN0zto8HmgsBKsbxBH6jg+bgGCbm+AhlmbiizGTEXWacS4a183VYfipHBeSrRT6M6r9Iwie+NtFNPM21YL3A38cKVl5sq7WKeqll1pm1blBtWlxbbfYJXfgclwmJk7S1pbc7KEUNbgU79zDqvCZumyR12+wiYh1XsMT2ZdISSYYinWBccBFumUoAzkxxwiGutmZVuT0OaTHhzhjptL/6qnXFe9juNLR3cOpnuQzysGmaAaoS/0SCTxbx1XMsj8gSCjQizocjcjP3wu8UggIq05pqTQvTyqs3RTyDHaOEDfOUwooULkWPp6z5Q+9Lq0n+Te4FveMTgk88d+QbsQAICDVWOYW2hOjxGAzNitad+uUEd/O+oHMP+k2ZfYhIaL135DOrb19LRkygqAcm7IpxXMH14AXIlvZDE5rZenVL1D5JZg== X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: PjLZczK25Y7/h/Ef4ltingMLZ8Z7WIdYHRxAsQI6MhCHtMDNGHmUClHJ00EoxZCjNIqz/GrmA+bEJLgo4xlHeZo/pgthLM2nRhUgZ02bArmYNcIWEPYrzuAtiooH6w3FmGJ0Tgjyi8eM3Jqi8oZG5o1jWK+Bx4/jX7E40cy9R/KuCiLeWeGLnS4RmhVeyP3ndt/tDPcWx9chDkfuIDQN2zkwUwTprMfZkn5fXUxWdrdZmJ+ox0EdHkUkEZLh5ePlixD3UgnplxUnvsHW+Y0J540fiz87HRVPO4x9XlHLJEDL7UniRVW/k/1iyBcKDQuSLAYe+Q6GfPB52LmbW3tOBbc2x6luTq5pdfxG+rYk+M2g/VYxz53d31rlCUu+5wtM/xEtZTBR4CGCqCyK+E/o4uPV4tE23KN8//TSxV0Xte9QPyeCTrpR/BBeBSWtUZXDN2eA17UzTEVvNGFWyBHel0RgStoLAEY6cwSwXo5dSk1leCPCTOVJqZTSckIn7PGic3TYT18ZtIwdJQeY3yY3jTp7T4PlIGSNrnTzASizgc7obgWDfGupH0ls2JosS4DE/nNIue7/E0mA+ZOiOMlOia8Bk8Kb2sEpDgiKlHGILQs= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 465955cd-aa31-4d31-2167-08dcf37f3dd2 X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB3366.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Oct 2024 16:25:00.7223 (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: KkLlTFvSNpT8HvhgISLYs3a2YAu+JAD0dRTWH1XuxwaG4ih3sKM0b2oSWfDSm/NnSK3hEjirM+TRGXxIOoFlpy3/Q4UnanEEFImvHKsUZIs= X-MS-Exchange-Transport-CrossTenantHeadersStamped: CH3PR10MB6836 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1051,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-10-23_13,2024-10-23_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 phishscore=0 malwarescore=0 adultscore=0 mlxscore=0 mlxlogscore=999 spamscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2410230095 X-Proofpoint-GUID: Qo3N_ypuxQvQZShNx9GUNmFG4Foq13m5 X-Proofpoint-ORIG-GUID: Qo3N_ypuxQvQZShNx9GUNmFG4Foq13m5 Add a new PTE marker that results in any access causing the accessing process to segfault. This is preferable to PTE_MARKER_POISONED, which results in the same handling as hardware poisoned memory, and is thus undesirable for cases where we simply wish to 'soft' poison a range. This is in preparation for implementing the ability to specify guard pages at the page table level, i.e. ranges that, when accessed, should cause process termination. Additionally, rename zap_drop_file_uffd_wp() to zap_drop_markers() - the function checks the ZAP_FLAG_DROP_MARKER flag so naming it for this single purpose was simply incorrect. We then reuse the same logic to determine whether a zap should clear a guard entry - this should only be performed on teardown and never on MADV_DONTNEED or MADV_FREE. We additionally add a WARN_ON_ONCE() in hugetlb logic should a guard marker be encountered there, as we explicitly do not support this operation and this should not occur. Acked-by: Vlastimil Babka Signed-off-by: Lorenzo Stoakes Acked-by: Jarkko Sakkinen --- include/linux/mm_inline.h | 2 +- include/linux/swapops.h | 24 +++++++++++++++++++++++- mm/hugetlb.c | 4 ++++ mm/memory.c | 18 +++++++++++++++--- mm/mprotect.c | 6 ++++-- 5 files changed, 47 insertions(+), 7 deletions(-) diff --git a/include/linux/mm_inline.h b/include/linux/mm_inline.h index 355cf46a01a6..1b6a917fffa4 100644 --- a/include/linux/mm_inline.h +++ b/include/linux/mm_inline.h @@ -544,7 +544,7 @@ static inline pte_marker copy_pte_marker( { pte_marker srcm = pte_marker_get(entry); /* Always copy error entries. */ - pte_marker dstm = srcm & PTE_MARKER_POISONED; + pte_marker dstm = srcm & (PTE_MARKER_POISONED | PTE_MARKER_GUARD); /* Only copy PTE markers if UFFD register matches. */ if ((srcm & PTE_MARKER_UFFD_WP) && userfaultfd_wp(dst_vma)) diff --git a/include/linux/swapops.h b/include/linux/swapops.h index cb468e418ea1..96f26e29fefe 100644 --- a/include/linux/swapops.h +++ b/include/linux/swapops.h @@ -426,9 +426,19 @@ typedef unsigned long pte_marker; * "Poisoned" here is meant in the very general sense of "future accesses are * invalid", instead of referring very specifically to hardware memory errors. * This marker is meant to represent any of various different causes of this. + * + * Note that, when encountered by the faulting logic, PTEs with this marker will + * result in VM_FAULT_HWPOISON and thus regardless trigger hardware memory error + * logic. */ #define PTE_MARKER_POISONED BIT(1) -#define PTE_MARKER_MASK (BIT(2) - 1) +/* + * Indicates that, on fault, this PTE will case a SIGSEGV signal to be + * sent. This means guard markers behave in effect as if the region were mapped + * PROT_NONE, rather than if they were a memory hole or equivalent. + */ +#define PTE_MARKER_GUARD BIT(2) +#define PTE_MARKER_MASK (BIT(3) - 1) static inline swp_entry_t make_pte_marker_entry(pte_marker marker) { @@ -464,6 +474,18 @@ static inline int is_poisoned_swp_entry(swp_entry_t entry) { return is_pte_marker_entry(entry) && (pte_marker_get(entry) & PTE_MARKER_POISONED); + +} + +static inline swp_entry_t make_guard_swp_entry(void) +{ + return make_pte_marker_entry(PTE_MARKER_GUARD); +} + +static inline int is_guard_swp_entry(swp_entry_t entry) +{ + return is_pte_marker_entry(entry) && + (pte_marker_get(entry) & PTE_MARKER_GUARD); } /* diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 906294ac85dc..2c8c5da0f5d3 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6353,6 +6353,10 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struct vm_area_struct *vma, ret = VM_FAULT_HWPOISON_LARGE | VM_FAULT_SET_HINDEX(hstate_index(h)); goto out_mutex; + } else if (WARN_ON_ONCE(marker & PTE_MARKER_GUARD)) { + /* This isn't supported in hugetlb. */ + ret = VM_FAULT_SIGSEGV; + goto out_mutex; } } diff --git a/mm/memory.c b/mm/memory.c index 0f614523b9f4..551455cd453f 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -1455,7 +1455,7 @@ static inline bool should_zap_folio(struct zap_details *details, return !folio_test_anon(folio); } -static inline bool zap_drop_file_uffd_wp(struct zap_details *details) +static inline bool zap_drop_markers(struct zap_details *details) { if (!details) return false; @@ -1476,7 +1476,7 @@ zap_install_uffd_wp_if_needed(struct vm_area_struct *vma, if (vma_is_anonymous(vma)) return; - if (zap_drop_file_uffd_wp(details)) + if (zap_drop_markers(details)) return; for (;;) { @@ -1671,7 +1671,15 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb, * drop the marker if explicitly requested. */ if (!vma_is_anonymous(vma) && - !zap_drop_file_uffd_wp(details)) + !zap_drop_markers(details)) + continue; + } else if (is_guard_swp_entry(entry)) { + /* + * Ordinary zapping should not remove guard PTE + * markers. Only do so if we should remove PTE markers + * in general. + */ + if (!zap_drop_markers(details)) continue; } else if (is_hwpoison_entry(entry) || is_poisoned_swp_entry(entry)) { @@ -4003,6 +4011,10 @@ static vm_fault_t handle_pte_marker(struct vm_fault *vmf) if (marker & PTE_MARKER_POISONED) return VM_FAULT_HWPOISON; + /* Hitting a guard page is always a fatal condition. */ + if (marker & PTE_MARKER_GUARD) + return VM_FAULT_SIGSEGV; + if (pte_marker_entry_uffd_wp(entry)) return pte_marker_handle_uffd_wp(vmf); diff --git a/mm/mprotect.c b/mm/mprotect.c index 0c5d6d06107d..1f671b0667bd 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -236,9 +236,11 @@ static long change_pte_range(struct mmu_gather *tlb, } else if (is_pte_marker_entry(entry)) { /* * Ignore error swap entries unconditionally, - * because any access should sigbus anyway. + * because any access should sigbus/sigsegv + * anyway. */ - if (is_poisoned_swp_entry(entry)) + if (is_poisoned_swp_entry(entry) || + is_guard_swp_entry(entry)) continue; /* * If this is uffd-wp pte marker and we'd like From patchwork Wed Oct 23 16:24:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 837921 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 94B971D042A; Wed, 23 Oct 2024 16:25:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=205.220.165.32 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729700752; cv=fail; b=sRi1IUfGpnfd2SbDVfhs4mwba3dNBM/f775OHHZB2+Qw1e/+cmkuVuanRpJ77ZA23PEh9dpXap4fbfb1I9ogbUb7kJoDxQuPvLhjSn2dn3hE/xDueaJLPjlwGrGhgSCU+RqsxD69IphtLGPIIiUb4K0G3hQFN38WlXpk/J7fBe0= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729700752; c=relaxed/simple; bh=0eJkbvUjfwgWwdrcQqvoEYnEkbWIIw9HCMeEXMNz754=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=CmHItXkJJkzdf+Ocy7nYpxxdkXhklxJadQJhDg7vKWkL+dnojtkE04CPqGY6KQ7n/toGzRaWZmyahcvALunqMsFOaeC3xZvY9VtqiCnCO5veDY4cDY7fxEIxv6j5Nnbw4a/ohlKblHYkYf8vm2+BoO6g4nMQzqM9sPhV4ufVE/E= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com; spf=pass smtp.mailfrom=oracle.com; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b=I1nIdR+t; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=aik1bNRI; arc=fail smtp.client-ip=205.220.165.32 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oracle.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="I1nIdR+t"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="aik1bNRI" Received: from pps.filterd (m0333521.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 49NFfeb6012114; Wed, 23 Oct 2024 16:25:11 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=cc :content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s= corp-2023-11-20; bh=rehhcD8tSJY8JRu91h1Z0RIYv4pfg7THxWZdCfk58+E=; b= I1nIdR+tHACsdA9f/p+9mGJSqonXPzF4pObzurnDb0hcA3xujskgRR6ZxniT7/6P eVWGcMUuwX5tNdLpXSYqsiXsYE3S9sy1/wZoX0rXkDvNHVJNKNyqiV5rGvimnB8x QTBmx3ilM8ipMwRNSDyX6n3Lf0CZ1wHo+mOnZpHfOQP9QJAhuRoQ+TzqMVxDeAyN AYGm+lBvhygXD3NvIteIUIyLRELBf4ftIeiD6G2LT7sLSxlyWAW8IBD1mu9o+FpJ pruCIZBVJjSY8ydNfuqaefRMp7VGXe83c80QKu0+KO3Rn6stziEhDOlJBFMZ5MT4 gv1fZawUERoWXxXfvOKM/A== Received: from iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta01.appoci.oracle.com [130.35.100.223]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42c53urhtb-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 23 Oct 2024 16:25:10 +0000 (GMT) Received: from pps.filterd (iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 49NG7XfL027087; Wed, 23 Oct 2024 16:25:09 GMT Received: from nam12-mw2-obe.outbound.protection.outlook.com (mail-mw2nam12lp2048.outbound.protection.outlook.com [104.47.66.48]) by iadpaimrmta01.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 42emh3294w-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 23 Oct 2024 16:25:09 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=uf5ZDCNWf+byTinSjCRZBw/8rdrlJHEMVjA/qOMhT++s4Za7NzzWENmox1Yt7H37X3xsNH3Gh4vXRfOF38324tr5va+cr6NxRlPB36yszYey31fiBMBA4Arl+oqOfFYrCn4E8maARFnRp/mRInsU2OiDVg6kDfwS14DqQJbCrvcO4LxRu0YId9wDmbgJXtyxJHBB0UuUpmIuUODWUS1CrNjTosfAF63aUKU3xr2BklquuTFd7ndxprGjvSagozOrLutXlNPhoy579Lq5dcW08yxc/FNOxkGbHmFWqtYisebAzxw78sqJDQhIOvGLO9FrIP1Mr0U74PjUHU4s+i8kXA== 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=rehhcD8tSJY8JRu91h1Z0RIYv4pfg7THxWZdCfk58+E=; b=tv5oPxSg57/KWUpx7liwuMgGcEU/U9GXyK2QsRNFxHqZ3XRVnzju1+oi2mQElDWAJOt9QZ8JdapyYbHPQwMT00mHAgQq0W5Ep5X4eEF0jL2axKrVh5Ur9zwunf67nG75y5zqFMU99ElrbsjYrcStLKXAal2WCojKcyT2Mr5h9eloH1xCe1ns9KMH5mg5a0rl62pxqFde5QgAZh7qqljLzBAYNP0OtUJKM91yBa8O3bSUIbta0NZui2pksBPcojPOBkj7fkFKoRw41y1tCGQiukRdOyXX2nZDF/wfBK5MugRoNRPL9F6FBbgYT2GgWZzu3QRL4KoIAJJ3ZlVh8Ee7BQ== 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=rehhcD8tSJY8JRu91h1Z0RIYv4pfg7THxWZdCfk58+E=; b=aik1bNRIKSADHUfusStQ/dO9819zsK52BF2OCFdqm3iORqW9DnsSdaNlePIpdBY8DNSbCTRbFaTZLYtZ3BvhySIM0ZBZ3M8cie8ZNQbQnp+Mca9gRyi3lxvjGy4/RI0EwgiAWfvHcxUSRIWaKUBZ/XAxcm3crDdJvoaNAPyLkHc= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by LV3PR10MB7771.namprd10.prod.outlook.com (2603:10b6:408:1b0::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8048.16; Wed, 23 Oct 2024 16:25:05 +0000 Received: from BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9]) by BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9%6]) with mapi id 15.20.8069.024; Wed, 23 Oct 2024 16:25:04 +0000 From: Lorenzo Stoakes To: Andrew Morton Cc: Suren Baghdasaryan , "Liam R . Howlett" , Matthew Wilcox , Vlastimil Babka , "Paul E . McKenney" , Jann Horn , David Hildenbrand , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Muchun Song , Richard Henderson , Matt Turner , Thomas Bogendoerfer , "James E . J . Bottomley" , Helge Deller , Chris Zankel , Max Filippov , Arnd Bergmann , linux-alpha@vger.kernel.org, linux-mips@vger.kernel.org, linux-parisc@vger.kernel.org, linux-arch@vger.kernel.org, Shuah Khan , Christian Brauner , linux-kselftest@vger.kernel.org, Sidhartha Kumar , Jeff Xu , Christoph Hellwig , linux-api@vger.kernel.org, John Hubbard Subject: [PATCH v3 3/5] mm: madvise: implement lightweight guard page mechanism Date: Wed, 23 Oct 2024 17:24:40 +0100 Message-ID: <415da1e6c5828d96db3af480d243a7f68ccabf6d.1729699916.git.lorenzo.stoakes@oracle.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: References: X-ClientProxiedBy: LO2P265CA0310.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:a5::34) To BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR10MB3366:EE_|LV3PR10MB7771:EE_ X-MS-Office365-Filtering-Correlation-Id: 04e9caa1-07be-4475-a9d1-08dcf37f3fb2 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|7416014|366016|10070799003|1800799024|376014; X-Microsoft-Antispam-Message-Info: IYZpdcwulA2MjzHVOQ1C+U8wt8EGiZF2TX7ULmvus9doFSub2WrTxElBkvxRPZ6+K6nu310oFH03mGFn8COcZrF0aoBe/Mi0vt8IeAWKVgBrNUNAtIWS/7yui3KsgV/jCYWCCuUId5gAGl1i2DI7aTQtjwGvOuaH8Aw92/miJ5LjLhW1SXFLOV9Huj7AgZxhC7RhYtyhQf0BHo9PL5Nm9+eCZYiO/+mN/Z0zLIggFxlPmakRxQUtXrCVEMHIjdv2HLJ01rLMhDwJWDAB9irGMHWmubzdin84zKji7r9LfnCveXlRueeH5TqcxZZ/vHGoEQfEj7w3avVKgnCo6rN2hYn1tpfEym/xctRuqL27cmrGWoQZvddXXM0TUHhWmkkvlerTrYfZRWbCcXQvWHx4yhp0dDjhtZUoFv+MUP+KfoFgu7aWJoZ829dBKO5DZv/tN8NGeBkNVSymgakmOpRiiCHPqBpmlx/IhYNGRt7/YVvnmpWn7xzFD5MArF6T+Avg9Vn+4W65BdM1KwufWtED3rXxY1djh0qAo/8MYfTSBcDXLquJrru8xN7ofxQxmuGKbSEYSOFbkoz8gqGr/06gPx7Svi4jLarbYeo4GOZf+RopB0KuPbZYRhklcthczOCSAtIDvLrSTwfZrgcMZ/dd5+SqLq7lyeN6pPp0wrVLvnL1sgxyepK2zPdJVto31ILldXVH08QUHJLd/3UJSRNlM3aqtd8hb8ti2BizAll+VcColPyAV3Jk6dhHFJZ705y9wYoLnu6FKLztPKO5UsAQV/LodQCvMOYq/VNt5iyHW6dGx+INLiCd09QIgFodK1/idfIG47inwmSFaFe760a6yNYKMtfUL/KW7t5S05g/6LI9UAWu1PkXm3kH4DT167ivOAi/duW69YMsc7eGffV+dHR4CLkooteQWHI7VMOpVMGtveGYuwOhMiNHQovaolz+I6+qfQ1W8ErrkwBxBEgRolUa2/6B5q3dRCgy15j7ltQYXwrLdLtjp9ZUt4o44JLuNcZEad6xmKh1yEVBSfY7kOf1f5VYcJ8KxoPFzfQOeAF7+4aa7wg8qs7+dGkAM5bo40wMayoRjFCq56gUDpyDpOTu65JGxDN02UKL14DGhr+EjLilkx0xmGz1uNxIl6Xnmgc04n++lK7+WFP4vQcvFtKG4W/3lICsclejQhk+ClmszwRYO0bi29668TzWLAuJ9LDw7pKThmXEFSLVXRawXBvHSEVF4GTh9sfP/4Hu8bGqGWwQOCYwCV8ZXCBopQzbpzkplHXB64WdtALqst+V+X+MIU/gRA6wt1ntMCERbGObjzjsbeZRimXVGHqt4pZY X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:BYAPR10MB3366.namprd10.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230040)(7416014)(366016)(10070799003)(1800799024)(376014); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 9rkFjLYOGJCPku7YbmDBkLMAdbf/2ZinmcphrWYCtL+4OIi7DlebS4VMcEWFC5WAP0m1hgiiS/Uu9TDYhtpCa1zhBW7o+mNnOcsJKSY0rLCDDV4mc44D6Njr07k801ysQvt1xniOOWX2vSInZo5mU53Z5GyHICprQ7pHLDLiv92NB2C0sJWGwMTysXz3PmkB62Wj3H2Z4cdJWjnCdt3npnMcOvcALX1ZZKqqChJ5KYXCphqYV5tiINalokQZCyKsJOabJtrETBsnMntVwemKFvTy2uEqIJYHlzP5Qoq8ZVzrEJyHqJQ408toNL9jp0XNvp9t6/cwAb7nTs7KodE7VLy4fYyMEc6mclptctDyKaP05MuzU6EX8/WcvuanZb213mziY8bDQV4zVAE5Ty7sOwiKcB0WDfFaScm0lbNNsv6Mu7ZSyVWCph3kpKaauowRxvLZqHZMHIvZyzBX4bvPmF2Y9oXPRxn592cwdW60On2PuMvh85JaStpQD6b75189Dai+3aN2Pa+y7j1W1yYUeguUrN/olBYqwevIJSVmGvbbFp3aAwMq6uIdi6i+c3lbhfD+xGUHtCY2pUEqc1kXoEwllytHPgFL1T/AqPQM3jVvQKi1emi1Gbe2BSh7sevYWAMX0j+CSgU49qnymGvccUQTs1kvX8a8mm4BOLyt7FQrg8/qLweKAeU+uJ6W/ZwC5vRaqdBRO/k4x5UsTqeO1JMBGEfkXFKP9TWbwdMyXBnPbl1t+0EWCQUfk271GkASB3T0OPmou4XXymf4jGISF4WlzVhEQ6Svo+H6SMsUUnSO4K3/w2N9EyocjR1ZefqUIbTx1vr0DrVPQL621p8K8YL9aEU7oMouqrE/PMLRVKLEh3X4zRj58BFdJ4ygO+o86fy2reKLe/6Dqqk+X9x1A9wqHXXMnXaUCfqTEOKjNWw3QSaVURd9iqa2EMBiJqie8XSmBpv1dXBlvRQihX84+Ri9Lls2nFoYvyMAUwcmQwU/6pJA4qEFYVDmLb/8/moPbx+/VmmUZhk5bw17itoks0u55fkpla5H4dGtgTl7qvXIutvScnqo2jaV/OS5TY0OdgGKptZKUby9Px887+4cbeCagtCMcR5srLVnYf6w3lQTUxs7kA1vhTqAkoM/RdSbM5P4DA7qdPkBHxHQIyHzNWviNlJHgatBmcHkCqignpL7MWBGWRkf8WQSaAFeQWilUsEgAlyHQBxhWpxEsJk7R3mgEMXYEQ7mJ4wLbAfiEVF+NPhkWeQF7IYvGMlwrDN7BJNdENRRTLgDZPtivQ3W1qjIve9+XH2IMkmLs2BmLTeZZjMsvYE2BUqyOFCxMPr/2m1roTSxHDHvH0D41Stc4fsHWqHIo0jG5N/JfNsQvSUgBjABNjavmAUFupxNnYzy5AfZ0Q4q8kNGotB3P91aBwz5DcS/29sByY96XWGxmbjVc5SMwl5mR2fhw0osmR3MJ8OT5tEBLXqTri0Ds4kf9jtcfBRtFPEsxNPuhqKtqJB55kJyePB0zE9rVzQjGnGbKEm0qVBhu22suOC9SDlONQbvwIvL0qVaZUkICqRvvbOdwV827MkB83iaE52HGTIcudMwIPdCg/DFFDceZecy5386ZzpO/RDyWjFn+bWxwRCxl6GXL4OQD1EGLFxPpg8LkyeUIoYIJukj5XN5EjJBwQ== X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: if5QiyLxD9rX40ghYLgbG41r8IUXlU04TLhK6maDJ4IuV1zSdPTvAwQcBY+OR/UAjxEv+xQcYwiQs1EDGo+2aKBFv4zdy3KtpCWlhQXg8oeALM6SrhCm6ZkKF0Z4Cf35SqyQstgf6+ovaGCY+dCUd05YLm8NPbWJzGRQeuuYROQn8Uq1kVsE6n5IiReY1TkPfOiaNIPSfUEj9i921nhJ8q+/U2OcuI0TgUf4QuKm60kSKCsQAA9sMF5VzxYrgD3ig/0/c7t+w965sDEo66BJwznvzZ/jyInnuPPCAC5YP0EFvN9J52B1OTYUrrd9wQa0SmITP00hs00EAANgOJKdMWhl7CdRSedBomapcPRtU9ftrobf2CqI2Zupx4Xkv205hqdw8GqPq2AMUOOaRMHp/5gxBfhMn5zglildTqEgC8L941G/ZViNfxGTdQC6EMpTLFtuWHto66kEmZB6MpdKWlTQUUtCR07wh/Up3bKO5j9bUFA/JNgisv3FC/TiATmvpIpifsqJjayrSkzOZuowmPgf3YoB/XW9WIWpmY4c9nzcU0gzSxYFtJ+sCBqP2NfhTTeUbHS9E/zrZlrCHmT+Xz5cCynUVu5ZHv/NmW1w+No= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 04e9caa1-07be-4475-a9d1-08dcf37f3fb2 X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB3366.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Oct 2024 16:25:04.0934 (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: lJs74We5nrfJsH4UHoob1kK1UzLechaF42hgOK2NzyK6Yx5PRTqpa53ugsqT9qTfTgGDuGs3sTKCf0LejDNwlSpgDvov+oIvPZlNGglW/vA= X-MS-Exchange-Transport-CrossTenantHeadersStamped: LV3PR10MB7771 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1051,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-10-23_13,2024-10-23_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 phishscore=0 malwarescore=0 adultscore=0 mlxscore=0 mlxlogscore=999 spamscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2410230095 X-Proofpoint-GUID: CDZ8coekquKxdI1ikuK1jM3uXG-RlJOq X-Proofpoint-ORIG-GUID: CDZ8coekquKxdI1ikuK1jM3uXG-RlJOq Implement a new lightweight guard page feature, that is regions of userland virtual memory that, when accessed, cause a fatal signal to arise. Currently users must establish PROT_NONE ranges to achieve this. However this is very costly memory-wise - we need a VMA for each and every one of these regions AND they become unmergeable with surrounding VMAs. In addition repeated mmap() calls require repeated kernel context switches and contention of the mmap lock to install these ranges, potentially also having to unmap memory if installed over existing ranges. The lightweight guard approach eliminates the VMA cost altogether - rather than establishing a PROT_NONE VMA, it operates at the level of page table entries - establishing PTE markers such that accesses to them cause a fault followed by a SIGSGEV signal being raised. This is achieved through the PTE marker mechanism, which we have already extended to provide PTE_MARKER_GUARD, which we installed via the generic page walking logic which we have extended for this purpose. These guard ranges are established with MADV_GUARD_INSTALL. If the range in which they are installed contain any existing mappings, they will be zapped, i.e. free the range and unmap memory (thus mimicking the behaviour of MADV_DONTNEED in this respect). Any existing guard entries will be left untouched. There is therefore no nesting of guarded pages. Guarded ranges are NOT cleared by MADV_DONTNEED nor MADV_FREE (in both instances the memory range may be reused at which point a user would expect guards to still be in place), but they are cleared via MADV_GUARD_REMOVE, process teardown or unmapping of memory ranges. The guard property can be removed from ranges via MADV_GUARD_REMOVE. The ranges over which this is applied, should they contain non-guard entries, will be untouched, with only guard entries being cleared. We permit this operation on anonymous memory only, and only VMAs which are non-special, non-huge and not mlock()'d (if we permitted this we'd have to drop locked pages which would be rather counterintuitive). Racing page faults can cause repeated attempts to install guard pages that are interrupted, result in a zap, and this process can end up being repeated. If this happens more than would be expected in normal operation, we rescind locks and retry the whole thing, which avoids lock contention in this scenario. Suggested-by: Vlastimil Babka Suggested-by: Jann Horn Suggested-by: David Hildenbrand Signed-off-by: Lorenzo Stoakes Reviewed-by: Vlastimil Babka Acked-by: Jarkko Sakkinen Tested-by: Jarkko Sakkinen --- arch/alpha/include/uapi/asm/mman.h | 3 + arch/mips/include/uapi/asm/mman.h | 3 + arch/parisc/include/uapi/asm/mman.h | 3 + arch/xtensa/include/uapi/asm/mman.h | 3 + include/uapi/asm-generic/mman-common.h | 3 + mm/internal.h | 6 + mm/madvise.c | 225 +++++++++++++++++++++++++ mm/mseal.c | 1 + 8 files changed, 247 insertions(+) diff --git a/arch/alpha/include/uapi/asm/mman.h b/arch/alpha/include/uapi/asm/mman.h index 763929e814e9..1e700468a685 100644 --- a/arch/alpha/include/uapi/asm/mman.h +++ b/arch/alpha/include/uapi/asm/mman.h @@ -78,6 +78,9 @@ #define MADV_COLLAPSE 25 /* Synchronous hugepage collapse */ +#define MADV_GUARD_INSTALL 102 /* fatal signal on access to range */ +#define MADV_GUARD_REMOVE 103 /* unguard range */ + /* compatibility flags */ #define MAP_FILE 0 diff --git a/arch/mips/include/uapi/asm/mman.h b/arch/mips/include/uapi/asm/mman.h index 9c48d9a21aa0..b700dae28c48 100644 --- a/arch/mips/include/uapi/asm/mman.h +++ b/arch/mips/include/uapi/asm/mman.h @@ -105,6 +105,9 @@ #define MADV_COLLAPSE 25 /* Synchronous hugepage collapse */ +#define MADV_GUARD_INSTALL 102 /* fatal signal on access to range */ +#define MADV_GUARD_REMOVE 103 /* unguard range */ + /* compatibility flags */ #define MAP_FILE 0 diff --git a/arch/parisc/include/uapi/asm/mman.h b/arch/parisc/include/uapi/asm/mman.h index 68c44f99bc93..b6a709506987 100644 --- a/arch/parisc/include/uapi/asm/mman.h +++ b/arch/parisc/include/uapi/asm/mman.h @@ -75,6 +75,9 @@ #define MADV_HWPOISON 100 /* poison a page for testing */ #define MADV_SOFT_OFFLINE 101 /* soft offline page for testing */ +#define MADV_GUARD_INSTALL 102 /* fatal signal on access to range */ +#define MADV_GUARD_REMOVE 103 /* unguard range */ + /* compatibility flags */ #define MAP_FILE 0 diff --git a/arch/xtensa/include/uapi/asm/mman.h b/arch/xtensa/include/uapi/asm/mman.h index 1ff0c858544f..99d4ccee7f6e 100644 --- a/arch/xtensa/include/uapi/asm/mman.h +++ b/arch/xtensa/include/uapi/asm/mman.h @@ -113,6 +113,9 @@ #define MADV_COLLAPSE 25 /* Synchronous hugepage collapse */ +#define MADV_GUARD_INSTALL 102 /* fatal signal on access to range */ +#define MADV_GUARD_REMOVE 103 /* unguard range */ + /* compatibility flags */ #define MAP_FILE 0 diff --git a/include/uapi/asm-generic/mman-common.h b/include/uapi/asm-generic/mman-common.h index 6ce1f1ceb432..1ea2c4c33b86 100644 --- a/include/uapi/asm-generic/mman-common.h +++ b/include/uapi/asm-generic/mman-common.h @@ -79,6 +79,9 @@ #define MADV_COLLAPSE 25 /* Synchronous hugepage collapse */ +#define MADV_GUARD_INSTALL 102 /* fatal signal on access to range */ +#define MADV_GUARD_REMOVE 103 /* unguard range */ + /* compatibility flags */ #define MAP_FILE 0 diff --git a/mm/internal.h b/mm/internal.h index fb1fb0c984e4..fcf08b5e64dc 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -423,6 +423,12 @@ extern unsigned long highest_memmap_pfn; */ #define MAX_RECLAIM_RETRIES 16 +/* + * Maximum number of attempts we make to install guard pages before we give up + * and return -ERESTARTNOINTR to have userspace try again. + */ +#define MAX_MADVISE_GUARD_RETRIES 3 + /* * in mm/vmscan.c: */ diff --git a/mm/madvise.c b/mm/madvise.c index e871a72a6c32..48eba25e25fe 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -60,6 +60,8 @@ static int madvise_need_mmap_write(int behavior) case MADV_POPULATE_READ: case MADV_POPULATE_WRITE: case MADV_COLLAPSE: + case MADV_GUARD_INSTALL: + case MADV_GUARD_REMOVE: return 0; default: /* be safe, default to 1. list exceptions explicitly */ @@ -1017,6 +1019,214 @@ static long madvise_remove(struct vm_area_struct *vma, return error; } +static bool is_valid_guard_vma(struct vm_area_struct *vma, bool allow_locked) +{ + vm_flags_t disallowed = VM_SPECIAL | VM_HUGETLB; + + /* + * A user could lock after setting a guard range but that's fine, as + * they'd not be able to fault in. The issue arises when we try to zap + * existing locked VMAs. We don't want to do that. + */ + if (!allow_locked) + disallowed |= VM_LOCKED; + + if (!vma_is_anonymous(vma)) + return false; + + if ((vma->vm_flags & (VM_MAYWRITE | disallowed)) != VM_MAYWRITE) + return false; + + return true; +} + +static bool is_guard_pte_marker(pte_t ptent) +{ + return is_pte_marker(ptent) && + is_guard_swp_entry(pte_to_swp_entry(ptent)); +} + +static int guard_install_pud_entry(pud_t *pud, unsigned long addr, + unsigned long next, struct mm_walk *walk) +{ + pud_t pudval = pudp_get(pud); + + /* If huge return >0 so we abort the operation + zap. */ + return pud_trans_huge(pudval) || pud_devmap(pudval); +} + +static int guard_install_pmd_entry(pmd_t *pmd, unsigned long addr, + unsigned long next, struct mm_walk *walk) +{ + pmd_t pmdval = pmdp_get(pmd); + + /* If huge return >0 so we abort the operation + zap. */ + return pmd_trans_huge(pmdval) || pmd_devmap(pmdval); +} + +static int guard_install_pte_entry(pte_t *pte, unsigned long addr, + unsigned long next, struct mm_walk *walk) +{ + pte_t pteval = ptep_get(pte); + unsigned long *nr_pages = (unsigned long *)walk->private; + + /* If there is already a guard page marker, we have nothing to do. */ + if (is_guard_pte_marker(pteval)) { + (*nr_pages)++; + + return 0; + } + + /* If populated return >0 so we abort the operation + zap. */ + return 1; +} + +static int guard_install_set_pte(unsigned long addr, unsigned long next, + pte_t *ptep, struct mm_walk *walk) +{ + unsigned long *nr_pages = (unsigned long *)walk->private; + + /* Simply install a PTE marker, this causes segfault on access. */ + *ptep = make_pte_marker(PTE_MARKER_GUARD); + (*nr_pages)++; + + return 0; +} + +static const struct mm_walk_ops guard_install_walk_ops = { + .pud_entry = guard_install_pud_entry, + .pmd_entry = guard_install_pmd_entry, + .pte_entry = guard_install_pte_entry, + .install_pte = guard_install_set_pte, + .walk_lock = PGWALK_RDLOCK, +}; + +static long madvise_guard_install(struct vm_area_struct *vma, + struct vm_area_struct **prev, + unsigned long start, unsigned long end) +{ + long err; + int i; + + *prev = vma; + if (!is_valid_guard_vma(vma, /* allow_locked = */false)) + return -EINVAL; + + /* + * If we install guard markers, then the range is no longer + * empty from a page table perspective and therefore it's + * appropriate to have an anon_vma. + * + * This ensures that on fork, we copy page tables correctly. + */ + err = anon_vma_prepare(vma); + if (err) + return err; + + /* + * Optimistically try to install the guard marker pages first. If any + * non-guard pages are encountered, give up and zap the range before + * trying again. + * + * We try a few times before giving up and releasing back to userland to + * loop around, releasing locks in the process to avoid contention. This + * would only happen if there was a great many racing page faults. + * + * In most cases we should simply install the guard markers immediately + * with no zap or looping. + */ + for (i = 0; i < MAX_MADVISE_GUARD_RETRIES; i++) { + unsigned long nr_pages = 0; + + /* Returns < 0 on error, == 0 if success, > 0 if zap needed. */ + err = walk_page_range_mm(vma->vm_mm, start, end, + &guard_install_walk_ops, &nr_pages); + if (err < 0) + return err; + + if (err == 0) { + unsigned long nr_expected_pages = PHYS_PFN(end - start); + + VM_WARN_ON(nr_pages != nr_expected_pages); + return 0; + } + + /* + * OK some of the range have non-guard pages mapped, zap + * them. This leaves existing guard pages in place. + */ + zap_page_range_single(vma, start, end - start, NULL); + } + + /* + * We were unable to install the guard pages due to being raced by page + * faults. This should not happen ordinarily. We return to userspace and + * immediately retry, relieving lock contention. + */ + return -ERESTARTNOINTR; +} + +static int guard_remove_pud_entry(pud_t *pud, unsigned long addr, + unsigned long next, struct mm_walk *walk) +{ + pud_t pudval = pudp_get(pud); + + /* If huge, cannot have guard pages present, so no-op - skip. */ + if (pud_trans_huge(pudval) || pud_devmap(pudval)) + walk->action = ACTION_CONTINUE; + + return 0; +} + +static int guard_remove_pmd_entry(pmd_t *pmd, unsigned long addr, + unsigned long next, struct mm_walk *walk) +{ + pmd_t pmdval = pmdp_get(pmd); + + /* If huge, cannot have guard pages present, so no-op - skip. */ + if (pmd_trans_huge(pmdval) || pmd_devmap(pmdval)) + walk->action = ACTION_CONTINUE; + + return 0; +} + +static int guard_remove_pte_entry(pte_t *pte, unsigned long addr, + unsigned long next, struct mm_walk *walk) +{ + pte_t ptent = ptep_get(pte); + + if (is_guard_pte_marker(ptent)) { + /* Simply clear the PTE marker. */ + pte_clear_not_present_full(walk->mm, addr, pte, false); + update_mmu_cache(walk->vma, addr, pte); + } + + return 0; +} + +static const struct mm_walk_ops guard_remove_walk_ops = { + .pud_entry = guard_remove_pud_entry, + .pmd_entry = guard_remove_pmd_entry, + .pte_entry = guard_remove_pte_entry, + .walk_lock = PGWALK_RDLOCK, +}; + +static long madvise_guard_remove(struct vm_area_struct *vma, + struct vm_area_struct **prev, + unsigned long start, unsigned long end) +{ + *prev = vma; + /* + * We're ok with removing guards in mlock()'d ranges, as this is a + * non-destructive action. + */ + if (!is_valid_guard_vma(vma, /* allow_locked = */true)) + return -EINVAL; + + return walk_page_range(vma->vm_mm, start, end, + &guard_remove_walk_ops, NULL); +} + /* * Apply an madvise behavior to a region of a vma. madvise_update_vma * will handle splitting a vm area into separate areas, each area with its own @@ -1098,6 +1308,10 @@ static int madvise_vma_behavior(struct vm_area_struct *vma, break; case MADV_COLLAPSE: return madvise_collapse(vma, prev, start, end); + case MADV_GUARD_INSTALL: + return madvise_guard_install(vma, prev, start, end); + case MADV_GUARD_REMOVE: + return madvise_guard_remove(vma, prev, start, end); } anon_name = anon_vma_name(vma); @@ -1197,6 +1411,8 @@ madvise_behavior_valid(int behavior) case MADV_DODUMP: case MADV_WIPEONFORK: case MADV_KEEPONFORK: + case MADV_GUARD_INSTALL: + case MADV_GUARD_REMOVE: #ifdef CONFIG_MEMORY_FAILURE case MADV_SOFT_OFFLINE: case MADV_HWPOISON: @@ -1490,6 +1706,15 @@ static ssize_t vector_madvise(struct mm_struct *mm, struct iov_iter *iter, while (iov_iter_count(iter)) { ret = do_madvise(mm, (unsigned long)iter_iov_addr(iter), iter_iov_len(iter), behavior); + /* + * We cannot return this, as we instead return the number of + * successful operations. Since all this would achieve in a + * single madvise() invocation is to re-enter the syscall, and + * we have already rescinded locks, it should be no problem to + * simply try again. + */ + if (ret == -ERESTARTNOINTR) + continue; if (ret < 0) break; iov_iter_advance(iter, iter_iov_len(iter)); diff --git a/mm/mseal.c b/mm/mseal.c index ece977bd21e1..81d6e980e8a9 100644 --- a/mm/mseal.c +++ b/mm/mseal.c @@ -30,6 +30,7 @@ static bool is_madv_discard(int behavior) case MADV_REMOVE: case MADV_DONTFORK: case MADV_WIPEONFORK: + case MADV_GUARD_INSTALL: return true; } From patchwork Wed Oct 23 16:24:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 838942 Received: from mx0a-00069f02.pphosted.com (mx0a-00069f02.pphosted.com [205.220.165.32]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 98BCB1CC150; Wed, 23 Oct 2024 16:25:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=205.220.165.32 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729700747; cv=fail; b=trxKmSowWdfvCFAkybe4jGIckStlfCANH6SCT6ctHTsBrjK8gYWPjYh1WUaDWBm7GJ7b+zQBUk9JMCMuYi3doTI9mITqt/ZBwZhhQ6czAhqjrVvSm1EkSFbIwg57jDE1TXZ6WAM8ePFcAefuU/ySx6HoTK8KBdYyaAf7nPJeoEs= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729700747; c=relaxed/simple; bh=UMXPNyMhuG8K3Sa4CmNpk9M3JSqkXu/o90O4N0El2xg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=AIm/WmRudhVCA1OQAd4hdAQIwiK7bIRVFVhmOA6PMMzf69p520F8t2Mkeruq3g5IAiXlZ6S+KpA8svk7MSbvRyWYpy3nnYUr6LPdHQErUI2sVT6zQSghCk2DNAJgBAUxZG0MrD/mGgg7hVSPh60iAbtU+uq9u302StorNF9/xTQ= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com; spf=pass smtp.mailfrom=oracle.com; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b=OQGzpDXG; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=C+a2OfwT; arc=fail smtp.client-ip=205.220.165.32 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oracle.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="OQGzpDXG"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="C+a2OfwT" Received: from pps.filterd (m0333521.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 49NFfeeR012091; Wed, 23 Oct 2024 16:25:12 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=cc :content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s= corp-2023-11-20; bh=A5pNDtupVIY/6qMld72YpfiKd1tj229APGKKEth5+Rc=; b= OQGzpDXGsAWINKUQV0qdWiS5B3rKRYaQNnQkgfMdCJ8BkiAZA58PdwjuFMsRAKZx QWj4UfSfBROWqjbiqWodDaXmBNc2Og4eMMKIAp8vgEiD6fk6rPl7PGn+HeglojAm Vfkd69Sy3hxOb1lGalnfAXZNHGNRlacKziK0FhS6WI3ICUzm05u6CumLCn09EGRH wC/d31bVCb6HOvfRaBCMrFXenQiH9n67gDO2zF96pr7ZAy2xiQNcNi0lj939bZ3A jM1d8bBNP+qfmJbfZLg+gPYFbR5L8u6G5DnygGekhJoZX94YSIDs7oy0lgNZ2rmU dbaPsLFmlXVSz+OGzQ24sg== Received: from phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta02.appoci.oracle.com [147.154.114.232]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42c53urhtd-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 23 Oct 2024 16:25:12 +0000 (GMT) Received: from pps.filterd (phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 49NG9cBJ025412; Wed, 23 Oct 2024 16:25:11 GMT Received: from nam02-sn1-obe.outbound.protection.outlook.com (mail-sn1nam02lp2041.outbound.protection.outlook.com [104.47.57.41]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 42emh9rd4k-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 23 Oct 2024 16:25:11 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=s2oCC2EW+/xPUlDvbvVIhEaqkB8+sKKfxkCNGSaZwmijAWMgHzKSm2SDrCoCaoI276I3m1zOvLoHv0A/892WDVWqvgER9P1fm026hk1zM+SAoH7HzAUZbtCFLWHtb2N8RKlxxDpwlvcQTqzuQJ552upvtMYHF67b6pFdmD4nZY7ZUG7TmS0pwnpGc9QFV2SyDq+9EOauv54MWJrBaqmVKWq4qlFG8ldV3EpF+fAUjjNQLfH3BY+31AkUM3Aed8nzifQQgi5P5yugzmJZtXDylDWClUTWgzgtBORizDXjN6X7Q63bXfMLV8mpbybevXjHsdL5oRpmxkYZXNXcC3E3aw== 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=A5pNDtupVIY/6qMld72YpfiKd1tj229APGKKEth5+Rc=; b=OJZewXa2mkWIjTUFRMJ6rUNbWiCCsqRArThHSLDoY/vEF71i9hYIg6DaIHcQ6UJ05z4exnYHTx+ljJVX9gO6P2N1G9WTKTlPmxQKkmVqTlmvhkK93CLPukaLydyKGhA/wuUS5tuZNNew9wItRnodSry2I3e/bQd63fLyI9OAbEJPKqqxgBp7Om3oyQMhvHFSY3yN+guKYuwaneuyVchTJmjjIPRd7Mxy77xe+oorwSdv1vCr6tUsTl/eSq1dlEcau4qKxcC/TfHDiQIVtzR9vvZUrNg6+IQA7h0cbU5/MmjXqPcSyT+9BLbSPSHlyowRy8ZTwJb71H06HisqI0HoMw== 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=A5pNDtupVIY/6qMld72YpfiKd1tj229APGKKEth5+Rc=; b=C+a2OfwTiiK3bLDOi6o7NEQEWwm6GFi8Cp76qXwfmYkAKhazAiHXLZ8IFCzCjGg9h98uE7uqt/QxHmoB+gsPiRUHdn1gsZjeNIi8cx+1HJgg9oYpV2oW/LBp1LwxZziKlmtDGPloRtRpUWOPxJPEFOA2Gaz0GcWaf6hREFDk11Y= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by LV3PR10MB7771.namprd10.prod.outlook.com (2603:10b6:408:1b0::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8048.16; Wed, 23 Oct 2024 16:25:08 +0000 Received: from BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9]) by BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9%6]) with mapi id 15.20.8069.024; Wed, 23 Oct 2024 16:25:08 +0000 From: Lorenzo Stoakes To: Andrew Morton Cc: Suren Baghdasaryan , "Liam R . Howlett" , Matthew Wilcox , Vlastimil Babka , "Paul E . McKenney" , Jann Horn , David Hildenbrand , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Muchun Song , Richard Henderson , Matt Turner , Thomas Bogendoerfer , "James E . J . Bottomley" , Helge Deller , Chris Zankel , Max Filippov , Arnd Bergmann , linux-alpha@vger.kernel.org, linux-mips@vger.kernel.org, linux-parisc@vger.kernel.org, linux-arch@vger.kernel.org, Shuah Khan , Christian Brauner , linux-kselftest@vger.kernel.org, Sidhartha Kumar , Jeff Xu , Christoph Hellwig , linux-api@vger.kernel.org, John Hubbard Subject: [PATCH v3 4/5] tools: testing: update tools UAPI header for mman-common.h Date: Wed, 23 Oct 2024 17:24:41 +0100 Message-ID: X-Mailer: git-send-email 2.47.0 In-Reply-To: References: X-ClientProxiedBy: LO2P265CA0219.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:b::15) To BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR10MB3366:EE_|LV3PR10MB7771:EE_ X-MS-Office365-Filtering-Correlation-Id: 1bb91279-36f2-4e60-807b-08dcf37f41d5 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|7416014|366016|10070799003|1800799024|376014; X-Microsoft-Antispam-Message-Info: UykFLxwYQiABzcvJJjn/1yVbnojflyAI9XIDBYYDe8nGrAjEU3/jJebGgNfXo+4Fo1MRJGn3nXFYuSP8PSqjWRwlsZEp9XiPIvo5Jy2DoebRLIrI1/M46eJVZqagXkwngHVoZXcIsrChuREL4MYuhs1MG8H052oNkaJ7IRdrOQsnW5cvcRK0yIDM522VWJds7KG6fsWfz/vhKm7HptlK/4yRSpuq/B4+PVOpnKcnVLc5IdmMiNSc8cc+XinmJy3nfxO4a/hKCZ20yKwBS/z8CJoyRA7C18VkmMPQlMo2zmUF73w9c8KC2UOEMK507wX16Lz/dIGY8LD4K1byPCeK0XwyslDwqzZ6EIah3HVL/G+aADhih0LZ+5wibiJhU6j90cKJF4cJvME0/MT675+WdyStTHGLKzCm6pmnCzrVVqop9BxIAY++5mHhUfhpbkza5umSdilSefb9RT/cCQ6oayx+95gSbWedlp6Q+UKDks/z9FWbSnxWasMS5Ut8QEcokChuZ3Rkz968lBMBzRLMxWTcIDgwKS4mP+YXzDjPqOjfkXEBourN8qP88ZgZcEJ+SQjhrOqkEaGPTi+Uopa0PdyWAEiigH9sYHd02nMEwHNSAfUDyxWL/K+jm9yTVdb5DIrIIV+Gyo6+34ZQCWw9djGzhNpaPIizmKqvmbv6QWHuI8d3kMm6BXO7bJe5sHrT3X7bRXPuq5V+X8/KZrKAjyrhp4rJ85o2xWcBNi+FNxVSz4gupTKEgYCFH2g570FB2srjyjc5vE8aQgLQCrCveqTLsUg8nfGInGGRLeQbvYIEoRrL+MSsnSXcww3MCPt3cGYCL4i8GdoUGt2qnExBDNhpUuKno50aF8yI2rccJWknRjmzJXybjlD2CWjGhte5IB9Q0CVpgZFHWu6VI/4wCcqT+Z0KdpOiZvND1scWsOAWqnXaaxotydatnkBjl+3E3UkObLuSyB1djadD6RRN3VbatBLN+d8O6HADXbqgMZsovw7Ffi/tZ1/Kpgf/HrfrJmdzROx/vatqNUUQIBkSVb8kXPSIga9uVpd0uNEzBwY3a/yGxWr43p/9gNrca8zwbO3MtlekNZJauXm2O1VuTqy/KxnH+eITfoVAeWHiuXP/UXHgxWgf7OeDu9wnlvQ0XxuSH9UPZu5g69VZavSpOiFIUHpEr+7DwAmq3RmF/HCz3WEthFAMeZxnpLNk+Qj72ImCpFcAthi5cfX9omwTB9/3yGZR5ZBeICN4LUdyRywzir67ivFx/f74ye86RcO41TzNz3BxsvMQ29NH+fq8GVMyfvOBSIL3qeUSlFOYIZ5D9MIIuxHX6OgIqvuBpYyW X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:BYAPR10MB3366.namprd10.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230040)(7416014)(366016)(10070799003)(1800799024)(376014); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: YikuZoNCf8R6tlLMSPFEZaE8R91Vepny4VnxZhlgZEOvcU117OEDu9LTMMmEo2djcjhkKHIY2lBOrB1xQbrdBzNpadMrVE6TBrQ2NURj5+PSeiolbzHu3dteUaCTxBddd6ISR8Y4MurTlykt8WlLNTabB2mMk/HAfYjxRkai3ejYaz0q71bnSeabMPFK8iIMJ1wCeAGiZ7QdwcCh25vnB+eo/uDPhuZSpKovYDVJlYhIjVyKGhA+qOKrtyFy/iu0RoJvXWpB7zQHAWPul3vhhBF6Id/ICWLxwcP+TP7sKGRZfC3FKJ2k1qr2sya++C7Qy3hSwegNL7q+3u12S4fLQrYptmMeOhmkK0XR0c0A3JYS/a+NPkSstnSYfThh6cZxWc3Eh2A2YktEgTTweyBv3DX4pGXCcSGaChM3cQ2uaz5U5gKSKYRJb+AJkQ+3PpRudygQml3pHzXZIIGCOuwzRMu/HpBZNAA8r9NniwKcVDdDznq4gLwBHyIGe4dz98MfZ9PNhRtPuoBm50/F8HxMqUD2oujZbxGQwj40tNAymk1wmb4xjc+FA7+aIQ20kwRx46rrP26ZGF6tQ603qKHTn5TJHkEB9NqheKASkwHqaUWUaSzwxBFIgXe44CtImtRq8LGN5mCwtTF9enqFLc/jjmR1zuVlINy8EWsp5Xaxs1waoo99d29SLgF8zFYtFrOQi4aMint3HRTR6Tj0831gbj9aee4hZ0PdiGCKoMl8XtGBbZAR3f5lveTa2AYjtFGI6eGOrYRLEQmV+WPxHR2rjSlHJcN1SYVoVpUgG0kpT0knZR7QOErbc0VM/wkD631VSNPX0ApgB/Yzotb2lBEWSyznppCfpVJfzvgRhYl7peUPttYsjjzEzqCv5KLI20nNS7b1txOjUnr+qidwhEA87opjEEpjrclXgJRUNcDNVnKOKw87/ylv7Fc23oBL5UGdxeqK5QERh40+z/N4eVafN1ceypp7aZRQ18BTg5qjStle/qjVWfAiwnwmxvUwabuEWZxJC/iWdObsaJhqD/oYyX+pED8gk38MrITC+LklMrX8MdISG0C86uK0dqXKRwZRPHBYI8n6DiEBIIc2XJEUppSVjWvgI1AaT/amWP5/S8bMesB8uUkYjg3Ox5TCdCyWYP/s4AqLAUyvAoTLxk3kAl8Egfx5/d8tBOAD4yzcdrziLlIelYXvIKI1rgstNvdXVK9MnAkgUYQQgZoM01+ZMkCcRDh6unoJLpWTZfJUw9jtpXI0dHPgYu3tQbPY2orgp/6v3jUnfRinDm8rfjkziDPolgSTW1j0yLrdKd9pYvNYm5thTfAypWENd6zfYlNQHpWUNsZ+SwgYzU8GlkpMJQApIO1T4eeZr1AsPJiKvNw569p8ZUcPNWw8KkxQrLvrp0D2moVgZ0Vvua6FBHbhiWd6WrnpYuUO/MmevmNBpVpVIugnuQDBCAz913yQK+F6wQCtuuH3W5R8vn+lB2rsXmUeDGA66jf6T5Es3v19/BRAGn+xor7Jo52vvAe1PT7oYW9FKatgJ9S3Lx/mkkkQZjfRyX+q+mXTjqN6/KVSd5UplWwA/E1ZcYKWkug5T3nQEifCFDwFWaX+NO/A/GMbsZbgbeNbhRXyRVESBpCIGa1eQKxCrQtDyrjt/OAShr0HfrW5l8bukNjGVN3NAT2nOA== X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: gqVKXU/AjOmdGGfQVN5LagOoJuyneNPpXo8rIlcZj1SCEQwCFB11yc3NPjJfIh17bzGE3QAgzigwV7g8GBzB5Js90lijTPSIIhJtNdHIJB3yUcU+qK0uTlMJ/1Oo3gee7NqLi/DSoLLiEL1pqj6UF0exCXMm8EqeQRE+18Jgvt8aHwN0fRkFaWpDxJKyjMPo4cr5f3wR8f3TFZhwPo03DUHi9OxSkA5K9tngh8ZY3SL6YiN9A9VrrVjENTisBNGvhOQZeK7LEvaP2yk1GlybEReqvTY3VOT8vS54Ld/Px5bq0btIzubQEf7OKPGsKSKmDXQWgni77kymHqhK6Tk2HA0C+9C9y0wOyVzg8PO8DmnGVO4wUQazHUGbQ3HHDjukSJiBY/YyyTIsP9l8/BSPKWGGT/F4jlhFaNHCQVc3dbKKTmD9exaB7W6Q0kVTlGWc4cz7KhnYGlZdEkkpGFmmtMVmSGnRlK4auTh5O7BdChuB71StJdd/diOxvA71Nf3vZwzoJkRpFmbLRjwhxIUwpNcHMJNbSNNN+xTP740VlIk1CfLHIB2eYPIR464rd0QpwL/BICa7dP3omRb4VjoPGY7XArpRTmnymOWHnUXyOMo= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 1bb91279-36f2-4e60-807b-08dcf37f41d5 X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB3366.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Oct 2024 16:25:07.9085 (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: RwiTvHlPuPImutbls+xxjc06+TlU9mzD4b/ohW+2xvnfEK/kCrYrc98PRWAwCbznvyMQLQ5y+wG+CimZkddYFQgt+2Q8SneH4CXTy8XiWcQ= X-MS-Exchange-Transport-CrossTenantHeadersStamped: LV3PR10MB7771 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1051,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-10-23_13,2024-10-23_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 malwarescore=0 spamscore=0 adultscore=0 suspectscore=0 mlxscore=0 bulkscore=0 mlxlogscore=858 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2410230102 X-Proofpoint-GUID: XemlPcOgAzeYzwjS2KKly0v-vLs_O2Iw X-Proofpoint-ORIG-GUID: XemlPcOgAzeYzwjS2KKly0v-vLs_O2Iw Import the new MADV_GUARD_INSTALL/REMOVE madvise flags. Signed-off-by: Lorenzo Stoakes --- tools/include/uapi/asm-generic/mman-common.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tools/include/uapi/asm-generic/mman-common.h b/tools/include/uapi/asm-generic/mman-common.h index 6ce1f1ceb432..1ea2c4c33b86 100644 --- a/tools/include/uapi/asm-generic/mman-common.h +++ b/tools/include/uapi/asm-generic/mman-common.h @@ -79,6 +79,9 @@ #define MADV_COLLAPSE 25 /* Synchronous hugepage collapse */ +#define MADV_GUARD_INSTALL 102 /* fatal signal on access to range */ +#define MADV_GUARD_REMOVE 103 /* unguard range */ + /* compatibility flags */ #define MAP_FILE 0 From patchwork Wed Oct 23 16:24:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 838941 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F35921CB301; Wed, 23 Oct 2024 16:26:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=205.220.177.32 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729700764; cv=fail; b=e8+Lpvyywb99A5c8872FCQRlJOrvbXG0VFEUnTy4F3bUAizCsWPaRE1PWi0zFiOo7IUXYmJrPWS35Vlw+1/15kT1LPwjJ/1pezY4K3pSMsgFOEVLGJ5pEZs7xCjjqZqD2XufC4kTlr0TJxYOCm8mCPK41spapZNtUahcTS4PZEM= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729700764; c=relaxed/simple; bh=HgSTrNnBcX5ZiJ2obkRFHh9pnZiuitbXDi4nPmrTgHw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=s7tsSKbQarlLc+SueAAcEsJfXhIUjzC4og2KHSP68QIVc5sS/CeWCaHroTVCHtItRz/Wvyfr7bHjBHO+rt/N7gt10S74TVvVyYSrA9FGhkaRNq30R8Je8pBHrMKFw8rVsOFjZtJSnEAfqaB8cpYLxh8hqJmClDJPKiDOK7qBIZE= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com; spf=pass smtp.mailfrom=oracle.com; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b=jPn2z3d/; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=j31gp08G; arc=fail smtp.client-ip=205.220.177.32 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oracle.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oracle.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="jPn2z3d/"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="j31gp08G" 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 49NFfkpS011171; Wed, 23 Oct 2024 16:25:27 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=cc :content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s= corp-2023-11-20; bh=m9e/npa4ZtTr6iLFSo4OUkRAsp4xmX97+p+Qp2ItzGg=; b= jPn2z3d/pR5uhPJz4Pkq2Oa5fqpQuE1++Y+2VY+hzfPtUNOPANd/EmRSrgNpBvj4 VbgV8wr+B0NFypKsVwA5ez0W/BRTkfgcADq21gWSD+GM9lKj5cbLB/QwIXnq2Eez QqIviivx9b8vbMCbCy/+KUVhnn8JJXPWsVJlAca+P9rCnPi326vJhAJuLKLmXdH1 3hsnAHtt7pYJtti8aZ+8CJl6f55aHfk2lzyE2C++uPe5b5ckM0Y+mqUOgyffpR2f VGMtS4gFn02qyvGyLdCewx6nunidCaWdXgLT/0S+7E5hTZdfe/6+Lex5Bo87CJXs 0rbPv/H2yxIncoGgmdLEXA== Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.appoci.oracle.com [138.1.37.129]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42cqv3fqe5-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 23 Oct 2024 16:25:27 +0000 (GMT) Received: from pps.filterd (phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 49NFMqeI036049; Wed, 23 Oct 2024 16:25:16 GMT Received: from nam12-bn8-obe.outbound.protection.outlook.com (mail-bn8nam12lp2177.outbound.protection.outlook.com [104.47.55.177]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 42emh2rcdt-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 23 Oct 2024 16:25:16 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=CP+Uo2nabR7SnjThlwEYoxGcL5nPN1iarXeN8a4XkzIWTDOgk0nUAuITQ86Ngkbrzd5FarkaHQoPu6DA19kn0DOzx+DPcqKAxxCvNiGwMnmRsd7vNzGvVLHh1qWAiQ7l49SjT2r1u98hd4DWszEOwQD6yQQR4abQEWigsJbsn2HRKRk0BAIQONLP9q3DLaP4A7CkhAkc4/Amh4hD1qVV4YgxvKZoQHEfGddBUESPlpeBynPpPtAFz45f1qhobiE+78naWU3j0iHOKUByf806slCezfcvU+P7y7PQPIlvzO5QtrHKrMUkklIMfpB6HgyIT9iI7i3CZ7svIXvN8+2IzA== 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=m9e/npa4ZtTr6iLFSo4OUkRAsp4xmX97+p+Qp2ItzGg=; b=bQyPCvDZOOp78181eWcLPU7iu8H8+nTe9/8T6nZop4g+xwxE24vL7Qr+291TqzfWI0yG4Djc/xgh0jImAJ8PHKUkm4VIiRswkEtnMFUsWun90CV5zCrCUvCTf9fF2M9PkZ9cPqqKCYR4JWJFHM8+N0SDRVZnf/6YAs9ltB3+X9A9Q6uEmPJ0N32gP83bclNqwELITJsr/l9Y8uwD2Eqy9JjgDoMNsTI0MHLq1I/G3NkLdB4pCG+3bYcZLkLkuZvev69ELDLPlOcjHzRFZKGftS0WDqSXMNqOQY7daM/xC41v5ycAvOw5KWra63qrQ6CVCEfLei3hr7de9kAczYEVrQ== 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=m9e/npa4ZtTr6iLFSo4OUkRAsp4xmX97+p+Qp2ItzGg=; b=j31gp08GAw0T6pyQwClZ58I+69ZfoUnJ5HSrTxmS3KgVinForfFK++Xw+pZdTDkzH+0CdMswtoPNkSQ+1N0nJ5Tu+7BZn2uHGXz0vn7FHt3gkUEw+VOvdxdMzsidbmUEVeHdADzatkXRDl/t2UPr6tpANDt5nWKHwZ4mTNVPUCk= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by LV3PR10MB7771.namprd10.prod.outlook.com (2603:10b6:408:1b0::15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8048.16; Wed, 23 Oct 2024 16:25:11 +0000 Received: from BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9]) by BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9%6]) with mapi id 15.20.8069.024; Wed, 23 Oct 2024 16:25:11 +0000 From: Lorenzo Stoakes To: Andrew Morton Cc: Suren Baghdasaryan , "Liam R . Howlett" , Matthew Wilcox , Vlastimil Babka , "Paul E . McKenney" , Jann Horn , David Hildenbrand , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Muchun Song , Richard Henderson , Matt Turner , Thomas Bogendoerfer , "James E . J . Bottomley" , Helge Deller , Chris Zankel , Max Filippov , Arnd Bergmann , linux-alpha@vger.kernel.org, linux-mips@vger.kernel.org, linux-parisc@vger.kernel.org, linux-arch@vger.kernel.org, Shuah Khan , Christian Brauner , linux-kselftest@vger.kernel.org, Sidhartha Kumar , Jeff Xu , Christoph Hellwig , linux-api@vger.kernel.org, John Hubbard Subject: [PATCH v3 5/5] selftests/mm: add self tests for guard page feature Date: Wed, 23 Oct 2024 17:24:42 +0100 Message-ID: <53efeca2f9db78f7accbeb721106f5786fec9e90.1729699916.git.lorenzo.stoakes@oracle.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: References: X-ClientProxiedBy: LO3P265CA0025.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:387::6) To BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) Precedence: bulk X-Mailing-List: linux-kselftest@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR10MB3366:EE_|LV3PR10MB7771:EE_ X-MS-Office365-Filtering-Correlation-Id: 280919a5-979e-43c6-56ff-08dcf37f4446 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; ARA:13230040|7416014|366016|10070799003|1800799024|376014; X-Microsoft-Antispam-Message-Info: quOxPF+on28JGp6Vl73nRcdVrCjY0S1uffNEJ0J7wmfugg7dQ8omyOE/2zfIbm3TUdl7nLIYAeA9Lt8vIzNXcS2Y/IxbM7v1iIoGMNygrdryPPO8Q28vIfVH2nbRCVFSkNW4czxWVebI/lmf4v/7VJHZ7OEZUglsZ3h2KdkOK4PBzpSYV2BXdl5p5f92wzVJID2ENZB1LDqdFeihs7CtXqcX5ScQqvIio0qEHCYajymo4DTL8cgVRfedX9rSOMi53dFarcc7DlW4jqYBmZXg0pBnmLWyvBbjycKV5c4AamyCj9apIXiUSDkHa9LpkqkdI3ZIm0Z6G6brZNtGCI3TZTyVo6mSjIAQnOrcE8sVa7lfbKNijJPLUhGlymNsEPpNOedhGCTkzmgpzc0F8ZAyWd+rwQTp3BG2zGkbHJtQA712KzsCN58KD5Dq/R2STOUzn8CKhyIukg1Vg2wOd6r058rYSnyGLT689WFmi7wBd/ptfx2PyKgRWoCUfZb/NK1vrce+dwjExbRdjKPDts4I0Mp0YbWITMPGO2TR5eS3BrJstpGXcHwd8Nn70t7Tj6/Lvt2W1x3i2nxuUqzpk3Nfhl0W/JI5IclxbNoGTDrikiYSb5pk1ryoSi4RbQN3O4fotGwAHD2bKjTz0q4q2oDZXGPapw0oOFWk1NRrIO+Vq6bQxS3ZP7Xn30fU3t4TN8sVLhE3+cAy4EoLsUGI2uOUxkRd5JEHdHaPiAicmbKcZocDwwyiaRD5HSN+ElhRoDT8YOhhclvZEEV1Hj7W7mRlfy2Upr+1MrKGGREC72ySgGJU1pJ4p90PDxRNtxRZ6EAXxfRIXahoabtHmO50hRVBIeDojw+m52KifWPOUc1dsxAAvHzjknHBONakO6bhoiGUvMBHYHTqEugO1rS1Vrrfw/8Hm/0kzMTR56P5v0GNSMuGVCOgELVZx6VcC0Wgli3pmm9Ap93JtIRe35SCa5JPA58Sglzg5Nyis+n+EzBpwmmtbcmbh94sw/hyQ/V+8zkG5RjRmwyTO/nhs3FClL9VhiAakhrxSwhTX6WQ5/y8qEaO+dwtyK84PXTbM7OQK9wxKkkKPK/jDwFYut7LCGKA7bZfz2fUX4vsoPmKPG7VT2MQpEz8Glx1HD0uiGz5ouCCk0j2Sbx3Iux0MO5jHtXcSIfm6JprQeZazGkSyqQsvCk1U4XB90GrCPm+6f7hR3VxlsCBxWh7oprg8lwkE7CVhIznEhQhcM/0QJwKLzUfj0TGHdoLmpDZqNo3FZX0OLHPxc4Xc+Ywmp/57L2wWTKQMTMPLYBUSoKO50c+Y1M2/vPgCoiIaXyJelH7XGpvSkRP X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:BYAPR10MB3366.namprd10.prod.outlook.com; PTR:; CAT:NONE; SFS:(13230040)(7416014)(366016)(10070799003)(1800799024)(376014); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: AVhx2PE/Tyj7FaFpRs0jqlyHAq6pefWYLw0XycS/PxKOQzt2gk7+NwNbXaqTVBXWUkUD6fEYqn/aEYu+BDi+UfMWeH9tSO//pEY0Nrlkw3eAVr8Ieofk69bZ0ZEG0l9FS5gO/aOqpo58S2kG6qvsXlqcIil9wJZ7GnOsAlHPyypW3DDatnvhkHzQ6gazOdoTuM90XLBh31R/ynFbz+c+8UVGxl/xV8CK8DDzbbyytg64we3Zhk0uytiwDZvtAxsMHqAcMVu2ln8Tlobm9ZHFLFlbSNnw4JzsJu74CY3QO1aq8LPCKiV32tFa04cXBSYtTGFiGiBLedQ845uwPmZlKQ1tTjbnpMtEtT6pLl3lnpySAFq87QBsiitexKs+XY04YGW4++gnXt8Y4ETCRw7d7qbe/3LjBv638ByzxtSkkQTqIa8czifAKpRNRww4tEBt5ncbeSRA/LAXMcfnT4SRH0ETCpBIR4wJ7OPW26nM0QFwpBmq3wJJRB6Utkh19vXTOh1nG0hIqaFNFusMH99uqT2pUd/phGPm/2+N1AL4QP7IPXVvEDt/kTz800OWQ4IXj/tgbxSjOLqQGQXSe5TNOiHunmKqAtgm4swZcFQn01XPqfOPZfaoklqybjhl0I7nIFY8Fte3jCntOXxT2K/u7j2+5Lsq/N4geD+UbE0LA5D9dRGQPRQ59P2/4XNMEu/SSD1Xiv4VobNosBeliR44h3H37Kv5EPOOSbUHjJ1bJrPdr+KTvajyQUFUDpq30SC6ysV8RYSyzZ01lVCJ5qey7Nrf4aNWN6cTYNBUfJAAkVkanxsqKZnmfRQ9mhgOeAd6Q9WdYQQNjsfxJb1Hu7CJU3F2+cHBFAbpq/Yjgb2ugk3TAvIpngfZ3IdD+HJm3K6iU2sxQT7G44ers/SEdiNhn/5XjUyghrkTPN6JTZ3BckhAqn8keKeEIXLNd19txKjlskEK93MtkfPn1Ht/n6vlTGCNTRQ/ge647Qg9viIP6kN47wUhviB5IfM5dGGOy5xpqqjer6E0mU5w2fr/AjVqiLKN5fUTUpXfFHq6J6JQGoYayIqQ8m5hnh0SfYgaSQMkxiISBbTXaQLqMeW9zCTnQu/HPeUuyG07x6XT+89VEZKLO/aLLANbkhivmi2nD3DVhSi24es1VgKtQkZ9pEzIzBLPSdLmSSTtel34hgVP4YrF/MelwIMhFLvwbfxMtkPRXKBckMSYOJPROB3zqWdTonRlMnnwQjPrB7aRsJAkiP0DxcBLTlQ1NbGx3I/cF7uIkxUPcr03SYKo6kjn1hHvTvoDUR2aqEvjOVXzYOnTkiR3db9bvNv9g8l4ZgdoPBxpMZIQKOw3lsiK7lKqBATMVuoVrkex5d7ElCJzm+uoW8HbWEA+vfcxMl31ng84JxtZK73mbx05A2A3LyLgQVTpcfLRM/aROvH7Xqypyhuy1MpaQJwYPsoXHzltoE9TICX8Ayj4jO2ssWVMp0sYsiP94U8v/BgDpi4dNstwQqyqCmtGBlVb60R5v4Wapw/audmy5HuVd8lBrmodR+V7+NM3pvNFcR4SC30EUSr/yFKK+IZu63KW3Vg7HKtLD4f2EAR391vMGNiMxYUR6gwW9Tlzfy+s/rmZyfhJq8VtNsOBXPmOZv4OvisjGulLwHhFqSX3roeL5242PTVUlD2us5QHpQ== X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: OH+4UOlHgbomePLJI4BjFc8cih4JfJSfzhYvA4qYXNSaF4yGqsL3UrnQ0hFohxYEEf9EulojcV9ES3+st6FLj0TpjhwC/RjovU5Dr3Xzj4cjCsmNYr6PxKaWTumfPPMoIptUXtpGaVi7kH3uNtLtFW342/0gXdHPwAj/9ijSWLUCNRZXRm3C+rsbn30bcCbj5XGVSiXhk0ovVBuhN0tXaZ6K9VxSxlNvtRGUxtaIReklDJ0JxKoGpgUzr0MNssCBa0efz4B4KU0l1vkSd/pHDjCy0cEOIdZPJ8aQTqlTrM5UrwmoO3/VMornX2wnp3MaT8DvjuhPnRmOFEXmqBQxa7rRchVERbnBta/Yh7VmYrkfJNZG0rawE74cFLTKGtQOzHZmGSVtIjaEM55/xWcT1eD/G7LYsjpuQqKBfN5/5CFq6X5rBp2f7NFG5y7m2EBTPchzKqkl8obzOTByIhr+hl84JhxMkdOuKu3ZBF4j5NnIRKwDDqPKo+gzTxupVymVvf+2Wa7//WD2uwrJg7EVBK9R/RHDjZ0thb3LYsY93pZgKwAGIMj5xHnVq13RYnlkyIG1Vl5fqD4/D7oquJl4Hj1mbaISbePYP/XMB30fWkc= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 280919a5-979e-43c6-56ff-08dcf37f4446 X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB3366.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 23 Oct 2024 16:25:11.4393 (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: oXMunrZi9YOONMbSAJIYwaLdItp1p0jj/wDGWyABVKbP90tkYdlag5n531nJb7YuNXtjPsddaGUTBc081ULxTunj9pGIvlEUPueLeS34UmM= X-MS-Exchange-Transport-CrossTenantHeadersStamped: LV3PR10MB7771 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1051,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-10-23_13,2024-10-23_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 malwarescore=0 adultscore=0 mlxlogscore=999 mlxscore=0 bulkscore=0 spamscore=0 phishscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2410230095 X-Proofpoint-ORIG-GUID: 7yKagatgMFEDx3YbE58izDUaabkKzlY6 X-Proofpoint-GUID: 7yKagatgMFEDx3YbE58izDUaabkKzlY6 Utilise the kselftest harmness to implement tests for the guard page implementation. We start by implement basic tests asserting that guard pages can be installed, removed and that touching guard pages result in SIGSEGV. We also assert that, in removing guard pages from a range, non-guard pages remain intact. We then examine different operations on regions containing guard markers behave to ensure correct behaviour: * Operations over multiple VMAs operate as expected. * Invoking MADV_GUARD_INSTALL / MADV_GUARD_REMOVE via process_madvise() in batches works correctly. * Ensuring that munmap() correctly tears down guard markers. * Using mprotect() to adjust protection bits does not in any way override or cause issues with guard markers. * Ensuring that splitting and merging VMAs around guard markers causes no issue - i.e. that a marker which 'belongs' to one VMA can function just as well 'belonging' to another. * Ensuring that madvise(..., MADV_DONTNEED) and madvise(..., MADV_FREE) do not remove guard markers. * Ensuring that mlock()'ing a range containing guard markers does not cause issues. * Ensuring that mremap() can move a guard range and retain guard markers. * Ensuring that mremap() can expand a guard range and retain guard markers (perhaps moving the range). * Ensuring that mremap() can shrink a guard range and retain guard markers. * Ensuring that forking a process correctly retains guard markers. * Ensuring that forking a VMA with VM_WIPEONFORK set behaves sanely. * Ensuring that lazyfree simply clears guard markers. * Ensuring that userfaultfd can co-exist with guard pages. * Ensuring that madvise(..., MADV_POPULATE_READ) and madvise(..., MADV_POPULATE_WRITE) error out when encountering guard markers. * Ensuring that madvise(..., MADV_COLD) and madvise(..., MADV_PAGEOUT) do not remove guard markers. If any test is unable to be run due to lack of permissions, that test is skipped. Reviewed-by: Shuah Khan Signed-off-by: Lorenzo Stoakes --- tools/testing/selftests/mm/.gitignore | 1 + tools/testing/selftests/mm/Makefile | 1 + tools/testing/selftests/mm/guard-pages.c | 1239 ++++++++++++++++++++++ 3 files changed, 1241 insertions(+) create mode 100644 tools/testing/selftests/mm/guard-pages.c diff --git a/tools/testing/selftests/mm/.gitignore b/tools/testing/selftests/mm/.gitignore index 689bbd520296..8f01f4da1c0d 100644 --- a/tools/testing/selftests/mm/.gitignore +++ b/tools/testing/selftests/mm/.gitignore @@ -54,3 +54,4 @@ droppable hugetlb_dio pkey_sighandler_tests_32 pkey_sighandler_tests_64 +guard-pages diff --git a/tools/testing/selftests/mm/Makefile b/tools/testing/selftests/mm/Makefile index 02e1204971b0..15c734d6cfec 100644 --- a/tools/testing/selftests/mm/Makefile +++ b/tools/testing/selftests/mm/Makefile @@ -79,6 +79,7 @@ TEST_GEN_FILES += hugetlb_fault_after_madv TEST_GEN_FILES += hugetlb_madv_vs_map TEST_GEN_FILES += hugetlb_dio TEST_GEN_FILES += droppable +TEST_GEN_FILES += guard-pages ifneq ($(ARCH),arm64) TEST_GEN_FILES += soft-dirty diff --git a/tools/testing/selftests/mm/guard-pages.c b/tools/testing/selftests/mm/guard-pages.c new file mode 100644 index 000000000000..7db9c913e9db --- /dev/null +++ b/tools/testing/selftests/mm/guard-pages.c @@ -0,0 +1,1239 @@ +// SPDX-License-Identifier: GPL-2.0-or-later + +#define _GNU_SOURCE +#include "../kselftest_harness.h" +#include /* Force the import of the tools version. */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* + * Ignore the checkpatch warning, as per the C99 standard, section 7.14.1.1: + * + * "If the signal occurs other than as the result of calling the abort or raise + * function, the behavior is undefined if the signal handler refers to any + * object with static storage duration other than by assigning a value to an + * object declared as volatile sig_atomic_t" + */ +static volatile sig_atomic_t signal_jump_set; +static sigjmp_buf signal_jmp_buf; + +/* + * Ignore the checkpatch warning, we must read from x but don't want to do + * anything with it in order to trigger a read page fault. We therefore must use + * volatile to stop the compiler from optimising this away. + */ +#define FORCE_READ(x) (*(volatile typeof(x) *)x) + +static int userfaultfd(int flags) +{ + return syscall(SYS_userfaultfd, flags); +} + +static void handle_fatal(int c) +{ + if (!signal_jump_set) + return; + + siglongjmp(signal_jmp_buf, c); +} + +static int pidfd_open(pid_t pid, unsigned int flags) +{ + return syscall(SYS_pidfd_open, pid, flags); +} + +/* + * Enable our signal catcher and try to read/write the specified buffer. The + * return value indicates whether the read/write succeeds without a fatal + * signal. + */ +static bool try_access_buf(char *ptr, bool write) +{ + bool failed; + + /* Tell signal handler to jump back here on fatal signal. */ + signal_jump_set = true; + /* If a fatal signal arose, we will jump back here and failed is set. */ + failed = sigsetjmp(signal_jmp_buf, 0) != 0; + + if (!failed) { + if (write) + *ptr = 'x'; + else + FORCE_READ(ptr); + } + + signal_jump_set = false; + return !failed; +} + +/* Try and read from a buffer, return true if no fatal signal. */ +static bool try_read_buf(char *ptr) +{ + return try_access_buf(ptr, false); +} + +/* Try and write to a buffer, return true if no fatal signal. */ +static bool try_write_buf(char *ptr) +{ + return try_access_buf(ptr, true); +} + +/* + * Try and BOTH read from AND write to a buffer, return true if BOTH operations + * succeed. + */ +static bool try_read_write_buf(char *ptr) +{ + return try_read_buf(ptr) && try_write_buf(ptr); +} + +FIXTURE(guard_pages) +{ + unsigned long page_size; +}; + +FIXTURE_SETUP(guard_pages) +{ + struct sigaction act = { + .sa_handler = &handle_fatal, + .sa_flags = SA_NODEFER, + }; + + sigemptyset(&act.sa_mask); + if (sigaction(SIGSEGV, &act, NULL)) + ksft_exit_fail_perror("sigaction"); + + self->page_size = (unsigned long)sysconf(_SC_PAGESIZE); +}; + +FIXTURE_TEARDOWN(guard_pages) +{ + struct sigaction act = { + .sa_handler = SIG_DFL, + .sa_flags = SA_NODEFER, + }; + + sigemptyset(&act.sa_mask); + sigaction(SIGSEGV, &act, NULL); +} + +TEST_F(guard_pages, basic) +{ + const unsigned long NUM_PAGES = 10; + const unsigned long page_size = self->page_size; + char *ptr; + int i; + + ptr = mmap(NULL, NUM_PAGES * page_size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANON, -1, 0); + ASSERT_NE(ptr, MAP_FAILED); + + /* Trivially assert we can touch the first page. */ + ASSERT_TRUE(try_read_write_buf(ptr)); + + ASSERT_EQ(madvise(ptr, page_size, MADV_GUARD_INSTALL), 0); + + /* Establish that 1st page SIGSEGV's. */ + ASSERT_FALSE(try_read_write_buf(ptr)); + + /* Ensure we can touch everything else.*/ + for (i = 1; i < NUM_PAGES; i++) { + char *curr = &ptr[i * page_size]; + + ASSERT_TRUE(try_read_write_buf(curr)); + } + + /* Establish a guard page at the end of the mapping. */ + ASSERT_EQ(madvise(&ptr[(NUM_PAGES - 1) * page_size], page_size, + MADV_GUARD_INSTALL), 0); + + /* Check that both guard pages result in SIGSEGV. */ + ASSERT_FALSE(try_read_write_buf(ptr)); + ASSERT_FALSE(try_read_write_buf(&ptr[(NUM_PAGES - 1) * page_size])); + + /* Remove the first guard page. */ + ASSERT_FALSE(madvise(ptr, page_size, MADV_GUARD_REMOVE)); + + /* Make sure we can touch it. */ + ASSERT_TRUE(try_read_write_buf(ptr)); + + /* Remove the last guard page. */ + ASSERT_FALSE(madvise(&ptr[(NUM_PAGES - 1) * page_size], page_size, + MADV_GUARD_REMOVE)); + + /* Make sure we can touch it. */ + ASSERT_TRUE(try_read_write_buf(&ptr[(NUM_PAGES - 1) * page_size])); + + /* + * Test setting a _range_ of pages, namely the first 3. The first of + * these be faulted in, so this also tests that we can install guard + * pages over backed pages. + */ + ASSERT_EQ(madvise(ptr, 3 * page_size, MADV_GUARD_INSTALL), 0); + + /* Make sure they are all guard pages. */ + for (i = 0; i < 3; i++) { + char *curr = &ptr[i * page_size]; + + ASSERT_FALSE(try_read_write_buf(curr)); + } + + /* Make sure the rest are not. */ + for (i = 3; i < NUM_PAGES; i++) { + char *curr = &ptr[i * page_size]; + + ASSERT_TRUE(try_read_write_buf(curr)); + } + + /* Remove guard pages. */ + ASSERT_EQ(madvise(ptr, NUM_PAGES * page_size, MADV_GUARD_REMOVE), 0); + + /* Now make sure we can touch everything. */ + for (i = 0; i < NUM_PAGES; i++) { + char *curr = &ptr[i * page_size]; + + ASSERT_TRUE(try_read_write_buf(curr)); + } + + /* + * Now remove all guard pages, make sure we don't remove existing + * entries. + */ + ASSERT_EQ(madvise(ptr, NUM_PAGES * page_size, MADV_GUARD_REMOVE), 0); + + for (i = 0; i < NUM_PAGES * page_size; i += page_size) { + char chr = ptr[i]; + + ASSERT_EQ(chr, 'x'); + } + + ASSERT_EQ(munmap(ptr, NUM_PAGES * page_size), 0); +} + +/* Assert that operations applied across multiple VMAs work as expected. */ +TEST_F(guard_pages, multi_vma) +{ + const unsigned long page_size = self->page_size; + char *ptr_region, *ptr, *ptr1, *ptr2, *ptr3; + int i; + + /* Reserve a 100 page region over which we can install VMAs. */ + ptr_region = mmap(NULL, 100 * page_size, PROT_NONE, + MAP_ANON | MAP_PRIVATE, -1, 0); + ASSERT_NE(ptr_region, MAP_FAILED); + + /* Place a VMA of 10 pages size at the start of the region. */ + ptr1 = mmap(ptr_region, 10 * page_size, PROT_READ | PROT_WRITE, + MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0); + ASSERT_NE(ptr1, MAP_FAILED); + + /* Place a VMA of 5 pages size 50 pages into the region. */ + ptr2 = mmap(&ptr_region[50 * page_size], 5 * page_size, + PROT_READ | PROT_WRITE, + MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0); + ASSERT_NE(ptr2, MAP_FAILED); + + /* Place a VMA of 20 pages size at the end of the region. */ + ptr3 = mmap(&ptr_region[80 * page_size], 20 * page_size, + PROT_READ | PROT_WRITE, + MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0); + ASSERT_NE(ptr3, MAP_FAILED); + + /* Unmap gaps. */ + ASSERT_EQ(munmap(&ptr_region[10 * page_size], 40 * page_size), 0); + ASSERT_EQ(munmap(&ptr_region[55 * page_size], 25 * page_size), 0); + + /* + * We end up with VMAs like this: + * + * 0 10 .. 50 55 .. 80 100 + * [---] [---] [---] + */ + + /* + * Now mark the whole range as guard pages and make sure all VMAs are as + * such. + */ + + /* + * madvise() is certifiable and lets you perform operations over gaps, + * everything works, but it indicates an error and errno is set to + * -ENOMEM. Also if anything runs out of memory it is set to + * -ENOMEM. You are meant to guess which is which. + */ + ASSERT_EQ(madvise(ptr_region, 100 * page_size, MADV_GUARD_INSTALL), -1); + ASSERT_EQ(errno, ENOMEM); + + for (i = 0; i < 10; i++) { + char *curr = &ptr1[i * page_size]; + + ASSERT_FALSE(try_read_write_buf(curr)); + } + + for (i = 0; i < 5; i++) { + char *curr = &ptr2[i * page_size]; + + ASSERT_FALSE(try_read_write_buf(curr)); + } + + for (i = 0; i < 20; i++) { + char *curr = &ptr3[i * page_size]; + + ASSERT_FALSE(try_read_write_buf(curr)); + } + + /* Now remove guar pages over range and assert the opposite. */ + + ASSERT_EQ(madvise(ptr_region, 100 * page_size, MADV_GUARD_REMOVE), -1); + ASSERT_EQ(errno, ENOMEM); + + for (i = 0; i < 10; i++) { + char *curr = &ptr1[i * page_size]; + + ASSERT_TRUE(try_read_write_buf(curr)); + } + + for (i = 0; i < 5; i++) { + char *curr = &ptr2[i * page_size]; + + ASSERT_TRUE(try_read_write_buf(curr)); + } + + for (i = 0; i < 20; i++) { + char *curr = &ptr3[i * page_size]; + + ASSERT_TRUE(try_read_write_buf(curr)); + } + + /* Now map incompatible VMAs in the gaps. */ + ptr = mmap(&ptr_region[10 * page_size], 40 * page_size, + PROT_READ | PROT_WRITE | PROT_EXEC, + MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0); + ASSERT_NE(ptr, MAP_FAILED); + ptr = mmap(&ptr_region[55 * page_size], 25 * page_size, + PROT_READ | PROT_WRITE | PROT_EXEC, + MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0); + ASSERT_NE(ptr, MAP_FAILED); + + /* + * We end up with VMAs like this: + * + * 0 10 .. 50 55 .. 80 100 + * [---][xxxx][---][xxxx][---] + * + * Where 'x' signifies VMAs that cannot be merged with those adjacent to + * them. + */ + + /* Multiple VMAs adjacent to one another should result in no error. */ + ASSERT_EQ(madvise(ptr_region, 100 * page_size, MADV_GUARD_INSTALL), 0); + for (i = 0; i < 100; i++) { + char *curr = &ptr_region[i * page_size]; + + ASSERT_FALSE(try_read_write_buf(curr)); + } + ASSERT_EQ(madvise(ptr_region, 100 * page_size, MADV_GUARD_REMOVE), 0); + for (i = 0; i < 100; i++) { + char *curr = &ptr_region[i * page_size]; + + ASSERT_TRUE(try_read_write_buf(curr)); + } + + /* Cleanup. */ + ASSERT_EQ(munmap(ptr_region, 100 * page_size), 0); +} + +/* + * Assert that batched operations performed using process_madvise() work as + * expected. + */ +TEST_F(guard_pages, process_madvise) +{ + const unsigned long page_size = self->page_size; + pid_t pid = getpid(); + int pidfd = pidfd_open(pid, 0); + char *ptr_region, *ptr1, *ptr2, *ptr3; + ssize_t count; + struct iovec vec[6]; + + ASSERT_NE(pidfd, -1); + + /* Reserve region to map over. */ + ptr_region = mmap(NULL, 100 * page_size, PROT_NONE, + MAP_ANON | MAP_PRIVATE, -1, 0); + ASSERT_NE(ptr_region, MAP_FAILED); + + /* 10 pages offset 1 page into reserve region. */ + ptr1 = mmap(&ptr_region[page_size], 10 * page_size, + PROT_READ | PROT_WRITE, + MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0); + ASSERT_NE(ptr1, MAP_FAILED); + /* We want guard markers at start/end of each VMA. */ + vec[0].iov_base = ptr1; + vec[0].iov_len = page_size; + vec[1].iov_base = &ptr1[9 * page_size]; + vec[1].iov_len = page_size; + + /* 5 pages offset 50 pages into reserve region. */ + ptr2 = mmap(&ptr_region[50 * page_size], 5 * page_size, + PROT_READ | PROT_WRITE, + MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0); + ASSERT_NE(ptr2, MAP_FAILED); + vec[2].iov_base = ptr2; + vec[2].iov_len = page_size; + vec[3].iov_base = &ptr2[4 * page_size]; + vec[3].iov_len = page_size; + + /* 20 pages offset 79 pages into reserve region. */ + ptr3 = mmap(&ptr_region[79 * page_size], 20 * page_size, + PROT_READ | PROT_WRITE, + MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0); + ASSERT_NE(ptr3, MAP_FAILED); + vec[4].iov_base = ptr3; + vec[4].iov_len = page_size; + vec[5].iov_base = &ptr3[19 * page_size]; + vec[5].iov_len = page_size; + + /* Free surrounding VMAs. */ + ASSERT_EQ(munmap(ptr_region, page_size), 0); + ASSERT_EQ(munmap(&ptr_region[11 * page_size], 39 * page_size), 0); + ASSERT_EQ(munmap(&ptr_region[55 * page_size], 24 * page_size), 0); + ASSERT_EQ(munmap(&ptr_region[99 * page_size], page_size), 0); + + /* Now guard in one step. */ + count = process_madvise(pidfd, vec, 6, MADV_GUARD_INSTALL, 0); + + /* OK we don't have permission to do this, skip. */ + if (count == -1 && errno == EPERM) + ksft_exit_skip("No process_madvise() permissions, try running as root.\n"); + + /* Returns the number of bytes advised. */ + ASSERT_EQ(count, 6 * page_size); + + /* Now make sure the guarding was applied. */ + + ASSERT_FALSE(try_read_write_buf(ptr1)); + ASSERT_FALSE(try_read_write_buf(&ptr1[9 * page_size])); + + ASSERT_FALSE(try_read_write_buf(ptr2)); + ASSERT_FALSE(try_read_write_buf(&ptr2[4 * page_size])); + + ASSERT_FALSE(try_read_write_buf(ptr3)); + ASSERT_FALSE(try_read_write_buf(&ptr3[19 * page_size])); + + /* Now do the same with unguard... */ + count = process_madvise(pidfd, vec, 6, MADV_GUARD_REMOVE, 0); + + /* ...and everything should now succeed. */ + + ASSERT_TRUE(try_read_write_buf(ptr1)); + ASSERT_TRUE(try_read_write_buf(&ptr1[9 * page_size])); + + ASSERT_TRUE(try_read_write_buf(ptr2)); + ASSERT_TRUE(try_read_write_buf(&ptr2[4 * page_size])); + + ASSERT_TRUE(try_read_write_buf(ptr3)); + ASSERT_TRUE(try_read_write_buf(&ptr3[19 * page_size])); + + /* Cleanup. */ + ASSERT_EQ(munmap(ptr1, 10 * page_size), 0); + ASSERT_EQ(munmap(ptr2, 5 * page_size), 0); + ASSERT_EQ(munmap(ptr3, 20 * page_size), 0); + close(pidfd); +} + +/* Assert that unmapping ranges does not leave guard markers behind. */ +TEST_F(guard_pages, munmap) +{ + const unsigned long page_size = self->page_size; + char *ptr, *ptr_new1, *ptr_new2; + + ptr = mmap(NULL, 10 * page_size, PROT_READ | PROT_WRITE, + MAP_ANON | MAP_PRIVATE, -1, 0); + ASSERT_NE(ptr, MAP_FAILED); + + /* Guard first and last pages. */ + ASSERT_EQ(madvise(ptr, page_size, MADV_GUARD_INSTALL), 0); + ASSERT_EQ(madvise(&ptr[9 * page_size], page_size, MADV_GUARD_INSTALL), 0); + + /* Assert that they are guarded. */ + ASSERT_FALSE(try_read_write_buf(ptr)); + ASSERT_FALSE(try_read_write_buf(&ptr[9 * page_size])); + + /* Unmap them. */ + ASSERT_EQ(munmap(ptr, page_size), 0); + ASSERT_EQ(munmap(&ptr[9 * page_size], page_size), 0); + + /* Map over them.*/ + ptr_new1 = mmap(ptr, page_size, PROT_READ | PROT_WRITE, + MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0); + ASSERT_NE(ptr_new1, MAP_FAILED); + ptr_new2 = mmap(&ptr[9 * page_size], page_size, PROT_READ | PROT_WRITE, + MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0); + ASSERT_NE(ptr_new2, MAP_FAILED); + + /* Assert that they are now not guarded. */ + ASSERT_TRUE(try_read_write_buf(ptr_new1)); + ASSERT_TRUE(try_read_write_buf(ptr_new2)); + + /* Cleanup. */ + ASSERT_EQ(munmap(ptr, 10 * page_size), 0); +} + +/* Assert that mprotect() operations have no bearing on guard markers. */ +TEST_F(guard_pages, mprotect) +{ + const unsigned long page_size = self->page_size; + char *ptr; + int i; + + ptr = mmap(NULL, 10 * page_size, PROT_READ | PROT_WRITE, + MAP_ANON | MAP_PRIVATE, -1, 0); + ASSERT_NE(ptr, MAP_FAILED); + + /* Guard the middle of the range. */ + ASSERT_EQ(madvise(&ptr[5 * page_size], 2 * page_size, + MADV_GUARD_INSTALL), 0); + + /* Assert that it is indeed guarded. */ + ASSERT_FALSE(try_read_write_buf(&ptr[5 * page_size])); + ASSERT_FALSE(try_read_write_buf(&ptr[6 * page_size])); + + /* Now make these pages read-only. */ + ASSERT_EQ(mprotect(&ptr[5 * page_size], 2 * page_size, PROT_READ), 0); + + /* Make sure the range is still guarded. */ + ASSERT_FALSE(try_read_buf(&ptr[5 * page_size])); + ASSERT_FALSE(try_read_buf(&ptr[6 * page_size])); + + /* Make sure we can guard again without issue.*/ + ASSERT_EQ(madvise(&ptr[5 * page_size], 2 * page_size, + MADV_GUARD_INSTALL), 0); + + /* Make sure the range is, yet again, still guarded. */ + ASSERT_FALSE(try_read_buf(&ptr[5 * page_size])); + ASSERT_FALSE(try_read_buf(&ptr[6 * page_size])); + + /* Now unguard the whole range. */ + ASSERT_EQ(madvise(ptr, 10 * page_size, MADV_GUARD_REMOVE), 0); + + /* Make sure the whole range is readable. */ + for (i = 0; i < 10; i++) { + char *curr = &ptr[i * page_size]; + + ASSERT_TRUE(try_read_buf(curr)); + } + + /* Cleanup. */ + ASSERT_EQ(munmap(ptr, 10 * page_size), 0); +} + +/* Split and merge VMAs and make sure guard pages still behave. */ +TEST_F(guard_pages, split_merge) +{ + const unsigned long page_size = self->page_size; + char *ptr, *ptr_new; + int i; + + ptr = mmap(NULL, 10 * page_size, PROT_READ | PROT_WRITE, + MAP_ANON | MAP_PRIVATE, -1, 0); + ASSERT_NE(ptr, MAP_FAILED); + + /* Guard the whole range. */ + ASSERT_EQ(madvise(ptr, 10 * page_size, MADV_GUARD_INSTALL), 0); + + /* Make sure the whole range is guarded. */ + for (i = 0; i < 10; i++) { + char *curr = &ptr[i * page_size]; + + ASSERT_FALSE(try_read_write_buf(curr)); + } + + /* Now unmap some pages in the range so we split. */ + ASSERT_EQ(munmap(&ptr[2 * page_size], page_size), 0); + ASSERT_EQ(munmap(&ptr[5 * page_size], page_size), 0); + ASSERT_EQ(munmap(&ptr[8 * page_size], page_size), 0); + + /* Make sure the remaining ranges are guarded post-split. */ + for (i = 0; i < 2; i++) { + char *curr = &ptr[i * page_size]; + + ASSERT_FALSE(try_read_write_buf(curr)); + } + for (i = 2; i < 5; i++) { + char *curr = &ptr[i * page_size]; + + ASSERT_FALSE(try_read_write_buf(curr)); + } + for (i = 6; i < 8; i++) { + char *curr = &ptr[i * page_size]; + + ASSERT_FALSE(try_read_write_buf(curr)); + } + for (i = 9; i < 10; i++) { + char *curr = &ptr[i * page_size]; + + ASSERT_FALSE(try_read_write_buf(curr)); + } + + /* Now map them again - the unmap will have cleared the guards. */ + ptr_new = mmap(&ptr[2 * page_size], page_size, PROT_READ | PROT_WRITE, + MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0); + ASSERT_NE(ptr_new, MAP_FAILED); + ptr_new = mmap(&ptr[5 * page_size], page_size, PROT_READ | PROT_WRITE, + MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0); + ASSERT_NE(ptr_new, MAP_FAILED); + ptr_new = mmap(&ptr[8 * page_size], page_size, PROT_READ | PROT_WRITE, + MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0); + ASSERT_NE(ptr_new, MAP_FAILED); + + /* Now make sure guard pages are established. */ + for (i = 0; i < 10; i++) { + char *curr = &ptr[i * page_size]; + bool result = try_read_write_buf(curr); + bool expect_true = i == 2 || i == 5 || i == 8; + + ASSERT_TRUE(expect_true ? result : !result); + } + + /* Now guard everything again. */ + ASSERT_EQ(madvise(ptr, 10 * page_size, MADV_GUARD_INSTALL), 0); + + /* Make sure the whole range is guarded. */ + for (i = 0; i < 10; i++) { + char *curr = &ptr[i * page_size]; + + ASSERT_FALSE(try_read_write_buf(curr)); + } + + /* Now split the range into three. */ + ASSERT_EQ(mprotect(ptr, 3 * page_size, PROT_READ), 0); + ASSERT_EQ(mprotect(&ptr[7 * page_size], 3 * page_size, PROT_READ), 0); + + /* Make sure the whole range is guarded for read. */ + for (i = 0; i < 10; i++) { + char *curr = &ptr[i * page_size]; + + ASSERT_FALSE(try_read_buf(curr)); + } + + /* Now reset protection bits so we merge the whole thing. */ + ASSERT_EQ(mprotect(ptr, 3 * page_size, PROT_READ | PROT_WRITE), 0); + ASSERT_EQ(mprotect(&ptr[7 * page_size], 3 * page_size, + PROT_READ | PROT_WRITE), 0); + + /* Make sure the whole range is still guarded. */ + for (i = 0; i < 10; i++) { + char *curr = &ptr[i * page_size]; + + ASSERT_FALSE(try_read_write_buf(curr)); + } + + /* Split range into 3 again... */ + ASSERT_EQ(mprotect(ptr, 3 * page_size, PROT_READ), 0); + ASSERT_EQ(mprotect(&ptr[7 * page_size], 3 * page_size, PROT_READ), 0); + + /* ...and unguard the whole range. */ + ASSERT_EQ(madvise(ptr, 10 * page_size, MADV_GUARD_REMOVE), 0); + + /* Make sure the whole range is remedied for read. */ + for (i = 0; i < 10; i++) { + char *curr = &ptr[i * page_size]; + + ASSERT_TRUE(try_read_buf(curr)); + } + + /* Merge them again. */ + ASSERT_EQ(mprotect(ptr, 3 * page_size, PROT_READ | PROT_WRITE), 0); + ASSERT_EQ(mprotect(&ptr[7 * page_size], 3 * page_size, + PROT_READ | PROT_WRITE), 0); + + /* Now ensure the merged range is remedied for read/write. */ + for (i = 0; i < 10; i++) { + char *curr = &ptr[i * page_size]; + + ASSERT_TRUE(try_read_write_buf(curr)); + } + + /* Cleanup. */ + ASSERT_EQ(munmap(ptr, 10 * page_size), 0); +} + +/* Assert that MADV_DONTNEED does not remove guard markers. */ +TEST_F(guard_pages, dontneed) +{ + const unsigned long page_size = self->page_size; + char *ptr; + int i; + + ptr = mmap(NULL, 10 * page_size, PROT_READ | PROT_WRITE, + MAP_ANON | MAP_PRIVATE, -1, 0); + ASSERT_NE(ptr, MAP_FAILED); + + /* Back the whole range. */ + for (i = 0; i < 10; i++) { + char *curr = &ptr[i * page_size]; + + *curr = 'y'; + } + + /* Guard every other page. */ + for (i = 0; i < 10; i += 2) { + char *curr = &ptr[i * page_size]; + int res = madvise(curr, page_size, MADV_GUARD_INSTALL); + + ASSERT_EQ(res, 0); + } + + /* Indicate that we don't need any of the range. */ + ASSERT_EQ(madvise(ptr, 10 * page_size, MADV_DONTNEED), 0); + + /* Check to ensure guard markers are still in place. */ + for (i = 0; i < 10; i++) { + char *curr = &ptr[i * page_size]; + bool result = try_read_buf(curr); + + if (i % 2 == 0) { + ASSERT_FALSE(result); + } else { + ASSERT_TRUE(result); + /* Make sure we really did get reset to zero page. */ + ASSERT_EQ(*curr, '\0'); + } + + /* Now write... */ + result = try_write_buf(&ptr[i * page_size]); + + /* ...and make sure same result. */ + ASSERT_TRUE(i % 2 != 0 ? result : !result); + } + + /* Cleanup. */ + ASSERT_EQ(munmap(ptr, 10 * page_size), 0); +} + +/* Assert that mlock()'ed pages work correctly with guard markers. */ +TEST_F(guard_pages, mlock) +{ + const unsigned long page_size = self->page_size; + char *ptr; + int i; + + ptr = mmap(NULL, 10 * page_size, PROT_READ | PROT_WRITE, + MAP_ANON | MAP_PRIVATE, -1, 0); + ASSERT_NE(ptr, MAP_FAILED); + + /* Populate. */ + for (i = 0; i < 10; i++) { + char *curr = &ptr[i * page_size]; + + *curr = 'y'; + } + + /* Lock. */ + ASSERT_EQ(mlock(ptr, 10 * page_size), 0); + + /* Now try to guard, should fail with EINVAL. */ + ASSERT_EQ(madvise(ptr, 10 * page_size, MADV_GUARD_INSTALL), -1); + ASSERT_EQ(errno, EINVAL); + + /* OK unlock. */ + ASSERT_EQ(munlock(ptr, 10 * page_size), 0); + + /* Guard first half of range, should now succeed. */ + ASSERT_EQ(madvise(ptr, 5 * page_size, MADV_GUARD_INSTALL), 0); + + /* Make sure guard works. */ + for (i = 0; i < 10; i++) { + char *curr = &ptr[i * page_size]; + bool result = try_read_write_buf(curr); + + if (i < 5) { + ASSERT_FALSE(result); + } else { + ASSERT_TRUE(result); + ASSERT_EQ(*curr, 'x'); + } + } + + /* + * Now lock the latter part of the range. We can't lock the guard pages, + * as this would result in the pages being populated and the guarding + * would cause this to error out. + */ + ASSERT_EQ(mlock(&ptr[5 * page_size], 5 * page_size), 0); + + /* + * Now remove guard pages, we permit mlock()'d ranges to have guard + * pages removed as it is a non-destructive operation. + */ + ASSERT_EQ(madvise(ptr, 10 * page_size, MADV_GUARD_REMOVE), 0); + + /* Now check that no guard pages remain. */ + for (i = 0; i < 10; i++) { + char *curr = &ptr[i * page_size]; + + ASSERT_TRUE(try_read_write_buf(curr)); + } + + /* Cleanup. */ + ASSERT_EQ(munmap(ptr, 10 * page_size), 0); +} + +/* + * Assert that moving, extending and shrinking memory via mremap() retains + * guard markers where possible. + * + * - Moving a mapping alone should retain markers as they are. + */ +TEST_F(guard_pages, mremap_move) +{ + const unsigned long page_size = self->page_size; + char *ptr, *ptr_new; + + /* Map 5 pages. */ + ptr = mmap(NULL, 5 * page_size, PROT_READ | PROT_WRITE, + MAP_ANON | MAP_PRIVATE, -1, 0); + ASSERT_NE(ptr, MAP_FAILED); + + /* Place guard markers at both ends of the 5 page span. */ + ASSERT_EQ(madvise(ptr, page_size, MADV_GUARD_INSTALL), 0); + ASSERT_EQ(madvise(&ptr[4 * page_size], page_size, MADV_GUARD_INSTALL), 0); + + /* Make sure the guard pages are in effect. */ + ASSERT_FALSE(try_read_write_buf(ptr)); + ASSERT_FALSE(try_read_write_buf(&ptr[4 * page_size])); + + /* Map a new region we will move this range into. Doing this ensures + * that we have reserved a range to map into. + */ + ptr_new = mmap(NULL, 5 * page_size, PROT_NONE, MAP_ANON | MAP_PRIVATE, + -1, 0); + ASSERT_NE(ptr_new, MAP_FAILED); + + ASSERT_EQ(mremap(ptr, 5 * page_size, 5 * page_size, + MREMAP_MAYMOVE | MREMAP_FIXED, ptr_new), ptr_new); + + /* Make sure the guard markers are retained. */ + ASSERT_FALSE(try_read_write_buf(ptr_new)); + ASSERT_FALSE(try_read_write_buf(&ptr_new[4 * page_size])); + + /* + * Clean up - we only need reference the new pointer as we overwrote the + * PROT_NONE range and moved the existing one. + */ + munmap(ptr_new, 5 * page_size); +} + +/* + * Assert that moving, extending and shrinking memory via mremap() retains + * guard markers where possible. + * + * Expanding should retain guard pages, only now in different position. The user + * will have to remove guard pages manually to fix up (they'd have to do the + * same if it were a PROT_NONE mapping). + */ +TEST_F(guard_pages, mremap_expand) +{ + const unsigned long page_size = self->page_size; + char *ptr, *ptr_new; + + /* Map 10 pages... */ + ptr = mmap(NULL, 10 * page_size, PROT_READ | PROT_WRITE, + MAP_ANON | MAP_PRIVATE, -1, 0); + ASSERT_NE(ptr, MAP_FAILED); + /* ...But unmap the last 5 so we can ensure we can expand into them. */ + ASSERT_EQ(munmap(&ptr[5 * page_size], 5 * page_size), 0); + + /* Place guard markers at both ends of the 5 page span. */ + ASSERT_EQ(madvise(ptr, page_size, MADV_GUARD_INSTALL), 0); + ASSERT_EQ(madvise(&ptr[4 * page_size], page_size, MADV_GUARD_INSTALL), 0); + + /* Make sure the guarding is in effect. */ + ASSERT_FALSE(try_read_write_buf(ptr)); + ASSERT_FALSE(try_read_write_buf(&ptr[4 * page_size])); + + /* Now expand to 10 pages. */ + ptr = mremap(ptr, 5 * page_size, 10 * page_size, 0); + ASSERT_NE(ptr, MAP_FAILED); + + /* + * Make sure the guard markers are retained in their original positions. + */ + ASSERT_FALSE(try_read_write_buf(ptr)); + ASSERT_FALSE(try_read_write_buf(&ptr[4 * page_size])); + + /* Reserve a region which we can move to and expand into. */ + ptr_new = mmap(NULL, 20 * page_size, PROT_NONE, + MAP_ANON | MAP_PRIVATE, -1, 0); + ASSERT_NE(ptr_new, MAP_FAILED); + + /* Now move and expand into it. */ + ptr = mremap(ptr, 10 * page_size, 20 * page_size, + MREMAP_MAYMOVE | MREMAP_FIXED, ptr_new); + ASSERT_EQ(ptr, ptr_new); + + /* + * Again, make sure the guard markers are retained in their original positions. + */ + ASSERT_FALSE(try_read_write_buf(ptr)); + ASSERT_FALSE(try_read_write_buf(&ptr[4 * page_size])); + + /* + * A real user would have to remove guard markers, but would reasonably + * expect all characteristics of the mapping to be retained, including + * guard markers. + */ + + /* Cleanup. */ + munmap(ptr, 20 * page_size); +} +/* + * Assert that moving, extending and shrinking memory via mremap() retains + * guard markers where possible. + * + * Shrinking will result in markers that are shrunk over being removed. Again, + * if the user were using a PROT_NONE mapping they'd have to manually fix this + * up also so this is OK. + */ +TEST_F(guard_pages, mremap_shrink) +{ + const unsigned long page_size = self->page_size; + char *ptr; + int i; + + /* Map 5 pages. */ + ptr = mmap(NULL, 5 * page_size, PROT_READ | PROT_WRITE, + MAP_ANON | MAP_PRIVATE, -1, 0); + ASSERT_NE(ptr, MAP_FAILED); + + /* Place guard markers at both ends of the 5 page span. */ + ASSERT_EQ(madvise(ptr, page_size, MADV_GUARD_INSTALL), 0); + ASSERT_EQ(madvise(&ptr[4 * page_size], page_size, MADV_GUARD_INSTALL), 0); + + /* Make sure the guarding is in effect. */ + ASSERT_FALSE(try_read_write_buf(ptr)); + ASSERT_FALSE(try_read_write_buf(&ptr[4 * page_size])); + + /* Now shrink to 3 pages. */ + ptr = mremap(ptr, 5 * page_size, 3 * page_size, MREMAP_MAYMOVE); + ASSERT_NE(ptr, MAP_FAILED); + + /* We expect the guard marker at the start to be retained... */ + ASSERT_FALSE(try_read_write_buf(ptr)); + + /* ...But remaining pages will not have guard markers. */ + for (i = 1; i < 3; i++) { + char *curr = &ptr[i * page_size]; + + ASSERT_TRUE(try_read_write_buf(curr)); + } + + /* + * As with expansion, a real user would have to remove guard pages and + * fixup. But you'd have to do similar manual things with PROT_NONE + * mappings too. + */ + + /* + * If we expand back to the original size, the end marker will, of + * course, no longer be present. + */ + ptr = mremap(ptr, 3 * page_size, 5 * page_size, 0); + ASSERT_NE(ptr, MAP_FAILED); + + /* Again, we expect the guard marker at the start to be retained... */ + ASSERT_FALSE(try_read_write_buf(ptr)); + + /* ...But remaining pages will not have guard markers. */ + for (i = 1; i < 5; i++) { + char *curr = &ptr[i * page_size]; + + ASSERT_TRUE(try_read_write_buf(curr)); + } + + /* Cleanup. */ + munmap(ptr, 5 * page_size); +} + +/* + * Assert that forking a process with VMAs that do not have VM_WIPEONFORK set + * retain guard pages. + */ +TEST_F(guard_pages, fork) +{ + const unsigned long page_size = self->page_size; + char *ptr; + pid_t pid; + int i; + + /* Map 10 pages. */ + ptr = mmap(NULL, 10 * page_size, PROT_READ | PROT_WRITE, + MAP_ANON | MAP_PRIVATE, -1, 0); + ASSERT_NE(ptr, MAP_FAILED); + + /* Establish guard apges in the first 5 pages. */ + ASSERT_EQ(madvise(ptr, 5 * page_size, MADV_GUARD_INSTALL), 0); + + pid = fork(); + ASSERT_NE(pid, -1); + if (!pid) { + /* This is the child process now. */ + + /* Assert that the guarding is in effect. */ + for (i = 0; i < 10; i++) { + char *curr = &ptr[i * page_size]; + bool result = try_read_write_buf(curr); + + ASSERT_TRUE(i >= 5 ? result : !result); + } + + /* Now unguard the range.*/ + ASSERT_EQ(madvise(ptr, 10 * page_size, MADV_GUARD_REMOVE), 0); + + exit(0); + } + + /* Parent process. */ + + /* Parent simply waits on child. */ + waitpid(pid, NULL, 0); + + /* Child unguard does not impact parent page table state. */ + for (i = 0; i < 10; i++) { + char *curr = &ptr[i * page_size]; + bool result = try_read_write_buf(curr); + + ASSERT_TRUE(i >= 5 ? result : !result); + } + + /* Cleanup. */ + ASSERT_EQ(munmap(ptr, 10 * page_size), 0); +} + +/* + * Assert that forking a process with VMAs that do have VM_WIPEONFORK set + * behave as expected. + */ +TEST_F(guard_pages, fork_wipeonfork) +{ + const unsigned long page_size = self->page_size; + char *ptr; + pid_t pid; + int i; + + /* Map 10 pages. */ + ptr = mmap(NULL, 10 * page_size, PROT_READ | PROT_WRITE, + MAP_ANON | MAP_PRIVATE, -1, 0); + ASSERT_NE(ptr, MAP_FAILED); + + /* Mark wipe on fork. */ + ASSERT_EQ(madvise(ptr, 10 * page_size, MADV_WIPEONFORK), 0); + + /* Guard the first 5 pages. */ + ASSERT_EQ(madvise(ptr, 5 * page_size, MADV_GUARD_INSTALL), 0); + + pid = fork(); + ASSERT_NE(pid, -1); + if (!pid) { + /* This is the child process now. */ + + /* Guard will have been wiped. */ + for (i = 0; i < 10; i++) { + char *curr = &ptr[i * page_size]; + + ASSERT_TRUE(try_read_write_buf(curr)); + } + + exit(0); + } + + /* Parent process. */ + + waitpid(pid, NULL, 0); + + /* Guard markers should be in effect.*/ + for (i = 0; i < 10; i++) { + char *curr = &ptr[i * page_size]; + bool result = try_read_write_buf(curr); + + ASSERT_TRUE(i >= 5 ? result : !result); + } + + /* Cleanup. */ + ASSERT_EQ(munmap(ptr, 10 * page_size), 0); +} + +/* Ensure that MADV_FREE retains guard entries as expected. */ +TEST_F(guard_pages, lazyfree) +{ + const unsigned long page_size = self->page_size; + char *ptr; + int i; + + /* Map 10 pages. */ + ptr = mmap(NULL, 10 * page_size, PROT_READ | PROT_WRITE, + MAP_ANON | MAP_PRIVATE, -1, 0); + ASSERT_NE(ptr, MAP_FAILED); + + /* Guard range. */ + ASSERT_EQ(madvise(ptr, 10 * page_size, MADV_GUARD_INSTALL), 0); + + /* Ensure guarded. */ + for (i = 0; i < 10; i++) { + char *curr = &ptr[i * page_size]; + + ASSERT_FALSE(try_read_write_buf(curr)); + } + + /* Lazyfree range. */ + ASSERT_EQ(madvise(ptr, 10 * page_size, MADV_FREE), 0); + + /* This should leave the guard markers in place. */ + for (i = 0; i < 10; i++) { + char *curr = &ptr[i * page_size]; + + ASSERT_FALSE(try_read_write_buf(curr)); + } + + /* Cleanup. */ + ASSERT_EQ(munmap(ptr, 10 * page_size), 0); +} + +/* Ensure that MADV_POPULATE_READ, MADV_POPULATE_WRITE behave as expected. */ +TEST_F(guard_pages, populate) +{ + const unsigned long page_size = self->page_size; + char *ptr; + + /* Map 10 pages. */ + ptr = mmap(NULL, 10 * page_size, PROT_READ | PROT_WRITE, + MAP_ANON | MAP_PRIVATE, -1, 0); + ASSERT_NE(ptr, MAP_FAILED); + + /* Guard range. */ + ASSERT_EQ(madvise(ptr, 10 * page_size, MADV_GUARD_INSTALL), 0); + + /* Populate read should error out... */ + ASSERT_EQ(madvise(ptr, 10 * page_size, MADV_POPULATE_READ), -1); + ASSERT_EQ(errno, EFAULT); + + /* ...as should populate write. */ + ASSERT_EQ(madvise(ptr, 10 * page_size, MADV_POPULATE_WRITE), -1); + ASSERT_EQ(errno, EFAULT); + + /* Cleanup. */ + ASSERT_EQ(munmap(ptr, 10 * page_size), 0); +} + +/* Ensure that MADV_COLD, MADV_PAGEOUT do not remove guard markers. */ +TEST_F(guard_pages, cold_pageout) +{ + const unsigned long page_size = self->page_size; + char *ptr; + int i; + + /* Map 10 pages. */ + ptr = mmap(NULL, 10 * page_size, PROT_READ | PROT_WRITE, + MAP_ANON | MAP_PRIVATE, -1, 0); + ASSERT_NE(ptr, MAP_FAILED); + + /* Guard range. */ + ASSERT_EQ(madvise(ptr, 10 * page_size, MADV_GUARD_INSTALL), 0); + + /* Ensured guarded. */ + for (i = 0; i < 10; i++) { + char *curr = &ptr[i * page_size]; + + ASSERT_FALSE(try_read_write_buf(curr)); + } + + /* Now mark cold. This should have no impact on guard markers. */ + ASSERT_EQ(madvise(ptr, 10 * page_size, MADV_COLD), 0); + + /* Should remain guarded. */ + for (i = 0; i < 10; i++) { + char *curr = &ptr[i * page_size]; + + ASSERT_FALSE(try_read_write_buf(curr)); + } + + /* OK, now page out. This should equally, have no effect on markers. */ + ASSERT_EQ(madvise(ptr, 10 * page_size, MADV_PAGEOUT), 0); + + /* Should remain guarded. */ + for (i = 0; i < 10; i++) { + char *curr = &ptr[i * page_size]; + + ASSERT_FALSE(try_read_write_buf(curr)); + } + + /* Cleanup. */ + ASSERT_EQ(munmap(ptr, 10 * page_size), 0); +} + +/* Ensure that guard pages do not break userfaultd. */ +TEST_F(guard_pages, uffd) +{ + const unsigned long page_size = self->page_size; + int uffd; + char *ptr; + int i; + struct uffdio_api api = { + .api = UFFD_API, + .features = 0, + }; + struct uffdio_register reg; + struct uffdio_range range; + + /* Set up uffd. */ + uffd = userfaultfd(0); + if (uffd == -1 && errno == EPERM) + ksft_exit_skip("No userfaultfd permissions, try running as root.\n"); + ASSERT_NE(uffd, -1); + + ASSERT_EQ(ioctl(uffd, UFFDIO_API, &api), 0); + + /* Map 10 pages. */ + ptr = mmap(NULL, 10 * page_size, PROT_READ | PROT_WRITE, + MAP_ANON | MAP_PRIVATE, -1, 0); + ASSERT_NE(ptr, MAP_FAILED); + + /* Register the range with uffd. */ + range.start = (unsigned long)ptr; + range.len = 10 * page_size; + reg.range = range; + reg.mode = UFFDIO_REGISTER_MODE_MISSING; + ASSERT_EQ(ioctl(uffd, UFFDIO_REGISTER, ®), 0); + + /* Guard the range. This should not trigger the uffd. */ + ASSERT_EQ(madvise(ptr, 10 * page_size, MADV_GUARD_INSTALL), 0); + + /* The guarding should behave as usual with no uffd intervention. */ + for (i = 0; i < 10; i++) { + char *curr = &ptr[i * page_size]; + + ASSERT_FALSE(try_read_write_buf(curr)); + } + + /* Cleanup. */ + ASSERT_EQ(ioctl(uffd, UFFDIO_UNREGISTER, &range), 0); + close(uffd); + ASSERT_EQ(munmap(ptr, 10 * page_size), 0); +} + +TEST_HARNESS_MAIN