Skip to contentSkip to conversation

kmap: Create mem*_page interfaces

PATCH Superseded [email protected] 8 participants 33 messages Nov 24, 2020 – Dec 7, 2020 2 messages not in the archive

Subsystems (22)
DRM drivers VFS SCSI DRM misc +18 more subsystems

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover kmap: Create mem*_page interfaces

From: Ira Weiny <[email protected]>

The following pattern is used often:

	kmap()
	memcpy(), memmove(), or memset()
	kunmap()

The problem with this is 2 fold: 1) This is best done with k[un]map_atomic().
2) kmap() is expanding and evolving beyond the use of highmem.

To the second point we have new functionality being placed behind kmap, such as PKS, which has nothing to do with highmem. Also we have new kmap interfaces, kmap_local() which allow for a more fined grained mapping of pages and would be very appropriate for the above pattern.

iov_iter.c already defined 3 functions which do most of what we want.

memcpy_from_page()
memcpy_to_page()
memzero_page()

Lift these to the core and enhance with memcpy_page(), memmove_page(), and memset_page(). Then replace the patterns throughout the kernel as appropriate.

Once the kmap_local() implementation is finalized the kmap_atomic() can be replaced with kmap_local(). For the moment use kmap_atomic().

Ira Weiny (17):
mm/highmem: Lift memcpy_[to|from]_page and memset_page to core drivers/firmware_loader: Use new memcpy_[to|from]_page() drivers/gpu: Convert to mem*_page()
fs/afs: Convert to memzero_page()
fs/btrfs: Convert to memzero_page()
fs/hfs: Convert to mem*_page() interface
fs/cifs: Convert to memcpy_page()
fs/hfsplus: Convert to mem*_page()
fs/f2fs: Remove f2fs_copy_page()
fs/freevxfs: Use memcpy_to_page()
fs/reiserfs: Use memcpy_from_page()
fs/cramfs: Use memcpy_from_page()
drivers/target: Convert to mem*_page()
drivers/scsi: Use memcpy_to_page()
drivers/staging: Use memcpy_to/from_page()
lib: Use mempcy_to/from_page()
samples: Use memcpy_to/from_page()

 drivers/base/firmware_loader/fallback.c   | 11 +++--
 drivers/gpu/drm/gma500/gma_display.c      |  7 ++-
 drivers/gpu/drm/gma500/mmu.c              |  4 +-
 drivers/gpu/drm/i915/gem/i915_gem_shmem.c |  6 +--
 drivers/gpu/drm/i915/gt/intel_gtt.c       |  9 +---
 drivers/gpu/drm/i915/gt/shmem_utils.c     |  8 ++--
 drivers/scsi/ipr.c                        | 11 +----
 drivers/staging/rts5208/rtsx_transport.c  |  8 ++--
 drivers/target/target_core_rd.c           |  6 +--
 drivers/target/target_core_transport.c    | 10 ++---
 fs/afs/write.c                            |  5 +--
 fs/btrfs/inode.c                          | 21 +++------
 fs/cifs/smb2ops.c                         | 10 ++---
 fs/cramfs/inode.c                         |  3 +-
 fs/f2fs/f2fs.h                            | 10 -----
 fs/f2fs/file.c                            |  3 +-
 fs/freevxfs/vxfs_immed.c                  |  6 +--
 fs/hfs/bnode.c                            | 13 ++----
 fs/hfsplus/bnode.c                        | 53 +++++++----------------
 fs/reiserfs/journal.c                     |  9 ++--
 include/linux/pagemap.h                   | 49 +++++++++++++++++++++
 lib/iov_iter.c                            | 21 ---------
 lib/test_bpf.c                            | 11 +----
 lib/test_hmm.c                            | 10 +----
 samples/vfio-mdev/mbochs.c                |  8 ++--
 25 files changed, 119 insertions(+), 193 deletions(-)
Signature · 1 line
2.28.0.rc0.12.gb6a658bd00c9
#1 lore raw

PATCH 1/17 mm/highmem: Lift memcpy_[to|from]_page and memset_page to core

From: Ira Weiny <[email protected]>

Working through a conversion to a call such as kmap_thread() revealed many places where the pattern kmap/memcpy/kunmap occurred.

Eric Biggers, Matthew Wilcox, Christoph Hellwig, Dan Williams, and Al Viro all suggested putting this code into helper functions. Al Viro further pointed out that these functions already existed in the iov_iter code.[1]

Placing these functions in 'highmem.h' is suboptimal especially with the changes being proposed in the functionality of kmap. From a caller perspective including/using 'highmem.h' implies that the functions defined in that header are only required when highmem is in use which is increasingly not the case with modern processors. Some headers like mm.h or string.h seem ok but don't really portray the functionality well. 'pagemap.h', on the other hand, makes sense and is already included in many of the places we want to convert.

Another alternative would be to create a new header for the promoted memcpy functions, but it masks the fact that these are designed to copy to/from pages using the kernel direct mappings and complicates matters with a new header.

Lift memcpy_to_page(), memcpy_from_page(), and memzero_page() to pagemap.h.

Also, add a memcpy_page(), memmove_page, and memset_page() to cover more kmap/mem*/kunmap. patterns.

[1] https://lore.kernel.org/lkml/[email protected]/

    https://lore.kernel.org/lkml/[email protected]/
---
 include/linux/pagemap.h | 49 +++++++++++++++++++++++++++++++++++++++++
 lib/iov_iter.c          | 21 ------------------
 2 files changed, 49 insertions(+), 21 deletions(-)
include/linux/pagemap.h +49 -0
index c77b7c31b2e4..82a0af6bc843 100644
@@ -1028,4 +1028,53 @@ unsigned int i_blocks_per_page(struct inode *inode, struct page *page)
 {
 	return thp_size(page) >> inode->i_blkbits;
 }
+
+static inline void memcpy_page(struct page *dst_page, size_t dst_off,
+			       struct page *src_page, size_t src_off,
+			       size_t len)
+{
+	char *dst = kmap_atomic(dst_page);
+	char *src = kmap_atomic(src_page);
+	memcpy(dst + dst_off, src + src_off, len);
+	kunmap_atomic(src);
+	kunmap_atomic(dst);
+}
+
+static inline void memmove_page(struct page *dst_page, size_t dst_off,
+			       struct page *src_page, size_t src_off,
+			       size_t len)
+{
+	char *dst = kmap_atomic(dst_page);
+	char *src = kmap_atomic(src_page);
+	memmove(dst + dst_off, src + src_off, len);
+	kunmap_atomic(src);
+	kunmap_atomic(dst);
+}
+
+static inline void memcpy_from_page(char *to, struct page *page, size_t offset, size_t len)
+{
+	char *from = kmap_atomic(page);
+	memcpy(to, from + offset, len);
+	kunmap_atomic(from);
+}
+
+static inline void memcpy_to_page(struct page *page, size_t offset, const char *from, size_t len)
+{
+	char *to = kmap_atomic(page);
+	memcpy(to + offset, from, len);
+	kunmap_atomic(to);
+}
+
+static inline void memset_page(struct page *page, int val, size_t offset, size_t len)
+{
+	char *addr = kmap_atomic(page);
+	memset(addr + offset, val, len);
+	kunmap_atomic(addr);
+}
+
+static inline void memzero_page(struct page *page, size_t offset, size_t len)
+{
+	memset_page(page, 0, offset, len);
+}
+
 #endif /* _LINUX_PAGEMAP_H */
lib/iov_iter.c +0 -21
index 1635111c5bd2..2439a8b4f0d2 100644
@@ -466,27 +466,6 @@ void iov_iter_init(struct iov_iter *i, unsigned int direction,
 }
 EXPORT_SYMBOL(iov_iter_init);
 
