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+aYRL26JWfm6eBUYZv7d8fbHpilCYuZXqSaP2qzNV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IJWaxhQtecm2rTxF6NhKjItgoGBRUkeJIBkuf52E0WYiXgXiQ0Hqbe6ZkmkIrzp3b4jiEaXPO/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);