From nobody Tue Nov 26 06:38:59 2024 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 98A63194AD5; Sun, 20 Oct 2024 16:21:12 +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=1729441275; cv=fail; b=KHldr/YL42X4LNYTBAulnWI/IkVgp7R8xXzyPFZBEB/QjqP2yCrjjLP/ayfaDsqcSAj00hxhA5bCwWRIfr10wNuPBM1Ow2uLpkZvwx5s2Su9eD+jcIuvaXXa53UWeg6TA4NeGrvjF9X2gYlQ8WBVs+w2GatGl2AaQ1NtA3FondA= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729441275; c=relaxed/simple; bh=imwnoMZ5P2Wy2dxWIQWvxWeGPLqzt7jcTPr4NJQozaE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=lI8T9m+2GTOwMFKHV4+C+g9cIotoS19MN6o3LbE0arcghXTqTiER0VD5Cok9afgYKGiqJI+GLseYSytHJVNlRtdAUSDsdbdbH5wm5hLCno418dwHKXuJWgLToxxIzUdu+cri9wB8+VFere8NWzWzBcwYZMLnYmOYWVbkUTrYZIc= 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=DqtvXDzy; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=YAsb/lpQ; 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="DqtvXDzy"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="YAsb/lpQ" Received: from pps.filterd (m0246632.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 49KDC9IH011703; Sun, 20 Oct 2024 16:20:23 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=Vd2Vp6szhFvYTVsLkfnXlXF7Mkv+sqGaCcSK4gFh5M0=; b= DqtvXDzyMJdNvzJ4/b3t1NxBm4DvIcwh/RoDYAIoaT+46dhVBg+FN1JjkGp5IxJw q6sp3+h+4cqfkKFmyV4V1PIMPL25TdJh0gmOM4qq0CNTrsGJ4lFWuhm7jewiEnNG wio4cMxabRrvdxjpqeS6OwV54dAL8xtbmk7mDpGrVFhVk6NrKEjQ8+9WozJZGIO8 jU8m7uupHBpE/27Kl3agE78ZkQ9jUQ6mHS36GxmfMAQOMQRGsyIMyOW4cWxRRXLm mXTBP1fTQMtPvMii1q4Ixx9gf5rEvYckzEIE3Ut0xlfckdod86Vbp6NrQMBX/vtK XuUBDOTb8Nf6Rrx1yBBriQ== Received: from phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta02.appoci.oracle.com [147.154.114.232]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42ckkqs3hx-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sun, 20 Oct 2024 16:20:22 +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 49KCurTG008250; Sun, 20 Oct 2024 16:20:21 GMT Received: from nam02-bn1-obe.outbound.protection.outlook.com (mail-bn1nam02lp2049.outbound.protection.outlook.com [104.47.51.49]) by phxpaimrmta02.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 42c375gqxp-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sun, 20 Oct 2024 16:20:21 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=mYoLplVzSpO1o10TNFaxPXS+D8m1zRgxemO+lvE5JPqirTIPVdoH4wyZ5QIZaDuGwOan00PhhDFGZxlcjWeWIEl3PymCNxZH1p/FsJxPdUuTrX+cyTC2TAW6pouCNgr6sqPH8NOZZQM1KyFwD8cfhuebx+/6GecwWVomY2P+Etu0lJaYOCWupYDD6zI6x1b5Z/WEIn4fIXR8du/1xziWvScn37QnCvPGtqUMDiXvmDSR7CqdD6NPwQa4xPKfvOTmy2RiPAnwg74WLTMvqqUcKmzeciFDcKxupGV5FfYfhnfcgLGe2ELHTqyonpSrhhL2isIX/THGZrJI+WhK6D6wIw== 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=Vd2Vp6szhFvYTVsLkfnXlXF7Mkv+sqGaCcSK4gFh5M0=; b=a4W3fatYAQQlRNnxJsAL2ou8HEGJXtDYJY1vjbaBnOazcfldDR4kVjrEtIBk93alyvKgJiL2cKbyJE4mIna+2Ap6wy3gNShs7qxrj+S07DtfX6daNwZw5k93kKcMAtTmNr6vrB7nVZa16u9XExiDTwMarzbCrOOm8p4FhfkjJlO7HeiQcBn9s+MnOqFZkyG6mVHsB1acA6DyKBDNkF4UFmWHWZ3Zgc7zuKTWihMtIuF/uY9/O4QRKYDTHgT4wqmKHcFyu9hdqTSr1gdpjBnWiI2CyZbJnhIaYeTmTRJyWhtfp3SDunnXhQMthSwq6vj7lYmVHoy9WKMjnPNTLhjA1w== 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=Vd2Vp6szhFvYTVsLkfnXlXF7Mkv+sqGaCcSK4gFh5M0=; b=YAsb/lpQN/ahO/phof+4aullfY+YQgXQPO6xj90GNLqkRehvUsmjdXM1HsNjASYl7nHlzL7ikxJ/S1mzayBzQu/WhxLIXwr+Zxwo+MD4if8gCvGhL1WOneELYK43g+Kk0/gAVd3bxhQhccxbdUBOlgAeR57aAqb2ErudUVSdAFI= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by DS0PR10MB6703.namprd10.prod.outlook.com (2603:10b6:8:139::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8069.27; Sun, 20 Oct 2024 16:20:17 +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; Sun, 20 Oct 2024 16:20:17 +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 , Ivan Kokshaysky , 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 v2 1/5] mm: pagewalk: add the ability to install PTEs Date: Sun, 20 Oct 2024 17:20:01 +0100 Message-ID: X-Mailer: git-send-email 2.47.0 In-Reply-To: References: Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: LO4P302CA0015.GBRP302.PROD.OUTLOOK.COM (2603:10a6:600:2c2::11) To BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR10MB3366:EE_|DS0PR10MB6703:EE_ X-MS-Office365-Filtering-Correlation-Id: 14c616a0-58cb-4ceb-14c6-08dcf12315b0 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|10070799003|1800799024|7416014|376014|366016; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?MOmAdZP7Y8NQ/oELA0qq6nwlDUCJKJQfCYqsDS7dPorHa9ZeNkxD6hhXMpe9?= =?us-ascii?Q?PJhtP5V6bRBRu9fpLzuzo1Ceh3wSCZxqST2XT/0qJBjaRPBQjz85wRDdwj14?= =?us-ascii?Q?V+mMwCx+hqIC948SmMqLQxepZbm1RP2Fqwg45jObYK9TmsoEBEhvindqQ6sP?= =?us-ascii?Q?GWugCfGi6O7puIKr4qu7R++D8U2NVMH3kXYryW+e0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0nt0zliaD0wqfqKQ3MvnA4jagDGOFgF3ORZv6yRdZ?= =?us-ascii?Q?nZ/jGy9twdoIdZDrcm1yTY1YTr8sXi8I5ZGMxUH64zji8Ptmo9SOWaquWukF?= =?us-ascii?Q?imDQ84IKaEQyi1ggSPhg4DlgTbq/lh+dp8pswFWPKQBk6BLBj+U498kdAAJh?= =?us-ascii?Q?0mOPfzptDER0U1sF1xIxUF/FG9mCIc7PXOwyzisPfKuBLGlRfdSYOPGPefzv?= =?us-ascii?Q?kNhqVaEQA4a4Iyhm+Qy1DXmy?= 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)(1800799024)(7416014)(376014)(366016);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?nebTmjShqgiYO/+uWbrynKS6nR5LQiqSLHJAwMGJ9cNzSri6w2rFImEKnD9Y?= =?us-ascii?Q?0upS6KQtnYokErpCtwfT/rum5cjV1+mTK76ZI5Q1tixFSpgK6PX3I7cg/xpK?= =?us-ascii?Q?Vxiy+XrVaQrkZ9UJa+BW+vO8xmNbsoxLGJLcPE/chdif2on3pimkDe9EwYEy?= =?us-ascii?Q?RxbFLyiJ7UGFpzGAlMp0i7zw/FGAuOV13o2jE6nRcVN2lcgXFxAruTtZhmxg?= =?us-ascii?Q?/JtO8BasPfJRDmoexcS0zAk1sCv/UliwpS8C6ITYDmf7FR5zveUudR3WxVGb?= =?us-ascii?Q?t3vfofwaxj17kXVgD+pCUojCQ9lW5naxHEUgZh6CzTRfYNT2+bEI+an/TRDH?= =?us-ascii?Q?GIQzL+zJ7r3dWMSEbBid2718crBVAdp7Yr346uAvJISl6YU+mLQXhI6x/Ma4?= =?us-ascii?Q?9cq7ymBISIPRPhLK4wG8+Kp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ascii?Q?imn6bEP6mdPbmvkEVKRBxlBsgbPzj1ylWacDne2tLdEt5aCUcgSo2h5DHxM2?= =?us-ascii?Q?68UHzwUckrZbtDitzQF+JQxwt8iIHKidz+09/cJgqXuZxze2jlmfhiHvXniv?= =?us-ascii?Q?YGuyEg5cBSqbkUowu1YD0OCMvBDpYAhWuUK6QyPzvigPFxvTYi8yE3wiDF//?= =?us-ascii?Q?HBT94eYhphJkflJf/hjj4gaLocBa25Mnbw9TWmVIpsA18SS0h70V6sRWyo5i?= =?us-ascii?Q?XlhQgApbP/5Hc6CsBPOh8q8JrXY9yb4e6BtPgaBrCgcyQOUoxs9nZzO6E18X?= =?us-ascii?Q?HIZfGhN2oMFrGvbyFWzYSgJ/AlmlTNmUXmJx9zdUWtFCrRhn/MSdSnbGh8Lb?= =?us-ascii?Q?iBUqWQ=3D=3D?= X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: EgRw2whUAFm7JaubURTEQ+d8Kj6yhN0gYE8gxFZCTw+hsjUfJzjfd3FClHSPAQw9YuXV0UrR5UNqNSJg2Z/kIiYV2jDNtQRtp0BCwGDbjbx+KGgxvBmqG19JbfemucsctpPIaf0Y9iE/8GouZy0sdEmtXmSlcv5wtCB6uFmve7AswYisM/H2smtKz8RpM9njDfKHbqegwR+21fixi3romGEpSPVYmAoz9yh/VtEFJSL3ybw6Vy776+ctglGiXK3d+MRW2prONdXroCa8sxtCLo5kLLyAoPeBksnQVgon24tu1+ILWYxwjhKuBDNFqHqX6xqhMhUvabkE2PJpbYWyb9pdxRSR1uTcRVb6XPg/vChSdHdK8QqjKHU7anVe7P+atNJSZS3lMpJ98ZPaRc466em7vt9V0s78fkR40Xetg8Akw7EBD5jySlrGC/anIEeSodvfP9Rh5sgyxv9mTZF+f8zabVwfhFzgOJp+FigM3k5uZ1dudVEap035CWceAhSFxCFkm8DYIaOov3Kn7jPKs5NNDcC2+av4udMYuQn1azF658tAqq2U12TOLjNX+QSMY/q4k2KI8odEy8vM8xLHxctl3eb4Z2LqX2qgJHSNCvM= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 14c616a0-58cb-4ceb-14c6-08dcf12315b0 X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB3366.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Oct 2024 16:20:17.2575 (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: iGZPNNcP8FLmLgt6xP49Rp7VNzyn8G7YCzUo/cRCzMZH7gTg4NL8XgGYpUeyy+B/EykAUCKsOU/AYc2Ul7TDGOF9DI7/XyL3xbxUQN58pZY= X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR10MB6703 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-20_14,2024-10-17_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 adultscore=0 bulkscore=0 malwarescore=0 spamscore=0 mlxlogscore=999 phishscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2410200114 X-Proofpoint-GUID: TFpztYeXN_of0AZF1ycRAlRL_b9_wfDa X-Proofpoint-ORIG-GUID: TFpztYeXN_of0AZF1ycRAlRL_b9_wfDa Content-Type: text/plain; charset="utf-8" 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, we make use of existing logic to split it. Then once we reach the PTE level, we invoke the install_pte() callback which provides a PTE entry to install. 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 Suggested-by: David Hildenbrand Suggested-by: Jann Horn Suggested-by: Vlastimil Babka --- include/linux/pagewalk.h | 18 +++- mm/internal.h | 6 ++ mm/pagewalk.c | 200 ++++++++++++++++++++++++++++----------- 3 files changed, 165 insertions(+), 59 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, pro= vided * that @pre_vma and the vma walk succeeded. + * @install_pte: if set, missing page table entries are installed a= nd + * thus all levels are always walked in the specified + * range. This callback is then invoked at the PTE le= vel + * (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. N= ot + * 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; }; =20 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 */ =20 +/* 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..261cd5f2de38 100644 --- a/mm/pagewalk.c +++ b/mm/pagewalk.c @@ -3,9 +3,14 @@ #include #include #include +#include #include #include =20 +#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 lon= g addr, int err =3D 0; =20 for (;;) { - err =3D 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 =3D 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 =3D ops->pte_entry(pte, addr, addr + PAGE_SIZE, walk); + if (err) + break; + } if (addr >=3D end - PAGE_SIZE) break; addr +=3D PAGE_SIZE; @@ -89,11 +108,14 @@ static int walk_pmd_range(pud_t *pud, unsigned long ad= dr, unsigned long end, again: next =3D pmd_addr_end(addr, end); if (pmd_none(*pmd)) { - if (ops->pte_hole) + if (ops->install_pte) + err =3D __pte_alloc(walk->mm, pmd); + else if (ops->pte_hole) err =3D ops->pte_hole(addr, next, depth, walk); if (err) break; - continue; + if (!ops->install_pte) + continue; } =20 walk->action =3D ACTION_SUBTREE; @@ -116,7 +138,7 @@ static int walk_pmd_range(pud_t *pud, unsigned long add= r, unsigned long end, */ if ((!walk->vma && (pmd_leaf(*pmd) || !pmd_present(*pmd))) || walk->action =3D=3D ACTION_CONTINUE || - !(ops->pte_entry)) + !(ops->pte_entry || ops->install_pte)) continue; =20 if (walk->vma) @@ -148,11 +170,14 @@ static int walk_pud_range(p4d_t *p4d, unsigned long a= ddr, unsigned long end, again: next =3D pud_addr_end(addr, end); if (pud_none(*pud)) { - if (ops->pte_hole) + if (ops->install_pte) + err =3D __pmd_alloc(walk->mm, pud, addr); + else if (ops->pte_hole) err =3D ops->pte_hole(addr, next, depth, walk); if (err) break; - continue; + if (!ops->install_pte) + continue; } =20 walk->action =3D ACTION_SUBTREE; @@ -167,7 +192,7 @@ static int walk_pud_range(p4d_t *p4d, unsigned long add= r, unsigned long end, =20 if ((!walk->vma && (pud_leaf(*pud) || !pud_present(*pud))) || walk->action =3D=3D ACTION_CONTINUE || - !(ops->pmd_entry || ops->pte_entry)) + !(ops->pmd_entry || ops->pte_entry || ops->install_pte)) continue; =20 if (walk->vma) @@ -196,18 +221,22 @@ static int walk_p4d_range(pgd_t *pgd, unsigned long a= ddr, unsigned long end, do { next =3D p4d_addr_end(addr, end); if (p4d_none_or_clear_bad(p4d)) { - if (ops->pte_hole) + if (ops->install_pte) + err =3D __pud_alloc(walk->mm, p4d, addr); + else if (ops->pte_hole) err =3D ops->pte_hole(addr, next, depth, walk); if (err) break; - continue; + if (!ops->install_pte) + continue; } if (ops->p4d_entry) { err =3D 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 =3D walk_pud_range(p4d, addr, next, walk); if (err) break; @@ -231,18 +260,22 @@ static int walk_pgd_range(unsigned long addr, unsigne= d long end, do { next =3D pgd_addr_end(addr, end); if (pgd_none_or_clear_bad(pgd)) { - if (ops->pte_hole) + if (ops->install_pte) + err =3D __p4d_alloc(walk->mm, pgd, addr); + else if (ops->pte_hole) err =3D ops->pte_hole(addr, next, 0, walk); if (err) break; - continue; + if (!ops->install_pte) + continue; } if (ops->pgd_entry) { err =3D 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 =3D walk_p4d_range(pgd, addr, next, walk); if (err) break; @@ -334,6 +367,11 @@ static int __walk_page_range(unsigned long start, unsi= gned long end, int err =3D 0; struct vm_area_struct *vma =3D walk->vma; const struct mm_walk_ops *ops =3D walk->ops; + bool is_hugetlb =3D is_vm_hugetlb_page(vma); + + /* We do not support hugetlb PTE installation. */ + if (ops->install_pte && is_hugetlb) + return -EINVAL; =20 if (ops->pre_vma) { err =3D ops->pre_vma(start, end, walk); @@ -341,7 +379,7 @@ static int __walk_page_range(unsigned long start, unsig= ned long end, return err; } =20 - if (is_vm_hugetlb_page(vma)) { + if (is_hugetlb) { if (ops->hugetlb_entry) err =3D walk_hugetlb_range(start, end, walk); } else @@ -380,47 +418,14 @@ static inline void process_vma_walk_lock(struct vm_ar= ea_struct *vma, #endif } =20 -/** - * 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 th= ese - * 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 t= he - * 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 wal= ked, - * 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 pm= d, - * 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->mma= p_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/interna= l.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 +484,80 @@ int walk_page_range(struct mm_struct *mm, unsigned lon= g start, return err; } =20 +/* + * 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() functi= on 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 th= ese + * 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 t= he + * 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 wal= ked, + * 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 pm= d, + * 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->mma= p_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 +573,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 nee= d to - * take other effective approache (mmap lock may be insufficient) to preve= nt + * take other effective approaches (mmap lock may be insufficient) to prev= ent * the intermediate kernel page tables belonging to the specified address = range * from being freed (e.g. memory hot-remove). */ @@ -513,6 +592,8 @@ int walk_page_range_novma(struct mm_struct *mm, unsigne= d long start, =20 if (start >=3D end || !walk.mm) return -EINVAL; + if (!check_ops_valid(ops)) + return -EINVAL; =20 /* * 1) For walking the user virtual address space: @@ -556,6 +637,8 @@ int walk_page_range_vma(struct vm_area_struct *vma, uns= igned long start, return -EINVAL; if (start < vma->vm_start || end > vma->vm_end) return -EINVAL; + if (!check_ops_valid(ops)) + return -EINVAL; =20 process_mm_walk_lock(walk.mm, ops->walk_lock); process_vma_walk_lock(vma, ops->walk_lock); @@ -574,6 +657,8 @@ int walk_page_vma(struct vm_area_struct *vma, const str= uct mm_walk_ops *ops, =20 if (!walk.mm) return -EINVAL; + if (!check_ops_valid(ops)) + return -EINVAL; =20 process_mm_walk_lock(walk.mm, ops->walk_lock); process_vma_walk_lock(vma, ops->walk_lock); @@ -623,6 +708,9 @@ int walk_page_mapping(struct address_space *mapping, pg= off_t first_index, unsigned long start_addr, end_addr; int err =3D 0; =20 + 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) { --=20 2.47.0 From nobody Tue Nov 26 06:38:59 2024 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 B473A1370; Sun, 20 Oct 2024 16:21:05 +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=1729441267; cv=fail; b=ewa0/qYg4m7xCjXKBb7fk/q2Y4f1ZDgvm9X+82uswHmsqo1EmEcz6p12MdSbvOFxZwoIBt4dnQETZtuK8GGdYVslhTPRAcmA5KshP4OvzfZ0I9HkBPG8LoyZ65sotLrJb9dtwmLGSe6V3PVBRajHPdKWUFe3J/5Cg6HIzGXKsXM= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729441267; c=relaxed/simple; bh=ACUNtPIZvF+aeRLwFmoU2XVPvNLugIa8fPicUy5I6K4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=HtCulO5SG4m4xoyaGXMj6sIBaSUJeWFpqxyypOrKb+IqEBwXdedYE3/lPKY5sc1gTtfwOBtPYAoyW8g7mv28eCHQBpa3CWSdcM8YP6Kfexyu3uYQuJDsRhGr/1MLIBz1S8jI1PrhP5iYjMo4qfDn4mSAdYEAClyVP5Yy09S0aA4= 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=Qbl1skzW; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=b70TdbEY; 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="Qbl1skzW"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="b70TdbEY" Received: from pps.filterd (m0246627.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 49KEeO3W002389; Sun, 20 Oct 2024 16:20:28 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=H8+OeiOA7LbXlMb4dJ93wFyuZQtE9inPfUq4/oA3sfE=; b= Qbl1skzWIjCs2U2rqn+M/SaB6YfE8SV0vY7REqEyWh5EhLXmAFr84Wxjo8hZPbVp ND7kDFe6Qk8OTWZ8p4hQSR/X0zOUga7Zm6SAj6N+kc3pcR/Vtp74bh3nrk+FxTJy +PvO6l6wg/FbUEdhx5DMpNVnbhbdNDAFCdFcf0YWyk7G7izNw/Ryw+NS0XaNymvw yhIp4/OY4u23e5/mHaC5d0sddjGo5dW+WQdZrecjr0uXE7KUNdX8DXmcFyzsWCcq RqnvL6rc8B3OyGuM/IG+tMzLaJNmMmB8mQjn6MHM2pfzjBTE6AhxBHEsbHUdA4vQ McXU6iq8guBxRd0PGlwGWw== Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.appoci.oracle.com [138.1.37.129]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42c55ustxg-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sun, 20 Oct 2024 16:20:28 +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 49KE3Tpg035552; Sun, 20 Oct 2024 16:20:27 GMT Received: from nam11-co1-obe.outbound.protection.outlook.com (mail-co1nam11lp2171.outbound.protection.outlook.com [104.47.56.171]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 42c3768fqb-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sun, 20 Oct 2024 16:20:27 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=rXdfuzYabm0mIq96brROStUIdEHen6BpysgsPBPdjCtE1SvL/3RMm0AKjmHKCMdfpbID9eqa9gNzP5oguXKLKNMTG9UEdFI2ztS2Ku+aHhOlNIPFiyOTzPn37jB8IM96H//rBdtG6jAz3V7Uv5+MivGvUHZTZI36Cz9xp2DgWCv7WgZB13GCWX4q+5zXmiu0xhiWWsUtBBDrB5xZpR/UwLwYwC0555RsRuaHIbTSmOnMx7G1TyjdpgN+A4RWSKW9vHFw9FiUkbMyQnCwb3XtVXg0Zf1CwXHobp7RKMszFnFM9K9IYNWc+DHqV9fzTgQjVmVSLH5UtNcXo6bv+rEr+Q== 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=H8+OeiOA7LbXlMb4dJ93wFyuZQtE9inPfUq4/oA3sfE=; b=judN2HQzkswrA92dXhajFIyIBcnV/M1A/p90YMyTqYUTkPzingyxJ4VtLVfP2KTLBZOjQj5Zl3nbH7qQ7WTPO97nXVYiSrPMKrU0v3TpwkK0ic7/bxyQNSCV8dEvFa3qJ9Pdzl65ENdP2hWFvzWSfpFwYW+tCoM8LMKlOVL2N9fqJWp48k++fH7767tEHelIehc4VeiA1S49bvrB8vRJLOk8hrgaTpjKR6yWiG0VFbaW6L2L/qBtRIYgBwKoTFUYmTmofrWDJ0tcTqKiA8y+VnQT/93qzcmJFbkOqAQT7+Tj+od/kKv1XHMqlIaB07k2t0+V1zNtVPRIPW1Vnn9u4w== 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=H8+OeiOA7LbXlMb4dJ93wFyuZQtE9inPfUq4/oA3sfE=; b=b70TdbEYV7/Ifu9lQM3+dKj+egjgTeQ+nz4hgM4fSwEHGWZYtQaCwIbVI9B06fxwjsKYJnwrdzlROTFPsff3Z16vmuS1R8FUo+gb7yZ9Yu0ft3wLLdEXRi8OGGhtKuEhAVRstofPbXmi7uLAfEzB7DkxwtUrGkCmTGOOrtFHgDo= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by DS0PR10MB6703.namprd10.prod.outlook.com (2603:10b6:8:139::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8069.27; Sun, 20 Oct 2024 16:20:20 +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; Sun, 20 Oct 2024 16:20:20 +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 , Ivan Kokshaysky , 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 v2 2/5] mm: add PTE_MARKER_GUARD PTE marker Date: Sun, 20 Oct 2024 17:20:02 +0100 Message-ID: <081837b697a98c7fa5832542b20f603d49e0b557.1729440856.git.lorenzo.stoakes@oracle.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: References: Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: LO4P265CA0050.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:2ac::16) To BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR10MB3366:EE_|DS0PR10MB6703:EE_ X-MS-Office365-Filtering-Correlation-Id: 452254f3-efb4-48c4-b0da-08dcf12317a2 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|10070799003|1800799024|7416014|376014|366016; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?Is5Az2c4onPppbZN1/q6i4LLoPwhGu59ogzE7DZGh6CwzMvFUxR5A03skOik?= =?us-ascii?Q?/ss35mKvtCJAVARxaZrdSczfJhPi38XcrnAuULvFOpnNubT6QkDItjLeUW71?= =?us-ascii?Q?6Lui4lh3rLeEw3Be+32GS1BLTnXLsqIRvM1cL5X+hU+8nSZSb+wbpHq8Wwap?= =?us-ascii?Q?2P8LvCPOMSOQzsLM4cDq2FEWQweS8IcExbfNYCVW8VbqmP5F7M7CM6cXsq0l?= =?us-ascii?Q?EqIOF7YxAKWcLOFLG398ZkEq3s07w4//QkZnCNBtWgctRPSpi3WwVfh6FGem?= =?us-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7ZxEGRPJeWwx15NUyuOaBHGqUOPbEYcUA?= =?us-ascii?Q?rQ46aZmFOhjRimCQ0m05jvzKKFWD0t9bMejjdOGc/NpL1+B2TtmMuy6+zRuT?= =?us-ascii?Q?sJ7XXCUQKqSCuddV4Q/Um7yCE7QTx/o5e+epeRkyFj7qUkRJ5BxX0Meuvujs?= =?us-ascii?Q?alc=3D?= 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)(1800799024)(7416014)(376014)(366016);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?2+siz7bM8do+NgkQnswhKBhf5ZsGCrBEJnBHJ08K/NqgT7ZJroZ6s6jAcMJR?= =?us-ascii?Q?W38UoLib94vYgHkteY5VOUFpFN3HQvag34yENUFzqF7WOQkUT4icF78LP4uV?= =?us-ascii?Q?1kmGURT9blUq6CKZmuu0kCRI2EmWFjaoRIndAd7o+9zNtifx7H+ht0wELV7n?= =?us-ascii?Q?OAbbIu5w6kyHPx3Iw/xX3namDiMzKS2eCinP/Lu4q3p1dvnyE2OVOus7esYu?= =?us-ascii?Q?kPjQPEqSC9dXg3+NIJRmZmLJj9jGF2L2LnXJHI49gJGvS3xU065eXkLSs8Fc?= =?us-ascii?Q?DTERrY0z/d/X8aPqTLHY/u9XLBDJKpybGsWKLRa+s7zYa7iptN84L87K0Vp3?= =?us-ascii?Q?+UK03P58A/j3EhHS+Cy1Klplvzkervu76zOLXXqVDb6rcc5YDU249y9+LuoL?= =?us-ascii?Q?mpxnS31G2NARQ/xrdAxZJ/hEV7rZKBmh7yyuxhrkx9ArpMIU5Y3Wk0r2tQkx?= =?us-ascii?Q?w4wZXrWRXhtWxIRZ4f49rI4ZZOLLKNV1X5MBpu2KujwW5iz5a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= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 452254f3-efb4-48c4-b0da-08dcf12317a2 X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB3366.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Oct 2024 16:20:20.4874 (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: db7Hjelck6q8W+dPWCIEi0SvAayclm1APxneG4okPM6fGM8s1ru75q7Y4NwFHhCTO/9cXG9ijfgK/vyiWyC6d2ZgIlH1ncC1jpZ/sFXaCpk= X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR10MB6703 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-20_14,2024-10-17_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 malwarescore=0 bulkscore=0 adultscore=0 mlxscore=0 spamscore=0 mlxlogscore=999 suspectscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2410200114 X-Proofpoint-GUID: iX_W0srb_wC0cISVUGMtD1It4TYlLpxT X-Proofpoint-ORIG-GUID: iX_W0srb_wC0cISVUGMtD1It4TYlLpxT Content-Type: text/plain; charset="utf-8" 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 the like. Signed-off-by: Lorenzo Stoakes Acked-by: Vlastimil Babka Suggested-by: David Hildenbrand Suggested-by: Jann Horn Suggested-by: Vlastimil Babka --- include/linux/mm_inline.h | 2 +- include/linux/swapops.h | 26 ++++++++++++++++++++++++-- mm/hugetlb.c | 3 +++ mm/memory.c | 18 +++++++++++++++--- 4 files changed, 43 insertions(+), 6 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 =3D pte_marker_get(entry); /* Always copy error entries. */ - pte_marker dstm =3D srcm & PTE_MARKER_POISONED; + pte_marker dstm =3D srcm & (PTE_MARKER_POISONED | PTE_MARKER_GUARD); =20 /* 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..4d0606df0791 100644 --- a/include/linux/swapops.h +++ b/include/linux/swapops.h @@ -426,9 +426,15 @@ 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 err= ors. * This marker is meant to represent any of various different causes of th= is. + * + * Note that, when encountered by the faulting logic, PTEs with this marke= r 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 sen= t. */ +#define PTE_MARKER_GUARD BIT(2) +#define PTE_MARKER_MASK (BIT(3) - 1) =20 static inline swp_entry_t make_pte_marker_entry(pte_marker marker) { @@ -461,9 +467,25 @@ static inline swp_entry_t make_poisoned_swp_entry(void) } =20 static inline int is_poisoned_swp_entry(swp_entry_t entry) +{ + /* + * We treat guard pages as poisoned too as these have the same semantics + * as poisoned ranges, only with different fault handling. + */ + return is_pte_marker_entry(entry) && + (pte_marker_get(entry) & + (PTE_MARKER_POISONED | PTE_MARKER_GUARD)); +} + +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_POISONED); + (pte_marker_get(entry) & PTE_MARKER_GUARD); } =20 /* diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 906294ac85dc..50e3f6ed73ac 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -6353,6 +6353,9 @@ vm_fault_t hugetlb_fault(struct mm_struct *mm, struct= vm_area_struct *vma, ret =3D VM_FAULT_HWPOISON_LARGE | VM_FAULT_SET_HINDEX(hstate_index(h)); goto out_mutex; + } else if (marker & PTE_MARKER_GUARD) { + ret =3D VM_FAULT_SIGSEGV; + goto out_mutex; } } =20 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_detail= s *details, return !folio_test_anon(folio); } =20 -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; =20 - if (zap_drop_file_uffd_wp(details)) + if (zap_drop_markers(details)) return; =20 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; =20 + /* 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); =20 --=20 2.47.0 From nobody Tue Nov 26 06:38:59 2024 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 77F84194A63; Sun, 20 Oct 2024 16:21:11 +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=1729441273; cv=fail; b=sOV17FDeiDTSoqe9zdizLC72td9OyBpzkYKbTEif+Whi9TR/L8u9XUzm+UGL4uDKmxjY0YDpY9KAoBcp3rkiZC76I0phUXUxj9MIcQbk1c0ESc772cJ4PgzpwFVOl7BgRbzfGUIx7HkzVQ1K+BPapRgwvaOQQhj4FpWSCmAh7qU= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729441273; c=relaxed/simple; bh=PburVRVOXhRebpXAVJyjBDphcuYWehketyfoA8fICrw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=WTAiE+WGTSR1Gh4+mUQggn8vcqb0cgBlY9LrmNLGIdWdFy6Lvh8sGraeJXB5iA9v3owWXC68XjQwzLKPrs4YkU1wfcFKfE58AV/CMpPDEjUesZ3iaExIue4CWl4qW4R4wV6o5OVEgSmccqjsTlepfByet8KWMfHzCKEOzS0C78w= 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=POhPJjOR; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=rcT0fZ+v; 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="POhPJjOR"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="rcT0fZ+v" Received: from pps.filterd (m0246629.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 49KFSriv013958; Sun, 20 Oct 2024 16:20:29 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=SGHznQQIdiH2KmEGsxVR6E28i9W2PXwoIAUXAHRKpcQ=; b= POhPJjOReKFHsvCdgjQhWUsi5UqGuN424WBQ9X8JbC5WgyXY1ZWaIlBqLIF9pt+C FYQyKLYBbVKwelxMmNAha8x37iHsZQs8q0hI1FR4Rfju0SBkbClAki84zm9SPQat 6Usk7Q9XWaG3P9r63Odi2zl2KNL59cwxewzaWlGwm1awy4sru2NfckDJcR6slX56 Uv4ThmzcL8DXDAcy9OhWY6jq1t0QWH1C3cu/+yvlB1eEi8xp3GWyvmXFQuzmiE0g e+2NeuSXkSqOuGEezrgTZLY6l273ucEb4G0FgFKJa9DYqh3VbeIuA8i8eqYgM1fJ APkmwVSJ0uqvNaoK33B2jQ== Received: from phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta03.appoci.oracle.com [138.1.37.129]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42c55e9t27-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sun, 20 Oct 2024 16:20:28 +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 49KE3Tph035552; Sun, 20 Oct 2024 16:20:28 GMT Received: from nam11-co1-obe.outbound.protection.outlook.com (mail-co1nam11lp2171.outbound.protection.outlook.com [104.47.56.171]) by phxpaimrmta03.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 42c3768fqb-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sun, 20 Oct 2024 16:20:28 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=mAV3qievVqKQ42xAqnoTRQbLXxT3L1kg1xzeZ17ShgUe4Yyw6svAxm+n2ADLlzhazhhylFjT11UUhXr8ylViMqEHsx+Mz3BJfRC3QLBlgQCBY7z8KVGpW/KCIuXY1K4WpiNV4Wv3+8rS1lZZcC8SCdMbij45WNgEMGOSPGCe1cHKNqm7Wug5K80SGqyPVYIr/9L8djEcFfBCQ4ITgAZmueYkZQoz1qw4vfhbGHvNrLGq1k/3YjAxpf36R7z0wHBLNV5WMRac1C9POZizyQOY2Lj3Tixs63ZoGhaRdR8jCBdZKMspHEczec2O94sylsnMoBsdEBCmPBIx+xYodu2NbQ== 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=SGHznQQIdiH2KmEGsxVR6E28i9W2PXwoIAUXAHRKpcQ=; b=gTgemjrE/cTlFT/wCNrlZsBRWA4GBhIZiRpCNGPBt2JWSSnQWcxzNmljgWy/k2Nr6SoTG6FY/em0ipWIroczykhhHaQeMxNhQEeQV20GnNxeC01RwoSg87j5JTLvoYOcd1jPTwzx+eL6MYzUk+83bMru0daPDQW5pHkLXl7Dlx+ZcPXKKNghGd+3WaGdN+y/Yrrbw7h3E0IfXq5e6CfrVHRecqKZ3UHTdRVjVZzb/PEaF2Qy5emGhc5NqH9u/AsyEKZ3M4Yv23VkMWy9Hp3JK3fSBjz0oICWk+kbxAUEyCcOONTTbEBcnSEoTGVkBRXDdrYUXcxeTwY7RjkqKaZNyw== 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=SGHznQQIdiH2KmEGsxVR6E28i9W2PXwoIAUXAHRKpcQ=; b=rcT0fZ+vurxDYZE9fxpsGyQBn/89/qaPHz+ZYa5vc6HV9usejXHrUrh0/xelDymlqm38HxJUfKlmb4SRJprjty+GYDop8dDyGDLdkgP7uyf5y4ZyLyPAiDHbPRLPqUpMhlz8psGal2/VWJwm4OXGFx5IOLzCt6zEaINb0CHwqVU= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by DS0PR10MB6703.namprd10.prod.outlook.com (2603:10b6:8:139::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8069.27; Sun, 20 Oct 2024 16:20:23 +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; Sun, 20 Oct 2024 16:20:23 +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 , Ivan Kokshaysky , 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 v2 3/5] mm: madvise: implement lightweight guard page mechanism Date: Sun, 20 Oct 2024 17:20:03 +0100 Message-ID: X-Mailer: git-send-email 2.47.0 In-Reply-To: References: Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: LO4P123CA0132.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:193::11) To BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR10MB3366:EE_|DS0PR10MB6703:EE_ X-MS-Office365-Filtering-Correlation-Id: 52d524f5-c627-44a1-8729-08dcf1231951 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|10070799003|1800799024|7416014|376014|366016; X-Microsoft-Antispam-Message-Info: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WOXJY92undLxUxy/hetf1XKWYxxRn1T?= =?us-ascii?Q?pLa51C6Exol0nZv2l7l7d3U0GhH79rrNs1NCSyM5gx8iNkp8l7d4NnhxYARb?= =?us-ascii?Q?qhebcz8XgjeAz87Pg+gmvWaGhNLcptFy6KiIyhnUpqEtvjXymO3mqIMQNdpk?= =?us-ascii?Q?CwfsaXT7gQujt/E4IoGEMqaYsY2ecnCZBeH/quCiXhY5VEQrzfy1GFxpPQAW?= =?us-ascii?Q?Dz8yfoZr30TYK0u8OxFG16aGLRktK6YZzXQ+C5+kwx3EBtn4WDeC2Qd0glIA?= =?us-ascii?Q?8xlXGLzF+CKAw+guOD85pLRyHeM3pcmA/ga4aBDSQKMtFntvYZ+aUKu51m3x?= =?us-ascii?Q?EyRtoVo6zGlplT1773cQkXAgq3qp6kg40CaxHmaqmxPT3ku75eCXLW1ep3JT?= =?us-ascii?Q?cJm+nf3MuDcHRgUuZ3CeXdkH1mVbkI7deSpHpUYuc3L4Nh3/++bbPV+bMpzx?= =?us-ascii?Q?tf8ELGzB/iwFjgfrQldTOVl+?= 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)(1800799024)(7416014)(376014)(366016);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?V82zzO0GRQeahhHrp9sw4AKSIVX+aLIKDrjquNAQ8Da0ZQS6xIltpMctq9rM?= =?us-ascii?Q?OKG3xw17xy8mvQB7c/et/hOg404NMaYo6PD9iC2tsmvvFLBx6pd4h8ohk9PV?= =?us-ascii?Q?8XX5IqXEXh2k/SJnjK8cyrwLe4PUj2uh1MTHI6gsNvD18n9svrLDzn8/WC19?= =?us-ascii?Q?SvfaNEaf+vSRpIIG7YlqpK58GFlNlmi1/eIcD5D8FTh7NpJf6veUh3RqzqOd?= =?us-ascii?Q?krX9I9SRGiIMYUGpDIl0jodlwwqqUw0IoMMIAIBaeSJ2Y/vUDLsysYlCy8Af?= =?us-ascii?Q?qG2dey7uJ+TwzztTCh5EbaBFfLBmvdsFyEwdFBsUcLYHFCYFrJ45QgJowE7e?= =?us-ascii?Q?g9VsreYr1heE9Ou9B4Pbxom4TH1fealOcd9q/laV0YsdLh5QcACvuHdry8xu?= =?us-ascii?Q?3UCz70Cx9BbSytc6T/f9g5qv0N0JhkCNVlnlR5dnGFNpT1L6xUl5cta7gJGs?= =?us-ascii?Q?o7u8+s409K/IENyv99vTiWzovZrfzw9IzBuiOo2coBXClnFl6QxHOTecyfxL?= =?us-ascii?Q?IinxqT8dxy75h2n1EeWm7rGyLLsIIGe+03xBAuI0tzxeGGCx0cDTVX1B4gui?= =?us-ascii?Q?ElcrKJRBy57Xq+pCYyItPJco9+cpos/RgnQiuLUObZP3k6+ZAJVjmzwBxmQm?= =?us-ascii?Q?/ycdU8mObf23g1EESmPU8T6G/EqMdrI3217JEIxzz1pdb5KMVeO22fWsYihf?= =?us-ascii?Q?ctd0eQp2Ra3DwkhtWCvu+xdEjA8+lzXvZDFxouLOcZguJ6S15fdlr4IQds+m?= =?us-ascii?Q?9cBKkhB2NwJsiMdQKbvTAy4uU7xGlw6K0ip+dwt5z2qIMyK8Vl1IxY2U56Q/?= =?us-ascii?Q?55khKO3F1RA4JDxukyQyBogrFhUwXgrJsn9pluLyg0AP+64pU1R2LvPcL8p6?= =?us-ascii?Q?jrYWsLfgZ8t+InjfHZHWgJkHz7MUamD1ANjnGR9fO1PnUUDCPlBvcuioBoEF?= =?us-ascii?Q?LgylSszX2G7iosy98I/7I8c2EMChp0b2j8kVQDrVclETziVbmGV/30qFaNr9?= =?us-ascii?Q?qE8I98hbg2lVXMoK7cEVDqbRTWw8eeKrNfNovdna2wY419VZ0bvWFAMfRUsh?= =?us-ascii?Q?4vxJVbPud7mLB3EpUUN35llsSRDjYglfRYPC1XdMQbJM2+LTNvVfFtxPt5au?= =?us-ascii?Q?PUySGoQACwUvFFcjoyMSdrvM177YqjJO9T1q+yJ0VK4HhfNIN9lQEmo5D+F1?= =?us-ascii?Q?Y/TqcjrYZXG4ZrQpvEEDXO+SAHuGRfSN9/PUAUpofSVKClaBQmNp9FvWTyCI?= =?us-ascii?Q?39cXtOS6UEe0h+gswkyFK47+KlptkU5owlkAUOCPOgSy8XrQ+X2nOzl6ulh+?= =?us-ascii?Q?3Xsir4FX2L07bPwQCA2Ndfg+KZ867rHzF3lY6IQoymtdVRyi+E8btlB4Vr24?= =?us-ascii?Q?a5+4YARaPjaM5R64uJohy+uXk1kW6r6TeXm9aJravredqiDHj8u0nTvZVwz/?= =?us-ascii?Q?Y8RCysOK+xXoUcdpgzNn8PRL+GGZvdHlXWzKd1mpwCOG9k3FacxtKzAh2Wt4?= =?us-ascii?Q?9HmRvSlel8XF38fIp6MzoMn8YhmQnF4b16qj0VZRc9sKEHO56EiG7xElCr4l?= =?us-ascii?Q?Aa1icO50ZGKvwX2tsT8PC2dM9rCQy0qHlK2vB6ZVwC+OBHyjr/W08bZsXieg?= =?us-ascii?Q?QROzD6dbhvhIDvNHqQ/w2ZBjR34NxinNyQAmgEbkB/YxAaF14TycTXLQqkT0?= =?us-ascii?Q?F5nSeQ=3D=3D?= X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: ziVeIkapEiASmtXs66iOYlr+Itu/CJWKvvE+B1wWHzqRnVbydNMxoWT9WOeH4T/juYSkSCAjzJa3EvorYftndgtcw9pekbHGO9f203Ceu6sBqOuLaj546oN0DXf9Jxh9rTl0T4FYvIOxZJ2gyYH8EH5ZMWJjhPBhPj4eYu3NLO08rIt66y1WaxbDcnYlXNDygoyzxfdAv9bSyGMBP9GzWk7QwsHyJ0lvFUxKypDcpg2nfaDuJ4r3Xj7HWChIhioe51F2hnfC+ULthD1rITyp2yAvkFny3c0gs0Hl8vXCtUD2KGt7bna7GGtxkbMi84v52p1rTO77ypghkNOBpbDGt8sM4p7h36ZRLruizafpu2lTuIy2Xb803cJuSXaaeIU0s+GBp1uB2viiRm0Dq5AvjfOYPCnpRW+qVxrlHdG2ffoU1vfPp2f3B8r57J25EYvSFeXVqKaMgXTz79hBzeVNySWSDiEIY2V7Kbnt7qP7pWlrG09i47wVQZ4irjX7EfQvltOZ7432K9pnMZqNzsd8hFGhC4JGb6noI4DXhZAg+/qnlmCz6ko4Osq4Y21EuNRLECxBGJRoq0CFiMDfFg0zKkmCjTOY7aN2UlZWPf3OhAY= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 52d524f5-c627-44a1-8729-08dcf1231951 X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB3366.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Oct 2024 16:20:23.2000 (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: IboqsP9PY9gaLbXX2pa8fdCoF8H9JixOemuHEaAW352jZi9OPoA4mzQWaEGVQsWVVYuMCWGTB493gLQUELbkQcx4s9/K0An3Cz4/koRwRrI= X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR10MB6703 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-20_14,2024-10-17_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 malwarescore=0 bulkscore=0 adultscore=0 mlxscore=0 spamscore=0 mlxlogscore=931 suspectscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2410200114 X-Proofpoint-ORIG-GUID: _FIN8rVFT07EnmKBG9hlheBc5q9aA55x X-Proofpoint-GUID: _FIN8rVFT07EnmKBG9hlheBc5q9aA55x Content-Type: text/plain; charset="utf-8" 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 - poisoning PTEs such that accesses to them cause a fault followed by a SIGSGEV signal being raised. This is achieved through the PTE marker mechanism, which a previous commit in this series extended to permit this to be done, installed via the generic page walking logic, also extended by a prior commit for this purpose. These poison ranges are established with MADV_GUARD_POISON, and 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 poison entries will be left untouched. There is no nesting of poisoned pages. Poisoned ranges are NOT cleared by MADV_DONTNEED, as this would be rather unexpected behaviour, but are cleared on process teardown or unmapping of memory ranges. Ranges can have the poison property removed by MADV_GUARD_UNPOISON - 'remedying' the poisoning. The ranges over which this is applied, should they contain non-poison entries, will be untouched, only poison entries will be 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). Suggested-by: Vlastimil Babka Suggested-by: Jann Horn Suggested-by: David Hildenbrand Signed-off-by: Lorenzo Stoakes --- 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/madvise.c | 168 +++++++++++++++++++++++++ mm/mprotect.c | 3 +- mm/mseal.c | 1 + 8 files changed, 186 insertions(+), 1 deletion(-) diff --git a/arch/alpha/include/uapi/asm/mman.h b/arch/alpha/include/uapi/a= sm/mman.h index 763929e814e9..71e13f27742d 100644 --- a/arch/alpha/include/uapi/asm/mman.h +++ b/arch/alpha/include/uapi/asm/mman.h @@ -78,6 +78,9 @@ =20 #define MADV_COLLAPSE 25 /* Synchronous hugepage collapse */ =20 +#define MADV_GUARD_POISON 102 /* fatal signal on access to range */ +#define MADV_GUARD_UNPOISON 103 /* revoke guard poisoning */ + /* compatibility flags */ #define MAP_FILE 0 =20 diff --git a/arch/mips/include/uapi/asm/mman.h b/arch/mips/include/uapi/asm= /mman.h index 9c48d9a21aa0..1a2222322f77 100644 --- a/arch/mips/include/uapi/asm/mman.h +++ b/arch/mips/include/uapi/asm/mman.h @@ -105,6 +105,9 @@ =20 #define MADV_COLLAPSE 25 /* Synchronous hugepage collapse */ =20 +#define MADV_GUARD_POISON 102 /* fatal signal on access to range */ +#define MADV_GUARD_UNPOISON 103 /* revoke guard poisoning */ + /* compatibility flags */ #define MAP_FILE 0 =20 diff --git a/arch/parisc/include/uapi/asm/mman.h b/arch/parisc/include/uapi= /asm/mman.h index 68c44f99bc93..380905522397 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 */ =20 +#define MADV_GUARD_POISON 102 /* fatal signal on access to range */ +#define MADV_GUARD_UNPOISON 103 /* revoke guard poisoning */ + /* compatibility flags */ #define MAP_FILE 0 =20 diff --git a/arch/xtensa/include/uapi/asm/mman.h b/arch/xtensa/include/uapi= /asm/mman.h index 1ff0c858544f..e8d5affceb28 100644 --- a/arch/xtensa/include/uapi/asm/mman.h +++ b/arch/xtensa/include/uapi/asm/mman.h @@ -113,6 +113,9 @@ =20 #define MADV_COLLAPSE 25 /* Synchronous hugepage collapse */ =20 +#define MADV_GUARD_POISON 102 /* fatal signal on access to range */ +#define MADV_GUARD_UNPOISON 103 /* revoke guard poisoning */ + /* compatibility flags */ #define MAP_FILE 0 =20 diff --git a/include/uapi/asm-generic/mman-common.h b/include/uapi/asm-gene= ric/mman-common.h index 6ce1f1ceb432..5dfd3d442de4 100644 --- a/include/uapi/asm-generic/mman-common.h +++ b/include/uapi/asm-generic/mman-common.h @@ -79,6 +79,9 @@ =20 #define MADV_COLLAPSE 25 /* Synchronous hugepage collapse */ =20 +#define MADV_GUARD_POISON 102 /* fatal signal on access to range */ +#define MADV_GUARD_UNPOISON 103 /* revoke guard poisoning */ + /* compatibility flags */ #define MAP_FILE 0 =20 diff --git a/mm/madvise.c b/mm/madvise.c index e871a72a6c32..7b9a357b84d2 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_POISON: + case MADV_GUARD_UNPOISON: return 0; default: /* be safe, default to 1. list exceptions explicitly */ @@ -1017,6 +1019,166 @@ static long madvise_remove(struct vm_area_struct *v= ma, return error; } =20 +static bool is_valid_guard_vma(struct vm_area_struct *vma, bool allow_lock= ed) +{ + vm_flags_t disallowed =3D VM_SPECIAL | VM_HUGETLB; + + /* + * A user could lock after poisoning 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 |=3D VM_LOCKED; + + if (!vma_is_anonymous(vma)) + return false; + + if ((vma->vm_flags & (VM_MAYWRITE | disallowed)) !=3D 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_poison_pud_entry(pud_t *pud, unsigned long addr, unsigned= long next, + struct mm_walk *walk) +{ + pud_t pudval =3D pudp_get(pud); + + /* Do not split a huge pud - we do nothing with these so just ignore. */ + if (pud_trans_huge(pudval) || pud_devmap(pudval)) + walk->action =3D ACTION_CONTINUE; + + return 0; +} + +static int guard_poison_pmd_entry(pmd_t *pmd, unsigned long addr, unsigned= long next, + struct mm_walk *walk) +{ + pmd_t pmdval =3D pmdp_get(pmd); + + /* Do not split a huge pmd - we do nothing with these so just ignore. */ + if (pmd_trans_huge(pmdval) || pmd_devmap(pmdval)) + walk->action =3D ACTION_CONTINUE; + + return 0; +} + +static int guard_poison_pte_entry(pte_t *pte, unsigned long addr, + unsigned long next, struct mm_walk *walk) +{ + pte_t pteval =3D ptep_get(pte); + + /* + * If not a guard marker, simply abort the operation. We return a value + * > 0 indicating a non-error abort. + */ + return !is_guard_pte_marker(pteval); +} + +static int guard_poison_install_pte(unsigned long addr, unsigned long next, + pte_t *ptep, struct mm_walk *walk) +{ + /* Simply install a PTE marker, this causes segfault on access. */ + *ptep =3D make_pte_marker(PTE_MARKER_GUARD); + + return 0; +} + +static const struct mm_walk_ops guard_poison_walk_ops =3D { + .pud_entry =3D guard_poison_pud_entry, + .pmd_entry =3D guard_poison_pmd_entry, + .pte_entry =3D guard_poison_pte_entry, + .install_pte =3D guard_poison_install_pte, + .walk_lock =3D PGWALK_RDLOCK, +}; + +static long madvise_guard_poison(struct vm_area_struct *vma, + struct vm_area_struct **prev, + unsigned long start, unsigned long end) +{ + long err; + + *prev =3D vma; + if (!is_valid_guard_vma(vma, /* allow_locked =3D */false)) + return -EINVAL; + + /* + * If we install poison 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 =3D anon_vma_prepare(vma); + if (err) + return err; + + /* + * Optimistically try to install the guard poison pages first. If any + * non-guard pages are encountered, give up and zap the range before + * trying again. + */ + while (true) { + /* Returns < 0 on error, =3D=3D 0 if success, > 0 if zap needed. */ + err =3D walk_page_range_mm(vma->vm_mm, start, end, + &guard_poison_walk_ops, NULL); + if (err <=3D 0) + return err; + + /* + * 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); + + if (fatal_signal_pending(current)) + return -EINTR; + cond_resched(); + } +} + +static int guard_unpoison_pte_entry(pte_t *pte, unsigned long addr, + unsigned long next, struct mm_walk *walk) +{ + pte_t ptent =3D 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_unpoison_walk_ops =3D { + .pte_entry =3D guard_unpoison_pte_entry, + .walk_lock =3D PGWALK_RDLOCK, +}; + +static long madvise_guard_unpoison(struct vm_area_struct *vma, + struct vm_area_struct **prev, + unsigned long start, unsigned long end) +{ + *prev =3D vma; + /* + * We're ok with unpoisoning mlock()'d ranges, as this is a + * non-destructive action. + */ + if (!is_valid_guard_vma(vma, /* allow_locked =3D */true)) + return -EINVAL; + + return walk_page_range(vma->vm_mm, start, end, + &guard_unpoison_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 +1260,10 @@ static int madvise_vma_behavior(struct vm_area_struc= t *vma, break; case MADV_COLLAPSE: return madvise_collapse(vma, prev, start, end); + case MADV_GUARD_POISON: + return madvise_guard_poison(vma, prev, start, end); + case MADV_GUARD_UNPOISON: + return madvise_guard_unpoison(vma, prev, start, end); } =20 anon_name =3D anon_vma_name(vma); @@ -1197,6 +1363,8 @@ madvise_behavior_valid(int behavior) case MADV_DODUMP: case MADV_WIPEONFORK: case MADV_KEEPONFORK: + case MADV_GUARD_POISON: + case MADV_GUARD_UNPOISON: #ifdef CONFIG_MEMORY_FAILURE case MADV_SOFT_OFFLINE: case MADV_HWPOISON: diff --git a/mm/mprotect.c b/mm/mprotect.c index 0c5d6d06107d..d0e3ebfadef8 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -236,7 +236,8 @@ 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)) continue; diff --git a/mm/mseal.c b/mm/mseal.c index ece977bd21e1..21bf5534bcf5 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_POISON: return true; } =20 --=20 2.47.0 From nobody Tue Nov 26 06:38:59 2024 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 BE265194C6B; Sun, 20 Oct 2024 16:21:13 +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=1729441275; cv=fail; b=gEE+TwWpMubl4rFzkq7ac3oDHZLfpisijfeidmoN0Hp7Ooyn6z9gBoVE1VxKDcsGIAyW+rb0Dtas0k6cROYiHf2UekHNism7LktF/Y5HPJrHMme5wNqZJrFldjJnV+2qYuPYqzCS298AD/VSotYGtVh8SsNfcUxvyMfY8MN+wTM= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729441275; c=relaxed/simple; bh=sjjFz9E4aTHiMF007oK+Xu+zSo4I2HHezKX/xjcZTY4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=CBfNTWp0s9RkKtLMOmloG6/MwacEAgqDpu1UA4xICi6+b40829i76QY5MX9AR2yxCmtHDb0Vut8sT+addtQ6d6HuBZJeeCgHz5u0VPNY7Wb1kHOu1keqjleUZkHAJx2MRn7B5RUbPy9A50z6rv6tmmGCue1FhNQ1zJUWRI6baDI= 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=Ei3vQIkl; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=rXyj0baI; 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="Ei3vQIkl"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="rXyj0baI" Received: from pps.filterd (m0246632.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 49KDC9II011703; Sun, 20 Oct 2024 16:20:34 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=fRzZp6Oobr+FLi4PT6KQdTCpdyNKg2FQUH4AiS58xHU=; b= Ei3vQIklDQlraNeWIn0wT+u8DW7Mo29Wom+Z15o+9x0tjTVI5gbDIBXXxayTrCiB 3vqjqCJFTf7Xoq+sM8Ei9sxrhufiQHaQiMxDgTPWXuIotfcNrHSYGJJb/pfO0plq Hsgnj7HvcH4SrmCz7YFrxpwD/Tfb+SvNzVlQokZ8eS2H+/mKOUWYLtvwB8BZ/tDH Ff4eYiMGznVSa3wnkDXc6VNgGVnUCPF5Omq5cGa7kIbeyatXMQRuRSNOSuVC2daS PERMA7ENLFfYLuN9fZjVOGgXWiDSasBGeGPqZNE+s0XbIcI4DzyDkzE0mrvuvsHR 2CiJfqdbBtXdXseavEPOFA== Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42ckkqs3j3-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sun, 20 Oct 2024 16:20:33 +0000 (GMT) Received: from pps.filterd (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 49KCWxoC026253; Sun, 20 Oct 2024 16:20:32 GMT Received: from nam11-co1-obe.outbound.protection.outlook.com (mail-co1nam11lp2169.outbound.protection.outlook.com [104.47.56.169]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 42c375g0ad-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sun, 20 Oct 2024 16:20:32 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=TWsxDLW3BLtelurwWJk9dt0Fz++H08QHrf8ljxNPeHU+2/CsH9unm4uMkjl4EXdsEhaZxRXIUSXftz0Y50SGL9ZVPsSW4W55yPTnYjoMov640gPdRQ0dUvdSfseMfazQhIZI9z29LdKYcko8XhDjEz7RYwC7JNGyzuNwqyEpUo5vJmnILCFXyGNwCzsvO2yx/mdeqVTQFqbOt43G4LjD+1yjwBW8uY7TYwTnOOcZXcMhWsLraQ+efhBKRNU4nNl9dEw/MtyXMzm2ND7QK9ynJTo/hXrjgblJfehWPx4fLx5sQHnKhVoa2hXxT74OcTtsBoENkcOusuPeq99NhpwrFg== 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=fRzZp6Oobr+FLi4PT6KQdTCpdyNKg2FQUH4AiS58xHU=; b=JahTTvac7bdYu9NBlXxDRPTU6+vz2v1I6lr4P6iQLb85sbNnEHVkq4dSM9Vyenv180mhuab5qDBSxOJXfaRnWx5Q1TV8j44v0D/Kh9uQDVQR2nLoRTd5jap40JEy8Kh1fGTJF4cqCqa7SnIei/uPcN67EqtIVz8l9990PLuzdmtcLzkWv7mJJbjXfbLkfO/06yC18rhsE6jGWRi0kle1XmC4WrUC6b15nWD0Flg5AGKof5aD11MuS6tVxKDbrMWEwodZ2Z0IWYgUgEnk6oDPK93GARfhJsmvKioHVoM07jxoQk01I97psVO9dzpfNb4UO+VckEOdxWRM+gJSs8WOqg== 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=fRzZp6Oobr+FLi4PT6KQdTCpdyNKg2FQUH4AiS58xHU=; b=rXyj0baIInskoGJqKx+qR2EbjGNOhIFJ2P+3KbvSzuYbcZ+HAJOQe/kRqx0UG+sMVFXgUZZr0QlSlaHIfDvuEYezfAZNR3ImTcskRvm23QzZoSh0c+nAUALDqqFA0UEAmx7lDHjgps635Xu7ocOs579UsuqlRL31e6dV1lRRJe4= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by DS0PR10MB6703.namprd10.prod.outlook.com (2603:10b6:8:139::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8069.27; Sun, 20 Oct 2024 16:20:26 +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; Sun, 20 Oct 2024 16:20:26 +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 , Ivan Kokshaysky , 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 v2 4/5] tools: testing: update tools UAPI header for mman-common.h Date: Sun, 20 Oct 2024 17:20:04 +0100 Message-ID: <6899594f48ef250d6984bbcb90877f92a0cb1753.1729440856.git.lorenzo.stoakes@oracle.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: References: Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: LO2P265CA0052.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:60::16) To BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR10MB3366:EE_|DS0PR10MB6703:EE_ X-MS-Office365-Filtering-Correlation-Id: 61df5ebd-887b-4ac0-42ab-08dcf1231b1b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|10070799003|1800799024|7416014|376014|366016; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?jz/5RKdm1Zoxkhkjuwukuygpqea+CGFh2gjbCRV0dt7xQredpzXz1uSBx+9a?= =?us-ascii?Q?gh1yKKhnUNUZq+BdDaE60/9mph+gAQhkjxvZp3Q965W9JOUvi8dOCC6a0Elu?= =?us-ascii?Q?adHkgBtfNbpUSwwd6QMnDIXcMTcU3alo9bwpYtSq66OcDz9mMXH8WUGM2+/c?= =?us-ascii?Q?SG2OWTkCHo/ZNjJ/LDyYfgo3GbJtYMmv2oB0wa8HIL4BS8pWK2v3VTyfzfIz?= =?us-ascii?Q?n/m69Dea1ycENumFLuZrLGXFMnv7lDDaPrNSV+Z2DEpRvEcQYRmY4/MNxusi?= =?us-ascii?Q?+S8oqEUWzvIWLQreJVZPNSYYalGYWwsurBuWDVq0rFSSL5NIFk5DhteYIWR7?= =?us-ascii?Q?mPVKeP4ACxQ+NiW4H3wJS/Jg9aeu0/4Wgo+bN1XObL3bf4UWCGSX5IiCsRoZ?= =?us-ascii?Q?ccdc/MA4zlyEPlZ2B8D9+ZypWkkPMxVHN0YiUi2lMPxgaEIfOLrbJ2uNTv+7?= =?us-ascii?Q?FVzTPPszsgDzryIfzjnzC0OMQuCkNDp0/Ez16ShZ2kEy7+POPPoYdodhZnW3?= =?us-ascii?Q?tppFbw+RKkCIAO3rntJMlwK8QhXEkXG97Wyob8b1GWkgLH0YAiurbc+YYaam?= =?us-ascii?Q?qjqWguF3DBPdbWr+IzNqjioIAJr7LQmOzMGE7FzqwCN/zYijmVxP0iCSCDBN?= =?us-ascii?Q?93zfD7MovqvfxWYTpeG48ZFvJUnENQVMBWMAhnBr2yrPBt7D4n3MQnz1xkNW?= =?us-ascii?Q?7H52V5aN4tnaIiOhDcizcqeN9ykj870Fx17GKMKP5wkt8imknDqyTSyOBOS6?= =?us-ascii?Q?MqVRysSi2Eb0VdPu+O5kVgauSQqtMxDApa5X6q0FT5gq6sW2X3s3jL8+7d0x?= =?us-ascii?Q?pGuQmcvdXQQ3/QIz5TuDm4jECnEcGC7Ma/IlEW6DlRxG7pRkaDN8yHiEyQVp?= =?us-ascii?Q?0R7nBMUnPHp3Xg3i9A3smTF/SrvkwC3RVQjax2uss9eSSsJM1UqyWwWD7+sK?= =?us-ascii?Q?2IBgBGIACb4WLlWf8773wqR3eMAFKwTruPEW3fvOh6ZIviAkEsb6dL19x8LJ?= =?us-ascii?Q?BGDuA8iLZlfWDlkQxRctR62HUgIdCoRzJkh9t5CFoE20tZ4PSceu0RAQrLfR?= =?us-ascii?Q?CeJfMt2r5Ch6L5vusZvgtevp+/0sTFJ1ScZ82csdeFV1KMXDO+JyoQBDfYkk?= =?us-ascii?Q?SUe5v34UP2ig9ZmMlgFwzUmwIoWVz9pZVLuynQN9OQrauWkvfaw80Gbb9Wfy?= =?us-ascii?Q?rqQIQubNqT0ZSs6ah/LZ2JCjkh6IEsEGheG88QFqE9uiG+4xqG5Sa2zgPLYB?= =?us-ascii?Q?E0Lf9DC4TlBSh6vWwvKIYpnPLsMYCNxeNr1L9siPKTyebo4kvB+/ivOeqwKo?= =?us-ascii?Q?nC3QUrmbhV6zeeuzi/ey5RWf?= 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)(1800799024)(7416014)(376014)(366016);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?xTf/BVYKYS9hXW3uVyli90jhKRuoEFbAK0R87gW8sMkJL1aYA9X3QxMj4Otj?= =?us-ascii?Q?6Rkye3znBsRIo8FawCSCKybBJNE+5OSTY/hes//4SF68kyuRF/zCPNwI7dRn?= =?us-ascii?Q?ssTLG+68nomSVVma1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5ayu6t/QN21o4nvh?= =?us-ascii?Q?2J/vIqBB5Ik5QDsTXyF93mk+YMpsGD5zBw/02tM5gZnKd/1AWuDtkWo8D+yI?= =?us-ascii?Q?LnBJHwuUJgvrE0+lv4chLVVCKvxkzYTawsDDzC+jKvDtzVsflOpnhWHUWP9U?= =?us-ascii?Q?AcEyvDooI/EVkUKDrQhTuH8Q8RaB6JIxeIDo0AAe2mfqwreGOunVUk/dx+HY?= =?us-ascii?Q?q4f2dwIecJcEmW4NC3dVMWJqEArVPPq53nTzabhfriQe6QV13XgNNYCuvfGU?= =?us-ascii?Q?4gnYKtXXj5YSRL9/YYHOY4G1I+NwGdRCwnAwmcelInoYkkVw/ajK1VOWc3kk?= =?us-ascii?Q?qdlZtw5fjzgyllm6UfLm3sDsnje3wM9LpTbN0Ukg2s93y97yOCF5owm0nnVr?= =?us-ascii?Q?8iJ7UhjVJuIi+4Jr2PSOYgTZMi+6s//JTPRM2bzbrqVuNaY3rL3TYllrMWJF?= =?us-ascii?Q?6v5+zAD5oscXzYKTPz4tng2cdnPg3RA9iSJf5iSfzT/D5YAx43AL/0dPJD5u?= =?us-ascii?Q?v8TsQHHBjIlZh3Ad90LoAgBT6kpqMxqmGpNxlnBWb/Yk7xjQJXzLxC0k2BFM?= =?us-ascii?Q?14CDrjRY9oinnrc9K2tGEXRA0U1s0bK5DBGJKFCv0kWew9iP9QgRr1sBXk8J?= =?us-ascii?Q?k7NYTMfv4BE3Em7vH0pFjoyyPxyjcA4nlUfODX8rlZ9iPmCZT/YMd5pDz+iX?= =?us-ascii?Q?NZBwdw=3D=3D?= X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: 8ns2FhFIZwsi3VdwSsX1B4NC70PuAjg1jGLAV/8Nkg0/ojnhgIuFAhw2YzSff0N8xK+zO91wlT/Ljz8Riqnfk7pzwBqy/hmwxxFVzqsyD9mYYGMPsn52hOvdonyUXkCy9ARBmiRnaYH1O60LV5PFqWMOl0GXIlyX74yJZig2/IlCZ4RBDxL6PGJdwyiKuT2tqWSv5mPrdBMEPtDfvb6+OESvn84t78AUbiw598c/Din67Qw5CbuRehL6jdgbAIONH1NPpjXNVMELeJ+sUAPEqJCuj3axU63U4dRqvS06u+qqDKrXKg3t8LFtr8ZxlFxp0KpPfSqWKyVRfILWifATeKkZ70zRkX3GeQhEg7adY3L8V5IBzLDJ1lftxfZtD2r2GS2vdmd5KW8BxkmjrwQGd/Ul4WXThEKObQp5Uxdho4MQqdLr4TVqxnRUjP+Df+rDq87lIAr2b9i+UXbP6OiQzclyMM36bjy29SlI91MDdsOJ0WDPd56dy5zM9p+uglZRjrDvXM/5RNYmfEA5lf+QeuWik+rThLkHFeu+964vhX9ZpRV0uaayLSk0Uwq6FRgbmFmEEc1TnbnG8x8XeptCJyOnKHw1gScvGAh5rqVaRGU= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 61df5ebd-887b-4ac0-42ab-08dcf1231b1b X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB3366.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Oct 2024 16:20:26.1597 (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: 1SouEhZFQnOdkyhDFT18NhWKoSaSk98Qyq9MF3u55glsExKLaQo5hQyO0UuD74GWq4/4NdmseIeqbFtFA39sdjkM1hXu4LK7dO6NUtrD78g= X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR10MB6703 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-20_14,2024-10-17_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 adultscore=0 suspectscore=0 spamscore=0 malwarescore=0 mlxscore=0 mlxlogscore=878 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2410200114 X-Proofpoint-GUID: XYGwlR16DP78uKCZU_OWKIvAVbitExy7 X-Proofpoint-ORIG-GUID: XYGwlR16DP78uKCZU_OWKIvAVbitExy7 Content-Type: text/plain; charset="utf-8" Import the new MADV_GUARD_POISON/UNPOISON madvise flags. Signed-off-by: Lorenzo Stoakes Suggested-by: David Hildenbrand Suggested-by: Jann Horn Suggested-by: Vlastimil Babka --- 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/u= api/asm-generic/mman-common.h index 6ce1f1ceb432..5dfd3d442de4 100644 --- a/tools/include/uapi/asm-generic/mman-common.h +++ b/tools/include/uapi/asm-generic/mman-common.h @@ -79,6 +79,9 @@ =20 #define MADV_COLLAPSE 25 /* Synchronous hugepage collapse */ =20 +#define MADV_GUARD_POISON 102 /* fatal signal on access to range */ +#define MADV_GUARD_UNPOISON 103 /* revoke guard poisoning */ + /* compatibility flags */ #define MAP_FILE 0 =20 --=20 2.47.0 From nobody Tue Nov 26 06:38:59 2024 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 CF35C194A48; Sun, 20 Oct 2024 16:21:09 +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=1729441273; cv=fail; b=QkrY5M4Ks4VHyj0JS87n3uEG8fMoxQ2wPOTFcgv1WWqn0zAPP0aD8RT0YqSeK3j1ZaKxlUc/GHnRdLOf4H5pPceMZfliJPMGw4jefXmuF+OcNL8w3knE8ha/kVe8AAaUW0HQ3qBHUSBxHvBL/b48Ln1k4i/D0UOEpsZSARcvSa8= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729441273; c=relaxed/simple; bh=mBdFR5iz2DIYNrryxIYiv7cPXmGutfL45fVkRBbdTbQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=X46Tby44M51qC9LLyYNXc6iNg4W16mDUP62hZOzMGEg1dPnPx45onod5ymWZt9xHU4o1sGocRp2p2JbP1FF29iylyFNqePawIoui3rGnQnCxrVbEPynjYQHwusyADs0ubKpfYM6yNmjYHKYAo/QnMtIdfg16gbzDChOTbed9tBQ= 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=dY5z0y0F; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=hrVNdYsP; 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="dY5z0y0F"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="hrVNdYsP" Received: from pps.filterd (m0246632.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 49KDCUhl012343; Sun, 20 Oct 2024 16:20:39 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=hloMDzJ7xNRqR2Ml9NixNXpGBgDEPZG6/GT48LQVgGU=; b= dY5z0y0FhFad+ogJ6DnCBtxrrqBdBvqowzVBs7y/Nf/gFDjri5+Zv/I4dCIfoR76 V9TxN4EPwJllKXdhgQnK5s2Kd3aBsBNFLRZrPxQoFyilBoxKvuakr+ba2j4ltnjO 8AHaJdB2tVK53RCozOQ0P6Hsg/8zEXgCgxwe5QSKdZQu8mfFpRlsXdJe6Lgp8x3n cU40AKvxwW2oR1tthDmJiQyb2MeP7q+XDkmLakv9b0lthumTons1kn81kUAWJWl0 LEprBD92tAY76Vc2UQnXVKcqHaYuLrYAyB3aqzN8kzCIT0zeLffwcYpoV+mf5coz 9/nNZ+jR9SPf+wivABeq3g== Received: from phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (phxpaimrmta01.appoci.oracle.com [138.1.114.2]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42ckkqs3j4-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sun, 20 Oct 2024 16:20:38 +0000 (GMT) Received: from pps.filterd (phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com [127.0.0.1]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 49KEBLia026202; Sun, 20 Oct 2024 16:20:37 GMT Received: from nam11-bn8-obe.outbound.protection.outlook.com (mail-bn8nam11lp2175.outbound.protection.outlook.com [104.47.58.175]) by phxpaimrmta01.imrmtpd1.prodappphxaev1.oraclevcn.com (PPS) with ESMTPS id 42c375g0ax-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sun, 20 Oct 2024 16:20:37 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=wBeV96v/BTVN83bjrvKrzYW1/QYaK67Kv2+G25J2tEdK+WJsxphpTsX43eRyRbv6G4jUqIW6rLtYjkyX6ASOmZBXkVcrRBoppl4xMIKooDpeqaz9ybl3s6iv1StsN/NLyngPViJDswl/HKWUszbRTQddTgpxnAFDNv66K/D7xWkB6ygliAA6I7NDM/jIhPMwBtghXi1xfUwLlg5XKtJwz7olgCDYlt5tNuYCaHt7l/Ja05euWUD08vGsyiRUYPaw47kcXbkuTCjuI8MpWAH5RziH/AvGTHLPh2lUtsoNTGPMlj69NClPctZmK61tzWNRQX2j7V3ieCaCzunWDQf7ZA== 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=hloMDzJ7xNRqR2Ml9NixNXpGBgDEPZG6/GT48LQVgGU=; b=kiNT/7t+20nz+Y5IM+IMUgUWiogE8tX6vWe3gmsgm/WkfShES9+JPJ3V/PkTiB/0F8QInGnzTMW/y0GwO5es426tFFW8PZ4FYwpkqo0l4vrqFhPjKSAYZQZyo1C/F+vaJzxYiV1GyS2qdVN7S5V8gUzll6/SUzcQIPu4oyBJNYIOuyvH87Hd9z9Yi4/c6cYxcHcJ9IPWb76iNBatZ4/8YJUJ8Kx2a0modcIgjbGL20U6e+xQfZbOe3o6JHrh7vI7vApJWi98/f6a6Ejmn9TgqyuisRVSB1Z1VSVbdh/ZZb14KNqgw9RWzm6Kmh0HIbHj4qIzrUgWyQn+BUs/J00cwg== 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=hloMDzJ7xNRqR2Ml9NixNXpGBgDEPZG6/GT48LQVgGU=; b=hrVNdYsPAL86mTBPHSCd/U0pHElw1KwR1efyKr2YxnYbnp6qbOaLIOHWPOwX7kzG3Cd8u9ZFzR9h5gtgMfNnHkjs0/ZizFCuFVmyjsgsBJYSb30GNn5BLEYs0/dL/E9WMekWQau+WHcE7YySPpQ84gvZ1sf9w/BUs9sO2vP9Ba4= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by DS0PR10MB6703.namprd10.prod.outlook.com (2603:10b6:8:139::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8069.27; Sun, 20 Oct 2024 16:20:29 +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; Sun, 20 Oct 2024 16:20:29 +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 , Ivan Kokshaysky , 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 v2 5/5] selftests/mm: add self tests for guard page feature Date: Sun, 20 Oct 2024 17:20:05 +0100 Message-ID: <090ed13db09bc25863a78eba902d8bf2c6534ced.1729440856.git.lorenzo.stoakes@oracle.com> X-Mailer: git-send-email 2.47.0 In-Reply-To: References: Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: LO4P302CA0033.GBRP302.PROD.OUTLOOK.COM (2603:10a6:600:317::8) To BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR10MB3366:EE_|DS0PR10MB6703:EE_ X-MS-Office365-Filtering-Correlation-Id: 922aac19-af90-47bc-9456-08dcf1231d24 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|10070799003|1800799024|7416014|376014|366016; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?D4xXaQSzlIDDB35YscIPcP5GLcECP3un6EJGOVSP0/aB6Q73QVv1qqwT12Hd?= =?us-ascii?Q?TpgPKLAxcfw4K1JtmbENSwr+UU7GjoamFbnmUcFq9H8KZ8oRF3VXFqcZYEIF?= =?us-ascii?Q?RvWwIx0nUOMadT1j5X34R671qXczA4jCUuv8x+Z93GuVVW0W5zR21doT3liR?= =?us-ascii?Q?5/dwLxwk3eAksqMjCz13rQvzJZwURImUzM5guKGlIOFh74vQHeRs1t3E+LcY?= =?us-ascii?Q?AteNAxSi5sOA6Z6d1xhF6ZL8l+LSbRXHv6T7wajBnxzuA8xf7meTnrKs6jtQ?= =?us-ascii?Q?XD0i93LH3CLj11EB5A6aDWVEEtjnqva9n4VjGvbeO/NxTqgEaHb+pXi3ojT/?= =?us-ascii?Q?hQSbT5AZ5IVyQsKOb7D7LvXBizwDO9xw2ZoXmjhR8wSDqPKWmWzTdKlpZGR7?= =?us-ascii?Q?A2QMEiWxkDAyAEGoocMvIEIPbQnIobqOVJO5c4+rwg9AQ2lHCBdzBkGkYH9g?= =?us-ascii?Q?rsU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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)(1800799024)(7416014)(376014)(366016);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?CzyG5i8RgCOM9HsxPJoMWLgxMpm7IihBe4Hlpz45I1HfsOPg0gfgoyjF4OQU?= =?us-ascii?Q?GSYVL7+DKqrQEXKiN9+G2FkeGbZ+yv2HQqn+CqZOWqnWyuhRTJeIB9WPXG5a?= =?us-ascii?Q?CT0QQ4p68OLpLYAZcPo+x9qrST0e9Lp9ssJLn4Gzv42ft6BlM2Olefl7onyG?= =?us-ascii?Q?PXmQptbEtGjLen/OThMzXcHyh6u8wY5Zv/PcD9ZioXmBF+J5O65k5FLqpWoY?= =?us-ascii?Q?zzigUPD3pYdHNx/oinooUE3ajLDLpGP73AZJ8ImhiPJ7K869MyhgFbWfPSlc?= =?us-ascii?Q?WCX3pjdISqQqh4/Hx+OzDoccTRS2NecwZuWa3qRFEby5s/Mi1DxD9OZfd1nn?= =?us-ascii?Q?2ZIFLoCaYZcBfOZmPmseQw6AOSuOjttP/8umq6P8onG+Q1v8xEUZN9Z4vfu6?= =?us-ascii?Q?DHtHgWBPgf2Z39MpTHaT/+amCytxeBZIdTki4RM70wkp/YuD9ZkpXwEbnUha?= =?us-ascii?Q?TK06N5XWgybyaTNTqmb+IXONdIb5oQytimnd60uiKU2jgg8Cx0pNlR1xwq+x?= =?us-ascii?Q?ts8LzcK1GoB9QUF3SZeo4j4TXMdwm+hFMjSRE4ovQK1PV/4bdrJoXWrLUdEV?= =?us-ascii?Q?0R8gMTsSsQeI7czs2Qecvc64xEqPg2+YAzHy6v3hOnyJjFw00P81zQ8U7nGV?= =?us-ascii?Q?jEEE4OFQS/PBdKvZWSfJNRX6/sd2xZMI/ZE9VX5It5s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fPDsndsRu35md+vslU7S6YfJdpVVcZbL50fTmG6VptF9ep?= =?us-ascii?Q?2sAPh4kmM02KTTkC+9wJwwXOt3wdnUl+c/I8Byy1ClytWMVwvEqdsmehdpYI?= =?us-ascii?Q?Dw9Geg=3D=3D?= X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: 5iEN5peKuHkjXOQ56zqcgJ9b0ou0p9R7sakS9+tj2rycfnEFKBDRW/21BceQkv+pIZYFffrb2DYNtW5JzrDj7qs53uKOgXJhnoV0FG048W4q8c8oVV0HDCEgXnV+kQmFFjsJIXHc/qZiEjHQZh7oMK81TU+DqSSBx5ssWmJhFMP1Bhw4esq6JP9CydHQldWzx/HaHmFxHPoYstQ9nmmXPdvvTX3ufJr2J41bjjo1la2Du9J2wM7heucNhbaywK1AllmykqYof6Ug6/6xArg3ZJC/EFWSLuhazShpLY7VLd1lxdXKjztEUHEZYh9/uFHrkFzmOjLjCxsehfLTYbLFJ5UigbJhTnqqIjAxuZ/3JO91g/bs0b29rrrcUn354LcMUn7SGN5w8AcpADaZHhDiRMzuVJdyZ1f0DcNfk8m6NBvDUY6me7oNfpsHmMrzVBFRlmEsK4iYTBp2E57M1vhlaebzyQBzAikt0GnpYhWPZB/owSdqqRlHJY8LMGiOZwt83Mpo1feZHk88SkqEcTD77+mVZ3HsNhH9aWYJtQMfwZCDBXz078xlJnO7K5YNX+n1FVhXz/UjZKRnqkO+hmw0Uu5kCX11+vRUWrrwwUHD/bQ= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 922aac19-af90-47bc-9456-08dcf1231d24 X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB3366.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Oct 2024 16:20:29.7943 (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: EH9Tyddj/0mrn/DqeXu+cIQycrZVAAlTIiCkc2R3tS9qJwfkWav7zFzhlFwWQyXTXhS1AC9Sf2t0QaopC5LcnzG8Y7MAnOvCF0XT0+O6fAU= X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR10MB6703 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-20_14,2024-10-17_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 bulkscore=0 adultscore=0 suspectscore=0 spamscore=0 malwarescore=0 mlxscore=0 mlxlogscore=999 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2410200114 X-Proofpoint-GUID: pmfcAVRKClJfSxpY9KvG6fhYh5OpGPap X-Proofpoint-ORIG-GUID: pmfcAVRKClJfSxpY9KvG6fhYh5OpGPap Content-Type: text/plain; charset="utf-8" Utilise the kselftest harmness to implement tests for the guard page implementation. We start by implement basic tests asserting that guard pages can be established (poisoned), cleared (remedied) and that touching poisoned pages result in SIGSEGV. We also assert that, in remedying a range, non-poison pages remain intact. We then examine different operations on regions containing poison markers behave to ensure correct behaviour: * Operations over multiple VMAs operate as expected. * Invoking MADV_GUARD_POISION / MADV_GUARD_REMEDY via process_madvise() in batches works correctly. * Ensuring that munmap() correctly tears down poison markers. * Using mprotect() to adjust protection bits does not in any way override or cause issues with poison markers. * Ensuring that splitting and merging VMAs around poison 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) does not remove poison markers. * Ensuring that mlock()'ing a range containing poison markers does not cause issues. * Ensuring that mremap() can move a poisoned range and retain poison markers. * Ensuring that mremap() can expand a poisoned range and retain poison markers (perhaps moving the range). * Ensuring that mremap() can shrink a poisoned range and retain poison markers. * Ensuring that forking a process correctly retains poison markers. * Ensuring that forking a VMA with VM_WIPEONFORK set behaves sanely. * Ensuring that lazyfree simply clears poison 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 poison markers. * Ensuring that madvise(..., MADV_COLD) and madvise(..., MADV_PAGEOUT) do not remove poison markers. If any test is unable to be run due to lack of permissions, that test is skipped. Signed-off-by: Lorenzo Stoakes Reviewed-by: Shuah Khan Suggested-by: David Hildenbrand Suggested-by: Jann Horn Suggested-by: Vlastimil Babka --- tools/testing/selftests/mm/.gitignore | 1 + tools/testing/selftests/mm/Makefile | 1 + tools/testing/selftests/mm/guard-pages.c | 1228 ++++++++++++++++++++++ 3 files changed, 1230 insertions(+) create mode 100644 tools/testing/selftests/mm/guard-pages.c diff --git a/tools/testing/selftests/mm/.gitignore b/tools/testing/selftest= s/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 +=3D hugetlb_fault_after_madv TEST_GEN_FILES +=3D hugetlb_madv_vs_map TEST_GEN_FILES +=3D hugetlb_dio TEST_GEN_FILES +=3D droppable +TEST_GEN_FILES +=3D guard-pages =20 ifneq ($(ARCH),arm64) TEST_GEN_FILES +=3D soft-dirty diff --git a/tools/testing/selftests/mm/guard-pages.c b/tools/testing/selft= ests/mm/guard-pages.c new file mode 100644 index 000000000000..f67d2700d44a --- /dev/null +++ b/tools/testing/selftests/mm/guard-pages.c @@ -0,0 +1,1228 @@ +// 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 mu= st 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. T= he + * 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 =3D true; + /* If a fatal signal arose, we will jump back here and failed is set. */ + failed =3D sigsetjmp(signal_jmp_buf, 0) !=3D 0; + + if (!failed) { + if (write) + *ptr =3D 'x'; + else + FORCE_READ(ptr); + } + + signal_jump_set =3D 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 opera= tions + * 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 =3D { + .sa_handler =3D &handle_fatal, + .sa_flags =3D SA_NODEFER, + }; + + sigemptyset(&act.sa_mask); + if (sigaction(SIGSEGV, &act, NULL)) + ksft_exit_fail_perror("sigaction"); + + self->page_size =3D (unsigned long)sysconf(_SC_PAGESIZE); +}; + +FIXTURE_TEARDOWN(guard_pages) +{ + struct sigaction act =3D { + .sa_handler =3D SIG_DFL, + .sa_flags =3D SA_NODEFER, + }; + + sigemptyset(&act.sa_mask); + sigaction(SIGSEGV, &act, NULL); +} + +TEST_F(guard_pages, basic) +{ + const unsigned long NUM_PAGES =3D 10; + const unsigned long page_size =3D self->page_size; + char *ptr; + int i; + + ptr =3D 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_POISON), 0); + + /* Establish that 1st page SIGSEGV's. */ + ASSERT_FALSE(try_read_write_buf(ptr)); + + /* Ensure we can touch everything else.*/ + for (i =3D 1; i < NUM_PAGES; i++) { + char *curr =3D &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_POISON), 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])); + + /* Unpoison the first. */ + ASSERT_FALSE(madvise(ptr, page_size, MADV_GUARD_UNPOISON)); + + /* Make sure we can touch it. */ + ASSERT_TRUE(try_read_write_buf(ptr)); + + /* Unpoison the last. */ + ASSERT_FALSE(madvise(&ptr[(NUM_PAGES - 1) * page_size], page_size, + MADV_GUARD_UNPOISON)); + + /* 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 poison backed + * pages. + */ + ASSERT_EQ(madvise(ptr, 3 * page_size, MADV_GUARD_POISON), 0); + + /* Make sure they are all poisoned. */ + for (i =3D 0; i < 3; i++) { + char *curr =3D &ptr[i * page_size]; + + ASSERT_FALSE(try_read_write_buf(curr)); + } + + /* Make sure the rest are not. */ + for (i =3D 3; i < NUM_PAGES; i++) { + char *curr =3D &ptr[i * page_size]; + + ASSERT_TRUE(try_read_write_buf(curr)); + } + + /* Unpoison them. */ + ASSERT_EQ(madvise(ptr, NUM_PAGES * page_size, MADV_GUARD_UNPOISON), 0); + + /* Now make sure we can touch everything. */ + for (i =3D 0; i < NUM_PAGES; i++) { + char *curr =3D &ptr[i * page_size]; + + ASSERT_TRUE(try_read_write_buf(curr)); + } + + /* Now unpoison everything, make sure we don't remove existing entries */ + ASSERT_EQ(madvise(ptr, NUM_PAGES * page_size, MADV_GUARD_UNPOISON), 0); + + for (i =3D 0; i < NUM_PAGES * page_size; i +=3D page_size) { + char chr =3D 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 =3D 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 =3D 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 =3D 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 =3D 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 =3D 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 poison the whole range and make sure all VMAs are poisoned. */ + + /* + * 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_POISON), -1); + ASSERT_EQ(errno, ENOMEM); + + for (i =3D 0; i < 10; i++) { + char *curr =3D &ptr1[i * page_size]; + + ASSERT_FALSE(try_read_write_buf(curr)); + } + + for (i =3D 0; i < 5; i++) { + char *curr =3D &ptr2[i * page_size]; + + ASSERT_FALSE(try_read_write_buf(curr)); + } + + for (i =3D 0; i < 20; i++) { + char *curr =3D &ptr3[i * page_size]; + + ASSERT_FALSE(try_read_write_buf(curr)); + } + + /* Now unpoison the range and assert the opposite. */ + + ASSERT_EQ(madvise(ptr_region, 100 * page_size, MADV_GUARD_UNPOISON), -1); + ASSERT_EQ(errno, ENOMEM); + + for (i =3D 0; i < 10; i++) { + char *curr =3D &ptr1[i * page_size]; + + ASSERT_TRUE(try_read_write_buf(curr)); + } + + for (i =3D 0; i < 5; i++) { + char *curr =3D &ptr2[i * page_size]; + + ASSERT_TRUE(try_read_write_buf(curr)); + } + + for (i =3D 0; i < 20; i++) { + char *curr =3D &ptr3[i * page_size]; + + ASSERT_TRUE(try_read_write_buf(curr)); + } + + /* Now map incompatible VMAs in the gaps. */ + ptr =3D 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 =3D 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_POISON), 0); + for (i =3D 0; i < 100; i++) { + char *curr =3D &ptr_region[i * page_size]; + + ASSERT_FALSE(try_read_write_buf(curr)); + } + ASSERT_EQ(madvise(ptr_region, 100 * page_size, MADV_GUARD_UNPOISON), 0); + for (i =3D 0; i < 100; i++) { + char *curr =3D &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 =3D self->page_size; + pid_t pid =3D getpid(); + int pidfd =3D 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 =3D 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 =3D 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 poison markers at start/end of each VMA. */ + vec[0].iov_base =3D ptr1; + vec[0].iov_len =3D page_size; + vec[1].iov_base =3D &ptr1[9 * page_size]; + vec[1].iov_len =3D page_size; + + /* 5 pages offset 50 pages into reserve region. */ + ptr2 =3D 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 =3D ptr2; + vec[2].iov_len =3D page_size; + vec[3].iov_base =3D &ptr2[4 * page_size]; + vec[3].iov_len =3D page_size; + + /* 20 pages offset 79 pages into reserve region. */ + ptr3 =3D 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 =3D ptr3; + vec[4].iov_len =3D page_size; + vec[5].iov_base =3D &ptr3[19 * page_size]; + vec[5].iov_len =3D 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 poison in one step. */ + count =3D process_madvise(pidfd, vec, 6, MADV_GUARD_POISON, 0); + + /* OK we don't have permission to do this, skip. */ + if (count =3D=3D -1 && errno =3D=3D 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 poisoning 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 unpoison... */ + count =3D process_madvise(pidfd, vec, 6, MADV_GUARD_UNPOISON, 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 poison behind. */ +TEST_F(guard_pages, munmap) +{ + const unsigned long page_size =3D self->page_size; + char *ptr, *ptr_new1, *ptr_new2; + + ptr =3D mmap(NULL, 10 * page_size, PROT_READ | PROT_WRITE, + MAP_ANON | MAP_PRIVATE, -1, 0); + ASSERT_NE(ptr, MAP_FAILED); + + /* Poison first and last pages. */ + ASSERT_EQ(madvise(ptr, page_size, MADV_GUARD_POISON), 0); + ASSERT_EQ(madvise(&ptr[9 * page_size], page_size, MADV_GUARD_POISON), 0); + + /* Assert that they are poisoned. */ + 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 =3D mmap(ptr, page_size, PROT_READ | PROT_WRITE, + MAP_FIXED | MAP_ANON | MAP_PRIVATE, -1, 0); + ASSERT_NE(ptr_new1, MAP_FAILED); + ptr_new2 =3D 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 poisoned. */ + 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 poison marke= rs. */ +TEST_F(guard_pages, mprotect) +{ + const unsigned long page_size =3D self->page_size; + char *ptr; + int i; + + ptr =3D mmap(NULL, 10 * page_size, PROT_READ | PROT_WRITE, + MAP_ANON | MAP_PRIVATE, -1, 0); + ASSERT_NE(ptr, MAP_FAILED); + + /* Poison the middle of the range. */ + ASSERT_EQ(madvise(&ptr[5 * page_size], 2 * page_size, + MADV_GUARD_POISON), 0); + + /* Assert that it is indeed poisoned. */ + 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 poisoned. */ + ASSERT_FALSE(try_read_buf(&ptr[5 * page_size])); + ASSERT_FALSE(try_read_buf(&ptr[6 * page_size])); + + /* Make sure we can poison again without issue.*/ + ASSERT_EQ(madvise(&ptr[5 * page_size], 2 * page_size, + MADV_GUARD_POISON), 0); + + /* Make sure the range is, yet again, still poisoned. */ + ASSERT_FALSE(try_read_buf(&ptr[5 * page_size])); + ASSERT_FALSE(try_read_buf(&ptr[6 * page_size])); + + /* Now unpoison the whole range. */ + ASSERT_EQ(madvise(ptr, 10 * page_size, MADV_GUARD_UNPOISON), 0); + + /* Make sure the whole range is readable. */ + for (i =3D 0; i < 10; i++) { + char *curr =3D &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 =3D self->page_size; + char *ptr, *ptr_new; + int i; + + ptr =3D mmap(NULL, 10 * page_size, PROT_READ | PROT_WRITE, + MAP_ANON | MAP_PRIVATE, -1, 0); + ASSERT_NE(ptr, MAP_FAILED); + + /* Poison the whole range. */ + ASSERT_EQ(madvise(ptr, 10 * page_size, MADV_GUARD_POISON), 0); + + /* Make sure the whole range is poisoned. */ + for (i =3D 0; i < 10; i++) { + char *curr =3D &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 poisoned post-split. */ + for (i =3D 0; i < 2; i++) { + char *curr =3D &ptr[i * page_size]; + + ASSERT_FALSE(try_read_write_buf(curr)); + } + for (i =3D 2; i < 5; i++) { + char *curr =3D &ptr[i * page_size]; + + ASSERT_FALSE(try_read_write_buf(curr)); + } + for (i =3D 6; i < 8; i++) { + char *curr =3D &ptr[i * page_size]; + + ASSERT_FALSE(try_read_write_buf(curr)); + } + for (i =3D 9; i < 10; i++) { + char *curr =3D &ptr[i * page_size]; + + ASSERT_FALSE(try_read_write_buf(curr)); + } + + /* Now map them again - the unmap will have cleared the poison. */ + ptr_new =3D 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 =3D 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 =3D 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 poisoning is as expected. */ + for (i =3D 0; i < 10; i++) { + char *curr =3D &ptr[i * page_size]; + bool result =3D try_read_write_buf(curr); + bool expect_true =3D i =3D=3D 2 || i =3D=3D 5 || i =3D=3D 8; + + ASSERT_TRUE(expect_true ? result : !result); + } + + /* Now poison everything again. */ + ASSERT_EQ(madvise(ptr, 10 * page_size, MADV_GUARD_POISON), 0); + + /* Make sure the whole range is poisoned. */ + for (i =3D 0; i < 10; i++) { + char *curr =3D &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 poisoned for read. */ + for (i =3D 0; i < 10; i++) { + char *curr =3D &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 poisoned. */ + for (i =3D 0; i < 10; i++) { + char *curr =3D &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 unpoison the whole range. */ + ASSERT_EQ(madvise(ptr, 10 * page_size, MADV_GUARD_UNPOISON), 0); + + /* Make sure the whole range is remedied for read. */ + for (i =3D 0; i < 10; i++) { + char *curr =3D &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 =3D 0; i < 10; i++) { + char *curr =3D &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 poison markers. */ +TEST_F(guard_pages, dontneed) +{ + const unsigned long page_size =3D self->page_size; + char *ptr; + int i; + + ptr =3D 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 =3D 0; i < 10; i++) { + char *curr =3D &ptr[i * page_size]; + + *curr =3D 'y'; + } + + /* Poison every other page. */ + for (i =3D 0; i < 10; i +=3D 2) { + char *curr =3D &ptr[i * page_size]; + int res =3D madvise(curr, page_size, MADV_GUARD_POISON); + + 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 poison markers are still in place. */ + for (i =3D 0; i < 10; i++) { + char *curr =3D &ptr[i * page_size]; + bool result =3D try_read_buf(curr); + + if (i % 2 =3D=3D 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 =3D try_write_buf(&ptr[i * page_size]); + + /* ...and make sure same result. */ + ASSERT_TRUE(i % 2 !=3D 0 ? result : !result); + } + + /* Cleanup. */ + ASSERT_EQ(munmap(ptr, 10 * page_size), 0); +} + +/* Assert that mlock()'ed pages work correctly with poison markers. */ +TEST_F(guard_pages, mlock) +{ + const unsigned long page_size =3D self->page_size; + char *ptr; + int i; + + ptr =3D mmap(NULL, 10 * page_size, PROT_READ | PROT_WRITE, + MAP_ANON | MAP_PRIVATE, -1, 0); + ASSERT_NE(ptr, MAP_FAILED); + + /* Populate. */ + for (i =3D 0; i < 10; i++) { + char *curr =3D &ptr[i * page_size]; + + *curr =3D 'y'; + } + + /* Lock. */ + ASSERT_EQ(mlock(ptr, 10 * page_size), 0); + + /* Now try to poison, should fail with EINVAL. */ + ASSERT_EQ(madvise(ptr, 10 * page_size, MADV_GUARD_POISON), -1); + ASSERT_EQ(errno, EINVAL); + + /* OK unlock. */ + ASSERT_EQ(munlock(ptr, 10 * page_size), 0); + + /* Poison first half of range, should now succeed. */ + ASSERT_EQ(madvise(ptr, 5 * page_size, MADV_GUARD_POISON), 0); + + /* Make sure poison works. */ + for (i =3D 0; i < 10; i++) { + char *curr =3D &ptr[i * page_size]; + bool result =3D 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 poisoned + * pages, as this would result in the pages being populated and the + * poisoning would cause this to error out. + */ + ASSERT_EQ(mlock(&ptr[5 * page_size], 5 * page_size), 0); + + /* + * Now unpoison, we do not permit mlock()'d ranges to be remedied as it is + * a non-destructive operation. + */ + ASSERT_EQ(madvise(ptr, 10 * page_size, MADV_GUARD_UNPOISON), 0); + + /* Now check that everything is remedied. */ + for (i =3D 0; i < 10; i++) { + char *curr =3D &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 + * poison markers where possible. + * + * - Moving a mapping alone should retain markers as they are. + */ +TEST_F(guard_pages, mremap_move) +{ + const unsigned long page_size =3D self->page_size; + char *ptr, *ptr_new; + + /* Map 5 pages. */ + ptr =3D mmap(NULL, 5 * page_size, PROT_READ | PROT_WRITE, + MAP_ANON | MAP_PRIVATE, -1, 0); + ASSERT_NE(ptr, MAP_FAILED); + + /* Place poison markers at both ends of the 5 page span. */ + ASSERT_EQ(madvise(ptr, page_size, MADV_GUARD_POISON), 0); + ASSERT_EQ(madvise(&ptr[4 * page_size], page_size, MADV_GUARD_POISON), 0); + + /* Make sure the poison is 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 =3D 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 poison is 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 + * poison markers where possible. + * + * - Expanding should retain, only now in different position. The user wil= l have + * to unpoison 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 =3D self->page_size; + char *ptr, *ptr_new; + + /* Map 10 pages... */ + ptr =3D 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 poison markers at both ends of the 5 page span. */ + ASSERT_EQ(madvise(ptr, page_size, MADV_GUARD_POISON), 0); + ASSERT_EQ(madvise(&ptr[4 * page_size], page_size, MADV_GUARD_POISON), 0); + + /* Make sure the poison 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 =3D mremap(ptr, 5 * page_size, 10 * page_size, 0); + ASSERT_NE(ptr, MAP_FAILED); + + /* Make sure the poison is retained in its 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 =3D 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 =3D mremap(ptr, 10 * page_size, 20 * page_size, + MREMAP_MAYMOVE | MREMAP_FIXED, ptr_new); + ASSERT_EQ(ptr, ptr_new); + + /* Again, make sure the poison is retained in its 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 unpoison, but would reasonably expect all + * characteristics of the mapping to be retained, including poison + * markers. + */ + + /* Cleanup. */ + munmap(ptr, 20 * page_size); +} +/* + * Assert that moving, extending and shrinking memory via mremap() retains + * poison 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 fi= x this + * up also so this is OK. + */ +TEST_F(guard_pages, mremap_shrink) +{ + const unsigned long page_size =3D self->page_size; + char *ptr; + int i; + + /* Map 5 pages. */ + ptr =3D mmap(NULL, 5 * page_size, PROT_READ | PROT_WRITE, + MAP_ANON | MAP_PRIVATE, -1, 0); + ASSERT_NE(ptr, MAP_FAILED); + + /* Place poison markers at both ends of the 5 page span. */ + ASSERT_EQ(madvise(ptr, page_size, MADV_GUARD_POISON), 0); + ASSERT_EQ(madvise(&ptr[4 * page_size], page_size, MADV_GUARD_POISON), 0); + + /* Make sure the poison 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 =3D mremap(ptr, 5 * page_size, 3 * page_size, MREMAP_MAYMOVE); + ASSERT_NE(ptr, MAP_FAILED); + + /* We expect the poison marker at the start to be retained... */ + ASSERT_FALSE(try_read_write_buf(ptr)); + + /* ...But remaining pages will not have poison markers. */ + for (i =3D 1; i < 3; i++) { + char *curr =3D &ptr[i * page_size]; + + ASSERT_TRUE(try_read_write_buf(curr)); + } + + /* + * As with expansion, a real user would have to unpoison 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 =3D mremap(ptr, 3 * page_size, 5 * page_size, 0); + ASSERT_NE(ptr, MAP_FAILED); + + /* Again, we expect the poison marker at the start to be retained... */ + ASSERT_FALSE(try_read_write_buf(ptr)); + + /* ...But remaining pages will not have poison markers. */ + for (i =3D 1; i < 5; i++) { + char *curr =3D &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 =3D self->page_size; + char *ptr; + pid_t pid; + int i; + + /* Map 10 pages. */ + ptr =3D mmap(NULL, 10 * page_size, PROT_READ | PROT_WRITE, + MAP_ANON | MAP_PRIVATE, -1, 0); + ASSERT_NE(ptr, MAP_FAILED); + + /* Poison the first 5 pages. */ + ASSERT_EQ(madvise(ptr, 5 * page_size, MADV_GUARD_POISON), 0); + + pid =3D fork(); + ASSERT_NE(pid, -1); + if (!pid) { + /* This is the child process now. */ + + /* Assert that the poisoning is in effect. */ + for (i =3D 0; i < 10; i++) { + char *curr =3D &ptr[i * page_size]; + bool result =3D try_read_write_buf(curr); + + ASSERT_TRUE(i >=3D 5 ? result : !result); + } + + /* Now unpoison the range.*/ + ASSERT_EQ(madvise(ptr, 10 * page_size, MADV_GUARD_UNPOISON), 0); + + exit(0); + } + + /* Parent process. */ + + /* Parent simply waits on child. */ + waitpid(pid, NULL, 0); + + /* Child unpoison does not impact parent page table state. */ + for (i =3D 0; i < 10; i++) { + char *curr =3D &ptr[i * page_size]; + bool result =3D try_read_write_buf(curr); + + ASSERT_TRUE(i >=3D 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 =3D self->page_size; + char *ptr; + pid_t pid; + int i; + + /* Map 10 pages. */ + ptr =3D 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); + + /* Poison the first 5 pages. */ + ASSERT_EQ(madvise(ptr, 5 * page_size, MADV_GUARD_POISON), 0); + + pid =3D fork(); + ASSERT_NE(pid, -1); + if (!pid) { + /* This is the child process now. */ + + /* Poison will have been wiped. */ + for (i =3D 0; i < 10; i++) { + char *curr =3D &ptr[i * page_size]; + + ASSERT_TRUE(try_read_write_buf(curr)); + } + + exit(0); + } + + /* Parent process. */ + + waitpid(pid, NULL, 0); + + /* Poison should be in effect.*/ + for (i =3D 0; i < 10; i++) { + char *curr =3D &ptr[i * page_size]; + bool result =3D try_read_write_buf(curr); + + ASSERT_TRUE(i >=3D 5 ? result : !result); + } + + /* Cleanup. */ + ASSERT_EQ(munmap(ptr, 10 * page_size), 0); +} + +/* Ensure that MADV_FREE frees poison entries as expected. */ +TEST_F(guard_pages, lazyfree) +{ + const unsigned long page_size =3D self->page_size; + char *ptr; + int i; + + /* Map 10 pages. */ + ptr =3D mmap(NULL, 10 * page_size, PROT_READ | PROT_WRITE, + MAP_ANON | MAP_PRIVATE, -1, 0); + ASSERT_NE(ptr, MAP_FAILED); + + /* Poison range. */ + ASSERT_EQ(madvise(ptr, 10 * page_size, MADV_GUARD_POISON), 0); + + /* Ensure poisoned. */ + for (i =3D 0; i < 10; i++) { + char *curr =3D &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 simply clear the poison markers. */ + for (i =3D 0; i < 10; i++) { + char *curr =3D &ptr[i * page_size]; + + ASSERT_TRUE(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 =3D self->page_size; + char *ptr; + + /* Map 10 pages. */ + ptr =3D mmap(NULL, 10 * page_size, PROT_READ | PROT_WRITE, + MAP_ANON | MAP_PRIVATE, -1, 0); + ASSERT_NE(ptr, MAP_FAILED); + + /* Poison range. */ + ASSERT_EQ(madvise(ptr, 10 * page_size, MADV_GUARD_POISON), 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 poison markers. */ +TEST_F(guard_pages, cold_pageout) +{ + const unsigned long page_size =3D self->page_size; + char *ptr; + int i; + + /* Map 10 pages. */ + ptr =3D mmap(NULL, 10 * page_size, PROT_READ | PROT_WRITE, + MAP_ANON | MAP_PRIVATE, -1, 0); + ASSERT_NE(ptr, MAP_FAILED); + + /* Poison range. */ + ASSERT_EQ(madvise(ptr, 10 * page_size, MADV_GUARD_POISON), 0); + + /* Ensured poisoned. */ + for (i =3D 0; i < 10; i++) { + char *curr =3D &ptr[i * page_size]; + + ASSERT_FALSE(try_read_write_buf(curr)); + } + + /* Now mark cold. This should have no impact on poison markers. */ + ASSERT_EQ(madvise(ptr, 10 * page_size, MADV_COLD), 0); + + /* Should remain poisoned. */ + for (i =3D 0; i < 10; i++) { + char *curr =3D &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 poisoned. */ + for (i =3D 0; i < 10; i++) { + char *curr =3D &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 =3D self->page_size; + int uffd; + char *ptr; + int i; + struct uffdio_api api =3D { + .api =3D UFFD_API, + .features =3D 0, + }; + struct uffdio_register reg; + struct uffdio_range range; + + /* Set up uffd. */ + uffd =3D userfaultfd(0); + if (uffd =3D=3D -1 && errno =3D=3D 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 =3D 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 =3D (unsigned long)ptr; + range.len =3D 10 * page_size; + reg.range =3D range; + reg.mode =3D UFFDIO_REGISTER_MODE_MISSING; + ASSERT_EQ(ioctl(uffd, UFFDIO_REGISTER, ®), 0); + + /* Poison the range. This should not trigger the uffd. */ + ASSERT_EQ(madvise(ptr, 10 * page_size, MADV_GUARD_POISON), 0); + + /* The poisoning should behave as usual with no uffd intervention. */ + for (i =3D 0; i < 10; i++) { + char *curr =3D &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 --=20 2.47.0