Convert page cache to XArray
Subsystems (28)
Matthew Wilcox
maintainerv14 cover Convert page cache to XArray
The XArray is a replacement for the radix tree. For the moment it uses the same data structures, enabling a gradual replacement. This patch set implements the XArray and converts the page cache to use it.
A version of these patches has been running under xfstests for over 48 hours, so I have some confidence in them. The DAX changes have now also had a reasonable test outing. This is based on next-20180615 and is available as a git tree atgit://git.infradead.org/users/willy/linux-dax.git xarray-20180615
I shall create a git branch from -rc1 and ask for that to be included in -next. I'm a little concerned I still have no reviews on some of the later patches.
Changes since v13:
- Actually fixed bug in workingset conversion that led to exceptional entries not being deleted from the XArray. Not sure how I dropped that patch for v13. Thanks to David Sterba for noticing.
- Fixed bug in DAX writeback conversion that failed to wake up waiters. Thanks to Ross for testing, and to Dan & Jeff for helping me get a setup working to reproduce the problem.
- Converted the new dax_lock_page / dax_unlock_page functions.
- Moved XArray test suite entirely into the test_xarray kernel module to match other test suites. It can still be built in userspace as part of the radix tree test suite.
- Changed email address.
- Moved a few functions into different patches to make the test-suite additions more logical.
- Fixed a bug in XA_BUG_ON (oh the irony) where it evaluated the condition twice.
- Constified xa_head() / xa_parent() / xa_entry() and their _locked variants.
- Moved xa_parent() to xarray.h so it can be used from the workingset code.
- Call the xarray testsuite from the radix tree test suite to ensure that I remember to run both test suites ;-)
- Added some more tests to the test suite.
Matthew Wilcox (74):
Update email address
radix tree test suite: Enable ubsan
dax: Fix use of zero page
xarray: Replace exceptional entries
xarray: Change definition of sibling entries
xarray: Add definition of struct xarray
xarray: Define struct xa_node
xarray: Add documentation
xarray: Add XArray load operation
xarray: Add XArray tags
xarray: Add XArray unconditional store operations
xarray: Add XArray conditional store operations
xarray: Add XArray iterators
xarray: Extract entries from an XArray
xarray: Destroy an XArray
xarray: Step through an XArray
xarray: Add xas_for_each_conflict
xarray: Add xas_create_range
xarray: Add MAINTAINERS entry
page cache: Rearrange address_space
page cache: Convert hole search to XArray page cache: Add and replace pages using the XArray page cache: Convert page deletion to XArray page cache: Convert find_get_entry to XArray page cache: Convert find_get_entries to XArray page cache: Convert find_get_pages_range to XArray page cache: Convert find_get_pages_contig to XArray page cache; Convert find_get_pages_range_tag to XArray page cache: Convert find_get_entries_tag to XArray page cache: Convert filemap_map_pages to XArray radix tree test suite: Convert regression1 to XArray page cache: Convert delete_batch to XArray page cache: Remove stray radix comment
page cache: Convert filemap_range_has_page to XArray
mm: Convert page-writeback to XArray
mm: Convert workingset to XArray
mm: Convert truncate to XArray
mm: Convert add_to_swap_cache to XArray
mm: Convert delete_from_swap_cache to XArray
mm: Convert __do_page_cache_readahead to XArray
mm: Convert page migration to XArray
mm: Convert huge_memory to XArray
mm: Convert collapse_shmem to XArray
mm: Convert khugepaged_scan_shmem to XArray
mm: Convert is_page_cache_freeable to XArray
pagevec: Use xa_tag_t
shmem: Convert shmem_radix_tree_replace to XArray
shmem: Convert shmem_confirm_swap to XArray
shmem: Convert find_swap_entry to XArray
shmem: Convert shmem_add_to_page_cache to XArray
shmem: Convert shmem_alloc_hugepage to XArray
shmem: Convert shmem_free_swap to XArray
shmem: Convert shmem_partial_swap_usage to XArray
memfd: Convert memfd_wait_for_pins to XArray
memfd: Convert memfd_tag_pins to XArray
shmem: Comment fixups
btrfs: Convert page cache to XArray
fs: Convert buffer to XArray
fs: Convert writeback to XArray
nilfs2: Convert to XArray
f2fs: Convert to XArray
dax: Rename some functions
dax: Hash on XArray instead of mapping
dax: Convert dax_insert_pfn_mkwrite to XArray
dax: Convert dax_layout_busy_page to XArray
dax: Convert __dax_invalidate_entry to XArray
dax: Convert dax writeback to XArray
dax: Convert dax_lock_page to XArray
dax: Convert page fault handlers to XArray page cache: Finish XArray conversion
radix tree: Remove radix_tree_update_node_t radix tree: Remove split/join code
radix tree: Remove radix_tree_maybe_preload_order radix tree: Remove radix_tree_clear_tags
.clang-format | 1 - .mailmap | 7 + Documentation/core-api/index.rst | 1 + Documentation/core-api/xarray.rst | 395 ++++ MAINTAINERS | 17 +- arch/powerpc/include/asm/book3s/64/pgtable.h | 4 +- arch/powerpc/include/asm/nohash/64/pgtable.h | 4 +- drivers/gpu/drm/i915/i915_gem.c | 17 +- fs/btrfs/compression.c | 6 +- fs/btrfs/extent_io.c | 12 +- fs/buffer.c | 14 +- fs/dax.c | 878 ++++----- fs/ext4/inode.c | 2 +- fs/f2fs/data.c | 6 +- fs/f2fs/dir.c | 2 +- fs/f2fs/f2fs.h | 2 +- fs/f2fs/inline.c | 2 +- fs/f2fs/node.c | 6 +- fs/fs-writeback.c | 25 +- fs/gfs2/aops.c | 2 +- fs/inode.c | 2 +- fs/nfs/blocklayout/blocklayout.c | 2 +- fs/nilfs2/btnode.c | 26 +- fs/nilfs2/page.c | 29 +- fs/proc/task_mmu.c | 2 +- include/linux/fs.h | 63 +- include/linux/pagemap.h | 10 +- include/linux/pagevec.h | 8 +- include/linux/radix-tree.h | 136 +- include/linux/swap.h | 22 +- include/linux/swapops.h | 19 +- include/linux/xarray.h | 1047 +++++++++- lib/Kconfig.debug | 3 + lib/Makefile | 3 +- lib/idr.c | 66 +- lib/radix-tree.c | 575 +----- lib/test_xarray.c | 676 +++++++ lib/xarray.c | 1753 +++++++++++++++++ mm/filemap.c | 723 +++---- mm/huge_memory.c | 17 +- mm/khugepaged.c | 177 +- mm/madvise.c | 2 +- mm/memcontrol.c | 2 +- mm/memfd.c | 105 +- mm/migrate.c | 48 +- mm/mincore.c | 2 +- mm/page-writeback.c | 72 +- mm/readahead.c | 10 +- mm/shmem.c | 201 +- mm/swap.c | 6 +- mm/swap_state.c | 119 +- mm/truncate.c | 27 +- mm/vmscan.c | 10 +- mm/workingset.c | 69 +- tools/include/asm-generic/bitops.h | 1 + tools/include/asm-generic/bitops/atomic.h | 9 - tools/include/asm-generic/bitops/non-atomic.h | 109 + tools/include/linux/bitmap.h | 1 + tools/include/linux/kernel.h | 1 + tools/include/linux/spinlock.h | 12 +- tools/testing/radix-tree/.gitignore | 1 + tools/testing/radix-tree/Makefile | 16 +- tools/testing/radix-tree/benchmark.c | 91 - tools/testing/radix-tree/bitmap.c | 23 + tools/testing/radix-tree/idr-test.c | 6 +- tools/testing/radix-tree/linux/bug.h | 1 + tools/testing/radix-tree/linux/kconfig.h | 1 + tools/testing/radix-tree/linux/kernel.h | 5 + tools/testing/radix-tree/linux/lockdep.h | 11 + tools/testing/radix-tree/linux/radix-tree.h | 1 - tools/testing/radix-tree/linux/rcupdate.h | 2 + tools/testing/radix-tree/linux/xarray.h | 2 + tools/testing/radix-tree/main.c | 21 +- tools/testing/radix-tree/multiorder.c | 272 +-- tools/testing/radix-tree/regression1.c | 58 +- tools/testing/radix-tree/regression3.c | 23 - tools/testing/radix-tree/tag_check.c | 29 - tools/testing/radix-tree/test.c | 8 +- tools/testing/radix-tree/test.h | 1 + tools/testing/radix-tree/xarray.c | 33 + 80 files changed, 5462 insertions(+), 2711 deletions(-) create mode 100644 Documentation/core-api/xarray.rst create mode 100644 lib/test_xarray.c create mode 100644 lib/xarray.c create mode 100644 tools/include/asm-generic/bitops/non-atomic.h create mode 100644 tools/testing/radix-tree/bitmap.c create mode 100644 tools/testing/radix-tree/linux/kconfig.h create mode 100644 tools/testing/radix-tree/linux/lockdep.h create mode 100644 tools/testing/radix-tree/linux/xarray.h create mode 100644 tools/testing/radix-tree/xarray.c
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 1/74 Update email address
Redirect some older email addresses that are in the git logs.
--- .mailmap | 7 +++++++ MAINTAINERS | 6 +++--- include/linux/xarray.h | 2 +- 3 files changed, 11 insertions(+), 4 deletions(-)
.mailmap +7 -0
@@ -114,6 +114,13 @@ Mark Brown <[email protected]> Mark Yao <[email protected]> <[email protected]> Martin Kepplinger <[email protected]> <[email protected]> Martin Kepplinger <[email protected]> <[email protected]> +Matthew Wilcox <[email protected]> <[email protected]> +Matthew Wilcox <[email protected]> <[email protected]> +Matthew Wilcox <[email protected]> <[email protected]> +Matthew Wilcox <[email protected]> <[email protected]> +Matthew Wilcox <[email protected]> <[email protected]> +Matthew Wilcox <[email protected]> <[email protected]> +Matthew Wilcox <[email protected]> <[email protected]> Matthieu CASTET <[email protected]> Mauro Carvalho Chehab <[email protected]> <[email protected]> Mauro Carvalho Chehab <[email protected]> <[email protected]>
MAINTAINERS +3 -3
@@ -529,7 +529,7 @@ F: Documentation/hwmon/adt7475 F: drivers/hwmon/adt7475.c ADVANSYS SCSI DRIVER -M: Matthew Wilcox <[email protected]> +M: Matthew Wilcox <[email protected]> M: Hannes Reinecke <[email protected]> L: [email protected] S: Maintained
@@ -4268,7 +4268,7 @@ S: Maintained F: drivers/i2c/busses/i2c-diolan-u2c.c FILESYSTEM DIRECT ACCESS (DAX) -M: Matthew Wilcox <[email protected]> +M: Matthew Wilcox <[email protected]> M: Ross Zwisler <[email protected]> L: [email protected] S: Supported
@@ -8452,7 +8452,7 @@ F: drivers/message/fusion/ F: drivers/scsi/mpt3sas/ LSILOGIC/SYMBIOS/NCR 53C8XX and 53C1010 PCI-SCSI drivers -M: Matthew Wilcox <[email protected]> +M: Matthew Wilcox <[email protected]> L: [email protected] S: Maintained F: drivers/scsi/sym53c8xx_2/
include/linux/xarray.h +1 -1
@@ -4,7 +4,7 @@/* * eXtensible Arrays * Copyright (c) 2017 Microsoft Corporation -* Author: Matthew Wilcox <[email protected]> +* Author: Matthew Wilcox <[email protected]> */ #include <linux/spinlock.h>
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 2/74 radix tree test suite: Enable ubsan
In reply to Matthew WilcoxAdd support for the undefined behaviour sanitizer and fix the bugs that ubsan pointed out. Nothing major, and all in the test suite, not the code.
--- tools/testing/radix-tree/Makefile | 5 +++-- tools/testing/radix-tree/main.c | 20 +++++++++++--------- 2 files changed, 14 insertions(+), 11 deletions(-)
tools/testing/radix-tree/Makefile +3 -2
@@ -1,7 +1,8 @@ # SPDX-License-Identifier: GPL-2.0 -CFLAGS += -I. -I../../include -g -O2 -Wall -D_LGPL_SOURCE -fsanitize=address -LDFLAGS += -fsanitize=address +CFLAGS += -I. -I../../include -g -Og -Wall -D_LGPL_SOURCE -fsanitize=address \ + -fsanitize=undefined +LDFLAGS += -fsanitize=address -fsanitize=undefined LDLIBS+= -lpthread -lurcu TARGETS = main idr-test multiorder CORE_OFILES := radix-tree.o idr.o linux.o test.o find_bit.o
tools/testing/radix-tree/main.c +11 -9
@@ -27,20 +27,22 @@ void __gang_check(unsigned long middle, long down, long up, int chunk, int hop)item_check_present (&tree, middle + idx);item_check_absent (&tree, middle + up); -item_gang_check_present (&tree, middle - down, - up + down, chunk, hop); -item_full_scan (&tree, middle - down, down + up, chunk); +if (chunk >0 ) { +item_gang_check_present (&tree, middle - down, up + down, + chunk, hop); +item_full_scan (&tree, middle - down, down + up, chunk); + }item_kill_tree (&tree); }void gang_check (void ) { -__gang_check (1 <<30 ,128 ,128 ,35 ,2 ); -__gang_check (1 <<31 ,128 ,128 ,32 ,32 ); -__gang_check (1 <<31 ,128 ,128 ,32 ,100 ); -__gang_check (1 <<31 ,128 ,128 ,17 ,7 ); -__gang_check (0xffff0000 ,0 ,65536 ,17 ,7 ); -__gang_check (0xfffffffe ,1 ,1 ,17 ,7 ); +__gang_check (1UL <<30 ,128 ,128 ,35 ,2 ); +__gang_check (1UL <<31 ,128 ,128 ,32 ,32 ); +__gang_check (1UL <<31 ,128 ,128 ,32 ,100 ); +__gang_check (1UL <<31 ,128 ,128 ,17 ,7 ); +__gang_check (0xffff0000UL ,0 ,65536 ,17 ,7 ); +__gang_check (0xfffffffeUL ,1 ,1 ,17 ,7 ); }void __big_gang_check (void )
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 3/74 dax: Fix use of zero page
In reply to Matthew WilcoxUse my_zero_pfn instead of ZERO_PAGE, and pass the vaddr to it so it works on MIPS and s390.
--- fs/dax.c | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-)
fs/dax.c +2 -11
@@ -1093,21 +1093,12 @@ static vm_fault_t dax_load_hole(struct address_space *mapping, void *entry, {struct inode *inode = mapping->host;unsigned long vaddr = vmf->address; -vm_fault_t ret =VM_FAULT_NOPAGE ; -struct page *zero_page; -pfn_t pfn; - - zero_page =ZERO_PAGE (0 ); -if (unlikely (!zero_page)) { - ret =VM_FAULT_OOM ; - goto out; - } +pfn_t pfn =pfn_to_pfn_t (my_zero_pfn (vaddr)); +vm_fault_t ret; - pfn =page_to_pfn_t (zero_page);dax_insert_mapping_entry (mapping, vmf, entry, pfn,RADIX_DAX_ZERO_PAGE , false); ret =vmf_insert_mixed (vmf->vma, vaddr, pfn); -out:trace_dax_load_hole (inode, vmf, ret);return ret; }
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 4/74 xarray: Replace exceptional entries
In reply to Matthew WilcoxIntroduce xarray value entries to replace the radix tree exceptional entry code. This is a slight change in encoding to allow the use of an extra bit (we can now store BITS_PER_LONG - 1 bits in a value entry). It is also a change in emphasis; exceptional entries are intimidating and different. As the comment explains, you can choose to store values or pointers in the xarray and they are both first-class citizens.
--- arch/powerpc/include/asm/book3s/64/pgtable.h | 4 +- arch/powerpc/include/asm/nohash/64/pgtable.h | 4 +- drivers/gpu/drm/i915/i915_gem.c | 17 ++- fs/btrfs/compression.c | 2 +- fs/dax.c | 114 +++++++++---------- fs/proc/task_mmu.c | 2 +- include/linux/radix-tree.h | 36 ++---- include/linux/swapops.h | 19 ++-- include/linux/xarray.h | 54 +++++++++ lib/idr.c | 60 ++++------ lib/radix-tree.c | 21 ++-- mm/filemap.c | 10 +- mm/khugepaged.c | 2 +- mm/madvise.c | 2 +- mm/memcontrol.c | 2 +- mm/mincore.c | 2 +- mm/readahead.c | 2 +- mm/shmem.c | 10 +- mm/swap.c | 2 +- mm/truncate.c | 12 +- mm/workingset.c | 12 +- tools/testing/radix-tree/idr-test.c | 6 +- tools/testing/radix-tree/linux/radix-tree.h | 1 - tools/testing/radix-tree/linux/xarray.h | 2 + tools/testing/radix-tree/multiorder.c | 47 ++++---- tools/testing/radix-tree/test.c | 2 +- 26 files changed, 226 insertions(+), 221 deletions(-) create mode 100644 tools/testing/radix-tree/linux/xarray.h
arch/powerpc/include/asm/book3s/64/pgtable.h +1 -3
@@ -703,9 +703,7 @@ static inline bool pte_user(pte_t pte)BUILD_BUG_ON (_PAGE_HPTEFLAGS & (0x1f << _PAGE_BIT_SWAP_TYPE)); \BUILD_BUG_ON (_PAGE_HPTEFLAGS & _PAGE_SWP_SOFT_DIRTY); \ } while (0) -/* -* on pte we don't need handle RADIX_TREE_EXCEPTIONAL_SHIFT; - */ +#define SWP_TYPE_BITS 5#define __swp_type (x) (((x).val >> _PAGE_BIT_SWAP_TYPE) \ & ((1UL << SWP_TYPE_BITS) - 1))
arch/powerpc/include/asm/nohash/64/pgtable.h +1 -3
@@ -322,9 +322,7 @@ static inline void __ptep_set_access_flags(struct vm_area_struct *vma,#define MAX_SWAPFILES_CHECK () do { \ BUILD_BUG_ON(MAX_SWAPFILES_SHIFT > SWP_TYPE_BITS); \ } while (0) -/* -* on pte we don't need handle RADIX_TREE_EXCEPTIONAL_SHIFT; -*/ +#define SWP_TYPE_BITS 5#define __swp_type (x) (((x).val >> _PAGE_BIT_SWAP_TYPE) \ & ((1UL << SWP_TYPE_BITS) - 1))
drivers/gpu/drm/i915/i915_gem.c +7 -10
@@ -5865,7 +5865,8 @@ i915_gem_object_get_sg(struct drm_i915_gem_object *obj, count =__sg_page_count (sg);while (idx + count <= n) { -unsigned long exception, i; +void *entry; +unsigned long i;int ret;/* If we cannot allocate and insert this entry, or the
@@ -5880,12 +5881,9 @@ i915_gem_object_get_sg(struct drm_i915_gem_object *obj,if (ret && ret != -EEXIST ) goto scan; - exception = -RADIX_TREE_EXCEPTIONAL_ENTRY | - idx <<RADIX_TREE_EXCEPTIONAL_SHIFT ; + entry =xa_mk_value (idx);for (i =1 ; i < count; i++) { - ret =radix_tree_insert (&iter->radix, idx + i, - (void *)exception); + ret =radix_tree_insert (&iter->radix, idx + i, entry);if (ret && ret != -EEXIST ) goto scan; }
@@ -5923,15 +5921,14 @@ i915_gem_object_get_sg(struct drm_i915_gem_object *obj,GEM_BUG_ON (!sg);/* If this index is in the middle of multi-page sg entry, -* the radixtree will contain an exceptional entry that points +* the radix tree will contain a value entry that points * to the start of that range. We will return the pointer to * the base page and the offset of this page within the * sg entry's range. */ *offset =0 ; -if (unlikely (radix_tree_exception (sg))) { -unsigned long base = - (unsigned long )sg >>RADIX_TREE_EXCEPTIONAL_SHIFT ; +if (unlikely (xa_is_value (sg))) { +unsigned long base =xa_to_value (sg); sg =radix_tree_lookup (&iter->radix, base);GEM_BUG_ON (!sg);
fs/btrfs/compression.c +1 -1
@@ -447,7 +447,7 @@ static noinline int add_ra_bio_pages(struct inode *inode,rcu_read_lock (); page =radix_tree_lookup (&mapping->i_pages, pg_index);rcu_read_unlock (); -if (page && !radix_tree_exceptional_entry (page)) { +if (page && !xa_is_value (page)) { misses++;if (misses >4 )break ;
fs/dax.c +56 -58
289 lines not on this page · raw message
fs/proc/task_mmu.c +1 -1
@@ -553,7 +553,7 @@ static void smaps_pte_entry(pte_t *pte, unsigned long addr,if (!page)return ; -if (radix_tree_exceptional_entry (page)) +if (xa_is_value (page)) mss->swap +=PAGE_SIZE ;else put_page (page);
include/linux/radix-tree.h +8 -28
@@ -28,34 +28,26 @@#include <linux/rcupdate.h> #include <linux/spinlock.h> #include <linux/types.h> +#include <linux/xarray.h> /* * The bottom two bits of the slot determine how the remaining bits in the * slot are interpreted: * * 00 - data pointer -* 01 - internal entry -* 10 - exceptional entry -* 11 - this bit combination is currently unused/reserved +* 10 - internal entry +* x1 - value entry * * The internal entry may be a pointer to the next level in the tree, a * sibling entry, or an indicator that the entry in this slot has been moved * to another location in the tree and the lookup should be restarted. While * NULL fits the 'data pointer' pattern, it means that there is no entry in * the tree for this index (no matter what level of the tree it is found at). -* This means that you cannot store NULL in the tree as a value for the index. +* This means that storing a NULL entry in the tree is the same as deleting +* the entry from the tree. */ #define RADIX_TREE_ENTRY_MASK 3UL -#define RADIX_TREE_INTERNAL_NODE 1UL - -/* -* Most users of the radix tree store pointers but shmem/tmpfs stores swap -* entries in the same tree. They are marked as exceptional entries to -* distinguish them from pointers to struct page. -* EXCEPTIONAL_ENTRY tests the bit, EXCEPTIONAL_SHIFT shifts content past it. -*/ -#define RADIX_TREE_EXCEPTIONAL_ENTRY 2 -#define RADIX_TREE_EXCEPTIONAL_SHIFT 2 +#define RADIX_TREE_INTERNAL_NODE 2ULstatic inline bool radix_tree_is_internal_node (void *ptr) {
@@ -83,11 +75,10 @@ static inline bool radix_tree_is_internal_node(void *ptr)/* * @count is the count of every non-NULL element in the ->slots array -* whether that is an exceptional entry, a retry entry, a user pointer, +* whether that is a data entry, a retry entry, a user pointer, * a sibling entry or a pointer to the next level of the tree. * @exceptional is the count of every element in ->slots which is -* either radix_tree_exceptional_entry() or is a sibling entry for an -* exceptional entry. +* either a data entry or a sibling entry for data. */ struct radix_tree_node {unsigned char shift;/* Bits remaining in each slot */
@@ -268,17 +259,6 @@ static inline int radix_tree_deref_retry(void *arg)return unlikely (radix_tree_is_internal_node (arg)); } -/** -* radix_tree_exceptional_entry - radix_tree_deref_slot gave exceptional entry? -* @arg: value returned by radix_tree_deref_slot -* Returns: 0 if well-aligned pointer, non-0 if exceptional entry. -*/ -static inline int radix_tree_exceptional_entry (void *arg) -{ -/* Not unlikely because radix_tree_exception often tested first */ -return (unsigned long )arg &RADIX_TREE_EXCEPTIONAL_ENTRY ; -} -/** * radix_tree_exception - radix_tree_deref_slot returned either exception? * @arg: value returned by radix_tree_deref_slot
include/linux/swapops.h +7 -12
@@ -17,9 +17,8 @@* * swp_entry_t's are *never* stored anywhere in their arch-dependent format. */ -#define SWP_TYPE_SHIFT (e) ((sizeof(e.val) * 8) - \ - (MAX_SWAPFILES_SHIFT + RADIX_TREE_EXCEPTIONAL_SHIFT)) -#define SWP_OFFSET_MASK (e) ((1UL << SWP_TYPE_SHIFT(e)) - 1) +#define SWP_TYPE_SHIFT (BITS_PER_XA_VALUE - MAX_SWAPFILES_SHIFT) +#define SWP_OFFSET_MASK ((1UL << SWP_TYPE_SHIFT) - 1)/* * Store a type+offset into a swp_entry_t in an arch-independent format
@@ -28,8 +27,7 @@ static inline swp_entry_t swp_entry(unsigned long type, pgoff_t offset) {swp_entry_t ret; - ret.val = (type <<SWP_TYPE_SHIFT (ret)) | - (offset &SWP_OFFSET_MASK (ret)); + ret.val = (type <<SWP_TYPE_SHIFT ) | (offset &SWP_OFFSET_MASK );return ret; }
@@ -39,7 +37,7 @@ static inline swp_entry_t swp_entry(unsigned long type, pgoff_t offset)*/ static inline unsigned swp_type (swp_entry_t entry) { -return (entry.val >>SWP_TYPE_SHIFT (entry)); +return (entry.val >>SWP_TYPE_SHIFT ); }/*
@@ -48,7 +46,7 @@ static inline unsigned swp_type(swp_entry_t entry)*/ static inline pgoff_t swp_offset (swp_entry_t entry) { -return entry.val &SWP_OFFSET_MASK (entry); +return entry.val &SWP_OFFSET_MASK ; }#ifdef CONFIG_MMU
@@ -89,16 +87,13 @@ static inline swp_entry_t radix_to_swp_entry(void *arg) {swp_entry_t entry; - entry.val = (unsigned long )arg >>RADIX_TREE_EXCEPTIONAL_SHIFT ; + entry.val =xa_to_value (arg);return entry; }static inline void *swp_to_radix_entry (swp_entry_t entry) { -unsigned long value; - - value = entry.val <<RADIX_TREE_EXCEPTIONAL_SHIFT ; -return (void *)(value |RADIX_TREE_EXCEPTIONAL_ENTRY ); +return xa_mk_value (entry.val); }#if IS_ENABLED (CONFIG_DEVICE_PRIVATE )
include/linux/xarray.h +54 -0
63 lines not on this page · raw message
lib/idr.c +25 -35
116 lines not on this page · raw message
lib/radix-tree.c +9 -12
@@ -340,14 +340,12 @@ static void dump_ida_node(void *entry, unsigned long index)for (i =0 ; i <RADIX_TREE_MAP_SIZE ; i++)dump_ida_node (node->slots[i], index | (i << node->shift)); - }else if (radix_tree_exceptional_entry (entry)) { + }else if (xa_is_value (entry)) {pr_debug ("ida excp: %p offset %d indices %lu-%lu data %lx\n" , entry, (int )(index &RADIX_TREE_MAP_MASK ), index *IDA_BITMAP_BITS , - index *IDA_BITMAP_BITS +BITS_PER_LONG - -RADIX_TREE_EXCEPTIONAL_SHIFT , - (unsigned long )entry >> -RADIX_TREE_EXCEPTIONAL_SHIFT ); + index *IDA_BITMAP_BITS +BITS_PER_XA_VALUE , +xa_to_value (entry)); }else {struct ida_bitmap *bitmap = entry;
@@ -656,7 +654,7 @@ static int radix_tree_extend(struct radix_tree_root *root, gfp_t gfp,BUG_ON (shift >BITS_PER_LONG );if (radix_tree_is_internal_node (entry)) {entry_to_node (entry)->parent = node; - }else if (radix_tree_exceptional_entry (entry)) { + }else if (xa_is_value (entry)) {/* Moving an exceptional root->rnode to a node */ node->exceptional =1 ; }
@@ -947,12 +945,12 @@ static inline int insert_entries(struct radix_tree_node *node, !is_sibling_entry(node, old) && (old != RADIX_TREE_RETRY))radix_tree_free_nodes (old); -if (radix_tree_exceptional_entry (old)) +if (xa_is_value (old)) node->exceptional--; }if (node) { node->count += n; -if (radix_tree_exceptional_entry (item)) +if (xa_is_value (item)) node->exceptional += n; }return n;
@@ -966,7 +964,7 @@ static inline int insert_entries(struct radix_tree_node *node,rcu_assign_pointer (*slot, item);if (node) { node->count++; -if (radix_tree_exceptional_entry (item)) +if (xa_is_value (item)) node->exceptional++; }return 1 ;
@@ -1183,8 +1181,7 @@ void __radix_tree_replace(struct radix_tree_root *root, radix_tree_update_node_t update_node) {void *old =rcu_dereference_raw (*slot); -int exceptional = !!radix_tree_exceptional_entry (item) - - !!radix_tree_exceptional_entry (old); +int exceptional = !!xa_is_value (item) - !!xa_is_value (old);int count =calculate_count (root, node, slot, item, old);/*
@@ -1985,7 +1982,7 @@ static bool __radix_tree_delete(struct radix_tree_root *root, struct radix_tree_node *node, void __rcu **slot) { void *old = rcu_dereference_raw(*slot); -int exceptional =radix_tree_exceptional_entry (old) ?-1 :0 ; +int exceptional =xa_is_value (old) ?-1 :0 ;unsigned offset =get_slot_offset (node, slot);int tag;
mm/filemap.c +5 -5
@@ -127,7 +127,7 @@ static int page_cache_tree_insert(struct address_space *mapping, p =radix_tree_deref_slot_protected (slot, &mapping->i_pages.xa_lock); -if (!radix_tree_exceptional_entry (p)) +if (!xa_is_value (p))return -EEXIST ; mapping->nrexceptional--;
@@ -336,7 +336,7 @@ page_cache_tree_delete_batch(struct address_space *mapping,break ; page =radix_tree_deref_slot_protected (slot, &mapping->i_pages.xa_lock); -if (radix_tree_exceptional_entry (page)) +if (xa_is_value (page))continue ;if (!tail_pages) {/*
@@ -1355,7 +1355,7 @@ pgoff_t page_cache_next_hole(struct address_space *mapping,struct page *page; page =radix_tree_lookup (&mapping->i_pages, index); -if (!page ||radix_tree_exceptional_entry (page)) +if (!page ||xa_is_value (page))break ; index++;if (index ==0 )
@@ -1396,7 +1396,7 @@ pgoff_t page_cache_prev_hole(struct address_space *mapping,struct page *page; page =radix_tree_lookup (&mapping->i_pages, index); -if (!page ||radix_tree_exceptional_entry (page)) +if (!page ||xa_is_value (page))break ; index--;if (index ==ULONG_MAX )
@@ -1539,7 +1539,7 @@ struct page *pagecache_get_page(struct address_space *mapping, pgoff_t offset, repeat: page =find_get_entry (mapping, offset); -if (radix_tree_exceptional_entry (page)) +if (xa_is_value (page)) page =NULL ;if (!page) goto no_page;
mm/khugepaged.c +1 -1
@@ -1368,7 +1368,7 @@ static void collapse_shmem(struct mm_struct *mm, page =radix_tree_deref_slot_protected (slot, &mapping->i_pages.xa_lock); -if (radix_tree_exceptional_entry (page) || !PageUptodate (page)) { +if (xa_is_value (page) || !PageUptodate (page)) {xa_unlock_irq (&mapping->i_pages);/* swap in or instantiate fallocated page */ if (shmem_getpage(mapping->host, index, &page,
mm/madvise.c +1 -1
@@ -251,7 +251,7 @@ static void force_shm_swapin_readahead(struct vm_area_struct *vma, index = ((start - vma->vm_start) >>PAGE_SHIFT ) + vma->vm_pgoff; page =find_get_entry (mapping, index); -if (!radix_tree_exceptional_entry (page)) { +if (!xa_is_value (page)) {if (page)put_page (page);continue ;
mm/memcontrol.c +1 -1
@@ -4682,7 +4682,7 @@ static struct page *mc_handle_file_pte(struct vm_area_struct *vma,/* shmem/tmpfs may report page out on swap: account for that too. */ if (shmem_mapping (mapping)) { page =find_get_entry (mapping, pgoff); -if (radix_tree_exceptional_entry (page)) { +if (xa_is_value (page)) {swp_entry_t swp =radix_to_swp_entry (page);if (do_memsw_account ()) *entry = swp;
mm/mincore.c +1 -1
@@ -66,7 +66,7 @@ static unsigned char mincore_page(struct address_space *mapping, pgoff_t pgoff)* shmem/tmpfs may return swap: account for swapcache * page too. */ -if (radix_tree_exceptional_entry (page)) { +if (xa_is_value (page)) {swp_entry_t swp =radix_to_swp_entry (page);struct swap_info_struct *si;
mm/readahead.c +1 -1
@@ -177,7 +177,7 @@ unsigned int __do_page_cache_readahead(struct address_space *mapping,rcu_read_lock (); page =radix_tree_lookup (&mapping->i_pages, page_offset);rcu_read_unlock (); -if (page && !radix_tree_exceptional_entry (page)) { +if (page && !xa_is_value (page)) {/* * Page already present? Kick off the current batch of * contiguous pages before continuing with the next
mm/shmem.c +5 -5
@@ -708,7 +708,7 @@ unsigned long shmem_partial_swap_usage(struct address_space *mapping,continue ; } -if (radix_tree_exceptional_entry (page)) +if (xa_is_value (page)) swapped++;if (need_resched ()) {
@@ -823,7 +823,7 @@ static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend,if (index >= end)break ; -if (radix_tree_exceptional_entry (page)) { +if (xa_is_value (page)) {if (unfalloc)continue ; nr_swaps_freed += !shmem_free_swap(mapping,
@@ -920,7 +920,7 @@ static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend,if (index >= end)break ; -if (radix_tree_exceptional_entry (page)) { +if (xa_is_value (page)) {if (unfalloc)continue ;if (shmem_free_swap (mapping, index, page)) {
@@ -1641,7 +1641,7 @@ static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, repeat: swap.val =0 ; page =find_lock_entry (mapping, index); -if (radix_tree_exceptional_entry (page)) { +if (xa_is_value (page)) { swap =radix_to_swp_entry (page); page =NULL ; }
@@ -2574,7 +2574,7 @@ static pgoff_t shmem_seek_hole_data(struct address_space *mapping, index = indices[i]; } page = pvec.pages[i]; -if (page && !radix_tree_exceptional_entry (page)) { +if (page && !xa_is_value (page)) {if (!PageUptodate (page)) page =NULL ; }
mm/swap.c +1 -1
@@ -965,7 +965,7 @@ void pagevec_remove_exceptionals(struct pagevec *pvec)for (i =0 , j =0 ; i <pagevec_count (pvec); i++) {struct page *page = pvec->pages[i]; -if (!radix_tree_exceptional_entry (page)) +if (!xa_is_value (page)) pvec->pages[j++] = page; } pvec->nr = j;
mm/truncate.c +6 -6
@@ -70,7 +70,7 @@ static void truncate_exceptional_pvec_entries(struct address_space *mapping,return ;for (j =0 ; j <pagevec_count (pvec); j++) -if (radix_tree_exceptional_entry (pvec->pages[j])) +if (xa_is_value (pvec->pages[j]))break ;if (j ==pagevec_count (pvec))
@@ -85,7 +85,7 @@ static void truncate_exceptional_pvec_entries(struct address_space *mapping,struct page *page = pvec->pages[i];pgoff_t index = indices[i]; -if (!radix_tree_exceptional_entry (page)) { +if (!xa_is_value (page)) { pvec->pages[j++] = page;continue ; }
@@ -347,7 +347,7 @@ void truncate_inode_pages_range(struct address_space *mapping,if (index >= end)break ; -if (radix_tree_exceptional_entry (page)) +if (xa_is_value (page))continue ;if (!trylock_page (page))
@@ -442,7 +442,7 @@ void truncate_inode_pages_range(struct address_space *mapping,break ; } -if (radix_tree_exceptional_entry (page)) +if (xa_is_value (page))continue ;lock_page (page);
@@ -561,7 +561,7 @@ unsigned long invalidate_mapping_pages(struct address_space *mapping,if (index > end)break ; -if (radix_tree_exceptional_entry (page)) { +if (xa_is_value (page)) {invalidate_exceptional_entry (mapping, index, page);continue ;
@@ -692,7 +692,7 @@ int invalidate_inode_pages2_range(struct address_space *mapping,if (index > end)break ; -if (radix_tree_exceptional_entry (page)) { +if (xa_is_value (page)) {if (!invalidate_exceptional_entry2 (mapping, index, page)) ret = -EBUSY ;
mm/workingset.c +5 -7
@@ -155,8 +155,8 @@* refault distance will immediately activate the refaulting page. */ -#define EVICTION_SHIFT (RADIX_TREE_EXCEPTIONAL_ENTRY + \ - NODES_SHIFT + \ +#define EVICTION_SHIFT ((BITS_PER_LONG - BITS_PER_XA_VALUE) + \ + NODES_SHIFT + \ MEM_CGROUP_ID_SHIFT)#define EVICTION_MASK (~0UL >> EVICTION_SHIFT)
@@ -175,18 +175,16 @@ static void *pack_shadow(int memcgid, pg_data_t *pgdat, unsigned long eviction) eviction >>= bucket_order; eviction = (eviction <<MEM_CGROUP_ID_SHIFT ) | memcgid; eviction = (eviction <<NODES_SHIFT ) | pgdat->node_id; - eviction = (eviction <<RADIX_TREE_EXCEPTIONAL_SHIFT ); -return (void *)(eviction |RADIX_TREE_EXCEPTIONAL_ENTRY ); +return xa_mk_value (eviction); }static void unpack_shadow (void *shadow,int *memcgidp,pg_data_t **pgdat,unsigned long *evictionp) { -unsigned long entry = (unsigned long )shadow; +unsigned long entry =xa_to_value (shadow);int memcgid, nid; - entry >>=RADIX_TREE_EXCEPTIONAL_SHIFT ; nid = entry & ((1UL <<NODES_SHIFT ) -1 ); entry >>=NODES_SHIFT ; memcgid = entry & ((1UL <<MEM_CGROUP_ID_SHIFT ) -1 );
@@ -453,7 +451,7 @@ static enum lru_status shadow_lru_isolate(struct list_head *item, goto out_invalid;for (i =0 ; i <RADIX_TREE_MAP_SIZE ; i++) {if (node->slots[i]) { -if (WARN_ON_ONCE (!radix_tree_exceptional_entry (node->slots[i]))) +if (WARN_ON_ONCE (!xa_is_value (node->slots[i]))) goto out_invalid;if (WARN_ON_ONCE (!node->exceptional)) goto out_invalid;
tools/testing/radix-tree/idr-test.c +3 -3
@@ -19,7 +19,7 @@#include "test.h" -#define DUMMY_PTR ((void *)0x12) +#define DUMMY_PTR ((void *)0x10)int item_idr_free (int id,void *p,void *data) {
@@ -400,11 +400,11 @@ void ida_check_conv(void)for (i =0 ; i <1000000 ; i++) {int err =ida_get_new (&ida, &id);if (err == -EAGAIN ) { -assert ((i %IDA_BITMAP_BITS ) == (BITS_PER_LONG -2 )); +assert ((i %IDA_BITMAP_BITS ) ==BITS_PER_XA_VALUE );assert (ida_pre_get (&ida,GFP_KERNEL )); err =ida_get_new (&ida, &id); }else { -assert ((i %IDA_BITMAP_BITS ) != (BITS_PER_LONG -2 )); +assert ((i %IDA_BITMAP_BITS ) !=BITS_PER_XA_VALUE ); }assert (!err);assert (id == i);
tools/testing/radix-tree/linux/radix-tree.h +0 -1
@@ -2,7 +2,6 @@#ifndef _TEST_RADIX_TREE_H#define _TEST_RADIX_TREE_H -#include "generated/map-shift.h" #include "../../../../include/linux/radix-tree.h" extern int kmalloc_verbose;
tools/testing/radix-tree/linux/xarray.h new file +2 -0
@@ -0,0 +1,2 @@ +#include "generated/map-shift.h" +#include "../../../../include/linux/xarray.h"
tools/testing/radix-tree/multiorder.c +23 -24
145 lines not on this page · raw message
tools/testing/radix-tree/test.c +1 -1
@@ -295,7 +295,7 @@ void item_kill_tree(struct radix_tree_root *root)int nfound;radix_tree_for_each_slot (slot, root, &iter,0 ) { -if (radix_tree_exceptional_entry (*slot)) +if (xa_is_value (*slot))radix_tree_delete (root, iter.index); }
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 5/74 xarray: Change definition of sibling entries
In reply to Matthew WilcoxInstead of storing a pointer to the slot containing the canonical entry, store the offset of the slot. Produces slightly more efficient code (~300 bytes) and simplifies the implementation.
--- include/linux/xarray.h | 93 ++++++++++++++++++++++++++++++++++++++++++ lib/radix-tree.c | 64 +++++++++-------------------- 2 files changed, 112 insertions(+), 45 deletions(-)
include/linux/xarray.h +93 -0
@@ -22,6 +22,12 @@* x1: Value entry * * Attempting to store internal entries in the XArray is a bug. +* +* Most internal entries are pointers to the next node in the tree. +* The following internal entries have a special meaning: +* +* 0-62: Sibling entries +* 256: Retry entry */ #define BITS_PER_XA_VALUE (BITS_PER_LONG - 1)
@@ -63,6 +69,42 @@ static inline bool xa_is_value(const void *entry)return (unsigned long )entry &1 ; } +/* +* xa_mk_internal() - Create an internal entry. +* @v: Value to turn into an internal entry. +* +* Context: Any context. +* Return: An XArray internal entry corresponding to this value. +*/ +static inline void *xa_mk_internal (unsigned long v) +{ +return (void *)((v <<2 ) |2 ); +} + +/* +* xa_to_internal() - Extract the value from an internal entry. +* @entry: XArray entry. +* +* Context: Any context. +* Return: The value which was stored in the internal entry. +*/ +static inline unsigned long xa_to_internal (const void *entry) +{ +return (unsigned long )entry >>2 ; +} + +/* +* xa_is_internal() - Is the entry an internal entry? +* @entry: XArray entry. +* +* Context: Any context. +* Return: %true if the entry is an internal entry. +*/ +static inline bool xa_is_internal (const void *entry) +{ +return ((unsigned long )entry &3 ) ==2 ; +} +#define xa_trylock (xa) spin_trylock(&(xa)->xa_lock)#define xa_lock (xa) spin_lock(&(xa)->xa_lock)#define xa_unlock (xa) spin_unlock(&(xa)->xa_lock)
@@ -75,4 +117,55 @@ static inline bool xa_is_value(const void *entry)#define xa_unlock_irqrestore (xa, flags) \ spin_unlock_irqrestore(&(xa)->xa_lock, flags) +/* Everything below here is the Advanced API. Proceed with caution. */ + +/* +* The xarray is constructed out of a set of 'chunks' of pointers. Choosing +* the best chunk size requires some tradeoffs. A power of two recommends +* itself so that we can walk the tree based purely on shifts and masks. +* Generally, the larger the better; as the number of slots per level of the +* tree increases, the less tall the tree needs to be. But that needs to be +* balanced against the memory consumption of each node. On a 64-bit system, +* xa_node is currently 576 bytes, and we get 7 of them per 4kB page. If we +* doubled the number of slots per node, we'd get only 3 nodes per 4kB page. +*/ +#ifndef XA_CHUNK_SHIFT +#define XA_CHUNK_SHIFT (CONFIG_BASE_SMALL ? 4 : 6) +#endif +#define XA_CHUNK_SIZE (1UL << XA_CHUNK_SHIFT) +#define XA_CHUNK_MASK (XA_CHUNK_SIZE - 1) + +/* Private */ +static inline bool xa_is_node (const void *entry) +{ +return xa_is_internal (entry) && (unsigned long )entry >4096 ; +} + +/* Private */ +static inline void *xa_mk_sibling (unsigned int offset) +{ +return xa_mk_internal (offset); +} + +/* Private */ +static inline unsigned long xa_to_sibling (const void *entry) +{ +return xa_to_internal (entry); +} + +/** +* xa_is_sibling() - Is the entry a sibling entry? +* @entry: Entry retrieved from the XArray +* +* Return: %true if the entry is a sibling entry. +*/ +static inline bool xa_is_sibling (const void *entry) +{ +return IS_ENABLED (CONFIG_RADIX_TREE_MULTIORDER ) && +xa_is_internal (entry) && + (entry <xa_mk_sibling (XA_CHUNK_SIZE -1 )); +} + +#define XA_RETRY_ENTRY xa_mk_internal(256) +#endif /* _LINUX_XARRAY_H */
lib/radix-tree.c +19 -45
158 lines not on this page · raw message
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 6/74 xarray: Add definition of struct xarray
In reply to Matthew WilcoxThis is a direct replacement for struct radix_tree_root. Some of the struct members have changed name; convert those, and use a #define so that radix_tree users continue to work without change.
--- include/linux/radix-tree.h | 33 +++-------- include/linux/xarray.h | 61 +++++++++++++++++++ lib/Makefile | 2 +- lib/idr.c | 4 +- lib/radix-tree.c | 75 ++++++++++++------------ lib/xarray.c | 44 ++++++++++++++ tools/testing/radix-tree/Makefile | 7 ++- tools/testing/radix-tree/linux/bug.h | 1 + tools/testing/radix-tree/linux/kconfig.h | 1 + tools/testing/radix-tree/multiorder.c | 6 +- tools/testing/radix-tree/test.c | 6 +- tools/testing/radix-tree/xarray.c | 7 +++ 12 files changed, 174 insertions(+), 73 deletions(-) create mode 100644 lib/xarray.c create mode 100644 tools/testing/radix-tree/linux/kconfig.h create mode 100644 tools/testing/radix-tree/xarray.c
include/linux/radix-tree.h +9 -24
@@ -30,6 +30,9 @@#include <linux/types.h> #include <linux/xarray.h> +/* Keep unconverted code working */ +#define radix_tree_root xarray +/* * The bottom two bits of the slot determine how the remaining bits in the * slot are interpreted:
@@ -59,10 +62,7 @@ static inline bool radix_tree_is_internal_node(void *ptr)#define RADIX_TREE_MAX_TAGS 3 -#ifndef RADIX_TREE_MAP_SHIFT -#define RADIX_TREE_MAP_SHIFT (CONFIG_BASE_SMALL ? 4 : 6) -#endif - +#define RADIX_TREE_MAP_SHIFT XA_CHUNK_SHIFT#define RADIX_TREE_MAP_SIZE (1UL << RADIX_TREE_MAP_SHIFT)#define RADIX_TREE_MAP_MASK (RADIX_TREE_MAP_SIZE-1)
@@ -95,36 +95,21 @@ struct radix_tree_node {unsigned long tags[RADIX_TREE_MAX_TAGS ][RADIX_TREE_TAG_LONGS ]; }; -/* The IDR tag is stored in the low bits of the GFP flags */ +/* The IDR tag is stored in the low bits of xa_flags */ #define ROOT_IS_IDR ((__force gfp_t)4) -/* The top bits of gfp_mask are used to store the root tags */ +/* The top bits of xa_flags are used to store the root tags */ #define ROOT_TAG_SHIFT (__GFP_BITS_SHIFT) -struct radix_tree_root { -spinlock_t xa_lock; -gfp_t gfp_mask; -struct radix_tree_node __rcu *rnode; -}; - -#define RADIX_TREE_INIT (name, mask) { \ - .xa_lock = __SPIN_LOCK_UNLOCKED(name.xa_lock), \ - .gfp_mask = (mask), \ - .rnode = NULL, \ -} +#define RADIX_TREE_INIT (name, mask) XARRAY_INIT_FLAGS(name, mask)#define RADIX_TREE (name, mask) \ struct radix_tree_root name = RADIX_TREE_INIT(name, mask) -#define INIT_RADIX_TREE (root, mask) \ -do { \ - spin_lock_init(&(root)->xa_lock); \ - (root)->gfp_mask = (mask); \ - (root)->rnode = NULL; \ -} while (0) +#define INIT_RADIX_TREE (root, mask) xa_init_flags(root, mask)static inline bool radix_tree_empty (const struct radix_tree_root *root) { -return root->rnode ==NULL ; +return root->xa_head ==NULL ; }/**
include/linux/xarray.h +61 -0
73 lines not on this page · raw message
lib/Makefile +1 -1
@@ -18,7 +18,7 @@ KCOV_INSTRUMENT_debugobjects.o := n KCOV_INSTRUMENT_dynamic_debug.o := n lib-y := ctype.o string.o vsprintf.o cmdline.o \ - rbtree.o radix-tree.o timerqueue.o\ + rbtree.o radix-tree.o timerqueue.o xarray.o \ idr.o int_sqrt.o extable.o \ sha1.o chacha20.o irq_regs.o argv_split.o \ flex_proportions.o ratelimit.o show_mem.o \
lib/idr.c +2 -2
@@ -41,8 +41,8 @@ int idr_alloc_u32(struct idr *idr, void *ptr, u32 *nextid,if (WARN_ON_ONCE (radix_tree_is_internal_node (ptr)))return -EINVAL ; -if (WARN_ON_ONCE (!(idr->idr_rt.gfp_mask &ROOT_IS_IDR ))) - idr->idr_rt.gfp_mask |=IDR_RT_MARKER ; +if (WARN_ON_ONCE (!(idr->idr_rt.xa_flags &ROOT_IS_IDR ))) + idr->idr_rt.xa_flags |=IDR_RT_MARKER ; id = (id < base) ?0 : id - base;radix_tree_iter_init (&iter, id);
lib/radix-tree.c +37 -38
232 lines not on this page · raw message
lib/xarray.c new file +44 -0
44 lines not on this page · raw message
tools/testing/radix-tree/Makefile +5 -2
@@ -5,7 +5,7 @@ CFLAGS += -I. -I../../include -g -Og -Wall -D_LGPL_SOURCE -fsanitize=address \ LDFLAGS += -fsanitize=address -fsanitize=undefined LDLIBS+= -lpthread -lurcu TARGETS = main idr-test multiorder -CORE_OFILES := radix-tree.o idr.o linux.o test.o find_bit.o +CORE_OFILES := xarray.o radix-tree.o idr.o linux.o test.o find_bit.o OFILES = main.o $(CORE_OFILES) regression1.o regression2.o regression3.o \ tag_check.o multiorder.o idr-test.o iteration_check.o benchmark.o
@@ -34,6 +34,7 @@ vpath %.c ../../lib $(OFILES): Makefile *.h */*.h generated/map-shift.h \ ../../include/linux/*.h \ ../../include/asm/*.h \ + ../../../include/linux/xarray.h \ ../../../include/linux/radix-tree.h \ ../../../include/linux/idr.h
@@ -43,8 +44,10 @@ radix-tree.c: ../../../lib/radix-tree.c idr.c: ../../../lib/idr.c sed -e 's/^static //' -e 's/__always_inline //' -e 's/inline //' < $< > $@ +xarray.o: ../../../lib/xarray.c + generated/map-shift.h: @if ! grep -qws $(SHIFT) generated/map-shift.h; then \ - echo "#define RADIX_TREE_MAP_SHIFT $(SHIFT)" > \ + echo "#define XA_CHUNK_SHIFT $(SHIFT)" > \ generated/map-shift.h; \ fi
tools/testing/radix-tree/linux/bug.h +1 -0
@@ -1 +1,2 @@ +#include <stdio.h> #include "asm/bug.h"
tools/testing/radix-tree/linux/kconfig.h new file +1 -0
@@ -0,0 +1 @@ +#include "../../../../include/linux/kconfig.h"
tools/testing/radix-tree/multiorder.c +3 -3
@@ -192,13 +192,13 @@ static void multiorder_shrink(unsigned long index, int order)assert (item_insert_order (&tree,0 , order) ==0 ); - node = tree.rnode; + node = tree.xa_head;assert (item_insert (&tree, index) ==0 ); -assert (node != tree.rnode); +assert (node != tree.xa_head);assert (item_delete (&tree, index) !=0 ); -assert (node == tree.rnode); +assert (node == tree.xa_head);for (i =0 ; i < max; i++) {struct item *item =item_lookup (&tree, i);
tools/testing/radix-tree/test.c +3 -3
@@ -281,7 +281,7 @@ static int verify_node(struct radix_tree_node *slot, unsigned int tag,void verify_tag_consistency (struct radix_tree_root *root,unsigned int tag) { -struct radix_tree_node *node = root->rnode; +struct radix_tree_node *node = root->xa_head;if (!radix_tree_is_internal_node (node))return ;verify_node (node, tag, !!root_tag_get (root, tag));
@@ -311,13 +311,13 @@ void item_kill_tree(struct radix_tree_root *root) } }assert (radix_tree_gang_lookup (root, (void **)items,0 ,32 ) ==0 ); -assert (root->rnode ==NULL ); +assert (root->xa_head ==NULL ); }void tree_verify_min_height (struct radix_tree_root *root,int maxindex) {unsigned shift; -struct radix_tree_node *node = root->rnode; +struct radix_tree_node *node = root->xa_head;if (!radix_tree_is_internal_node (node)) {assert (maxindex ==0 );return ;
tools/testing/radix-tree/xarray.c new file +7 -0
@@ -0,0 +1,7 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* +* xarray.c: Userspace shim for XArray test-suite +* Copyright (c) 2018 Matthew Wilcox <[email protected]> +*/ + +#include "../../../lib/xarray.c"
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 7/74 xarray: Define struct xa_node
In reply to Matthew WilcoxThis is a direct replacement for struct radix_tree_node. A couple of struct members have changed name, so convert those. Use a #define so that radix tree users continue to work without change.
--- include/linux/radix-tree.h | 29 ++-------------- include/linux/xarray.h | 24 ++++++++++++++ lib/radix-tree.c | 48 +++++++++++++-------------- mm/workingset.c | 16 ++++----- tools/testing/radix-tree/multiorder.c | 30 ++++++++--------- 5 files changed, 74 insertions(+), 73 deletions(-)
include/linux/radix-tree.h +3 -26
@@ -32,6 +32,7 @@/* Keep unconverted code working */ #define radix_tree_root xarray +#define radix_tree_node xa_node/* * The bottom two bits of the slot determine how the remaining bits in the
@@ -60,41 +61,17 @@ static inline bool radix_tree_is_internal_node(void *ptr)/*** radix-tree API starts here ***/ -#define RADIX_TREE_MAX_TAGS 3 -#define RADIX_TREE_MAP_SHIFT XA_CHUNK_SHIFT#define RADIX_TREE_MAP_SIZE (1UL << RADIX_TREE_MAP_SHIFT)#define RADIX_TREE_MAP_MASK (RADIX_TREE_MAP_SIZE-1) -#define RADIX_TREE_TAG_LONGS \ - ((RADIX_TREE_MAP_SIZE + BITS_PER_LONG - 1) / BITS_PER_LONG) +#define RADIX_TREE_MAX_TAGS XA_MAX_TAGS +#define RADIX_TREE_TAG_LONGS XA_TAG_LONGS #define RADIX_TREE_INDEX_BITS (8 /* CHAR_BIT */ * sizeof(unsigned long))#define RADIX_TREE_MAX_PATH (DIV_ROUND_UP(RADIX_TREE_INDEX_BITS, \ RADIX_TREE_MAP_SHIFT)) -/* -* @count is the count of every non-NULL element in the ->slots array -* whether that is a data entry, a retry entry, a user pointer, -* a sibling entry or a pointer to the next level of the tree. -* @exceptional is the count of every element in ->slots which is -* either a data entry or a sibling entry for data. -*/ -struct radix_tree_node { -unsigned char shift;/* Bits remaining in each slot */ -unsigned char offset;/* Slot offset in parent */ -unsigned char count;/* Total entry count */ -unsigned char exceptional;/* Exceptional entry count */ -struct radix_tree_node *parent;/* Used when ascending tree */ -struct radix_tree_root *root;/* The tree we belong to */ -union { -struct list_head private_list;/* For tree user */ -struct rcu_head rcu_head;/* Used when freeing node */ - }; -void __rcu *slots[RADIX_TREE_MAP_SIZE ]; -unsigned long tags[RADIX_TREE_MAX_TAGS ][RADIX_TREE_TAG_LONGS ]; -}; -/* The IDR tag is stored in the low bits of xa_flags */ #define ROOT_IS_IDR ((__force gfp_t)4)/* The top bits of xa_flags are used to store the root tags */
include/linux/xarray.h +24 -0
@@ -195,6 +195,30 @@ static inline void xa_init(struct xarray *xa)#endif #define XA_CHUNK_SIZE (1UL << XA_CHUNK_SHIFT)#define XA_CHUNK_MASK (XA_CHUNK_SIZE - 1) +#define XA_MAX_TAGS 3 +#define XA_TAG_LONGS DIV_ROUND_UP(XA_CHUNK_SIZE, BITS_PER_LONG) + +/* +* @count is the count of every non-NULL element in the ->slots array +* whether that is a value entry, a retry entry, a user pointer, +* a sibling entry or a pointer to the next level of the tree. +* @nr_values is the count of every element in ->slots which is +* either a value entry or a sibling entry to a value entry. +*/ +struct xa_node { +unsigned char shift;/* Bits remaining in each slot */ +unsigned char offset;/* Slot offset in parent */ +unsigned char count;/* Total entry count */ +unsigned char nr_values;/* Value entry count */ +struct xa_node __rcu *parent;/* NULL at top of tree */ +struct xarray *array;/* The array we belong to */ +union { +struct list_head private_list;/* For tree user */ +struct rcu_head rcu_head;/* Used when freeing node */ + }; +void __rcu *slots[XA_CHUNK_SIZE ]; +unsigned long tags[XA_MAX_TAGS ][XA_TAG_LONGS ]; +};/* Private */ static inline bool xa_is_node (const void *entry)
lib/radix-tree.c +24 -24
151 lines not on this page · raw message
mm/workingset.c +8 -8
@@ -348,7 +348,7 @@ void workingset_update_node(struct radix_tree_node *node)* already where they should be. The list_empty() test is safe * as node->private_list is protected by the i_pages lock. */ -if (node->count && node->count == node->exceptional) { +if (node->count && node->count == node->nr_values) {if (list_empty (&node->private_list))list_lru_add (&shadow_nodes, &node->private_list); }else {
@@ -427,8 +427,8 @@ static enum lru_status shadow_lru_isolate(struct list_head *item,* to reclaim, take the node off-LRU, and drop the lru_lock. */ - node = container_of(item, struct radix_tree_node, private_list); - mapping = container_of(node->root, struct address_space, i_pages); + node = container_of(item, struct xa_node, private_list); + mapping = container_of(node->array, struct address_space, i_pages);/* Coming from the list, invert the lock order */ if (!xa_trylock (&mapping->i_pages)) {
@@ -445,25 +445,25 @@ static enum lru_status shadow_lru_isolate(struct list_head *item,* no pages, so we expect to be able to remove them all and * delete and free the empty node afterwards. */ -if (WARN_ON_ONCE (!node->exceptional)) +if (WARN_ON_ONCE (!node->nr_values)) goto out_invalid; -if (WARN_ON_ONCE (node->count != node->exceptional)) +if (WARN_ON_ONCE (node->count != node->nr_values)) goto out_invalid;for (i =0 ; i <RADIX_TREE_MAP_SIZE ; i++) {if (node->slots[i]) {if (WARN_ON_ONCE (!xa_is_value (node->slots[i]))) goto out_invalid; -if (WARN_ON_ONCE (!node->exceptional)) +if (WARN_ON_ONCE (!node->nr_values)) goto out_invalid;if (WARN_ON_ONCE (!mapping->nrexceptional)) goto out_invalid; node->slots[i] =NULL ; - node->exceptional--; + node->nr_values--; node->count--; mapping->nrexceptional--; } } -if (WARN_ON_ONCE (node->exceptional)) +if (WARN_ON_ONCE (node->nr_values)) goto out_invalid;inc_lruvec_page_state (virt_to_page (node),WORKINGSET_NODERECLAIM ); __radix_tree_delete_node(&mapping->i_pages, node,
tools/testing/radix-tree/multiorder.c +15 -15
@@ -393,7 +393,7 @@ static void multiorder_join2(unsigned order1, unsigned order2)radix_tree_insert (&tree,1 << order2,xa_mk_value (5 )); item2 =__radix_tree_lookup (&tree,1 << order2, &node,NULL );assert (item2 ==xa_mk_value (5 )); -assert (node->exceptional ==1 ); +assert (node->nr_values ==1 ); item2 =radix_tree_lookup (&tree,0 );free (item2);
@@ -401,7 +401,7 @@ static void multiorder_join2(unsigned order1, unsigned order2)radix_tree_join (&tree,0 , order1, item1); item2 =__radix_tree_lookup (&tree,1 << order2, &node,NULL );assert (item2 == item1); -assert (node->exceptional ==0 ); +assert (node->nr_values ==0 );item_kill_tree (&tree); }
@@ -409,7 +409,7 @@ static void multiorder_join2(unsigned order1, unsigned order2)* This test revealed an accounting bug for inline data entries at one point. * Nodes were being freed back into the pool with an elevated exception count * by radix_tree_join() and then radix_tree_split() was failing to zero the -* count of exceptional entries. +* count of value entries. */ static void multiorder_join3 (unsigned int order) {
@@ -433,7 +433,7 @@ static void multiorder_join3(unsigned int order) }__radix_tree_lookup (&tree,0 , &node,NULL ); -assert (node->exceptional == node->count); +assert (node->nr_values == node->count);item_kill_tree (&tree); }
@@ -520,7 +520,7 @@ static void __multiorder_split2(int old_order, int new_order) item =__radix_tree_lookup (&tree,0 , &node,NULL );assert (item ==xa_mk_value (5 )); -assert (node->exceptional >0 ); +assert (node->nr_values >0 );radix_tree_split (&tree,0 , new_order);radix_tree_for_each_slot (slot, &tree, &iter,0 ) {
@@ -530,7 +530,7 @@ static void __multiorder_split2(int old_order, int new_order) item =__radix_tree_lookup (&tree,0 , &node,NULL );assert (item !=xa_mk_value (5 )); -assert (node->exceptional ==0 ); +assert (node->nr_values ==0 );item_kill_tree (&tree); }
@@ -547,7 +547,7 @@ static void __multiorder_split3(int old_order, int new_order) item =__radix_tree_lookup (&tree,0 , &node,NULL );assert (item ==xa_mk_value (5 )); -assert (node->exceptional >0 ); +assert (node->nr_values >0 );radix_tree_split (&tree,0 , new_order);radix_tree_for_each_slot (slot, &tree, &iter,0 ) {
@@ -556,7 +556,7 @@ static void __multiorder_split3(int old_order, int new_order) item =__radix_tree_lookup (&tree,0 , &node,NULL );assert (item ==xa_mk_value (7 )); -assert (node->exceptional >0 ); +assert (node->nr_values >0 );item_kill_tree (&tree);
@@ -564,7 +564,7 @@ static void __multiorder_split3(int old_order, int new_order) item =__radix_tree_lookup (&tree,0 , &node,NULL );assert (item ==xa_mk_value (5 )); -assert (node->exceptional >0 ); +assert (node->nr_values >0 );radix_tree_split (&tree,0 , new_order);radix_tree_for_each_slot (slot, &tree, &iter,0 ) {
@@ -577,13 +577,13 @@ static void __multiorder_split3(int old_order, int new_order) item =__radix_tree_lookup (&tree,1 << new_order, &node,NULL );assert (item ==xa_mk_value (7 )); -assert (node->count == node->exceptional); +assert (node->count == node->nr_values);do { node = node->parent;if (!node)break ;assert (node->count ==1 ); -assert (node->exceptional ==0 ); +assert (node->nr_values ==0 ); }while (1 );item_kill_tree (&tree);
@@ -611,15 +611,15 @@ static void multiorder_account(void)__radix_tree_insert (&tree,1 <<5 ,5 ,xa_mk_value (5 ));__radix_tree_lookup (&tree,0 , &node,NULL ); -assert (node->count == node->exceptional *2 ); +assert (node->count == node->nr_values *2 );radix_tree_delete (&tree,1 <<5 ); -assert (node->exceptional ==0 ); +assert (node->nr_values ==0 );__radix_tree_insert (&tree,1 <<5 ,5 ,xa_mk_value (5 ));__radix_tree_lookup (&tree,1 <<5 , &node, &slot); -assert (node->count == node->exceptional *2 ); +assert (node->count == node->nr_values *2 );__radix_tree_replace (&tree, node, slot,NULL ,NULL ); -assert (node->exceptional ==0 ); +assert (node->nr_values ==0 );item_kill_tree (&tree); }
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 8/74 xarray: Add documentation
In reply to Matthew WilcoxThis is documentation on how to use the XArray, not details about its internal implementation.
--- Documentation/core-api/index.rst | 1 + Documentation/core-api/xarray.rst | 395 ++++++++++++++++++++++++++++++ 2 files changed, 396 insertions(+) create mode 100644 Documentation/core-api/xarray.rst
Documentation/core-api/index.rst +1 -0
@@ -21,6 +21,7 @@ Core utilities local_ops workqueue genericirq + xarray flexible-arrays librs genalloc
Documentation/core-api/xarray.rst new file +395 -0
395 lines not on this page · raw message
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 9/74 xarray: Add XArray load operation
In reply to Matthew WilcoxThe xa_load function brings with it a lot of infrastructure; xa_empty(), xa_is_err(), and large chunks of the XArray advanced API that are used to implement xa_load.
As the test-suite demonstrates, it is possible to use the XArray functions on a radix tree. The radix tree functions depend on the GFP flags being stored in the root of the tree, so it's not possible to use the radix tree functions on an XArray.
--- include/linux/xarray.h | 336 ++++++++++++++++++++++ lib/Kconfig.debug | 3 + lib/Makefile | 1 + lib/radix-tree.c | 43 --- lib/test_xarray.c | 84 ++++++ lib/xarray.c | 192 +++++++++++++ tools/include/linux/kernel.h | 1 + tools/testing/radix-tree/.gitignore | 1 + tools/testing/radix-tree/Makefile | 6 +- tools/testing/radix-tree/linux/kernel.h | 1 + tools/testing/radix-tree/linux/rcupdate.h | 2 + tools/testing/radix-tree/main.c | 1 + tools/testing/radix-tree/test.h | 1 + tools/testing/radix-tree/xarray.c | 26 ++ 14 files changed, 653 insertions(+), 45 deletions(-) create mode 100644 lib/test_xarray.c
include/linux/xarray.h +336 -0
@@ -12,6 +12,8 @@#include <linux/bug.h> #include <linux/compiler.h> #include <linux/kconfig.h> +#include <linux/kernel.h> +#include <linux/rcupdate.h> #include <linux/spinlock.h> #include <linux/types.h>
@@ -30,6 +32,10 @@* * 0-62: Sibling entries * 256: Retry entry +* +* Errors are also represented as internal entries, but use the negative +* space (-4094 to -2). They're never stored in the slots array; only +* returned by the normal API. */ #define BITS_PER_XA_VALUE (BITS_PER_LONG - 1)
@@ -107,6 +113,42 @@ static inline bool xa_is_internal(const void *entry)return ((unsigned long )entry &3 ) ==2 ; } +/** +* xa_is_err() - Report whether an XArray operation returned an error +* @entry: Result from calling an XArray function +* +* If an XArray operation cannot complete an operation, it will return +* a special value indicating an error. This function tells you +* whether an error occurred; xa_err() tells you which error occurred. +* +* Context: Any context. +* Return: %true if the entry indicates an error. +*/ +static inline bool xa_is_err (const void *entry) +{ +return unlikely (xa_is_internal (entry)); +} + +/** +* xa_err() - Turn an XArray result into an errno. +* @entry: Result from calling an XArray function. +* +* If an XArray operation cannot complete an operation, it will return +* a special pointer value which encodes an errno. This function extracts +* the errno from the pointer value, or returns 0 if the pointer does not +* represent an errno. +* +* Context: Any context. +* Return: A negative errno or 0. +*/ +static inline int xa_err (void *entry) +{ +/* xa_to_internal() would not do sign extension. */ +if (xa_is_err (entry)) +return (long )entry >>2 ; +return 0 ; +} +/** * struct xarray - The anchor of the XArray. * @xa_lock: Lock that protects the contents of the XArray.
@@ -152,6 +194,7 @@ struct xarray { struct xarray name = XARRAY_INIT_FLAGS(name, flags)void xa_init_flags (struct xarray *,gfp_t flags); +void *xa_load (struct xarray *,unsigned long index);/** * xa_init() - Initialise an empty XArray.
@@ -166,6 +209,18 @@ static inline void xa_init(struct xarray *xa)xa_init_flags (xa,0 ); } +/** +* xa_empty() - Determine if an array has any present entries. +* @xa: XArray. +* +* Context: Any context. +* Return: %true if the array contains only NULL pointers. +*/ +static inline bool xa_empty (const struct xarray *xa) +{ +return xa->xa_head ==NULL ; +} +#define xa_trylock (xa) spin_trylock(&(xa)->xa_lock)#define xa_lock (xa) spin_lock(&(xa)->xa_lock)#define xa_unlock (xa) spin_unlock(&(xa)->xa_lock)
@@ -220,6 +275,65 @@ struct xa_node {unsigned long tags[XA_MAX_TAGS ][XA_TAG_LONGS ]; }; +void xa_dump (const struct xarray *); +void xa_dump_node (const struct xa_node *); + +#ifdef XA_DEBUG +#define XA_BUG_ON (xa, x) do { \ + if (x) { \ + xa_dump(xa); \ + BUG(); \ + } \ + } while (0) +#define XA_NODE_BUG_ON (node, x) do { \ + if (x) { \ + if (node) xa_dump_node(node); \ + BUG(); \ + } \ + } while (0) +#else +#define XA_BUG_ON (xa, x) do { } while (0) +#define XA_NODE_BUG_ON (node, x) do { } while (0) +#endif + +/* Private */ +static inline void *xa_head (const struct xarray *xa) +{ +return rcu_dereference_check (xa->xa_head, +lockdep_is_held (&xa->xa_lock)); +} + +/* Private */ +static inline void *xa_head_locked (const struct xarray *xa) +{ +return rcu_dereference_protected (xa->xa_head, +lockdep_is_held (&xa->xa_lock)); +} + +/* Private */ +static inline void *xa_entry (const struct xarray *xa, +const struct xa_node *node,unsigned int offset) +{ +XA_NODE_BUG_ON (node, offset >=XA_CHUNK_SIZE ); +return rcu_dereference_check (node->slots[offset], +lockdep_is_held (&xa->xa_lock)); +} + +/* Private */ +static inline void *xa_entry_locked (const struct xarray *xa, +const struct xa_node *node,unsigned int offset) +{ +XA_NODE_BUG_ON (node, offset >=XA_CHUNK_SIZE ); +return rcu_dereference_protected (node->slots[offset], +lockdep_is_held (&xa->xa_lock)); +} + +/* Private */ +static inline struct xa_node *xa_to_node (const void *entry) +{ +return (struct xa_node *)((unsigned long )entry -2 ); +} +/* Private */ static inline bool xa_is_node (const void *entry) {
@@ -253,4 +367,226 @@ static inline bool xa_is_sibling(const void *entry)#define XA_RETRY_ENTRY xa_mk_internal(256) +/** +* xa_is_retry() - Is the entry a retry entry? +* @entry: Entry retrieved from the XArray +* +* Return: %true if the entry is a retry entry. +*/ +static inline bool xa_is_retry (const void *entry) +{ +return unlikely (entry ==XA_RETRY_ENTRY ); +} + +/** +* typedef xa_update_node_t - A callback function from the XArray. +* @node: The node which is being processed +* +* This function is called every time the XArray updates the count of +* present and value entries in a node. It allows advanced users to +* maintain the private_list in the node. +* +* Context: The xa_lock is held and interrupts may be disabled. +* Implementations should not drop the xa_lock, nor re-enable +* interrupts. +*/ +typedef void (*xa_update_node_t )(struct xa_node *node); + +/* +* The xa_state is opaque to its users. It contains various different pieces +* of state involved in the current operation on the XArray. It should be +* declared on the stack and passed between the various internal routines. +* The various elements in it should not be accessed directly, but only +* through the provided accessor functions. The below documentation is for +* the benefit of those working on the code, not for users of the XArray. +* +* @xa_node usually points to the xa_node containing the slot we're operating +* on (and @xa_offset is the offset in the slots array). If there is a +* single entry in the array at index 0, there are no allocated xa_nodes to +* point to, and so we store %NULL in @xa_node. @xa_node is set to +* the value %XAS_RESTART if the xa_state is not walked to the correct +* position in the tree of nodes for this operation. If an error occurs +* during an operation, it is set to an %XAS_ERROR value. If we run off the +* end of the allocated nodes, it is set to %XAS_BOUNDS. +*/ +struct xa_state { +struct xarray *xa; +unsigned long xa_index; +unsigned char xa_shift; +unsigned char xa_sibs; +unsigned char xa_offset; +unsigned char xa_pad;/* Helps gcc generate better code */ +struct xa_node *xa_node; +struct xa_node *xa_alloc; +xa_update_node_t xa_update; +}; + +/* +* We encode errnos in the xas->xa_node. If an error has happened, we need to +* drop the lock to fix it, and once we've done so the xa_state is invalid. +*/ +#define XA_ERROR (errno) ((struct xa_node *)(((unsigned long)errno << 2) | 2UL)) +#define XAS_BOUNDS ((struct xa_node *)1UL) +#define XAS_RESTART ((struct xa_node *)3UL) + +#define __XA_STATE (array, index, shift, sibs) { \ + .xa = array, \ + .xa_index = index, \ + .xa_shift = shift, \ + .xa_sibs = sibs, \ + .xa_offset = 0, \ + .xa_pad = 0, \ + .xa_node = XAS_RESTART, \ + .xa_alloc = NULL, \ + .xa_update = NULL \ +} + +/** +* XA_STATE() - Declare an XArray operation state. +* @name: Name of this operation state (usually xas). +* @array: Array to operate on. +* @index: Initial index of interest. +* +* Declare and initialise an xa_state on the stack. +*/ +#define XA_STATE (name, array, index) \ + struct xa_state name = __XA_STATE(array, index, 0, 0) + +/** +* XA_STATE_ORDER() - Declare an XArray operation state. +* @name: Name of this operation state (usually xas). +* @array: Array to operate on. +* @index: Initial index of interest. +* @order: Order of entry. +* +* Declare and initialise an xa_state on the stack. This variant of +* XA_STATE() allows you to specify the 'order' of the element you +* want to operate on.` +*/ +#define XA_STATE_ORDER (name, array, index, order) \ + struct xa_state name = __XA_STATE(array, \ + (index >> order) << order, \ + order - (order % XA_CHUNK_SHIFT), \ + (1U << (order % XA_CHUNK_SHIFT)) - 1) + +#define xas_tagged (xas, tag) xa_tagged((xas)->xa, (tag)) +#define xas_trylock (xas) xa_trylock((xas)->xa) +#define xas_lock (xas) xa_lock((xas)->xa) +#define xas_unlock (xas) xa_unlock((xas)->xa) +#define xas_lock_bh (xas) xa_lock_bh((xas)->xa) +#define xas_unlock_bh (xas) xa_unlock_bh((xas)->xa) +#define xas_lock_irq (xas) xa_lock_irq((xas)->xa) +#define xas_unlock_irq (xas) xa_unlock_irq((xas)->xa) +#define xas_lock_irqsave (xas, flags) \ + xa_lock_irqsave((xas)->xa, flags) +#define xas_unlock_irqrestore (xas, flags) \ + xa_unlock_irqrestore((xas)->xa, flags) + +/** +* xas_error() - Return an errno stored in the xa_state. +* @xas: XArray operation state. +* +* Return: 0 if no error has been noted. A negative errno if one has. +*/ +static inline int xas_error (const struct xa_state *xas) +{ +return xa_err (xas->xa_node); +} + +/** +* xas_set_err() - Note an error in the xa_state. +* @xas: XArray operation state. +* @err: Negative error number. +* +* Only call this function with a negative @err; zero or positive errors +* will probably not behave the way you think they should. If you want +* to clear the error from an xa_state, use xas_reset(). +*/ +static inline void xas_set_err (struct xa_state *xas,long err) +{ + xas->xa_node =XA_ERROR (err); +} + +/** +* xas_invalid() - Is the xas in a retry or error state? +* @xas: XArray operation state. +* +* Return: %true if the xas cannot be used for operations. +*/ +static inline bool xas_invalid (const struct xa_state *xas) +{ +return (unsigned long )xas->xa_node &3 ; +} + +/** +* xas_valid() - Is the xas a valid cursor into the array? +* @xas: XArray operation state. +* +* Return: %true if the xas can be used for operations. +*/ +static inline bool xas_valid (const struct xa_state *xas) +{ +return !xas_invalid (xas); +} + +/** +* xas_reset() - Reset an XArray operation state. +* @xas: XArray operation state. +* +* Resets the error or walk state of the @xas so future walks of the +* array will start from the root. Use this if you have dropped the +* xarray lock and want to reuse the xa_state. +* +* Context: Any context. +*/ +static inline void xas_reset (struct xa_state *xas) +{ + xas->xa_node =XAS_RESTART ; +} + +/** +* xas_retry() - Handle a retry entry. +* @xas: XArray operation state. +* @entry: Entry from xarray. +* +* An RCU-protected read may see a retry entry as a side-effect of a +* simultaneous modification. This function sets up the @xas to retry +* the walk from the head of the array. +* +* Context: Any context. +* Return: true if the operation needs to be retried. +*/ +static inline bool xas_retry (struct xa_state *xas,const void *entry) +{ +if (!xa_is_retry (entry)) +return false; +xas_reset (xas); +return true; +} + +void *xas_load (struct xa_state *); + +/** +* xas_reload() - Refetch an entry from the xarray. +* @xas: XArray operation state. +* +* Use this function to check that a previously loaded entry still has +* the same value. This is useful for the lockless pagecache lookup where +* we walk the array with only the RCU lock to protect us, lock the page, +* then check that the page hasn't moved since we looked it up. +* +* The caller guarantees that @xas is still valid. If it may be in an +* error or restart state, call xas_load() instead. +* +* Return: The entry at this location in the xarray. +*/ +static inline void *xas_reload (struct xa_state *xas) +{ +struct xa_node *node = xas->xa_node; + +if (node) +return xa_entry (xas->xa, node, xas->xa_offset); +return xa_head (xas->xa); +} +#endif /* _LINUX_XARRAY_H */
lib/Kconfig.debug +3 -0
@@ -1802,6 +1802,9 @@ config TEST_BITMAP If unsure, say N. +config TEST_XARRAY + tristate "Test the XArray code at runtime" + config TEST_UUID tristate "Test functions located in the uuid module at runtime"
lib/Makefile +1 -0
@@ -69,6 +69,7 @@ obj-$(CONFIG_TEST_STATIC_KEYS) += test_static_key_base.o obj-$(CONFIG_TEST_PRINTF) += test_printf.o obj-$(CONFIG_TEST_BITMAP) += test_bitmap.o obj-$(CONFIG_TEST_UUID) += test_uuid.o +obj-$(CONFIG_TEST_XARRAY) += test_xarray.o obj-$(CONFIG_TEST_PARMAN) += test_parman.o obj-$(CONFIG_TEST_KMOD) += test_kmod.o obj-$(CONFIG_TEST_DEBUG_VIRTUAL) += test_debug_virtual.o
lib/radix-tree.c +0 -43
49 lines not on this page · raw message
lib/test_xarray.c new file +84 -0
84 lines not on this page · raw message
lib/xarray.c +192 -0
201 lines not on this page · raw message
tools/include/linux/kernel.h +1 -0
@@ -70,6 +70,7 @@#define BUG_ON (cond) assert(!(cond))#endif #endif +#define BUG () BUG_ON(1)#if __BYTE_ORDER == __BIG_ENDIAN#define cpu_to_le16 bswap_16
tools/testing/radix-tree/.gitignore +1 -0
@@ -4,3 +4,4 @@ idr-test main multiorder radix-tree.c +xarray
tools/testing/radix-tree/Makefile +4 -2
@@ -4,7 +4,7 @@ CFLAGS += -I. -I../../include -g -Og -Wall -D_LGPL_SOURCE -fsanitize=address \ -fsanitize=undefined LDFLAGS += -fsanitize=address -fsanitize=undefined LDLIBS+= -lpthread -lurcu -TARGETS = main idr-test multiorder +TARGETS = main idr-test multiorder xarray CORE_OFILES := xarray.o radix-tree.o idr.o linux.o test.o find_bit.o OFILES = main.o $(CORE_OFILES) regression1.o regression2.o regression3.o \ tag_check.o multiorder.o idr-test.o iteration_check.o benchmark.o
@@ -24,6 +24,8 @@ main: $(OFILES) idr-test: idr-test.o $(CORE_OFILES) +xarray: $(CORE_OFILES) + multiorder: multiorder.o $(CORE_OFILES) clean:
@@ -44,7 +46,7 @@ radix-tree.c: ../../../lib/radix-tree.c idr.c: ../../../lib/idr.c sed -e 's/^static //' -e 's/__always_inline //' -e 's/inline //' < $< > $@ -xarray.o: ../../../lib/xarray.c +xarray.o: ../../../lib/xarray.c ../../../lib/test_xarray.c generated/map-shift.h: @if ! grep -qws $(SHIFT) generated/map-shift.h; then \
tools/testing/radix-tree/linux/kernel.h +1 -0
@@ -14,6 +14,7 @@#include "../../../include/linux/kconfig.h" #define printk printf +#define pr_info printk#define pr_debug printk#define pr_cont printk
tools/testing/radix-tree/linux/rcupdate.h +2 -0
@@ -6,5 +6,7 @@#define rcu_dereference_raw (p) rcu_dereference(p)#define rcu_dereference_protected (p, cond) rcu_dereference(p) +#define rcu_dereference_check (p, cond) rcu_dereference(p) +#define RCU_INIT_POINTER (p, v) (p) = (v)#endif
tools/testing/radix-tree/main.c +1 -0
@@ -365,6 +365,7 @@ int main(int argc, char **argv)rcu_register_thread ();radix_tree_init (); +xarray_tests ();regression1_test ();regression2_test ();regression3_test ();
tools/testing/radix-tree/test.h +1 -0
@@ -34,6 +34,7 @@ int tag_tagged_items(struct radix_tree_root *, pthread_mutex_t *, unsigned iftag, unsigned thentag);unsigned long find_item (struct radix_tree_root *,void *item); +void xarray_tests (void );void tag_check (void );void multiorder_checks (void );void iteration_test (unsigned order,unsigned duration);
tools/testing/radix-tree/xarray.c +26 -0
@@ -4,4 +4,30 @@* Copyright (c) 2018 Matthew Wilcox <[email protected]> */ +#include "test.h" + +#define module_init (x) +#define module_exit (x) +#define dump_stack () assert(0) + +#define XA_DEBUG #include "../../../lib/xarray.c" +#undef XA_DEBUG +#include "../../../lib/test_xarray.c" + +void xarray_tests (void ) +{ +xarray_checks (); +xarray_exit (); +} + +int __weak main (void ) +{ +radix_tree_init (); +xarray_tests (); +radix_tree_cpu_dead (1 ); +rcu_barrier (); +if (nr_allocated) +printf ("nr_allocated = %d\n" , nr_allocated); +return 0 ; +}
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 10/74 xarray: Add XArray tags
In reply to Matthew WilcoxXArray tags are slightly more strongly typed than the radix tree tags, but occupy the same bits. This commit adds the basic get/set/clear operations.
--- include/linux/xarray.h | 63 +++++ lib/test_xarray.c | 34 +++ lib/xarray.c | 227 +++++++++++++++++- tools/include/asm-generic/bitops.h | 1 + tools/include/asm-generic/bitops/atomic.h | 9 - tools/include/asm-generic/bitops/non-atomic.h | 109 +++++++++ tools/include/linux/spinlock.h | 10 +- 7 files changed, 440 insertions(+), 13 deletions(-) create mode 100644 tools/include/asm-generic/bitops/non-atomic.h
include/linux/xarray.h +63 -0
@@ -11,6 +11,7 @@#include <linux/bug.h> #include <linux/compiler.h> +#include <linux/gfp.h> #include <linux/kconfig.h> #include <linux/kernel.h> #include <linux/rcupdate.h>
@@ -149,6 +150,20 @@ static inline int xa_err(void *entry)return 0 ; } +typedef unsigned __bitwise xa_tag_t ; +#define XA_TAG_0 ((__force xa_tag_t)0U) +#define XA_TAG_1 ((__force xa_tag_t)1U) +#define XA_TAG_2 ((__force xa_tag_t)2U) +#define XA_PRESENT ((__force xa_tag_t)8U) +#define XA_TAG_MAX XA_TAG_2 + +/* +* Values for xa_flags. The radix tree stores its GFP flags in the xa_flags, +* and we remain compatible with that. +*/ +#define XA_FLAGS_TAG (tag) ((__force gfp_t)((1U << __GFP_BITS_SHIFT) << \ + (__force unsigned)(tag))) +/** * struct xarray - The anchor of the XArray. * @xa_lock: Lock that protects the contents of the XArray.
@@ -195,6 +210,9 @@ struct xarray {void xa_init_flags (struct xarray *,gfp_t flags);void *xa_load (struct xarray *,unsigned long index); +bool xa_get_tag (struct xarray *,unsigned long index,xa_tag_t ); +void xa_set_tag (struct xarray *,unsigned long index,xa_tag_t ); +void xa_clear_tag (struct xarray *,unsigned long index,xa_tag_t );/** * xa_init() - Initialise an empty XArray.
@@ -221,6 +239,19 @@ static inline bool xa_empty(const struct xarray *xa)return xa->xa_head ==NULL ; } +/** +* xa_tagged() - Inquire whether any entry in this array has a tag set +* @xa: Array +* @tag: Tag value +* +* Context: Any context. +* Return: %true if any entry has this tag set. +*/ +static inline bool xa_tagged (const struct xarray *xa,xa_tag_t tag) +{ +return xa->xa_flags &XA_FLAGS_TAG (tag); +} +#define xa_trylock (xa) spin_trylock(&(xa)->xa_lock)#define xa_lock (xa) spin_lock(&(xa)->xa_lock)#define xa_unlock (xa) spin_unlock(&(xa)->xa_lock)
@@ -233,6 +264,12 @@ static inline bool xa_empty(const struct xarray *xa)#define xa_unlock_irqrestore (xa, flags) \ spin_unlock_irqrestore(&(xa)->xa_lock, flags) +/* +* Versions of the normal API which require the caller to hold the xa_lock. +*/ +void __xa_set_tag (struct xarray *,unsigned long index,xa_tag_t ); +void __xa_clear_tag (struct xarray *,unsigned long index,xa_tag_t ); +/* Everything below here is the Advanced API. Proceed with caution. */ /*
@@ -328,6 +365,22 @@ static inline void *xa_entry_locked(const struct xarray *xa, lockdep_is_held(&xa->xa_lock)); } +/* Private */ +static inline struct xa_node *xa_parent (const struct xarray *xa, +const struct xa_node *node) +{ +return rcu_dereference_check (node->parent, +lockdep_is_held (&xa->xa_lock)); +} + +/* Private */ +static inline struct xa_node *xa_parent_locked (const struct xarray *xa, +const struct xa_node *node) +{ +return rcu_dereference_protected (node->parent, +lockdep_is_held (&xa->xa_lock)); +} +/* Private */ static inline struct xa_node *xa_to_node (const void *entry) {
@@ -529,6 +582,12 @@ static inline bool xas_valid(const struct xa_state *xas)return !xas_invalid (xas); } +/* True if the pointer is something other than a node */ +static inline bool xas_not_node (struct xa_node *node) +{ +return ((unsigned long )node &3 ) || !node; +} +/** * xas_reset() - Reset an XArray operation state. * @xas: XArray operation state.
@@ -566,6 +625,10 @@ static inline bool xas_retry(struct xa_state *xas, const void *entry)void *xas_load (struct xa_state *); +bool xas_get_tag (const struct xa_state *,xa_tag_t ); +void xas_set_tag (const struct xa_state *,xa_tag_t ); +void xas_clear_tag (const struct xa_state *,xa_tag_t ); +/** * xas_reload() - Refetch an entry from the xarray. * @xas: XArray operation state.
lib/test_xarray.c +34 -0
@@ -66,11 +66,45 @@ static void check_xa_load(struct xarray *xa)XA_BUG_ON (xa, !xa_empty (xa)); } +static void check_xa_tag_1 (struct xarray *xa,unsigned long index) +{ +/* NULL elements have no tags set */ +XA_BUG_ON (xa,xa_get_tag (xa, index,XA_TAG_0 )); +xa_set_tag (xa, index,XA_TAG_0 ); +XA_BUG_ON (xa,xa_get_tag (xa, index,XA_TAG_0 )); + +/* Storing a pointer will not make a tag appear */ +XA_BUG_ON (xa,xa_store_value (xa, index,GFP_KERNEL ) !=NULL ); +XA_BUG_ON (xa,xa_get_tag (xa, index,XA_TAG_0 )); +xa_set_tag (xa, index,XA_TAG_0 ); +XA_BUG_ON (xa, !xa_get_tag (xa, index,XA_TAG_0 )); + +/* Setting one tag will not set another tag */ +XA_BUG_ON (xa,xa_get_tag (xa, index +1 ,XA_TAG_0 )); +XA_BUG_ON (xa,xa_get_tag (xa, index,XA_TAG_1 )); + +/* Storing NULL clears tags, and they can't be set again */ +xa_erase_value (xa, index); +XA_BUG_ON (xa, !xa_empty (xa)); +XA_BUG_ON (xa,xa_get_tag (xa, index,XA_TAG_0 )); +xa_set_tag (xa, index,XA_TAG_0 ); +XA_BUG_ON (xa,xa_get_tag (xa, index,XA_TAG_0 )); +} + +static void check_xa_tag (struct xarray *xa) +{ +check_xa_tag_1 (xa,0 ); +check_xa_tag_1 (xa,4 ); +check_xa_tag_1 (xa,64 ); +check_xa_tag_1 (xa,4096 ); +} +static int xarray_checks (void ) {RADIX_TREE (array,GFP_KERNEL );check_xa_load (&array); +check_xa_tag (&array);printk ("XArray: %u of %u tests passed\n" , tests_passed, tests_run);return (tests_run != tests_passed) ?0 : -EINVAL ;
lib/xarray.c +225 -2
257 lines not on this page · raw message
tools/include/asm-generic/bitops.h +1 -0
@@ -27,5 +27,6 @@#include <asm-generic/bitops/hweight.h> #include <asm-generic/bitops/atomic.h> +#include <asm-generic/bitops/non-atomic.h> #endif /* __TOOLS_ASM_GENERIC_BITOPS_H */
tools/include/asm-generic/bitops/atomic.h +0 -9
@@ -15,13 +15,4 @@ static inline void clear_bit(int nr, unsigned long *addr) addr[nr / __BITS_PER_LONG] &= ~(1UL << (nr % __BITS_PER_LONG)); } -static __always_inline int test_bit(unsigned int nr, const unsigned long *addr) -{ -return ((1UL << (nr % __BITS_PER_LONG)) & - (((unsigned long *)addr)[nr / __BITS_PER_LONG])) !=0 ; -} - -#define __set_bit (nr, addr) set_bit(nr, addr) -#define __clear_bit (nr, addr) clear_bit(nr, addr) -#endif /* _TOOLS_LINUX_ASM_GENERIC_BITOPS_ATOMIC_H_ */
tools/include/asm-generic/bitops/non-atomic.h new file +109 -0
109 lines not on this page · raw message
tools/include/linux/spinlock.h +8 -2
@@ -8,8 +8,14 @@#define spinlock_t pthread_mutex_t#define DEFINE_SPINLOCK (x) pthread_mutex_t x = PTHREAD_MUTEX_INITIALIZER#define __SPIN_LOCK_UNLOCKED (x) (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER -#define spin_lock_init (x) pthread_mutex_init(x, NULL) - +#define spin_lock_init (x) pthread_mutex_init(x, NULL) + +#define spin_lock (x) pthread_mutex_lock(x) +#define spin_unlock (x) pthread_mutex_unlock(x) +#define spin_lock_bh (x) pthread_mutex_lock(x) +#define spin_unlock_bh (x) pthread_mutex_unlock(x) +#define spin_lock_irq (x) pthread_mutex_lock(x) +#define spin_unlock_irq (x) pthread_mutex_unlock(x)#define spin_lock_irqsave (x, f) (void)f, pthread_mutex_lock(x)#define spin_unlock_irqrestore (x, f) (void)f, pthread_mutex_unlock(x)
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 11/74 xarray: Add XArray unconditional store operations
In reply to Matthew Wilcoxxa_store() differs from radix_tree_insert() in that it will overwrite an existing element in the array rather than returning an error. This is the behaviour which most users want, and those that want more complex behaviour generally want to use the xas family of routines anyway.
For memory allocation, xa_store() will first attempt to request memory from the slab allocator; if memory is not immediately available, it will drop the xa_lock and allocate memory, keeping a pointer in the xa_state. It does not use the per-CPU cache, although those will continue to exist until all radix tree users are converted to the xarray.
This patch also includes xa_erase() and __xa_erase() for a streamlined way to store NULL. Since there is no need to allocate memory in order to store a NULL in the XArray, we do not need to trouble the user with deciding what memory allocation flags to use.
--- include/linux/xarray.h | 96 ++++ lib/radix-tree.c | 4 +- lib/test_xarray.c | 152 ++++- lib/xarray.c | 684 +++++++++++++++++++++++ tools/include/linux/bitmap.h | 1 + tools/include/linux/spinlock.h | 2 + tools/testing/radix-tree/Makefile | 2 +- tools/testing/radix-tree/bitmap.c | 23 + tools/testing/radix-tree/linux/kernel.h | 4 + tools/testing/radix-tree/linux/lockdep.h | 11 + 10 files changed, 972 insertions(+), 7 deletions(-) create mode 100644 tools/testing/radix-tree/bitmap.c create mode 100644 tools/testing/radix-tree/linux/lockdep.h
include/linux/xarray.h +96 -0
@@ -157,10 +157,17 @@ typedef unsigned __bitwise xa_tag_t;#define XA_PRESENT ((__force xa_tag_t)8U)#define XA_TAG_MAX XA_TAG_2 +enum xa_lock_type { +XA_LOCK_IRQ =1 , +XA_LOCK_BH =2 , +}; +/* * Values for xa_flags. The radix tree stores its GFP flags in the xa_flags, * and we remain compatible with that. */ +#define XA_FLAGS_LOCK_IRQ ((__force gfp_t)XA_LOCK_IRQ) +#define XA_FLAGS_LOCK_BH ((__force gfp_t)XA_LOCK_BH)#define XA_FLAGS_TAG (tag) ((__force gfp_t)((1U << __GFP_BITS_SHIFT) << \ (__force unsigned)(tag)))
@@ -210,6 +217,7 @@ struct xarray {void xa_init_flags (struct xarray *,gfp_t flags);void *xa_load (struct xarray *,unsigned long index); +void *xa_store (struct xarray *,unsigned long index,void *entry,gfp_t );bool xa_get_tag (struct xarray *,unsigned long index,xa_tag_t );void xa_set_tag (struct xarray *,unsigned long index,xa_tag_t );void xa_clear_tag (struct xarray *,unsigned long index,xa_tag_t );
@@ -252,6 +260,23 @@ static inline bool xa_tagged(const struct xarray *xa, xa_tag_t tag)return xa->xa_flags &XA_FLAGS_TAG (tag); } +/** +* xa_erase() - Erase this entry from the XArray. +* @xa: XArray. +* @index: Index of entry. +* +* This function is the equivalent of calling xa_store() with %NULL as +* the third argument. The XArray does not need to allocate memory, so +* the user does not need to provide GFP flags. +* +* Context: Process context. Takes and releases the xa_lock. +* Return: The entry which used to be at this index. +*/ +static inline void *xa_erase (struct xarray *xa,unsigned long index) +{ +return xa_store (xa, index,NULL ,0 ); +} +#define xa_trylock (xa) spin_trylock(&(xa)->xa_lock)#define xa_lock (xa) spin_lock(&(xa)->xa_lock)#define xa_unlock (xa) spin_unlock(&(xa)->xa_lock)
@@ -266,7 +291,11 @@ static inline bool xa_tagged(const struct xarray *xa, xa_tag_t tag)/* * Versions of the normal API which require the caller to hold the xa_lock. +* If the GFP flags allow it, will drop the lock in order to allocate +* memory, then reacquire it afterwards. */ +void *__xa_erase (struct xarray *,unsigned long index); +void *__xa_store (struct xarray *,unsigned long index,void *entry,gfp_t );void __xa_set_tag (struct xarray *,unsigned long index,xa_tag_t );void __xa_clear_tag (struct xarray *,unsigned long index,xa_tag_t );
@@ -381,6 +410,12 @@ static inline struct xa_node *xa_parent_locked(const struct xarray *xa, lockdep_is_held(&xa->xa_lock)); } +/* Private */ +static inline void *xa_mk_node (const struct xa_node *node) +{ +return (void *)((unsigned long )node |2 ); +} +/* Private */ static inline struct xa_node *xa_to_node (const void *entry) {
@@ -588,6 +623,12 @@ static inline bool xas_not_node(struct xa_node *node)return ((unsigned long )node &3 ) || !node; } +/* True if the node represents head-of-tree, RESTART or BOUNDS */ +static inline bool xas_top (struct xa_node *node) +{ +return node <=XAS_RESTART ; +} +/** * xas_reset() - Reset an XArray operation state. * @xas: XArray operation state.
@@ -624,10 +665,14 @@ static inline bool xas_retry(struct xa_state *xas, const void *entry) }void *xas_load (struct xa_state *); +void *xas_store (struct xa_state *,void *entry);bool xas_get_tag (const struct xa_state *,xa_tag_t );void xas_set_tag (const struct xa_state *,xa_tag_t );void xas_clear_tag (const struct xa_state *,xa_tag_t ); +void xas_init_tags (const struct xa_state *); + +bool xas_nomem (struct xa_state *,gfp_t );/** * xas_reload() - Refetch an entry from the xarray.
@@ -652,4 +697,55 @@ static inline void *xas_reload(struct xa_state *xas) return xa_head(xas->xa); } +/** +* xas_set() - Set up XArray operation state for a different index. +* @xas: XArray operation state. +* @index: New index into the XArray. +* +* Move the operation state to refer to a different index. This will +* have the effect of starting a walk from the top; see xas_next() +* to move to an adjacent index. +*/ +static inline void xas_set (struct xa_state *xas,unsigned long index) +{ + xas->xa_index = index; + xas->xa_node =XAS_RESTART ; +} + +/** +* xas_set_order() - Set up XArray operation state for a multislot entry. +* @xas: XArray operation state. +* @index: Target of the operation. +* @order: Entry occupies 2^@order indices. +*/ +static inline void xas_set_order (struct xa_state *xas,unsigned long index, +unsigned int order) +{ +#ifdef CONFIG_RADIX_TREE_MULTIORDER + xas->xa_index = order <BITS_PER_LONG ? (index >> order) << order :0 ; + xas->xa_shift = order - (order %XA_CHUNK_SHIFT ); + xas->xa_sibs = (1 << (order %XA_CHUNK_SHIFT )) -1 ; + xas->xa_node =XAS_RESTART ; +#else +BUG_ON (order >0 ); +xas_set (xas, index); +#endif +} + +/** +* xas_set_update() - Set up XArray operation state for a callback. +* @xas: XArray operation state. +* @update: Function to call when updating a node. +* +* The XArray can notify a caller after it has updated an xa_node. +* This is advanced functionality and is only needed by the page cache. +*/ +static inline void xas_set_update (struct xa_state *xas,xa_update_node_t update) +{ + xas->xa_update = update; +} + +/* Internal functions, mostly shared between radix-tree.c, xarray.c and idr.c */ +void xas_destroy (struct xa_state *); +#endif /* _LINUX_XARRAY_H */
lib/radix-tree.c +2 -2
@@ -47,7 +47,7 @@ static unsigned long height_to_maxnodes[RADIX_TREE_MAX_PATH + 1] __read_mostly;/* * Radix tree node cache. */ -static struct kmem_cache *radix_tree_node_cachep; +struct kmem_cache *radix_tree_node_cachep;/* * The radix tree is variable-height, so an insert operation not only has
@@ -365,7 +365,7 @@ radix_tree_node_alloc(gfp_t gfp_mask, struct radix_tree_node *parent,return ret; } -static void radix_tree_node_rcu_free (struct rcu_head *head) +void radix_tree_node_rcu_free (struct rcu_head *head) {struct radix_tree_node *node = container_of(head, struct radix_tree_node, rcu_head);
lib/test_xarray.c +148 -4
179 lines not on this page · raw message
lib/xarray.c +684 -0
729 lines not on this page · raw message
tools/include/linux/bitmap.h +1 -0
@@ -15,6 +15,7 @@ void __bitmap_or(unsigned long *dst, const unsigned long *bitmap1, const unsigned long *bitmap2, int bits);int __bitmap_and (unsigned long *dst,const unsigned long *bitmap1,const unsigned long *bitmap2,unsigned int bits); +void bitmap_clear (unsigned long *map,unsigned int start,int len);#define BITMAP_FIRST_WORD_MASK (start) (~0UL << ((start) & (BITS_PER_LONG - 1)))
tools/include/linux/spinlock.h +2 -0
@@ -37,4 +37,6 @@ static inline bool arch_spin_is_locked(arch_spinlock_t *mutex) return true; } +#include <linux/lockdep.h> +#endif
tools/testing/radix-tree/Makefile +1 -1
@@ -5,7 +5,7 @@ CFLAGS += -I. -I../../include -g -Og -Wall -D_LGPL_SOURCE -fsanitize=address \ LDFLAGS += -fsanitize=address -fsanitize=undefined LDLIBS+= -lpthread -lurcu TARGETS = main idr-test multiorder xarray -CORE_OFILES := xarray.o radix-tree.o idr.o linux.o test.o find_bit.o +CORE_OFILES := xarray.o radix-tree.o idr.o linux.o test.o find_bit.o bitmap.o OFILES = main.o $(CORE_OFILES) regression1.o regression2.o regression3.o \ tag_check.o multiorder.o idr-test.o iteration_check.o benchmark.o
tools/testing/radix-tree/bitmap.c new file +23 -0
@@ -0,0 +1,23 @@ +/* lib/bitmap.c pulls in at least two other files. */ + +#include <linux/bitmap.h> + +void bitmap_clear (unsigned long *map,unsigned int start,int len) +{ +unsigned long *p = map +BIT_WORD (start); +const unsigned int size = start + len; +int bits_to_clear =BITS_PER_LONG - (start %BITS_PER_LONG ); +unsigned long mask_to_clear =BITMAP_FIRST_WORD_MASK (start); + +while (len - bits_to_clear >=0 ) { + *p &= ~mask_to_clear; + len -= bits_to_clear; + bits_to_clear =BITS_PER_LONG ; + mask_to_clear = ~0UL ; + p++; + } +if (len) { + mask_to_clear &=BITMAP_LAST_WORD_MASK (size); + *p &= ~mask_to_clear; + } +}
tools/testing/radix-tree/linux/kernel.h +4 -0
@@ -18,4 +18,8 @@#define pr_debug printk#define pr_cont printk +#define __acquires (x) +#define __releases (x) +#define __must_hold (x) +#endif /* _KERNEL_H */
tools/testing/radix-tree/linux/lockdep.h new file +11 -0
@@ -0,0 +1,11 @@ +#ifndef _LINUX_LOCKDEP_H +#define _LINUX_LOCKDEP_H +struct lock_class_key { +unsigned int a; +}; + +static inline void lockdep_set_class (spinlock_t *lock, +struct lock_class_key *key) +{ +} +#endif /* _LINUX_LOCKDEP_H */
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 12/74 xarray: Add XArray conditional store operations
In reply to Matthew WilcoxLike cmpxchg(), xa_cmpxchg will only store to the index if the current entry matches the old entry. It returns the current entry, which is usually more useful than the errno returned by radix_tree_insert(). For the users who really only want the errno, the xa_insert() wrapper provides a more convenient calling convention.
--- include/linux/xarray.h | 60 +++++++++++++++++++++++++++++++++++ lib/test_xarray.c | 20 ++++++++++++ lib/xarray.c | 71 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 151 insertions(+)
include/linux/xarray.h +60 -0
@@ -218,6 +218,8 @@ struct xarray {void xa_init_flags (struct xarray *,gfp_t flags);void *xa_load (struct xarray *,unsigned long index);void *xa_store (struct xarray *,unsigned long index,void *entry,gfp_t ); +void *xa_cmpxchg (struct xarray *,unsigned long index, +void *old,void *entry,gfp_t );bool xa_get_tag (struct xarray *,unsigned long index,xa_tag_t );void xa_set_tag (struct xarray *,unsigned long index,xa_tag_t );void xa_clear_tag (struct xarray *,unsigned long index,xa_tag_t );
@@ -277,6 +279,34 @@ static inline void *xa_erase(struct xarray *xa, unsigned long index)return xa_store (xa, index,NULL ,0 ); } +/** +* xa_insert() - Store this entry in the XArray unless another entry is +* already present. +* @xa: XArray. +* @index: Index into array. +* @entry: New entry. +* @gfp: Memory allocation flags. +* +* If you would rather see the existing entry in the array, use xa_cmpxchg(). +* This function is for users who don't care what the entry is, only that +* one is present. +* +* Context: Process context. Takes and releases the xa_lock. +* May sleep if the @gfp flags permit. +* Return: 0 if the store succeeded. -EEXIST if another entry was present. +* -ENOMEM if memory could not be allocated. +*/ +static inline int xa_insert (struct xarray *xa,unsigned long index, +void *entry,gfp_t gfp) +{ +void *curr =xa_cmpxchg (xa, index,NULL , entry, gfp); +if (!curr) +return 0 ; +if (xa_is_err (curr)) +return xa_err (curr); +return -EEXIST ; +} +#define xa_trylock (xa) spin_trylock(&(xa)->xa_lock)#define xa_lock (xa) spin_lock(&(xa)->xa_lock)#define xa_unlock (xa) spin_unlock(&(xa)->xa_lock)
@@ -296,9 +326,39 @@ static inline void *xa_erase(struct xarray *xa, unsigned long index)*/ void *__xa_erase (struct xarray *,unsigned long index);void *__xa_store (struct xarray *,unsigned long index,void *entry,gfp_t ); +void *__xa_cmpxchg (struct xarray *,unsigned long index,void *old, +void *entry,gfp_t );void __xa_set_tag (struct xarray *,unsigned long index,xa_tag_t );void __xa_clear_tag (struct xarray *,unsigned long index,xa_tag_t ); +/** +* __xa_insert() - Store this entry in the XArray unless another entry is +* already present. +* @xa: XArray. +* @index: Index into array. +* @entry: New entry. +* @gfp: Memory allocation flags. +* +* If you would rather see the existing entry in the array, use __xa_cmpxchg(). +* This function is for users who don't care what the entry is, only that +* one is present. +* +* Context: Any context. Expects xa_lock to be held on entry. May +* release and reacquire xa_lock if the @gfp flags permit. +* Return: 0 if the store succeeded. -EEXIST if another entry was present. +* -ENOMEM if memory could not be allocated. +*/ +static inline int __xa_insert (struct xarray *xa,unsigned long index, +void *entry,gfp_t gfp) +{ +void *curr =__xa_cmpxchg (xa, index,NULL , entry, gfp); +if (!curr) +return 0 ; +if (xa_is_err (curr)) +return xa_err (curr); +return -EEXIST ; +} +/* Everything below here is the Advanced API. Proceed with caution. */ /*
lib/test_xarray.c +20 -0
@@ -181,6 +181,25 @@ static void check_xa_shrink(struct xarray *xa)XA_BUG_ON (xa, !xa_empty (xa)); } +static void check_cmpxchg (struct xarray *xa) +{ +void *FIVE =xa_mk_value (5 ); +void *SIX =xa_mk_value (6 ); +void *LOTS =xa_mk_value (12345678 ); + +XA_BUG_ON (xa, !xa_empty (xa)); +XA_BUG_ON (xa,xa_store_value (xa,12345678 ,GFP_KERNEL ) !=NULL ); +XA_BUG_ON (xa,xa_insert (xa,12345678 , xa,GFP_KERNEL ) != -EEXIST ); +XA_BUG_ON (xa,xa_cmpxchg (xa,12345678 ,SIX ,FIVE ,GFP_KERNEL ) !=LOTS ); +XA_BUG_ON (xa,xa_cmpxchg (xa,12345678 ,LOTS ,FIVE ,GFP_KERNEL ) !=LOTS ); +XA_BUG_ON (xa,xa_cmpxchg (xa,12345678 ,FIVE ,LOTS ,GFP_KERNEL ) !=FIVE ); +XA_BUG_ON (xa,xa_cmpxchg (xa,5 ,FIVE ,NULL ,GFP_KERNEL ) !=NULL ); +XA_BUG_ON (xa,xa_cmpxchg (xa,5 ,NULL ,FIVE ,GFP_KERNEL ) !=NULL ); +xa_erase_value (xa,12345678 ); +xa_erase_value (xa,5 ); +XA_BUG_ON (xa, !xa_empty (xa)); +} +static void check_multi_store (struct xarray *xa) {unsigned long i, j, k;
@@ -248,6 +267,7 @@ static int xarray_checks(void)check_xa_load (&array);check_xa_tag (&array);check_xa_shrink (&array); +check_cmpxchg (&array);check_multi_store (&array);printk ("XArray: %u of %u tests passed\n" , tests_passed, tests_run);
lib/xarray.c +71 -0
77 lines not on this page · raw message
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 13/74 xarray: Add XArray iterators
In reply to Matthew WilcoxThe xa_for_each iterator allows the user to efficiently walk a range of the array, executing the loop body once for each entry in that range that matches the filter. This commit also includes xa_find() and xa_find_above() which are helper functions for xa_for_each() but may also be useful in their own right.
In the xas family of functions, we have xas_for_each(), xas_find(), xas_next_entry(), xas_for_each_tagged(), xas_find_tagged(), xas_next_tagged() and xas_pause().
--- include/linux/xarray.h | 169 +++++++++++++++++++++++++ lib/test_xarray.c | 128 +++++++++++++++++++ lib/xarray.c | 274 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 571 insertions(+)
include/linux/xarray.h +169 -0
@@ -223,6 +223,10 @@ void *xa_cmpxchg(struct xarray *, unsigned long index,bool xa_get_tag (struct xarray *,unsigned long index,xa_tag_t );void xa_set_tag (struct xarray *,unsigned long index,xa_tag_t );void xa_clear_tag (struct xarray *,unsigned long index,xa_tag_t ); +void *xa_find (struct xarray *xa,unsigned long *index, +unsigned long max,xa_tag_t ) __attribute__((nonnull (2 ))); +void *xa_find_after (struct xarray *xa,unsigned long *index, +unsigned long max,xa_tag_t ) __attribute__((nonnull (2 )));/** * xa_init() - Initialise an empty XArray.
@@ -307,6 +311,35 @@ static inline int xa_insert(struct xarray *xa, unsigned long index,return -EEXIST ; } +/** +* xa_for_each() - Iterate over a portion of an XArray. +* @xa: XArray. +* @entry: Entry retrieved from array. +* @index: Index of @entry. +* @max: Maximum index to retrieve from array. +* @filter: Selection criterion. +* +* Initialise @index to the lowest index you want to retrieve from the +* array. During the iteration, @entry will have the value of the entry +* stored in @xa at @index. The iteration will skip all entries in the +* array which do not match @filter. You may modify @index during the +* iteration if you want to skip or reprocess indices. It is safe to modify +* the array during the iteration. At the end of the iteration, @entry will +* be set to NULL and @index will have a value less than or equal to max. +* +* xa_for_each() is O(n.log(n)) while xas_for_each() is O(n). You have +* to handle your own locking with xas_for_each(), and if you have to unlock +* after each iteration, it will also end up being O(n.log(n)). xa_for_each() +* will spin if it hits a retry entry; if you intend to see retry entries, +* you should use the xas_for_each() iterator instead. The xas_for_each() +* iterator will expand into more inline code than xa_for_each(). +* +* Context: Any context. Takes and releases the RCU lock. +*/ +#define xa_for_each (xa, entry, index, max, filter) \ + for (entry = xa_find(xa, &index, max, filter); entry; \ + entry = xa_find_after(xa, &index, max, filter)) +#define xa_trylock (xa) spin_trylock(&(xa)->xa_lock)#define xa_lock (xa) spin_lock(&(xa)->xa_lock)#define xa_unlock (xa) spin_unlock(&(xa)->xa_lock)
@@ -726,13 +759,16 @@ static inline bool xas_retry(struct xa_state *xas, const void *entry)void *xas_load (struct xa_state *);void *xas_store (struct xa_state *,void *entry); +void *xas_find (struct xa_state *,unsigned long max);bool xas_get_tag (const struct xa_state *,xa_tag_t );void xas_set_tag (const struct xa_state *,xa_tag_t );void xas_clear_tag (const struct xa_state *,xa_tag_t ); +void *xas_find_tagged (struct xa_state *,unsigned long max,xa_tag_t );void xas_init_tags (const struct xa_state *);bool xas_nomem (struct xa_state *,gfp_t ); +void xas_pause (struct xa_state *);/** * xas_reload() - Refetch an entry from the xarray.
@@ -805,6 +841,139 @@ static inline void xas_set_update(struct xa_state *xas, xa_update_node_t update) xas->xa_update = update; } +/* Skip over any of these entries when iterating */ +static inline bool xa_iter_skip (const void *entry) +{ +return unlikely (!entry || + (xa_is_internal (entry) && entry <XA_RETRY_ENTRY )); +} + +/** +* xas_next_entry() - Advance iterator to next present entry. +* @xas: XArray operation state. +* @max: Highest index to return. +* +* xas_next_entry() is an inline function to optimise xarray traversal for +* speed. It is equivalent to calling xas_find(), and will call xas_find() +* for all the hard cases. +* +* Return: The next present entry after the one currently referred to by @xas. +*/ +static inline void *xas_next_entry (struct xa_state *xas,unsigned long max) +{ +struct xa_node *node = xas->xa_node; +void *entry; + +if (unlikely (xas_not_node (node) || node->shift)) +return xas_find (xas, max); + +do { +if (unlikely (xas->xa_index >= max)) +return xas_find (xas, max); +if (unlikely (xas->xa_offset ==XA_CHUNK_MASK )) +return xas_find (xas, max); + xas->xa_index++; + xas->xa_offset++; + entry =xa_entry (xas->xa, node, xas->xa_offset); + }while (xa_iter_skip (entry)); + +return entry; +} + +/* Private */ +static inline unsigned int xas_find_chunk (struct xa_state *xas,bool advance, +xa_tag_t tag) +{ +unsigned long *addr = xas->xa_node->tags[(__force unsigned )tag]; +unsigned int offset = xas->xa_offset; + +if (advance) + offset++; +if (XA_CHUNK_SIZE ==BITS_PER_LONG ) { +if (offset <XA_CHUNK_SIZE ) { +unsigned long data = *addr & (~0UL << offset); +if (data) +return __ffs (data); + } +return XA_CHUNK_SIZE ; + } + +return find_next_bit (addr,XA_CHUNK_SIZE , offset); +} + +/** +* xas_next_tagged() - Advance iterator to next tagged entry. +* @xas: XArray operation state. +* @max: Highest index to return. +* @tag: Tag to search for. +* +* xas_next_tagged() is an inline function to optimise xarray traversal for +* speed. It is equivalent to calling xas_find_tagged(), and will call +* xas_find_tagged() for all the hard cases. +* +* Return: The next tagged entry after the one currently referred to by @xas. +*/ +static inline void *xas_next_tagged (struct xa_state *xas,unsigned long max, +xa_tag_t tag) +{ +struct xa_node *node = xas->xa_node; +unsigned int offset; + +if (unlikely (xas_not_node (node) || node->shift)) +return xas_find_tagged (xas, max, tag); + offset =xas_find_chunk (xas, true, tag); + xas->xa_offset = offset; + xas->xa_index = (xas->xa_index & ~XA_CHUNK_MASK ) + offset; +if (xas->xa_index > max) +return NULL ; +if (offset ==XA_CHUNK_SIZE ) +return xas_find_tagged (xas, max, tag); +return xa_entry (xas->xa, node, offset); +} + +/* +* If iterating while holding a lock, drop the lock and reschedule +* every %XA_CHECK_SCHED loops. +*/ +enum { +XA_CHECK_SCHED =4096 , +}; + +/** +* xas_for_each() - Iterate over a range of an XArray. +* @xas: XArray operation state. +* @entry: Entry retrieved from the array. +* @max: Maximum index to retrieve from array. +* +* The loop body will be executed for each entry present in the xarray +* between the current xas position and @max. @entry will be set to +* the entry retrieved from the xarray. It is safe to delete entries +* from the array in the loop body. You should hold either the RCU lock +* or the xa_lock while iterating. If you need to drop the lock, call +* xas_pause() first. +*/ +#define xas_for_each (xas, entry, max) \ + for (entry = xas_find(xas, max); entry; \ + entry = xas_next_entry(xas, max)) + +/** +* xas_for_each_tagged() - Iterate over a range of an XArray. +* @xas: XArray operation state. +* @entry: Entry retrieved from the array. +* @max: Maximum index to retrieve from array. +* @tag: Tag to search for. +* +* The loop body will be executed for each tagged entry in the xarray +* between the current xas position and @max. @entry will be set to +* the entry retrieved from the xarray. It is safe to delete entries +* from the array in the loop body. You should hold either the RCU lock +* or the xa_lock while iterating. If you need to drop the lock, call +* xas_pause() first. +*/ +#define xas_for_each_tagged (xas, entry, max, tag) \ + for (entry = xas_find_tagged(xas, max, tag); entry; \ + entry = xas_next_tagged(xas, max, tag)) +/* Internal functions, mostly shared between radix-tree.c, xarray.c and idr.c */ void xas_destroy (struct xa_state *);
lib/test_xarray.c +128 -0
156 lines not on this page · raw message
lib/xarray.c +274 -0
298 lines not on this page · raw message
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 14/74 xarray: Extract entries from an XArray
In reply to Matthew WilcoxThe xa_extract function combines the functionality of radix_tree_gang_lookup() and radix_tree_gang_lookup_tagged(). It extracts entries matching the specified filter into a normal array.
--- include/linux/xarray.h | 2 ++ lib/xarray.c | 80 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+)
include/linux/xarray.h +2 -0
@@ -227,6 +227,8 @@ void *xa_find(struct xarray *xa, unsigned long *index, unsigned long max, xa_tag_t) __attribute__((nonnull(2)));void *xa_find_after (struct xarray *xa,unsigned long *index,unsigned long max,xa_tag_t ) __attribute__((nonnull (2 ))); +unsigned int xa_extract (struct xarray *,void **dst,unsigned long start, +unsigned long max,unsigned int n,xa_tag_t );/** * xa_init() - Initialise an empty XArray.
lib/xarray.c +80 -0
86 lines not on this page · raw message
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 15/74 xarray: Destroy an XArray
In reply to Matthew WilcoxThis function frees all the internal memory allocated to the xarray and reinitialises it to be empty.
--- include/linux/xarray.h | 1 + lib/xarray.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+)
include/linux/xarray.h +1 -0
@@ -229,6 +229,7 @@ void *xa_find_after(struct xarray *xa, unsigned long *index, unsigned long max, xa_tag_t) __attribute__((nonnull(2)));unsigned int xa_extract (struct xarray *,void **dst,unsigned long start,unsigned long max,unsigned int n,xa_tag_t ); +void xa_destroy (struct xarray *);/** * xa_init() - Initialise an empty XArray.
lib/xarray.c +28 -0
@@ -1493,6 +1493,34 @@ unsigned int xa_extract(struct xarray *xa, void **dst, unsigned long start, }EXPORT_SYMBOL (xa_extract); +/** +* xa_destroy() - Free all internal data structures. +* @xa: XArray. +* +* After calling this function, the XArray is empty and has freed all memory +* allocated for its internal data structures. You are responsible for +* freeing the objects referenced by the XArray. +* +* Context: Any context. Takes and releases the xa_lock, interrupt-safe. +*/ +void xa_destroy (struct xarray *xa) +{ +XA_STATE (xas, xa,0 ); +unsigned long flags; +void *entry; + + xas.xa_node =NULL ; +xas_lock_irqsave (&xas, flags); + entry =xa_head_locked (xa); +RCU_INIT_POINTER (xa->xa_head,NULL ); +xas_init_tags (&xas); +/* lockdep checks we're still holding the lock in xas_free_nodes() */ +if (xa_is_node (entry)) +xas_free_nodes (&xas,xa_to_node (entry)); +xas_unlock_irqrestore (&xas, flags); +} +EXPORT_SYMBOL (xa_destroy); +#ifdef XA_DEBUG void xa_dump_node (const struct xa_node *node) {
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 16/74 xarray: Step through an XArray
In reply to Matthew WilcoxThe xas_next and xas_prev functions move the xas index by one position, and adjust the rest of the iterator state to match it. This is more efficient than calling xas_set() as it keeps the iterator at the leaves of the tree instead of walking the iterator from the root each time.
--- include/linux/xarray.h | 67 +++++++++++++++++++++++++ lib/test_xarray.c | 109 +++++++++++++++++++++++++++++++++++++++++ lib/xarray.c | 74 ++++++++++++++++++++++++++++ 3 files changed, 250 insertions(+)
include/linux/xarray.h +67 -0
@@ -719,6 +719,12 @@ static inline bool xas_not_node(struct xa_node *node)return ((unsigned long )node &3 ) || !node; } +/* True if the node represents RESTART or an error */ +static inline bool xas_frozen (struct xa_node *node) +{ +return (unsigned long )node &2 ; +} +/* True if the node represents head-of-tree, RESTART or BOUNDS */ static inline bool xas_top (struct xa_node *node) {
@@ -977,6 +983,67 @@ enum {for (entry =xas_find_tagged (xas, max, tag); entry; \ entry =xas_next_tagged (xas, max, tag)) +void *__xas_next(struct xa_state *); +void *__xas_prev (struct xa_state *); + +/** +* xas_prev() - Move iterator to previous index. +* @xas: XArray operation state. +* +* If the @xas was in an error state, it will remain in an error state +* and this function will return %NULL. If the @xas has never been walked, +* it will have the effect of calling xas_load(). Otherwise one will be +* subtracted from the index and the state will be walked to the correct +* location in the array for the next operation. +* +* If the iterator was referencing index 0, this function wraps +* around to %ULONG_MAX. +* +* Return: The entry at the new index. This may be %NULL or an internal +* entry. +*/ +static inline void *xas_prev (struct xa_state *xas) +{ +struct xa_node *node = xas->xa_node; + +if (unlikely (xas_not_node (node) || node->shift || + xas->xa_offset ==0 )) +return __xas_prev (xas); + + xas->xa_index--; + xas->xa_offset--; +return xa_entry (xas->xa, node, xas->xa_offset); +} + +/** +* xas_next() - Move state to next index. +* @xas: XArray operation state. +* +* If the @xas was in an error state, it will remain in an error state +* and this function will return %NULL. If the @xas has never been walked, +* it will have the effect of calling xas_load(). Otherwise one will be +* added to the index and the state will be walked to the correct +* location in the array for the next operation. +* +* If the iterator was referencing index %ULONG_MAX, this function wraps +* around to 0. +* +* Return: The entry at the new index. This may be %NULL or an internal +* entry. +*/ +static inline void *xas_next (struct xa_state *xas) +{ +struct xa_node *node = xas->xa_node; + +if (unlikely (xas_not_node (node) || node->shift || + xas->xa_offset ==XA_CHUNK_MASK )) +return __xas_next (xas); + + xas->xa_index++; + xas->xa_offset++; +return xa_entry (xas->xa, node, xas->xa_offset); +} +/* Internal functions, mostly shared between radix-tree.c, xarray.c and idr.c */ void xas_destroy (struct xa_state *);
lib/test_xarray.c +109 -0
121 lines not on this page · raw message
lib/xarray.c +74 -0
80 lines not on this page · raw message
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 17/74 xarray: Add xas_for_each_conflict
In reply to Matthew WilcoxThis iterator iterates over each entry that is stored in the index or indices specified by the xa_state. This is intended for use for a conditional store of a multiindex entry, or to allow entries which are about to be removed from the xarray to be disposed of properly.
--- include/linux/xarray.h | 17 ++++++++++++ lib/test_xarray.c | 63 ++++++++++++++++++++++++++++++++++++++++++ lib/xarray.c | 61 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 141 insertions(+)
include/linux/xarray.h +17 -0
@@ -769,6 +769,7 @@ static inline bool xas_retry(struct xa_state *xas, const void *entry)void *xas_load (struct xa_state *);void *xas_store (struct xa_state *,void *entry);void *xas_find (struct xa_state *,unsigned long max); +void *xas_next_conflict (struct xa_state *);bool xas_get_tag (const struct xa_state *,xa_tag_t );void xas_set_tag (const struct xa_state *,xa_tag_t );
@@ -983,6 +984,22 @@ enum {for (entry =xas_find_tagged (xas, max, tag); entry; \ entry =xas_next_tagged (xas, max, tag)) +/** +* xas_for_each_conflict() - Iterate over a range of an XArray. +* @xas: XArray operation state. +* @entry: Entry retrieved from the array. +* +* The loop body will be executed for each entry in the XArray that lies +* within the range specified by @xas. If the loop completes successfully, +* any entries that lie in this range will be replaced by @entry. The caller +* may break out of the loop; if they do so, the contents of the XArray will +* be unchanged. The operation may fail due to an out of memory condition. +* The caller may also call xa_set_err() to exit the loop while setting an +* error to record the reason. + */ +#define xas_for_each_conflict (xas, entry) \ + while ((entry = xas_next_conflict(xas))) +void *__xas_next (struct xa_state *);void *__xas_prev (struct xa_state *);
lib/test_xarray.c +63 -0
75 lines not on this page · raw message
lib/xarray.c +61 -0
67 lines not on this page · raw message
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 18/74 xarray: Add xas_create_range
In reply to Matthew WilcoxThis hopefully temporary function is useful for users who have not yet been converted to multi-index entries.
--- include/linux/xarray.h | 2 ++ lib/xarray.c | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+)
include/linux/xarray.h +2 -0
@@ -780,6 +780,8 @@ void xas_init_tags(const struct xa_state *);bool xas_nomem (struct xa_state *,gfp_t );void xas_pause (struct xa_state *); +void xas_create_range (struct xa_state *,unsigned long max); +/** * xas_reload() - Refetch an entry from the xarray. * @xas: XArray operation state.
lib/xarray.c +22 -0
@@ -624,6 +624,28 @@ static void *xas_create(struct xa_state *xas)return entry; } +/** +* xas_create_range() - Ensure that stores to this range will succeed +* @xas: XArray operation state. +* @max: The highest index to create a slot for. +* +* Creates all of the slots in the range between the current position of +* @xas and @max. This is for the benefit of users who have not yet been +* converted to multi-index entries. +* +* The implementation is naive. +*/ +void xas_create_range (struct xa_state *xas,unsigned long max) +{ +XA_STATE (tmp, xas->xa, xas->xa_index); + +do { +xas_create (&tmp); +xas_set (&tmp, tmp.xa_index +XA_CHUNK_SIZE ); + }while (tmp.xa_index < max); +} +EXPORT_SYMBOL_GPL (xas_create_range); +static void update_node (struct xa_state *xas,struct xa_node *node,int count,int values) {
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 19/74 xarray: Add MAINTAINERS entry
In reply to Matthew WilcoxAdd myself as XArray and IDR maintainer.
--- MAINTAINERS | 11 +++++++++++ 1 file changed, 11 insertions(+)
MAINTAINERS +11 -0
@@ -15596,6 +15596,17 @@ T: git git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip.git x86/vdso S: Maintained F: arch/x86/entry/vdso/ +XARRAY +M: Matthew Wilcox <[email protected]> +L: [email protected] +S: Supported +F: Documentation/core-api/xarray.rst +F: lib/idr.c +F: lib/xarray.c +F: include/linux/idr.h +F: include/linux/xarray.h +F: tools/testing/radix-tree + XC2028/3028 TUNER DRIVER M: Mauro Carvalho Chehab <[email protected]> L: [email protected]
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 20/74 page cache: Rearrange address_space
In reply to Matthew WilcoxChange i_pages from a radix_tree_root to an xarray, convert the documentation into kernel-doc format and change the order of the elements to pack them better on 64-bit systems.
--- include/linux/fs.h | 46 +++++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 15 deletions(-)
include/linux/fs.h +31 -15
@@ -396,24 +396,40 @@ int pagecache_write_end(struct file *, struct address_space *mapping, loff_t pos, unsigned len, unsigned copied, struct page *page, void *fsdata); +/** +* struct address_space - Contents of a cacheable, mappable object. +* @host: Owner, either the inode or the block_device. +* @i_pages: Cached pages. +* @gfp_mask: Memory allocation flags to use for allocating pages. +* @i_mmap_writable: Number of VM_SHARED mappings. +* @i_mmap: Tree of private and shared mappings. +* @i_mmap_rwsem: Protects @i_mmap and @i_mmap_writable. +* @nrpages: Number of page entries, protected by the i_pages lock. +* @nrexceptional: Shadow or DAX entries, protected by the i_pages lock. +* @writeback_index: Writeback starts here. +* @a_ops: Methods. +* @flags: Error bits and flags (AS_*). +* @wb_err: The most recent error which has occurred. +* @private_lock: For use by the owner of the address_space. +* @private_list: For use by the owner of the address_space. +* @private_data: For use by the owner of the address_space. +*/ struct address_space { -struct inode *host;/* owner: inode, block_device */ -struct radix_tree_root i_pages;/* cached pages */ -atomic_t i_mmap_writable;/* count VM_SHARED mappings */ -struct rb_root_cached i_mmap;/* tree of private and shared mappings */ -struct rw_semaphore i_mmap_rwsem;/* protect tree, count, list */ -/* Protected by the i_pages lock */ -unsigned long nrpages;/* number of total pages */ -/* number of shadow or DAX exceptional entries */ +struct inode *host; +struct xarray i_pages; +gfp_t gfp_mask; +atomic_t i_mmap_writable; +struct rb_root_cached i_mmap; +struct rw_semaphore i_mmap_rwsem; +unsigned long nrpages;unsigned long nrexceptional; -pgoff_t writeback_index;/* writeback starts here */ -const struct address_space_operations *a_ops;/* methods */ -unsigned long flags;/* error bits */ -spinlock_t private_lock;/* for use by the address_space */ -gfp_t gfp_mask;/* implicit gfp mask for allocations */ -struct list_head private_list;/* for use by the address_space */ -void *private_data;/* ditto */ +pgoff_t writeback_index; +const struct address_space_operations *a_ops; +unsigned long flags;errseq_t wb_err; +spinlock_t private_lock; +struct list_head private_list; +void *private_data; } __attribute__((aligned (sizeof (long )))) __randomize_layout;/* * On most architectures that alignment is already the case; but
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 21/74 page cache: Convert hole search to XArray
In reply to Matthew WilcoxThe page cache offers the ability to search for a miss in the previous or next N locations. Rather than teach the XArray about the page cache's definition of a miss, use xas_prev() and xas_next() to search the page array. This should be more efficient as it does not have to start the lookup from the top for each index.
--- fs/nfs/blocklayout/blocklayout.c | 2 +- include/linux/pagemap.h | 4 +- mm/filemap.c | 110 ++++++++++++++----------------- mm/readahead.c | 4 +- 4 files changed, 55 insertions(+), 65 deletions(-)
fs/nfs/blocklayout/blocklayout.c +1 -1
@@ -895,7 +895,7 @@ static u64 pnfs_num_cont_bytes(struct inode *inode, pgoff_t idx) end =DIV_ROUND_UP (i_size_read (inode),PAGE_SIZE );if (end != inode->i_mapping->nrpages) {rcu_read_lock (); - end =page_cache_next_hole (mapping, idx +1 ,ULONG_MAX ); + end =page_cache_next_gap (mapping, idx +1 ,ULONG_MAX );rcu_read_unlock (); }
include/linux/pagemap.h +2 -2
@@ -241,9 +241,9 @@ static inline gfp_t readahead_gfp_mask(struct address_space *x)typedef int filler_t (void *,struct page *); -pgoff_t page_cache_next_hole (struct address_space *mapping, +pgoff_t page_cache_next_gap (struct address_space *mapping,pgoff_t index,unsigned long max_scan); -pgoff_t page_cache_prev_hole (struct address_space *mapping, +pgoff_t page_cache_prev_gap (struct address_space *mapping,pgoff_t index,unsigned long max_scan);#define FGP_ACCESSED 0x00000001
mm/filemap.c +50 -60
136 lines not on this page · raw message
mm/readahead.c +2 -2
@@ -334,7 +334,7 @@ static pgoff_t count_history_pages(struct address_space *mapping,pgoff_t head;rcu_read_lock (); - head =page_cache_prev_hole (mapping, offset -1 , max); + head =page_cache_prev_gap (mapping, offset -1 , max);rcu_read_unlock ();return offset -1 - head;
@@ -422,7 +422,7 @@ ondemand_readahead(struct address_space *mapping,pgoff_t start;rcu_read_lock (); - start =page_cache_next_hole (mapping, offset +1 , max_pages); + start =page_cache_next_gap (mapping, offset +1 , max_pages);rcu_read_unlock ();if (!start || start - offset > max_pages)
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 22/74 page cache: Add and replace pages using the XArray
In reply to Matthew WilcoxUse the XArray APIs to add and replace pages in the page cache. This removes two uses of the radix tree preload API and is significantly shorter code. It also removes the last user of __radix_tree_create() outside radix-tree.c itself, so make it static.
--- include/linux/radix-tree.h | 3 - include/linux/swap.h | 8 ++- lib/radix-tree.c | 6 +- mm/filemap.c | 139 +++++++++++++++---------------------- 4 files changed, 66 insertions(+), 90 deletions(-)
include/linux/radix-tree.h +0 -3
@@ -231,9 +231,6 @@ static inline int radix_tree_exception(void *arg)return unlikely ((unsigned long )arg &RADIX_TREE_ENTRY_MASK ); } -int __radix_tree_create (struct radix_tree_root *,unsigned long index, -unsigned order,struct radix_tree_node **nodep, -void __rcu ***slotp);int __radix_tree_insert (struct radix_tree_root *,unsigned long index,unsigned order,void *); static inline int radix_tree_insert(struct radix_tree_root *root,
include/linux/swap.h +6 -2
@@ -300,8 +300,12 @@ void *workingset_eviction(struct address_space *mapping, struct page *page);bool workingset_refault (void *shadow);void workingset_activation (struct page *page); -/* Do not use directly, use workingset_lookup_update */ -void workingset_update_node (struct radix_tree_node *node); +/* Only track the nodes of mappings with shadow entries */ +void workingset_update_node (struct xa_node *node); +#define mapping_set_update (xas, mapping) do { \ + if (!dax_mapping(mapping) && !shmem_mapping(mapping)) \ + xas_set_update(xas, workingset_update_node); \ +} while (0)/* Returns workingset_update_node() if the mapping has shadow entries. */ #define workingset_lookup_update (mapping) \
lib/radix-tree.c +3 -3
@@ -740,9 +740,9 @@ static bool delete_node(struct radix_tree_root *root,* * Returns -ENOMEM, or 0 for success. */ -int __radix_tree_create (struct radix_tree_root *root,unsigned long index, -unsigned order,struct radix_tree_node **nodep, -void __rcu ***slotp) +static int __radix_tree_create (struct radix_tree_root *root, +unsigned long index,unsigned order, +struct radix_tree_node **nodep,void __rcu ***slotp) {struct radix_tree_node *node =NULL , *child;void __rcu **slot = (void __rcu **)&root->xa_head;
mm/filemap.c +57 -82
194 lines not on this page · raw message
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 23/74 page cache: Convert page deletion to XArray
In reply to Matthew WilcoxThe code is slightly shorter and simpler.
--- mm/filemap.c | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-)
mm/filemap.c +13 -18
@@ -111,31 +111,26 @@* ->tasklist_lock (memory_failure, collect_procs_ao) */ -static void page_cache_tree_delete (struct address_space *mapping, +static void page_cache_delete (struct address_space *mapping,struct page *page,void *shadow) { -int i, nr; +XA_STATE (xas, &mapping->i_pages, page->index); +unsigned int nr =1 ; -/* hugetlb pages are represented by one entry in the radix tree */ - nr =PageHuge (page) ?1 :hpage_nr_pages (page); +mapping_set_update (&xas, mapping); + +/* hugetlb pages are represented by a single entry in the xarray */ +if (!PageHuge (page)) { +xas_set_order (&xas, page->index,compound_order (page)); + nr =1U <<compound_order (page); + }VM_BUG_ON_PAGE (!PageLocked (page), page);VM_BUG_ON_PAGE (PageTail (page), page);VM_BUG_ON_PAGE (nr !=1 && shadow, page); -for (i =0 ; i < nr; i++) { -struct radix_tree_node *node; -void **slot; - -__radix_tree_lookup (&mapping->i_pages, page->index + i, - &node, &slot); - -VM_BUG_ON_PAGE (!node && nr !=1 , page); - -radix_tree_clear_tags (&mapping->i_pages, node, slot); -__radix_tree_replace (&mapping->i_pages, node, slot, shadow, -workingset_lookup_update (mapping)); - } +xas_store (&xas, shadow); +xas_init_tags (&xas); page->mapping =NULL ;/* Leave page->index set: truncation lookup relies upon it */
@@ -234,7 +229,7 @@ void __delete_from_page_cache(struct page *page, void *shadow)trace_mm_filemap_delete_from_page_cache (page);unaccount_page_cache_page (mapping, page); -page_cache_tree_delete (mapping, page, shadow); +page_cache_delete (mapping, page, shadow); } static void page_cache_free_page(struct address_space *mapping,
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 24/74 page cache: Convert find_get_entry to XArray
In reply to Matthew WilcoxSlightly shorter and simpler code.
--- mm/filemap.c | 63 +++++++++++++++++++++++----------------------------- 1 file changed, 28 insertions(+), 35 deletions(-)
mm/filemap.c +28 -35
@@ -1382,47 +1382,40 @@ EXPORT_SYMBOL(page_cache_prev_gap);*/ struct page *find_get_entry (struct address_space *mapping,pgoff_t offset) { -void **pagep; +XA_STATE (xas, &mapping->i_pages, offset);struct page *head, *page;rcu_read_lock (); repeat: - page =NULL ; - pagep =radix_tree_lookup_slot (&mapping->i_pages, offset); -if (pagep) { - page =radix_tree_deref_slot (pagep); -if (unlikely (!page)) - goto out; -if (radix_tree_exception (page)) { -if (radix_tree_deref_retry (page)) - goto repeat; -/* -* A shadow entry of a recently evicted page, -* or a swap entry from shmem/tmpfs. Return -* it without attempting to raise page count. -*/ - goto out; - } +xas_reset (&xas); + page =xas_load (&xas); +if (xas_retry (&xas, page)) + goto repeat; +/* +* A shadow entry of a recently evicted page, or a swap entry from +* shmem/tmpfs. Return it without attempting to raise page count. +*/ +if (!page ||xa_is_value (page)) + goto out; - head =compound_head (page); -if (!page_cache_get_speculative (head)) - goto repeat; + head =compound_head (page); +if (!page_cache_get_speculative (head)) + goto repeat; -/* The page was split under us? */ -if (compound_head (page) != head) { -put_page (head); - goto repeat; - } +/* The page was split under us? */ +if (compound_head (page) != head) { +put_page (head); + goto repeat; + } -/* -* Has the page moved? -* This is part of the lockless pagecache protocol. See -* include/linux/pagemap.h for details. -*/ -if (unlikely (page != *pagep)) { -put_page (head); - goto repeat; - } +/* +* Has the page moved? +* This is part of the lockless pagecache protocol. See +* include/linux/pagemap.h for details. +*/ +if (unlikely (page !=xas_reload (&xas))) { +put_page (head); + goto repeat; } out:rcu_read_unlock ();
@@ -1453,7 +1446,7 @@ struct page *find_lock_entry(struct address_space *mapping, pgoff_t offset) repeat: page =find_get_entry (mapping, offset); -if (page && !radix_tree_exception (page)) { +if (page && !xa_is_value (page)) {lock_page (page);/* Has the page been truncated? */ if (unlikely (page_mapping (page) != mapping)) {
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 25/74 page cache: Convert find_get_entries to XArray
In reply to Matthew WilcoxSlightly shorter and simpler code.
--- mm/filemap.c | 51 +++++++++++++++++++++++---------------------------- 1 file changed, 23 insertions(+), 28 deletions(-)
mm/filemap.c +23 -28
@@ -1578,53 +1578,48 @@ unsigned find_get_entries(struct address_space *mapping, pgoff_t start, unsigned int nr_entries, struct page **entries, pgoff_t *indices) { - void **slot; + XA_STATE(xas, &mapping->i_pages, start); +struct page *page;unsigned int ret =0 ; -struct radix_tree_iter iter;if (!nr_entries)return 0 ;rcu_read_lock (); -radix_tree_for_each_slot (slot, &mapping->i_pages, &iter, start) { -struct page *head, *page; -repeat: - page =radix_tree_deref_slot (slot); -if (unlikely (!page)) +xas_for_each (&xas, page,ULONG_MAX ) { +struct page *head; +if (xas_retry (&xas, page))continue ; -if (radix_tree_exception (page)) { -if (radix_tree_deref_retry (page)) { - slot =radix_tree_iter_retry (&iter); -continue ; - } -/* -* A shadow entry of a recently evicted page, a swap -* entry from shmem/tmpfs or a DAX entry. Return it -* without attempting to raise page count. -*/ +/* +* A shadow entry of a recently evicted page, a swap +* entry from shmem/tmpfs or a DAX entry. Return it +* without attempting to raise page count. +*/ +if (xa_is_value (page)) goto export; - } head =compound_head (page);if (!page_cache_get_speculative (head)) - goto repeat; + goto retry;/* The page was split under us? */ -if (compound_head (page) != head) { -put_page (head); - goto repeat; - } +if (compound_head (page) != head) + goto put_page;/* Has the page moved? */ -if (unlikely (page != *slot)) { -put_page (head); - goto repeat; - } +if (unlikely (page !=xas_reload (&xas))) + goto put_page; + export: - indices[ret] = iter.index; + indices[ret] = xas.xa_index; entries[ret] = page;if (++ret == nr_entries)break ; +continue ; +put_page: +put_page (head); +retry: +xas_reset (&xas); }rcu_read_unlock ();return ret;
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 26/74 page cache: Convert find_get_pages_range to XArray
In reply to Matthew WilcoxThe 'end' parameter of the xas_for_each iterator avoids a useless iteration at the end of the range.
--- mm/filemap.c | 52 +++++++++++++++++++--------------------------------- 1 file changed, 19 insertions(+), 33 deletions(-)
mm/filemap.c +19 -33
@@ -1650,64 +1650,50 @@ unsigned find_get_pages_range(struct address_space *mapping, pgoff_t *start, pgoff_t end, unsigned int nr_pages, struct page **pages) { - struct radix_tree_iter iter; -void **slot; + XA_STATE(xas, &mapping->i_pages, *start); +struct page *page;unsigned ret =0 ;if (unlikely (!nr_pages))return 0 ;rcu_read_lock (); -radix_tree_for_each_slot (slot, &mapping->i_pages, &iter, *start) { -struct page *head, *page; - -if (iter.index > end) -break ; -repeat: - page =radix_tree_deref_slot (slot); -if (unlikely (!page)) +xas_for_each (&xas, page, end) { +struct page *head; +if (xas_retry (&xas, page))continue ; - -if (radix_tree_exception (page)) { -if (radix_tree_deref_retry (page)) { - slot =radix_tree_iter_retry (&iter); -continue ; - } -/* -* A shadow entry of a recently evicted page, -* or a swap entry from shmem/tmpfs. Skip -* over it. -*/ +/* Skip over shadow, swap and DAX entries */ +if (xa_is_value (page))continue ; - } head =compound_head (page);if (!page_cache_get_speculative (head)) - goto repeat; + goto retry;/* The page was split under us? */ -if (compound_head (page) != head) { -put_page (head); - goto repeat; - } +if (compound_head (page) != head) + goto put_page;/* Has the page moved? */ -if (unlikely (page != *slot)) { -put_page (head); - goto repeat; - } +if (unlikely (page !=xas_reload (&xas))) + goto put_page; pages[ret] = page;if (++ret == nr_pages) { - *start = pages[ret -1 ]->index +1 ; + *start = page->index +1 ; goto out; } +continue ; +put_page: +put_page (head); +retry: +xas_reset (&xas); }/* * We come here when there is no page beyond @end. We take care to not * overflow the index @start as it confuses some of the callers. This -* breaks the iteration when there is page at index -1 but that is +* breaks the iteration when there is a page at index -1 but that is * already broken anyway. */ if (end == (pgoff_t)-1 )
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 27/74 page cache: Convert find_get_pages_contig to XArray
In reply to Matthew WilcoxThere's no direct replacement for radix_tree_for_each_contig() in the XArray API as it's an unusual thing to do. Instead, open-code a loop using xas_next(). This removes the only user of radix_tree_for_each_contig() so delete the iterator from the API and the test suite code for it.
--- .clang-format | 1 - include/linux/radix-tree.h | 17 --------- mm/filemap.c | 53 +++++++++++--------------- tools/testing/radix-tree/regression3.c | 23 ----------- 4 files changed, 22 insertions(+), 72 deletions(-)
.clang-format +0 -1
@@ -323,7 +323,6 @@ ForEachMacros: - 'protocol_for_each_card' - 'protocol_for_each_dev' - 'queue_for_each_hw_ctx' - - 'radix_tree_for_each_contig' - 'radix_tree_for_each_slot' - 'radix_tree_for_each_tagged' - 'rbtree_postorder_for_each_entry_safe'
include/linux/radix-tree.h +0 -17
@@ -522,23 +522,6 @@ static __always_inline void __rcu **radix_tree_next_slot(void __rcu **slot, slot || (slot =radix_tree_next_chunk (root, iter,0 )) ; \ slot = radix_tree_next_slot(slot, iter, 0)) -/** -* radix_tree_for_each_contig - iterate over contiguous slots -* -* @slot: the void** variable for pointer to slot -* @root: the struct radix_tree_root pointer -* @iter: the struct radix_tree_iter pointer -* @start: iteration starting index -* -* @slot points to radix tree slot, @iter->index contains its index. -*/ -#define radix_tree_for_each_contig (slot, root, iter, start) \ - for (slot = radix_tree_iter_init(iter, start) ; \ - slot || (slot = radix_tree_next_chunk(root, iter, \ - RADIX_TREE_ITER_CONTIG)) ; \ - slot = radix_tree_next_slot(slot, iter, \ - RADIX_TREE_ITER_CONTIG)) -/** * radix_tree_for_each_tagged - iterate over tagged slots *
mm/filemap.c +22 -31
85 lines not on this page · raw message
tools/testing/radix-tree/regression3.c +0 -23
@@ -69,21 +69,6 @@ void regression3_test(void)continue ; } } -radix_tree_delete (&root,1 ); - - first = true; -radix_tree_for_each_contig (slot, &root, &iter,0 ) { -printv (2 ,"contig %ld %p\n" , iter.index, *slot); -if (first) { -radix_tree_insert (&root,1 , ptr); - first = false; - } -if (radix_tree_deref_retry (*slot)) { -printv (2 ,"retry at %ld\n" , iter.index); - slot =radix_tree_iter_retry (&iter); -continue ; - } - }radix_tree_for_each_slot (slot, &root, &iter,0 ) {printv (2 ,"slot %ld %p\n" , iter.index, *slot);
@@ -93,14 +78,6 @@ void regression3_test(void) } } -radix_tree_for_each_contig (slot, &root, &iter,0 ) { -printv (2 ,"contig %ld %p\n" , iter.index, *slot); -if (!iter.index) { -printv (2 ,"next at %ld\n" , iter.index); - slot =radix_tree_iter_resume (slot, &iter); - } - } -radix_tree_tag_set (&root,0 ,0 );radix_tree_tag_set (&root,1 ,0 );radix_tree_for_each_tagged (slot, &root, &iter,0 ,0 ) {
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 28/74 page cache; Convert find_get_pages_range_tag to XArray
In reply to Matthew WilcoxThe 'end' parameter of the xas_for_each iterator avoids a useless iteration at the end of the range.
--- include/linux/pagemap.h | 4 +-- mm/filemap.c | 68 ++++++++++++++++------------------------- 2 files changed, 28 insertions(+), 44 deletions(-)
include/linux/pagemap.h +2 -2
@@ -363,10 +363,10 @@ static inline unsigned find_get_pages(struct address_space *mapping,unsigned find_get_pages_contig (struct address_space *mapping,pgoff_t start,unsigned int nr_pages,struct page **pages);unsigned find_get_pages_range_tag (struct address_space *mapping,pgoff_t *index, -pgoff_t end,int tag,unsigned int nr_pages, +pgoff_t end,xa_tag_t tag,unsigned int nr_pages,struct page **pages);static inline unsigned find_get_pages_tag (struct address_space *mapping, -pgoff_t *index,int tag,unsigned int nr_pages, +pgoff_t *index,xa_tag_t tag,unsigned int nr_pages,struct page **pages) { return find_get_pages_range_tag(mapping, index, (pgoff_t)-1, tag,
mm/filemap.c +26 -42
100 lines not on this page · raw message
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 29/74 page cache: Convert find_get_entries_tag to XArray
In reply to Matthew WilcoxSlightly shorter and simpler code.
--- include/linux/pagemap.h | 2 +- mm/filemap.c | 54 ++++++++++++++++++----------------------- 2 files changed, 25 insertions(+), 31 deletions(-)
include/linux/pagemap.h +1 -1
@@ -373,7 +373,7 @@ static inline unsigned find_get_pages_tag(struct address_space *mapping, nr_pages, pages); }unsigned find_get_entries_tag (struct address_space *mapping,pgoff_t start, -int tag,unsigned int nr_entries, +xa_tag_t tag,unsigned int nr_entries,struct page **entries,pgoff_t *indices); struct page *grab_cache_page_write_begin(struct address_space *mapping,
mm/filemap.c +24 -30
81 lines not on this page · raw message
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 30/74 page cache: Convert filemap_map_pages to XArray
In reply to Matthew WilcoxSlight change of strategy here; if we have trouble getting hold of a page for whatever reason (eg a compound page is split underneath us), don't spin to stabilise the page, just continue the iteration, like we would if we failed to trylock the page. Since this is a speculative optimisation, it feels like we should allow the process to take an extra fault if it turns out to need this page instead of spending time to pin down a page it may not need.
--- mm/filemap.c | 42 +++++++++++++----------------------------- 1 file changed, 13 insertions(+), 29 deletions(-)
mm/filemap.c +13 -29
@@ -2516,45 +2516,31 @@ EXPORT_SYMBOL(filemap_fault);void filemap_map_pages (struct vm_fault *vmf,pgoff_t start_pgoff,pgoff_t end_pgoff) { -struct radix_tree_iter iter; -void **slot;struct file *file = vmf->vma->vm_file;struct address_space *mapping = file->f_mapping;pgoff_t last_pgoff = start_pgoff;unsigned long max_idx; +XA_STATE (xas, &mapping->i_pages, start_pgoff);struct page *head, *page;rcu_read_lock (); -radix_tree_for_each_slot (slot, &mapping->i_pages, &iter, start_pgoff) { -if (iter.index > end_pgoff) -break ; -repeat: - page =radix_tree_deref_slot (slot); -if (unlikely (!page)) - goto next; -if (radix_tree_exception (page)) { -if (radix_tree_deref_retry (page)) { - slot =radix_tree_iter_retry (&iter); -continue ; - } +xas_for_each (&xas, page, end_pgoff) { +if (xas_retry (&xas, page)) +continue ; +if (xa_is_value (page)) goto next; - } head =compound_head (page);if (!page_cache_get_speculative (head)) - goto repeat; + goto next;/* The page was split under us? */ -if (compound_head (page) != head) { -put_page (head); - goto repeat; - } +if (compound_head (page) != head) + goto skip;/* Has the page moved? */ -if (unlikely (page != *slot)) { -put_page (head); - goto repeat; - } +if (unlikely (page !=xas_reload (&xas))) + goto skip; if (!PageUptodate(page) || PageReadahead(page) ||
@@ -2573,10 +2559,10 @@ void filemap_map_pages(struct vm_fault *vmf,if (file->f_ra.mmap_miss >0 ) file->f_ra.mmap_miss--; - vmf->address += (iter.index - last_pgoff) <<PAGE_SHIFT ; + vmf->address += (xas.xa_index - last_pgoff) <<PAGE_SHIFT ;if (vmf->pte) - vmf->pte += iter.index - last_pgoff; - last_pgoff = iter.index; + vmf->pte += xas.xa_index - last_pgoff; + last_pgoff = xas.xa_index;if (alloc_set_pte (vmf,NULL , page)) goto unlock;unlock_page (page);
@@ -2589,8 +2575,6 @@ void filemap_map_pages(struct vm_fault *vmf,/* Huge page is mapped? No need to proceed. */ if (pmd_trans_huge (*vmf->pmd))break ; -if (iter.index == end_pgoff) -break ; }rcu_read_unlock (); }
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 31/74 radix tree test suite: Convert regression1 to XArray
In reply to Matthew WilcoxNow the page cache lookup is using the XArray, let's convert this regression test from the radix tree API to the XArray so it's testing roughly the same thing it was testing before.
--- tools/testing/radix-tree/regression1.c | 58 +++++++++----------------- 1 file changed, 19 insertions(+), 39 deletions(-)
tools/testing/radix-tree/regression1.c +19 -39
@@ -53,12 +53,12 @@ struct page {unsigned long index; }; -static struct page *page_alloc (void ) +static struct page *page_alloc (int index) {struct page *p; p =malloc (sizeof (struct page )); p->count =1 ; - p->index =1 ; + p->index = index;pthread_mutex_init (&p->lock,NULL );return p;
@@ -80,53 +80,33 @@ static void page_free(struct page *p)static unsigned find_get_pages (unsigned long start,unsigned int nr_pages,struct page **pages) { -unsigned int i; -unsigned int ret; -unsigned int nr_found; +XA_STATE (xas, &mt_tree, start); +struct page *page; +unsigned int ret =0 ;rcu_read_lock (); -restart: - nr_found =radix_tree_gang_lookup_slot (&mt_tree, - (void ***)pages,NULL , start, nr_pages); - ret =0 ; -for (i =0 ; i < nr_found; i++) { -struct page *page; -repeat: - page =radix_tree_deref_slot ((void **)pages[i]); -if (unlikely (!page)) +xas_for_each (&xas, page,ULONG_MAX ) { +if (xas_retry (&xas, page))continue ; -if (radix_tree_exception (page)) { -if (radix_tree_deref_retry (page)) { -/* -* Transient condition which can only trigger -* when entry at index 0 moves out of or back -* to root: none yet gotten, safe to restart. -*/ -assert ((start | i) ==0 ); - goto restart; - } -/* -* No exceptional entries are inserted in this test. -*/ -assert (0 ); - } -pthread_mutex_lock (&page->lock); -if (!page->count) { -pthread_mutex_unlock (&page->lock); - goto repeat; - } +if (!page->count) + goto unlock; +/* don't actually update page refcount */ pthread_mutex_unlock (&page->lock);/* Has the page moved? */ -if (unlikely (page != *((void **)pages[i]))) { - goto repeat; - } +if (unlikely (page !=xas_reload (&xas))) + goto put_page; pages[ret] = page; ret++; +continue ; +unlock: +pthread_mutex_unlock (&page->lock); +put_page: +xas_reset (&xas); }rcu_read_unlock ();return ret;
@@ -145,12 +125,12 @@ static void *regression1_fn(void *arg)for (j =0 ; j <1000000 ; j++) {struct page *p; - p =page_alloc (); + p =page_alloc (0 );pthread_mutex_lock (&mt_lock);radix_tree_insert (&mt_tree,0 , p);pthread_mutex_unlock (&mt_lock); - p =page_alloc (); + p =page_alloc (1 );pthread_mutex_lock (&mt_lock);radix_tree_insert (&mt_tree,1 , p);pthread_mutex_unlock (&mt_lock);
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 32/74 page cache: Convert delete_batch to XArray
In reply to Matthew WilcoxRename the function from page_cache_tree_delete_batch to just page_cache_delete_batch.
--- mm/filemap.c | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-)
mm/filemap.c +13 -15
@@ -272,7 +272,7 @@ void delete_from_page_cache(struct page *page)EXPORT_SYMBOL (delete_from_page_cache);/* -* page_cache_tree_delete_batch - delete several pages from page cache +* page_cache_delete_batch - delete several pages from page cache * @mapping: the mapping to which pages belong * @pvec: pagevec with pages to delete *
@@ -285,23 +285,18 @@ EXPORT_SYMBOL(delete_from_page_cache);* * The function expects the i_pages lock to be held. */ -static void -page_cache_tree_delete_batch (struct address_space *mapping, +static void page_cache_delete_batch (struct address_space *mapping,struct pagevec *pvec) { -struct radix_tree_iter iter; -void **slot; +XA_STATE (xas, &mapping->i_pages, pvec->pages[0 ]->index);int total_pages =0 ;int i =0 , tail_pages =0 ;struct page *page; -pgoff_t start; - start = pvec->pages[0 ]->index; -radix_tree_for_each_slot (slot, &mapping->i_pages, &iter, start) { +mapping_set_update (&xas, mapping); +xas_for_each (&xas, page,ULONG_MAX ) {if (i >=pagevec_count (pvec) && !tail_pages)break ; - page =radix_tree_deref_slot_protected (slot, - &mapping->i_pages.xa_lock);if (xa_is_value (page))continue ;if (!tail_pages) {
@@ -310,8 +305,11 @@ page_cache_tree_delete_batch(struct address_space *mapping,* have our pages locked so they are protected from * being removed. */ -if (page != pvec->pages[i]) +if (page != pvec->pages[i]) { +VM_BUG_ON_PAGE (page->index > + pvec->pages[i]->index, page);continue ; + }WARN_ON_ONCE (!PageLocked (page));if (PageTransHuge (page) && !PageHuge (page)) tail_pages =HPAGE_PMD_NR -1 ;
@@ -322,11 +320,11 @@ page_cache_tree_delete_batch(struct address_space *mapping,*/ i++; }else { +VM_BUG_ON_PAGE (page->index +HPAGE_PMD_NR - tail_pages + != pvec->pages[i]->index, page); tail_pages--; } -radix_tree_clear_tags (&mapping->i_pages, iter.node, slot); -__radix_tree_replace (&mapping->i_pages, iter.node, slot,NULL , -workingset_lookup_update (mapping)); +xas_store (&xas,NULL ); total_pages++; } mapping->nrpages -= total_pages;
@@ -347,7 +345,7 @@ void delete_from_page_cache_batch(struct address_space *mapping,unaccount_page_cache_page (mapping, pvec->pages[i]); } -page_cache_tree_delete_batch (mapping, pvec); +page_cache_delete_batch (mapping, pvec);xa_unlock_irqrestore (&mapping->i_pages, flags);for (i =0 ; i <pagevec_count (pvec); i++)
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 33/74 page cache: Remove stray radix comment
In reply to Matthew Wilcox--- mm/filemap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
mm/filemap.c +1 -1
@@ -2682,7 +2682,7 @@ static struct page *do_read_cache_page(struct address_space *mapping,put_page (page);if (err == -EEXIST ) goto repeat; -/* Presumably ENOMEM for radix tree node */ +/* Presumably ENOMEM for xarray node */ return ERR_PTR (err); }
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 34/74 page cache: Convert filemap_range_has_page to XArray
In reply to Matthew WilcoxInstead of calling find_get_pages_range() and putting any reference, use xas_find() to iterate over any entries in the range, skipping the shadow/swap entries.
--- mm/filemap.c | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-)
mm/filemap.c +18 -8
@@ -455,20 +455,30 @@ EXPORT_SYMBOL(filemap_flush);bool filemap_range_has_page (struct address_space *mapping,loff_t start_byte,loff_t end_byte) { -pgoff_t index = start_byte >>PAGE_SHIFT ; -pgoff_t end = end_byte >>PAGE_SHIFT ;struct page *page; +XA_STATE (xas, &mapping->i_pages, start_byte >>PAGE_SHIFT ); +pgoff_t max = end_byte >>PAGE_SHIFT ;if (end_byte < start_byte)return false; -if (mapping->nrpages ==0 ) -return false; +rcu_read_lock (); +do { + page =xas_find (&xas, max); +if (xas_retry (&xas, page)) +continue ; +/* Shadow entries don't count */ +if (xa_is_value (page)) +continue ; +/* +* We don't need to try to pin this page; we're about to +* release the RCU lock anyway. It is enough to know that +* there was a page here recently. +*/ + }while (0 ); +rcu_read_unlock (); -if (!find_get_pages_range (mapping, &index, end,1 , &page)) -return false; -put_page (page); -return true; +return page !=NULL ; }EXPORT_SYMBOL (filemap_range_has_page);
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 35/74 mm: Convert page-writeback to XArray
In reply to Matthew WilcoxIncludes moving mapping_tagged() to fs.h as a static inline, and changing it to return bool.
--- include/linux/fs.h | 17 ++++++----- mm/page-writeback.c | 72 ++++++++++++++++----------------------------- 2 files changed, 36 insertions(+), 53 deletions(-)
include/linux/fs.h +10 -7
@@ -476,15 +476,18 @@ struct block_device {struct mutex bd_fsfreeze_mutex; } __randomize_layout; +/* XArray tags, for tagging dirty and writeback pages in the pagecache. */ +#define PAGECACHE_TAG_DIRTY XA_TAG_0 +#define PAGECACHE_TAG_WRITEBACK XA_TAG_1 +#define PAGECACHE_TAG_TOWRITE XA_TAG_2 +/* -* Radix-tree tags, for tagging dirty and writeback pages within the pagecache -* radix trees +* Returns true if any of the pages in the mapping are marked with the tag. */ -#define PAGECACHE_TAG_DIRTY 0 -#define PAGECACHE_TAG_WRITEBACK 1 -#define PAGECACHE_TAG_TOWRITE 2 - -int mapping_tagged (struct address_space *mapping,int tag); +static inline bool mapping_tagged (struct address_space *mapping,xa_tag_t tag) +{ +return xa_tagged (&mapping->i_pages, tag); +}static inline void i_mmap_lock_write (struct address_space *mapping) {
mm/page-writeback.c +26 -46
148 lines not on this page · raw message
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 36/74 mm: Convert workingset to XArray
In reply to Matthew WilcoxWe construct an XA_STATE and use it to delete the node with xas_store() rather than adding a special function for this unique use case. Includes a test that simulates this usage for the test suite.
--- include/linux/swap.h | 9 ------- lib/test_xarray.c | 61 ++++++++++++++++++++++++++++++++++++++++++++ mm/workingset.c | 51 +++++++++++++++--------------------- 3 files changed, 82 insertions(+), 39 deletions(-)
include/linux/swap.h +0 -9
@@ -307,15 +307,6 @@ void workingset_update_node(struct xa_node *node);xas_set_update (xas, workingset_update_node); \ }while (0 ) -/* Returns workingset_update_node() if the mapping has shadow entries. */ -#define workingset_lookup_update (mapping) \ -({ \ - radix_tree_update_node_t __helper = workingset_update_node; \ - if (dax_mapping(mapping) || shmem_mapping(mapping)) \ - __helper = NULL; \ - __helper; \ -}) -/* linux/mm/page_alloc.c */ extern unsigned long totalram_pages;extern unsigned long totalreserve_pages;
lib/test_xarray.c +61 -0
73 lines not on this page · raw message
mm/workingset.c +21 -30
117 lines not on this page · raw message
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 37/74 mm: Convert truncate to XArray
In reply to Matthew WilcoxThis is essentially xa_cmpxchg() with the locking handled above us, and it doesn't have to handle replacing a NULL entry.
--- mm/truncate.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-)
mm/truncate.c +6 -9
@@ -33,15 +33,12 @@static inline void __clear_shadow_entry (struct address_space *mapping,pgoff_t index,void *entry) { -struct radix_tree_node *node; -void **slot; +XA_STATE (xas, &mapping->i_pages, index); -if (!__radix_tree_lookup (&mapping->i_pages, index, &node, &slot)) +xas_set_update (&xas, workingset_update_node); +if (xas_load (&xas) != entry)return ; -if (*slot != entry) -return ; -__radix_tree_replace (&mapping->i_pages, node, slot,NULL , - workingset_update_node); +xas_store (&xas,NULL ); mapping->nrexceptional--; }
@@ -738,10 +735,10 @@ int invalidate_inode_pages2_range(struct address_space *mapping, index++; }/* -* For DAX we invalidate page tables after invalidating radix tree. We +* For DAX we invalidate page tables after invalidating page cache. We * could invalidate page tables while invalidating each entry however * that would be expensive. And doing range unmapping before doesn't -* work as we have no cheap way to find whether radix tree entry didn't +* work as we have no cheap way to find whether page cache entry didn't * get remapped later. */ if (dax_mapping (mapping)) {
Signature · 6 lines
2.17.1 ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot
Matthew Wilcox
maintainerPATCH v14 38/74 mm: Convert add_to_swap_cache to XArray
In reply to Matthew WilcoxCombine __add_to_swap_cache and add_to_swap_cache into one function since there is no more need to preload.
--- mm/swap_state.c | 93 +++++++++++++++---------------------------------- 1 file changed, 29 insertions(+), 64 deletions(-)
mm/swap_state.c +29 -64
@@ -107,14 +107,15 @@ void show_swap_cache_info(void) }/* -* __add_to_swap_cache resembles add_to_page_cache_locked on swapper_space, +* add_to_swap_cache resembles add_to_page_cache_locked on swapper_space, * but sets SwapCache flag and private instead of mapping and index. */ -int __add_to_swap_cache (struct page *page,swp_entry_t entry) +int add_to_swap_cache (struct page *page,swp_entry_t entry,gfp_t gfp) { -int error, i, nr =hpage_nr_pages (page); -struct address_space *address_space; +struct address_space *address_space =swap_address_space (entry);pgoff_t idx =swp_offset (entry); +XA_STATE (xas, &address_space->i_pages, idx); +unsigned long i, nr =1UL <<compound_order (page);VM_BUG_ON_PAGE (!PageLocked (page), page);VM_BUG_ON_PAGE (PageSwapCache (page), page);
@@ -123,50 +124,30 @@ int __add_to_swap_cache(struct page *page, swp_entry_t entry)page_ref_add (page, nr);SetPageSwapCache (page); - address_space =swap_address_space (entry); -xa_lock_irq (&address_space->i_pages); -for (i =0 ; i < nr; i++) { -set_page_private (page + i, entry.val + i); - error =radix_tree_insert (&address_space->i_pages, - idx + i, page + i); -if (unlikely (error)) -break ; - } -if (likely (!error)) { +do { +xas_lock_irq (&xas); +xas_create_range (&xas, idx + nr -1 ); +if (xas_error (&xas)) + goto unlock; +for (i =0 ; i < nr; i++) { +VM_BUG_ON_PAGE (xas.xa_index != idx + i, page); +set_page_private (page + i, entry.val + i); +xas_store (&xas, page + i); +xas_next (&xas); + } address_space->nrpages += nr;__mod_node_page_state (page_pgdat (page),NR_FILE_PAGES , nr);ADD_CACHE_INFO (add_total, nr); - }else { -/* -* Only the context which have set SWAP_HAS_CACHE flag -* would call add_to_swap_cache(). -* So add_to_swap_cache() doesn't returns -EEXIST. -*/ -VM_BUG_ON (error == -EEXIST ); -set_page_private (page + i,0UL ); -while (i--) { -radix_tree_delete (&address_space->i_pages, idx + i); -set_page_private (page + i,0UL ); - } -ClearPageSwapCache (page); -page_ref_sub (page, nr); - } -xa_unlock_irq (&address_space->i_pages); +unlock: +xas_unlock_irq (&xas); + }while (xas_nomem (&xas, gfp)); -return error; -} - - -int add_to_swap_cache (struct page *page,swp_entry_t entry,gfp_t gfp_mask) -{ -int error; +if (!xas_error (&xas)) +return 0 ; - error =radix_tree_maybe_preload_order (gfp_mask,compound_order (page)); -if (!error) { - error =__add_to_swap_cache (page, entry); -radix_tree_preload_end (); - } -return error; +ClearPageSwapCache (page); +page_ref_sub (page, nr); +return xas_error (&xas); }/*
@@ -217,7 +198,7 @@ int add_to_swap(struct page *page)return 0 ;/* -* Radix-tree node allocations from PF_MEMALLOC contexts could +* XArray node allocations from PF_MEMALLOC contexts could * completely exhaust the page allocator. __GFP_NOMEMALLOC * stops emergency reserves from being allocated. *
@@ -229,7 +210,6 @@ int add_to_swap(struct page *page)*/ err =add_to_swap_cache (page, entry, __GFP_HIGH|__GFP_NOMEMALLOC|__GFP_NOWARN); -/* -ENOMEM radix-tree allocation failure */ if (err)/* * add_to_swap_cache() doesn't return -EEXIST, so we can safely
@@ -423,19 +403,11 @@ struct page *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,break ;/* Out of memory */ } -/* -* call radix_tree_preload() while we can wait. -*/ - err =radix_tree_maybe_preload (gfp_mask &GFP_KERNEL ); -if (err) -break ; -/* * Swap entry may have been freed since our caller observed it. */ err =swapcache_prepare (entry);if (err == -EEXIST ) { -radix_tree_preload_end ();/* * We might race against get_swap_page() and stumble * across a SWAP_HAS_CACHE swap_map entry whose page
@@ -443,26 +415,19 @@ struct page *__read_swap_cache_async(swp_entry_t entry, gfp_t gfp_mask,*/ cond_resched ();continue ; - } -if (err) {/* swp entry is obsolete ? */ -radix_tree_preload_end (); + }else if (err)/* swp entry is obsolete ? */ break ; - } -/* May fail (-ENOMEM) if radix-tree node allocation failed. */ +/* May fail (-ENOMEM) if XArray node allocation failed. */ __SetPageLocked (new_page);__SetPageSwapBacked (new_page); - err =__add_to_swap_cache (new_page, entry); + err =add_to_swap_cache (new_page, entry, gfp_mask &GFP_KERNEL );if (likely (!err)) { -radix_tree_preload_end (); -/* -* Initiate read into locked page and return. -*/ +/* Initiate read into locked page */ lru_cache_add_anon (new_page); *new_page_allocated = true;return new_page; } -radix_tree_preload_end ();__ClearPageLocked (new_page);/* * add_to_swap_cache() doesn't return -EEXIST, so we can safely
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 39/74 mm: Convert delete_from_swap_cache to XArray
In reply to Matthew WilcoxBoth callers of __delete_from_swap_cache have the swp_entry_t already, so pass that in to make constructing the XA_STATE easier.
--- include/linux/swap.h | 5 +++-- mm/swap_state.c | 24 ++++++++++-------------- mm/vmscan.c | 2 +- 3 files changed, 14 insertions(+), 17 deletions(-)
include/linux/swap.h +3 -2
@@ -404,7 +404,7 @@ extern void show_swap_cache_info(void);extern int add_to_swap (struct page *page);extern int add_to_swap_cache (struct page *,swp_entry_t ,gfp_t );extern int __add_to_swap_cache (struct page *page,swp_entry_t entry); -extern void __delete_from_swap_cache (struct page *); +extern void __delete_from_swap_cache (struct page *,swp_entry_t entry);extern void delete_from_swap_cache (struct page *);extern void free_page_and_swap_cache (struct page *);extern void free_pages_and_swap_cache (struct page **,int );
@@ -564,7 +564,8 @@ static inline int add_to_swap_cache(struct page *page, swp_entry_t entry,return -1 ; } -static inline void __delete_from_swap_cache (struct page *page) +static inline void __delete_from_swap_cache (struct page *page, +swp_entry_t entry) { }
mm/swap_state.c +10 -14
@@ -154,23 +154,22 @@ int add_to_swap_cache(struct page *page, swp_entry_t entry, gfp_t gfp)* This must be called only on pages that have * been verified to be in the swap cache. */ -void __delete_from_swap_cache (struct page *page) +void __delete_from_swap_cache (struct page *page,swp_entry_t entry) { -struct address_space *address_space; +struct address_space *address_space =swap_address_space (entry);int i, nr =hpage_nr_pages (page); -swp_entry_t entry; -pgoff_t idx; +pgoff_t idx =swp_offset (entry); +XA_STATE (xas, &address_space->i_pages, idx);VM_BUG_ON_PAGE (!PageLocked (page), page);VM_BUG_ON_PAGE (!PageSwapCache (page), page);VM_BUG_ON_PAGE (PageWriteback (page), page); - entry.val =page_private (page); - address_space =swap_address_space (entry); - idx =swp_offset (entry);for (i =0 ; i < nr; i++) { -radix_tree_delete (&address_space->i_pages, idx + i); +void *entry =xas_store (&xas,NULL ); +VM_BUG_ON_PAGE (entry != page + i, entry);set_page_private (page + i,0 ); +xas_next (&xas); }ClearPageSwapCache (page); address_space->nrpages -= nr;
@@ -243,14 +242,11 @@ int add_to_swap(struct page *page)*/ void delete_from_swap_cache (struct page *page) { -swp_entry_t entry; -struct address_space *address_space; - - entry.val =page_private (page); +swp_entry_t entry = { .val =page_private (page) }; +struct address_space *address_space =swap_address_space (entry); - address_space =swap_address_space (entry);xa_lock_irq (&address_space->i_pages); -__delete_from_swap_cache (page); +__delete_from_swap_cache (page, entry);xa_unlock_irq (&address_space->i_pages);put_swap_page (page, entry);
mm/vmscan.c +1 -1
@@ -753,7 +753,7 @@ static int __remove_mapping(struct address_space *mapping, struct page *page,if (PageSwapCache (page)) {swp_entry_t swap = { .val =page_private (page) };mem_cgroup_swapout (page, swap); -__delete_from_swap_cache (page); +__delete_from_swap_cache (page, swap);xa_unlock_irqrestore (&mapping->i_pages, flags);put_swap_page (page, swap); }else {
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 40/74 mm: Convert __do_page_cache_readahead to XArray
In reply to Matthew WilcoxThis one is trivial.
--- mm/readahead.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
mm/readahead.c +1 -3
@@ -174,9 +174,7 @@ unsigned int __do_page_cache_readahead(struct address_space *mapping,if (page_offset > end_index)break ; -rcu_read_lock (); - page =radix_tree_lookup (&mapping->i_pages, page_offset); -rcu_read_unlock (); + page =xa_load (&mapping->i_pages, page_offset);if (page && !xa_is_value (page)) {/* * Page already present? Kick off the current batch of
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 41/74 mm: Convert page migration to XArray
In reply to Matthew Wilcox--- mm/migrate.c | 48 ++++++++++++++++++------------------------------ 1 file changed, 18 insertions(+), 30 deletions(-)
mm/migrate.c +18 -30
@@ -323,7 +323,7 @@ void __migration_entry_wait(struct mm_struct *mm, pte_t *ptep, page =migration_entry_to_page (entry);/* -* Once radix-tree replacement of page migration started, page_count +* Once page cache replacement of page migration started, page_count * *must* be zero. And, we don't want to call wait_on_page_locked() * against a page without get_page(). * So, we use get_page_unless_zero(), here. Even failed, page fault
@@ -438,10 +438,10 @@ int migrate_page_move_mapping(struct address_space *mapping, struct buffer_head *head, enum migrate_mode mode, int extra_count) { + XA_STATE(xas, &mapping->i_pages, page_index(page));struct zone *oldzone, *newzone;int dirty;int expected_count =1 + extra_count; -void **pslot;/* * Device public or private pages have an extra refcount as they are
@@ -467,21 +467,16 @@ int migrate_page_move_mapping(struct address_space *mapping, oldzone =page_zone (page); newzone =page_zone (newpage); -xa_lock_irq (&mapping->i_pages); - - pslot =radix_tree_lookup_slot (&mapping->i_pages, -page_index (page)); +xas_lock_irq (&xas); expected_count +=hpage_nr_pages (page) +page_has_private (page); -if (page_count (page) != expected_count || -radix_tree_deref_slot_protected (pslot, - &mapping->i_pages.xa_lock) != page) { -xa_unlock_irq (&mapping->i_pages); +if (page_count (page) != expected_count ||xas_load (&xas) != page) { +xas_unlock_irq (&xas);return -EAGAIN ; }if (!page_ref_freeze (page, expected_count)) { -xa_unlock_irq (&mapping->i_pages); +xas_unlock_irq (&xas);return -EAGAIN ; }
@@ -495,7 +490,7 @@ int migrate_page_move_mapping(struct address_space *mapping,if (mode ==MIGRATE_ASYNC && head && !buffer_migrate_lock_buffers (head, mode)) {page_ref_unfreeze (page, expected_count); -xa_unlock_irq (&mapping->i_pages); +xas_unlock_irq (&xas);return -EAGAIN ; }
@@ -523,16 +518,13 @@ int migrate_page_move_mapping(struct address_space *mapping,SetPageDirty (newpage); } -radix_tree_replace_slot (&mapping->i_pages, pslot, newpage); +xas_store (&xas, newpage);if (PageTransHuge (page)) {int i; -int index =page_index (page);for (i =1 ; i <HPAGE_PMD_NR ; i++) { - pslot =radix_tree_lookup_slot (&mapping->i_pages, - index + i); -radix_tree_replace_slot (&mapping->i_pages, pslot, - newpage + i); +xas_next (&xas); +xas_store (&xas, newpage + i); } }
@@ -543,7 +535,7 @@ int migrate_page_move_mapping(struct address_space *mapping,*/ page_ref_unfreeze (page, expected_count -hpage_nr_pages (page)); -xa_unlock (&mapping->i_pages); +xas_unlock (&xas);/* Leave irq disabled to prevent preemption while updating stats */ /*
@@ -583,22 +575,18 @@ EXPORT_SYMBOL(migrate_page_move_mapping);int migrate_huge_page_move_mapping (struct address_space *mapping,struct page *newpage,struct page *page) { +XA_STATE (xas, &mapping->i_pages,page_index (page));int expected_count; -void **pslot; - -xa_lock_irq (&mapping->i_pages); - - pslot =radix_tree_lookup_slot (&mapping->i_pages,page_index (page)); +xas_lock_irq (&xas); expected_count =2 +page_has_private (page); -if (page_count (page) != expected_count || -radix_tree_deref_slot_protected (pslot, &mapping->i_pages.xa_lock) != page) { -xa_unlock_irq (&mapping->i_pages); +if (page_count (page) != expected_count ||xas_load (&xas) != page) { +xas_unlock_irq (&xas);return -EAGAIN ; }if (!page_ref_freeze (page, expected_count)) { -xa_unlock_irq (&mapping->i_pages); +xas_unlock_irq (&xas);return -EAGAIN ; }
@@ -607,11 +595,11 @@ int migrate_huge_page_move_mapping(struct address_space *mapping,get_page (newpage); -radix_tree_replace_slot (&mapping->i_pages, pslot, newpage); +xas_store (&xas, newpage);page_ref_unfreeze (page, expected_count -1 ); -xa_unlock_irq (&mapping->i_pages); +xas_unlock_irq (&xas);return MIGRATEPAGE_SUCCESS ; }
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 42/74 mm: Convert huge_memory to XArray
In reply to Matthew WilcoxQuite a straightforward conversion.
--- mm/huge_memory.c | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-)
mm/huge_memory.c +7 -10
@@ -2439,13 +2439,13 @@ static void __split_huge_page(struct page *page, struct list_head *list,ClearPageCompound (head);/* See comment in __split_huge_page_tail() */ if (PageAnon (head)) { -/* Additional pin to radix tree of swap cache */ +/* Additional pin to swap cache */ if (PageSwapCache (head))page_ref_add (head,2 );else page_ref_inc (head); }else { -/* Additional pin to radix tree */ +/* Additional pin to page cache */ page_ref_add (head,2 );xa_unlock (&head->mapping->i_pages); }
@@ -2557,7 +2557,7 @@ bool can_split_huge_page(struct page *page, int *pextra_pins) {int extra_pins; -/* Additional pins from radix tree */ +/* Additional pins from page cache */ if (PageAnon (page)) extra_pins =PageSwapCache (page) ?HPAGE_PMD_NR :0 ;else
@@ -2653,17 +2653,14 @@ int split_huge_page_to_list(struct page *page, struct list_head *list)spin_lock_irqsave (zone_lru_lock (page_zone (head)), flags);if (mapping) { -void **pslot; +XA_STATE (xas, &mapping->i_pages,page_index (head)); -xa_lock (&mapping->i_pages); - pslot =radix_tree_lookup_slot (&mapping->i_pages, -page_index (head));/* -* Check if the head page is present in radix tree. +* Check if the head page is present in page cache. * We assume all tail are present too, if head is there. */ -if (radix_tree_deref_slot_protected (pslot, - &mapping->i_pages.xa_lock) != head) +xa_lock (&mapping->i_pages); +if (xas_load (&xas) != head) goto fail; }
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 43/74 mm: Convert collapse_shmem to XArray
In reply to Matthew WilcoxI found another victim of the radix tree being hard to use. Because there was no call to radix_tree_preload(), khugepaged was allocating radix_tree_nodes using GFP_ATOMIC.
I also converted a local_irq_save()/restore() pair to disable()/enable().
--- mm/khugepaged.c | 158 ++++++++++++++++++++---------------------------- 1 file changed, 65 insertions(+), 93 deletions(-)
mm/khugepaged.c +65 -93
@@ -1287,17 +1287,17 @@ static void retract_page_tables(struct address_space *mapping, pgoff_t pgoff)* * Basic scheme is simple, details are more complex: * - allocate and freeze a new huge page; -* - scan over radix tree replacing old pages the new one +* - scan page cache replacing old pages with the new one * + swap in pages if necessary; * + fill in gaps; -* + keep old pages around in case if rollback is required; -* - if replacing succeed: +* + keep old pages around in case rollback is required; +* - if replacing succeeds: * + copy data over; * + free old pages; * + unfreeze huge page; * - if replacing failed; * + put all pages back and unfreeze them; -* + restore gaps in the radix-tree; +* + restore gaps in the page cache; * + free huge page; */ static void collapse_shmem(struct mm_struct *mm,
@@ -1305,12 +1305,11 @@ static void collapse_shmem(struct mm_struct *mm, struct page **hpage, int node) {gfp_t gfp; -struct page *page, *new_page, *tmp; +struct page *new_page;struct mem_cgroup *memcg;pgoff_t index, end = start +HPAGE_PMD_NR ;LIST_HEAD (pagelist); -struct radix_tree_iter iter; -void **slot; +XA_STATE (xas, &mapping->i_pages, start);int nr_none =0 , result =SCAN_SUCCEED ;VM_BUG_ON (start & (HPAGE_PMD_NR -1 ));
@@ -1335,48 +1334,48 @@ static void collapse_shmem(struct mm_struct *mm,__SetPageLocked (new_page);BUG_ON (!page_ref_freeze (new_page,1 )); -/* -* At this point the new_page is 'frozen' (page_count() is zero), locked -* and not up-to-date. It's safe to insert it into radix tree, because -* nobody would be able to map it or use it in other way until we -* unfreeze it. +* At this point the new_page is 'frozen' (page_count() is zero), +* locked and not up-to-date. It's safe to insert it into the page +* cache, because nobody would be able to map it or use it in other +* way until we unfreeze it. */ - index = start; -xa_lock_irq (&mapping->i_pages); -radix_tree_for_each_slot (slot, &mapping->i_pages, &iter, start) { -int n =min (iter.index, end) - index; - -/* -* Handle holes in the radix tree: charge it from shmem and -* insert relevant subpage of new_page into the radix-tree. -*/ -if (n && !shmem_charge (mapping->host, n)) { - result =SCAN_FAIL ; +/* This will be less messy when we use multi-index entries */ +do { +xas_lock_irq (&xas); +xas_create_range (&xas, end -1 ); +if (!xas_error (&xas))break ; - } - nr_none += n; -for (; index <min (iter.index, end); index++) { -radix_tree_insert (&mapping->i_pages, index, - new_page + (index %HPAGE_PMD_NR )); - } +xas_unlock_irq (&xas); +if (!xas_nomem (&xas,GFP_KERNEL )) + goto out; + }while (1 ); -/* We are done. */ -if (index >= end) -break ; +for (index = start; index < end; index++) { +struct page *page =xas_next (&xas); + +VM_BUG_ON (index != xas.xa_index); +if (!page) { +if (!shmem_charge (mapping->host,1 )) { + result =SCAN_FAIL ; +break ; + } +xas_store (&xas, new_page + (index %HPAGE_PMD_NR )); + nr_none++; +continue ; + } - page =radix_tree_deref_slot_protected (slot, - &mapping->i_pages.xa_lock);if (xa_is_value (page) || !PageUptodate (page)) { -xa_unlock_irq (&mapping->i_pages); +xas_unlock_irq (&xas);/* swap in or instantiate fallocated page */ if (shmem_getpage (mapping->host, index, &page,SGP_NOHUGE )) { result =SCAN_FAIL ; - goto tree_unlocked; + goto xa_unlocked; } -xa_lock_irq (&mapping->i_pages); +xas_lock_irq (&xas); +xas_set (&xas, index); }else if (trylock_page (page)) {get_page (page); }else {
@@ -1396,7 +1395,7 @@ static void collapse_shmem(struct mm_struct *mm, result =SCAN_TRUNCATED ; goto out_unlock; } -xa_unlock_irq (&mapping->i_pages); +xas_unlock_irq (&xas);if (isolate_lru_page (page)) { result =SCAN_DEL_PAGE_LRU ;
@@ -1406,17 +1405,16 @@ static void collapse_shmem(struct mm_struct *mm,if (page_mapped (page))unmap_mapping_pages (mapping, index,1 , false); -xa_lock_irq (&mapping->i_pages); +xas_lock (&xas); +xas_set (&xas, index); - slot =radix_tree_lookup_slot (&mapping->i_pages, index); -VM_BUG_ON_PAGE (page !=radix_tree_deref_slot_protected (slot, - &mapping->i_pages.xa_lock), page); +VM_BUG_ON_PAGE (page !=xas_load (&xas), page);VM_BUG_ON_PAGE (page_mapped (page), page);/* * The page is expected to have page_count() == 3: * - we hold a pin on it; -* - one reference from radix tree; +* - one reference from page cache; * - one from isolate_lru_page; */ if (!page_ref_freeze (page,3 )) {
@@ -1431,56 +1429,30 @@ static void collapse_shmem(struct mm_struct *mm,list_add_tail (&page->lru, &pagelist);/* Finally, replace with the new page. */ -radix_tree_replace_slot (&mapping->i_pages, slot, - new_page + (index %HPAGE_PMD_NR )); - - slot =radix_tree_iter_resume (slot, &iter); - index++; +xas_store (&xas, new_page + (index %HPAGE_PMD_NR ));continue ; out_lru: -xa_unlock_irq (&mapping->i_pages); +xas_unlock_irq (&xas);putback_lru_page (page); out_isolate_failed:unlock_page (page);put_page (page); - goto tree_unlocked; + goto xa_unlocked; out_unlock:unlock_page (page);put_page (page);break ; } +xas_unlock_irq (&xas); -/* -* Handle hole in radix tree at the end of the range. -* This code only triggers if there's nothing in radix tree -* beyond 'end'. -*/ -if (result ==SCAN_SUCCEED && index < end) { -int n = end - index; - -if (!shmem_charge (mapping->host, n)) { - result =SCAN_FAIL ; - goto tree_locked; - } - -for (; index < end; index++) { -radix_tree_insert (&mapping->i_pages, index, - new_page + (index %HPAGE_PMD_NR )); - } - nr_none += n; - } - -tree_locked: -xa_unlock_irq (&mapping->i_pages); -tree_unlocked: - +xa_unlocked:if (result ==SCAN_SUCCEED ) { -unsigned long flags; +struct page *page, *tmp;struct zone *zone =page_zone (new_page);/* -* Replacing old pages with new one has succeed, now we need to -* copy the content and free old pages. +* Replacing old pages with new one has succeeded, now we +* need to copy the content and free the old pages. */ list_for_each_entry_safe (page, tmp, &pagelist, lru) { copy_highpage(new_page + (page->index % HPAGE_PMD_NR),
@@ -1494,16 +1466,16 @@ static void collapse_shmem(struct mm_struct *mm,put_page (page); } -local_irq_save (flags); +local_irq_disable ();__inc_node_page_state (new_page,NR_SHMEM_THPS );if (nr_none) {__mod_node_page_state (zone->zone_pgdat,NR_FILE_PAGES , nr_none);__mod_node_page_state (zone->zone_pgdat,NR_SHMEM , nr_none); } -local_irq_restore (flags); +local_irq_enable ();/* -* Remove pte page tables, so we can re-faulti +* Remove pte page tables, so we can re-fault * the page as huge. */ retract_page_tables (mapping, start);
@@ -1518,37 +1490,37 @@ static void collapse_shmem(struct mm_struct *mm, *hpage =NULL ; }else { -/* Something went wrong: rollback changes to the radix-tree */ +struct page *page; +/* Something went wrong: roll back page cache changes */ shmem_uncharge (mapping->host, nr_none); -xa_lock_irq (&mapping->i_pages); -radix_tree_for_each_slot (slot, &mapping->i_pages, &iter, start) { -if (iter.index >= end) -break ; +xas_lock_irq (&xas); +xas_set (&xas, start); +xas_for_each (&xas, page, end -1 ) { page =list_first_entry_or_null (&pagelist, struct page, lru); -if (!page || iter.index < page->index) { +if (!page || xas.xa_index < page->index) {if (!nr_none)break ; nr_none--;/* Put holes back where they were */ -radix_tree_delete (&mapping->i_pages, iter.index); +xas_store (&xas,NULL );continue ; } -VM_BUG_ON_PAGE (page->index != iter.index, page); +VM_BUG_ON_PAGE (page->index != xas.xa_index, page);/* Unfreeze the page. */ list_del (&page->lru);page_ref_unfreeze (page,2 ); -radix_tree_replace_slot (&mapping->i_pages, slot, page); - slot =radix_tree_iter_resume (slot, &iter); -xa_unlock_irq (&mapping->i_pages); +xas_store (&xas, page); +xas_pause (&xas); +xas_unlock_irq (&xas);putback_lru_page (page);unlock_page (page); -xa_lock_irq (&mapping->i_pages); +xas_lock_irq (&xas); }VM_BUG_ON (nr_none); -xa_unlock_irq (&mapping->i_pages); +xas_unlock_irq (&xas);/* Unfreeze new_page, caller would take care about freeing it */ page_ref_unfreeze (new_page,1 );
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 44/74 mm: Convert khugepaged_scan_shmem to XArray
In reply to Matthew WilcoxSlightly shorter and easier to read code.
--- mm/khugepaged.c | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-)
mm/khugepaged.c +5 -12
@@ -1538,8 +1538,7 @@ static void khugepaged_scan_shmem(struct mm_struct *mm, pgoff_t start, struct page **hpage) { struct page *page = NULL; -struct radix_tree_iter iter; -void **slot; +XA_STATE (xas, &mapping->i_pages, start);int present, swap;int node =NUMA_NO_NODE ;int result =SCAN_SUCCEED ;
@@ -1548,17 +1547,11 @@ static void khugepaged_scan_shmem(struct mm_struct *mm, swap =0 ;memset (khugepaged_node_load,0 ,sizeof (khugepaged_node_load));rcu_read_lock (); -radix_tree_for_each_slot (slot, &mapping->i_pages, &iter, start) { -if (iter.index >= start +HPAGE_PMD_NR ) -break ; - - page =radix_tree_deref_slot (slot); -if (radix_tree_deref_retry (page)) { - slot =radix_tree_iter_retry (&iter); +xas_for_each (&xas, page, start +HPAGE_PMD_NR -1 ) { +if (xas_retry (&xas, page))continue ; - } -if (radix_tree_exception (page)) { +if (xa_is_value (page)) {if (++swap > khugepaged_max_ptes_swap) { result =SCAN_EXCEED_SWAP_PTE ;break ;
@@ -1597,7 +1590,7 @@ static void khugepaged_scan_shmem(struct mm_struct *mm, present++;if (need_resched ()) { - slot =radix_tree_iter_resume (slot, &iter); +xas_pause (&xas);cond_resched_rcu (); } }
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 45/74 mm: Convert is_page_cache_freeable to XArray
In reply to Matthew WilcoxThis is just a variable rename and comment change.
--- mm/vmscan.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
mm/vmscan.c +4 -4
@@ -572,12 +572,12 @@ static inline int is_page_cache_freeable(struct page *page) {/* * A freeable page cache page is referenced only by the caller -* that isolated the page, the page cache radix tree and -* optional buffer heads at page->private. +* that isolated the page, the page cache and optional buffer +* heads at page->private. */ -int radix_pins =PageTransHuge (page) &&PageSwapCache (page) ? +int page_cache_pins =PageTransHuge (page) &&PageSwapCache (page) ?HPAGE_PMD_NR :1 ; -return page_count (page) -page_has_private (page) ==1 + radix_pins; +return page_count (page) -page_has_private (page) ==1 + page_cache_pins; }static int may_write_to_inode (struct inode *inode,struct scan_control *sc)
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 46/74 pagevec: Use xa_tag_t
In reply to Matthew WilcoxRemoves sparse warnings.
--- fs/btrfs/extent_io.c | 4 ++-- fs/ext4/inode.c | 2 +- fs/f2fs/data.c | 2 +- fs/gfs2/aops.c | 2 +- include/linux/pagevec.h | 8 +++++--- mm/swap.c | 4 ++-- 6 files changed, 12 insertions(+), 10 deletions(-)
fs/btrfs/extent_io.c +2 -2
@@ -3792,7 +3792,7 @@ int btree_write_cache_pages(struct address_space *mapping,pgoff_t index;pgoff_t end;/* Inclusive */ int scanned =0 ; -int tag; +xa_tag_t tag;pagevec_init (&pvec);if (wbc->range_cyclic) {
@@ -3917,7 +3917,7 @@ static int extent_write_cache_pages(struct address_space *mapping,pgoff_t done_index;int range_whole =0 ;int scanned =0 ; -int tag; +xa_tag_t tag;/* * We have to hold onto the inode so that ordered extents can do their
fs/ext4/inode.c +1 -1
@@ -2615,7 +2615,7 @@ static int mpage_prepare_extent_to_map(struct mpage_da_data *mpd)long left = mpd->wbc->nr_to_write;pgoff_t index = mpd->first_page;pgoff_t end = mpd->last_page; -int tag; +xa_tag_t tag;int i, err =0 ;int blkbits = mpd->inode->i_blkbits;ext4_lblk_t lblk;
fs/f2fs/data.c +1 -1
@@ -1942,7 +1942,7 @@ static int f2fs_write_cache_pages(struct address_space *mapping,pgoff_t last_idx =ULONG_MAX ;int cycled;int range_whole =0 ; -int tag; +xa_tag_t tag;pagevec_init (&pvec);
fs/gfs2/aops.c +1 -1
@@ -370,7 +370,7 @@ static int gfs2_write_cache_jdata(struct address_space *mapping,pgoff_t done_index;int cycled;int range_whole =0 ; -int tag; +xa_tag_t tag;pagevec_init (&pvec);if (wbc->range_cyclic) {
include/linux/pagevec.h +5 -3
@@ -9,6 +9,8 @@#ifndef _LINUX_PAGEVEC_H#define _LINUX_PAGEVEC_H +#include <linux/xarray.h> +/* 15 pointers + header align the pagevec structure to a power of two */ #define PAGEVEC_SIZE 15
@@ -40,12 +42,12 @@ static inline unsigned pagevec_lookup(struct pagevec *pvec,unsigned pagevec_lookup_range_tag (struct pagevec *pvec,struct address_space *mapping,pgoff_t *index,pgoff_t end, -int tag); +xa_tag_t tag);unsigned pagevec_lookup_range_nr_tag (struct pagevec *pvec,struct address_space *mapping,pgoff_t *index,pgoff_t end, -int tag,unsigned max_pages); +xa_tag_t tag,unsigned max_pages);static inline unsigned pagevec_lookup_tag (struct pagevec *pvec, -struct address_space *mapping,pgoff_t *index,int tag) +struct address_space *mapping,pgoff_t *index,xa_tag_t tag) {return pagevec_lookup_range_tag (pvec, mapping, index, (pgoff_t)-1 , tag); }
mm/swap.c +2 -2
@@ -1002,7 +1002,7 @@ EXPORT_SYMBOL(pagevec_lookup_range);unsigned pagevec_lookup_range_tag (struct pagevec *pvec,struct address_space *mapping,pgoff_t *index,pgoff_t end, -int tag) +xa_tag_t tag) { pvec->nr =find_get_pages_range_tag (mapping, index, end, tag,PAGEVEC_SIZE , pvec->pages);
@@ -1012,7 +1012,7 @@ EXPORT_SYMBOL(pagevec_lookup_range_tag);unsigned pagevec_lookup_range_nr_tag (struct pagevec *pvec,struct address_space *mapping,pgoff_t *index,pgoff_t end, -int tag,unsigned max_pages) +xa_tag_t tag,unsigned max_pages) { pvec->nr =find_get_pages_range_tag (mapping, index, end, tag, min_t(unsigned int, max_pages, PAGEVEC_SIZE), pvec->pages);
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 47/74 shmem: Convert shmem_radix_tree_replace to XArray
In reply to Matthew WilcoxRename shmem_radix_tree_replace() to shmem_replace_entry() and convert it to use the XArray API.
--- mm/shmem.c | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-)
mm/shmem.c +8 -14
@@ -321,24 +321,20 @@ void shmem_uncharge(struct inode *inode, long pages) }/* -* Replace item expected in radix tree by a new item, while holding tree lock. +* Replace item expected in xarray by a new item, while holding xa_lock. */ -static int shmem_radix_tree_replace (struct address_space *mapping, +static int shmem_replace_entry (struct address_space *mapping,pgoff_t index,void *expected,void *replacement) { -struct radix_tree_node *node; -void __rcu **pslot; +XA_STATE (xas, &mapping->i_pages, index);void *item;VM_BUG_ON (!expected);VM_BUG_ON (!replacement); - item =__radix_tree_lookup (&mapping->i_pages, index, &node, &pslot); -if (!item) -return -ENOENT ; + item =xas_load (&xas);if (item != expected)return -ENOENT ; -__radix_tree_replace (&mapping->i_pages, node, pslot, - replacement,NULL ); +xas_store (&xas, replacement);return 0 ; }
@@ -623,8 +619,7 @@ static int shmem_add_to_page_cache(struct page *page, }else if (!expected) { error =radix_tree_insert (&mapping->i_pages, index, page); }else { - error =shmem_radix_tree_replace (mapping, index, expected, - page); + error =shmem_replace_entry (mapping, index, expected, page); }if (!error) {
@@ -653,7 +648,7 @@ static void shmem_delete_from_page_cache(struct page *page, void *radswap)VM_BUG_ON_PAGE (PageCompound (page), page);xa_lock_irq (&mapping->i_pages); - error =shmem_radix_tree_replace (mapping, page->index, page, radswap); + error =shmem_replace_entry (mapping, page->index, page, radswap); page->mapping =NULL ; mapping->nrpages--;__dec_node_page_state (page,NR_FILE_PAGES );
@@ -1577,8 +1572,7 @@ static int shmem_replace_page(struct page **pagep, gfp_t gfp,* a nice clean interface for us to replace oldpage by newpage there. */ xa_lock_irq (&swap_mapping->i_pages); - error =shmem_radix_tree_replace (swap_mapping, swap_index, oldpage, - newpage); + error =shmem_replace_entry (swap_mapping, swap_index, oldpage, newpage);if (!error) {__inc_node_page_state (newpage,NR_FILE_PAGES );__dec_node_page_state (oldpage,NR_FILE_PAGES );
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 48/74 shmem: Convert shmem_confirm_swap to XArray
In reply to Matthew Wilcoxxa_load has its own RCU locking, so we can eliminate it here.
--- mm/shmem.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-)
mm/shmem.c +1 -6
@@ -348,12 +348,7 @@ static int shmem_replace_entry(struct address_space *mapping,static bool shmem_confirm_swap (struct address_space *mapping,pgoff_t index,swp_entry_t swap) { -void *item; - -rcu_read_lock (); - item =radix_tree_lookup (&mapping->i_pages, index); -rcu_read_unlock (); -return item ==swp_to_radix_entry (swap); +return xa_load (&mapping->i_pages, index) ==swp_to_radix_entry (swap); }/*
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 49/74 shmem: Convert find_swap_entry to XArray
In reply to Matthew WilcoxThis is a 1:1 conversion.
--- mm/shmem.c | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-)
mm/shmem.c +10 -17
@@ -1099,34 +1099,27 @@ static void shmem_evict_inode(struct inode *inode)clear_inode (inode); } -static unsigned long find_swap_entry (struct radix_tree_root *root,void *item) +static unsigned long find_swap_entry (struct xarray *xa,void *item) { -struct radix_tree_iter iter; -void __rcu **slot; -unsigned long found =-1 ; +XA_STATE (xas, xa,0 );unsigned int checked =0 ; +void *entry;rcu_read_lock (); -radix_tree_for_each_slot (slot, root, &iter,0 ) { -void *entry =radix_tree_deref_slot (slot); - -if (radix_tree_deref_retry (entry)) { - slot =radix_tree_iter_retry (&iter); +xas_for_each (&xas, entry,ULONG_MAX ) { +if (xas_retry (&xas, entry))continue ; - } -if (entry == item) { - found = iter.index; +if (entry == item)break ; - } checked++; -if ((checked %4096 ) !=0 ) +if ((checked %XA_CHECK_SCHED ) !=0 )continue ; - slot =radix_tree_iter_resume (slot, &iter); +xas_pause (&xas);cond_resched_rcu (); } -rcu_read_unlock (); -return found; + +return xas_invalid (&xas) ?-1 : xas.xa_index; }/*
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 50/74 shmem: Convert shmem_add_to_page_cache to XArray
In reply to Matthew WilcoxThis removes the last caller of radix_tree_maybe_preload_order(). Simpler code, unless we run out of memory for new xa_nodes partway through inserting entries into the xarray. Hopefully we can support multi-index entries in the page cache soon and all the awful code goes away.
--- mm/shmem.c | 87 ++++++++++++++++++++++++------------------------------ 1 file changed, 39 insertions(+), 48 deletions(-)
mm/shmem.c +39 -48
@@ -576,9 +576,10 @@ static inline bool is_huge_enabled(struct shmem_sb_info *sbinfo)*/ static int shmem_add_to_page_cache (struct page *page,struct address_space *mapping, -pgoff_t index,void *expected) +pgoff_t index,void *expected,gfp_t gfp) { -int error, nr =hpage_nr_pages (page); +XA_STATE (xas, &mapping->i_pages, index); +unsigned long i, nr =1UL <<compound_order (page);VM_BUG_ON_PAGE (PageTail (page), page);VM_BUG_ON_PAGE (index !=round_down (index, nr), page);
@@ -587,49 +588,47 @@ static int shmem_add_to_page_cache(struct page *page,VM_BUG_ON (expected &&PageTransHuge (page));page_ref_add (page, nr); - page->mapping = mapping; page->index = index; + page->mapping = mapping; -xa_lock_irq (&mapping->i_pages); -if (PageTransHuge (page)) { -void __rcu **results; -pgoff_t idx; -int i; - - error =0 ; -if (radix_tree_gang_lookup_slot (&mapping->i_pages, - &results, &idx, index,1 ) && - idx < index +HPAGE_PMD_NR ) { - error = -EEXIST ; +do { +xas_lock_irq (&xas); +xas_create_range (&xas, index + nr -1 ); +if (xas_error (&xas)) + goto unlock; +for (i =0 ; i < nr; i++) { +void *entry =xas_load (&xas); +if (entry != expected) +xas_set_err (&xas, -ENOENT ); +if (xas_error (&xas)) + goto undo; +xas_store (&xas, page + i); +xas_next (&xas); } - -if (!error) { -for (i =0 ; i <HPAGE_PMD_NR ; i++) { - error =radix_tree_insert (&mapping->i_pages, - index + i, page + i); -VM_BUG_ON (error); - } +if (PageTransHuge (page)) {count_vm_event (THP_FILE_ALLOC ); +__inc_node_page_state (page,NR_SHMEM_THPS ); } - }else if (!expected) { - error =radix_tree_insert (&mapping->i_pages, index, page); - }else { - error =shmem_replace_entry (mapping, index, expected, page); - } - -if (!error) { mapping->nrpages += nr; -if (PageTransHuge (page)) -__inc_node_page_state (page,NR_SHMEM_THPS );__mod_node_page_state (page_pgdat (page),NR_FILE_PAGES , nr);__mod_node_page_state (page_pgdat (page),NR_SHMEM , nr); -xa_unlock_irq (&mapping->i_pages); - }else { + goto unlock; +undo: +while (i-- >0 ) { +xas_store (&xas,NULL ); +xas_prev (&xas); + } +unlock: +xas_unlock_irq (&xas); + }while (xas_nomem (&xas, gfp)); + +if (xas_error (&xas)) { page->mapping =NULL ; -xa_unlock_irq (&mapping->i_pages);page_ref_sub (page, nr); +return xas_error (&xas); } -return error; + +return 0 ; }/*
@@ -1182,7 +1181,7 @@ static int shmem_unuse_inode(struct shmem_inode_info *info,*/ if (!error) error =shmem_add_to_page_cache (*pagep, mapping, index, - radswap); + radswap, gfp);if (error != -ENOMEM ) {/* * Truncation and eviction use free_swap_and_cache(), which
@@ -1698,7 +1697,7 @@ static int shmem_getpage_gfp(struct inode *inode, pgoff_t index, false);if (!error) { error =shmem_add_to_page_cache (page, mapping, index, -swp_to_radix_entry (swap)); +swp_to_radix_entry (swap), gfp);/* * We already confirmed swap under page lock, and make * no memory allocation here, so usually no possibility
@@ -1804,13 +1803,8 @@ alloc_nohuge: page = shmem_alloc_and_acct_page(gfp, inode, PageTransHuge(page));if (error) goto unacct; - error =radix_tree_maybe_preload_order (gfp &GFP_RECLAIM_MASK , -compound_order (page)); -if (!error) { - error =shmem_add_to_page_cache (page, mapping, hindex, -NULL ); -radix_tree_preload_end (); - } + error =shmem_add_to_page_cache (page, mapping, hindex, +NULL , gfp &GFP_RECLAIM_MASK );if (error) {mem_cgroup_cancel_charge (page, memcg,PageTransHuge (page));
@@ -2277,11 +2271,8 @@ static int shmem_mfill_atomic_pte(struct mm_struct *dst_mm,if (ret) goto out_release; - ret =radix_tree_maybe_preload (gfp &GFP_RECLAIM_MASK ); -if (!ret) { - ret =shmem_add_to_page_cache (page, mapping, pgoff,NULL ); -radix_tree_preload_end (); - } + ret =shmem_add_to_page_cache (page, mapping, pgoff,NULL , + gfp &GFP_RECLAIM_MASK );if (ret) goto out_release_uncharge;
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 51/74 shmem: Convert shmem_alloc_hugepage to XArray
In reply to Matthew Wilcoxxa_find() is a slightly easier API to use than radix_tree_gang_lookup_slot() because it contains its own RCU locking. This commit removes the last user of radix_tree_gang_lookup_slot() so remove the function too.
--- include/linux/radix-tree.h | 6 +----- lib/radix-tree.c | 44 +------------------------------------- mm/shmem.c | 14 ++++-------- 3 files changed, 6 insertions(+), 58 deletions(-)
include/linux/radix-tree.h +1 -5
@@ -147,12 +147,11 @@ static inline unsigned int iter_shift(const struct radix_tree_iter *iter)* radix_tree_lookup_slot * radix_tree_tag_get * radix_tree_gang_lookup -* radix_tree_gang_lookup_slot * radix_tree_gang_lookup_tag * radix_tree_gang_lookup_tag_slot * radix_tree_tagged * -* The first 8 functions are able to be called locklessly, using RCU. The +* The first 7 functions are able to be called locklessly, using RCU. The * caller must ensure calls to these functions are made within rcu_read_lock() * regions. Other readers (lock-free or otherwise) and modifications may be * running concurrently.
@@ -263,9 +262,6 @@ void radix_tree_clear_tags(struct radix_tree_root *, struct radix_tree_node *,unsigned int radix_tree_gang_lookup (const struct radix_tree_root *,void **results,unsigned long first_index,unsigned int max_items); -unsigned int radix_tree_gang_lookup_slot (const struct radix_tree_root *, -void __rcu ***results,unsigned long *indices, -unsigned long first_index,unsigned int max_items);int radix_tree_preload (gfp_t gfp_mask);int radix_tree_maybe_preload (gfp_t gfp_mask);int radix_tree_maybe_preload_order (gfp_t gfp_mask,int order);
lib/radix-tree.c +1 -43
56 lines not on this page · raw message
mm/shmem.c +4 -10
@@ -1434,23 +1434,17 @@ static struct page *shmem_alloc_hugepage(gfp_t gfp, struct shmem_inode_info *info, pgoff_t index) {struct vm_area_struct pvma; -struct inode *inode = &info->vfs_inode; -struct address_space *mapping = inode->i_mapping; -pgoff_t idx, hindex; -void __rcu **results; +struct address_space *mapping = info->vfs_inode.i_mapping; +pgoff_t hindex;struct page *page;if (!IS_ENABLED (CONFIG_TRANSPARENT_HUGE_PAGECACHE ))return NULL ; hindex =round_down (index,HPAGE_PMD_NR ); -rcu_read_lock (); -if (radix_tree_gang_lookup_slot (&mapping->i_pages, &results, &idx, - hindex,1 ) && idx < hindex +HPAGE_PMD_NR ) { -rcu_read_unlock (); +if (xa_find (&mapping->i_pages, &hindex, hindex +HPAGE_PMD_NR -1 , +XA_PRESENT ))return NULL ; - } -rcu_read_unlock ();shmem_pseudo_vma_init (&pvma, info, hindex); page = alloc_pages_vma(gfp | __GFP_COMP | __GFP_NORETRY | __GFP_NOWARN,
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 52/74 shmem: Convert shmem_free_swap to XArray
In reply to Matthew WilcoxSince we are conditionally storing NULL in the XArray, we do not need to allocate memory and the GFP flags will be unused.
--- mm/shmem.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
mm/shmem.c +2 -2
@@ -653,7 +653,7 @@ static void shmem_delete_from_page_cache(struct page *page, void *radswap) }/* -* Remove swap entry from radix tree, free the swap and its page cache. +* Remove swap entry from page cache, free the swap and its page cache. */ static int shmem_free_swap (struct address_space *mapping,pgoff_t index,void *radswap)
@@ -661,7 +661,7 @@ static int shmem_free_swap(struct address_space *mapping,void *old;xa_lock_irq (&mapping->i_pages); - old =radix_tree_delete_item (&mapping->i_pages, index, radswap); + old =__xa_cmpxchg (&mapping->i_pages, index, radswap,NULL ,0 );xa_unlock_irq (&mapping->i_pages);if (old != radswap)return -ENOENT ;
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 53/74 shmem: Convert shmem_partial_swap_usage to XArray
In reply to Matthew WilcoxSimpler code because the xarray takes care of things like the limit and dereferencing the slot.
--- mm/shmem.c | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-)
mm/shmem.c +4 -14
@@ -679,29 +679,19 @@ static int shmem_free_swap(struct address_space *mapping,unsigned long shmem_partial_swap_usage (struct address_space *mapping,pgoff_t start,pgoff_t end) { -struct radix_tree_iter iter; -void __rcu **slot; +XA_STATE (xas, &mapping->i_pages, start);struct page *page;unsigned long swapped =0 ;rcu_read_lock (); - -radix_tree_for_each_slot (slot, &mapping->i_pages, &iter, start) { -if (iter.index >= end) -break ; - - page =radix_tree_deref_slot (slot); - -if (radix_tree_deref_retry (page)) { - slot =radix_tree_iter_retry (&iter); +xas_for_each (&xas, page, end -1 ) { +if (xas_retry (&xas, page))continue ; - } -if (xa_is_value (page)) swapped++;if (need_resched ()) { - slot =radix_tree_iter_resume (slot, &iter); +xas_pause (&xas);cond_resched_rcu (); } }
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 54/74 memfd: Convert memfd_wait_for_pins to XArray
In reply to Matthew WilcoxSimplify the locking by taking the spinlock while we walk the tree on the assumption that many acquires and releases of the lock will be worse than holding the lock while we process an entire batch of pages.
--- mm/memfd.c | 61 ++++++++++++++++++++++-------------------------------- 1 file changed, 25 insertions(+), 36 deletions(-)
mm/memfd.c +25 -36
@@ -21,7 +21,7 @@#include <uapi/linux/memfd.h> /* -* We need a tag: a new tag would expand every radix_tree_node by 8 bytes, +* We need a tag: a new tag would expand every xa_node by 8 bytes, * so reuse a tag which we firmly believe is never set or cleared on tmpfs * or hugetlbfs because they are memory only filesystems. */
@@ -72,9 +72,7 @@ static void memfd_tag_pins(struct address_space *mapping)*/ static int memfd_wait_for_pins (struct address_space *mapping) { -struct radix_tree_iter iter; -void __rcu **slot; -pgoff_t start; +XA_STATE (xas, &mapping->i_pages,0 );struct page *page;int error, scan;
@@ -82,7 +80,9 @@ static int memfd_wait_for_pins(struct address_space *mapping) error =0 ;for (scan =0 ; scan <=LAST_SCAN ; scan++) { -if (!radix_tree_tagged (&mapping->i_pages,MEMFD_TAG_PINNED )) +unsigned int tagged =0 ; + +if (!xas_tagged (&xas,MEMFD_TAG_PINNED ))break ;if (!scan)
@@ -90,45 +90,34 @@ static int memfd_wait_for_pins(struct address_space *mapping)else if (schedule_timeout_killable ((HZ << scan) /200 )) scan =LAST_SCAN ; - start =0 ; -rcu_read_lock (); -radix_tree_for_each_tagged (slot, &mapping->i_pages, &iter, - start,MEMFD_TAG_PINNED ) { - - page =radix_tree_deref_slot (slot); -if (radix_tree_exception (page)) { -if (radix_tree_deref_retry (page)) { - slot =radix_tree_iter_retry (&iter); -continue ; - } - - page =NULL ; - } - -if (page && -page_count (page) -page_mapcount (page) !=1 ) { -if (scan <LAST_SCAN ) - goto continue_resched; - +xas_set (&xas,0 ); +xas_lock_irq (&xas); +xas_for_each_tagged (&xas, page,ULONG_MAX ,MEMFD_TAG_PINNED ) { +bool clear = true; +if (xa_is_value (page)) +continue ; +if (page_count (page) -page_mapcount (page) !=1 ) {/* * On the last scan, we clean up all those tags * we inserted; but make a note that we still * found pages pinned. */ - error = -EBUSY ; +if (scan ==LAST_SCAN ) + error = -EBUSY ; +else + clear = false; } +if (clear) +xas_clear_tag (&xas,MEMFD_TAG_PINNED ); +if (++tagged %XA_CHECK_SCHED ) +continue ; -xa_lock_irq (&mapping->i_pages); -radix_tree_tag_clear (&mapping->i_pages, - iter.index,MEMFD_TAG_PINNED ); -xa_unlock_irq (&mapping->i_pages); -continue_resched: -if (need_resched ()) { - slot =radix_tree_iter_resume (slot, &iter); -cond_resched_rcu (); - } +xas_pause (&xas); +xas_unlock_irq (&xas); +cond_resched (); +xas_lock_irq (&xas); } -rcu_read_unlock (); +xas_unlock_irq (&xas); }return error;
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 55/74 memfd: Convert memfd_tag_pins to XArray
In reply to Matthew WilcoxSwitch to a batch-processing model like memfd_wait_for_pins() and use the xa_state previously set up by memfd_wait_for_pins().
--- mm/memfd.c | 44 ++++++++++++++++++-------------------------- 1 file changed, 18 insertions(+), 26 deletions(-)
mm/memfd.c +18 -26
@@ -28,37 +28,29 @@#define MEMFD_TAG_PINNED PAGECACHE_TAG_TOWRITE#define LAST_SCAN 4/* about 150ms max */ -static void memfd_tag_pins (struct address_space *mapping) +static void memfd_tag_pins (struct xa_state *xas) { -struct radix_tree_iter iter; -void __rcu **slot; -pgoff_t start;struct page *page; +unsigned int tagged =0 ;lru_add_drain (); - start =0 ; -rcu_read_lock (); - -radix_tree_for_each_slot (slot, &mapping->i_pages, &iter, start) { - page =radix_tree_deref_slot (slot); -if (!page ||radix_tree_exception (page)) { -if (radix_tree_deref_retry (page)) { - slot =radix_tree_iter_retry (&iter); -continue ; - } - }else if (page_count (page) -page_mapcount (page) >1 ) { -xa_lock_irq (&mapping->i_pages); -radix_tree_tag_set (&mapping->i_pages, iter.index, -MEMFD_TAG_PINNED ); -xa_unlock_irq (&mapping->i_pages); - } -if (need_resched ()) { - slot =radix_tree_iter_resume (slot, &iter); -cond_resched_rcu (); - } +xas_lock_irq (xas); +xas_for_each (xas, page,ULONG_MAX ) { +if (xa_is_value (page)) +continue ; +if (page_count (page) -page_mapcount (page) >1 ) +xas_set_tag (xas,MEMFD_TAG_PINNED ); + +if (++tagged %XA_CHECK_SCHED ) +continue ; + +xas_pause (xas); +xas_unlock_irq (xas); +cond_resched (); +xas_lock_irq (xas); } -rcu_read_unlock (); +xas_unlock_irq (xas); }/*
@@ -76,7 +68,7 @@ static int memfd_wait_for_pins(struct address_space *mapping)struct page *page;int error, scan; -memfd_tag_pins (mapping); +memfd_tag_pins (&xas); error =0 ;for (scan =0 ; scan <=LAST_SCAN ; scan++) {
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 56/74 shmem: Comment fixups
In reply to Matthew WilcoxRemove the last mentions of radix tree from various comments.
--- mm/shmem.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
mm/shmem.c +6 -6
@@ -766,7 +766,7 @@ void shmem_unlock_mapping(struct address_space *mapping) }/* -* Remove range of pages and swap entries from radix tree, and free them. +* Remove range of pages and swap entries from page cache, and free them. * If !unfalloc, truncate or punch hole; if unfalloc, undo failed fallocate. */ static void shmem_undo_range(struct inode *inode, loff_t lstart, loff_t lend,
@@ -1146,10 +1146,10 @@ static int shmem_unuse_inode(struct shmem_inode_info *info,* We needed to drop mutex to make that restrictive page * allocation, but the inode might have been freed while we * dropped it: although a racing shmem_evict_inode() cannot -* complete without emptying the radix_tree, our page lock +* complete without emptying the page cache, our page lock * on this swapcache page is not enough to prevent that - * free_swap_and_cache() of our swap entry will only -* trylock_page(), removing swap from radix_tree whatever. +* trylock_page(), removing swap from page cache whatever. * * We must not proceed to shmem_add_to_page_cache() if the * inode has been freed, but of course we cannot rely on
@@ -1215,7 +1215,7 @@ int shmem_unuse(swp_entry_t swap, struct page *page) false);if (error) goto out; -/* No radix_tree_preload: swap entry keeps a place for page in tree */ +/* No memory allocation: swap entry occupies the slot for the page */ error = -EAGAIN ;mutex_lock (&shmem_swaplist_mutex);
@@ -1889,7 +1889,7 @@ alloc_nohuge: page = shmem_alloc_and_acct_page(gfp, inode,spin_unlock_irq (&info->lock); goto repeat; } -if (error == -EEXIST )/* from above or from radix_tree_insert */ +if (error == -EEXIST ) goto repeat;return error; }
@@ -2501,7 +2501,7 @@ static ssize_t shmem_file_read_iter(struct kiocb *iocb, struct iov_iter *to) }/* -* llseek SEEK_DATA or SEEK_HOLE through the radix_tree. +* llseek SEEK_DATA or SEEK_HOLE through the page cache. */ static pgoff_t shmem_seek_hole_data (struct address_space *mapping,pgoff_t index,pgoff_t end,int whence)
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 57/74 btrfs: Convert page cache to XArray
In reply to Matthew Wilcox--- fs/btrfs/compression.c | 4 +--- fs/btrfs/extent_io.c | 8 +++----- 2 files changed, 4 insertions(+), 8 deletions(-)
fs/btrfs/compression.c +1 -3
@@ -444,9 +444,7 @@ static noinline int add_ra_bio_pages(struct inode *inode,if (pg_index > end_index)break ; -rcu_read_lock (); - page =radix_tree_lookup (&mapping->i_pages, pg_index); -rcu_read_unlock (); + page =xa_load (&mapping->i_pages, pg_index);if (page && !xa_is_value (page)) { misses++;if (misses >4 )
fs/btrfs/extent_io.c +3 -5
@@ -5169,11 +5169,9 @@ void clear_extent_buffer_dirty(struct extent_buffer *eb)clear_page_dirty_for_io (page);xa_lock_irq (&page->mapping->i_pages); -if (!PageDirty (page)) { -radix_tree_tag_clear (&page->mapping->i_pages, -page_index (page), -PAGECACHE_TAG_DIRTY ); - } +if (!PageDirty (page)) +__xa_clear_tag (&page->mapping->i_pages, +page_index (page),PAGECACHE_TAG_DIRTY );xa_unlock_irq (&page->mapping->i_pages);ClearPageError (page);unlock_page (page);
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 58/74 fs: Convert buffer to XArray
In reply to Matthew WilcoxMostly comment fixes, but one use of __xa_set_tag.
--- fs/buffer.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
fs/buffer.c +7 -7
@@ -562,7 +562,7 @@ void mark_buffer_dirty_inode(struct buffer_head *bh, struct inode *inode)EXPORT_SYMBOL (mark_buffer_dirty_inode);/* -* Mark the page dirty, and set it dirty in the radix tree, and mark the inode +* Mark the page dirty, and set it dirty in the page cache, and mark the inode * dirty. * * If warn is true, then emit a warning if the page is not uptodate and has
@@ -579,8 +579,8 @@ void __set_page_dirty(struct page *page, struct address_space *mapping,if (page->mapping) {/* Race with truncate? */ WARN_ON_ONCE (warn && !PageUptodate (page));account_page_dirtied (page, mapping); -radix_tree_tag_set (&mapping->i_pages, -page_index (page),PAGECACHE_TAG_DIRTY ); +__xa_set_tag (&mapping->i_pages,page_index (page), +PAGECACHE_TAG_DIRTY ); }xa_unlock_irqrestore (&mapping->i_pages, flags); }
@@ -1043,7 +1043,7 @@ __getblk_slow(struct block_device *bdev, sector_t block,* The relationship between dirty buffers and dirty pages: * * Whenever a page has any dirty buffers, the page's dirty bit is set, and -* the page is tagged dirty in its radix tree. +* the page is tagged dirty in the page cache. * * At all times, the dirtiness of the buffers represents the dirtiness of * subsections of the page. If the page has buffers, the page dirty bit is
@@ -1066,9 +1066,9 @@ __getblk_slow(struct block_device *bdev, sector_t block,* mark_buffer_dirty - mark a buffer_head as needing writeout * @bh: the buffer_head to mark dirty * -* mark_buffer_dirty() will set the dirty bit against the buffer, then set its -* backing page dirty, then tag the page as dirty in its address_space's radix -* tree and then attach the address_space's inode to its superblock's dirty +* mark_buffer_dirty() will set the dirty bit against the buffer, then set +* its backing page dirty, then tag the page as dirty in the page cache +* and then attach the address_space's inode to its superblock's dirty * inode list. * * mark_buffer_dirty() is atomic. It takes bh->b_page->mapping->private_lock,
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 59/74 fs: Convert writeback to XArray
In reply to Matthew WilcoxA couple of short loops.
--- fs/fs-writeback.c | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-)
fs/fs-writeback.c +9 -16
@@ -339,9 +339,9 @@ static void inode_switch_wbs_work_fn(struct work_struct *work)struct address_space *mapping = inode->i_mapping;struct bdi_writeback *old_wb = inode->i_wb;struct bdi_writeback *new_wb = isw->new_wb; -struct radix_tree_iter iter; +XA_STATE (xas, &mapping->i_pages,0 ); +struct page *page;bool switched = false; -void **slot;/* * By the time control reaches here, RCU grace period has passed
@@ -375,25 +375,18 @@ static void inode_switch_wbs_work_fn(struct work_struct *work)* to possibly dirty pages while PAGECACHE_TAG_WRITEBACK points to * pages actually under writeback. */ -radix_tree_for_each_tagged (slot, &mapping->i_pages, &iter,0 , -PAGECACHE_TAG_DIRTY ) { -struct page *page =radix_tree_deref_slot_protected (slot, - &mapping->i_pages.xa_lock); -if (likely (page) &&PageDirty (page)) { +xas_for_each_tagged (&xas, page,ULONG_MAX ,PAGECACHE_TAG_DIRTY ) { +if (PageDirty (page)) {dec_wb_stat (old_wb,WB_RECLAIMABLE );inc_wb_stat (new_wb,WB_RECLAIMABLE ); } } -radix_tree_for_each_tagged (slot, &mapping->i_pages, &iter,0 , -PAGECACHE_TAG_WRITEBACK ) { -struct page *page =radix_tree_deref_slot_protected (slot, - &mapping->i_pages.xa_lock); -if (likely (page)) { -WARN_ON_ONCE (!PageWriteback (page)); -dec_wb_stat (old_wb,WB_WRITEBACK ); -inc_wb_stat (new_wb,WB_WRITEBACK ); - } +xas_set (&xas,0 ); +xas_for_each_tagged (&xas, page,ULONG_MAX ,PAGECACHE_TAG_WRITEBACK ) { +WARN_ON_ONCE (!PageWriteback (page)); +dec_wb_stat (old_wb,WB_WRITEBACK ); +inc_wb_stat (new_wb,WB_WRITEBACK ); }wb_get (new_wb);
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 60/74 nilfs2: Convert to XArray
In reply to Matthew WilcoxThis is close to a 1:1 replacement of radix tree APIs with their XArray equivalents. It would be possible to optimise nilfs_copy_back_pages(), but that doesn't seem to be in the performance path. Also, I think it has a pre-existing bug, and I've added a note to that effect in the source code.
--- fs/nilfs2/btnode.c | 26 +++++++++----------------- fs/nilfs2/page.c | 29 +++++++++++++---------------- 2 files changed, 22 insertions(+), 33 deletions(-)
fs/nilfs2/btnode.c +9 -17
@@ -177,24 +177,18 @@ int nilfs_btnode_prepare_change_key(struct address_space *btnc, ctxt->newbh =NULL ;if (inode->i_blkbits ==PAGE_SHIFT ) { -lock_page (obh->b_page); -/* -* We cannot call radix_tree_preload for the kernels older -* than 2.6.23, because it is not exported for modules. -*/ +struct page *opage = obh->b_page; +lock_page (opage); retry: - err =radix_tree_preload (GFP_NOFS & ~__GFP_HIGHMEM); -if (err) - goto failed_unlock;/* BUG_ON(oldkey != obh->b_page->index); */ -if (unlikely (oldkey != obh->b_page->index)) -NILFS_PAGE_BUG (obh->b_page, +if (unlikely (oldkey != opage->index)) +NILFS_PAGE_BUG (opage,"invalid oldkey %lld (newkey=%lld)" , (unsigned long long )oldkey, (unsigned long long )newkey);xa_lock_irq (&btnc->i_pages); - err =radix_tree_insert (&btnc->i_pages, newkey, obh->b_page); + err =__xa_insert (&btnc->i_pages, newkey, opage,GFP_NOFS );xa_unlock_irq (&btnc->i_pages);/* * Note: page->index will not change to newkey until
@@ -202,7 +196,6 @@ int nilfs_btnode_prepare_change_key(struct address_space *btnc,* To protect the page in intermediate state, the page lock * is held. */ -radix_tree_preload_end ();if (!err)return 0 ;else if (err != -EEXIST )
@@ -212,7 +205,7 @@ int nilfs_btnode_prepare_change_key(struct address_space *btnc,if (!err) goto retry;/* fallback to copy mode */ -unlock_page (obh->b_page); +unlock_page (opage); } nbh =nilfs_btnode_create_block (btnc, newkey);
@@ -252,9 +245,8 @@ void nilfs_btnode_commit_change_key(struct address_space *btnc,mark_buffer_dirty (obh);xa_lock_irq (&btnc->i_pages); -radix_tree_delete (&btnc->i_pages, oldkey); -radix_tree_tag_set (&btnc->i_pages, newkey, -PAGECACHE_TAG_DIRTY ); +__xa_erase (&btnc->i_pages, oldkey); +__xa_set_tag (&btnc->i_pages, newkey,PAGECACHE_TAG_DIRTY );xa_unlock_irq (&btnc->i_pages); opage->index = obh->b_blocknr = newkey;
@@ -284,7 +276,7 @@ void nilfs_btnode_abort_change_key(struct address_space *btnc,if (nbh ==NULL ) {/* blocksize == pagesize */ xa_lock_irq (&btnc->i_pages); -radix_tree_delete (&btnc->i_pages, newkey); +__xa_erase (&btnc->i_pages, newkey);xa_unlock_irq (&btnc->i_pages);unlock_page (ctxt->bh->b_page); }else
fs/nilfs2/page.c +13 -16
@@ -298,7 +298,7 @@ int nilfs_copy_dirty_pages(struct address_space *dmap,* @dmap: destination page cache * @smap: source page cache * -* No pages must no be added to the cache during this process. +* No pages must be added to the cache during this process. * This must be ensured by the caller. */ void nilfs_copy_back_pages(struct address_space *dmap,
@@ -307,7 +307,6 @@ void nilfs_copy_back_pages(struct address_space *dmap,struct pagevec pvec;unsigned int i, n;pgoff_t index =0 ; -int err;pagevec_init (&pvec); repeat:
@@ -322,35 +321,34 @@ void nilfs_copy_back_pages(struct address_space *dmap,lock_page (page); dpage =find_lock_page (dmap, offset);if (dpage) { -/* override existing page on the destination cache */ +/* overwrite existing page in the destination cache */ WARN_ON (PageDirty (dpage));nilfs_copy_page (dpage, page,0 );unlock_page (dpage);put_page (dpage); +/* Do we not need to remove page from smap here? */ }else { -struct page *page2; +struct page *p;/* move the page to the destination cache */ xa_lock_irq (&smap->i_pages); - page2 =radix_tree_delete (&smap->i_pages, offset); -WARN_ON (page2 != page); - + p =__xa_erase (&smap->i_pages, offset); +WARN_ON (page != p); smap->nrpages--;xa_unlock_irq (&smap->i_pages);xa_lock_irq (&dmap->i_pages); - err =radix_tree_insert (&dmap->i_pages, offset, page); -if (unlikely (err <0 )) { -WARN_ON (err == -EEXIST ); + p =__xa_store (&dmap->i_pages, offset, page,GFP_NOFS ); +if (unlikely (p)) { +/* Probably -ENOMEM */ page->mapping =NULL ; -put_page (page);/* for cache */ +put_page (page); }else { page->mapping = dmap; dmap->nrpages++;if (PageDirty (page)) -radix_tree_tag_set (&dmap->i_pages, - offset, -PAGECACHE_TAG_DIRTY ); +__xa_set_tag (&dmap->i_pages, offset, +PAGECACHE_TAG_DIRTY ); }xa_unlock_irq (&dmap->i_pages); }
@@ -476,8 +474,7 @@ int __nilfs_clear_page_dirty(struct page *page)if (mapping) {xa_lock_irq (&mapping->i_pages);if (test_bit (PG_dirty, &page->flags)) { -radix_tree_tag_clear (&mapping->i_pages, -page_index (page), +__xa_clear_tag (&mapping->i_pages,page_index (page),PAGECACHE_TAG_DIRTY );xa_unlock_irq (&mapping->i_pages);return clear_page_dirty_for_io (page);
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 61/74 f2fs: Convert to XArray
In reply to Matthew WilcoxThis is a straightforward conversion.
--- fs/f2fs/data.c | 4 ++-- fs/f2fs/dir.c | 2 +- fs/f2fs/f2fs.h | 2 +- fs/f2fs/inline.c | 2 +- fs/f2fs/node.c | 6 ++---- 5 files changed, 7 insertions(+), 9 deletions(-)
fs/f2fs/data.c +2 -2
@@ -2600,13 +2600,13 @@ const struct address_space_operations f2fs_dblock_aops = {#endif }; -void f2fs_clear_radix_tree_dirty_tag (struct page *page) +void f2fs_clear_page_cache_dirty_tag (struct page *page) {struct address_space *mapping =page_mapping (page);unsigned long flags;xa_lock_irqsave (&mapping->i_pages, flags); -radix_tree_tag_clear (&mapping->i_pages,page_index (page), +__xa_clear_tag (&mapping->i_pages,page_index (page),PAGECACHE_TAG_DIRTY );xa_unlock_irqrestore (&mapping->i_pages, flags); }
fs/f2fs/dir.c +1 -1
@@ -730,7 +730,7 @@ void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,if (bit_pos ==NR_DENTRY_IN_BLOCK && !f2fs_truncate_hole (dir, page->index, page->index +1 )) { -f2fs_clear_radix_tree_dirty_tag (page); +f2fs_clear_page_cache_dirty_tag (page);clear_page_dirty_for_io (page);ClearPagePrivate (page);ClearPageUptodate (page);
fs/f2fs/f2fs.h +1 -1
@@ -2973,7 +2973,7 @@ int f2fs_migrate_page(struct address_space *mapping, struct page *newpage, struct page *page, enum migrate_mode mode);#endif bool f2fs_overwrite_io (struct inode *inode,loff_t pos,size_t len); -void f2fs_clear_radix_tree_dirty_tag (struct page *page); +void f2fs_clear_page_cache_dirty_tag (struct page *page);/* * gc.c
fs/f2fs/inline.c +1 -1
@@ -226,7 +226,7 @@ int f2fs_write_inline_data(struct inode *inode, struct page *page)kunmap_atomic (src_addr);set_page_dirty (dn.inode_page); -f2fs_clear_radix_tree_dirty_tag (page); +f2fs_clear_page_cache_dirty_tag (page);set_inode_flag (inode,FI_APPEND_WRITE );set_inode_flag (inode,FI_DATA_EXIST );
fs/f2fs/node.c +2 -4
@@ -103,7 +103,7 @@ bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type)static void clear_node_page_dirty (struct page *page) {if (PageDirty (page)) { -f2fs_clear_radix_tree_dirty_tag (page); +f2fs_clear_page_cache_dirty_tag (page);clear_page_dirty_for_io (page);dec_page_count (F2FS_P_SB (page),F2FS_DIRTY_NODES ); }
@@ -1168,9 +1168,7 @@ void f2fs_ra_node_page(struct f2fs_sb_info *sbi, nid_t nid)if (f2fs_check_nid_range (sbi, nid))return ; -rcu_read_lock (); - apage =radix_tree_lookup (&NODE_MAPPING (sbi)->i_pages, nid); -rcu_read_unlock (); + apage =xa_load (&NODE_MAPPING (sbi)->i_pages, nid);if (apage)return ;
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 62/74 dax: Rename some functions
In reply to Matthew WilcoxRemove mentions of 'radix' and 'radix tree'. Simplify some names by dropping the word 'mapping'.
--- fs/dax.c | 86 +++++++++++++++++++++++++++----------------------------- 1 file changed, 42 insertions(+), 44 deletions(-)
fs/dax.c +42 -44
@@ -74,18 +74,18 @@ fs_initcall(init_dax_wait_table);#define DAX_ZERO_PAGE (1UL << 2)#define DAX_EMPTY (1UL << 3) -static unsigned long dax_radix_pfn (void *entry) +static unsigned long dax_to_pfn (void *entry) {return xa_to_value (entry) >>DAX_SHIFT ; } -static void *dax_radix_locked_entry (unsigned long pfn,unsigned long flags) +static void *dax_make_locked (unsigned long pfn,unsigned long flags) {return xa_mk_value (flags | ((unsigned long )pfn <<DAX_SHIFT ) |DAX_LOCKED ); } -static unsigned int dax_radix_order (void *entry) +static unsigned int dax_entry_order (void *entry) {if (xa_to_value (entry) &DAX_PMD )return PMD_SHIFT -PAGE_SHIFT ;
@@ -113,7 +113,7 @@ static int dax_is_empty_entry(void *entry) }/* -* DAX radix tree locking +* DAX page cache entry locking */ struct exceptional_entry_key {struct address_space *mapping;
@@ -217,7 +217,7 @@ static inline void *unlock_slot(struct address_space *mapping, void **slot) }/* -* Lookup entry in radix tree, wait for it to become unlocked if it is +* Lookup entry in page cache, wait for it to become unlocked if it is * a DAX entry and return it. The caller must call * put_unlocked_mapping_entry() when he decided not to lock the entry or * put_locked_mapping_entry() when he locked the entry and now wants to
@@ -280,7 +280,7 @@ static void put_locked_mapping_entry(struct address_space *mapping, }/* -* Called when we are done with radix tree entry we looked up via +* Called when we are done with page cache entry we looked up via * get_unlocked_mapping_entry() and which we didn't lock in the end. */ static void put_unlocked_mapping_entry(struct address_space *mapping,
@@ -289,7 +289,7 @@ static void put_unlocked_mapping_entry(struct address_space *mapping,if (!entry)return ; -/* We have to wake up next waiter for the radix tree entry lock */ +/* We have to wake up next waiter for the page cache entry lock */ dax_wake_mapping_entry_waiter (mapping, index, entry, false); }
@@ -305,9 +305,9 @@ static unsigned long dax_entry_size(void *entry)return PAGE_SIZE ; } -static unsigned long dax_radix_end_pfn (void *entry) +static unsigned long dax_end_pfn (void *entry) { -return dax_radix_pfn (entry) +dax_entry_size (entry) /PAGE_SIZE ; +return dax_to_pfn (entry) +dax_entry_size (entry) /PAGE_SIZE ; }/*
@@ -315,8 +315,8 @@ static unsigned long dax_radix_end_pfn(void *entry)* 'empty' and 'zero' entries. */ #define for_each_mapped_pfn (entry, pfn) \ - for (pfn = dax_radix_pfn(entry); \ - pfn < dax_radix_end_pfn(entry); pfn++) + for (pfn = dax_to_pfn(entry); \ + pfn < dax_end_pfn(entry); pfn++)/* * TODO: for reflink+dax we need a way to associate a single page with
@@ -449,9 +449,9 @@ void dax_unlock_page(struct page *page) }/* -* Find radix tree entry at given index. If it is a DAX entry, return it -* with the radix tree entry locked. If the radix tree doesn't contain the -* given index, create an empty entry for the index and return with it locked. +* Find page cache entry at given index. If it is a DAX entry, return it +* with the entry locked. If the page cache doesn't contain an entry at +* that index, add a locked empty entry. * * When requesting an entry with size DAX_PMD, grab_mapping_entry() will * either return that locked entry or will return an error. This error will
@@ -560,10 +560,10 @@ static void *grab_mapping_entry(struct address_space *mapping, pgoff_t index, true); } - entry =dax_radix_locked_entry (0 , size_flag |DAX_EMPTY ); + entry =dax_make_locked (0 , size_flag |DAX_EMPTY ); err =__radix_tree_insert (&mapping->i_pages, index, -dax_radix_order (entry), entry); +dax_entry_order (entry), entry);radix_tree_preload_end ();if (err) {xa_unlock_irq (&mapping->i_pages);
@@ -672,7 +672,7 @@ struct page *dax_layout_busy_page(struct address_space *mapping) }EXPORT_SYMBOL_GPL (dax_layout_busy_page); -static int __dax_invalidate_mapping_entry (struct address_space *mapping, +static int __dax_invalidate_entry (struct address_space *mapping,pgoff_t index,bool trunc) {int ret =0 ;
@@ -702,12 +702,12 @@ static int __dax_invalidate_mapping_entry(struct address_space *mapping,*/ int dax_delete_mapping_entry (struct address_space *mapping,pgoff_t index) { -int ret =__dax_invalidate_mapping_entry (mapping, index, true); +int ret =__dax_invalidate_entry (mapping, index, true);/* * This gets called from truncate / punch_hole path. As such, the caller * must hold locks protecting against concurrent modifications of the -* radix tree (usually fs-private i_mmap_sem for writing). Since the +* page cache (usually fs-private i_mmap_sem for writing). Since the * caller has seen a DAX entry for this index, we better find it * at that index as well... */
@@ -721,7 +721,7 @@ int dax_delete_mapping_entry(struct address_space *mapping, pgoff_t index)int dax_invalidate_mapping_entry_sync (struct address_space *mapping,pgoff_t index) { -return __dax_invalidate_mapping_entry (mapping, index, false); +return __dax_invalidate_entry (mapping, index, false); } static int copy_user_dax(struct block_device *bdev, struct dax_device *dax_dev,
@@ -758,10 +758,9 @@ static int copy_user_dax(struct block_device *bdev, struct dax_device *dax_dev,* already in the tree, we will skip the insertion and just dirty the PMD as * appropriate. */ -static void *dax_insert_mapping_entry (struct address_space *mapping, -struct vm_fault *vmf, -void *entry,pfn_t pfn_t, -unsigned long flags,bool dirty) +static void *dax_insert_entry (struct address_space *mapping, +struct vm_fault *vmf, +void *entry,pfn_t pfn_t,unsigned long flags,bool dirty) {struct radix_tree_root *pages = &mapping->i_pages;unsigned long pfn =pfn_t_to_pfn (pfn_t);
@@ -781,7 +780,7 @@ static void *dax_insert_mapping_entry(struct address_space *mapping, }xa_lock_irq (pages); - new_entry =dax_radix_locked_entry (pfn, flags); + new_entry =dax_make_locked (pfn, flags);if (dax_entry_size (entry) !=dax_entry_size (new_entry)) {dax_disassociate_entry (entry, mapping, false);dax_associate_entry (new_entry, mapping, vmf->vma, vmf->address);
@@ -789,9 +788,9 @@ static void *dax_insert_mapping_entry(struct address_space *mapping,if (dax_is_zero_entry (entry) ||dax_is_empty_entry (entry)) {/* -* Only swap our new entry into the radix tree if the current +* Only swap our new entry into the page cache if the current * entry is a zero page or an empty entry. If a normal PTE or -* PMD entry is already in the tree, we leave it alone. This +* PMD entry is already in the cache, we leave it alone. This * means that if we are trying to insert a PTE and the * existing entry is a PMD, we will just leave the PMD in the * tree and dirty it if necessary.
@@ -814,8 +813,8 @@ static void *dax_insert_mapping_entry(struct address_space *mapping,return entry; } -static inline unsigned long -pgoff_address (pgoff_t pgoff,struct vm_area_struct *vma) +static inline +unsigned long pgoff_address (pgoff_t pgoff,struct vm_area_struct *vma) {unsigned long address;
@@ -825,8 +824,8 @@ pgoff_address(pgoff_t pgoff, struct vm_area_struct *vma) }/* Walk all mappings of a given index of a file and writeprotect them */ -static void dax_mapping_entry_mkclean (struct address_space *mapping, -pgoff_t index,unsigned long pfn) +static void dax_entry_mkclean (struct address_space *mapping,pgoff_t index, +unsigned long pfn) {struct vm_area_struct *vma;pte_t pte, *ptep =NULL ;
@@ -922,7 +921,7 @@ static int dax_writeback_one(struct dax_device *dax_dev,* compare pfns as we must not bail out due to difference in lockbit * or entry type. */ -if (dax_radix_pfn (entry2) !=dax_radix_pfn (entry)) +if (dax_to_pfn (entry2) !=dax_to_pfn (entry)) goto put_unlocked;if (WARN_ON_ONCE (dax_is_empty_entry (entry) ||dax_is_zero_entry (entry))) {
@@ -952,10 +951,10 @@ static int dax_writeback_one(struct dax_device *dax_dev,* This allows us to flush for PMD_SIZE and not have to worry about * partial PMD writebacks. */ - pfn =dax_radix_pfn (entry); - size =PAGE_SIZE <<dax_radix_order (entry); + pfn =dax_to_pfn (entry); + size =PAGE_SIZE <<dax_entry_order (entry); -dax_mapping_entry_mkclean (mapping, index, pfn); +dax_entry_mkclean (mapping, index, pfn);dax_flush (dax_dev,page_address (pfn_to_page (pfn)), size);/* * After we have flushed the cache, we can clear the dirty tag. There
@@ -1093,7 +1092,7 @@ static vm_fault_t dax_load_hole(struct address_space *mapping, void *entry,pfn_t pfn =pfn_to_pfn_t (my_zero_pfn (vaddr));vm_fault_t ret; -dax_insert_mapping_entry (mapping, vmf, entry, pfn, +dax_insert_entry (mapping, vmf, entry, pfn,DAX_ZERO_PAGE , false); ret =vmf_insert_mixed (vmf->vma, vaddr, pfn);
@@ -1407,7 +1406,7 @@ static vm_fault_t dax_iomap_pte_fault(struct vm_fault *vmf, pfn_t *pfnp,if (error <0 ) goto error_finish_iomap; - entry =dax_insert_mapping_entry (mapping, vmf, entry, pfn, + entry =dax_insert_entry (mapping, vmf, entry, pfn,0 , write && !sync);/*
@@ -1487,7 +1486,7 @@ static vm_fault_t dax_pmd_load_hole(struct vm_fault *vmf, struct iomap *iomap, goto fallback; pfn =page_to_pfn_t (zero_page); - ret =dax_insert_mapping_entry (mapping, vmf, entry, pfn, + ret =dax_insert_entry (mapping, vmf, entry, pfn,DAX_PMD |DAX_ZERO_PAGE , false); ptl =pmd_lock (vmf->vma->vm_mm, vmf->pmd);
@@ -1540,7 +1539,7 @@ static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp,* Make sure that the faulting address's PMD offset (color) matches * the PMD offset from the start of the file. This is necessary so * that a PMD range in the page table overlaps exactly with a PMD -* range in the radix tree. +* range in the page cache. */ if ((vmf->pgoff &PG_PMD_COLOUR ) != ((vmf->address >>PAGE_SHIFT ) &PG_PMD_COLOUR ))
@@ -1608,7 +1607,7 @@ static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp,if (error <0 ) goto finish_iomap; - entry =dax_insert_mapping_entry (mapping, vmf, entry, pfn, + entry =dax_insert_entry (mapping, vmf, entry, pfn,DAX_PMD , write && !sync);/*
@@ -1701,15 +1700,14 @@ vm_fault_t dax_iomap_fault(struct vm_fault *vmf, enum page_entry_size pe_size, }EXPORT_SYMBOL_GPL (dax_iomap_fault); -/** +/* * dax_insert_pfn_mkwrite - insert PTE or PMD entry into page tables * @vmf: The description of the fault * @pe_size: Size of entry to be inserted * @pfn: PFN to insert * -* This function inserts writeable PTE or PMD entry into page tables for mmaped -* DAX file. It takes care of marking corresponding radix tree entry as dirty -* as well. +* This function inserts a writeable PTE or PMD entry into the page tables +* for an mmaped DAX file. It also marks the page cache entry as dirty. */ static vm_fault_t dax_insert_pfn_mkwrite(struct vm_fault *vmf, enum page_entry_size pe_size,
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 63/74 dax: Hash on XArray instead of mapping
In reply to Matthew WilcoxSince the XArray is embedded in the struct address_space, its address contains exactly as much entropy as the address of the mapping. This patch is purely preparatory for later patches which will simplify the wait/wake interfaces.
--- fs/dax.c | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-)
fs/dax.c +17 -15
@@ -116,7 +116,7 @@ static int dax_is_empty_entry(void *entry)* DAX page cache entry locking */ struct exceptional_entry_key { -struct address_space *mapping; +struct xarray *xa;pgoff_t entry_start; };
@@ -125,7 +125,7 @@ struct wait_exceptional_entry_queue {struct exceptional_entry_key key; }; -static wait_queue_head_t *dax_entry_waitqueue (struct address_space *mapping, +static wait_queue_head_t *dax_entry_waitqueue (struct xarray *xa,pgoff_t index,void *entry,struct exceptional_entry_key *key) {unsigned long hash;
@@ -138,21 +138,21 @@ static wait_queue_head_t *dax_entry_waitqueue(struct address_space *mapping,if (dax_is_pmd_entry (entry)) index &= ~PG_PMD_COLOUR ; - key->mapping = mapping; + key->xa = xa; key->entry_start = index; - hash =hash_long ((unsigned long )mapping ^ index,DAX_WAIT_TABLE_BITS ); + hash =hash_long ((unsigned long )xa ^ index,DAX_WAIT_TABLE_BITS );return wait_table + hash; } -static int wake_exceptional_entry_func (wait_queue_entry_t *wait,unsigned int mode, -int sync,void *keyp) +static int wake_exceptional_entry_func (wait_queue_entry_t *wait, +unsigned int mode,int sync,void *keyp) {struct exceptional_entry_key *key = keyp;struct wait_exceptional_entry_queue *ewait = container_of(wait, struct wait_exceptional_entry_queue, wait); -if (key->mapping != ewait->key.mapping || +if (key->xa != ewait->key.xa || key->entry_start != ewait->key.entry_start)return 0 ;return autoremove_wake_function (wait, mode, sync,NULL );
@@ -163,13 +163,13 @@ static int wake_exceptional_entry_func(wait_queue_entry_t *wait, unsigned int mo* The important information it's conveying is whether the entry at * this index used to be a PMD entry. */ -static void dax_wake_mapping_entry_waiter (struct address_space *mapping, +static void dax_wake_mapping_entry_waiter (struct xarray *xa,pgoff_t index,void *entry,bool wake_all) {struct exceptional_entry_key key;wait_queue_head_t *wq; - wq =dax_entry_waitqueue (mapping, index, entry, &key); + wq =dax_entry_waitqueue (xa, index, entry, &key);/* * Checking for locked entry and prepare_to_wait_exclusive() happens
@@ -246,7 +246,8 @@ static void *get_unlocked_mapping_entry(struct address_space *mapping,return entry; } - wq =dax_entry_waitqueue (mapping, index, entry, &ewait.key); + wq =dax_entry_waitqueue (&mapping->i_pages, index, entry, + &ewait.key);prepare_to_wait_exclusive (wq, &ewait.wait,TASK_UNINTERRUPTIBLE );xa_unlock_irq (&mapping->i_pages);
@@ -270,7 +271,7 @@ static void dax_unlock_mapping_entry(struct address_space *mapping, }unlock_slot (mapping, slot);xa_unlock_irq (&mapping->i_pages); -dax_wake_mapping_entry_waiter (mapping, index, entry, false); +dax_wake_mapping_entry_waiter (&mapping->i_pages, index, entry, false); } static void put_locked_mapping_entry(struct address_space *mapping,
@@ -290,7 +291,7 @@ static void put_unlocked_mapping_entry(struct address_space *mapping,return ;/* We have to wake up next waiter for the page cache entry lock */ -dax_wake_mapping_entry_waiter (mapping, index, entry, false); +dax_wake_mapping_entry_waiter (&mapping->i_pages, index, entry, false); }static unsigned long dax_entry_size (void *entry)
@@ -423,7 +424,8 @@ struct page *dax_lock_page(unsigned long pfn)break ; } - wq =dax_entry_waitqueue (mapping, index, entry, &ewait.key); + wq =dax_entry_waitqueue (&mapping->i_pages, index, entry, + &ewait.key);prepare_to_wait_exclusive (wq, &ewait.wait,TASK_UNINTERRUPTIBLE );xa_unlock_irq (&mapping->i_pages);
@@ -556,8 +558,8 @@ static void *grab_mapping_entry(struct address_space *mapping, pgoff_t index,dax_disassociate_entry (entry, mapping, false);radix_tree_delete (&mapping->i_pages, index); mapping->nrexceptional--; -dax_wake_mapping_entry_waiter (mapping, index, entry, - true); +dax_wake_mapping_entry_waiter (&mapping->i_pages, + index, entry, true); } entry =dax_make_locked (0 , size_flag |DAX_EMPTY );
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 64/74 dax: Convert dax_insert_pfn_mkwrite to XArray
In reply to Matthew WilcoxAdd some XArray-based helper functions to replace the radix tree based metaphors currently in use. The biggest change is that converted code doesn't see its own lock bit; get_unlocked_entry() always returns an entry with the lock bit clear, and locking the entry now returns void. So we don't have to mess around loading the current entry and clearing the lock bit; we can just store the entry that we were using.
--- fs/dax.c | 145 +++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 113 insertions(+), 32 deletions(-)
fs/dax.c +113 -32
@@ -38,6 +38,17 @@#define CREATE_TRACE_POINTS #include <trace/events/fs_dax.h> +static inline unsigned int pe_order (enum page_entry_size pe_size) +{ +if (pe_size ==PE_SIZE_PTE ) +return PAGE_SHIFT -PAGE_SHIFT ; +if (pe_size ==PE_SIZE_PMD ) +return PMD_SHIFT -PAGE_SHIFT ; +if (pe_size ==PE_SIZE_PUD ) +return PUD_SHIFT -PAGE_SHIFT ; +return ~0 ; +} +/* We choose 4096 entries - same as per-zone page wait tables */ #define DAX_WAIT_TABLE_BITS 12#define DAX_WAIT_TABLE_ENTRIES (1 << DAX_WAIT_TABLE_BITS)
@@ -46,6 +57,9 @@#define PG_PMD_COLOUR ((PMD_SIZE >> PAGE_SHIFT) - 1)#define PG_PMD_NR (PMD_SIZE >> PAGE_SHIFT) +/* The order of a PMD entry */ +#define PMD_ORDER (PMD_SHIFT - PAGE_SHIFT) +static wait_queue_head_t wait_table[DAX_WAIT_TABLE_ENTRIES ];static int __init init_dax_wait_table (void )
@@ -85,10 +99,15 @@ static void *dax_make_locked(unsigned long pfn, unsigned long flags) DAX_LOCKED); } +static bool dax_is_locked (void *entry) +{ +return xa_to_value (entry) &DAX_LOCKED ; +} +static unsigned int dax_entry_order (void *entry) {if (xa_to_value (entry) &DAX_PMD ) -return PMD_SHIFT -PAGE_SHIFT ; +return PMD_ORDER ;return 0 ; }
@@ -181,6 +200,77 @@ static void dax_wake_mapping_entry_waiter(struct xarray *xa,__wake_up (wq,TASK_NORMAL , wake_all ?0 :1 , &key); } +static void dax_wake_entry (struct xa_state *xas,void *entry,bool wake_all) +{ +return dax_wake_mapping_entry_waiter (xas->xa, xas->xa_index, entry, + wake_all); +} + +/* +* Look up entry in page cache, wait for it to become unlocked if it +* is a DAX entry and return it. The caller must subsequently call +* put_unlocked_entry() if it did not lock the entry or put_locked_entry() +* if it did. +* +* Must be called with the i_pages lock held. +*/ +static void *get_unlocked_entry (struct xa_state *xas) +{ +void *entry; +struct wait_exceptional_entry_queue ewait; +wait_queue_head_t *wq; + +init_wait (&ewait.wait); + ewait.wait.func = wake_exceptional_entry_func; + +for (;;) { + entry =xas_load (xas); +if (!entry ||WARN_ON_ONCE (!xa_is_value (entry)) || + !dax_is_locked (entry)) +return entry; + + wq =dax_entry_waitqueue (xas->xa, xas->xa_index, entry, + &ewait.key); +prepare_to_wait_exclusive (wq, &ewait.wait, +TASK_UNINTERRUPTIBLE ); +xas_unlock_irq (xas); +xas_reset (xas); +schedule (); +finish_wait (wq, &ewait.wait); +xas_lock_irq (xas); + } +} + +static void put_unlocked_entry (struct xa_state *xas,void *entry) +{ +/* If we were the only waiter woken, wake the next one */ +if (entry) +dax_wake_entry (xas, entry, false); +} + +/* +* We used the xa_state to get the entry, but then we locked the entry and +* dropped the xa_lock, so we know the xa_state is stale and must be reset +* before use. +*/ +static void put_locked_entry (struct xa_state *xas,void *entry) +{ +void *old; + +xas_reset (xas); +xas_lock_irq (xas); + old =xas_store (xas, entry); +xas_unlock_irq (xas); +BUG_ON (!dax_is_locked (old)); +dax_wake_entry (xas, entry, false); +} + +static void dax_lock_entry (struct xa_state *xas,void *entry) +{ +unsigned long v =xa_to_value (entry); +xas_store (xas,xa_mk_value (v |DAX_LOCKED )); +} +/* * Check whether the given slot is locked. Must be called with the i_pages * lock held.
@@ -1705,50 +1795,46 @@ EXPORT_SYMBOL_GPL(dax_iomap_fault);/* * dax_insert_pfn_mkwrite - insert PTE or PMD entry into page tables * @vmf: The description of the fault -* @pe_size: Size of entry to be inserted * @pfn: PFN to insert +* @order: Order of entry to insert. * * This function inserts a writeable PTE or PMD entry into the page tables * for an mmaped DAX file. It also marks the page cache entry as dirty. */ -static vm_fault_t dax_insert_pfn_mkwrite (struct vm_fault *vmf, -enum page_entry_size pe_size, -pfn_t pfn) +static vm_fault_t +dax_insert_pfn_mkwrite (struct vm_fault *vmf,pfn_t pfn,unsigned int order) {struct address_space *mapping = vmf->vma->vm_file->f_mapping; -void *entry, **slot; -pgoff_t index = vmf->pgoff; +XA_STATE_ORDER (xas, &mapping->i_pages, vmf->pgoff, order); +void *entry;vm_fault_t ret; -xa_lock_irq (&mapping->i_pages); - entry =get_unlocked_mapping_entry (mapping, index, &slot); +xas_lock_irq (&xas); + entry =get_unlocked_entry (&xas);/* Did we race with someone splitting entry or so? */ if (!entry || - (pe_size ==PE_SIZE_PTE && !dax_is_pte_entry (entry)) || - (pe_size ==PE_SIZE_PMD && !dax_is_pmd_entry (entry))) { -put_unlocked_mapping_entry (mapping, index, entry); -xa_unlock_irq (&mapping->i_pages); + (order ==0 && !dax_is_pte_entry (entry)) || + (order ==PMD_ORDER && (xa_is_internal (entry) || + !dax_is_pmd_entry (entry)))) { +put_unlocked_entry (&xas, entry); +xas_unlock_irq (&xas);trace_dax_insert_pfn_mkwrite_no_entry (mapping->host, vmf,VM_FAULT_NOPAGE );return VM_FAULT_NOPAGE ; } -radix_tree_tag_set (&mapping->i_pages, index,PAGECACHE_TAG_DIRTY ); - entry =lock_slot (mapping, slot); -xa_unlock_irq (&mapping->i_pages); -switch (pe_size) { -case PE_SIZE_PTE : +xas_set_tag (&xas,PAGECACHE_TAG_DIRTY ); +dax_lock_entry (&xas, entry); +xas_unlock_irq (&xas); +if (order ==0 ) ret =vmf_insert_mixed_mkwrite (vmf->vma, vmf->address, pfn); -break ;#ifdef CONFIG_FS_DAX_PMD -case PE_SIZE_PMD : + else if (order == PMD_ORDER) ret =vmf_insert_pfn_pmd (vmf->vma, vmf->address, vmf->pmd, pfn, true); -break ;#endif -default : + else ret =VM_FAULT_FALLBACK ; - } -put_locked_mapping_entry (mapping, index); +put_locked_entry (&xas, entry);trace_dax_insert_pfn_mkwrite (mapping->host, vmf, ret);return ret; }
@@ -1768,17 +1854,12 @@ vm_fault_t dax_finish_sync_fault(struct vm_fault *vmf, {int err;loff_t start = ((loff_t )vmf->pgoff) <<PAGE_SHIFT ; -size_t len =0 ; +unsigned int order =pe_order (pe_size); +size_t len =PAGE_SIZE << order; -if (pe_size ==PE_SIZE_PTE ) - len =PAGE_SIZE ; -else if (pe_size ==PE_SIZE_PMD ) - len =PMD_SIZE ; -else -WARN_ON_ONCE (1 ); err =vfs_fsync_range (vmf->vma->vm_file, start, start + len -1 ,1 );if (err)return VM_FAULT_SIGBUS ; -return dax_insert_pfn_mkwrite (vmf, pe_size, pfn); +return dax_insert_pfn_mkwrite (vmf, pfn, order); }EXPORT_SYMBOL_GPL (dax_finish_sync_fault);
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 65/74 dax: Convert dax_layout_busy_page to XArray
In reply to Matthew WilcoxInstead of using a pagevec, just use the XArray iterators. Add a conditional rescheduling point which probably should have been there in the original.
--- fs/dax.c | 57 +++++++++++++++++++++----------------------------------- 1 file changed, 21 insertions(+), 36 deletions(-)
fs/dax.c +21 -36
@@ -697,11 +697,10 @@ static void *grab_mapping_entry(struct address_space *mapping, pgoff_t index,*/ struct page *dax_layout_busy_page (struct address_space *mapping) { -pgoff_t indices[PAGEVEC_SIZE ]; +XA_STATE (xas, &mapping->i_pages,0 ); +void *entry; +unsigned int scanned =0 ;struct page *page =NULL ; -struct pagevec pvec; -pgoff_t index, end; -unsigned i;/* * In the 'limited' case get_user_pages() for dax is disabled.
@@ -712,13 +711,9 @@ struct page *dax_layout_busy_page(struct address_space *mapping)if (!dax_mapping (mapping) || !mapping_mapped (mapping))return NULL ; -pagevec_init (&pvec); - index =0 ; - end =-1 ; -/* * If we race get_user_pages_fast() here either we'll see the -* elevated page count in the pagevec_lookup and wait, or +* elevated page count in the iteration and wait, or * get_user_pages_fast() will see that the page it took a reference * against is no longer mapped in the page tables and bail to the * get_user_pages() slow path. The slow path is protected by
@@ -730,36 +725,26 @@ struct page *dax_layout_busy_page(struct address_space *mapping)*/ unmap_mapping_range (mapping,0 ,0 ,1 ); -while (index < end &&pagevec_lookup_entries (&pvec, mapping, index, -min (end - index, (pgoff_t )PAGEVEC_SIZE ), - indices)) { -for (i =0 ; i <pagevec_count (&pvec); i++) { -struct page *pvec_ent = pvec.pages[i]; -void *entry; - - index = indices[i]; -if (index >= end) -break ; - -if (!xa_is_value (pvec_ent)) -continue ; - -xa_lock_irq (&mapping->i_pages); - entry =get_unlocked_mapping_entry (mapping, index,NULL ); -if (entry) - page =dax_busy_page (entry); -put_unlocked_mapping_entry (mapping, index, entry); -xa_unlock_irq (&mapping->i_pages); -if (page) -break ; - } -pagevec_remove_exceptionals (&pvec); -pagevec_release (&pvec); - index++; - +xas_lock_irq (&xas); +xas_for_each (&xas, entry,ULONG_MAX ) { +if (!xa_is_value (entry)) +continue ; +if (unlikely (dax_is_locked (entry))) + entry =get_unlocked_entry (&xas); +if (entry) + page =dax_busy_page (entry); +put_unlocked_entry (&xas, entry);if (page)break ; +if (++scanned %XA_CHECK_SCHED ) +continue ; + +xas_pause (&xas); +xas_unlock_irq (&xas); +cond_resched (); +xas_lock_irq (&xas); } +xas_unlock_irq (&xas);return page; }EXPORT_SYMBOL_GPL (dax_layout_busy_page);
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 66/74 dax: Convert __dax_invalidate_entry to XArray
In reply to Matthew WilcoxAvoids walking the radix tree multiple times looking for tags.
--- fs/dax.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-)
fs/dax.c +9 -8
@@ -752,27 +752,28 @@ EXPORT_SYMBOL_GPL(dax_layout_busy_page);static int __dax_invalidate_entry (struct address_space *mapping,pgoff_t index,bool trunc) { +XA_STATE (xas, &mapping->i_pages, index);int ret =0 ;void *entry; -struct radix_tree_root *pages = &mapping->i_pages; -xa_lock_irq (pages); - entry =get_unlocked_mapping_entry (mapping, index,NULL ); +xas_lock_irq (&xas); + entry =get_unlocked_entry (&xas);if (!entry ||WARN_ON_ONCE (!xa_is_value (entry))) goto out;if (!trunc && - (radix_tree_tag_get (pages, index,PAGECACHE_TAG_DIRTY ) || -radix_tree_tag_get (pages, index,PAGECACHE_TAG_TOWRITE ))) + (xas_get_tag (&xas,PAGECACHE_TAG_DIRTY ) || +xas_get_tag (&xas,PAGECACHE_TAG_TOWRITE ))) goto out;dax_disassociate_entry (entry, mapping, trunc); -radix_tree_delete (pages, index); +xas_store (&xas,NULL ); mapping->nrexceptional--; ret =1 ; out: -put_unlocked_mapping_entry (mapping, index, entry); -xa_unlock_irq (pages); +put_unlocked_entry (&xas, entry); +xas_unlock_irq (&xas);return ret; } +/* * Delete DAX entry at @index from @mapping. Wait for it * to be unlocked before deleting it.
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 67/74 dax: Convert dax writeback to XArray
In reply to Matthew WilcoxUse XArray iteration instead of a pagevec.
--- fs/dax.c | 130 ++++++++++++++++++++++++++----------------------------- 1 file changed, 62 insertions(+), 68 deletions(-)
fs/dax.c +62 -68
@@ -973,11 +973,9 @@ static void dax_entry_mkclean(struct address_space *mapping, pgoff_t index,i_mmap_unlock_read (mapping); } -static int dax_writeback_one (struct dax_device *dax_dev, -struct address_space *mapping,pgoff_t index,void *entry) +static int dax_writeback_one (struct xa_state *xas,struct dax_device *dax_dev, +struct address_space *mapping,void *entry) { -struct radix_tree_root *pages = &mapping->i_pages; -void *entry2, **slot;unsigned long pfn;long ret =0 ;size_t size;
@@ -989,29 +987,35 @@ static int dax_writeback_one(struct dax_device *dax_dev,if (WARN_ON (!xa_is_value (entry)))return -EIO ; -xa_lock_irq (pages); - entry2 =get_unlocked_mapping_entry (mapping, index, &slot); -/* Entry got punched out / reallocated? */ -if (!entry2 ||WARN_ON_ONCE (!xa_is_value (entry2))) - goto put_unlocked; -/* -* Entry got reallocated elsewhere? No need to writeback. We have to -* compare pfns as we must not bail out due to difference in lockbit -* or entry type. -*/ -if (dax_to_pfn (entry2) !=dax_to_pfn (entry)) - goto put_unlocked; -if (WARN_ON_ONCE (dax_is_empty_entry (entry) || -dax_is_zero_entry (entry))) { - ret = -EIO ; - goto put_unlocked; +if (unlikely (dax_is_locked (entry))) { +void *old_entry = entry; + + entry =get_unlocked_entry (xas); + +/* Entry got punched out / reallocated? */ +if (!entry ||WARN_ON_ONCE (!xa_is_value (entry))) + goto put_unlocked; +/* +* Entry got reallocated elsewhere? No need to writeback. +* We have to compare pfns as we must not bail out due to +* difference in lockbit or entry type. +*/ +if (dax_to_pfn (old_entry) !=dax_to_pfn (entry)) + goto put_unlocked; +if (WARN_ON_ONCE (dax_is_empty_entry (entry) || +dax_is_zero_entry (entry))) { + ret = -EIO ; + goto put_unlocked; + } + +/* Another fsync thread may have already done this entry */ +if (!xas_get_tag (xas,PAGECACHE_TAG_TOWRITE )) + goto put_unlocked; } -/* Another fsync thread may have already written back this entry */ -if (!radix_tree_tag_get (pages, index,PAGECACHE_TAG_TOWRITE )) - goto put_unlocked;/* Lock the entry to serialize with page faults */ - entry =lock_slot (mapping, slot); +dax_lock_entry (xas, entry); +/* * We can clear the tag now but we have to be careful so that concurrent * dax_writeback_one() calls for the same index cannot finish before we
@@ -1019,8 +1023,8 @@ static int dax_writeback_one(struct dax_device *dax_dev,* at the entry only under the i_pages lock and once they do that * they will see the entry locked and wait for it to unlock. */ -radix_tree_tag_clear (pages, index,PAGECACHE_TAG_TOWRITE ); -xa_unlock_irq (pages); +xas_clear_tag (xas,PAGECACHE_TAG_TOWRITE ); +xas_unlock_irq (xas);/* * Even if dax_writeback_mapping_range() was given a wbc->range_start
@@ -1032,7 +1036,7 @@ static int dax_writeback_one(struct dax_device *dax_dev, pfn =dax_to_pfn (entry); size =PAGE_SIZE <<dax_entry_order (entry); -dax_entry_mkclean (mapping, index, pfn); +dax_entry_mkclean (mapping, xas->xa_index, pfn);dax_flush (dax_dev,page_address (pfn_to_page (pfn)), size);/* * After we have flushed the cache, we can clear the dirty tag. There
@@ -1040,16 +1044,18 @@ static int dax_writeback_one(struct dax_device *dax_dev,* the pfn mappings are writeprotected and fault waits for mapping * entry lock. */ -xa_lock_irq (pages); -radix_tree_tag_clear (pages, index,PAGECACHE_TAG_DIRTY ); -xa_unlock_irq (pages); -trace_dax_writeback_one (mapping->host, index, size >>PAGE_SHIFT ); -put_locked_mapping_entry (mapping, index); +xas_reset (xas); +xas_lock_irq (xas); +xas_store (xas, entry); +xas_clear_tag (xas,PAGECACHE_TAG_DIRTY ); +dax_wake_entry (xas, entry, false); + +trace_dax_writeback_one (mapping->host, xas->xa_index, + size >>PAGE_SHIFT );return ret; put_unlocked: -put_unlocked_mapping_entry (mapping, index, entry2); -xa_unlock_irq (pages); +put_unlocked_entry (xas, entry);return ret; }
@@ -1061,13 +1067,13 @@ static int dax_writeback_one(struct dax_device *dax_dev,int dax_writeback_mapping_range (struct address_space *mapping,struct block_device *bdev,struct writeback_control *wbc) { +XA_STATE (xas, &mapping->i_pages, wbc->range_start >>PAGE_SHIFT );struct inode *inode = mapping->host; -pgoff_t start_index, end_index; -pgoff_t indices[PAGEVEC_SIZE ]; +pgoff_t end_index = wbc->range_end >>PAGE_SHIFT ;struct dax_device *dax_dev; -struct pagevec pvec; -bool done = false; -int i, ret =0 ; +void *entry; +int ret =0 ; +unsigned int scanned =0 ;if (WARN_ON_ONCE (inode->i_blkbits !=PAGE_SHIFT ))return -EIO ;
@@ -1079,41 +1085,29 @@ int dax_writeback_mapping_range(struct address_space *mapping,if (!dax_dev)return -EIO ; - start_index = wbc->range_start >>PAGE_SHIFT ; - end_index = wbc->range_end >>PAGE_SHIFT ; - -trace_dax_writeback_range (inode, start_index, end_index); - -tag_pages_for_writeback (mapping, start_index, end_index); +trace_dax_writeback_range (inode, xas.xa_index, end_index); -pagevec_init (&pvec); -while (!done) { - pvec.nr =find_get_entries_tag (mapping, start_index, -PAGECACHE_TAG_TOWRITE ,PAGEVEC_SIZE , - pvec.pages, indices); +tag_pages_for_writeback (mapping, xas.xa_index, end_index); -if (pvec.nr ==0 ) +xas_lock_irq (&xas); +xas_for_each_tagged (&xas, entry, end_index,PAGECACHE_TAG_TOWRITE ) { + ret =dax_writeback_one (&xas, dax_dev, mapping, entry); +if (ret <0 ) { +mapping_set_error (mapping, ret);break ; - -for (i =0 ; i < pvec.nr; i++) { -if (indices[i] > end_index) { - done = true; -break ; - } - - ret =dax_writeback_one (dax_dev, mapping, indices[i], - pvec.pages[i]); -if (ret <0 ) { -mapping_set_error (mapping, ret); - goto out; - } } - start_index = indices[pvec.nr -1 ] +1 ; +if (++scanned %XA_CHECK_SCHED ) +continue ; + +xas_pause (&xas); +xas_unlock_irq (&xas); +cond_resched (); +xas_lock_irq (&xas); } -out: +xas_unlock_irq (&xas);put_dax (dax_dev); -trace_dax_writeback_range_done (inode, start_index, end_index); -return (ret <0 ? ret :0 ); +trace_dax_writeback_range_done (inode, xas.xa_index, end_index); +return ret; }EXPORT_SYMBOL_GPL (dax_writeback_mapping_range);
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 68/74 dax: Convert dax_lock_page to XArray
In reply to Matthew Wilcox--- fs/dax.c | 96 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 50 insertions(+), 46 deletions(-)
fs/dax.c +50 -46
@@ -99,6 +99,22 @@ static void *dax_make_locked(unsigned long pfn, unsigned long flags) DAX_LOCKED); } +static unsigned long dax_is_pmd_entry (void *entry) +{ +return xa_to_value (entry) &DAX_PMD ; +} + +static void *dax_make_entry (pfn_t pfn,unsigned long flags) +{ +return xa_mk_value (flags | (pfn_t_to_pfn (pfn) <<DAX_SHIFT )); +} + +static void *dax_make_page_entry (struct page *page,void *entry) +{ +pfn_t pfn =page_to_pfn_t (page); +return dax_make_entry (pfn,dax_is_pmd_entry (entry)); +} +static bool dax_is_locked (void *entry) {return xa_to_value (entry) &DAX_LOCKED ;
@@ -111,11 +127,6 @@ static unsigned int dax_entry_order(void *entry)return 0 ; } -static int dax_is_pmd_entry (void *entry) -{ -return xa_to_value (entry) &DAX_PMD ; -} -static int dax_is_pte_entry (void *entry) {return !(xa_to_value (entry) &DAX_PMD );
@@ -466,78 +477,71 @@ static struct page *dax_busy_page(void *entry)struct page *dax_lock_page (unsigned long pfn) { -pgoff_t index; -struct inode *inode; -wait_queue_head_t *wq; -void *entry =NULL , **slot; +struct page *page =pfn_to_page (pfn); +XA_STATE (xas,NULL ,0 ); +void *entry;struct address_space *mapping; -struct wait_exceptional_entry_queue ewait; -struct page *ret =NULL , *page =pfn_to_page (pfn); -rcu_read_lock ();for (;;) { +rcu_read_lock (); mapping =READ_ONCE (page->mapping); -if (!mapping || !IS_DAX (mapping->host)) +if (!mapping || !IS_DAX (mapping->host)) { + page =NULL ;break ; + }/* * In the device-dax case there's no need to lock, a * struct dev_pagemap pin is sufficient to keep the * inode alive. */ - inode = mapping->host; -if (S_ISCHR (inode->i_mode)) { - ret = page; +if (S_ISCHR (mapping->host->i_mode))break ; - } -xa_lock_irq (&mapping->i_pages); + xas.xa = &mapping->i_pages; +xas_lock_irq (&xas); +rcu_read_unlock ();if (mapping != page->mapping) {xa_unlock_irq (&mapping->i_pages);continue ; } - index = page->index; - -init_wait (&ewait.wait); - ewait.wait.func = wake_exceptional_entry_func; - - entry =__radix_tree_lookup (&mapping->i_pages, index,NULL , - &slot); -if (!entry ||WARN_ON_ONCE (!xa_is_value (entry))) { -xa_unlock_irq (&mapping->i_pages); -break ; - }else if (!slot_locked (mapping, slot)) { -lock_slot (mapping, slot); - ret = page; -xa_unlock_irq (&mapping->i_pages); -break ; +xas_set (&xas, page->index); + entry =xas_load (&xas); +if (dax_is_locked (entry)) { + entry =get_unlocked_entry (&xas); +/* Did the page move while we slept? */ +if (dax_to_pfn (entry) != pfn) { +xas_unlock_irq (&xas); +continue ; + } } - - wq =dax_entry_waitqueue (&mapping->i_pages, index, entry, - &ewait.key); -prepare_to_wait_exclusive (wq, &ewait.wait, -TASK_UNINTERRUPTIBLE ); -xa_unlock_irq (&mapping->i_pages); -rcu_read_unlock (); -schedule (); -finish_wait (wq, &ewait.wait); -rcu_read_lock (); +dax_lock_entry (&xas, entry); +xas_unlock_irq (&xas); + goto out; }rcu_read_unlock (); +out:return page; }void dax_unlock_page (struct page *page) {struct address_space *mapping = page->mapping; -struct inode *inode = mapping->host; +XA_STATE (xas, &mapping->i_pages, page->index); +void *entry; -if (S_ISCHR (inode->i_mode)) +if (S_ISCHR (mapping->host->i_mode))return ; -dax_unlock_mapping_entry (mapping, page->index); +xas_lock_irq (&xas); + entry =xas_load (&xas); +BUG_ON (!dax_is_locked (entry)); + entry =dax_make_page_entry (page, entry); +xas_store (&xas, entry); +dax_wake_entry (&xas, entry, false); +xas_unlock_irq (&xas); }/*
Signature · 1 line
2.17.1
Re: [PATCH v14 68/74] dax: Convert dax_lock_page to XArray
Quoted 2 lines · Matthew Wilcox
Signed-off-by Matthew Wilcox <[email protected]>---
<>
fs/dax.c static void *dax_make_locked(unsigned long pfn, unsigned long flags) · line 116
Quoted 5 lines
fs/dax.c +5
-0+static void *dax_make_page_entry (struct page *page,void *entry) +{ +pfn_t pfn =page_to_pfn_t (page); +return dax_make_entry (pfn,dax_is_pmd_entry (entry)); +}
This function is defined and never used, so we get:
fs/dax.c:106:14: warning: ‘dax_make_page_entry’ defined but not used [-Wunused-function]
static void *dax_make_page_entry(struct page *page, void *entry)
^~~~~~~~~~~~~~~~~~~
PATCH 1/1 fs/dax: remove unused function
When CONFIG_FS_DAX is enabled we get this warning:fs/dax.c:106:14: warning: ‘dax_make_page_entry’ defined but not used
[-Wunused-function]
static void *dax_make_page_entry(struct page *page, void *entry)
^~~~~~~~~~~~~~~~~~~
Remove the unused function.
--- fs/dax.c | 6 ------ 1 file changed, 6 deletions(-)
fs/dax.c +0 -6
@@ -103,12 +103,6 @@ static void *dax_make_entry(pfn_t pfn, unsigned long flags)return xa_mk_value (flags | (pfn_t_to_pfn (pfn) <<DAX_SHIFT )); } -static void *dax_make_page_entry (struct page *page,void *entry) -{ -pfn_t pfn =page_to_pfn_t (page); -return dax_make_entry (pfn,dax_is_pmd_entry (entry)); -} -static bool dax_is_locked (void *entry) {return xa_to_value (entry) &DAX_LOCKED ;
Signature · 1 line
2.18.0
Matthew Wilcox
maintainerRe: [PATCH v14 68/74] dax: Convert dax_lock_page to XArray
In reply to Ross ZwislerQuoted 12 lines · Ross Zwisler
Quoted 2 lines · Matthew Wilcox
Signed-off-by Matthew Wilcox <[email protected]>---
<>
fs/dax.c static void *dax_make_locked(unsigned long pfn, unsigned long flags) · line 116
Quoted 5 lines
fs/dax.c +5
-0+static void *dax_make_page_entry (struct page *page,void *entry) +{ +pfn_t pfn =page_to_pfn_t (page); +return dax_make_entry (pfn,dax_is_pmd_entry (entry)); +}This function is defined and never used, so we get:
Quoted 3 lines
fs/dax.c:106:14: warning: ‘dax_make_page_entry’ defined but not used [-Wunused-function]
static void *dax_make_page_entry(struct page *page, void *entry)
^~~~~~~~~~~~~~~~~~~
Yeah, it was used in one of the functions Dan added, then removed. I understand he's planning on bringing that back before 4.19 and I'm going to rebase on top of that, so I've left it there for now.
Matthew Wilcox
maintainerPATCH v14 69/74 dax: Convert page fault handlers to XArray
In reply to Matthew WilcoxThis is the last part of DAX to be converted to the XArray so remove all the old helper functions.
--- fs/dax.c | 392 ++++++++++++++++--------------------------------------- 1 file changed, 111 insertions(+), 281 deletions(-)
fs/dax.c +111 -281
@@ -93,12 +93,6 @@ static unsigned long dax_to_pfn(void *entry)return xa_to_value (entry) >>DAX_SHIFT ; } -static void *dax_make_locked (unsigned long pfn,unsigned long flags) -{ -return xa_mk_value (flags | ((unsigned long )pfn <<DAX_SHIFT ) | -DAX_LOCKED ); -} -static unsigned long dax_is_pmd_entry (void *entry) {return xa_to_value (entry) &DAX_PMD ;
@@ -155,10 +149,11 @@ struct wait_exceptional_entry_queue {struct exceptional_entry_key key; }; -static wait_queue_head_t *dax_entry_waitqueue (struct xarray *xa, -pgoff_t index,void *entry,struct exceptional_entry_key *key) +static wait_queue_head_t *dax_entry_waitqueue (struct xa_state *xas, +void *entry,struct exceptional_entry_key *key) {unsigned long hash; +unsigned long index = xas->xa_index;/* * If 'entry' is a PMD, align the 'index' that we use for the wait
@@ -167,11 +162,10 @@ static wait_queue_head_t *dax_entry_waitqueue(struct xarray *xa,*/ if (dax_is_pmd_entry (entry)) index &= ~PG_PMD_COLOUR ; - - key->xa = xa; + key->xa = xas->xa; key->entry_start = index; - hash =hash_long ((unsigned long )xa ^ index,DAX_WAIT_TABLE_BITS ); + hash =hash_long ((unsigned long )xas->xa ^ index,DAX_WAIT_TABLE_BITS );return wait_table + hash; }
@@ -193,13 +187,12 @@ static int wake_exceptional_entry_func(wait_queue_entry_t *wait,* The important information it's conveying is whether the entry at * this index used to be a PMD entry. */ -static void dax_wake_mapping_entry_waiter (struct xarray *xa, -pgoff_t index,void *entry,bool wake_all) +static void dax_wake_entry (struct xa_state *xas,void *entry,bool wake_all) {struct exceptional_entry_key key;wait_queue_head_t *wq; - wq =dax_entry_waitqueue (xa, index, entry, &key); + wq =dax_entry_waitqueue (xas, entry, &key);/* * Checking for locked entry and prepare_to_wait_exclusive() happens
@@ -211,12 +204,6 @@ static void dax_wake_mapping_entry_waiter(struct xarray *xa,__wake_up (wq,TASK_NORMAL , wake_all ?0 :1 , &key); } -static void dax_wake_entry (struct xa_state *xas,void *entry,bool wake_all) -{ -return dax_wake_mapping_entry_waiter (xas->xa, xas->xa_index, entry, - wake_all); -} -/* * Look up entry in page cache, wait for it to become unlocked if it * is a DAX entry and return it. The caller must subsequently call
@@ -240,8 +227,7 @@ static void *get_unlocked_entry(struct xa_state *xas) !dax_is_locked(entry))return entry; - wq =dax_entry_waitqueue (xas->xa, xas->xa_index, entry, - &ewait.key); + wq =dax_entry_waitqueue (xas, entry, &ewait.key);prepare_to_wait_exclusive (wq, &ewait.wait,TASK_UNINTERRUPTIBLE );xas_unlock_irq (xas);
@@ -282,119 +268,6 @@ static void dax_lock_entry(struct xa_state *xas, void *entry)xas_store (xas,xa_mk_value (v |DAX_LOCKED )); } -/* -* Check whether the given slot is locked. Must be called with the i_pages -* lock held. -*/ -static inline int slot_locked (struct address_space *mapping,void **slot) -{ -unsigned long entry =xa_to_value ( -radix_tree_deref_slot_protected (slot, &mapping->i_pages.xa_lock)); -return entry &DAX_LOCKED ; -} - -/* -* Mark the given slot as locked. Must be called with the i_pages lock held. -*/ -static inline void *lock_slot (struct address_space *mapping,void **slot) -{ -unsigned long v =xa_to_value ( -radix_tree_deref_slot_protected (slot, &mapping->i_pages.xa_lock)); -void *entry =xa_mk_value (v |DAX_LOCKED ); -radix_tree_replace_slot (&mapping->i_pages, slot, entry); -return entry; -} - -/* -* Mark the given slot as unlocked. Must be called with the i_pages lock held. -*/ -static inline void *unlock_slot (struct address_space *mapping,void **slot) -{ -unsigned long v =xa_to_value ( -radix_tree_deref_slot_protected (slot, &mapping->i_pages.xa_lock)); -void *entry =xa_mk_value (v & ~DAX_LOCKED ); -radix_tree_replace_slot (&mapping->i_pages, slot, entry); -return entry; -} - -/* -* Lookup entry in page cache, wait for it to become unlocked if it is -* a DAX entry and return it. The caller must call -* put_unlocked_mapping_entry() when he decided not to lock the entry or -* put_locked_mapping_entry() when he locked the entry and now wants to -* unlock it. -* -* Must be called with the i_pages lock held. -*/ -static void *get_unlocked_mapping_entry (struct address_space *mapping, -pgoff_t index,void ***slotp) -{ -void *entry, **slot; -struct wait_exceptional_entry_queue ewait; -wait_queue_head_t *wq; - -init_wait (&ewait.wait); - ewait.wait.func = wake_exceptional_entry_func; - -for (;;) { - entry =__radix_tree_lookup (&mapping->i_pages, index,NULL , - &slot); -if (!entry || -WARN_ON_ONCE (!xa_is_value (entry)) || - !slot_locked (mapping, slot)) { -if (slotp) - *slotp = slot; -return entry; - } - - wq =dax_entry_waitqueue (&mapping->i_pages, index, entry, - &ewait.key); -prepare_to_wait_exclusive (wq, &ewait.wait, -TASK_UNINTERRUPTIBLE ); -xa_unlock_irq (&mapping->i_pages); -schedule (); -finish_wait (wq, &ewait.wait); -xa_lock_irq (&mapping->i_pages); - } -} - -static void dax_unlock_mapping_entry (struct address_space *mapping, -pgoff_t index) -{ -void *entry, **slot; - -xa_lock_irq (&mapping->i_pages); - entry =__radix_tree_lookup (&mapping->i_pages, index,NULL , &slot); -if (WARN_ON_ONCE (!entry || !xa_is_value (entry) || - !slot_locked (mapping, slot))) { -xa_unlock_irq (&mapping->i_pages); -return ; - } -unlock_slot (mapping, slot); -xa_unlock_irq (&mapping->i_pages); -dax_wake_mapping_entry_waiter (&mapping->i_pages, index, entry, false); -} - -static void put_locked_mapping_entry (struct address_space *mapping, -pgoff_t index) -{ -dax_unlock_mapping_entry (mapping, index); -} - -/* -* Called when we are done with page cache entry we looked up via -* get_unlocked_mapping_entry() and which we didn't lock in the end. -*/ -static void put_unlocked_mapping_entry (struct address_space *mapping, -pgoff_t index,void *entry) -{ -if (!entry) -return ; - -/* We have to wake up next waiter for the page cache entry lock */ -dax_wake_mapping_entry_waiter (&mapping->i_pages, index, entry, false); -} -static unsigned long dax_entry_size (void *entry) {if (dax_is_zero_entry (entry))
@@ -550,9 +423,9 @@ void dax_unlock_page(struct page *page)* that index, add a locked empty entry. * * When requesting an entry with size DAX_PMD, grab_mapping_entry() will -* either return that locked entry or will return an error. This error will -* happen if there are any 4k entries within the 2MiB range that we are -* requesting. +* either return that locked entry or will return VM_FAULT_FALLBACK. +* This will happen if there are any 4k entries within the 2MiB range +* that we are requesting. * * We always favor 4k entries over 2MiB entries. There isn't a flow where we * evict 4k entries in order to 'upgrade' them to a 2MiB entry. A 2MiB
@@ -568,29 +441,31 @@ void dax_unlock_page(struct page *page)* Note: Unlike filemap_fault() we don't honor FAULT_FLAG_RETRY flags. For * persistent memory the benefit is doubtful. We can add that later if we can * show it helps. +* +* On error, this function does not return an ERR_PTR. Instead it returns +* a VM_FAULT code, encoded as an xarray internal entry. The ERR_PTR values +* overlap with xarray value entries. */ -static void *grab_mapping_entry (struct address_space *mapping,pgoff_t index, -unsigned long size_flag) +static +void *grab_mapping_entry (struct xa_state *xas,struct address_space *mapping) {bool pmd_downgrade = false;/* splitting 2MiB entry into 4k entries? */ -void *entry, **slot; - -restart: -xa_lock_irq (&mapping->i_pages); - entry =get_unlocked_mapping_entry (mapping, index, &slot); - -if (WARN_ON_ONCE (entry && !xa_is_value (entry))) { - entry =ERR_PTR (-EIO ); - goto out_unlock; - } +void *locked =dax_make_entry (pfn_to_pfn_t (0 ), +DAX_EMPTY |DAX_LOCKED ); +void *unlocked =dax_make_entry (pfn_to_pfn_t (0 ),DAX_EMPTY ); +void *entry; -if (entry) { -if (size_flag &DAX_PMD ) { +retry: +xas_lock_irq (xas); +xas_for_each_conflict (xas, entry) { +if (dax_is_locked (entry)) + entry =get_unlocked_entry (xas); +if (!xa_is_value (entry)) + goto error; +if (xas->xa_shift) {if (dax_is_pte_entry (entry)) { -put_unlocked_mapping_entry (mapping, index, - entry); - entry =ERR_PTR (-EEXIST ); - goto out_unlock; +put_unlocked_entry (xas, entry); + goto fallback; } }else {/* trying to grab a PTE entry */ if (dax_is_pmd_entry(entry) &&
@@ -599,89 +474,56 @@ static void *grab_mapping_entry(struct address_space *mapping, pgoff_t index, pmd_downgrade = true; } } +dax_lock_entry (xas, entry); +break ;/* We don't want to overwrite an existing entry */ } -/* No entry for given index? Make sure radix tree is big enough. */ -if (!entry || pmd_downgrade) { -int err; - -if (pmd_downgrade) { -/* -* Make sure 'entry' remains valid while we drop -* the i_pages lock. -*/ - entry =lock_slot (mapping, slot); - } +if (!entry) { +xas_store (xas, locked); +if (xas_error (xas)) + goto error; + entry = unlocked; + mapping->nrexceptional++; + } -xa_unlock_irq (&mapping->i_pages); +if (pmd_downgrade) {/* * Besides huge zero pages the only other thing that gets * downgraded are empty entries which don't need to be * unmapped. */ -if (pmd_downgrade &&dax_is_zero_entry (entry)) -unmap_mapping_pages (mapping, index & ~PG_PMD_COLOUR , +if (dax_is_zero_entry (entry)) { +xas_unlock_irq (xas); +unmap_mapping_pages (mapping, xas->xa_index,PG_PMD_NR , false); - - err =radix_tree_preload ( -mapping_gfp_mask (mapping) & ~__GFP_HIGHMEM); -if (err) { -if (pmd_downgrade) -put_locked_mapping_entry (mapping, index); -return ERR_PTR (err); - } -xa_lock_irq (&mapping->i_pages); - -if (!entry) { -/* -* We needed to drop the i_pages lock while calling -* radix_tree_preload() and we didn't have an entry to -* lock. See if another thread inserted an entry at -* our index during this time. -*/ - entry =__radix_tree_lookup (&mapping->i_pages, index, -NULL , &slot); -if (entry) { -radix_tree_preload_end (); -xa_unlock_irq (&mapping->i_pages); - goto restart; - } +xas_reset (xas); +xas_lock_irq (xas); } -if (pmd_downgrade) { -dax_disassociate_entry (entry, mapping, false); -radix_tree_delete (&mapping->i_pages, index); +dax_disassociate_entry (entry, mapping, false); +xas_store (xas,NULL );/* undo the PMD join */ +xas_store (xas, locked);/* may fail with ENOMEM */ +dax_wake_entry (xas, entry, true); +if (xas_error (xas)) { mapping->nrexceptional--; -dax_wake_mapping_entry_waiter (&mapping->i_pages, - index, entry, true); + goto error; } - - entry =dax_make_locked (0 , size_flag |DAX_EMPTY ); - - err =__radix_tree_insert (&mapping->i_pages, index, -dax_entry_order (entry), entry); -radix_tree_preload_end (); -if (err) { -xa_unlock_irq (&mapping->i_pages); -/* -* Our insertion of a DAX entry failed, most likely -* because we were inserting a PMD entry and it -* collided with a PTE sized entry at a different -* index in the PMD range. We haven't inserted -* anything into the radix tree and have no waiters to -* wake. -*/ -return ERR_PTR (err); - } -/* Good, we have inserted empty locked entry into the tree. */ - mapping->nrexceptional++; -xa_unlock_irq (&mapping->i_pages); -return entry; + entry = unlocked; } - entry =lock_slot (mapping, slot); - out_unlock: -xa_unlock_irq (&mapping->i_pages); + +xas_unlock_irq (xas);return entry; + +fallback: +xas_unlock_irq (xas); +return xa_mk_internal (VM_FAULT_FALLBACK ); +error: +xas_unlock_irq (xas); +if (xas_nomem (xas,GFP_NOIO )) + goto retry; +if (xas->xa_node ==XA_ERROR (-ENOMEM )) +return xa_mk_internal (VM_FAULT_OOM ); +return xa_mk_internal (VM_FAULT_SIGBUS ); }/**
@@ -840,29 +682,25 @@ static int copy_user_dax(struct block_device *bdev, struct dax_device *dax_dev,* already in the tree, we will skip the insertion and just dirty the PMD as * appropriate. */ -static void *dax_insert_entry (struct address_space *mapping, -struct vm_fault *vmf, +static void *dax_insert_entry (struct xa_state *xas, +struct address_space *mapping,struct vm_fault *vmf,void *entry,pfn_t pfn_t,unsigned long flags,bool dirty) { -struct radix_tree_root *pages = &mapping->i_pages; -unsigned long pfn =pfn_t_to_pfn (pfn_t); -pgoff_t index = vmf->pgoff; -void *new_entry; - +void *new_entry =dax_make_entry (pfn_t, flags);if (dirty)__mark_inode_dirty (mapping->host,I_DIRTY_PAGES );if (dax_is_zero_entry (entry) && !(flags &DAX_ZERO_PAGE )) { +unsigned long index = xas->xa_index;/* we are replacing a zero page with block mapping */ if (dax_is_pmd_entry (entry)) -unmap_mapping_pages (mapping, index & ~PG_PMD_COLOUR , -PG_PMD_NR , false); +unmap_mapping_pages (mapping, index,PG_PMD_NR , false);else /* pte entry */ -unmap_mapping_pages (mapping, vmf->pgoff,1 , false); +unmap_mapping_pages (mapping, index,1 , false); } -xa_lock_irq (pages); - new_entry =dax_make_locked (pfn, flags); +xas_reset (xas); +xas_lock_irq (xas);if (dax_entry_size (entry) !=dax_entry_size (new_entry)) {dax_disassociate_entry (entry, mapping, false);dax_associate_entry (new_entry, mapping, vmf->vma, vmf->address);
@@ -877,21 +715,16 @@ static void *dax_insert_entry(struct address_space *mapping,* existing entry is a PMD, we will just leave the PMD in the * tree and dirty it if necessary. */ -struct radix_tree_node *node; -void **slot; -void *ret; - - ret =__radix_tree_lookup (pages, index, &node, &slot); -WARN_ON_ONCE (ret != entry); -__radix_tree_replace (pages, node, slot, - new_entry,NULL ); +dax_lock_entry (xas, new_entry); entry = new_entry; } -if (dirty) -radix_tree_tag_set (pages, index,PAGECACHE_TAG_DIRTY ); +if (dirty) { +xas_load (xas);/* Walk the xa_state */ +xas_set_tag (xas,PAGECACHE_TAG_DIRTY ); + } -xa_unlock_irq (pages); +xas_unlock_irq (xas);return entry; }
@@ -1160,15 +993,16 @@ static int dax_iomap_pfn(struct iomap *iomap, loff_t pos, size_t size,* If this page is ever written to we will re-fault and change the mapping to * point to real DAX storage instead. */ -static vm_fault_t dax_load_hole (struct address_space *mapping,void *entry, -struct vm_fault *vmf) +static vm_fault_t dax_load_hole (struct xa_state *xas, +struct address_space *mapping,void **entry, +struct vm_fault *vmf) {struct inode *inode = mapping->host;unsigned long vaddr = vmf->address;pfn_t pfn =pfn_to_pfn_t (my_zero_pfn (vaddr));vm_fault_t ret; -dax_insert_entry (mapping, vmf, entry, pfn, + *entry =dax_insert_entry (xas, mapping, vmf, *entry, pfn,DAX_ZERO_PAGE , false); ret =vmf_insert_mixed (vmf->vma, vaddr, pfn);
@@ -1381,6 +1215,7 @@ static vm_fault_t dax_iomap_pte_fault(struct vm_fault *vmf, pfn_t *pfnp, {struct vm_area_struct *vma = vmf->vma;struct address_space *mapping = vma->vm_file->f_mapping; +XA_STATE (xas, &mapping->i_pages, vmf->pgoff);struct inode *inode = mapping->host;unsigned long vaddr = vmf->address;loff_t pos = (loff_t )vmf->pgoff <<PAGE_SHIFT ;
@@ -1407,9 +1242,9 @@ static vm_fault_t dax_iomap_pte_fault(struct vm_fault *vmf, pfn_t *pfnp,if (write && !vmf->cow_page) flags |=IOMAP_WRITE ; - entry =grab_mapping_entry (mapping, vmf->pgoff,0 ); -if (IS_ERR (entry)) { - ret =dax_fault_return (PTR_ERR (entry)); + entry =grab_mapping_entry (&xas, mapping); +if (xa_is_internal (entry)) { + ret =xa_to_internal (entry); goto out; }
@@ -1482,7 +1317,7 @@ static vm_fault_t dax_iomap_pte_fault(struct vm_fault *vmf, pfn_t *pfnp,if (error <0 ) goto error_finish_iomap; - entry =dax_insert_entry (mapping, vmf, entry, pfn, + entry =dax_insert_entry (&xas, mapping, vmf, entry, pfn,0 , write && !sync);/*
@@ -1510,7 +1345,7 @@ static vm_fault_t dax_iomap_pte_fault(struct vm_fault *vmf, pfn_t *pfnp,case IOMAP_UNWRITTEN :case IOMAP_HOLE :if (!write) { - ret =dax_load_hole (mapping, entry, vmf); + ret =dax_load_hole (&xas, mapping, &entry, vmf); goto finish_iomap; }/*FALLTHRU*/
@@ -1537,21 +1372,20 @@ static vm_fault_t dax_iomap_pte_fault(struct vm_fault *vmf, pfn_t *pfnp, ops->iomap_end (inode, pos,PAGE_SIZE , copied, flags, &iomap); } unlock_entry: -put_locked_mapping_entry (mapping, vmf->pgoff); +put_locked_entry (&xas, entry); out:trace_dax_pte_fault_done (inode, vmf, ret);return ret | major; }#ifdef CONFIG_FS_DAX_PMD -static vm_fault_t dax_pmd_load_hole (struct vm_fault *vmf,struct iomap *iomap, -void *entry) +static vm_fault_t dax_pmd_load_hole (struct xa_state *xas,struct vm_fault *vmf, +struct iomap *iomap,void **entry) {struct address_space *mapping = vmf->vma->vm_file->f_mapping;unsigned long pmd_addr = vmf->address &PMD_MASK ;struct inode *inode = mapping->host;struct page *zero_page; -void *ret =NULL ;spinlock_t *ptl;pmd_t pmd_entry;pfn_t pfn;
@@ -1562,7 +1396,7 @@ static vm_fault_t dax_pmd_load_hole(struct vm_fault *vmf, struct iomap *iomap, goto fallback; pfn =page_to_pfn_t (zero_page); - ret =dax_insert_entry (mapping, vmf, entry, pfn, + *entry =dax_insert_entry (xas, mapping, vmf, *entry, pfn,DAX_PMD |DAX_ZERO_PAGE , false); ptl =pmd_lock (vmf->vma->vm_mm, vmf->pmd);
@@ -1575,11 +1409,11 @@ static vm_fault_t dax_pmd_load_hole(struct vm_fault *vmf, struct iomap *iomap, pmd_entry =pmd_mkhuge (pmd_entry);set_pmd_at (vmf->vma->vm_mm, pmd_addr, vmf->pmd, pmd_entry);spin_unlock (ptl); -trace_dax_pmd_load_hole (inode, vmf, zero_page, ret); +trace_dax_pmd_load_hole (inode, vmf, zero_page, *entry);return VM_FAULT_NOPAGE ; fallback: -trace_dax_pmd_load_hole_fallback (inode, vmf, zero_page, ret); +trace_dax_pmd_load_hole_fallback (inode, vmf, zero_page, *entry);return VM_FAULT_FALLBACK ; }
@@ -1588,6 +1422,7 @@ static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp, {struct vm_area_struct *vma = vmf->vma;struct address_space *mapping = vma->vm_file->f_mapping; +XA_STATE_ORDER (xas, &mapping->i_pages, vmf->pgoff,PMD_ORDER );unsigned long pmd_addr = vmf->address &PMD_MASK ;bool write = vmf->flags &FAULT_FLAG_WRITE ;bool sync;
@@ -1595,7 +1430,7 @@ static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp,struct inode *inode = mapping->host;vm_fault_t result =VM_FAULT_FALLBACK ;struct iomap iomap = {0 }; -pgoff_t max_pgoff, pgoff; +pgoff_t max_pgoff;void *entry;loff_t pos;int error;
@@ -1606,7 +1441,6 @@ static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp,* supposed to hold locks serializing us with truncate / punch hole so * this is a reliable test. */ - pgoff =linear_page_index (vma, pmd_addr); max_pgoff =DIV_ROUND_UP (i_size_read (inode),PAGE_SIZE );trace_dax_pmd_fault (inode, vmf, max_pgoff,0 );
@@ -1631,24 +1465,21 @@ static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp,if ((pmd_addr +PMD_SIZE ) > vma->vm_end) goto fallback; -if (pgoff >= max_pgoff) { - result =VM_FAULT_SIGBUS ; - goto out; - } -/* If the PMD would extend beyond the file size */ -if ((pgoff |PG_PMD_COLOUR ) >= max_pgoff) +if ((xas.xa_index |PG_PMD_COLOUR ) >= max_pgoff) goto fallback;/* -* grab_mapping_entry() will make sure we get a 2MiB empty entry, a -* 2MiB zero page entry or a DAX PMD. If it can't (because a 4k page -* is already in the tree, for instance), it will return -EEXIST and -* we just fall back to 4k entries. +* grab_mapping_entry() will make sure we get a 2MiB empty entry, +* a 2MiB zero page entry or a DAX PMD. If it can't (because a 4k +* page is already in the tree, for instance), it will return +* VM_FAULT_FALLBACK. */ - entry =grab_mapping_entry (mapping, pgoff,DAX_PMD ); -if (IS_ERR (entry)) + entry =grab_mapping_entry (&xas, mapping); +if (xa_is_internal (entry)) { + result =xa_to_internal (entry); goto fallback; + }/* * It is possible, particularly with mixed reads & writes to private
@@ -1667,7 +1498,7 @@ static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp,* setting up a mapping, so really we're using iomap_begin() as a way * to look up our filesystem block. */ - pos = (loff_t )pgoff <<PAGE_SHIFT ; + pos = (loff_t )xas.xa_index <<PAGE_SHIFT ; error = ops->iomap_begin (inode, pos,PMD_SIZE , iomap_flags, &iomap);if (error) goto unlock_entry;
@@ -1683,7 +1514,7 @@ static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp,if (error <0 ) goto finish_iomap; - entry =dax_insert_entry (mapping, vmf, entry, pfn, + entry =dax_insert_entry (&xas, mapping, vmf, entry, pfn,DAX_PMD , write && !sync);/*
@@ -1708,7 +1539,7 @@ static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp,case IOMAP_HOLE :if (WARN_ON_ONCE (write))break ; - result =dax_pmd_load_hole (vmf, &iomap, entry); + result =dax_pmd_load_hole (&xas, vmf, &iomap, &entry);break ;default :WARN_ON_ONCE (1 );
@@ -1731,13 +1562,12 @@ static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp, &iomap); } unlock_entry: -put_locked_mapping_entry (mapping, pgoff); +put_locked_entry (&xas, entry); fallback:if (result ==VM_FAULT_FALLBACK ) {split_huge_pmd (vma, vmf->pmd, vmf->address);count_vm_event (THP_FAULT_FALLBACK ); } -out:trace_dax_pmd_fault_done (inode, vmf, max_pgoff, result);return result; }
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 70/74 page cache: Finish XArray conversion
In reply to Matthew WilcoxWith no more radix tree API users left, we can drop the GFP flags and use xa_init() instead of INIT_RADIX_TREE().
--- fs/inode.c | 2 +- mm/swap_state.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
fs/inode.c +1 -1
@@ -349,7 +349,7 @@ EXPORT_SYMBOL(inc_nlink);static void __address_space_init_once (struct address_space *mapping) { -INIT_RADIX_TREE (&mapping->i_pages,GFP_ATOMIC | __GFP_ACCOUNT); +xa_init_flags (&mapping->i_pages,XA_FLAGS_LOCK_IRQ );init_rwsem (&mapping->i_mmap_rwsem);INIT_LIST_HEAD (&mapping->private_list);spin_lock_init (&mapping->private_lock);
mm/swap_state.c +1 -1
@@ -596,7 +596,7 @@ int init_swap_address_space(unsigned int type, unsigned long nr_pages)return -ENOMEM ;for (i =0 ; i < nr; i++) { space = spaces + i; -INIT_RADIX_TREE (&space->i_pages,GFP_ATOMIC |__GFP_NOWARN); +xa_init_flags (&space->i_pages,XA_FLAGS_LOCK_IRQ );atomic_set (&space->i_mmap_writable,0 ); space->a_ops = &swap_aops;/* swap cache doesn't use writeback related tags */
Signature · 6 lines
2.17.1 ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot
Matthew Wilcox
maintainerPATCH v14 71/74 radix tree: Remove radix_tree_update_node_t
In reply to Matthew WilcoxThe only user of this functionality was the workingset code, and it's now been converted to the XArray. Remove __radix_tree_delete_node() entirely as it was also only used by the workingset code.
--- include/linux/radix-tree.h | 7 +---- lib/idr.c | 2 +- lib/radix-tree.c | 42 +++++---------------------- tools/testing/radix-tree/multiorder.c | 2 +- 4 files changed, 11 insertions(+), 42 deletions(-)
include/linux/radix-tree.h +1 -6
@@ -242,17 +242,12 @@ void *__radix_tree_lookup(const struct radix_tree_root *, unsigned long index,void *radix_tree_lookup (const struct radix_tree_root *,unsigned long );void __rcu **radix_tree_lookup_slot (const struct radix_tree_root *,unsigned long index); -typedef void (*radix_tree_update_node_t )(struct radix_tree_node *);void __radix_tree_replace (struct radix_tree_root *,struct radix_tree_node *, -void __rcu **slot,void *entry, -radix_tree_update_node_t update_node); +void __rcu **slot,void *entry);void radix_tree_iter_replace (struct radix_tree_root *,const struct radix_tree_iter *,void __rcu **slot,void *entry);void radix_tree_replace_slot (struct radix_tree_root *,void __rcu **slot,void *entry); -void __radix_tree_delete_node (struct radix_tree_root *, -struct radix_tree_node *, -radix_tree_update_node_t update_node);void radix_tree_iter_delete (struct radix_tree_root *,struct radix_tree_iter *iter,void __rcu **slot);void *radix_tree_delete_item (struct radix_tree_root *,unsigned long ,void *);
lib/idr.c +1 -1
@@ -303,7 +303,7 @@ void *idr_replace(struct idr *idr, void *ptr, unsigned long id)if (!slot ||radix_tree_tag_get (&idr->idr_rt, id,IDR_FREE ))return ERR_PTR (-ENOENT ); -__radix_tree_replace (&idr->idr_rt, node, slot, ptr,NULL ); +__radix_tree_replace (&idr->idr_rt, node, slot, ptr);return entry; }
lib/radix-tree.c +8 -34
108 lines not on this page · raw message
tools/testing/radix-tree/multiorder.c +1 -1
@@ -618,7 +618,7 @@ static void multiorder_account(void)__radix_tree_insert (&tree,1 <<5 ,5 ,xa_mk_value (5 ));__radix_tree_lookup (&tree,1 <<5 , &node, &slot);assert (node->count == node->nr_values *2 ); -__radix_tree_replace (&tree, node, slot,NULL ,NULL ); +__radix_tree_replace (&tree, node, slot,NULL );assert (node->nr_values ==0 );item_kill_tree (&tree);
Signature · 6 lines
2.17.1 ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot
Matthew Wilcox
maintainerPATCH v14 72/74 radix tree: Remove split/join code
In reply to Matthew Wilcoxradix_tree_split and radix_tree_join were never used upstream. Remove them; if they're needed in future they will be replaced by XArray equivalents.
--- include/linux/radix-tree.h | 6 - lib/radix-tree.c | 171 +----------------- tools/testing/radix-tree/benchmark.c | 91 ---------- tools/testing/radix-tree/multiorder.c | 247 -------------------------- 4 files changed, 2 insertions(+), 513 deletions(-)
include/linux/radix-tree.h +0 -6
@@ -284,12 +284,6 @@ static inline void radix_tree_preload_end(void)preempt_enable (); } -int radix_tree_split_preload (unsigned old_order,unsigned new_order,gfp_t ); -int radix_tree_split (struct radix_tree_root *,unsigned long index, -unsigned new_order); -int radix_tree_join (struct radix_tree_root *,unsigned long index, -unsigned new_order,void *); -void __rcu **idr_get_free (struct radix_tree_root *root,struct radix_tree_iter *iter,gfp_t gfp,unsigned long max);
lib/radix-tree.c +2 -169
189 lines not on this page · raw message
tools/testing/radix-tree/benchmark.c +0 -91
101 lines not on this page · raw message
tools/testing/radix-tree/multiorder.c +0 -247
259 lines not on this page · raw message
Signature · 6 lines
2.17.1 ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot
Matthew Wilcox
maintainerPATCH v14 73/74 radix tree: Remove radix_tree_maybe_preload_order
In reply to Matthew WilcoxThis function was only used by the page cache which is now converted to the XArray.
--- include/linux/radix-tree.h | 1 - lib/radix-tree.c | 74 -------------------------------------- 2 files changed, 75 deletions(-)
include/linux/radix-tree.h +0 -1
@@ -259,7 +259,6 @@ unsigned int radix_tree_gang_lookup(const struct radix_tree_root *, unsigned int max_items);int radix_tree_preload (gfp_t gfp_mask);int radix_tree_maybe_preload (gfp_t gfp_mask); -int radix_tree_maybe_preload_order (gfp_t gfp_mask,int order);void radix_tree_init (void );void *radix_tree_tag_set (struct radix_tree_root *,unsigned long index,unsigned int tag);
lib/radix-tree.c +0 -74
98 lines not on this page · raw message
Signature · 1 line
2.17.1
Matthew Wilcox
maintainerPATCH v14 74/74 radix tree: Remove radix_tree_clear_tags
In reply to Matthew WilcoxThe page cache was the only user of this interface and it has now been converted to the XArray. Transform the test into a test of xas_init_tags().
--- include/linux/radix-tree.h | 2 -- lib/radix-tree.c | 13 ----------- lib/test_xarray.c | 33 ++++++++++++++++++++++++++++ tools/testing/radix-tree/tag_check.c | 29 ------------------------ 4 files changed, 33 insertions(+), 44 deletions(-)
include/linux/radix-tree.h +0 -2
@@ -252,8 +252,6 @@ void radix_tree_iter_delete(struct radix_tree_root *, struct radix_tree_iter *iter, void __rcu **slot);void *radix_tree_delete_item (struct radix_tree_root *,unsigned long ,void *);void *radix_tree_delete (struct radix_tree_root *,unsigned long ); -void radix_tree_clear_tags (struct radix_tree_root *,struct radix_tree_node *, -void __rcu **slot);unsigned int radix_tree_gang_lookup (const struct radix_tree_root *,void **results,unsigned long first_index,unsigned int max_items);
lib/radix-tree.c +0 -13
@@ -1711,19 +1711,6 @@ void *radix_tree_delete(struct radix_tree_root *root, unsigned long index) }EXPORT_SYMBOL (radix_tree_delete); -void radix_tree_clear_tags (struct radix_tree_root *root, -struct radix_tree_node *node, -void __rcu **slot) -{ -if (node) { -unsigned int tag, offset =get_slot_offset (node, slot); -for (tag =0 ; tag <RADIX_TREE_MAX_TAGS ; tag++) -node_tag_clear (root, node, tag, offset); - }else { -root_tag_clear_all (root); - } -} -/** * radix_tree_tagged - test whether any items in the tree are tagged * @root: radix tree root
lib/test_xarray.c +33 -0
@@ -170,12 +170,45 @@ static void check_xa_tag_1(struct xarray *xa, unsigned long index)XA_BUG_ON (xa, !xa_empty (xa)); } +static void check_xa_tag_2 (struct xarray *xa) +{ +XA_STATE (xas, xa,0 ); +unsigned long index; +unsigned int count =0 ; +void *entry; + +xa_store_value (xa,0 ,GFP_KERNEL ); +xa_set_tag (xa,0 ,XA_TAG_0 ); +xas_load (&xas); +xas_init_tags (&xas); +XA_BUG_ON (xa, !xa_get_tag (xa,0 ,XA_TAG_0 ) ==0 ); + +for (index =3500 ; index <4500 ; index++) { +xa_store_value (xa, index,GFP_KERNEL ); +xa_set_tag (xa, index,XA_TAG_0 ); + } + +xas_reset (&xas); +xas_for_each_tagged (&xas, entry,ULONG_MAX ,XA_TAG_0 ) + count++; +XA_BUG_ON (xa, count !=1000 ); + +xas_for_each (&xas, entry,ULONG_MAX ) { +xas_init_tags (&xas); +XA_BUG_ON (xa, !xa_get_tag (xa, xas.xa_index,XA_TAG_0 )); +XA_BUG_ON (xa, !xas_get_tag (&xas,XA_TAG_0 )); + } + +xa_destroy (xa); +} +static void check_xa_tag (struct xarray *xa) {check_xa_tag_1 (xa,0 );check_xa_tag_1 (xa,4 );check_xa_tag_1 (xa,64 );check_xa_tag_1 (xa,4096 ); +check_xa_tag_2 (xa); }static void check_xa_shrink (struct xarray *xa)
tools/testing/radix-tree/tag_check.c +0 -29
@@ -331,34 +331,6 @@ static void single_check(void)item_kill_tree (&tree); } -void radix_tree_clear_tags_test (void ) -{ -unsigned long index; -struct radix_tree_node *node; -struct radix_tree_iter iter; -void **slot; - -RADIX_TREE (tree,GFP_KERNEL ); - -item_insert (&tree,0 ); -item_tag_set (&tree,0 ,0 ); -__radix_tree_lookup (&tree,0 , &node, &slot); -radix_tree_clear_tags (&tree, node, slot); -assert (item_tag_get (&tree,0 ,0 ) ==0 ); - -for (index =0 ; index <1000 ; index++) { -item_insert (&tree, index); -item_tag_set (&tree, index,0 ); - } - -radix_tree_for_each_slot (slot, &tree, &iter,0 ) { -radix_tree_clear_tags (&tree, iter.node, slot); -assert (item_tag_get (&tree, iter.index,0 ) ==0 ); - } - -item_kill_tree (&tree); -} -void tag_check (void ) {single_check ();
@@ -376,5 +348,4 @@ void tag_check(void)thrash_tags ();rcu_barrier ();printv (2 ,"after thrash_tags: %d allocated\n" , nr_allocated); -radix_tree_clear_tags_test (); }
Signature · 1 line
2.17.1
Quoted 28 lines · Matthew Wilcox
The XArray is a replacement for the radix tree. For the moment it uses the same data structures, enabling a gradual replacement. This patch set implements the XArray and converts the page cache to use it.
A version of these patches has been running under xfstests for over 48 hours, so I have some confidence in them. The DAX changes have now also had a reasonable test outing. This is based on next-20180615 and is available as a git tree at
git://git.infradead.org/users/willy/linux-dax.gitxarray-20180615I shall create a git branch from -rc1 and ask for that to be included in -next. I'm a little concerned I still have no reviews on some of the later patches.
Changes since v13:
- Actually fixed bug in workingset conversion that led to exceptional entries not being deleted from the XArray. Not sure how I dropped that patch for v13. Thanks to David Sterba for noticing.
- Fixed bug in DAX writeback conversion that failed to wake up waiters. Thanks to Ross for testing, and to Dan & Jeff for helping me get a setup working to reproduce the problem.
- Converted the newdax_lock_page/dax_unlock_pagefunctions.
- Moved XArray test suite entirely into thetest_xarraykernel module to match other test suites. It can still be built in userspace as part of the radix tree test suite.
- Changed email address.
- Moved a few functions into different patches to make the test-suite additions more logical.
Quoted 8 lines
- Fixed a bug in
XA_BUG_ON(oh the irony) where it evaluated the condition twice.
- Constifiedxa_head()/xa_parent()/xa_entry()and their_lockedvariants.
- Movedxa_parent()toxarray.hso it can be used from the workingset code.
- Call the xarray testsuite from the radix tree test suite to ensure that I remember to run both test suites ;-)
- Added some more tests to the test suite.
Hit another deadlock. This one reproduces 100% of the time in my setup with XFS + DAX + generic/340. It doesn't reproduce for me at all with next-20180615. Here's the output from "echo w > /proc/sysrq-trigger":
[ 92.849119] sysrq: SysRq : Show Blocked State [ 92.850506] task PC stack pid father [ 92.852299] holetest D 0 1651 1466 0x00000000 [ 92.853912] Call Trace: [ 92.854610] __schedule+0x2c5/0xad0 [ 92.855612] schedule+0x36/0x90 [ 92.856602] get_unlocked_entry+0xce/0x120 [ 92.857756] ? dax_insert_entry+0x2b0/0x2b0 [ 92.858931] grab_mapping_entry+0x19e/0x250 [ 92.860119] dax_iomap_pte_fault+0x115/0x1140 [ 92.860836] dax_iomap_fault+0x37/0x40 [ 92.861235] __xfs_filemap_fault+0x2de/0x310 [ 92.861681] xfs_filemap_fault+0x2c/0x30 [ 92.862113] __do_fault+0x26/0x160 [ 92.862531] __handle_mm_fault+0xc96/0x1320 [ 92.863059] handle_mm_fault+0x1ba/0x3c0 [ 92.863534] __do_page_fault+0x2b4/0x590 [ 92.864029] do_page_fault+0x38/0x2c0 [ 92.864472] do_async_page_fault+0x2c/0xb0 [ 92.864985] ? async_page_fault+0x8/0x30 [ 92.865459] async_page_fault+0x1e/0x30 [ 92.865941] RIP: 0033:0x401442 [ 92.866322] Code: Bad RIP value. [ 92.866739] RSP: 002b:00007fa29c9feec0 EFLAGS: 00010212 [ 92.867366] RAX: 00007fa29ca00400 RBX: 0000000000001000 RCX: 0000000000008000 [ 92.868219] RDX: 0000000000000009 RSI: 0000000000000000 RDI: 0000000000000000 [ 92.869082] RBP: 00007fa29c9ff700 R08: 00007fa29c9ff700 R09: 00007fa29c9ff700 [ 92.869939] R10: 0000000000000070 R11: 00007fa29e571ba0 R12: 00007fa29ca00000 [ 92.870804] R13: 00007fffd5f24160 R14: 0000000000000400 R15: 00007fffd5f240b0
This looks very similar to the one I reported last week with generic/269.
- Ross
Matthew Wilcox
maintainerQuoted 3 lines · Ross Zwisler
Hit another deadlock. This one reproduces 100% of the time in my setup with XFS + DAX + generic/340. It doesn't reproduce for me at all with next-20180615. Here's the output from "echo w >
/proc/sysrq-trigger":
*sigh*. I wonder what the differences are between our setups ...
Quoted 11 lines
[ 92.849119] sysrq: SysRq : Show Blocked State [ 92.850506] task PC stack pid father [ 92.852299] holetest D 0 1651 1466 0x00000000 [ 92.853912] Call Trace: [ 92.854610] __schedule+0x2c5/0xad0 [ 92.855612] schedule+0x36/0x90 [ 92.856602] get_unlocked_entry+0xce/0x120 [ 92.857756] ? dax_insert_entry+0x2b0/0x2b0 [ 92.858931] grab_mapping_entry+0x19e/0x250 [ 92.860119] dax_iomap_pte_fault+0x115/0x1140 [ 92.860836] dax_iomap_fault+0x37/0x40
...
Quoted 1 line
This looks very similar to the one I reported last week with generic/269.
Yeah, another missing wakeup, no doubt. Can you bisect this? That was how I found the last one; bisected it to a single patch and stared very hard at the patch until I saw it. I'm not going to be in a position to tinker with my DAX setup until the first week of July.
Quoted 18 lines · Matthew Wilcox
Quoted 3 lines · Ross Zwisler
Hit another deadlock. This one reproduces 100% of the time in my setup with XFS + DAX + generic/340. It doesn't reproduce for me at all with next-20180615. Here's the output from "echo w >
/proc/sysrq-trigger":*sigh*. I wonder what the differences are between our setups ...
Quoted 11 lines
[ 92.849119] sysrq: SysRq : Show Blocked State [ 92.850506] task PC stack pid father [ 92.852299] holetest D 0 1651 1466 0x00000000 [ 92.853912] Call Trace: [ 92.854610] __schedule+0x2c5/0xad0 [ 92.855612] schedule+0x36/0x90 [ 92.856602] get_unlocked_entry+0xce/0x120 [ 92.857756] ? dax_insert_entry+0x2b0/0x2b0 [ 92.858931] grab_mapping_entry+0x19e/0x250 [ 92.860119] dax_iomap_pte_fault+0x115/0x1140 [ 92.860836] dax_iomap_fault+0x37/0x40
Quoted 7 lines
...
Quoted 1 line
This looks very similar to the one I reported last week with generic/269.
Yeah, another missing wakeup, no doubt. Can you bisect this? That was how I found the last one; bisected it to a single patch and stared very hard at the patch until I saw it. I'm not going to be in a position to tinker with my DAX setup until the first week of July.
It bisected to this commit:
b4b4daa7e8fb0ad0fee35d3e28d00e97c849a6cb is the first bad commit commit b4b4daa7e8fb0ad0fee35d3e28d00e97c849a6cb
Author: Matthew Wilcox <[email protected]> Date: Thu Mar 29 22:58:27 2018 -0400
dax: Convert page fault handlers to XArray
This is the last part of DAX to be converted to the XArray so remove all the old helper functions.
Matthew Wilcox
maintainerQuoted 34 lines · Ross Zwisler
Quoted 18 lines · Matthew Wilcox
Quoted 3 lines · Ross Zwisler
Hit another deadlock. This one reproduces 100% of the time in my setup with XFS + DAX + generic/340. It doesn't reproduce for me at all with next-20180615. Here's the output from "echo w >
/proc/sysrq-trigger":*sigh*. I wonder what the differences are between our setups ...
Quoted 11 lines
[ 92.849119] sysrq: SysRq : Show Blocked State [ 92.850506] task PC stack pid father [ 92.852299] holetest D 0 1651 1466 0x00000000 [ 92.853912] Call Trace: [ 92.854610] __schedule+0x2c5/0xad0 [ 92.855612] schedule+0x36/0x90 [ 92.856602] get_unlocked_entry+0xce/0x120 [ 92.857756] ? dax_insert_entry+0x2b0/0x2b0 [ 92.858931] grab_mapping_entry+0x19e/0x250 [ 92.860119] dax_iomap_pte_fault+0x115/0x1140 [ 92.860836] dax_iomap_fault+0x37/0x40Quoted 7 lines
...
Quoted 1 line
This looks very similar to the one I reported last week with generic/269.
Yeah, another missing wakeup, no doubt. Can you bisect this? That was how I found the last one; bisected it to a single patch and stared very hard at the patch until I saw it. I'm not going to be in a position to tinker with my DAX setup until the first week of July.
It bisected to this commit:
b4b4daa7e8fb0ad0fee35d3e28d00e97c849a6cbis the first bad commit commitb4b4daa7e8fb0ad0fee35d3e28d00e97c849a6cbAuthor: Matthew Wilcox <[email protected]> Date: Thu Mar 29 22:58:27 2018 -0400
Quoted 6 lines
dax: Convert page fault handlers to XArray
This is the last part of DAX to be converted to the XArray so remove all the old helper functions.
Signed-off-by Matthew Wilcox <[email protected]>
I think I see a bug. No idea if it's the one you're hitting ;-)
I had been intending to not use the 'entry' to decide whether we were waiting on a 2MB or 4kB page, but rather the xas. I shelved that idea, but not before dropping the DAX_PMD flag being passed from the PMD pagefault caller. So if I put that back ...
fs/dax.c +6 -6
@@ -367,13 +367,13 @@ static struct page *dax_busy_page(void *entry)* a VM_FAULT code, encoded as an xarray internal entry. The ERR_PTR values * overlap with xarray value entries. */ -static -void *grab_mapping_entry (struct xa_state *xas,struct address_space *mapping) +static void *grab_mapping_entry (struct xa_state *xas, +struct address_space *mapping,unsigned long size) {bool pmd_downgrade = false;/* splitting 2MiB entry into 4k entries? */ void *locked =dax_make_entry (pfn_to_pfn_t (0 ), -DAX_EMPTY |DAX_LOCKED ); -void *unlocked =dax_make_entry (pfn_to_pfn_t (0 ),DAX_EMPTY ); + size |DAX_EMPTY |DAX_LOCKED ); +void *unlocked =dax_make_entry (pfn_to_pfn_t (0 ), size |DAX_EMPTY );void *entry; retry:
@@ -1163,7 +1163,7 @@ static vm_fault_t dax_iomap_pte_fault(struct vm_fault *vmf, pfn_t *pfnp,if (write && !vmf->cow_page) flags |=IOMAP_WRITE ; - entry =grab_mapping_entry (&xas, mapping); + entry =grab_mapping_entry (&xas, mapping,0 );if (xa_is_internal (entry)) { ret =xa_to_internal (entry); goto out;
@@ -1396,7 +1396,7 @@ static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp,* page is already in the tree, for instance), it will return * VM_FAULT_FALLBACK. */ - entry =grab_mapping_entry (&xas, mapping); + entry =grab_mapping_entry (&xas, mapping,DAX_PMD );if (xa_is_internal (entry)) { result =xa_to_internal (entry); goto fallback;
Matthew Wilcox
maintainerQuoted 6 lines · Matthew Wilcox
I think I see a bug. No idea if it's the one you're hitting ;-)
I had been intending to not use the 'entry' to decide whether we were waiting on a 2MB or 4kB page, but rather the xas. I shelved that idea, but not before dropping the
DAX_PMDflag being passed from the PMD pagefault caller. So if I put that back ...
Did you get a chance to test this?
Quoted 41 lines
fs/dax.c +6
-6@@ -367,13 +367,13 @@ static struct page *dax_busy_page(void *entry)* a VM_FAULT code, encoded as an xarray internal entry. The ERR_PTR values * overlap with xarray value entries. */ -static -void *grab_mapping_entry (struct xa_state *xas,struct address_space *mapping) +static void *grab_mapping_entry (struct xa_state *xas, +struct address_space *mapping,unsigned long size) {bool pmd_downgrade = false;/* splitting 2MiB entry into 4k entries? */ void *locked =dax_make_entry (pfn_to_pfn_t (0 ), -DAX_EMPTY |DAX_LOCKED ); -void *unlocked =dax_make_entry (pfn_to_pfn_t (0 ),DAX_EMPTY ); + size |DAX_EMPTY |DAX_LOCKED ); +void *unlocked =dax_make_entry (pfn_to_pfn_t (0 ), size |DAX_EMPTY );void *entry; retry:@@ -1163,7 +1163,7 @@ static vm_fault_t dax_iomap_pte_fault(struct vm_fault *vmf, pfn_t *pfnp,if (write && !vmf->cow_page) flags |=IOMAP_WRITE ; - entry =grab_mapping_entry (&xas, mapping); + entry =grab_mapping_entry (&xas, mapping,0 );if (xa_is_internal (entry)) { ret =xa_to_internal (entry); goto out;@@ -1396,7 +1396,7 @@ static vm_fault_t dax_iomap_pmd_fault(struct vm_fault *vmf, pfn_t *pfnp,* page is already in the tree, for instance), it will return * VM_FAULT_FALLBACK. */ - entry =grab_mapping_entry (&xas, mapping); + entry =grab_mapping_entry (&xas, mapping,DAX_PMD );if (xa_is_internal (entry)) { result =xa_to_internal (entry); goto fallback;
Quoted 9 lines · Matthew Wilcox
Quoted 6 lines · Matthew Wilcox
I think I see a bug. No idea if it's the one you're hitting ;-)
I had been intending to not use the 'entry' to decide whether we were waiting on a 2MB or 4kB page, but rather the xas. I shelved that idea, but not before dropping the
DAX_PMDflag being passed from the PMD pagefault caller. So if I put that back ...Did you get a chance to test this?
With this patch it doesn't deadlock, but the test dies with a SIGBUS and we hit a WARN_ON in the DAX code:
WARNING: CPU: 5 PID: 1678 at fs/dax.c:226 get_unlocked_entry+0xf7/0x120
I don't have a lot of time this week to debug further. The quickest path to victory is probably for you to get this reproducing in your test setup. Does XFS + DAX + generic/340 pass for you?
Matthew Wilcox
maintainerQuoted 11 lines · Ross Zwisler
Quoted 9 lines · Matthew Wilcox
Quoted 6 lines · Matthew Wilcox
I think I see a bug. No idea if it's the one you're hitting ;-)
I had been intending to not use the 'entry' to decide whether we were waiting on a 2MB or 4kB page, but rather the xas. I shelved that idea, but not before dropping the
DAX_PMDflag being passed from the PMD pagefault caller. So if I put that back ...Did you get a chance to test this?
Quoted 8 lines
With this patch it doesn't deadlock, but the test dies with a SIGBUS and we hit a
WARN_ONin the DAX code:WARNING: CPU: 5 PID: 1678 at fs/dax.c:226 get_unlocked_entry+0xf7/0x120I don't have a lot of time this week to debug further. The quickest path to victory is probably for you to get this reproducing in your test setup. Does XFS + DAX + generic/340 pass for you?
I won't be back in front of my test box until Tuesday, but that test does work for me because I couldn't get your instructions to give me a 2MB aligned DAX setup. I had to settle for 4k, so none of the 2MB stuff has been tested properly.
Quoted 17 lines · Matthew Wilcox
Quoted 16 lines · Ross Zwisler
Quoted 9 lines · Matthew Wilcox
Quoted 6 lines · Matthew Wilcox
I think I see a bug. No idea if it's the one you're hitting ;-)
I had been intending to not use the 'entry' to decide whether we were waiting on a 2MB or 4kB page, but rather the xas. I shelved that idea, but not before dropping the
DAX_PMDflag being passed from the PMD pagefault caller. So if I put that back ...Did you get a chance to test this?
With this patch it doesn't deadlock, but the test dies with a SIGBUS and we hit a
WARN_ONin the DAX code:WARNING: CPU: 5 PID: 1678 at fs/dax.c:226 get_unlocked_entry+0xf7/0x120
Quoted 8 lines
Quoted 3 lines
I don't have a lot of time this week to debug further. The quickest path to victory is probably for you to get this reproducing in your test setup. Does XFS + DAX + generic/340 pass for you?
I won't be back in front of my test box until Tuesday, but that test does work for me because I couldn't get your instructions to give me a 2MB aligned DAX setup. I had to settle for 4k, so none of the 2MB stuff has been tested properly.
Ah. I've documented both my qemu setup and my filesystem setup here:
https://nvdimm.wiki.kernel.org/pmem_in_qemu
https://nvdimm.wiki.kernel.org/2mib_fs_dax
This should be enough to get you up and running in QEMU and able to reliably get filesystem DAX PMD faults.
Matthew Wilcox
maintainerQuoted 11 lines · Ross Zwisler
Quoted 9 lines · Matthew Wilcox
Quoted 6 lines · Matthew Wilcox
I think I see a bug. No idea if it's the one you're hitting ;-)
I had been intending to not use the 'entry' to decide whether we were waiting on a 2MB or 4kB page, but rather the xas. I shelved that idea, but not before dropping the
DAX_PMDflag being passed from the PMD pagefault caller. So if I put that back ...Did you get a chance to test this?
Quoted 8 lines
With this patch it doesn't deadlock, but the test dies with a SIGBUS and we hit a
WARN_ONin the DAX code:WARNING: CPU: 5 PID: 1678 at fs/dax.c:226 get_unlocked_entry+0xf7/0x120I don't have a lot of time this week to debug further. The quickest path to victory is probably for you to get this reproducing in your test setup. Does XFS + DAX + generic/340 pass for you?
I now have generic/340 passing. I've pushed a new version togit://git.infradead.org/users/willy/linux-dax.git xarray
Quoted 15 lines · Matthew Wilcox
Quoted 14 lines · Ross Zwisler
Quoted 9 lines · Matthew Wilcox
Quoted 6 lines · Matthew Wilcox
I think I see a bug. No idea if it's the one you're hitting ;-)
I had been intending to not use the 'entry' to decide whether we were waiting on a 2MB or 4kB page, but rather the xas. I shelved that idea, but not before dropping the
DAX_PMDflag being passed from the PMD pagefault caller. So if I put that back ...Did you get a chance to test this?
With this patch it doesn't deadlock, but the test dies with a SIGBUS and we hit a
WARN_ONin the DAX code:
Quoted 8 lines
Quoted 5 lines
WARNING: CPU: 5 PID: 1678 at fs/dax.c:226 get_unlocked_entry+0xf7/0x120I don't have a lot of time this week to debug further. The quickest path to victory is probably for you to get this reproducing in your test setup. Does XFS + DAX + generic/340 pass for you?
I now have generic/340 passing. I've pushed a new version to
git://git.infradead.org/users/willy/linux-dax.gitxarray
Thanks, I'll throw it in my test setup.
Quoted 15 lines · Matthew Wilcox
Quoted 14 lines · Ross Zwisler
Quoted 9 lines · Matthew Wilcox
Quoted 6 lines · Matthew Wilcox
I think I see a bug. No idea if it's the one you're hitting ;-)
I had been intending to not use the 'entry' to decide whether we were waiting on a 2MB or 4kB page, but rather the xas. I shelved that idea, but not before dropping the
DAX_PMDflag being passed from the PMD pagefault caller. So if I put that back ...Did you get a chance to test this?
With this patch it doesn't deadlock, but the test dies with a SIGBUS and we hit a
WARN_ONin the DAX code:
Quoted 8 lines
Quoted 5 lines
WARNING: CPU: 5 PID: 1678 at fs/dax.c:226 get_unlocked_entry+0xf7/0x120I don't have a lot of time this week to debug further. The quickest path to victory is probably for you to get this reproducing in your test setup. Does XFS + DAX + generic/340 pass for you?
I now have generic/340 passing. I've pushed a new version to
git://git.infradead.org/users/willy/linux-dax.gitxarray
Okay, the next failure I'm hitting is with DAX + XFS + generic/344. It doesn't happen every time, but I can usually recreate it within 10 iterations of the test. Here's the failure:
generic/344 21s ...[ 1852.564559] run fstests generic/344 at 2018-07-27 11:19:05
[ 1853.033177] XFS (pmem0p2): Unmounting Filesystem [ 1853.134497] XFS (pmem0p2): DAX enabled. Warning: EXPERIMENTAL, use at your own risk [ 1853.135335] XFS (pmem0p2): Mounting V5 Filesystem [ 1853.138119] XFS (pmem0p2): Ending clean mount [ 1862.251185] WARNING: CPU: 10 PID: 15695 at mm/memory.c:1801 insert_pfn+0x229/0x240 [ 1862.252023] Modules linked in: dax_pmem device_dax nd_pmem nd_btt nfit libnvdimm [ 1862.252853] CPU: 10 PID: 15695 Comm: holetest Tainted: G W 4.18.0-rc6-00077-gc79b37ebab6d-dirty #1 [ 1862.253979] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.11.1-0-g0551a4be2c-prebuilt.qemu-project.org 04/01/2014 [ 1862.255232] RIP: 0010:insert_pfn+0x229/0x240 [ 1862.255734] Code: 21 fa 4c 89 4d b0 48 89 45 c0 c6 05 3c 47 74 01 01 e8 db c2 e2 ff 0f 0b 44 8b 45 ac 4c 8b 4d b0 4c 8b 55 b8 48 8b 45 c0 eb 92 <0f> 0b e9 45 fe ff ff 41 bf f4 ff ff ff e9 43 fe ff ff e8 50 c5 e2 [ 1862.257526] RSP: 0000:ffffc9000e197af8 EFLAGS: 00010216 [ 1862.257994] RAX: 0000000000002df7 RBX: ffff8800368ffe00 RCX: 0000000000000002 [ 1862.258673] RDX: 000fffffffffffff RSI: 000000000000000a RDI: 8000000002df7225 [ 1862.259319] RBP: ffffc9000e197b50 R08: 0000000000000001 R09: ffff8800b8fecd48 [ 1862.260097] R10: ffffc9000e197a30 R11: ffff88010d649a80 R12: ffff8800bb616e80 [ 1862.260843] R13: 00007fc2137c0000 R14: 00000000004521d4 R15: 00000000fffffff0 [ 1862.261563] FS: 00007fc2157ff700(0000) GS:ffff880115800000(0000) knlGS:0000000000000000 [ 1862.262420] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 1862.263003] CR2: 00007fc2137c0c00 CR3: 00000000b866e000 CR4: 00000000000006e0 [ 1862.263631] Call Trace: [ 1862.263862] ? trace_hardirqs_on_caller+0xf4/0x190 [ 1862.264300] __vm_insert_mixed+0x83/0xd0 [ 1862.264657] vmf_insert_mixed_mkwrite+0x13/0x40 [ 1862.265066] dax_iomap_pte_fault+0x760/0x1140 [ 1862.265478] dax_iomap_fault+0x37/0x40 [ 1862.265816] __xfs_filemap_fault+0x2de/0x310 [ 1862.266207] xfs_filemap_page_mkwrite+0x15/0x20 [ 1862.266610] xfs_filemap_pfn_mkwrite+0xe/0x10 [ 1862.267044] do_wp_page+0x1bb/0x660 [ 1862.267435] __handle_mm_fault+0xc78/0x1320 [ 1862.267912] handle_mm_fault+0x1ba/0x3c0 [ 1862.268359] __do_page_fault+0x2b4/0x590 [ 1862.268799] do_page_fault+0x38/0x2c0 [ 1862.269218] do_async_page_fault+0x2c/0xb0 [ 1862.269674] ? async_page_fault+0x8/0x30 [ 1862.270081] async_page_fault+0x1e/0x30 [ 1862.270431] RIP: 0033:0x401442 [ 1862.270709] Code: 1d 20 00 85 f6 0f 85 7d 00 00 00 48 85 db 7e 20 4b 8d 04 34 31 d2 66 90 48 8b 0d 21 1d 20 00 48 0f af ca 48 83 c2 01 48 39 d3 <48> 89 2c 08 75 e8 8b 0d de 1c 20 00 31 c0 85 c9 74 0a 8b 15 d6 1c [ 1862.272811] RSP: 002b:00007fc2157feec0 EFLAGS: 00010216 [ 1862.273392] RAX: 00007fc213600c00 RBX: 0000000000001000 RCX: 00000000001c0000 [ 1862.274182] RDX: 00000000000001c1 RSI: 0000000000000000 RDI: 0000000000000001 [ 1862.274961] RBP: 00007fc2157ff700 R08: 00007fc2157ff700 R09: 00007fc2157ff700 [ 1862.275740] R10: 00007fc2157ff9d0 R11: 0000000000000202 R12: 00007fc213600000 [ 1862.276519] R13: 00007ffe07faa240 R14: 0000000000000c00 R15: 00007ffe07faa170 [ 1862.277296] irq event stamp: 13256 [ 1862.277666] hardirqs last enabled at (13255): [<ffffffff81c8c24c>] _raw_spin_unlock_irq+0x2c/0x60 [ 1862.278630] hardirqs last disabled at (13256): [<ffffffff81e011d3>] error_entry+0x93/0x110 [ 1862.279510] softirqs last enabled at (13250): [<ffffffff820003bf>] __do_softirq+0x3bf/0x520 [ 1862.280410] softirqs last disabled at (13229): [<ffffffff810ac388>] irq_exit+0xe8/0xf0 [ 1862.281251] ---[ end trace 4b8bc73df4e9e7ba ]--- 22s [ 1874.258598] XFS (pmem0p2): Unmounting Filesystem [ 1874.347182] XFS (pmem0p2): DAX enabled. Warning: EXPERIMENTAL, use at your own risk [ 1874.348721] XFS (pmem0p2): Mounting V5 Filesystem [ 1874.353946] XFS (pmem0p2): Ending clean mount
_check_dmesg: something found in dmesg (see /root/xfstests/results//generic/344.dmesg)
Ran: generic/344
Failures: generic/344
Failed 1 of 1 tests
- Ross
Matthew Wilcox
maintainerQuoted 3 lines · Ross Zwisler
Okay, the next failure I'm hitting is with DAX + XFS + generic/344. It doesn't happen every time, but I can usually recreate it within 10 iterations of the test. Here's the failure:
Thanks. I've made some progress with this; the WARNing is coming from a vm_insert_* mkwrite call. Inserting sufficient debugging code has let me determine we still have a zero_pfn in the page table when we're trying to insert a new PFN.