-static void memcpy_from_page(char *to, struct page *page, size_t offset, size_t len)
-{
-	char *from = kmap_atomic(page);
-	memcpy(to, from + offset, len);
-	kunmap_atomic(from);
-}
-
-static void memcpy_to_page(struct page *page, size_t offset, const char *from, size_t len)
-{
-	char *to = kmap_atomic(page);
-	memcpy(to + offset, from, len);
-	kunmap_atomic(to);
-}
-
-static void memzero_page(struct page *page, size_t offset, size_t len)
-{
-	char *addr = kmap_atomic(page);
-	memset(addr + offset, 0, len);
-	kunmap_atomic(addr);
-}
-
 static inline bool allocated(struct pipe_buffer *buf)
 {
 	return buf->ops == &default_pipe_buf_ops;
Signature · 1 line
2.28.0.rc0.12.gb6a658bd00c9
maintainer
#2 lore raw

Re: [PATCH 01/17] mm/highmem: Lift memcpy_[to|from]_page and memset_page to core

include/linux/pagemap.h unsigned int i_blocks_per_page(struct inode *inode, struct page *page) · line 1078

Quoted 4 lines · On Mon, Nov 23, 2020 at 10:07:39PM -0800, [email protected] wrote:
include/linux/pagemap.h +4 -0
+static inline void memzero_page(struct page *page, size_t offset, size_t len)
+{
+	memset_page(page, 0, offset, len);
+}

This is a less-capable zero_user_segments().

author
#3 lore raw

Re: [PATCH 01/17] mm/highmem: Lift memcpy_[to|from]_page and memset_page to core

Quoted 7 lines · Matthew Wilcox

include/linux/pagemap.h unsigned int i_blocks_per_page(struct inode *inode, struct page *page) · line 1078

Quoted 4 lines · On Mon, Nov 23, 2020 at 10:07:39PM -0800, [email protected] wrote:
include/linux/pagemap.h +4 -0
+static inline void memzero_page(struct page *page, size_t offset, size_t len)
+{
+	memset_page(page, 0, offset, len);
+}

This is a less-capable zero_user_segments().

Actually it is a duplicate of zero_user()... Sorry I did not notice those... :-(

Why are they called '_user_'?

Ira

maintainer
#4 lore raw

Re: [PATCH 01/17] mm/highmem: Lift memcpy_[to|from]_page and memset_page to core

Quoted 9 lines · Ira Weiny
Quoted 7 lines · Matthew Wilcox

include/linux/pagemap.h unsigned int i_blocks_per_page(struct inode *inode, struct page *page) · line 1078

Quoted 4 lines · On Mon, Nov 23, 2020 at 10:07:39PM -0800, [email protected] wrote:
include/linux/pagemap.h +4 -0
+static inline void memzero_page(struct page *page, size_t offset, size_t len)
+{
+	memset_page(page, 0, offset, len);
+}

This is a less-capable zero_user_segments().

Quoted 4 lines

Actually it is a duplicate of zero_user()... Sorry I did not notice those... :-(

Why are they called '_user_'?

git knows ...

commit 01f2705daf5a36208e69d7cf95db9c330f843af6

Author: Nate Diller <[email protected]>
Date:   Wed May 9 02:35:07 2007 -0700

fs: convert core functions to zero_user_page

It's very common for file systems to need to zero part or all of a page, the simplist way is just to use kmap_atomic() and memset(). There's actually a library function in include/linux/highmem.h that does exactly that, but it's confusingly named memclear_highpage_flush(), which is descriptive of *how* it does the work rather than what the *purpose* is. So this patchset renames the function to zero_user_page(), and calls it from the various places that currently open code it.

This first patch introduces the new function call, and converts all the core kernel callsites, both the open-coded ones and the old memclear_highpage_flush() ones. Following this patch is a series of conversions for each file system individually, per AKPM, and finally a patch deprecating the old call. The diffstat below shows the entire patchset.

A message that is not in the archive (sent off-list or not yet received): 160648238432.10416.12405581766428273347@jlahtine-mobl.ger.corp.intel.com

maintainer
#6 lore raw

Re: [PATCH 01/17] mm/highmem: Lift memcpy_[to|from]_page and memset_page to core

Quoted 44 lines · Joonas Lahtinen

Quoting [email protected] (2020-11-24 08:07:39)

Quoted 40 lines

From: Ira Weiny <[email protected]>

Working through a conversion to a call such as kmap_thread() revealed many places where the pattern kmap/memcpy/kunmap occurred.

Eric Biggers, Matthew Wilcox, Christoph Hellwig, Dan Williams, and Al Viro all suggested putting this code into helper functions. Al Viro further pointed out that these functions already existed in the iov_iter code.[1]

Placing these functions in 'highmem.h' is suboptimal especially with the changes being proposed in the functionality of kmap. From a caller perspective including/using 'highmem.h' implies that the functions defined in that header are only required when highmem is in use which is increasingly not the case with modern processors. Some headers like mm.h or string.h seem ok but don't really portray the functionality well. 'pagemap.h', on the other hand, makes sense and is already included in many of the places we want to convert.

Another alternative would be to create a new header for the promoted memcpy functions, but it masks the fact that these are designed to copy to/from pages using the kernel direct mappings and complicates matters with a new header.

Lift memcpy_to_page(), memcpy_from_page(), and memzero_page() to pagemap.h.

Also, add a memcpy_page(), memmove_page, and memset_page() to cover more kmap/mem*/kunmap. patterns.

[1] https://lore.kernel.org/lkml/[email protected]/

    https://lore.kernel.org/lkml/[email protected]/

<SNIP>

Quoted 9 lines

include/linux/pagemap.h unsigned int i_blocks_per_page(struct inode *inode, struct page *page) · line 1073

Quoted 6 lines
include/linux/pagemap.h +6 -0
+static inline void memset_page(struct page *page, int val, size_t offset, size_t len)
+{
+       char *addr = kmap_atomic(page);
+       memset(addr + offset, val, len);
+       kunmap_atomic(addr);
+}

Other functions have (page, offset) pair. Insertion of 'val' in the middle here required to take a double look during review.

Let's be explicit here. Your suggested order is:

(page, offset, val, len)

right? I think I would prefer that to (page, val, offset, len).

A message that is not in the archive (sent off-list or not yet received): 160672815223.3453.2374529656870007787@jlahtine-mobl.ger.corp.intel.com

author
#8 lore raw

Re: [PATCH 01/17] mm/highmem: Lift memcpy_[to|from]_page and memset_page to core

Quoted 56 lines · Joonas Lahtinen

Quoting Matthew Wilcox (2020-11-27 15:20:06)

Quoted 55 lines · Joonas Lahtinen
Quoted 53 lines

Quoting [email protected] (2020-11-24 08:07:39)

Quoted 40 lines

From: Ira Weiny <[email protected]>

Working through a conversion to a call such as kmap_thread() revealed many places where the pattern kmap/memcpy/kunmap occurred.

Eric Biggers, Matthew Wilcox, Christoph Hellwig, Dan Williams, and Al Viro all suggested putting this code into helper functions. Al Viro further pointed out that these functions already existed in the iov_iter code.[1]

Placing these functions in 'highmem.h' is suboptimal especially with the changes being proposed in the functionality of kmap. From a caller perspective including/using 'highmem.h' implies that the functions defined in that header are only required when highmem is in use which is increasingly not the case with modern processors. Some headers like mm.h or string.h seem ok but don't really portray the functionality well. 'pagemap.h', on the other hand, makes sense and is already included in many of the places we want to convert.

Another alternative would be to create a new header for the promoted memcpy functions, but it masks the fact that these are designed to copy to/from pages using the kernel direct mappings and complicates matters with a new header.

Lift memcpy_to_page(), memcpy_from_page(), and memzero_page() to pagemap.h.

Also, add a memcpy_page(), memmove_page, and memset_page() to cover more kmap/mem*/kunmap. patterns.

[1] https://lore.kernel.org/lkml/[email protected]/

    https://lore.kernel.org/lkml/[email protected]/

<SNIP>

include/linux/pagemap.h unsigned int i_blocks_per_page(struct inode *inode, struct page *page) · line 1073

Quoted 6 lines
include/linux/pagemap.h +6 -0
+static inline void memset_page(struct page *page, int val, size_t offset, size_t len)
+{
+       char *addr = kmap_atomic(page);
+       memset(addr + offset, val, len);
+       kunmap_atomic(addr);
+}

Other functions have (page, offset) pair. Insertion of 'val' in the middle here required to take a double look during review.

Quoted 7 lines
Quoted 5 lines

Let's be explicit here. Your suggested order is:

(page, offset, val, len)

right? I think I would prefer that to (page, val, offset, len).

Yeah, I think that would be most consistent order.

Yes as I have been reworking these I have found it odd as well. I'm going to swap it around. Been learning Coccinelle which has helped find other instances... So V2 is taking a bit of time.

Thanks,
Ira

#9 lore raw

PATCH 2/17 drivers/firmware_loader: Use new memcpy_[to|from]_page()

In reply to [email protected]

From: Ira Weiny <[email protected]>

Too many users are using kmap_*() incorrectly and a common pattern is for them to kmap/mempcy/kunmap. Change these calls to use the newly lifted memcpy_[to|from]_page() calls.

Cc Luis Chamberlain <[email protected]>
---
 drivers/base/firmware_loader/fallback.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)
drivers/base/firmware_loader/fallback.c +5 -6
index 4dec4b79ae06..dc93dc307d18 100644
@@ -10,6 +10,7 @@
 #include <linux/sysctl.h>
 #include <linux/vmalloc.h>
 #include <linux/module.h>
+#include <linux/pagemap.h>
 
 #include "fallback.h"
 #include "firmware.h"
@@ -317,19 +318,17 @@ static void firmware_rw(struct fw_priv *fw_priv, char *buffer,
 			loff_t offset, size_t count, bool read)
 {
 	while (count) {
-		void *page_data;
 		int page_nr = offset >> PAGE_SHIFT;
 		int page_ofs = offset & (PAGE_SIZE-1);
 		int page_cnt = min_t(size_t, PAGE_SIZE - page_ofs, count);
 
-		page_data = kmap(fw_priv->pages[page_nr]);
-
 		if (read)
-			memcpy(buffer, page_data + page_ofs, page_cnt);
+			memcpy_from_page(buffer, fw_priv->pages[page_nr],
+					 page_ofs, page_cnt);
 		else
-			memcpy(page_data + page_ofs, buffer, page_cnt);
+			memcpy_to_page(fw_priv->pages[page_nr], page_ofs,
+				       buffer, page_cnt);
 
-		kunmap(fw_priv->pages[page_nr]);
 		buffer += page_cnt;
 		offset += page_cnt;
 		count -= page_cnt;
Signature · 1 line
2.28.0.rc0.12.gb6a658bd00c9
#10 lore raw

PATCH 3/17 drivers/gpu: Convert to mem*_page()

In reply to [email protected]

From: Ira Weiny <[email protected]>

The pattern of kmap/mem*/kunmap is repeated. Use the new mem*_page() calls instead.

Cc Patrik Jakobsson <[email protected]>
Cc Jani Nikula <[email protected]>
Cc Joonas Lahtinen <[email protected]>
Cc Rodrigo Vivi <[email protected]>
---
 drivers/gpu/drm/gma500/gma_display.c      | 7 +++----
 drivers/gpu/drm/gma500/mmu.c              | 4 ++--
 drivers/gpu/drm/i915/gem/i915_gem_shmem.c | 6 ++----
 drivers/gpu/drm/i915/gt/intel_gtt.c       | 9 ++-------
 drivers/gpu/drm/i915/gt/shmem_utils.c     | 8 +++-----
 5 files changed, 12 insertions(+), 22 deletions(-)
drivers/gpu/drm/gma500/gma_display.c +3 -4
index 3df6d6e850f5..f81114594211 100644
@@ -9,6 +9,7 @@
 
 #include <linux/delay.h>
 #include <linux/highmem.h>
+#include <linux/pagemap.h>
 
 #include <drm/drm_crtc.h>
 #include <drm/drm_fourcc.h>
@@ -334,7 +335,7 @@ int gma_crtc_cursor_set(struct drm_crtc *crtc,
 	struct gtt_range *gt;
 	struct gtt_range *cursor_gt = gma_crtc->cursor_gt;
 	struct drm_gem_object *obj;
-	void *tmp_dst, *tmp_src;
+	void *tmp_dst;
 	int ret = 0, i, cursor_pages;
 
 	/* If we didn't get a handle then turn the cursor off */
@@ -400,9 +401,7 @@ int gma_crtc_cursor_set(struct drm_crtc *crtc,
 		/* Copy the cursor to cursor mem */
 		tmp_dst = dev_priv->vram_addr + cursor_gt->offset;
 		for (i = 0; i < cursor_pages; i++) {
-			tmp_src = kmap(gt->pages[i]);
-			memcpy(tmp_dst, tmp_src, PAGE_SIZE);
-			kunmap(gt->pages[i]);
+			memcpy_from_page(tmp_dst, gt->pages[i], 0, PAGE_SIZE);
 			tmp_dst += PAGE_SIZE;
 		}
 
drivers/gpu/drm/gma500/mmu.c +2 -2
index 505044c9a673..8a0856c7f439 100644
@@ -5,6 +5,7 @@
  **************************************************************************/
 
 #include <linux/highmem.h>
+#include <linux/pagemap.h>
 
 #include "mmu.h"
 #include "psb_drv.h"
@@ -204,8 +205,7 @@ struct psb_mmu_pd *psb_mmu_alloc_pd(struct psb_mmu_driver *driver,
 
 	kunmap(pd->p);
 
-	clear_page(kmap(pd->dummy_page));
-	kunmap(pd->dummy_page);
+	memzero_page(pd->dummy_page, 0, PAGE_SIZE);
 
 	pd->tables = vmalloc_user(sizeof(struct psb_mmu_pt *) * 1024);
 	if (!pd->tables)
drivers/gpu/drm/i915/gem/i915_gem_shmem.c +2 -4
index 75e8b71c18b9..8a25e08edd18 100644
@@ -558,7 +558,7 @@ i915_gem_object_create_shmem_from_data(struct drm_i915_private *dev_priv,
 	do {
 		unsigned int len = min_t(typeof(size), size, PAGE_SIZE);
 		struct page *page;
-		void *pgdata, *vaddr;
+		void *pgdata;
 
 		err = pagecache_write_begin(file, file->f_mapping,
 					    offset, len, 0,
@@ -566,9 +566,7 @@ i915_gem_object_create_shmem_from_data(struct drm_i915_private *dev_priv,
 		if (err < 0)
 			goto fail;
 
-		vaddr = kmap(page);
-		memcpy(vaddr, data, len);
-		kunmap(page);
+		memcpy_to_page(page, 0, data, len);
 
 		err = pagecache_write_end(file, file->f_mapping,
 					  offset, len, len,
drivers/gpu/drm/i915/gt/intel_gtt.c +2 -7
index 3f1114b58b01..f3d7c601d362 100644
@@ -153,13 +153,8 @@ static void poison_scratch_page(struct drm_i915_gem_object *scratch)
 	if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
 		val = POISON_FREE;
 
-	for_each_sgt_page(page, sgt, scratch->mm.pages) {
-		void *vaddr;
-
-		vaddr = kmap(page);
-		memset(vaddr, val, PAGE_SIZE);
-		kunmap(page);
-	}
+	for_each_sgt_page(page, sgt, scratch->mm.pages)
+		memset_page(page, val, 0, PAGE_SIZE);
 }
 
 int setup_scratch_page(struct i915_address_space *vm)
drivers/gpu/drm/i915/gt/shmem_utils.c +3 -5
index f011ea42487e..2d5f1f2e803d 100644
@@ -95,19 +95,17 @@ static int __shmem_rw(struct file *file, loff_t off,
 		unsigned int this =
 			min_t(size_t, PAGE_SIZE - offset_in_page(off), len);
 		struct page *page;
-		void *vaddr;
 
 		page = shmem_read_mapping_page_gfp(file->f_mapping, pfn,
 						   GFP_KERNEL);
 		if (IS_ERR(page))
 			return PTR_ERR(page);
 
-		vaddr = kmap(page);
 		if (write)
-			memcpy(vaddr + offset_in_page(off), ptr, this);
+			memcpy_to_page(page, offset_in_page(off), ptr, this);
 		else
-			memcpy(ptr, vaddr + offset_in_page(off), this);
-		kunmap(page);
+			memcpy_from_page(ptr, page, offset_in_page(off), this);
+
 		put_page(page);
 
 		len -= this;
Signature · 1 line
2.28.0.rc0.12.gb6a658bd00c9
maintainer Reviewed-by on v1 3/17
#11 lore raw

Re: [Intel-gfx] [PATCH 03/17] drivers/gpu: Convert to mem*_page()

+ intel-gfx mailing list

Quoting [email protected] (2020-11-24 08:07:41)

Quoted 10 lines

From: Ira Weiny <[email protected]>

The pattern of kmap/mem*/kunmap is repeated. Use the new mem*_page() calls instead.

Cc Patrik Jakobsson <[email protected]>
Cc Jani Nikula <[email protected]>
Cc Joonas Lahtinen <[email protected]>
Cc Rodrigo Vivi <[email protected]>
Quoted 6 lines
---
 drivers/gpu/drm/gma500/gma_display.c      | 7 +++----
 drivers/gpu/drm/gma500/mmu.c              | 4 ++--
 drivers/gpu/drm/i915/gem/i915_gem_shmem.c | 6 ++----
 drivers/gpu/drm/i915/gt/intel_gtt.c       | 9 ++-------
 drivers/gpu/drm/i915/gt/shmem_utils.c     | 8 +++-----

Are you looking to merge all these from the same tree, or first merge the first patch and then trickle the rest through their own trees? Our last -next PR was already sent for i915, so I would queue this only for 5.12.

In any case, if you could split the i915 changes to a separate patch (we have multiple sub-trees in drm), those are:

The gma500 changes also appear correct, so feel free to apply the R-b for those, too.

Regards, Joonas

Quoted 130 lines
 5 files changed, 12 insertions(+), 22 deletions(-)
drivers/gpu/drm/gma500/gma_display.c +3 -4
index 3df6d6e850f5..f81114594211 100644
@@ -9,6 +9,7 @@
 
 #include <linux/delay.h>
 #include <linux/highmem.h>
+#include <linux/pagemap.h>
 
 #include <drm/drm_crtc.h>
 #include <drm/drm_fourcc.h>
@@ -334,7 +335,7 @@ int gma_crtc_cursor_set(struct drm_crtc *crtc,
        struct gtt_range *gt;
        struct gtt_range *cursor_gt = gma_crtc->cursor_gt;
        struct drm_gem_object *obj;
-       void *tmp_dst, *tmp_src;
+       void *tmp_dst;
        int ret = 0, i, cursor_pages;
 
        /* If we didn't get a handle then turn the cursor off */
@@ -400,9 +401,7 @@ int gma_crtc_cursor_set(struct drm_crtc *crtc,
                /* Copy the cursor to cursor mem */
                tmp_dst = dev_priv->vram_addr + cursor_gt->offset;
                for (i = 0; i < cursor_pages; i++) {
-                       tmp_src = kmap(gt->pages[i]);
-                       memcpy(tmp_dst, tmp_src, PAGE_SIZE);
-                       kunmap(gt->pages[i]);
+                       memcpy_from_page(tmp_dst, gt->pages[i], 0, PAGE_SIZE);
                        tmp_dst += PAGE_SIZE;
                }
 
drivers/gpu/drm/gma500/mmu.c +2 -2
index 505044c9a673..8a0856c7f439 100644
@@ -5,6 +5,7 @@
  **************************************************************************/
 
 #include <linux/highmem.h>
+#include <linux/pagemap.h>
 
 #include "mmu.h"
 #include "psb_drv.h"
@@ -204,8 +205,7 @@ struct psb_mmu_pd *psb_mmu_alloc_pd(struct psb_mmu_driver *driver,
 
        kunmap(pd->p);
 
-       clear_page(kmap(pd->dummy_page));
-       kunmap(pd->dummy_page);
+       memzero_page(pd->dummy_page, 0, PAGE_SIZE);
 
        pd->tables = vmalloc_user(sizeof(struct psb_mmu_pt *) * 1024);
        if (!pd->tables)
drivers/gpu/drm/i915/gem/i915_gem_shmem.c +2 -4
index 75e8b71c18b9..8a25e08edd18 100644
@@ -558,7 +558,7 @@ i915_gem_object_create_shmem_from_data(struct drm_i915_private *dev_priv,
        do {
                unsigned int len = min_t(typeof(size), size, PAGE_SIZE);
                struct page *page;
-               void *pgdata, *vaddr;
+               void *pgdata;
 
                err = pagecache_write_begin(file, file->f_mapping,
                                            offset, len, 0,
@@ -566,9 +566,7 @@ i915_gem_object_create_shmem_from_data(struct drm_i915_private *dev_priv,
                if (err < 0)
                        goto fail;
 
-               vaddr = kmap(page);
-               memcpy(vaddr, data, len);
-               kunmap(page);
+               memcpy_to_page(page, 0, data, len);
 
                err = pagecache_write_end(file, file->f_mapping,
                                          offset, len, len,
drivers/gpu/drm/i915/gt/intel_gtt.c +2 -7
index 3f1114b58b01..f3d7c601d362 100644
@@ -153,13 +153,8 @@ static void poison_scratch_page(struct drm_i915_gem_object *scratch)
        if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
                val = POISON_FREE;
 
-       for_each_sgt_page(page, sgt, scratch->mm.pages) {
-               void *vaddr;
-
-               vaddr = kmap(page);
-               memset(vaddr, val, PAGE_SIZE);
-               kunmap(page);
-       }
+       for_each_sgt_page(page, sgt, scratch->mm.pages)
+               memset_page(page, val, 0, PAGE_SIZE);
 }
 
 int setup_scratch_page(struct i915_address_space *vm)
drivers/gpu/drm/i915/gt/shmem_utils.c +3 -5
index f011ea42487e..2d5f1f2e803d 100644
@@ -95,19 +95,17 @@ static int __shmem_rw(struct file *file, loff_t off,
                unsigned int this =
                        min_t(size_t, PAGE_SIZE - offset_in_page(off), len);
                struct page *page;
-               void *vaddr;
 
                page = shmem_read_mapping_page_gfp(file->f_mapping, pfn,
                                                   GFP_KERNEL);
                if (IS_ERR(page))
                        return PTR_ERR(page);
 
-               vaddr = kmap(page);
                if (write)
-                       memcpy(vaddr + offset_in_page(off), ptr, this);
+                       memcpy_to_page(page, offset_in_page(off), ptr, this);
                else
-                       memcpy(ptr, vaddr + offset_in_page(off), this);
-               kunmap(page);
+                       memcpy_from_page(ptr, page, offset_in_page(off), this);
+
                put_page(page);
 
                len -= this;
Signature · 1 line
2.28.0.rc0.12.gb6a658bd00c9

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

author
#12 lore raw

Re: [PATCH 03/17] drivers/gpu: Convert to mem*_page()

Quoted 20 lines · Joonas Lahtinen

+ intel-gfx mailing list

Quoting [email protected] (2020-11-24 08:07:41)

Quoted 10 lines

From: Ira Weiny <[email protected]>

The pattern of kmap/mem*/kunmap is repeated. Use the new mem*_page() calls instead.

Cc Patrik Jakobsson <[email protected]>
Cc Jani Nikula <[email protected]>
Cc Joonas Lahtinen <[email protected]>
Cc Rodrigo Vivi <[email protected]>
Quoted 6 lines
---
 drivers/gpu/drm/gma500/gma_display.c      | 7 +++----
 drivers/gpu/drm/gma500/mmu.c              | 4 ++--
 drivers/gpu/drm/i915/gem/i915_gem_shmem.c | 6 ++----
 drivers/gpu/drm/i915/gt/intel_gtt.c       | 9 ++-------
 drivers/gpu/drm/i915/gt/shmem_utils.c     | 8 +++-----
Quoted 2 lines

Are you looking to merge all these from the same tree, or first merge the first patch and then trickle the rest through their own trees?

I was thinking that they would go through Andrew's tree in bulk. But as I go through all the 'variants' including adding any kmap_atomic() variants it is getting to be a pretty big change. I'm trying to use Coccinelle but I'm not 100% confident in it working, more precisely in my skill to make it work.

So I think I'm going to submit the base patch to Andrew today (with some cleanups per the comments in this thread).

If Andrew could land that then I will can submit separate patches to each subsystem which would get full testing... :-(

That is best.

Thanks for making me think on this,
Ira

Quoted 139 lines

Our last -next PR was already sent for i915, so I would queue this only for 5.12.

In any case, if you could split the i915 changes to a separate patch (we have multiple sub-trees in drm), those are:

The gma500 changes also appear correct, so feel free to apply the R-b for those, too.

Regards, Joonas

Quoted 126 lines
 5 files changed, 12 insertions(+), 22 deletions(-)
drivers/gpu/drm/gma500/gma_display.c +3 -4
index 3df6d6e850f5..f81114594211 100644
@@ -9,6 +9,7 @@
 
 #include <linux/delay.h>
 #include <linux/highmem.h>
+#include <linux/pagemap.h>
 
 #include <drm/drm_crtc.h>
 #include <drm/drm_fourcc.h>
@@ -334,7 +335,7 @@ int gma_crtc_cursor_set(struct drm_crtc *crtc,
        struct gtt_range *gt;
        struct gtt_range *cursor_gt = gma_crtc->cursor_gt;
        struct drm_gem_object *obj;
-       void *tmp_dst, *tmp_src;
+       void *tmp_dst;
        int ret = 0, i, cursor_pages;
 
        /* If we didn't get a handle then turn the cursor off */
@@ -400,9 +401,7 @@ int gma_crtc_cursor_set(struct drm_crtc *crtc,
                /* Copy the cursor to cursor mem */
                tmp_dst = dev_priv->vram_addr + cursor_gt->offset;
                for (i = 0; i < cursor_pages; i++) {
-                       tmp_src = kmap(gt->pages[i]);
-                       memcpy(tmp_dst, tmp_src, PAGE_SIZE);
-                       kunmap(gt->pages[i]);
+                       memcpy_from_page(tmp_dst, gt->pages[i], 0, PAGE_SIZE);
                        tmp_dst += PAGE_SIZE;
                }
 
drivers/gpu/drm/gma500/mmu.c +2 -2
index 505044c9a673..8a0856c7f439 100644
@@ -5,6 +5,7 @@
  **************************************************************************/
 
 #include <linux/highmem.h>
+#include <linux/pagemap.h>
 
 #include "mmu.h"
 #include "psb_drv.h"
@@ -204,8 +205,7 @@ struct psb_mmu_pd *psb_mmu_alloc_pd(struct psb_mmu_driver *driver,
 
        kunmap(pd->p);
 
-       clear_page(kmap(pd->dummy_page));
-       kunmap(pd->dummy_page);
+       memzero_page(pd->dummy_page, 0, PAGE_SIZE);
 
        pd->tables = vmalloc_user(sizeof(struct psb_mmu_pt *) * 1024);
        if (!pd->tables)
drivers/gpu/drm/i915/gem/i915_gem_shmem.c +2 -4
index 75e8b71c18b9..8a25e08edd18 100644
@@ -558,7 +558,7 @@ i915_gem_object_create_shmem_from_data(struct drm_i915_private *dev_priv,
        do {
                unsigned int len = min_t(typeof(size), size, PAGE_SIZE);
                struct page *page;
-               void *pgdata, *vaddr;
+               void *pgdata;
 
                err = pagecache_write_begin(file, file->f_mapping,
                                            offset, len, 0,
@@ -566,9 +566,7 @@ i915_gem_object_create_shmem_from_data(struct drm_i915_private *dev_priv,
                if (err < 0)
                        goto fail;
 
-               vaddr = kmap(page);
-               memcpy(vaddr, data, len);
-               kunmap(page);
+               memcpy_to_page(page, 0, data, len);
 
                err = pagecache_write_end(file, file->f_mapping,
                                          offset, len, len,
drivers/gpu/drm/i915/gt/intel_gtt.c +2 -7
index 3f1114b58b01..f3d7c601d362 100644
@@ -153,13 +153,8 @@ static void poison_scratch_page(struct drm_i915_gem_object *scratch)
        if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
                val = POISON_FREE;
 
-       for_each_sgt_page(page, sgt, scratch->mm.pages) {
-               void *vaddr;
-
-               vaddr = kmap(page);
-               memset(vaddr, val, PAGE_SIZE);
-               kunmap(page);
-       }
+       for_each_sgt_page(page, sgt, scratch->mm.pages)
+               memset_page(page, val, 0, PAGE_SIZE);
 }
 
 int setup_scratch_page(struct i915_address_space *vm)
drivers/gpu/drm/i915/gt/shmem_utils.c +3 -5
index f011ea42487e..2d5f1f2e803d 100644
@@ -95,19 +95,17 @@ static int __shmem_rw(struct file *file, loff_t off,
                unsigned int this =
                        min_t(size_t, PAGE_SIZE - offset_in_page(off), len);
                struct page *page;
-               void *vaddr;
 
                page = shmem_read_mapping_page_gfp(file->f_mapping, pfn,
                                                   GFP_KERNEL);
                if (IS_ERR(page))
                        return PTR_ERR(page);
 
-               vaddr = kmap(page);
                if (write)
-                       memcpy(vaddr + offset_in_page(off), ptr, this);
+                       memcpy_to_page(page, offset_in_page(off), ptr, this);
                else
-                       memcpy(ptr, vaddr + offset_in_page(off), this);
-               kunmap(page);
+                       memcpy_from_page(ptr, page, offset_in_page(off), this);
+
                put_page(page);
 
Quoted 4 lines
Quoted 4 lines
drivers/gpu/drm/i915/gt/shmem_utils.c +0 -0
                len -= this;
Signature · 1 line
2.28.0.rc0.12.gb6a658bd00c9

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

#13 lore raw

Re: [PATCH 03/17] drivers/gpu: Convert to mem*_page()

Quoted 2 lines · Ira Weiny

So I think I'm going to submit the base patch to Andrew today (with some cleanups per the comments in this thread).

Could you please base that on tip core/mm where the kmap_local() muck is and use kmap_local() right away?

Thanks,

tglx
_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

author
#14 lore raw

Re: [PATCH 03/17] drivers/gpu: Convert to mem*_page()

Quoted 6 lines · Thomas Gleixner
Quoted 2 lines · Ira Weiny

So I think I'm going to submit the base patch to Andrew today (with some cleanups per the comments in this thread).

Could you please base that on tip core/mm where the kmap_local() muck is and use kmap_local() right away?

Sure. Would that mean it should go through you and not Andrew?

Ira

Quoted 4 lines

Thanks,

tglx

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

#15 lore raw

Re: [PATCH 03/17] drivers/gpu: Convert to mem*_page()

Quoted 9 lines · Ira Weiny
Quoted 6 lines · Thomas Gleixner
Quoted 2 lines · Ira Weiny

So I think I'm going to submit the base patch to Andrew today (with some cleanups per the comments in this thread).

Could you please base that on tip core/mm where the kmap_local() muck is and use kmap_local() right away?

Sure. Would that mean it should go through you and not Andrew?

If Andrew has no objections of course.

Thanks,

tglx
_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

#16 lore raw

PATCH 4/17 fs/afs: Convert to memzero_page()

In reply to [email protected]

From: Ira Weiny <[email protected]>

Convert the kmap()/memcpy()/kunmap() pattern to memzero_page().

Cc David Howells <[email protected]>
---
 fs/afs/write.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
fs/afs/write.c +1 -4
index 50371207f327..ed7419de0178 100644
@@ -30,7 +30,6 @@ static int afs_fill_page(struct afs_vnode *vnode, struct key *key,
 {
 	struct afs_read *req;
 	size_t p;
-	void *data;
 	int ret;
 
 	_enter(",,%llu", (unsigned long long)pos);
@@ -38,9 +37,7 @@ static int afs_fill_page(struct afs_vnode *vnode, struct key *key,
 	if (pos >= vnode->vfs_inode.i_size) {
 		p = pos & ~PAGE_MASK;
 		ASSERTCMP(p + len, <=, PAGE_SIZE);
-		data = kmap(page);
-		memset(data + p, 0, len);
-		kunmap(page);
+		memzero_page(page, p, len);
 		return 0;
 	}
 
Signature · 1 line
2.28.0.rc0.12.gb6a658bd00c9
#18 lore raw

PATCH 5/17 fs/btrfs: Convert to memzero_page()

In reply to [email protected]

From: Ira Weiny <[email protected]>

Remove the kmap/memset()/kunmap pattern and use the new memzero_page() call where possible.

Cc Chris Mason <[email protected]>
Cc Josef Bacik <[email protected]>
Cc David Sterba <[email protected]>
---
 fs/btrfs/inode.c | 21 +++++----------------
 1 file changed, 5 insertions(+), 16 deletions(-)
fs/btrfs/inode.c +5 -16
index da58c58ef9aa..b0bcf9493236 100644
@@ -590,17 +590,12 @@ static noinline int compress_file_range(struct async_chunk *async_chunk)
 		if (!ret) {
 			unsigned long offset = offset_in_page(total_compressed);
 			struct page *page = pages[nr_pages - 1];
-			char *kaddr;
 
 			/* zero the tail end of the last page, we might be
 			 * sending it down to disk
 			 */
-			if (offset) {
-				kaddr = kmap_atomic(page);
-				memset(kaddr + offset, 0,
-				       PAGE_SIZE - offset);
-				kunmap_atomic(kaddr);
-			}
+			if (offset)
+				memzero_page(page, offset, PAGE_SIZE - offset);
 			will_compress = 1;
 		}
 	}
@@ -6485,11 +6480,8 @@ static noinline int uncompress_inline(struct btrfs_path *path,
 	 * cover that region here.
 	 */
 
-	if (max_size + pg_offset < PAGE_SIZE) {
-		char *map = kmap(page);
-		memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
-		kunmap(page);
-	}
+	if (max_size + pg_offset < PAGE_SIZE)
+		memzero_page(page, pg_offset + max_size, PAGE_SIZE - max_size - pg_offset);
 	kfree(tmp);
 	return ret;
 }
@@ -8245,7 +8237,6 @@ vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
 	struct btrfs_ordered_extent *ordered;
 	struct extent_state *cached_state = NULL;
 	struct extent_changeset *data_reserved = NULL;
-	char *kaddr;
 	unsigned long zero_start;
 	loff_t size;
 	vm_fault_t ret;
@@ -8352,10 +8343,8 @@ vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
 		zero_start = PAGE_SIZE;
 
 	if (zero_start != PAGE_SIZE) {
-		kaddr = kmap(page);
-		memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
+		memzero_page(page, zero_start, PAGE_SIZE - zero_start);
 		flush_dcache_page(page);
-		kunmap(page);
 	}
 	ClearPageChecked(page);
 	set_page_dirty(page);
Signature · 1 line
2.28.0.rc0.12.gb6a658bd00c9
#19 lore raw

Re: [PATCH 05/17] fs/btrfs: Convert to memzero_page()

Quoted 11 lines · On Mon, Nov 23, 2020 at 10:07:43PM -0800, [email protected] wrote:

From: Ira Weiny <[email protected]>

Remove the kmap/memset()/kunmap pattern and use the new memzero_page() call where possible.

Cc Chris Mason <[email protected]>
Cc Josef Bacik <[email protected]>
Cc David Sterba <[email protected]>
---
 fs/btrfs/inode.c | 21 +++++----------------

The patch converts the pattern only in inode.c, but there's more in compression.c, extent_io.c, zlib.c,d zstd.c (kmap_atomic) and reflink.c, send.c (kmap).

author
#20 lore raw

Re: [PATCH 05/17] fs/btrfs: Convert to memzero_page()

Quoted 13 lines · David Sterba
Quoted 11 lines · On Mon, Nov 23, 2020 at 10:07:43PM -0800, [email protected] wrote:

From: Ira Weiny <[email protected]>

Remove the kmap/memset()/kunmap pattern and use the new memzero_page() call where possible.

Cc Chris Mason <[email protected]>
Cc Josef Bacik <[email protected]>
Cc David Sterba <[email protected]>
---
 fs/btrfs/inode.c | 21 +++++----------------
Quoted 3 lines

The patch converts the pattern only in inode.c, but there's more in compression.c, extent_io.c, zlib.c,d zstd.c (kmap_atomic) and reflink.c, send.c (kmap).

Thanks... not sure how I missed reflink.c and send.c.

I'll add them in v2.

Thanks!
Ira

#21 lore raw

PATCH 6/17 fs/hfs: Convert to mem*_page() interface

In reply to [email protected]

From: Ira Weiny <[email protected]>

Where possible remove kmap/mem*/kunmap in favor of the new mem*_page() calls.

---
 fs/hfs/bnode.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
fs/hfs/bnode.c +4 -9
index b63a4df7327b..56037ae5ba69 100644
@@ -23,8 +23,7 @@ void hfs_bnode_read(struct hfs_bnode *node, void *buf,
 	off += node->page_offset;
 	page = node->page[0];
 
-	memcpy(buf, kmap(page) + off, len);
-	kunmap(page);
+	memcpy_from_page(buf, page, off, len);
 }
 
 u16 hfs_bnode_read_u16(struct hfs_bnode *node, int off)
@@ -65,8 +64,7 @@ void hfs_bnode_write(struct hfs_bnode *node, void *buf, int off, int len)
 	off += node->page_offset;
 	page = node->page[0];
 
-	memcpy(kmap(page) + off, buf, len);
-	kunmap(page);
+	memcpy_to_page(page, off, buf, len);
 	set_page_dirty(page);
 }
 
@@ -90,8 +88,7 @@ void hfs_bnode_clear(struct hfs_bnode *node, int off, int len)
 	off += node->page_offset;
 	page = node->page[0];
 
-	memset(kmap(page) + off, 0, len);
-	kunmap(page);
+	memzero_page(page, off, len);
 	set_page_dirty(page);
 }
 
@@ -108,9 +105,7 @@ void hfs_bnode_copy(struct hfs_bnode *dst_node, int dst,
 	src_page = src_node->page[0];
 	dst_page = dst_node->page[0];
 
-	memcpy(kmap(dst_page) + dst, kmap(src_page) + src, len);
-	kunmap(src_page);
-	kunmap(dst_page);
+	memcpy_page(dst_page, dst, src_page, src, len);
 	set_page_dirty(dst_page);
 }
 
Signature · 1 line
2.28.0.rc0.12.gb6a658bd00c9
#22 lore raw

PATCH 7/17 fs/cifs: Convert to memcpy_page()

In reply to [email protected]

From: Ira Weiny <[email protected]>

Use memcpy_page() instead of open coding kmap/memcpy/kunmap.

Cc Steve French <[email protected]>
---
 fs/cifs/smb2ops.c | 10 +++-------
 1 file changed, 3 insertions(+), 7 deletions(-)
fs/cifs/smb2ops.c +3 -7
index 504766cb6c19..d1088ee9a0e6 100644
@@ -4223,17 +4223,13 @@ smb3_init_transform_rq(struct TCP_Server_Info *server, int num_rqst,
 
 		/* copy pages form the old */
 		for (j = 0; j < npages; j++) {
-			char *dst, *src;
 			unsigned int offset, len;
 
 			rqst_page_get_length(&new_rq[i], j, &len, &offset);
 
-			dst = (char *) kmap(new_rq[i].rq_pages[j]) + offset;
-			src = (char *) kmap(old_rq[i - 1].rq_pages[j]) + offset;
-
-			memcpy(dst, src, len);
-			kunmap(new_rq[i].rq_pages[j]);
-			kunmap(old_rq[i - 1].rq_pages[j]);
+			memcpy_page(new_rq[i].rq_pages[j], offset,
+				    old_rq[i - 1].rq_pages[j], offset,
+				    len);
 		}
 	}
 
Signature · 1 line
2.28.0.rc0.12.gb6a658bd00c9
#23 lore raw

PATCH 8/17 fs/hfsplus: Convert to mem*_page()

In reply to [email protected]

From: Ira Weiny <[email protected]>

Remove the pattern of kmap/mem*/kunmap in favor of the new mem*_page() functions which handle the kmap'ing correctly for us.

---
 fs/hfsplus/bnode.c | 53 +++++++++++++---------------------------------
 1 file changed, 15 insertions(+), 38 deletions(-)
fs/hfsplus/bnode.c +15 -38
index 177fae4e6581..c4347b1cb36f 100644
@@ -29,14 +29,12 @@ void hfs_bnode_read(struct hfs_bnode *node, void *buf, int off, int len)
 	off &= ~PAGE_MASK;
 
 	l = min_t(int, len, PAGE_SIZE - off);
-	memcpy(buf, kmap(*pagep) + off, l);
-	kunmap(*pagep);
+	memcpy_from_page(buf, *pagep, off, l);
 
 	while ((len -= l) != 0) {
 		buf += l;
 		l = min_t(int, len, PAGE_SIZE);
-		memcpy(buf, kmap(*++pagep), l);
-		kunmap(*pagep);
+		memcpy_from_page(buf, *++pagep, 0, l);
 	}
 }
 
@@ -82,16 +80,14 @@ void hfs_bnode_write(struct hfs_bnode *node, void *buf, int off, int len)
 	off &= ~PAGE_MASK;
 
 	l = min_t(int, len, PAGE_SIZE - off);
-	memcpy(kmap(*pagep) + off, buf, l);
+	memcpy_to_page(*pagep, off, buf, l);
 	set_page_dirty(*pagep);
-	kunmap(*pagep);
 
 	while ((len -= l) != 0) {
 		buf += l;
 		l = min_t(int, len, PAGE_SIZE);
-		memcpy(kmap(*++pagep), buf, l);
+		memcpy_to_page(*++pagep, 0, buf, l);
 		set_page_dirty(*pagep);
-		kunmap(*pagep);
 	}
 }
 
@@ -112,15 +108,13 @@ void hfs_bnode_clear(struct hfs_bnode *node, int off, int len)
 	off &= ~PAGE_MASK;
 
 	l = min_t(int, len, PAGE_SIZE - off);
-	memset(kmap(*pagep) + off, 0, l);
+	memzero_page(*pagep, off, l);
 	set_page_dirty(*pagep);
-	kunmap(*pagep);
 
 	while ((len -= l) != 0) {
 		l = min_t(int, len, PAGE_SIZE);
-		memset(kmap(*++pagep), 0, l);
+		memzero_page(*++pagep, 0, l);
 		set_page_dirty(*pagep);
-		kunmap(*pagep);
 	}
 }
 
@@ -142,17 +136,13 @@ void hfs_bnode_copy(struct hfs_bnode *dst_node, int dst,
 
 	if (src == dst) {
 		l = min_t(int, len, PAGE_SIZE - src);
-		memcpy(kmap(*dst_page) + src, kmap(*src_page) + src, l);
-		kunmap(*src_page);
+		memcpy_page(*dst_page, src, *src_page, src, l);
 		set_page_dirty(*dst_page);
-		kunmap(*dst_page);
 
 		while ((len -= l) != 0) {
 			l = min_t(int, len, PAGE_SIZE);
-			memcpy(kmap(*++dst_page), kmap(*++src_page), l);
-			kunmap(*src_page);
+			memcpy_page(*++dst_page, 0, *++src_page, 0, l);
 			set_page_dirty(*dst_page);
-			kunmap(*dst_page);
 		}
 	} else {
 		void *src_ptr, *dst_ptr;
@@ -202,21 +192,16 @@ void hfs_bnode_move(struct hfs_bnode *node, int dst, int src, int len)
 
 		if (src == dst) {
 			while (src < len) {
-				memmove(kmap(*dst_page), kmap(*src_page), src);
-				kunmap(*src_page);
+				memmove_page(*dst_page, 0, *src_page, 0, src);
 				set_page_dirty(*dst_page);
-				kunmap(*dst_page);
 				len -= src;
 				src = PAGE_SIZE;
 				src_page--;
 				dst_page--;
 			}
 			src -= len;
-			memmove(kmap(*dst_page) + src,
-				kmap(*src_page) + src, len);
-			kunmap(*src_page);
+			memmove_page(*dst_page, src, *src_page, src, len);
 			set_page_dirty(*dst_page);
-			kunmap(*dst_page);
 		} else {
 			void *src_ptr, *dst_ptr;
 
@@ -251,19 +236,13 @@ void hfs_bnode_move(struct hfs_bnode *node, int dst, int src, int len)
 
 		if (src == dst) {
 			l = min_t(int, len, PAGE_SIZE - src);
-			memmove(kmap(*dst_page) + src,
-				kmap(*src_page) + src, l);
-			kunmap(*src_page);
+			memmove_page(*dst_page, src, *src_page, src, l);
 			set_page_dirty(*dst_page);
-			kunmap(*dst_page);
 
 			while ((len -= l) != 0) {
 				l = min_t(int, len, PAGE_SIZE);
-				memmove(kmap(*++dst_page),
-					kmap(*++src_page), l);
-				kunmap(*src_page);
+				memmove_page(*++dst_page, 0, *++src_page, 0, l);
 				set_page_dirty(*dst_page);
-				kunmap(*dst_page);
 			}
 		} else {
 			void *src_ptr, *dst_ptr;
@@ -593,14 +572,12 @@ struct hfs_bnode *hfs_bnode_create(struct hfs_btree *tree, u32 num)
 	}
 
 	pagep = node->page;
-	memset(kmap(*pagep) + node->page_offset, 0,
-	       min_t(int, PAGE_SIZE, tree->node_size));
+	memzero_page(*pagep, node->page_offset,
+		     min_t(int, PAGE_SIZE, tree->node_size));
 	set_page_dirty(*pagep);
-	kunmap(*pagep);
 	for (i = 1; i < tree->pages_per_bnode; i++) {
-		memset(kmap(*++pagep), 0, PAGE_SIZE);
+		memzero_page(*++pagep, 0, PAGE_SIZE);
 		set_page_dirty(*pagep);
-		kunmap(*pagep);
 	}
 	clear_bit(HFS_BNODE_NEW, &node->flags);
 	wake_up(&node->lock_wq);
Signature · 1 line
2.28.0.rc0.12.gb6a658bd00c9
#24 lore raw

PATCH 9/17 fs/f2fs: Remove f2fs_copy_page()

In reply to [email protected]

From: Ira Weiny <[email protected]>

The new common function memcpy_page() provides this exactly functionality. Remove the local f2fs_copy_page() and call memcpy_page() instead.

Cc Jaegeuk Kim <[email protected]>
Cc Chao Yu <[email protected]>
---
 fs/f2fs/f2fs.h | 10 ----------
 fs/f2fs/file.c |  3 ++-
 2 files changed, 2 insertions(+), 11 deletions(-)
fs/f2fs/f2fs.h +0 -10
index cb700d797296..546dba7d7cc2 100644
@@ -2428,16 +2428,6 @@ static inline struct page *f2fs_pagecache_get_page(
 	return pagecache_get_page(mapping, index, fgp_flags, gfp_mask);
 }
 
-static inline void f2fs_copy_page(struct page *src, struct page *dst)
-{
-	char *src_kaddr = kmap(src);
-	char *dst_kaddr = kmap(dst);
-
-	memcpy(dst_kaddr, src_kaddr, PAGE_SIZE);
-	kunmap(dst);
-	kunmap(src);
-}
-
 static inline void f2fs_put_page(struct page *page, int unlock)
 {
 	if (!page)
fs/f2fs/file.c +2 -1
index ee861c6d9ff0..c38aa186a7c6 100644
@@ -17,6 +17,7 @@
 #include <linux/uaccess.h>
 #include <linux/mount.h>
 #include <linux/pagevec.h>
+#include <linux/pagemap.h>
 #include <linux/uio.h>
 #include <linux/uuid.h>
 #include <linux/file.h>
@@ -1234,7 +1235,7 @@ static int __clone_blkaddrs(struct inode *src_inode, struct inode *dst_inode,
 				f2fs_put_page(psrc, 1);
 				return PTR_ERR(pdst);
 			}
-			f2fs_copy_page(psrc, pdst);
+			memcpy_page(psrc, 0, pdst, 0, PAGE_SIZE);
 			set_page_dirty(pdst);
 			f2fs_put_page(pdst, 1);
 			f2fs_put_page(psrc, 1);
Signature · 1 line
2.28.0.rc0.12.gb6a658bd00c9
Acked-by on v1 9/17
#25 lore raw

Re: [PATCH 09/17] fs/f2fs: Remove f2fs_copy_page()

Quoted 9 lines · On 2020/11/24 14:07, [email protected] wrote:

From: Ira Weiny <[email protected]>

The new common function memcpy_page() provides this exactly functionality. Remove the local f2fs_copy_page() and call memcpy_page() instead.

Cc Jaegeuk Kim <[email protected]>
Cc Chao Yu <[email protected]>

Thanks,

#26 lore raw

PATCH 10/17 fs/freevxfs: Use memcpy_to_page()

In reply to [email protected]

From: Ira Weiny <[email protected]>

Remove kmap/memcpy/kunmap pattern in favor of the new memcpy_to_page()

Cc Christoph Hellwig <[email protected]>
---
 fs/freevxfs/vxfs_immed.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
fs/freevxfs/vxfs_immed.c +1 -5
index bfc780c682fb..d185fa67b82f 100644
@@ -67,12 +67,8 @@ vxfs_immed_readpage(struct file *fp, struct page *pp)
 {
 	struct vxfs_inode_info	*vip = VXFS_INO(pp->mapping->host);
 	u_int64_t	offset = (u_int64_t)pp->index << PAGE_SHIFT;
-	caddr_t		kaddr;
 
-	kaddr = kmap(pp);
-	memcpy(kaddr, vip->vii_immed.vi_immed + offset, PAGE_SIZE);
-	kunmap(pp);
-	
+	memcpy_to_page(pp, 0, vip->vii_immed.vi_immed + offset, PAGE_SIZE);
 	flush_dcache_page(pp);
 	SetPageUptodate(pp);
         unlock_page(pp);
Signature · 1 line
2.28.0.rc0.12.gb6a658bd00c9
#27 lore raw

PATCH 11/17 fs/reiserfs: Use memcpy_from_page()

In reply to [email protected]

From: Ira Weiny <[email protected]>

Remove the open coding of kmap/memcpy/kunmap and use the new memcpy_from_page() function.

---
 fs/reiserfs/journal.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
fs/reiserfs/journal.c +3 -6
index e98f99338f8f..e288bbbe80ff 100644
@@ -4184,7 +4184,6 @@ static int do_journal_end(struct reiserfs_transaction_handle *th, int flags)
 		/* copy all the real blocks into log area.  dirty log blocks */
 		if (buffer_journaled(cn->bh)) {
 			struct buffer_head *tmp_bh;
-			char *addr;
 			struct page *page;
 			tmp_bh =
 			    journal_getblk(sb,
@@ -4194,11 +4193,9 @@ static int do_journal_end(struct reiserfs_transaction_handle *th, int flags)
 					    SB_ONDISK_JOURNAL_SIZE(sb)));
 			set_buffer_uptodate(tmp_bh);
 			page = cn->bh->b_page;
-			addr = kmap(page);
-			memcpy(tmp_bh->b_data,
-			       addr + offset_in_page(cn->bh->b_data),
-			       cn->bh->b_size);
-			kunmap(page);
+			memcpy_from_page(tmp_bh->b_data, page,
+					 offset_in_page(cn->bh->b_data),
+					 cn->bh->b_size);
 			mark_buffer_dirty(tmp_bh);
 			jindex++;
 			set_buffer_journal_dirty(cn->bh);
Signature · 1 line
2.28.0.rc0.12.gb6a658bd00c9
#28 lore raw

PATCH 12/17 fs/cramfs: Use memcpy_from_page()

In reply to [email protected]

From: Ira Weiny <[email protected]>

Remove open coded kmap/memcpy/kunmap and use mempcy_from_page() instead.

Cc Nicolas Pitre <[email protected]>
---
 fs/cramfs/inode.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
fs/cramfs/inode.c +1 -2
index 4b90cfd1ec36..996a3a32a01f 100644
@@ -247,8 +247,7 @@ static void *cramfs_blkdev_read(struct super_block *sb, unsigned int offset,
 		struct page *page = pages[i];
 
 		if (page) {
-			memcpy(data, kmap(page), PAGE_SIZE);
-			kunmap(page);
+			memcpy_from_page(data, page, 0, PAGE_SIZE);
 			put_page(page);
 		} else
 			memset(data, 0, PAGE_SIZE);
Signature · 1 line
2.28.0.rc0.12.gb6a658bd00c9
maintainer Acked-by on v1 12/17
#29 lore raw

Re: [PATCH 12/17] fs/cramfs: Use memcpy_from_page()

Quoted 6 lines · On Mon, 23 Nov 2020, [email protected] wrote:

From: Ira Weiny <[email protected]>

Remove open coded kmap/memcpy/kunmap and use mempcy_from_page() instead.

Cc Nicolas Pitre <[email protected]>
Quoted 22 lines
---
 fs/cramfs/inode.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
fs/cramfs/inode.c +1 -2
index 4b90cfd1ec36..996a3a32a01f 100644
@@ -247,8 +247,7 @@ static void *cramfs_blkdev_read(struct super_block *sb, unsigned int offset,
 		struct page *page = pages[i];
 
 		if (page) {
-			memcpy(data, kmap(page), PAGE_SIZE);
-			kunmap(page);
+			memcpy_from_page(data, page, 0, PAGE_SIZE);
 			put_page(page);
 		} else
 			memset(data, 0, PAGE_SIZE);
Signature · 1 line
2.28.0.rc0.12.gb6a658bd00c9
#30 lore raw

PATCH 13/17 drivers/target: Convert to mem*_page()

In reply to [email protected]

From: Ira Weiny <[email protected]>

Remove the kmap/mem*()/kunmap patter and use the new mem*_page() functions.

Cc "Martin K. Petersen" <[email protected]>
---
 drivers/target/target_core_rd.c        |  6 ++----
 drivers/target/target_core_transport.c | 10 +++-------
 2 files changed, 5 insertions(+), 11 deletions(-)
drivers/target/target_core_rd.c +2 -4
index bf936bbeccfe..30bf0fcae519 100644
@@ -18,6 +18,7 @@
 #include <linux/scatterlist.h>
 #include <linux/slab.h>
 #include <linux/spinlock.h>
+#include <linux/pagemap.h>
 #include <scsi/scsi_proto.h>
 
 #include <target/target_core_base.h>
@@ -117,7 +118,6 @@ static int rd_allocate_sgl_table(struct rd_dev *rd_dev, struct rd_dev_sg_table *
 				sizeof(struct scatterlist));
 	struct page *pg;
 	struct scatterlist *sg;
-	unsigned char *p;
 
 	while (total_sg_needed) {
 		unsigned int chain_entry = 0;
@@ -159,9 +159,7 @@ static int rd_allocate_sgl_table(struct rd_dev *rd_dev, struct rd_dev_sg_table *
 			sg_assign_page(&sg[j], pg);
 			sg[j].length = PAGE_SIZE;
 
-			p = kmap(pg);
-			memset(p, init_payload, PAGE_SIZE);
-			kunmap(pg);
+			memset_page(pg, init_payload, 0, PAGE_SIZE);
 		}
 
 		page_offset += sg_per_table;
drivers/target/target_core_transport.c +3 -7
index ff26ab0a5f60..4fec5c728344 100644
@@ -22,6 +22,7 @@
 #include <linux/module.h>
 #include <linux/ratelimit.h>
 #include <linux/vmalloc.h>
+#include <linux/pagemap.h>
 #include <asm/unaligned.h>
 #include <net/sock.h>
 #include <net/tcp.h>
@@ -1689,15 +1690,10 @@ int target_submit_cmd_map_sgls(struct se_cmd *se_cmd, struct se_session *se_sess
 		 */
 		if (!(se_cmd->se_cmd_flags & SCF_SCSI_DATA_CDB) &&
 		     se_cmd->data_direction == DMA_FROM_DEVICE) {
-			unsigned char *buf = NULL;
 
 			if (sgl)
-				buf = kmap(sg_page(sgl)) + sgl->offset;
-
-			if (buf) {
-				memset(buf, 0, sgl->length);
-				kunmap(sg_page(sgl));
-			}
+				memzero_page(sg_page(sgl), sgl->offset,
+					     sgl->length);
 		}
 
 		rc = transport_generic_map_mem_to_cmd(se_cmd, sgl, sgl_count,
Signature · 1 line
2.28.0.rc0.12.gb6a658bd00c9
#31 lore raw

PATCH 14/17 drivers/scsi: Use memcpy_to_page()

In reply to [email protected]

From: Ira Weiny <[email protected]>

Remove kmap/mem*()/kunmap pattern and use memcpy_to_page()

---
 drivers/scsi/ipr.c | 11 ++---------
 1 file changed, 2 insertions(+), 9 deletions(-)
drivers/scsi/ipr.c +2 -9
index b0aa58d117cc..3cdd8db24270 100644
@@ -3912,7 +3912,6 @@ static int ipr_copy_ucode_buffer(struct ipr_sglist *sglist,
 {
 	int bsize_elem, i, result = 0;
 	struct scatterlist *sg;
-	void *kaddr;
 
 	/* Determine the actual number of bytes per element */
 	bsize_elem = PAGE_SIZE * (1 << sglist->order);
@@ -3923,10 +3922,7 @@ static int ipr_copy_ucode_buffer(struct ipr_sglist *sglist,
 			buffer += bsize_elem) {
 		struct page *page = sg_page(sg);
 
-		kaddr = kmap(page);
-		memcpy(kaddr, buffer, bsize_elem);
-		kunmap(page);
-
+		memcpy_to_page(page, 0, buffer, bsize_elem);
 		sg->length = bsize_elem;
 
 		if (result != 0) {
@@ -3938,10 +3934,7 @@ static int ipr_copy_ucode_buffer(struct ipr_sglist *sglist,
 	if (len % bsize_elem) {
 		struct page *page = sg_page(sg);
 
-		kaddr = kmap(page);
-		memcpy(kaddr, buffer, len % bsize_elem);
-		kunmap(page);
-
+		memcpy_to_page(page, 0, buffer, len % bsize_elem);
 		sg->length = len % bsize_elem;
 	}
 
Signature · 1 line
2.28.0.rc0.12.gb6a658bd00c9
#32 lore raw

PATCH 15/17 drivers/staging: Use memcpy_to/from_page()

In reply to [email protected]

From: Ira Weiny <[email protected]>

Remove kmap/mem*()/kunmap pattern and use memcpy_to/from_page()

Cc Greg Kroah-Hartman <[email protected]>
---
 drivers/staging/rts5208/rtsx_transport.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/staging/rts5208/rtsx_transport.c +4 -4
index 909a3e663ef6..e0e52bae953e 100644
@@ -92,13 +92,13 @@ unsigned int rtsx_stor_access_xfer_buf(unsigned char *buffer,
 			while (sglen > 0) {
 				unsigned int plen = min(sglen, (unsigned int)
 						PAGE_SIZE - poff);
-				unsigned char *ptr = kmap(page);
 
 				if (dir == TO_XFER_BUF)
-					memcpy(ptr + poff, buffer + cnt, plen);
+					memcpy_to_page(page, poff,
+						       buffer + cnt, plen);
 				else
-					memcpy(buffer + cnt, ptr + poff, plen);
-				kunmap(page);
+					memcpy_from_page(buffer + cnt, page,
+							 poff, plen);
 
 				/* Start at the beginning of the next page */
 				poff = 0;
Signature · 1 line
2.28.0.rc0.12.gb6a658bd00c9
#33 lore raw

PATCH 16/17 lib: Use mempcy_to/from_page()

In reply to [email protected]

From: Ira Weiny <[email protected]>

Remove kmap/mem*()/kunmap pattern and use memcpy_to/from_page()

Cc Alexei Starovoitov <[email protected]>
Cc Daniel Borkmann <[email protected]>
Cc "Jérôme Glisse" <[email protected]>
---
 lib/test_bpf.c | 11 ++---------
 lib/test_hmm.c | 10 ++--------
 2 files changed, 4 insertions(+), 17 deletions(-)
lib/test_bpf.c +2 -9
index ca7d635bccd9..def048bc1c48 100644
@@ -14,6 +14,7 @@
 #include <linux/skbuff.h>
 #include <linux/netdevice.h>
 #include <linux/if_vlan.h>
+#include <linux/pagemap.h>
 #include <linux/random.h>
 #include <linux/highmem.h>
 #include <linux/sched.h>
@@ -6499,25 +6500,17 @@ static void *generate_test_data(struct bpf_test *test, int sub)
 		 * single fragment to the skb, filled with
 		 * test->frag_data.
 		 */
-		void *ptr;
-
 		page = alloc_page(GFP_KERNEL);
 
 		if (!page)
 			goto err_kfree_skb;
 
-		ptr = kmap(page);
-		if (!ptr)
-			goto err_free_page;
-		memcpy(ptr, test->frag_data, MAX_DATA);
-		kunmap(page);
+		memcpy_to_page(page, 0, test->frag_data, MAX_DATA);
 		skb_add_rx_frag(skb, 0, page, 0, MAX_DATA, MAX_DATA);
 	}
 
 	return skb;
 
-err_free_page:
-	__free_page(page);
 err_kfree_skb:
 	kfree_skb(skb);
 	return NULL;
lib/test_hmm.c +2 -8
index 80a78877bd93..6a5fe7c4088b 100644
@@ -321,16 +321,13 @@ static int dmirror_do_read(struct dmirror *dmirror, unsigned long start,
 	for (pfn = start >> PAGE_SHIFT; pfn < (end >> PAGE_SHIFT); pfn++) {
 		void *entry;
 		struct page *page;
-		void *tmp;
 
 		entry = xa_load(&dmirror->pt, pfn);
 		page = xa_untag_pointer(entry);
 		if (!page)
 			return -ENOENT;
 
-		tmp = kmap(page);
-		memcpy(ptr, tmp, PAGE_SIZE);
-		kunmap(page);
+		memcpy_from_page(ptr, page, 0, PAGE_SIZE);
 
 		ptr += PAGE_SIZE;
 		bounce->cpages++;
@@ -390,16 +387,13 @@ static int dmirror_do_write(struct dmirror *dmirror, unsigned long start,
 	for (pfn = start >> PAGE_SHIFT; pfn < (end >> PAGE_SHIFT); pfn++) {
 		void *entry;
 		struct page *page;
-		void *tmp;
 
 		entry = xa_load(&dmirror->pt, pfn);
 		page = xa_untag_pointer(entry);
 		if (!page || xa_pointer_tag(entry) != DPT_XA_TAG_WRITE)
 			return -ENOENT;
 
-		tmp = kmap(page);
-		memcpy(tmp, ptr, PAGE_SIZE);
-		kunmap(page);
+		memcpy_to_page(page, 0, ptr, PAGE_SIZE);
 
 		ptr += PAGE_SIZE;
 		bounce->cpages++;
Signature · 1 line
2.28.0.rc0.12.gb6a658bd00c9
#34 lore raw

PATCH 17/17 samples: Use memcpy_to/from_page()

In reply to [email protected]

From: Ira Weiny <[email protected]>

Remove kmap/mem*()/kunmap pattern and use memcpy_to/from_page()

Cc Kirti Wankhede <[email protected]>
---
 samples/vfio-mdev/mbochs.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
samples/vfio-mdev/mbochs.c +3 -5
index e03068917273..54fe04f63c66 100644
@@ -30,6 +30,7 @@
 #include <linux/iommu.h>
 #include <linux/sysfs.h>
 #include <linux/mdev.h>
+#include <linux/pagemap.h>
 #include <linux/pci.h>
 #include <linux/dma-buf.h>
 #include <linux/highmem.h>
@@ -442,7 +443,6 @@ static ssize_t mdev_access(struct mdev_device *mdev, char *buf, size_t count,
 	struct device *dev = mdev_dev(mdev);
 	struct page *pg;
 	loff_t poff;
-	char *map;
 	int ret = 0;
 
 	mutex_lock(&mdev_state->ops_lock);
@@ -479,12 +479,10 @@ static ssize_t mdev_access(struct mdev_device *mdev, char *buf, size_t count,
 		pos -= MBOCHS_MMIO_BAR_OFFSET;
 		poff = pos & ~PAGE_MASK;
 		pg = __mbochs_get_page(mdev_state, pos >> PAGE_SHIFT);
-		map = kmap(pg);
 		if (is_write)
-			memcpy(map + poff, buf, count);
+			memcpy_to_page(pg, poff, buf, count);
 		else
-			memcpy(buf, map + poff, count);
-		kunmap(pg);
+			memcpy_from_page(buf, pg, poff, count);
 		put_page(pg);
 
 	} else {
Signature · 1 line
2.28.0.rc0.12.gb6a658bd00c9