From patchwork Thu Dec 12 15:36:46 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13905388 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 21474217F34; Thu, 12 Dec 2024 15:37:49 +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=1734017872; cv=fail; b=KFLRO+eq5LV+ga4H5zqVwurVcpt90uCo408dDJf2JSJ/W5Frkd2QAaQ6zFHpNDQKAmSZ8VaQ/aXQdmPwEZ7Z97rxQ4Y1neEYS2kv4IhpTEvamIDSml04spLg/A40U9Pf5JUGf5i0NsNlTF9PMGcsobQTYA8H9cLIjNky+iinSk0= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734017872; c=relaxed/simple; bh=qHNz9b/eSFpH8qEyVpzIeUYLQPnOLOV51BbFtVT9n/M=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=NdqYrBJUqrno6TEvVYCFiaDXImHCtk5gYPCaYhk7/wW5grQHOqGrSqIpUJBzYQg6TLh0AEWp+oTUNg3oUO3+cVelBPcKS0rZqc28v8692Bjwa/N/f6tbeeEtgVw/C19HbH73xN4kqPpZESqC/XRwIPdJZtDaLkUen+cqfKYbBgY= 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=B9ZGeBYQ; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=O5nl/G4h; 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="B9ZGeBYQ"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="O5nl/G4h" 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 4BCCWuUB021240; Thu, 12 Dec 2024 15:37: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=RPMwFte6ft3Ci0McZVbfCvwGByLKCsXDAFOmeVJDhmU=; b= B9ZGeBYQ52FMV6+8w3ZboVm9zTmi4ewniviXP1xWwEp2vKhSmFyNq/ghUAS5Fw2i Rc1RfUiv6aQNMe3J79vnpV3olSm+7en/QaB5aPRBGs44BepqpwysWcbaEOUSG+sO scagjPX2byCPvRuonn8vQELp84mt3b91s+DaECSUzRn78fPMCyuSD1fIlYhBMF0C /fqmmnkf8dafaDAFNfeF2hMCeeBHqfw4ra6KIc8H+Hr3LUrkpoNVhioUI682B0w/ Sk2mWTyiMoWiaeGsWa5Cahba4Bma3yxiI5tNOzXSBDe8uMgJfaVBM/brcIv4ixVb aij6xu7UgDgy90T1ZJf57g== Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 43ce89b7n5-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 12 Dec 2024 15:37:34 +0000 (GMT) Received: from pps.filterd (iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 4BCEwPSw020564; Thu, 12 Dec 2024 15:37:32 GMT Received: from nam04-mw2-obe.outbound.protection.outlook.com (mail-mw2nam04lp2172.outbound.protection.outlook.com [104.47.73.172]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 43cctbap6a-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 12 Dec 2024 15:37:32 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=WWhF8gmf+rWIxDflVQXbUFBeZpoxMhpmZ7JY5FjPjVwWpjEygJrLSeUs5dAhYPlrYMQsLf1/lAmo7xN68kfczXRzrsrx+IgpEPTepv38QSRF/5BncivNJOm9/n8LPKlJ42dw1ALeF8hSae98hCFS+LBKx3atMad5bv7sic1TgsX6OjCPSQWKOLRfUVz1wch0/6r+7/pOcJ+ohMBoZErOpItFi+PLXtSkn9hHbZ5d79pXjEFXSLOr/wZHNqLOYR4i3cgK8ZYOhCrRb1YLFVvJ2tFtY1qmCxL9EO7Z/VOtF5CiBH5sDp0iMQbp5cq+Dmt2cpzmnQvUnjMV6ASn8xx14Q== 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=RPMwFte6ft3Ci0McZVbfCvwGByLKCsXDAFOmeVJDhmU=; b=sUdbOhnGnrDHoPFtiqjVgma277XGv5rPVZdEvI3Coc0a5fPG2HFggsSHD1qVfjFGd32ZEFMrdJjlwoTYdud3ZvaZ10dTBpWnyl2puW2UzTJtVCkTC7YcFL/FL1wyow5de00jjaFPIns+b9Nf2+zwj8HPO7WdfJmQYCL+KiHeoyWFvvrko8IdkRwhuMpf/I1Bjoh/B0xXitzBxEa0iSJcTMHlWgYdV1gy+t+PQuwZ1wf9CnAyM5BVWxuCfJLBmyQKLSlXtvS/VssMxorURA7kEfExzsOuCNjxLY74ACmkI1AlrUDSPYan49HZGCBHTQeF3GZLyE3JOSRN/KDELC8nPQ== 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=RPMwFte6ft3Ci0McZVbfCvwGByLKCsXDAFOmeVJDhmU=; b=O5nl/G4hZFlWV4SlrmEBEOiQq3EKF+EZZZO/2vFM3O9jB8MqqouK5y8/VY6TiVCZ9UWkadtGd6XOdZtZzUsygbS/t62k3SZw7TotMhydZvb1PE3afFrwydigxjmIe65u+pUNifLleGqpFwW+QUWr/+sWGPHQ7tftAu3ZZxswiO0= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by SJ0PR10MB5804.namprd10.prod.outlook.com (2603:10b6:a03:428::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8251.15; Thu, 12 Dec 2024 15:37:25 +0000 Received: from BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9]) by BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9%7]) with mapi id 15.20.8251.008; Thu, 12 Dec 2024 15:37:25 +0000 From: Lorenzo Stoakes To: Andrew Morton , "Liam R . Howlett" Cc: Thomas Bogendoerfer , Vlastimil Babka , Jann Horn , linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-mips@vger.kernel.org Subject: [RFC PATCH 1/2] mips: vdso: prefer do_mmap() to mmap_region() Date: Thu, 12 Dec 2024 15:36:46 +0000 Message-ID: <8f409e14df19bc722ad77e15ce03a95be9f829af.1734017293.git.lorenzo.stoakes@oracle.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: References: X-ClientProxiedBy: LNXP265CA0045.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:5c::33) To BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) Precedence: bulk X-Mailing-List: linux-mips@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR10MB3366:EE_|SJ0PR10MB5804:EE_ X-MS-Office365-Filtering-Correlation-Id: f099a0a0-3463-4a5f-50e0-08dd1ac2e0b2 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|1800799024|366016; X-Microsoft-Antispam-Message-Info: bGTvdEdIDNqQYi41FUL4ImN96m4IQgoS9QMYXjAOgQc6h3CaKejL6nDInxwvonTagyckJ/BW1QQDMLHpGtSheEFVCitfUh3mli3TBlflF7dyJ0gkpJPcvb4dNNI/dWeRQqSLEhGqH4L7iaKhWL9pVRtAVwPV8eGQD5L7q6y4eWIQa9tqKpvt8fig6UxDgO5qoATK3c8R+qCXAI1+qcTlD1OFrnJGdZFH4n92StUUlD+KTUlKFYO9jSQItbaCziPapRPzdmk1uwzPBr9TnJIbdmGnA0uoM9ywz+IwEJJVYrzuDUX5uPUXFvh02tkTTsa9/4ss+TW8UxYyTXj0ywPrHPZeNK8TnK1fuN3cbOdTuj4j08rXiOZZCG1/21j8e2Ki7pnJdEtxutypxIMwFuLELxFsMMmC6xkWI4YddzDw6+zZv44j4VVY6dj/y+g/z/CpO0s8RSv4O6bWPzwo0PRzRxBkZw5DbE1IHdIt/GDBbldBDpVavvQDWc1J4puTo0ZiC76TONIzRQQxACX0ymvvhODjfQ1E20wbWprayb5JDlwahR2Tyn1EryZmffP8TpFsBXbXQWDoWmqg6Dbid4chAidTGwEBuPL97y/ZRulntz6uqTGWm+vOesEDUbHOYy7GkkZhiK1+RUO+ng/M5J+bZeodO2QlrOM+RlRMPm662YPAaZS5jTvl7oRwxURG/fVgVLPO66mn/+dzmqbUnrEJS4FNeXgU3mdmtlcmqB3cfJGH3LhIG8/mtHELHjIyLjdU3KX5twG7bVyLRHe0js8liLYeN4p8DPM+NDQ0Ffs+olRwWm6Iz2eSD6ffmVCnCYeQGBUDSx/saXvpRJZZY0tWeL/Y239hHETbCT4UDM6pSphYtQoKcmAN3DtnbpIS418fh9A+JOww1qPy7i1eP0lFFiN1m/x9QDHBX+Fmt/cYDrIdczA5HRZQSX3ac5hEpxNivLrng8+uaylSFyX6i+OKX53n0EnyNW/9jqO36iOIIfek+uwHuBJHEKCfANK+ZIDzecGg5bWJLAT0amOhTsRueXyeFAYxv7kJv6xQxQZSPFg8XRmSOCymwKnKHLiq8z32UnP6TCQUYbwLE809rPB0ZP9QDigQyiW1SIkAnu/V7kBmJ21qQlG2EezTZA/APC4oDYqiYiqcNOQ6pO6x8EPjx+/X5q4EOKuFB8FTNgDNqVXXeZc14cQeEHOupuOoLaTiGrbuHVsKx6H9I2pUDgXFQfzdqu0o7Ykzdm6iZk4FnzbfK5giOJMgia00mt35MjUYwuKvMG58IJloxb3B69cjTbf/dqbJw2btLL/0ShKazfvdc+TQu8GLa6qkGuOnCsiQpY5UyJr3FkPEjwCpRkzlDg== 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)(376014)(1800799024)(366016);DIR:OUT;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MBocy+oPypTF36tGpgEsFLQPKwAnzHwcV9j3tRfOzeIIC3daWNLvmHPnSAzuE+yEppEvrZEWFi6po5YWyLemQx2dxr8UT1PPXapUBWvUZ1K7tgPSV74a5vJqVln4rM46U6BvhpkFWw8NBCTjIKrh677CsfwoAYaHn0y3ImVp+uNUwZAmbkbv9FPFze9x+TzGEAo4d0PqKNlLGJYyaB760H8TE= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: f099a0a0-3463-4a5f-50e0-08dd1ac2e0b2 X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB3366.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Dec 2024 15:37:25.3343 (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: eOdtv286Klh0Hrje8tn9fionBJvrnpTrKkiZ/XqcKDsBqeWEC9lF8tVuyvXioTxIYkTIeDJ4JfOZh1gspcnSbdDg+VxuXBjWi6qHINmzRpI= X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR10MB5804 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.68.34 definitions=2024-12-12_09,2024-12-12_01,2024-11-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxscore=0 malwarescore=0 adultscore=0 phishscore=0 suspectscore=0 spamscore=0 mlxlogscore=951 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2411120000 definitions=main-2412120113 X-Proofpoint-GUID: UAEX7khZerWH3AYubgYG5VDGzfDvM-L9 X-Proofpoint-ORIG-GUID: UAEX7khZerWH3AYubgYG5VDGzfDvM-L9 mmap_region() is an internal memory management implementation detail that is not intended to be used outside of the memory management subsystem. Map the delay slot emulation page using do_mmap() which makes use of the already-held mmap write lock and bypasses unneeded populate and userfaultfd logic. This should have the precise same behaviour as the existing logic. Signed-off-by: Lorenzo Stoakes --- arch/mips/kernel/vdso.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/arch/mips/kernel/vdso.c b/arch/mips/kernel/vdso.c index 4c8e3c0aa210..75c9d3618f58 100644 --- a/arch/mips/kernel/vdso.c +++ b/arch/mips/kernel/vdso.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -97,11 +98,12 @@ int arch_setup_additional_pages(struct linux_binprm *bprm, int uses_interp) return -EINTR; if (IS_ENABLED(CONFIG_MIPS_FP_SUPPORT)) { + unsigned long unused; + /* Map delay slot emulation page */ - base = mmap_region(NULL, STACK_TOP, PAGE_SIZE, - VM_READ | VM_EXEC | - VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC, - 0, NULL); + base = do_mmap(NULL, STACK_TOP, PAGE_SIZE, PROT_READ | PROT_EXEC, + MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, 0, 0, &unused, + NULL); if (IS_ERR_VALUE(base)) { ret = base; goto out; From patchwork Thu Dec 12 15:36:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13905390 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 1FA002210C2; Thu, 12 Dec 2024 15:37:53 +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=1734017876; cv=fail; b=syVyxJNsZ3xakGwjSTDwJrbIJ/FpJ3hGbvoC+xoGcOBklpPfF4wX3LXB9VTqQsH+4NXyn3XLlngQh0AFQkaorlK+k/+QvfENDg1oOVPMyRLoEqA/bmnJJteEI0wpaDC0INMPPMY00K/imDtucfQCxDO1d+135Y3VbNHPNUN0ebk= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1734017876; c=relaxed/simple; bh=G1wO4jTZI7uKPvbXiynCZBKlr+xnaV5jx905RLFKXcY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Content-Type:MIME-Version; b=UJ6FPqW0ovmh7i0pP8Vxnb3Lbmr+rnEOlvDeBsq7PseQHk9RKCGTpHQ4+h08TQXknvNttrZ+Fd6EW51L0vTAmWGJuKZlKgdREcqGtGuFKawNRO636z5JdZK9DWxwY5S3zqwSfFr2l6tOv1NOdtdBvJTb4kYElrtOz+zj1AhKCMk= 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=mtw6gbQw; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b=gl8Y59Zw; 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="mtw6gbQw"; dkim=pass (1024-bit key) header.d=oracle.onmicrosoft.com header.i=@oracle.onmicrosoft.com header.b="gl8Y59Zw" Received: from pps.filterd (m0246617.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 4BCCOBQR005387; Thu, 12 Dec 2024 15:37:36 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=6HkV2lyfUu+yiBqw99Z+AS5m2F7z5xSUawErWoaPIcc=; b= mtw6gbQwGuy01rwXIduakFecrcKKfLEbR+SodFq7AkLJDKruD8V2t7tw7Z6kycP+ 2P/QYhAvnJnrnTm6xsbPYx5ULODZ4V4hO/cRbqcceU+KslubEBscw792CVn2vJRp mqRCq5yORwkyiGJCo/PuTcTHtVMQteEWoTW7g552tiWiapd6j0nlD3gB47ZAR7yG oFMKc6yIR+NiHIpQTwxopQaUTHkadaQ8sMrV9eKLixfPZm/3LMLNnQFOCIZUo9wx rNAvcHo7S5g5yPdMgcsNV25JSg5CvBHPi2SfMq8P8S6Je33kLeGWABk023Dw7M8S TpNbnL/Gx9nGLd+zS59zGA== Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 43cewtbgwc-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 12 Dec 2024 15:37:35 +0000 (GMT) Received: from pps.filterd (iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 4BCEwPT2020564; Thu, 12 Dec 2024 15:37:34 GMT Received: from nam04-mw2-obe.outbound.protection.outlook.com (mail-mw2nam04lp2172.outbound.protection.outlook.com [104.47.73.172]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 43cctbap6a-3 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 12 Dec 2024 15:37:33 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=Ru+WvEhb7A2g1vkMRDzwasnYbANkN+xSmaWHejjUIwUvTpUg9/8/ppxoCwSWiZAAzOtXmVmSijTvCFGORaq7OLjUvNESvCpn8uNVbLcbSh6yGoQIlnrWfVIYGUpG9DX1c6elRI03eH2zqbYMG7p8OxtFenVK07wL0U3gkB4eSTqfKXwbPRByAE/WP94cg+x6eFtVMaSaTLAXDa5rDbKSZdZF/7jcDbbyZmo35E04ujC+PCgCLxmDlFXKDm5ed0IJOZCidYbHQ3uWSjm/s8WYoNauJbJpYdjj+uhOrFiT08qfnSMTMi7MDASQ3UmVdsu9ttvV1yLf/gqTTVcskoWwXQ== 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=6HkV2lyfUu+yiBqw99Z+AS5m2F7z5xSUawErWoaPIcc=; b=bWfFUAQi7t3g1gpsfJnAcMAJ3rdT6oBmeSUvXaailpvWJPoT4UENFY0KKmqcq5nxiit5CbTAdtJwmVVoiyC1pLeto5wUFUFC91VefQp5cBmCXwHMee6ATyMByUzjq/LXhWRP40BwFmDZpFY/qDdnoHaikPHpH00DC3qy57iZWopP3D9fv2JPwTa6PCh5qfvdcwzRqn6H3uRm1oH0Gth0whoCMmZQWqK8jQmFOE+3NVNtV4bRS7uMx6JohrnBeLXJdyit86ZZZkvDyI8BvWXXlXBJEk+xUdStOILHJ6ZiJG/1wQFvrYNRG/a2A+34KhVlp3OK+657qmiorcDWkz0HzA== 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=6HkV2lyfUu+yiBqw99Z+AS5m2F7z5xSUawErWoaPIcc=; b=gl8Y59ZwHUclUhgCNoYcVyPXa7dXtCOYIIxbqBN4On/wjyvZCabSmrxtqL0gM6ecihJiWfKtM0icfayk56LetOFGEtShvQL6uaoL/r+wou5t2VNl4DQB26W+cLMDR5qKVDhZ1P2gSWobL4EfpYMBjNklsE1Io5GDfhKv2ie6/Ac= Received: from BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) by SJ0PR10MB5804.namprd10.prod.outlook.com (2603:10b6:a03:428::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.8251.15; Thu, 12 Dec 2024 15:37:28 +0000 Received: from BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9]) by BYAPR10MB3366.namprd10.prod.outlook.com ([fe80::baf2:dff1:d471:1c9%7]) with mapi id 15.20.8251.008; Thu, 12 Dec 2024 15:37:28 +0000 From: Lorenzo Stoakes To: Andrew Morton , "Liam R . Howlett" Cc: Thomas Bogendoerfer , Vlastimil Babka , Jann Horn , linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-mips@vger.kernel.org Subject: [RFC PATCH 2/2] mm: make mmap_region() internal Date: Thu, 12 Dec 2024 15:36:47 +0000 Message-ID: <80b7c4ba4f0a2b3084117975bd1af62a403ac881.1734017293.git.lorenzo.stoakes@oracle.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: References: X-ClientProxiedBy: LO4P265CA0292.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:38f::18) To BYAPR10MB3366.namprd10.prod.outlook.com (2603:10b6:a03:14f::25) Precedence: bulk X-Mailing-List: linux-mips@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: BYAPR10MB3366:EE_|SJ0PR10MB5804:EE_ X-MS-Office365-Filtering-Correlation-Id: fae87885-9a2a-48d2-3c2c-08dd1ac2e264 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|376014|1800799024|366016; X-Microsoft-Antispam-Message-Info: upU9iMNSBTfqlKEWrzv4jHRv+0tmK66YXciEZJSsmBxi6mDzMsS/L8ME/QCPWHHesINOlR/B/tJKmHn+9VomHrmI71SLaoe2gokyk6QNkUPteXJGrOPCeIvSClzUiZthxzRVqjNyYQheHwiidD2uOem1D+LmGbAp3Z/4ikwa7lCg7/12/ALyUrguU2nKfH2mWdudvud4Utly2RbVEAvRWWVk9D+lP/HWy7FoGJLfsZJmjBjJDU/y5xvVLHdYf/bzj1DreohhZ65W8P/8Op0o0t7PRKdj8qW3k4SQ0rHVVWF377FJgnSrK5Yu893nwHpV3g2zD5+/xIaqr/rEl/UO56wTlRP+PBSn5oQ1EZi2POKExBMHVQvlfi7kv+yZAkKvaMGcVC1n0eT/j+j1rbtPQwcimTTsqOtMv+iC7mUPVN7k2xGdoY9SPhNXwWpg1CnIdxybmgOWQIFp8wvG+ZBf7PyO9H5d5OC2n32H6G4PsGOps5+5QRBRqZyLsX0T9Mj2n2XgfoKZihC4YyhJBgiaaVPxZHKiPllqQa4CnXEJpH44yZ+nUF3X7wsHAMLGufMptn7zkf3x9+S5FM5ovvwyyLQGoQjGvDJL9nDmxR2G8uQjU5+omajpzUYOOTD44sY9AUAnZd3z0ao5WeB+mq7hmp5ENyg3LgP7VZtNYhZR13v+ShRtOupPPY31z+ooCroOoOqLh9SKzhK/2b0RnFSFswXaJdyKB5coWKHnmPZFbIgHpqyPWfkkvSyzU7yK6FRbLcmudFx0cQUmO4inFtrGsoC/JAZJ5QP/oZQko4KRxWEAZ8vhKdA5iOEB4tQvnwOA6pl8Xj1mNgBo4Bw7q+WO0RzXTdnMZ8duRBDxrKw8AGfUt11V7tfxdpSLFtceOZRFYfPY/cy1k2djDBJtbFeE8V92AHhBGUy/yfbCGgv24fh+qog8kCi4H8JuPfesyQvESQlPXlWJdUDKBjzozXwyD3YrIFZIMKe5RUU1fXz7JEJIWvTjdhHITcoKivOrd2QtFcJ4Wb55RAXUn3kC+ovwuq2bxM/Ft46yycNNsJ1oBhUr2ju3WK/MzGIayIu6BNgI1eLnwvUtB1ZvsYPzg6vq5RDI+zDl1evxBfVpPvuwevW39x6Umw8Fsgom7dQhTA/TAvVieNeV/bT8y5ZsY2SJZid4Xo/wflAb1awRgASCq6H4MCPnJNmJEosbKq6dl4Kjz370ewqch1tYEqWU580qaIhd8TIrYv4eTi6WdKo/OPSGWt5W7YcYwO68A+cUATL3uJA9GUm+OOgK0mgDut2hQv4rd1p1FV2LnWi1JRsjb+wCQipGeZx33L9QGvwgerUTonBM5AFuS+bafM2wzYh8Kw== 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)(376014)(1800799024)(366016);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: m1ZYN32u0ggWtCEX610SbMhZ1vkk9NDhhuJm6tQT30RR9az9aIrgxdSDMQW6XJzv7cCo0TE1OeYNrV/4qBAxmR4W9IflGk2+7bMNDtlBseOOqxdFI9disUhphAT798psFEG2EvZUw10OBmFlcaACwrMM8IKS9uWNqWl8trk2fu1w7uwH8k7UDaMKy4cWgZpDnqYDImC1NKV8d+slERXaul5bEaPakUHrcglZE5tq+h8HycT6BQYqbJ0Ugs+RhgB/EFpr+ndeHNF6grVqHlad2TOZe+Nd7mPRS1MpNiiE4KlfTTjuJI9CLo+E5q402cnbi1HQratUQxTHV0NHRcodkg6zqVK5v+tC85oSimSz3YuT82txO6+y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/kNMAhnIcoznAD3FJ5E7Lemv4gZiqzBzZTOi2H6JuXqNGXDkWBau4fRcTGWA/iuHokJFFodlrJloB6sWtBjzhNvnJ2OmiOd+qCYZ2qRw/DgG1bqXQ2ZKnN+Yz5rKbZFOSm+oIdHEqJ1s0s87YWp22CK+9AguBfhGaw== X-MS-Exchange-AntiSpam-ExternalHop-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-ExternalHop-MessageData-0: bYzaxpnnJOlIZ/gf3gEfyrvrDWNk/uNA8G3n0gtdMuem3voYxl+N1yk4l8CGe6zQNJCu2R8Qgku9K0IghH9nMazqVxZxVs+9UrW0fRMkHJy+Q4Slph4D2eMxYttPpUeSb5DdQ5UkuBFXcldVjGzkAtx9SlJA21qpI7JpgDCxVLHgHilMESwtNPf0VWi7vxH0FQSp8ofhWRrnEoRKGaa7v0fzDvVm0OpLnq5mdlFhIOsgW0v6JOlbGQZWhAQFskfwR5r5m7j7kT2nqwNXxFIP+8X2sTNUHqkOz/R6KTYM0hO7lSsUg0S6GWupO8KFwrXRfF9RJWkdPtHFzp39jwAzcz/xUqJwvEYb8v/OD8jD+18khQv3x4BFKI4G+ikArj6ZA7TCpTR/qauaNY0jF78YMDpIWaWv2leCwBO5zKQxilQZVkjfpSoJnk/ctqS09zTHfI2+2dmrPOX72I7mWIRDyzpZp2W/LblSKd8HjRV1ca7Ad1p1vUQWgLYyUimhk6Rk8sqDOWZIMVrbnL1N2Wx9zzF9j5/jEKea0q5+3Oqqymf6aRMmOgbSa5SJoGVOmcTGGrXohQFuxWU1NdMw4m2ncdtj2owFFyFIsq1ddb1uiMc= X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: fae87885-9a2a-48d2-3c2c-08dd1ac2e264 X-MS-Exchange-CrossTenant-AuthSource: BYAPR10MB3366.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 12 Dec 2024 15:37:28.2917 (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: EuiL4SAyk1bvAupcTCyyh7jyTAw/oYysoBb2lyachlE792shgHlsJ1JAI205AISwvuWnEtDrmZKBz1vKSpgal+Xg3AlJXMltVD540Nfnt7o= X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR10MB5804 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.68.34 definitions=2024-12-12_09,2024-12-12_01,2024-11-22_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxscore=0 malwarescore=0 adultscore=0 phishscore=0 suspectscore=0 spamscore=0 mlxlogscore=999 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2411120000 definitions=main-2412120113 X-Proofpoint-ORIG-GUID: Std7YryAOEqZLVsKZWBmIZpozTHYyJu- X-Proofpoint-GUID: Std7YryAOEqZLVsKZWBmIZpozTHYyJu- Now that we have removed the one user of mmap_region() outside of mm, make it internal and add it to vma.c so it can be userland tested. This ensures that all external memory mappings are performed using the appropriate interfaces and allows us to modify memory mapping logic as we see fit. Additionally expand test stubs to allow for the mmap_region() code to compile and be userland testable. Signed-off-by: Lorenzo Stoakes --- include/linux/mm.h | 3 -- mm/mmap.c | 34 ----------------- mm/vma.c | 36 +++++++++++++++++- mm/vma.h | 6 +-- tools/testing/vma/vma_internal.h | 65 ++++++++++++++++++++++++++++++++ 5 files changed, 103 insertions(+), 41 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index a672339f2b8f..d145ddf1b2f0 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -3436,9 +3436,6 @@ get_unmapped_area(struct file *file, unsigned long addr, unsigned long len, return __get_unmapped_area(file, addr, len, pgoff, flags, 0); } -extern unsigned long mmap_region(struct file *file, unsigned long addr, - unsigned long len, vm_flags_t vm_flags, unsigned long pgoff, - struct list_head *uf); extern unsigned long do_mmap(struct file *file, unsigned long addr, unsigned long len, unsigned long prot, unsigned long flags, vm_flags_t vm_flags, unsigned long pgoff, unsigned long *populate, diff --git a/mm/mmap.c b/mm/mmap.c index df9154b15ef9..b771ae617629 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -1016,40 +1016,6 @@ int do_munmap(struct mm_struct *mm, unsigned long start, size_t len, return do_vmi_munmap(&vmi, mm, start, len, uf, false); } -unsigned long mmap_region(struct file *file, unsigned long addr, - unsigned long len, vm_flags_t vm_flags, unsigned long pgoff, - struct list_head *uf) -{ - unsigned long ret; - bool writable_file_mapping = false; - - /* Check to see if MDWE is applicable. */ - if (map_deny_write_exec(vm_flags, vm_flags)) - return -EACCES; - - /* Allow architectures to sanity-check the vm_flags. */ - if (!arch_validate_flags(vm_flags)) - return -EINVAL; - - /* Map writable and ensure this isn't a sealed memfd. */ - if (file && is_shared_maywrite(vm_flags)) { - int error = mapping_map_writable(file->f_mapping); - - if (error) - return error; - writable_file_mapping = true; - } - - ret = __mmap_region(file, addr, len, vm_flags, pgoff, uf); - - /* Clear our write mapping regardless of error. */ - if (writable_file_mapping) - mapping_unmap_writable(file->f_mapping); - - validate_mm(current->mm); - return ret; -} - int vm_munmap(unsigned long start, size_t len) { return __vm_munmap(start, len, false); diff --git a/mm/vma.c b/mm/vma.c index 2fabfd6c8957..218d8f154e99 100644 --- a/mm/vma.c +++ b/mm/vma.c @@ -2432,7 +2432,7 @@ static void __mmap_complete(struct mmap_state *map, struct vm_area_struct *vma) vma_set_page_prot(vma); } -unsigned long __mmap_region(struct file *file, unsigned long addr, +static unsigned long __mmap_region(struct file *file, unsigned long addr, unsigned long len, vm_flags_t vm_flags, unsigned long pgoff, struct list_head *uf) { @@ -2484,6 +2484,40 @@ unsigned long __mmap_region(struct file *file, unsigned long addr, return error; } +unsigned long mmap_region(struct file *file, unsigned long addr, + unsigned long len, vm_flags_t vm_flags, unsigned long pgoff, + struct list_head *uf) +{ + unsigned long ret; + bool writable_file_mapping = false; + + /* Check to see if MDWE is applicable. */ + if (map_deny_write_exec(vm_flags, vm_flags)) + return -EACCES; + + /* Allow architectures to sanity-check the vm_flags. */ + if (!arch_validate_flags(vm_flags)) + return -EINVAL; + + /* Map writable and ensure this isn't a sealed memfd. */ + if (file && is_shared_maywrite(vm_flags)) { + int error = mapping_map_writable(file->f_mapping); + + if (error) + return error; + writable_file_mapping = true; + } + + ret = __mmap_region(file, addr, len, vm_flags, pgoff, uf); + + /* Clear our write mapping regardless of error. */ + if (writable_file_mapping) + mapping_unmap_writable(file->f_mapping); + + validate_mm(current->mm); + return ret; +} + /* * do_brk_flags() - Increase the brk vma if the flags match. * @vmi: The vma iterator diff --git a/mm/vma.h b/mm/vma.h index 24636a2b0acf..dd943a593312 100644 --- a/mm/vma.h +++ b/mm/vma.h @@ -242,9 +242,9 @@ bool vma_wants_writenotify(struct vm_area_struct *vma, pgprot_t vm_page_prot); int mm_take_all_locks(struct mm_struct *mm); void mm_drop_all_locks(struct mm_struct *mm); -unsigned long __mmap_region(struct file *file, unsigned long addr, - unsigned long len, vm_flags_t vm_flags, unsigned long pgoff, - struct list_head *uf); +unsigned long mmap_region(struct file *file, unsigned long addr, + unsigned long len, vm_flags_t vm_flags, unsigned long pgoff, + struct list_head *uf); int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *brkvma, unsigned long addr, unsigned long request, unsigned long flags); diff --git a/tools/testing/vma/vma_internal.h b/tools/testing/vma/vma_internal.h index 3eeb1317cc69..9f6cf80c9682 100644 --- a/tools/testing/vma/vma_internal.h +++ b/tools/testing/vma/vma_internal.h @@ -41,6 +41,8 @@ extern unsigned long dac_mmap_min_addr; #define VM_BUG_ON(_expr) (BUG_ON(_expr)) #define VM_BUG_ON_VMA(_expr, _vma) (BUG_ON(_expr)) +#define MMF_HAS_MDWE 28 + #define VM_NONE 0x00000000 #define VM_READ 0x00000001 #define VM_WRITE 0x00000002 @@ -226,6 +228,8 @@ struct mm_struct { unsigned long stack_vm; /* VM_STACK */ unsigned long def_flags; + + unsigned long flags; /* Must use atomic bitops to access */ }; struct vma_lock { @@ -1174,4 +1178,65 @@ static inline void userfaultfd_unmap_complete(struct mm_struct *mm, { } +/* + * Denies creating a writable executable mapping or gaining executable permissions. + * + * This denies the following: + * + * a) mmap(PROT_WRITE | PROT_EXEC) + * + * b) mmap(PROT_WRITE) + * mprotect(PROT_EXEC) + * + * c) mmap(PROT_WRITE) + * mprotect(PROT_READ) + * mprotect(PROT_EXEC) + * + * But allows the following: + * + * d) mmap(PROT_READ | PROT_EXEC) + * mmap(PROT_READ | PROT_EXEC | PROT_BTI) + * + * This is only applicable if the user has set the Memory-Deny-Write-Execute + * (MDWE) protection mask for the current process. + * + * @old specifies the VMA flags the VMA originally possessed, and @new the ones + * we propose to set. + * + * Return: false if proposed change is OK, true if not ok and should be denied. + */ +static inline bool map_deny_write_exec(unsigned long old, unsigned long new) +{ + /* If MDWE is disabled, we have nothing to deny. */ + if (!test_bit(MMF_HAS_MDWE, ¤t->mm->flags)) + return false; + + /* If the new VMA is not executable, we have nothing to deny. */ + if (!(new & VM_EXEC)) + return false; + + /* Under MDWE we do not accept newly writably executable VMAs... */ + if (new & VM_WRITE) + return true; + + /* ...nor previously non-executable VMAs becoming executable. */ + if (!(old & VM_EXEC)) + return true; + + return false; +} + +static inline int mapping_map_writable(struct address_space *mapping) +{ + int c = atomic_read(&mapping->i_mmap_writable); + + /* Derived from the raw_atomic_inc_unless_negative() implementation. */ + do { + if (c < 0) + return -EPERM; + } while (!__sync_bool_compare_and_swap(&mapping->i_mmap_writable, c, c+1)); + + return 0; +} + #endif /* __MM_VMA_INTERNAL_H */