From patchwork Thu Nov 14 15:34:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harshvardhan Jha X-Patchwork-Id: 13875275 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 BEB2AD68B32 for ; Thu, 14 Nov 2024 15:34:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9DBE16B00B9; Thu, 14 Nov 2024 10:34:54 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 93D196B00BA; Thu, 14 Nov 2024 10:34:54 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7438D6B00BB; Thu, 14 Nov 2024 10:34:54 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 485376B00B9 for ; Thu, 14 Nov 2024 10:34:54 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id C26A51A1198 for ; Thu, 14 Nov 2024 15:34:53 +0000 (UTC) X-FDA: 82785096030.09.F76FDAB Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) by imf12.hostedemail.com (Postfix) with ESMTP id B0CB44000F for ; Thu, 14 Nov 2024 15:34:31 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b=mlsMyrpn; dmarc=pass (policy=reject) header.from=oracle.com; spf=pass (imf12.hostedemail.com: domain of harshvardhan.j.jha@oracle.com designates 205.220.177.32 as permitted sender) smtp.mailfrom=harshvardhan.j.jha@oracle.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1731598313; 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=1xUgbP7FM7tuWfjpTA/OYuQXMmkdV4tkkuGEzythhJQ=; b=l2MMRWrwzwuBB1wgM0fY88K4a/rjV/ybTpsG8zZo01wJhMznG630rP52lsJ1KP1GkSWc50 WKWVsWZWY5r8pU+O/KbrsR5iK34YkrYGrNCwU6HD4htcw8gT6wzC+hgqYTOhHbZTIvaWsc bEYBI/HmUJivfdZE75S+dsUBzdNk674= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=oracle.com header.s=corp-2023-11-20 header.b=mlsMyrpn; dmarc=pass (policy=reject) header.from=oracle.com; spf=pass (imf12.hostedemail.com: domain of harshvardhan.j.jha@oracle.com designates 205.220.177.32 as permitted sender) smtp.mailfrom=harshvardhan.j.jha@oracle.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731598313; a=rsa-sha256; cv=none; b=26kuIA8HyS0ePMR90EeWV/2R82xK/u6evrPG+mgfEUBfQYbmXhq61PJ4Y7CadIRcaEIY7U tHPNrFbrm3U6UH4naiwZAqCPvHdD3PZcF8lnGO7LP04SSZ5cJNF9d1HxJk/9k/3+kGdQyY 72e9gFL6pL4m/AuYTBglwJjoCqA+vtw= Received: from pps.filterd (m0246630.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 4AED1W0h002323; Thu, 14 Nov 2024 15:34:50 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=1xUgb P7FM7tuWfjpTA/OYuQXMmkdV4tkkuGEzythhJQ=; b=mlsMyrpndH3YRkiEJsZGt I0tzP3GJAsBAygUKTQfuD1REUVGsFjwmPO6LDDrkaHlV2n2n6yVvCQGOVZKuG8JG ihh4H1zynQT5CKVi1ElQIzrWINFhdc2rioTTJQMYGLjaeAGCcD5C8aZ6+6cGAqd9 XxSy+SMtiCKGbrpGMWNsySCivglFmiWHOAvuQ9CTVII/gmTo66ig8ZrQi52AsUkw cKSyjdl9HvNEcADARCH5x8sb7+ISHKw0l2rZjlsNIV+ADSEEZEeVD7Na0aOlJL1T NqbZG6RtKpgSKZqhGfzz0Ykrzb0bRzjDKz8CIUiH3XNVDZuWLQGIaXW2gMMiikXC A== Received: from iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta02.appoci.oracle.com [147.154.18.20]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 42t0k5hcwc-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 14 Nov 2024 15:34:50 +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 4AEEsJmo001205; Thu, 14 Nov 2024 15:34:50 GMT Received: from pps.reinject (localhost [127.0.0.1]) by iadpaimrmta02.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 42sx6bb03p-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 14 Nov 2024 15:34:49 +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 4AEFXvi3031217; Thu, 14 Nov 2024 15:34:49 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 42sx6baywk-5; Thu, 14 Nov 2024 15:34:49 +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.4.y 4/4] mm: add remap_pfn_range_notrack Date: Thu, 14 Nov 2024 07:34:43 -0800 Message-ID: <20241114153443.505015-5-harshvardhan.j.jha@oracle.com> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20241114153443.505015-1-harshvardhan.j.jha@oracle.com> References: <20241114153443.505015-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-14_05,2024-11-13_01,2024-09-30_01 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 malwarescore=0 adultscore=0 phishscore=0 bulkscore=0 suspectscore=0 spamscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2409260000 definitions=main-2411140121 X-Proofpoint-ORIG-GUID: fXCvyGJpMdYDnIU8yR-qpq6yfkyneqWY X-Proofpoint-GUID: fXCvyGJpMdYDnIU8yR-qpq6yfkyneqWY X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: B0CB44000F X-Stat-Signature: 8w18m38y3a6977ar6pktu4ag9bs3t3qy X-Rspam-User: X-HE-Tag: 1731598471-33047 X-HE-Meta: U2FsdGVkX18zYHYDyJuoOeTlYBofQPh8PDPOXFVsBaRSGNs0Lc9rr5QvoKvMer33G57dOIXP1g5+c9533OPcb4NT4HLJ99FvF4FRkOPH6jYKodt0vSMWhMIgqdEGmCvCvKuXagFL7+3YBFxeC8FbHUr992Ed4vJqJ7HAZ7bVP2+GS2Qq3qy+bS+KiTccojiWw+VaMfOAPio5hZvhHhMvRnfmP7RM2JG5Ju5U/U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GVpZKgMYidgn4oQjh5U74Vh5WqxTvyO7xoBaCWs4POfO8yWO6JK8bdyVgyV7JkiW8wq9PQHysCpGnICt+eM+t9KfJVAGrI1uvGkZfNBiyew6O1x2bI2NQoQDFBtQW/uGLFLuckLpqaFsNURg3/cz8nhWfHr53LyxMANcR87j728V4kdURn1W0LwR2LgJ+XW2rxYUgQMPH6scfmlfbEHxUVEYzQjHsmkMmdvlhBh5a44Ub+MG2M79S6PgdDgc5JSJxColzMPLTYcfJp88Meo6aTLsnNfg3r74QPWI33Q12+XLMQCurDnDggHGMpnOfL01vTX6zgpSo5QtvFsYGYBbTCGC+70apxMMU47gu 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 upstream. 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 Signed-off-by: Harshvardhan Jha Signed-off-by: Greg Kroah-Hartman (cherry picked from commit 69d4e1ce9087c8767f2fe9b9426fa2755c8e9072) 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 d14aba548ff4e..4d3657b630dba 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -2566,6 +2566,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_map_pages(struct vm_area_struct *vma, struct page **pages, unsigned long num); diff --git a/mm/memory.c b/mm/memory.c index 1d009d3d87b34..fc7454cc138b6 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -1917,26 +1917,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))) @@ -1966,10 +1957,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); @@ -1981,12 +1968,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);