From patchwork Tue Nov 5 15:50:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harshvardhan Jha X-Patchwork-Id: 13863137 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id CE58DD2B944 for ; Tue, 5 Nov 2024 15:50:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C856F6B0089; Tue, 5 Nov 2024 10:50:49 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C34576B008C; Tue, 5 Nov 2024 10:50:49 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AFC6A6B0096; Tue, 5 Nov 2024 10:50:49 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 928F26B0089 for ; Tue, 5 Nov 2024 10:50:49 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 437F81A145A for ; Tue, 5 Nov 2024 15:50:49 +0000 (UTC) X-FDA: 82752479082.14.B940C28 Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by imf09.hostedemail.com (Postfix) with ESMTP id E032014001F for ; Tue, 5 Nov 2024 15:50:23 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b="E/KjK3/o"; spf=pass (imf09.hostedemail.com: domain of harshvardhan.j.jha@oracle.com designates 205.220.177.32 as permitted sender) smtp.mailfrom=harshvardhan.j.jha@oracle.com; dmarc=pass (policy=reject) header.from=oracle.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730821663; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=a7kihQ+TcbsALW39TQkdlh/s0BSzmVBk+p3H5mqQ5Us=; b=5rWR++29A66Vn+gIfNx8iZ/9sIb9+/iSbfXInxTp1qqxh93NfwbGW7Do8L7Q8vamlXz/+r KtS1MnURuP3gr8SSgQnrYMP2Yr+8dfT8lfh7UBmxiQpdXFeRU6TkACKVCMjxJj+VfjiYfO hlfullXRbUVf8hTQ2nkHxHmvnxpOVDs= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b="E/KjK3/o"; spf=pass (imf09.hostedemail.com: domain of harshvardhan.j.jha@oracle.com designates 205.220.177.32 as permitted sender) smtp.mailfrom=harshvardhan.j.jha@oracle.com; dmarc=pass (policy=reject) header.from=oracle.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730821663; a=rsa-sha256; cv=none; b=0mAWBFBtNXOoyDAxPc89xUIySjkHEl5Uqrs9j8Gm33+MhGZXM/j5kxKbueBKwrBv+uITr0 8fWWMrYekWm+ts6vBHYda3RoNgP6A+VDyDNnqFsrMaZiE0lYttSGuPm81uhCv82y5ZF5tZ VGUQ8nhLAbNX9w4/P6OEbfM22MuCldc= Received: from pps.filterd (m0333520.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 4A5DiXhE029639; Tue, 5 Nov 2024 15:50:46 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=cc :content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=corp-2023-11-20; bh=a7kih Q+TcbsALW39TQkdlh/s0BSzmVBk+p3H5mqQ5Us=; b=E/KjK3/ohD+56/4CvE/LR QdxF5w9uN2FE2upixV0ljjqW92ek0QnNf9Y1CrTLDle7PaAurJeXLlks8yUNl9Tg t4x2kqLqUQJrpM4eiaAsRb96UiWxIEwH+IByVpLWvK2FR4AxK6YWMadR4nHNz5JE QouQqE9iU7vxLlDXsIoweBM7zLzqDhJ8Wo1QwFZFYc9lY34EjbQ8C/yig4PIPw22 aKmo81yyvUrvlkF7ejbU/x+1tYxfOvJr+zis1ZmAxE/asx2IHJxYSWhLDRGgy0PB qbDkqx6bMpi1Hy8zlPqv/8A8WB27c7sULau1TaHNyqKxurou/O1jiYOM+GClQnsD g== Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.appoci.oracle.com [147.154.18.20]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42nc4bwqs3-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 05 Nov 2024 15:50:46 +0000 (GMT) Received: from pps.filterd (iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 4A5EFAjE035653; Tue, 5 Nov 2024 15:50:45 GMT Received: from pps.reinject (localhost [127.0.0.1]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 42nahdkgk7-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 05 Nov 2024 15:50:45 +0000 Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 4A5Foi0K028682; Tue, 5 Nov 2024 15:50:45 GMT Received: from ca-dev112.us.oracle.com (ca-dev112.us.oracle.com [10.129.136.47]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTP id 42nahdkghq-2; Tue, 05 Nov 2024 15:50:44 +0000 From: Harshvardhan Jha To: akpm@linux-foundation.org Cc: harshvardhan.j.jha@oracle.com, linux-mm@kvack.org, stable@vger.kernel.org Subject: [PATCH 5.10.y 1/2] mm: add remap_pfn_range_notrack Date: Tue, 5 Nov 2024 07:50:41 -0800 Message-ID: <20241105155042.288813-2-harshvardhan.j.jha@oracle.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20241105155042.288813-1-harshvardhan.j.jha@oracle.com> References: <20241105155042.288813-1-harshvardhan.j.jha@oracle.com> MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-11-05_06,2024-11-05_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 malwarescore=0 spamscore=0 mlxlogscore=999 mlxscore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2411050122 X-Proofpoint-ORIG-GUID: 3Wk9KWOUtzR3TrBXL57Alnvs61QhUYcF X-Proofpoint-GUID: 3Wk9KWOUtzR3TrBXL57Alnvs61QhUYcF X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: E032014001F X-Stat-Signature: 7qp4rw8me4xi3xxcj56hy4sar7ffu8is X-Rspam-User: X-HE-Tag: 1730821823-596591 X-HE-Meta: U2FsdGVkX1/PYlUzgHtv3RrXcvezk2e1WGO+8Z3ehv7QuRHOcbDA8+CqQuxbMeofWSpPCvVBvT/YhHpIMSZdNXWy4s2yUQWqkhBeSaA+DSihQVKcyyAC3eUKtch6B4MXPyaOC7fhjTMqz3oHMQQ4BuOjHu3iNGR/HUokQmzgz/Lj7aEFGfkJHmrpbrAPWTc+aYRL26JWfm6eBUYZv7d8fbHpilCYuZXqSaP2qzNV5tHGmsnrqPN3yqjjygx2VIlApv2zz1ZUjZJcqaKz+YoPZydtxJXNGZE/5btC4u6G01qzE1fibFdcc1kmpDM3klT7XMHBeNrTyTK5JF2uga/4xAuWsdAFPmChpe0uDjZ1fkdJuq5LcQvqJ5+g8UjPz7YEvH8NZ2Qjx8rnrb526fznCIuADWXu16KALMJUD+5YfKSVWUXCTNpKkp2+1OfmqIUgcbFsJJV/PaxQUrzZqLSXn/FSTqwdqtd2rS/157651n7e0MdV+XRPhh/SX9DXCDfUCc90U5QdM0AF3/npCNGPKVIvnQwJcLXM7AEWNddU1fxNvYRtUp8QqgfX2C555K588mAowEwl8LP8rxnPUr7SM9Ypetknp/s1+LJy/051po0Eao6y24w/0n0jeXCzAJtto69KuAoB8orly82DZhK4Xoct5yAfdizq/Q+oHgR65OJOfzPGzlv9R2NRzz76Xuj5cMtfDstQyMSMaK8CZ3b61/jDmVrUCFbJNT8q4LSzZDT1EKxG2nD+SVDosbfgQU7nruBWTCAD7oOA3v3A7KgWgSf4M2j8BJhKPHt9OHtiN9lYvmJB8ZUcjs9blVV+9kIEH0VdwHctwcZWvPmofL4Z+/1RZqvaAF6nx0JKf9CgjJ7gplNv1KOhRw8zuOGUiu0V8+vaZFGN4tk+LmwtYHe8yeoc6lzWCipUT8BOA3jp+wtGaCgRCTj8LV3vV/d1YRXTSrZ2Pwwzijd+dcYuisE Vdqigu+j 8hYwgZWyPLVSta+/6ucffK+F2lxRjltAEqISRDhxx7ippbNrCcauCW8kJj3vxocmWexWJkGs0lS3ab2rRjgVFhuhfUfQzku7EkzLFYOE8FAgFlXOsBWXh0uOiBoSuH4GfhkXI9FahCEQrswo8SK/838SDB2lSpJ1l6lDl0DSfU8EtpxS4nUSzxkj1kO5w8Ia8dBJAwP+PvjxEqej9U+BMi6/1tg88D1z7E8lJS7oQrEom6WjfRRXILxiQw6UZNo004wnRCnJpV1HFk0gGlXAI9kdh010jBY4MXZHjxI9ONF4aAsWe7ETQrWQILUIt/5OxeGzn9IJWaxhQtecm2rTxF6NhKjItgoGBRUkeJIBkuf52E0WYiXgXiQ0Hqbe6ZkmkIrzp3b4jiEaXPO/zMq61Ue3R73PPs2h3jeA3czJYSSrqgM3JYmvwKBuVY6641Rw1PSPZzi9fg12I+AjAe2hP1GM495hW93XK6eS0N1R8yNEpWt513ON9AZCa1RGbu6YvGHVyV16cXU7O4U2k4fpGcNdIdSJPsPkPcJAhHx/2+MmE/HfCOAEoEIDndfeLq23SVY6f8dD4ZNStqwMfZbUIh5nC+P0d3SAorq3zjCC18ew3g2g= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Christoph Hellwig commit 74ffa5a3e68504dd289135b1cf0422c19ffb3f2e Patch series "add remap_pfn_range_notrack instead of reinventing it in i915", v2. i915 has some reason to want to avoid the track_pfn_remap overhead in remap_pfn_range. Add a function to the core VM to do just that rather than reinventing the functionality poorly in the driver. Note that the remap_io_sg path does get exercises when using Xorg on my Thinkpad X1, so this should be considered lightly tested, I've not managed to hit the remap_io_mapping path at all. This patch (of 4): Add a version of remap_pfn_range that does not call track_pfn_range. This will be used to fix horrible abuses of VM internals in the i915 driver. Link: https://lkml.kernel.org/r/20210326055505.1424432-1-hch@lst.de Link: https://lkml.kernel.org/r/20210326055505.1424432-2-hch@lst.de Signed-off-by: Christoph Hellwig Acked-by: Daniel Vetter Cc: Jani Nikula Cc: Joonas Lahtinen Cc: Rodrigo Vivi Cc: Chris Wilson Cc: Peter Zijlstra Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds (cherry picked from commit 74ffa5a3e68504dd289135b1cf0422c19ffb3f2e) Signed-off-by: Harshvardhan Jha --- include/linux/mm.h | 2 ++ mm/memory.c | 51 ++++++++++++++++++++++++++++------------------ 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index b8b677f47a8da..94e630862d58c 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2749,6 +2749,8 @@ unsigned long change_prot_numa(struct vm_area_struct *vma, struct vm_area_struct *find_extend_vma(struct mm_struct *, unsigned long addr); int remap_pfn_range(struct vm_area_struct *, unsigned long addr, unsigned long pfn, unsigned long size, pgprot_t); +int remap_pfn_range_notrack(struct vm_area_struct *vma, unsigned long addr, + unsigned long pfn, unsigned long size, pgprot_t prot); int vm_insert_page(struct vm_area_struct *, unsigned long addr, struct page *); int vm_insert_pages(struct vm_area_struct *vma, unsigned long addr, struct page **pages, unsigned long *num); diff --git a/mm/memory.c b/mm/memory.c index 2183003687cec..40a6cc6df9003 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -2290,26 +2290,17 @@ static inline int remap_p4d_range(struct mm_struct *mm, pgd_t *pgd, return 0; } -/** - * remap_pfn_range - remap kernel memory to userspace - * @vma: user vma to map to - * @addr: target page aligned user address to start at - * @pfn: page frame number of kernel physical memory address - * @size: size of mapping area - * @prot: page protection flags for this mapping - * - * Note: this is only safe if the mm semaphore is held when called. - * - * Return: %0 on success, negative error code otherwise. +/* + * Variant of remap_pfn_range that does not call track_pfn_remap. The caller + * must have pre-validated the caching bits of the pgprot_t. */ -int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr, - unsigned long pfn, unsigned long size, pgprot_t prot) +int remap_pfn_range_notrack(struct vm_area_struct *vma, unsigned long addr, + unsigned long pfn, unsigned long size, pgprot_t prot) { pgd_t *pgd; unsigned long next; unsigned long end = addr + PAGE_ALIGN(size); struct mm_struct *mm = vma->vm_mm; - unsigned long remap_pfn = pfn; int err; if (WARN_ON_ONCE(!PAGE_ALIGNED(addr))) @@ -2339,10 +2330,6 @@ int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr, vma->vm_pgoff = pfn; } - err = track_pfn_remap(vma, &prot, remap_pfn, addr, PAGE_ALIGN(size)); - if (err) - return -EINVAL; - vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP; BUG_ON(addr >= end); @@ -2354,12 +2341,36 @@ int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr, err = remap_p4d_range(mm, pgd, addr, next, pfn + (addr >> PAGE_SHIFT), prot); if (err) - break; + return err; } while (pgd++, addr = next, addr != end); + return 0; +} + +/** + * remap_pfn_range - remap kernel memory to userspace + * @vma: user vma to map to + * @addr: target page aligned user address to start at + * @pfn: page frame number of kernel physical memory address + * @size: size of mapping area + * @prot: page protection flags for this mapping + * + * Note: this is only safe if the mm semaphore is held when called. + * + * Return: %0 on success, negative error code otherwise. + */ +int remap_pfn_range(struct vm_area_struct *vma, unsigned long addr, + unsigned long pfn, unsigned long size, pgprot_t prot) +{ + int err; + + err = track_pfn_remap(vma, &prot, pfn, addr, PAGE_ALIGN(size)); if (err) - untrack_pfn(vma, remap_pfn, PAGE_ALIGN(size)); + return -EINVAL; + err = remap_pfn_range_notrack(vma, addr, pfn, size, prot); + if (err) + untrack_pfn(vma, pfn, PAGE_ALIGN(size)); return err; } EXPORT_SYMBOL(remap_pfn_range); From patchwork Tue Nov 5 15:50:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harshvardhan Jha X-Patchwork-Id: 13863138 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id C7792D2C13E for ; Tue, 5 Nov 2024 15:50:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B9BA06B0098; Tue, 5 Nov 2024 10:50:50 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B245D6B0099; Tue, 5 Nov 2024 10:50:50 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 99EBE6B009A; Tue, 5 Nov 2024 10:50:50 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 744A96B0098 for ; Tue, 5 Nov 2024 10:50:50 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 21CD441473 for ; Tue, 5 Nov 2024 15:50:50 +0000 (UTC) X-FDA: 82752477780.03.9A8CEEF Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by imf24.hostedemail.com (Postfix) with ESMTP id 784BD18000A for ; Tue, 5 Nov 2024 15:50:43 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b="MXmEB/Hb"; spf=pass (imf24.hostedemail.com: domain of harshvardhan.j.jha@oracle.com designates 205.220.177.32 as permitted sender) smtp.mailfrom=harshvardhan.j.jha@oracle.com; dmarc=pass (policy=reject) header.from=oracle.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730821763; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=+P0MvUmtvX+pdbAUezamlBg8piG53Ca6UVptHTajYJ8=; b=0CguaAhCtenZH/qrVRJAFV2kXEhDpOqlTx4UvCqpPLnqS49sVsh57tmQIYCPvlZvagENsC lwQBx+fGXbrkdJhBJ73b5O7OiTJnJFXQoIvCQqIv7LcUh1gELKnPjk4RKin68ZW3pi2CJU Zlw9Y21Sf5NEqz0VRTibDLVU4zO56Ds= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730821763; a=rsa-sha256; cv=none; b=Gy2G9eiHc4HnZQSQnrfKH1lF9GQMaj9vMQoH4iJqzF1iv+aUq9ls/ptltuefOVFM+WrRJo tsG5GSlHbQ6UESXA5q7Nwx1dvc92ocL3piZHYHFUs71wp31siZigbwx76ll7XBEhhxsc3V 3bIR1q53BcPtIOD21rJOpn9cLq9+nRM= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b="MXmEB/Hb"; spf=pass (imf24.hostedemail.com: domain of harshvardhan.j.jha@oracle.com designates 205.220.177.32 as permitted sender) smtp.mailfrom=harshvardhan.j.jha@oracle.com; dmarc=pass (policy=reject) header.from=oracle.com Received: from pps.filterd (m0333520.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 4A5DiYQV029653; Tue, 5 Nov 2024 15:50:47 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=cc :content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=corp-2023-11-20; bh=+P0Mv UmtvX+pdbAUezamlBg8piG53Ca6UVptHTajYJ8=; b=MXmEB/HbVWDlr4wdK74Kj 2QQX78MaLYq9xOWmsvGB/vC7LZavYWWx6P6wvAKAx7vkCf3Tnlc53CXsI4zq058l CYwL9CrHRUTm+1dPeLxWv6XxsKfw89Lx5F7xjbhl3NtuOpekpkcibxMLo4uOuQpc vJfw8ESYBDOGtN+VW0xMNpMz6hrstaNjniE7JVRnL69fq1LtkCBfh4S6konq3JZ1 vtpcNLKVgfJs5rE7yI96TQMwUHenq4ACAmIJZCfl2mmv1KxX6ChUieo/FK4gJ8y/ Eiv7jIHSof7qqd7a6lll1plPMLnVMPTZUaxHiOksg13BnSJ40sdJqB09KsySU0OC Q== Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.appoci.oracle.com [147.154.18.20]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42nc4bwqs5-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 05 Nov 2024 15:50:46 +0000 (GMT) Received: from pps.filterd (iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (8.18.1.2/8.18.1.2) with ESMTP id 4A5EUi9w035597; Tue, 5 Nov 2024 15:50:46 GMT Received: from pps.reinject (localhost [127.0.0.1]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 42nahdkgkv-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 05 Nov 2024 15:50:46 +0000 Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 4A5Foi0M028682; Tue, 5 Nov 2024 15:50:45 GMT Received: from ca-dev112.us.oracle.com (ca-dev112.us.oracle.com [10.129.136.47]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTP id 42nahdkghq-3; Tue, 05 Nov 2024 15:50:45 +0000 From: Harshvardhan Jha To: akpm@linux-foundation.org Cc: harshvardhan.j.jha@oracle.com, linux-mm@kvack.org, stable@vger.kernel.org Subject: [PATCH 5.10.y 2/2] mm: avoid leaving partial pfn mappings around in error case Date: Tue, 5 Nov 2024 07:50:42 -0800 Message-ID: <20241105155042.288813-3-harshvardhan.j.jha@oracle.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20241105155042.288813-1-harshvardhan.j.jha@oracle.com> References: <20241105155042.288813-1-harshvardhan.j.jha@oracle.com> MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1057,Hydra:6.0.680,FMLib:17.12.62.30 definitions=2024-11-05_06,2024-11-05_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=0 malwarescore=0 spamscore=0 mlxlogscore=971 mlxscore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2411050122 X-Proofpoint-ORIG-GUID: y1O3ccfMcuZIrW3QjvrQnmENMAZREd1X X-Proofpoint-GUID: y1O3ccfMcuZIrW3QjvrQnmENMAZREd1X X-Rspamd-Server: rspam10 X-Stat-Signature: 1nuznmudfa8a5m55hdf7uocf8m3c1hod X-Rspamd-Queue-Id: 784BD18000A X-Rspam-User: X-HE-Tag: 1730821843-183838 X-HE-Meta: U2FsdGVkX1+ukphRhc/+c5IRU+rEEriVmPUojFf3zwfummdqo0b6Ov+vYJdj1hwHt3Yv5MHvuJJIVZNYxanUm0LAd6zYvHWfziHj2am0zVhkk8fzGTpllN7JMYNpt+w1vpj2Jr7yymYBvxrojQb17GSWJxN2YZpdl6VmLQGj0nMcOdl8gthz9nvZ3SIp5pbXPF8RbrU0LbAqRz10wD6cCqCC4oB7mS1mdWsKoESCtRjdepRweZRjoQxWuhcCItx9rkzCjQxh9SEEMwhm6wjCMIXKnM+P/Xy1iNsC+7XzELbzfL80NG/hs7vvpXO+Lb0VIP+cU72jI2L/7EoDlUg0FsArxuiyEhv0fN/gSOODsD9Xj/XgBC1u5WmCDs7UYzE2H7Fa4clGNd31W/Exjq9OroHGJAFrD/KmzBLIKbb7nYKytqnE13nZYkLvYR22HT28rwX3ML/+jY6dC4cebaw4phOq+kYftVyzvuqPzMzr/HcQBWPaEVMdUnu4fc26ydHAJYPq/EJiRCd2CIxh2aQMBUJdBp+kR0Iyw2CpiLmbdfusTrOEu0NuNq82Q7W1ma4xq51cMT9tQJSjaro3f0o5dh11z47uC59fSUxGmJUUsQdWspin/bu2eEWxEP+NnqlRTs0zpe+XChJkjwA9g6sI8VZKnpmg0+VgIcyuV5soZ2X6JB2dD/LuowCYwyuyJgOG1s8PDSshm0ZBlZHVsFk9ZD3G0IXWtZwYuDcYSj10913dtprIZFJXHplD248yrYErMgKD+8uXugxExpA5JpTMxuh7M2K4kpIHEhMzVQExRTJFK4ujweIMCooAxtxcNbucxSmF/8xsE465sYSt7ZoYyaQoLiG27BigIUDcWDoFzOCVdEwnB1Kh8F/kbx82ZtfNaF+Nsamz+id9zgsCDNmaCSPC84IQWNc1ryvph39IdqHCtBfZLLeasOgvnL4WvAdYWic9SUKy4VyuuABDhpQ oC1efjjc /YMzoGqg6DrY4x0yWe4P4kcrqPpPDwv4BPw8LUoJXAyTY/ZSZ1Vh+bOJ43uAlzm/n8ZKw0ufb8cAewuZ/OW2szA42PmWaKXjZhhfIujjRtl/OttDuhL3YWgFirIqNsikWerIkEOcwlYiabwPF4nAHr8eXprj1B9xC3xnKDqRNPX6f+UWcy4dMkJOz9SRrhsoP8mJRYez8ZoLYrkh4u6FJq0dQ3emBb6qbNmR5GFxetGs9mImBbz7k6HA3sMeQd83nqvrin1oqa87Ixt6fqmHI5djEgZ5SY1PUZVIRgHxBXY6xeyk5qtvYvl3ka7BghdvtlrSFQ6G3ICEGEdGIAAd1JrXpSSs322m9r/3gA3qesF4Es5t3v7Dj0kiRkJBKl6AptUQzPGV6weP7qa0xDVXng/2Ior0ChOV9Yom1LWCdrVQRwYDtv+Gia5aIC2lH0pFndxWwxwrn9aM+T4BeL9nRnl8kn1p/5oKwrdnNlzknquarAz/h7yNppSKtL08IOHi4gLPKmi3crOPWKc6oeaFRKN2I/A== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Linus Torvalds commit 79a61cc3fc0466ad2b7b89618a6157785f0293b3 upstream. As Jann points out, PFN mappings are special, because unlike normal memory mappings, there is no lifetime information associated with the mapping - it is just a raw mapping of PFNs with no reference counting of a 'struct page'. That's all very much intentional, but it does mean that it's easy to mess up the cleanup in case of errors. Yes, a failed mmap() will always eventually clean up any partial mappings, but without any explicit lifetime in the page table mapping itself, it's very easy to do the error handling in the wrong order. In particular, it's easy to mistakenly free the physical backing store before the page tables are actually cleaned up and (temporarily) have stale dangling PTE entries. To make this situation less error-prone, just make sure that any partial pfn mapping is torn down early, before any other error handling. Reported-and-tested-by: Jann Horn Cc: Andrew Morton Cc: Jason Gunthorpe Cc: Simona Vetter Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman (cherry picked from commit 5b2c8b34f6d76bfbd1dd4936eb8a0fbfb9af3959) Signed-off-by: Harshvardhan Jha --- mm/memory.c | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index 40a6cc6df9003..29cce8aadb618 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -2290,11 +2290,7 @@ static inline int remap_p4d_range(struct mm_struct *mm, pgd_t *pgd, return 0; } -/* - * Variant of remap_pfn_range that does not call track_pfn_remap. The caller - * must have pre-validated the caching bits of the pgprot_t. - */ -int remap_pfn_range_notrack(struct vm_area_struct *vma, unsigned long addr, +static int remap_pfn_range_internal(struct vm_area_struct *vma, unsigned long addr, unsigned long pfn, unsigned long size, pgprot_t prot) { pgd_t *pgd; @@ -2347,6 +2343,27 @@ int remap_pfn_range_notrack(struct vm_area_struct *vma, unsigned long addr, return 0; } +/* + * Variant of remap_pfn_range that does not call track_pfn_remap. The caller + * must have pre-validated the caching bits of the pgprot_t. + */ +int remap_pfn_range_notrack(struct vm_area_struct *vma, unsigned long addr, + unsigned long pfn, unsigned long size, pgprot_t prot) +{ + int error = remap_pfn_range_internal(vma, addr, pfn, size, prot); + + if (!error) + return 0; + + /* + * A partial pfn range mapping is dangerous: it does not + * maintain page reference counts, and callers may free + * pages due to the error. So zap it early. + */ + zap_page_range_single(vma, addr, size, NULL); + return error; +} + /** * remap_pfn_range - remap kernel memory to userspace * @vma: user vma to map to