Batched page table updates for file-backed large folios
Subsystems (5)
Matthew Wilcox (Oracle)
maintainerv5 cover Batched page table updates for file-backed large folios
On page fault, filemap_map_pages() already retrieves a folio from the page cache and iterates over it, which realised some savings. This patch series drives that further down by allowing filemap to tell the MM to map a contiguous range of pages in the folio. This improves performance by batching the updates to the folio's refcount and the rmap counters.
Testing with a micro benchmark like will-it-scale.pagefault on a 48C/96T IceLake box showed:
- batched rmap brings around 15% performance gain
- batched refcount brings around 2% performance gain
v4:
- Add the set_ptes() architecture interface
- Change various interfaces to take (folio, struct page *, nr) instead of (folio, unsigned long start, nr)
- Remove do_set_pte() instead of keeping a compat interface
- Add a check in set_pte_range() to ensure that large anon folios are not being passed in yet (David Hildenbrand)
- Save / restore the old vmf->pte pointer instead of passing a different pte pointer to set_pte_range()
Matthew Wilcox (Oracle) (1):
mm: Add generic set_ptes()
Yin Fengwei (4):
filemap: Add filemap_map_folio_range()
rmap: add folio_add_file_rmap_range()
mm: Convert do_set_pte() to set_pte_range()
filemap: Batch PTE mappings
Documentation/filesystems/locking.rst | 2 +- include/linux/mm.h | 3 +- include/linux/pgtable.h | 27 +++++++ include/linux/rmap.h | 2 + mm/filemap.c | 111 ++++++++++++++++---------- mm/memory.c | 31 ++++--- mm/rmap.c | 60 ++++++++++---- 7 files changed, 164 insertions(+), 72 deletions(-)
Signature · 1 line
2.35.1
Matthew Wilcox (Oracle)
maintainerPATCH v5 1/5 filemap: Add filemap_map_folio_range()
From: Yin Fengwei <[email protected]>
filemap_map_folio_range() maps partial/full folio. Comparing to original filemap_map_pages(), it updates refcount once per folio instead of per page and gets minor performance improvement for large folio.
With a will-it-scale.page_fault3 like app (change file write fault testing to read fault testing. Trying to upstream it to will-it-scale at [1]), got 2% performance gain on a 48C/96T Cascade Lake test box with 96 processes running against xfs.
[1]: https://github.com/antonblanchard/will-it-scale/pull/37
--- mm/filemap.c | 98 +++++++++++++++++++++++++++++----------------------- 1 file changed, 54 insertions(+), 44 deletions(-)
mm/filemap.c +54 -44
@@ -2200,16 +2200,6 @@ unsigned filemap_get_folios(struct address_space *mapping, pgoff_t *start, }EXPORT_SYMBOL (filemap_get_folios); -static inline -bool folio_more_pages (struct folio *folio,pgoff_t index,pgoff_t max) -{ -if (!folio_test_large (folio) ||folio_test_hugetlb (folio)) -return false; -if (index >= max) -return false; -return index < folio->index +folio_nr_pages (folio) -1 ; -} -/** * filemap_get_folios_contig - Get a batch of contiguous folios * @mapping: The address_space to search
@@ -3351,6 +3341,53 @@ static inline struct folio *next_map_page(struct address_space *mapping, mapping, xas, end_pgoff); } +/* +* Map page range [start_page, start_page + nr_pages) of folio. +* start_page is gotten from start by folio_page(folio, start) +*/ +static vm_fault_t filemap_map_folio_range (struct vm_fault *vmf, +struct folio *folio,unsigned long start, +unsigned long addr,unsigned int nr_pages) +{ +vm_fault_t ret =0 ; +struct vm_area_struct *vma = vmf->vma; +struct file *file = vma->vm_file; +struct page *page =folio_page (folio, start); +unsigned int mmap_miss =READ_ONCE (file->f_ra.mmap_miss); +unsigned int ref_count =0 , count =0 ; + +do { +if (PageHWPoison (page)) +continue ; + +if (mmap_miss >0 ) + mmap_miss--; + +/* +* NOTE: If there're PTE markers, we'll leave them to be +* handled in the specific fault path, and it'll prohibit the +* fault-around logic. +*/ +if (!pte_none (*vmf->pte)) +continue ; + +if (vmf->address == addr) + ret =VM_FAULT_NOPAGE ; + + ref_count++; +do_set_pte (vmf, page, addr); +update_mmu_cache (vma, addr, vmf->pte); + }while (vmf->pte++, page++, addr +=PAGE_SIZE , ++count < nr_pages); + +/* Restore the vmf->pte */ + vmf->pte -= nr_pages; + +folio_ref_add (folio, ref_count); +WRITE_ONCE (file->f_ra.mmap_miss, mmap_miss); + +return ret; +} +vm_fault_t filemap_map_pages (struct vm_fault *vmf,pgoff_t start_pgoff,pgoff_t end_pgoff) {
@@ -3361,9 +3398,9 @@ vm_fault_t filemap_map_pages(struct vm_fault *vmf,unsigned long addr;XA_STATE (xas, &mapping->i_pages, start_pgoff);struct folio *folio; -struct page *page;unsigned int mmap_miss =READ_ONCE (file->f_ra.mmap_miss);vm_fault_t ret =0 ; +int nr_pages =0 ;rcu_read_lock (); folio =first_map_page (mapping, &xas, end_pgoff);
@@ -3378,45 +3415,18 @@ vm_fault_t filemap_map_pages(struct vm_fault *vmf, addr = vma->vm_start + ((start_pgoff - vma->vm_pgoff) <<PAGE_SHIFT ); vmf->pte =pte_offset_map_lock (vma->vm_mm, vmf->pmd, addr, &vmf->ptl);do { -again: - page =folio_file_page (folio, xas.xa_index); -if (PageHWPoison (page)) - goto unlock; - -if (mmap_miss >0 ) - mmap_miss--; +unsigned long end; addr += (xas.xa_index - last_pgoff) <<PAGE_SHIFT ; vmf->pte += xas.xa_index - last_pgoff; last_pgoff = xas.xa_index; + end = folio->index +folio_nr_pages (folio) -1 ; + nr_pages =min (end, end_pgoff) - xas.xa_index +1 ; -/* -* NOTE: If there're PTE markers, we'll leave them to be -* handled in the specific fault path, and it'll prohibit the -* fault-around logic. -*/ -if (!pte_none (*vmf->pte)) - goto unlock; - -/* We're about to handle the fault */ -if (vmf->address == addr) - ret =VM_FAULT_NOPAGE ; + ret |=filemap_map_folio_range (vmf, folio, + xas.xa_index - folio->index, addr, nr_pages); + xas.xa_index += nr_pages; -do_set_pte (vmf, page, addr); -/* no need to invalidate: a not-present page won't be cached */ -update_mmu_cache (vma, addr, vmf->pte); -if (folio_more_pages (folio, xas.xa_index, end_pgoff)) { - xas.xa_index++; -folio_ref_inc (folio); - goto again; - } -folio_unlock (folio); -continue ; -unlock: -if (folio_more_pages (folio, xas.xa_index, end_pgoff)) { - xas.xa_index++; - goto again; - }folio_unlock (folio);folio_put (folio); }while ((folio =next_map_page (mapping, &xas, end_pgoff)) !=NULL );
Signature · 1 line
2.35.1
Matthew Wilcox (Oracle)
maintainerPATCH v5 2/5 mm: Add generic set_ptes()
In reply to Matthew Wilcox (Oracle)This set_ptes should work for most architectures. Those that need to set a special bit in their PTEs or have their PFNs located at a different offset from PAGE_SHIFT will need to override it.
--- include/linux/pgtable.h | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+)
include/linux/pgtable.h +27 -0
@@ -1439,6 +1439,33 @@ static inline int pmd_protnone(pmd_t pmd)#endif /* CONFIG_MMU */ +#ifndef set_ptes +/** +* set_ptes - Map consecutive pages to a contiguous range of addresses. +* @mm: Address space to map the pages into. +* @addr: Address to map the first page at. +* @ptep: Page table pointer for the first entry. +* @pte: Page table entry for the first page. +* @nr: Number of pages to map. +* +* Context: The caller holds the page table lock. The PTEs all lie +* within a single PMD (and VMA, and folio). +*/ +static inline void set_ptes (struct mm_struct *mm,unsigned long addr, +pte_t *ptep,pte_t pte,unsigned int nr) +{ +for (;;) { +set_pte_at (mm, addr, ptep, pte); +if (--nr ==0 ) +break ; + ptep++; + addr +=PAGE_SIZE ; +/* This works for x86. Check how PTEs are encoded */ + pte =__pte (pte_val (pte) +PAGE_SIZE ); + } +} +#endif +#ifdef CONFIG_HAVE_ARCH_HUGE_VMAP #ifndef __PAGETABLE_P4D_FOLDED
Signature · 1 line
2.35.1
kernel test robot
build botRe: [PATCH v5 2/5] mm: Add generic set_ptes()
I love your patch! Yet something to improve:
Hi Matthew,
I love your patch! Yet something to improve:
[auto build test ERROR on next-20230207]
[also build test ERROR on v6.2-rc7]
[cannot apply to linus/master v6.2-rc7 v6.2-rc6 v6.2-rc5] [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-filemap_map_folio_range/20230208-041404 patch link: https://lore.kernel.org/r/20230207194937.122543-3-willy%40infradead.org patch subject: [PATCH v5 2/5] mm: Add generic set_ptes() config: arm-randconfig-r016-20230205 (https://download.01.org/0day-ci/archive/20230208/[email protected]/config) compiler: arm-linux-gnueabi-gcc (GCC) 12.1.0
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
# https://github.com/intel-lab-lkp/linux/commit/1549aed85e99407fbf2600da432b84c0660d2029
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Matthew-Wilcox-Oracle/filemap-Add-filemap_map_folio_range/20230208-041404 git checkout 1549aed85e99407fbf2600da432b84c0660d2029
# save the config file
mkdir build_dir && cp config build_dir/.config
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=arm olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=gcc-12.1.0 make.cross W=1 O=build_dir ARCH=arm prepare
If you fix the issue, kindly add following tag where applicable | Reported-by: kernel test robot <[email protected]>
All errors (new ones prefixed by >>):
In file included from include/linux/mm.h:29,
from arch/arm/kernel/asm-offsets.c:12:
include/linux/pgtable.h: In function 'set_ptes':
>> include/linux/pgtable.h:1458:17: error: implicit declaration of function 'set_pte_at'; did you mean 'set_ptes'? [-Werror=implicit-function-declaration]
1458 | set_pte_at(mm, addr, ptep, pte);
| ^~~~~~~~~~
| set_ptes cc1: some warnings being treated as errors
make[2]: *** [scripts/Makefile.build:114: arch/arm/kernel/asm-offsets.s] Error 1 make[2]: Target 'prepare' not remade because of errors.
make[1]: *** [Makefile:1286: prepare0] Error 2
make[1]: Target 'prepare' not remade because of errors.
make: *** [Makefile:226: __sub-make] Error 2
make: Target 'prepare' not remade because of errors.
vim +1458 include/linux/pgtable.h
1441
1442 #ifndef set_ptes
1443 /**
1444 * set_ptes - Map consecutive pages to a contiguous range of addresses.
1445 * @mm: Address space to map the pages into.
1446 * @addr: Address to map the first page at.
1447 * @ptep: Page table pointer for the first entry.
1448 * @pte: Page table entry for the first page.
1449 * @nr: Number of pages to map.
1450 *
1451 * Context: The caller holds the page table lock. The PTEs all lie
1452 * within a single PMD (and VMA, and folio).
1453 */
1454 static inline void set_ptes(struct mm_struct *mm, unsigned long addr,
1455 pte_t *ptep, pte_t pte, unsigned int nr)
1456 {
1457 for (;;) {
> 1458 set_pte_at(mm, addr, ptep, pte);
1459 if (--nr == 0)
1460 break;
1461 ptep++;
1462 addr += PAGE_SIZE;
1463 /* This works for x86. Check how PTEs are encoded */
1464 pte = __pte(pte_val(pte) + PAGE_SIZE);
1465 }
1466 }
1467 #endif
1468
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests
kernel test robot
build botRe: [PATCH v5 2/5] mm: Add generic set_ptes()
In reply to Matthew Wilcox (Oracle)I love your patch! Perhaps something to improve:
Hi Matthew,
I love your patch! Perhaps something to improve:
[auto build test WARNING on next-20230207]
[cannot apply to linus/master v6.2-rc7 v6.2-rc6 v6.2-rc5] [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-filemap_map_folio_range/20230208-041404 patch link: https://lore.kernel.org/r/20230207194937.122543-3-willy%40infradead.org patch subject: [PATCH v5 2/5] mm: Add generic set_ptes() config: hexagon-randconfig-r041-20230205 (https://download.01.org/0day-ci/archive/20230208/[email protected]/config) compiler: clang version 17.0.0 (https://github.com/llvm/llvm-project db0e6591612b53910a1b366863348bdb9d7d2fb1)
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
# https://github.com/intel-lab-lkp/linux/commit/1549aed85e99407fbf2600da432b84c0660d2029
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review Matthew-Wilcox-Oracle/filemap-Add-filemap_map_folio_range/20230208-041404 git checkout 1549aed85e99407fbf2600da432b84c0660d2029
# 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=hexagon olddefconfig COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross W=1 O=build_dir ARCH=hexagon SHELL=/bin/bash drivers/media/dvb-frontends/
If you fix the issue, kindly add following tag where applicable | Reported-by: kernel test robot <[email protected]>
All warnings (new ones prefixed by >>):
In file included from drivers/media/dvb-frontends/a8293.c:8:
In file included from drivers/media/dvb-frontends/a8293.h:11:
In file included from include/media/dvb_frontend.h:38:
In file included from include/linux/i2c.h:19:
In file included from include/linux/regulator/consumer.h:35:
In file included from include/linux/suspend.h:5:
In file included from include/linux/swap.h:9:
In file included from include/linux/memcontrol.h:13:
In file included from include/linux/cgroup.h:26:
In file included from include/linux/kernel_stat.h:9:
In file included from include/linux/interrupt.h:11:
In file included from include/linux/hardirq.h:11:
In file included from ./arch/hexagon/include/generated/asm/hardirq.h:1:
In file included from include/asm-generic/hardirq.h:17:
In file included from include/linux/irq.h:20:
In file included from include/linux/io.h:13:
In file included from arch/hexagon/include/asm/io.h:334:
include/asm-generic/io.h:547:31: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
val = __raw_readb(PCI_IOBASE + addr);
~~~~~~~~~~ ^
include/asm-generic/io.h:560:61: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
val = __le16_to_cpu((__le16 __force)__raw_readw(PCI_IOBASE + addr));
~~~~~~~~~~ ^
include/uapi/linux/byteorder/little_endian.h:37:51: note: expanded from macro '__le16_to_cpu'
#define __le16_to_cpu(x) ((__force __u16)(__le16)(x))
^
In file included from drivers/media/dvb-frontends/a8293.c:8:
In file included from drivers/media/dvb-frontends/a8293.h:11:
In file included from include/media/dvb_frontend.h:38:
In file included from include/linux/i2c.h:19:
In file included from include/linux/regulator/consumer.h:35:
In file included from include/linux/suspend.h:5:
In file included from include/linux/swap.h:9:
In file included from include/linux/memcontrol.h:13:
In file included from include/linux/cgroup.h:26:
In file included from include/linux/kernel_stat.h:9:
In file included from include/linux/interrupt.h:11:
In file included from include/linux/hardirq.h:11:
In file included from ./arch/hexagon/include/generated/asm/hardirq.h:1:
In file included from include/asm-generic/hardirq.h:17:
In file included from include/linux/irq.h:20:
In file included from include/linux/io.h:13:
In file included from arch/hexagon/include/asm/io.h:334:
include/asm-generic/io.h:573:61: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
val = __le32_to_cpu((__le32 __force)__raw_readl(PCI_IOBASE + addr));
~~~~~~~~~~ ^
include/uapi/linux/byteorder/little_endian.h:35:51: note: expanded from macro '__le32_to_cpu'
#define __le32_to_cpu(x) ((__force __u32)(__le32)(x))
^
In file included from drivers/media/dvb-frontends/a8293.c:8:
In file included from drivers/media/dvb-frontends/a8293.h:11:
In file included from include/media/dvb_frontend.h:38:
In file included from include/linux/i2c.h:19:
In file included from include/linux/regulator/consumer.h:35:
In file included from include/linux/suspend.h:5:
In file included from include/linux/swap.h:9:
In file included from include/linux/memcontrol.h:13:
In file included from include/linux/cgroup.h:26:
In file included from include/linux/kernel_stat.h:9:
In file included from include/linux/interrupt.h:11:
In file included from include/linux/hardirq.h:11:
In file included from ./arch/hexagon/include/generated/asm/hardirq.h:1:
In file included from include/asm-generic/hardirq.h:17:
In file included from include/linux/irq.h:20:
In file included from include/linux/io.h:13:
In file included from arch/hexagon/include/asm/io.h:334:
include/asm-generic/io.h:584:33: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
__raw_writeb(value, PCI_IOBASE + addr);
~~~~~~~~~~ ^
include/asm-generic/io.h:594:59: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
__raw_writew((u16 __force)cpu_to_le16(value), PCI_IOBASE + addr);
~~~~~~~~~~ ^
include/asm-generic/io.h:604:59: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
__raw_writel((u32 __force)cpu_to_le32(value), PCI_IOBASE + addr);
~~~~~~~~~~ ^
In file included from drivers/media/dvb-frontends/a8293.c:8:
In file included from drivers/media/dvb-frontends/a8293.h:11:
In file included from include/media/dvb_frontend.h:38:
In file included from include/linux/i2c.h:19:
In file included from include/linux/regulator/consumer.h:35:
In file included from include/linux/suspend.h:5:
In file included from include/linux/swap.h:9:
In file included from include/linux/memcontrol.h:20:
In file included from include/linux/mm.h:29:
>> include/linux/pgtable.h:1454:65: warning: parameter 'addr' set but not used [-Wunused-but-set-parameter]
static inline void set_ptes(struct mm_struct *mm, unsigned long addr,
^ 7 warnings generated.
--
In file included from drivers/media/dvb-frontends/mb86a20s.c:12:
In file included from include/media/dvb_frontend.h:38:
In file included from include/linux/i2c.h:19:
In file included from include/linux/regulator/consumer.h:35:
In file included from include/linux/suspend.h:5:
In file included from include/linux/swap.h:9:
In file included from include/linux/memcontrol.h:13:
In file included from include/linux/cgroup.h:26:
In file included from include/linux/kernel_stat.h:9:
In file included from include/linux/interrupt.h:11:
In file included from include/linux/hardirq.h:11:
In file included from ./arch/hexagon/include/generated/asm/hardirq.h:1:
In file included from include/asm-generic/hardirq.h:17:
In file included from include/linux/irq.h:20:
In file included from include/linux/io.h:13:
In file included from arch/hexagon/include/asm/io.h:334:
include/asm-generic/io.h:547:31: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
val = __raw_readb(PCI_IOBASE + addr);
~~~~~~~~~~ ^
include/asm-generic/io.h:560:61: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
val = __le16_to_cpu((__le16 __force)__raw_readw(PCI_IOBASE + addr));
~~~~~~~~~~ ^
include/uapi/linux/byteorder/little_endian.h:37:51: note: expanded from macro '__le16_to_cpu'
#define __le16_to_cpu(x) ((__force __u16)(__le16)(x))
^
In file included from drivers/media/dvb-frontends/mb86a20s.c:12:
In file included from include/media/dvb_frontend.h:38:
In file included from include/linux/i2c.h:19:
In file included from include/linux/regulator/consumer.h:35:
In file included from include/linux/suspend.h:5:
In file included from include/linux/swap.h:9:
In file included from include/linux/memcontrol.h:13:
In file included from include/linux/cgroup.h:26:
In file included from include/linux/kernel_stat.h:9:
In file included from include/linux/interrupt.h:11:
In file included from include/linux/hardirq.h:11:
In file included from ./arch/hexagon/include/generated/asm/hardirq.h:1:
In file included from include/asm-generic/hardirq.h:17:
In file included from include/linux/irq.h:20:
In file included from include/linux/io.h:13:
In file included from arch/hexagon/include/asm/io.h:334:
include/asm-generic/io.h:573:61: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
val = __le32_to_cpu((__le32 __force)__raw_readl(PCI_IOBASE + addr));
~~~~~~~~~~ ^
include/uapi/linux/byteorder/little_endian.h:35:51: note: expanded from macro '__le32_to_cpu'
#define __le32_to_cpu(x) ((__force __u32)(__le32)(x))
^
In file included from drivers/media/dvb-frontends/mb86a20s.c:12:
In file included from include/media/dvb_frontend.h:38:
In file included from include/linux/i2c.h:19:
In file included from include/linux/regulator/consumer.h:35:
In file included from include/linux/suspend.h:5:
In file included from include/linux/swap.h:9:
In file included from include/linux/memcontrol.h:13:
In file included from include/linux/cgroup.h:26:
In file included from include/linux/kernel_stat.h:9:
In file included from include/linux/interrupt.h:11:
In file included from include/linux/hardirq.h:11:
In file included from ./arch/hexagon/include/generated/asm/hardirq.h:1:
In file included from include/asm-generic/hardirq.h:17:
In file included from include/linux/irq.h:20:
In file included from include/linux/io.h:13:
In file included from arch/hexagon/include/asm/io.h:334:
include/asm-generic/io.h:584:33: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
__raw_writeb(value, PCI_IOBASE + addr);
~~~~~~~~~~ ^
include/asm-generic/io.h:594:59: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
__raw_writew((u16 __force)cpu_to_le16(value), PCI_IOBASE + addr);
~~~~~~~~~~ ^
include/asm-generic/io.h:604:59: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
__raw_writel((u32 __force)cpu_to_le32(value), PCI_IOBASE + addr);
~~~~~~~~~~ ^
In file included from drivers/media/dvb-frontends/mb86a20s.c:12:
In file included from include/media/dvb_frontend.h:38:
In file included from include/linux/i2c.h:19:
In file included from include/linux/regulator/consumer.h:35:
In file included from include/linux/suspend.h:5:
In file included from include/linux/swap.h:9:
In file included from include/linux/memcontrol.h:20:
In file included from include/linux/mm.h:29:
>> include/linux/pgtable.h:1454:65: warning: parameter 'addr' set but not used [-Wunused-but-set-parameter]
static inline void set_ptes(struct mm_struct *mm, unsigned long addr,
^
drivers/media/dvb-frontends/mb86a20s.c:1572:6: warning: variable 'active_layers' set but not used [-Wunused-but-set-variable]
int active_layers = 0, pre_ber_layers = 0, post_ber_layers = 0;
^ 8 warnings generated.
--
In file included from drivers/media/dvb-frontends/mn88443x.c:12:
In file included from include/linux/regmap.h:20:
In file included from include/linux/iopoll.h:14:
In file included from include/linux/io.h:13:
In file included from arch/hexagon/include/asm/io.h:334:
include/asm-generic/io.h:547:31: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
val = __raw_readb(PCI_IOBASE + addr);
~~~~~~~~~~ ^
include/asm-generic/io.h:560:61: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
val = __le16_to_cpu((__le16 __force)__raw_readw(PCI_IOBASE + addr));
~~~~~~~~~~ ^
include/uapi/linux/byteorder/little_endian.h:37:51: note: expanded from macro '__le16_to_cpu'
#define __le16_to_cpu(x) ((__force __u16)(__le16)(x))
^
In file included from drivers/media/dvb-frontends/mn88443x.c:12:
In file included from include/linux/regmap.h:20:
In file included from include/linux/iopoll.h:14:
In file included from include/linux/io.h:13:
In file included from arch/hexagon/include/asm/io.h:334:
include/asm-generic/io.h:573:61: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
val = __le32_to_cpu((__le32 __force)__raw_readl(PCI_IOBASE + addr));
~~~~~~~~~~ ^
include/uapi/linux/byteorder/little_endian.h:35:51: note: expanded from macro '__le32_to_cpu'
#define __le32_to_cpu(x) ((__force __u32)(__le32)(x))
^
In file included from drivers/media/dvb-frontends/mn88443x.c:12:
In file included from include/linux/regmap.h:20:
In file included from include/linux/iopoll.h:14:
In file included from include/linux/io.h:13:
In file included from arch/hexagon/include/asm/io.h:334:
include/asm-generic/io.h:584:33: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
__raw_writeb(value, PCI_IOBASE + addr);
~~~~~~~~~~ ^
include/asm-generic/io.h:594:59: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
__raw_writew((u16 __force)cpu_to_le16(value), PCI_IOBASE + addr);
~~~~~~~~~~ ^
include/asm-generic/io.h:604:59: warning: performing pointer arithmetic on a null pointer has undefined behavior [-Wnull-pointer-arithmetic]
__raw_writel((u32 __force)cpu_to_le32(value), PCI_IOBASE + addr);
~~~~~~~~~~ ^
In file included from drivers/media/dvb-frontends/mn88443x.c:15:
In file included from drivers/media/dvb-frontends/mn88443x.h:11:
In file included from include/media/dvb_frontend.h:38:
In file included from include/linux/i2c.h:19:
In file included from include/linux/regulator/consumer.h:35:
In file included from include/linux/suspend.h:5:
In file included from include/linux/swap.h:9:
In file included from include/linux/memcontrol.h:20:
In file included from include/linux/mm.h:29:
>> include/linux/pgtable.h:1454:65: warning: parameter 'addr' set but not used [-Wunused-but-set-parameter]
static inline void set_ptes(struct mm_struct *mm, unsigned long addr,
^
drivers/media/dvb-frontends/mn88443x.c:782:34: warning: unused variable 'mn88443x_of_match' [-Wunused-const-variable]
static const struct of_device_id mn88443x_of_match[] = {
^8 warnings generated.
vim +/addr +1454 include/linux/pgtable.h
1441
1442 #ifndef set_ptes
1443 /**
1444 * set_ptes - Map consecutive pages to a contiguous range of addresses.
1445 * @mm: Address space to map the pages into.
1446 * @addr: Address to map the first page at.
1447 * @ptep: Page table pointer for the first entry.
1448 * @pte: Page table entry for the first page.
1449 * @nr: Number of pages to map.
1450 *
1451 * Context: The caller holds the page table lock. The PTEs all lie
1452 * within a single PMD (and VMA, and folio).
1453 */
> 1454 static inline void set_ptes(struct mm_struct *mm, unsigned long addr,
1455 pte_t *ptep, pte_t pte, unsigned int nr)
1456 {
1457 for (;;) {
1458 set_pte_at(mm, addr, ptep, pte);
1459 if (--nr == 0)
1460 break;
1461 ptep++;
1462 addr += PAGE_SIZE;
1463 /* This works for x86. Check how PTEs are encoded */
1464 pte = __pte(pte_val(pte) + PAGE_SIZE);
1465 }
1466 }
1467 #endif
1468
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests
Re: [PATCH v5 2/5] mm: Add generic set_ptes()
In reply to Matthew Wilcox (Oracle)Quoted 5 lines · Matthew Wilcox (Oracle)
This
set_ptesshould work for most architectures. Those that need to set a special bit in their PTEs or have their PFNs located at a different offset fromPAGE_SHIFTwill need to override it.Signed-off-by Matthew Wilcox (Oracle) <[email protected]>
For x86_64
Regards
Yin, Fengwei
Quoted 42 lines
--- include/linux/pgtable.h | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+)include/linux/pgtable.h +27
-0@@ -1439,6 +1439,33 @@ static inline int pmd_protnone(pmd_t pmd)#endif /* CONFIG_MMU */ +#ifndef set_ptes +/** +* set_ptes - Map consecutive pages to a contiguous range of addresses. +* @mm: Address space to map the pages into. +* @addr: Address to map the first page at. +* @ptep: Page table pointer for the first entry. +* @pte: Page table entry for the first page. +* @nr: Number of pages to map. +* +* Context: The caller holds the page table lock. The PTEs all lie +* within a single PMD (and VMA, and folio). +*/ +static inline void set_ptes (struct mm_struct *mm,unsigned long addr, +pte_t *ptep,pte_t pte,unsigned int nr) +{ +for (;;) { +set_pte_at (mm, addr, ptep, pte); +if (--nr ==0 ) +break ; + ptep++; + addr +=PAGE_SIZE ; +/* This works for x86. Check how PTEs are encoded */ + pte =__pte (pte_val (pte) +PAGE_SIZE ); + } +} +#endif +#ifdef CONFIG_HAVE_ARCH_HUGE_VMAP #ifndef __PAGETABLE_P4D_FOLDED
Re: [PATCH v5 2/5] mm: Add generic set_ptes()
In reply to Matthew Wilcox (Oracle)Quoted 26 lines · Matthew Wilcox (Oracle)
This
set_ptesshould work for most architectures. Those that need to set a special bit in their PTEs or have their PFNs located at a different offset fromPAGE_SHIFTwill need to override it.Signed-off-by Matthew Wilcox (Oracle) <[email protected]>--- include/linux/pgtable.h | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+)include/linux/pgtable.h +9
-0@@ -1439,6 +1439,33 @@ static inline int pmd_protnone(pmd_t pmd)#endif /* CONFIG_MMU */ +#ifndef set_ptes +/** +* set_ptes - Map consecutive pages to a contiguous range of addresses. +* @mm: Address space to map the pages into. +* @addr: Address to map the first page at. +* @ptep: Page table pointer for the first entry. +* @pte: Page table entry for the first page. +* @nr: Number of pages to map. +*
include/linux/pgtable.h static inline int pmd_protnone(pmd_t pmd) · line 1460
Quoted 10 lines
include/linux/pgtable.h +10
-0+* Context: The caller holds the page table lock. The PTEs all lie +* within a single PMD (and VMA, and folio). +*/ +static inline void set_ptes (struct mm_struct *mm,unsigned long addr, +pte_t *ptep,pte_t pte,unsigned int nr) +{ +for (;;) { +set_pte_at (mm, addr, ptep, pte); +if (--nr ==0 ) +break ;
Maybe do { ... } while (--nr); instead of the for()?
include/linux/pgtable.h static inline int pmd_protnone(pmd_t pmd) · line 1464
Quoted 4 lines
include/linux/pgtable.h +4
-0+ ptep++; + addr +=PAGE_SIZE ; +/* This works for x86. Check how PTEs are encoded */ + pte =__pte (pte_val (pte) +PAGE_SIZE );
Looks like it deserves own helper. Something like
pte = pte_next(pte);
Quoted 11 lines
include/linux/pgtable.h +4
-0+ } +} +#endif +#ifdef CONFIG_HAVE_ARCH_HUGE_VMAP #ifndef __PAGETABLE_P4D_FOLDEDSignature · 1 line
2.35.1
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
Matthew Wilcox
maintainerRe: [PATCH v5 2/5] mm: Add generic set_ptes()
Quoted 9 lines · Kirill A. Shutemov
include/linux/pgtable.h static inline int pmd_protnone(pmd_t pmd) · line 1460
Quoted 7 lines
include/linux/pgtable.h +7
-0+static inline void set_ptes (struct mm_struct *mm,unsigned long addr, +pte_t *ptep,pte_t pte,unsigned int nr) +{ +for (;;) { +set_pte_at (mm, addr, ptep, pte); +if (--nr ==0 ) +break ;Maybe do { ... } while (--nr); instead of the
for()?
Seems unnecessary to do the extra calculation to advance pte if we're not going to use it?
Quoted 8 lines
include/linux/pgtable.h static inline int pmd_protnone(pmd_t pmd) · line 1464
Quoted 4 lines
include/linux/pgtable.h +4
-0+ ptep++; + addr +=PAGE_SIZE ; +/* This works for x86. Check how PTEs are encoded */ + pte =__pte (pte_val (pte) +PAGE_SIZE );Looks like it deserves own helper. Something like
pte = pte_next(pte);
Maybe, but then it needs to be added to each arch, which I was trying to avoid. That said, I had another idea which I like better; more patches coming soon. Here's a sneak preview:
+++ b/arch/x86/include/asm/pgtable.h
@@ -1019,13 +1019,22 @@ static inline pud_t native_local_pudp_get_and_clear(pud_ t *pudp)
return res;
}
-static inline void set_pte_at(struct mm_struct *mm, unsigned long addr,
- pte_t *ptep, pte_t pte)
+static inline void set_ptes(struct mm_struct *mm, unsigned long addr,
+ pte_t *ptep, pte_t pte, unsigned int nr)
{
- page_table_check_ptes_set(mm, addr, ptep, pte, 1); - set_pte(ptep, pte); + page_table_check_ptes_set(mm, addr, ptep, pte, nr);
+
+ for (;;) {
+ set_pte(ptep, pte);
+ if (--nr == 0)
+ break;
+ ptep++;
+ pte = __pte(pte_val(pte) + PAGE_SIZE);
+ }
}
+#define set_pte_at(mm, addr, ptep, pte) set_ptes(mm, addr, ptep, pte, 1) +
static inline void set_pmd_at(struct mm_struct *mm, unsigned long addr,
pmd_t *pmdp, pmd_t pmd)
{
Re: [PATCH v5 2/5] mm: Add generic set_ptes()
Quoted 11 lines · Matthew Wilcox
Quoted 9 lines · Kirill A. Shutemov
include/linux/pgtable.h static inline int pmd_protnone(pmd_t pmd) · line 1460
Quoted 7 lines
include/linux/pgtable.h +7
-0+static inline void set_ptes (struct mm_struct *mm,unsigned long addr, +pte_t *ptep,pte_t pte,unsigned int nr) +{ +for (;;) { +set_pte_at (mm, addr, ptep, pte); +if (--nr ==0 ) +break ;Maybe do { ... } while (--nr); instead of the
for()?
Quoted 2 lines
Seems unnecessary to do the extra calculation to advance pte if we're not going to use it?
It should be transparent to compiler, but okay.
Quoted 8 lines
Quoted 8 lines
include/linux/pgtable.h static inline int pmd_protnone(pmd_t pmd) · line 1464
Quoted 5 lines
include/linux/pgtable.h +4
-0+ ptep++; + addr +=PAGE_SIZE ; +/* This works for x86. Check how PTEs are encoded */ + pte =__pte (pte_val (pte) +PAGE_SIZE );Looks like it deserves own helper. Something like
Quoted 5 lines
Quoted 1 line
pte = pte_next(pte);Maybe, but then it needs to be added to each arch, which I was trying to avoid. That said, I had another idea which I like better; more patches coming soon.
Generic pte_next() that does the "+ PAGE_SIZE" trick should be fine. Arch code can override it if bit placement is different, instead of rewriting whole set_ptes()'
Quoted 14 lines
Here's a sneak preview:
+++
b/arch/x86/include/asm/pgtable.h(quoted diff) +2
-3@@ -1019,13 +1019,22 @@ static inline pud_t native_local_pudp_get_and_clear(pud_ t *pudp) return res; } -static inline void set_pte_at(struct mm_struct *mm, unsigned long addr, - pte_t *ptep, pte_t pte) +static inline void set_ptes(struct mm_struct *mm, unsigned long addr, + pte_t *ptep, pte_t pte, unsigned int nr) { - page_table_check_ptes_set(mm, addr, ptep, pte, 1);
Review static inline pud_t native_local_pudp_get_and_clear(pud_ · line 1034
Quoted 10 lines
(quoted diff) +9
-1- set_pte(ptep, pte); + page_table_check_ptes_set(mm, addr, ptep, pte, nr); + + for (;;) { + set_pte(ptep, pte); + if (--nr == 0) + break; + ptep++; + pte = __pte(pte_val(pte) + PAGE_SIZE); + }
for(; nr; nr--, ptep++, pte = pte_next(pte)) set_pte(ptep, pte);
Hm?
Quoted 10 lines
(quoted diff) +2
-0} +#define set_pte_at(mm, addr, ptep, pte) set_ptes(mm, addr, ptep, pte, 1) + static inline void set_pmd_at(struct mm_struct *mm, unsigned long addr, pmd_t *pmdp, pmd_t pmd){
Signature · 1 line
Kiryl Shutsemau / Kirill A. Shutemov
Matthew Wilcox (Oracle)
maintainerPATCH v5 3/5 rmap: add folio_add_file_rmap_range()
In reply to Matthew Wilcox (Oracle)From: Yin Fengwei <[email protected]>
folio_add_file_rmap_range() allows to add pte mapping to a specific range of file folio. Comparing to page_add_file_rmap(), it batched updates __lruvec_stat for large folio.
--- include/linux/rmap.h | 2 ++ mm/rmap.c | 60 +++++++++++++++++++++++++++++++++----------- 2 files changed, 48 insertions(+), 14 deletions(-)
include/linux/rmap.h +2 -0
@@ -198,6 +198,8 @@ void folio_add_new_anon_rmap(struct folio *, struct vm_area_struct *, unsigned long address);void page_add_file_rmap (struct page *,struct vm_area_struct *,bool compound); +void folio_add_file_rmap_range (struct folio *,struct page *,unsigned int nr, +struct vm_area_struct *,bool compound);void page_remove_rmap (struct page *,struct vm_area_struct *,bool compound);
mm/rmap.c +46 -14
83 lines not on this page · raw message
Signature · 1 line
2.35.1
Matthew Wilcox (Oracle)
maintainerPATCH v5 4/5 mm: Convert do_set_pte() to set_pte_range()
In reply to Matthew Wilcox (Oracle)From: Yin Fengwei <[email protected]>
set_pte_range() allows to setup page table entries for a specific range. It takes advantage of batched rmap update for large folio. It now takes care of calling update_mmu_cache().
--- Documentation/filesystems/locking.rst | 2 +- include/linux/mm.h | 3 ++- mm/filemap.c | 3 +-- mm/memory.c | 31 ++++++++++++++++----------- 4 files changed, 23 insertions(+), 16 deletions(-)
Documentation/filesystems/locking.rst +1 -1
@@ -663,7 +663,7 @@ locked. The VM will unlock the page. Filesystem should find and map pages associated with offsets from "start_pgoff" till "end_pgoff". ->map_pages() is called with page table locked and must not block. If it's not possible to reach a page without blocking, -filesystem should skip it. Filesystem should use do_set_pte() to setup +filesystem should skip it. Filesystem should use set_pte_range() to setup page table entry. Pointer to entry associated with the page is passed in "pte" field in vm_fault structure. Pointers to entries for other offsets should be calculated relative to "pte".
include/linux/mm.h +2 -1
@@ -1161,7 +1161,8 @@ static inline pte_t maybe_mkwrite(pte_t pte, struct vm_area_struct *vma) }vm_fault_t do_set_pmd (struct vm_fault *vmf,struct page *page); -void do_set_pte (struct vm_fault *vmf,struct page *page,unsigned long addr); +void set_pte_range (struct vm_fault *vmf,struct folio *folio, +struct page *page,unsigned int nr,unsigned long addr);vm_fault_t finish_fault (struct vm_fault *vmf);vm_fault_t finish_mkwrite_fault (struct vm_fault *vmf);
mm/filemap.c +1 -2
@@ -3375,8 +3375,7 @@ static vm_fault_t filemap_map_folio_range(struct vm_fault *vmf, ret =VM_FAULT_NOPAGE ; ref_count++; -do_set_pte (vmf, page, addr); -update_mmu_cache (vma, addr, vmf->pte); +set_pte_range (vmf, folio, page,1 , addr); }while (vmf->pte++, page++, addr +=PAGE_SIZE , ++count < nr_pages);/* Restore the vmf->pte */
mm/memory.c +19 -12
@@ -4257,15 +4257,18 @@ vm_fault_t do_set_pmd(struct vm_fault *vmf, struct page *page) }#endif -void do_set_pte (struct vm_fault *vmf,struct page *page,unsigned long addr) +void set_pte_range (struct vm_fault *vmf,struct folio *folio, +struct page *page,unsigned int nr,unsigned long addr) {struct vm_area_struct *vma = vmf->vma;bool uffd_wp =pte_marker_uffd_wp (vmf->orig_pte);bool write = vmf->flags &FAULT_FLAG_WRITE ;bool prefault = vmf->address != addr;pte_t entry; +unsigned int i; -flush_icache_page (vma, page); +for (i =0 ; i < nr; i++) +flush_icache_page (vma, page + i); entry =mk_pte (page, vma->vm_page_prot);if (prefault &&arch_wants_old_prefaulted_pte ())
@@ -4279,14 +4282,20 @@ void do_set_pte(struct vm_fault *vmf, struct page *page, unsigned long addr) entry =pte_mkuffd_wp (entry);/* copy-on-write page */ if (write && !(vma->vm_flags &VM_SHARED )) { -inc_mm_counter (vma->vm_mm,MM_ANONPAGES ); -page_add_new_anon_rmap (page, vma, addr); -lru_cache_add_inactive_or_unevictable (page, vma); +add_mm_counter (vma->vm_mm,MM_ANONPAGES , nr); +VM_BUG_ON_FOLIO (nr !=1 , folio); +folio_add_new_anon_rmap (folio, vma, addr); +folio_add_lru_vma (folio, vma); }else { -inc_mm_counter (vma->vm_mm,mm_counter_file (page)); -page_add_file_rmap (page, vma, false); +add_mm_counter (vma->vm_mm,mm_counter_file (page), nr); +folio_add_file_rmap_range (folio, page, nr, vma, false); } -set_pte_at (vma->vm_mm, addr, vmf->pte, entry); +set_ptes (vma->vm_mm, addr, vmf->pte, entry, nr); + +/* no need to invalidate: a not-present page won't be cached */ +for (i =0 ; i < nr; i++) +update_mmu_cache (vma, addr + i *PAGE_SIZE , vmf->pte + i); + }static bool vmf_pte_changed (struct vm_fault *vmf)
@@ -4359,11 +4368,9 @@ vm_fault_t finish_fault(struct vm_fault *vmf)/* Re-check under ptl */ if (likely (!vmf_pte_changed (vmf))) { -do_set_pte (vmf, page, vmf->address); - -/* no need to invalidate: a not-present page won't be cached */ -update_mmu_cache (vma, vmf->address, vmf->pte); +struct folio *folio =page_folio (page); +set_pte_range (vmf, folio, page,1 , vmf->address); ret =0 ; }else {update_mmu_tlb (vma, vmf->address, vmf->pte);
Signature · 1 line
2.35.1
Matthew Wilcox (Oracle)
maintainerPATCH v5 5/5 filemap: Batch PTE mappings
In reply to Matthew Wilcox (Oracle)From: Yin Fengwei <[email protected]>
Call set_pte_range() once per contiguous range of the folio instead of once per page. This batches the updates to mm counters and the rmap.
With a will-it-scale.page_fault3 like app (change file write fault testing to read fault testing. Trying to upstream it to will-it-scale at [1]) got 15% performance gain on a 48C/96T Cascade Lake test box with 96 processes running against xfs.
Perf data collected before/after the change:
18.73%--page_add_file_rmap
|
--11.60%--__mod_lruvec_page_state
|
|--7.40%--__mod_memcg_lruvec_state
| |
| --5.58%--cgroup_rstat_updated
|
--2.53%--__mod_lruvec_state
|
--1.48%--__mod_node_page_state9.93%--page_add_file_rmap_range
|
--2.67%--__mod_lruvec_page_state
|
|--1.95%--__mod_memcg_lruvec_state
| |
| --1.57%--cgroup_rstat_updated
|
--0.61%--__mod_lruvec_state
|
--0.54%--__mod_node_page_stateThe running time of __mode_lruvec_page_state() is reduced about 9%.
[1]: https://github.com/antonblanchard/will-it-scale/pull/37
--- mm/filemap.c | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-)
mm/filemap.c +25 -11
@@ -3354,11 +3354,12 @@ static vm_fault_t filemap_map_folio_range(struct vm_fault *vmf,struct file *file = vma->vm_file;struct page *page =folio_page (folio, start);unsigned int mmap_miss =READ_ONCE (file->f_ra.mmap_miss); -unsigned int ref_count =0 , count =0 ; +unsigned int count =0 ; +pte_t *old_ptep = vmf->pte; do { - if (PageHWPoison(page)) - continue; + if (PageHWPoison(page + count)) + goto skip; if (mmap_miss > 0) mmap_miss--;
@@ -3368,20 +3369,33 @@ static vm_fault_t filemap_map_folio_range(struct vm_fault *vmf,* handled in the specific fault path, and it'll prohibit the * fault-around logic. */ -if (!pte_none (*vmf->pte)) -continue ; +if (!pte_none (vmf->pte[count])) + goto skip;if (vmf->address == addr) ret =VM_FAULT_NOPAGE ; - ref_count++; -set_pte_range (vmf, folio, page,1 , addr); - }while (vmf->pte++, page++, addr +=PAGE_SIZE , ++count < nr_pages); + count++; +continue ; +skip: +if (count) { +set_pte_range (vmf, folio, page, count, addr); +folio_ref_add (folio, count); + } -/* Restore the vmf->pte */ - vmf->pte -= nr_pages; + count++; + page += count; + vmf->pte += count; + addr += count *PAGE_SIZE ; + count =0 ; + }while (--nr_pages >0 ); + +if (count) { +set_pte_range (vmf, folio, page, count, addr); +folio_ref_add (folio, count); + } -folio_ref_add (folio, ref_count); + vmf->pte = old_ptep;WRITE_ONCE (file->f_ra.mmap_miss, mmap_miss);return ret;
Signature · 1 line
2.35.1