diff mbox series

[1/2] filemap: Add folio_copy_tail()

Message ID 20230303064315.701090-2-willy@infradead.org (mailing list archive)
State Deferred, archived
Headers show
Series folio_copy_tail | expand

Commit Message

Matthew Wilcox March 3, 2023, 6:43 a.m. UTC
This is a helper function for filesystems which support file tails.

Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
---
 include/linux/pagemap.h |  1 +
 mm/filemap.c            | 57 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 58 insertions(+)

Comments

kernel test robot March 3, 2023, 12:05 p.m. UTC | #1
Hi Matthew,

I love your patch! Perhaps something to improve:

[auto build test WARNING on akpm-mm/mm-everything]
[also build test WARNING on linus/master next-20230303]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Matthew-Wilcox-Oracle/filemap-Add-folio_copy_tail/20230303-144359
base:   https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything
patch link:    https://lore.kernel.org/r/20230303064315.701090-2-willy%40infradead.org
patch subject: [PATCH 1/2] filemap: Add folio_copy_tail()
config: arm-randconfig-r006-20230302 (https://download.01.org/0day-ci/archive/20230303/202303031911.we6DjYXd-lkp@intel.com/config)
compiler: clang version 17.0.0 (https://github.com/llvm/llvm-project 67409911353323ca5edf2049ef0df54132fa1ca7)
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # install arm cross compiling tool for clang build
        # apt-get install binutils-arm-linux-gnueabi
        # https://github.com/intel-lab-lkp/linux/commit/a4ee841f30215e4f7c5dda2ab82007f590a6a62b
        git remote add linux-review https://github.com/intel-lab-lkp/linux
        git fetch --no-tags linux-review Matthew-Wilcox-Oracle/filemap-Add-folio_copy_tail/20230303-144359
        git checkout a4ee841f30215e4f7c5dda2ab82007f590a6a62b
        # save the config file
        mkdir build_dir && cp config build_dir/.config
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=arm olddefconfig
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=arm SHELL=/bin/bash

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <lkp@intel.com>
| Link: https://lore.kernel.org/oe-kbuild-all/202303031911.we6DjYXd-lkp@intel.com/

All warnings (new ones prefixed by >>):

>> mm/filemap.c:4160:17: warning: comparison of distinct pointer types ('typeof (poff + len) *' (aka 'unsigned int *') and 'typeof (((1UL) << 12)) *' (aka 'unsigned long *')) [-Wcompare-distinct-pointer-types]
                   size_t plen = min(poff + len, PAGE_SIZE) - poff;
                                 ^~~~~~~~~~~~~~~~~~~~~~~~~~
   include/linux/minmax.h:67:19: note: expanded from macro 'min'
   #define min(x, y)       __careful_cmp(x, y, <)
                           ^~~~~~~~~~~~~~~~~~~~~~
   include/linux/minmax.h:36:24: note: expanded from macro '__careful_cmp'
           __builtin_choose_expr(__safe_cmp(x, y), \
                                 ^~~~~~~~~~~~~~~~
   include/linux/minmax.h:26:4: note: expanded from macro '__safe_cmp'
                   (__typecheck(x, y) && __no_side_effects(x, y))
                    ^~~~~~~~~~~~~~~~~
   include/linux/minmax.h:20:28: note: expanded from macro '__typecheck'
           (!!(sizeof((typeof(x) *)1 == (typeof(y) *)1)))
                      ~~~~~~~~~~~~~~ ^  ~~~~~~~~~~~~~~
>> mm/filemap.c:4172:11: warning: comparison of distinct pointer types ('typeof (len) *' (aka 'unsigned int *') and 'typeof (((1UL) << 12)) *' (aka 'unsigned long *')) [-Wcompare-distinct-pointer-types]
                           plen = min(len, PAGE_SIZE);
                                  ^~~~~~~~~~~~~~~~~~~
   include/linux/minmax.h:67:19: note: expanded from macro 'min'
   #define min(x, y)       __careful_cmp(x, y, <)
                           ^~~~~~~~~~~~~~~~~~~~~~
   include/linux/minmax.h:36:24: note: expanded from macro '__careful_cmp'
           __builtin_choose_expr(__safe_cmp(x, y), \
                                 ^~~~~~~~~~~~~~~~
   include/linux/minmax.h:26:4: note: expanded from macro '__safe_cmp'
                   (__typecheck(x, y) && __no_side_effects(x, y))
                    ^~~~~~~~~~~~~~~~~
   include/linux/minmax.h:20:28: note: expanded from macro '__typecheck'
           (!!(sizeof((typeof(x) *)1 == (typeof(y) *)1)))
                      ~~~~~~~~~~~~~~ ^  ~~~~~~~~~~~~~~
   2 warnings generated.


vim +4160 mm/filemap.c

  4125	
  4126	/**
  4127	 * folio_copy_tail - Copy an in-memory file tail into a page cache folio.
  4128	 * @folio: The folio to copy into.
  4129	 * @pos: The file position of the first byte of data in the tail.
  4130	 * @src: The address of the tail data.
  4131	 * @max: The size of the buffer used for the tail data.
  4132	 *
  4133	 * Supports file tails starting at @pos that are a maximum of @max
  4134	 * bytes in size.  Zeroes the remainder of the folio.
  4135	 */
  4136	void folio_copy_tail(struct folio *folio, loff_t pos, void *src, size_t max)
  4137	{
  4138		loff_t isize = i_size_read(folio->mapping->host);
  4139		size_t offset, len = isize - pos;
  4140		char *dst;
  4141	
  4142		if (folio_pos(folio) > isize) {
  4143			len = 0;
  4144		} else if (folio_pos(folio) > pos) {
  4145			len -= folio_pos(folio) - pos;
  4146			src += folio_pos(folio) - pos;
  4147			max -= folio_pos(folio) - pos;
  4148			pos = folio_pos(folio);
  4149		}
  4150		/*
  4151		 * i_size is larger than the number of bytes stored in the tail?
  4152		 * Assume the remainder is zero-padded.
  4153		 */
  4154		if (WARN_ON_ONCE(len > max))
  4155			len = max;
  4156		offset = offset_in_folio(folio, pos);
  4157		dst = kmap_local_folio(folio, offset);
  4158		if (folio_test_highmem(folio) && folio_test_large(folio)) {
  4159			size_t poff = offset_in_page(offset);
> 4160			size_t plen = min(poff + len, PAGE_SIZE) - poff;
  4161	
  4162			for (;;) {
  4163				memcpy(dst, src, plen);
  4164				memset(dst + plen, 0, PAGE_SIZE - poff - plen);
  4165				offset += PAGE_SIZE - poff;
  4166				if (offset == folio_size(folio))
  4167					break;
  4168				kunmap_local(dst);
  4169				dst = kmap_local_folio(folio, offset);
  4170				len -= plen;
  4171				poff = 0;
> 4172				plen = min(len, PAGE_SIZE);
diff mbox series

Patch

diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h
index c21b3ad1068c..618fe184c248 100644
--- a/include/linux/pagemap.h
+++ b/include/linux/pagemap.h
@@ -764,6 +764,7 @@  static inline struct page *grab_cache_page(struct address_space *mapping,
 	return find_or_create_page(mapping, index, mapping_gfp_mask(mapping));
 }
 
+void folio_copy_tail(struct folio *, loff_t pos, void *src, size_t max);
 struct folio *read_cache_folio(struct address_space *, pgoff_t index,
 		filler_t *filler, struct file *file);
 struct folio *mapping_read_folio_gfp(struct address_space *, pgoff_t index,
diff --git a/mm/filemap.c b/mm/filemap.c
index 40be33b5ee46..b02d9c390d3c 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -4145,3 +4145,60 @@  bool filemap_release_folio(struct folio *folio, gfp_t gfp)
 	return try_to_free_buffers(folio);
 }
 EXPORT_SYMBOL(filemap_release_folio);
+
+/**
+ * folio_copy_tail - Copy an in-memory file tail into a page cache folio.
+ * @folio: The folio to copy into.
+ * @pos: The file position of the first byte of data in the tail.
+ * @src: The address of the tail data.
+ * @max: The size of the buffer used for the tail data.
+ *
+ * Supports file tails starting at @pos that are a maximum of @max
+ * bytes in size.  Zeroes the remainder of the folio.
+ */
+void folio_copy_tail(struct folio *folio, loff_t pos, void *src, size_t max)
+{
+	loff_t isize = i_size_read(folio->mapping->host);
+	size_t offset, len = isize - pos;
+	char *dst;
+
+	if (folio_pos(folio) > isize) {
+		len = 0;
+	} else if (folio_pos(folio) > pos) {
+		len -= folio_pos(folio) - pos;
+		src += folio_pos(folio) - pos;
+		max -= folio_pos(folio) - pos;
+		pos = folio_pos(folio);
+	}
+	/*
+	 * i_size is larger than the number of bytes stored in the tail?
+	 * Assume the remainder is zero-padded.
+	 */
+	if (WARN_ON_ONCE(len > max))
+		len = max;
+	offset = offset_in_folio(folio, pos);
+	dst = kmap_local_folio(folio, offset);
+	if (folio_test_highmem(folio) && folio_test_large(folio)) {
+		size_t poff = offset_in_page(offset);
+		size_t plen = min(poff + len, PAGE_SIZE) - poff;
+
+		for (;;) {
+			memcpy(dst, src, plen);
+			memset(dst + plen, 0, PAGE_SIZE - poff - plen);
+			offset += PAGE_SIZE - poff;
+			if (offset == folio_size(folio))
+				break;
+			kunmap_local(dst);
+			dst = kmap_local_folio(folio, offset);
+			len -= plen;
+			poff = 0;
+			plen = min(len, PAGE_SIZE);
+		}
+	} else {
+		memcpy(dst, src, len);
+		memset(dst + len, 0, folio_size(folio) - len - offset);
+	}
+	kunmap_local(dst);
+	flush_dcache_folio(folio);
+}
+EXPORT_SYMBOL_GPL(folio_copy_tail);