From: Christoph Hellwig <hch@lst.de>
To: Andrew Morton <akpm@linux-foundation.org>,
Jani Nikula <jani.nikula@linux.intel.com>,
Joonas Lahtinen <joonas.lahtinen@linux.intel.com>,
Rodrigo Vivi <rodrigo.vivi@intel.com>
Cc: Chris Wilson <chris@chris-wilson.co.uk>,
Daniel Vetter <daniel.vetter@ffwll.ch>,
Peter Zijlstra <peterz@infradead.org>,
intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org,
linux-mm@kvack.org
Subject: [PATCH 1/4] mm: add remap_pfn_range_notrack
Date: Fri, 26 Mar 2021 06:55:02 +0100 [thread overview]
Message-ID: <20210326055505.1424432-2-hch@lst.de> (raw)
In-Reply-To: <20210326055505.1424432-1-hch@lst.de>
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.
Signed-off-by: Christoph Hellwig <hch@lst.de>
---
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 64a71bf2053674..70a6160d729ded 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -2709,6 +2709,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 5efa07fb6cdc18..997cda24c4a0be 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -2260,26 +2260,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)))
@@ -2309,10 +2300,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);
@@ -2324,12 +2311,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);
--
2.30.1
next prev parent reply other threads:[~2021-03-26 5:55 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-03-26 5:55 add remap_pfn_range_notrack instead of reinventing it in i915 v2 Christoph Hellwig
2021-03-26 5:55 ` Christoph Hellwig [this message]
2021-03-26 5:55 ` [PATCH 2/4] mm: add a io_mapping_map_user helper Christoph Hellwig
2021-10-20 15:40 ` [Intel-gfx] " Lucas De Marchi
2021-10-20 19:37 ` Peter Zijlstra
2021-10-21 6:18 ` Christoph Hellwig
2021-03-26 5:55 ` [PATCH 3/4] i915: use io_mapping_map_user Christoph Hellwig
2021-03-26 5:55 ` [PATCH 4/4] i915: fix remap_io_sg to verify the pgprot Christoph Hellwig
2021-05-08 19:33 ` [Intel-gfx] " youling257
2021-05-10 8:58 ` Christoph Hellwig
2021-05-16 16:06 ` Serge Belyshev
2021-05-17 12:37 ` Christoph Hellwig
2021-05-17 13:09 ` Serge Belyshev
2021-05-17 13:11 ` Christoph Hellwig
2021-05-17 17:06 ` [Intel-gfx] " Matthew Auld
2021-05-18 13:21 ` Christoph Hellwig
2021-05-18 15:00 ` Matthew Auld
2021-05-19 5:46 ` Thomas Hellström (Intel)
2021-05-17 21:46 ` Thomas Hellström
2021-05-18 6:46 ` Thomas Hellström
2021-05-18 13:24 ` Christoph Hellwig
2021-05-18 13:33 ` Thomas Hellström
2021-05-18 13:23 ` Christoph Hellwig
2021-05-19 5:51 ` Thomas Hellström
2021-04-08 10:36 ` add remap_pfn_range_notrack instead of reinventing it in i915 v2 Daniel Vetter
2021-04-08 11:28 ` Christoph Hellwig
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210326055505.1424432-2-hch@lst.de \
--to=hch@lst.de \
--cc=akpm@linux-foundation.org \
--cc=chris@chris-wilson.co.uk \
--cc=daniel.vetter@ffwll.ch \
--cc=dri-devel@lists.freedesktop.org \
--cc=intel-gfx@lists.freedesktop.org \
--cc=jani.nikula@linux.intel.com \
--cc=joonas.lahtinen@linux.intel.com \
--cc=linux-mm@kvack.org \
--cc=peterz@infradead.org \
--cc=rodrigo.vivi@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).