Fix read-only THP for non-tmpfs filesystems
Subsystems (5)
Matthew Wilcox (Oracle)
maintainercover Fix read-only THP for non-tmpfs filesystems
As described more verbosely in the [3/3] changelog, we can inadvertently put an order-0 page in the page cache which occupies 512 consecutive entries. Users are running into this if they enable the READ_ONLY_THP_FOR_FS config option; see https://bugzilla.kernel.org/show_bug.cgi?id=206569
and Qian Cai has also reported it here:
https://lore.kernel.org/lkml/[email protected]/
This is a rather intrusive way of fixing the problem, but has the advantage that I've actually been testing it with the THP patches, which means that it sees far more use than it does upstream -- indeed, Song has been entirely unable to reproduce it. It also has the advantage that it removes a few patches from my gargantuan backlog of THP patches.
Matthew Wilcox (Oracle) (3):
XArray: Add xa_get_order
XArray: Add xas_split
mm/filemap: Fix storing to a THP shadow entry
Documentation/core-api/xarray.rst | 16 +-- include/linux/xarray.h | 22 ++++ lib/test_xarray.c | 62 ++++++++++ lib/xarray.c | 196 ++++++++++++++++++++++++++++-- mm/filemap.c | 42 +++++-- 5 files changed, 311 insertions(+), 27 deletions(-)
Signature · 1 line
2.28.0
Matthew Wilcox (Oracle)
maintainerPATCH 1/3 XArray: Add xa_get_order
This function returns the order of the entry at the index. We need this because there isn't space in the shadow entry to encode its order.
--- include/linux/xarray.h | 9 +++++++++ lib/test_xarray.c | 21 +++++++++++++++++++++ lib/xarray.c | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+)
include/linux/xarray.h +9 -0
@@ -1505,6 +1505,15 @@ void xas_pause(struct xa_state *);void xas_create_range (struct xa_state *); +#ifdef CONFIG_XARRAY_MULTI +int xa_get_order (struct xarray *,unsigned long index); +#else +static inline int xa_get_order (struct xarray *xa,unsigned long index) +{ +return 0 ; +} +#endif +/** * xas_reload() - Refetch an entry from the xarray. * @xas: XArray operation state.
lib/test_xarray.c +21 -0
@@ -1649,6 +1649,26 @@ static noinline void check_account(struct xarray *xa)#endif } +static noinline void check_get_order(struct xarray *xa) +{ +unsigned int max_order =IS_ENABLED (CONFIG_XARRAY_MULTI ) ?20 :1 ; +unsigned int order; +unsigned long i, j; + +for (i =0 ; i <3 ; i++) +XA_BUG_ON (xa,xa_get_order (xa, i) !=0 ); + +for (order =0 ; order < max_order; order++) { +for (i =0 ; i <10 ; i++) { +xa_store_order (xa, i << order, order, +xa_mk_index (i << order),GFP_KERNEL ); +for (j = i << order; j < (i +1 ) << order; j++) +XA_BUG_ON (xa,xa_get_order (xa, j) != order); +xa_erase (xa, i << order); + } + } +} + static noinline void check_destroy(struct xarray *xa) {unsigned long index;
@@ -1697,6 +1717,7 @@ static int xarray_checks(void)check_reserve (&array);check_reserve (&xa0);check_multi_store (&array); +check_get_order (&array);check_xa_alloc ();check_find (&array);check_find_entry (&array);
lib/xarray.c +39 -0
@@ -1592,6 +1592,45 @@ void *xa_store_range(struct xarray *xa, unsigned long first,return xas_result (&xas,NULL ); }EXPORT_SYMBOL (xa_store_range); + +/** +* xa_get_order() - Get the order of an entry. +* @xa: XArray. +* @index: Index of the entry. +* +* Return: A number between 0 and 63 indicating the order of the entry. +*/ +int xa_get_order (struct xarray *xa,unsigned long index) +{ +XA_STATE (xas, xa, index); +void *entry; +int order =0 ; + +rcu_read_lock (); + entry =xas_load (&xas); + +if (!entry) + goto unlock; + +if (!xas.xa_node) + goto unlock; + +for (;;) { +unsigned int slot = xas.xa_offset + (1 << order); + +if (slot >=XA_CHUNK_SIZE ) +break ; +if (!xa_is_sibling (xas.xa_node->slots[slot])) +break ; + order++; + } + + order += xas.xa_node->shift; +unlock: +rcu_read_unlock (); + +return order; +}#endif /* CONFIG_XARRAY_MULTI */ /**
Signature · 1 line
2.28.0
Matthew Wilcox (Oracle)
maintainerPATCH 2/3 XArray: Add xas_split
In reply to Matthew Wilcox (Oracle)In order to use multi-index entries for huge pages in the page cache, we need to be able to split a multi-index entry (eg if a file is truncated in the middle of a huge page entry). This version does not support splitting more than one level of the tree at a time. This is an acceptable limitation for the page cache as we do not expect to support order-12 pages in the near future.
--- Documentation/core-api/xarray.rst | 16 +-- include/linux/xarray.h | 13 +++ lib/test_xarray.c | 41 ++++++++ lib/xarray.c | 157 ++++++++++++++++++++++++++++-- 4 files changed, 211 insertions(+), 16 deletions(-)
Documentation/core-api/xarray.rst +9 -7
@@ -475,13 +475,15 @@ or iterations will move the index to the first index in the range. Each entry will only be returned once, no matter how many indices it occupies. -Using xas_next() or xas_prev() with a multi-index xa_state -is not supported. Using either of these functions on a multi-index entry -will reveal sibling entries; these should be skipped over by the caller. - -Storing ``NULL`` into any index of a multi-index entry will set the entry -at every index to ``NULL`` and dissolve the tie. Splitting a multi-index -entry into entries occupying smaller ranges is not yet supported. +Using xas_next() or xas_prev() with a multi-index xa_state is not +supported. Using either of these functions on a multi-index entry will +reveal sibling entries; these should be skipped over by the caller. + +Storing ``NULL`` into any index of a multi-index entry will set the +entry at every index to ``NULL`` and dissolve the tie. A multi-index +entry can be split into entries occupying smaller ranges by calling +xas_split_alloc() without the xa_lock held, followed by taking the lock +and calling xas_split(). Functions and structures ========================
include/linux/xarray.h +13 -0
@@ -1507,11 +1507,24 @@ void xas_create_range(struct xa_state *);#ifdef CONFIG_XARRAY_MULTI int xa_get_order (struct xarray *,unsigned long index); +void xas_split (struct xa_state *,void *entry,unsigned int order); +void xas_split_alloc (struct xa_state *,void *entry,unsigned int order,gfp_t );#else static inline int xa_get_order (struct xarray *xa,unsigned long index) {return 0 ; } + +static inline void xas_split (struct xa_state *xas,void *entry, +unsigned int order) +{ +xas_store (xas, entry); +} + +static inline void xas_split_alloc (struct xa_state *xas,void *entry, +unsigned int order,gfp_t gfp) +{ +}#endif /**
lib/test_xarray.c +41 -0
53 lines not on this page · raw message
lib/xarray.c +148 -9
200 lines not on this page · raw message
Signature · 1 line
2.28.0
Matthew Wilcox (Oracle)
maintainerPATCH 3/3 mm/filemap: Fix storing to a THP shadow entry
In reply to Matthew Wilcox (Oracle)When a THP is removed from the page cache by reclaim, we replace it with a shadow entry that occupies all slots of the XArray previously occupied by the THP. If the user then accesses that page again, we only allocate a single page, but storing it into the shadow entry replaces all entries with that one page. That leads to bugs like
page dumped because: VM_BUG_ON_PAGE(page_to_pgoff(page) != offset) ------------[ cut here ]------------ kernel BUG at mm/filemap.c:2529!
https://bugzilla.kernel.org/show_bug.cgi?id=206569
This is hard to reproduce with mainline, but happens regularly with the THP patchset (as so many more THPs are created). This solution is take from the THP patchset. It splits the shadow entry into order-0 pieces at the time that we bring a new page into cache.
--- mm/filemap.c | 42 +++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-)
mm/filemap.c +31 -11
@@ -829,13 +829,12 @@ EXPORT_SYMBOL_GPL(replace_page_cache_page);static int __add_to_page_cache_locked (struct page *page,struct address_space *mapping, -pgoff_t offset,gfp_t gfp_mask, +pgoff_t offset,gfp_t gfp,void **shadowp) {XA_STATE (xas, &mapping->i_pages, offset);int huge =PageHuge (page);int error; -void *old;VM_BUG_ON_PAGE (!PageLocked (page), page);VM_BUG_ON_PAGE (PageSwapBacked (page), page);
@@ -846,25 +845,46 @@ static int __add_to_page_cache_locked(struct page *page, page->index = offset;if (!huge) { - error =mem_cgroup_charge (page, current->mm, gfp_mask); + error =mem_cgroup_charge (page, current->mm, gfp);if (error) goto error; } + gfp &=GFP_RECLAIM_MASK ; + do { + unsigned int order = xa_get_order(xas.xa, xas.xa_index); + void *entry, *old = NULL; + + if (order > thp_order(page)) + xas_split_alloc(&xas, xa_load(xas.xa, xas.xa_index), + order, gfp); xas_lock_irq(&xas); - old = xas_load(&xas); - if (old && !xa_is_value(old)) - xas_set_err(&xas, -EEXIST); + xas_for_each_conflict(&xas, entry) { + old = entry; + if (!xa_is_value(entry)) { + xas_set_err(&xas, -EEXIST); + goto unlock; + } + } + + if (old) { + if (shadowp) + *shadowp = old; + /* entry may have been split before we acquired lock */ + order = xa_get_order(xas.xa, xas.xa_index); + if (order > thp_order(page)) { + xas_split(&xas, old, order); + xas_reset(&xas); + } + } + xas_store(&xas, page); if (xas_error(&xas)) goto unlock; - if (xa_is_value(old)) { + if (old) mapping->nrexceptional--; - if (shadowp) - *shadowp = old; - } mapping->nrpages++; /* hugetlb pages do not participate in page cache accounting */
@@ -872,7 +892,7 @@ static int __add_to_page_cache_locked(struct page *page,__inc_lruvec_page_state (page,NR_FILE_PAGES ); unlock:xas_unlock_irq (&xas); - }while (xas_nomem (&xas, gfp_mask &GFP_RECLAIM_MASK )); + }while (xas_nomem (&xas, gfp));if (xas_error (&xas)) { error =xas_error (&xas);
Signature · 1 line
2.28.0
Andrew Morton
maintainerQuoted 8 lines · On Thu, 3 Sep 2020 19:30:26 +0100 "Matthew Wilcox (Oracle)"…
As described more verbosely in the [3/3] changelog, we can inadvertently put an order-0 page in the page cache which occupies 512 consecutive entries. Users are running into this if they enable the
READ_ONLY_THP_FOR_FSconfig option; see https://bugzilla.kernel.org/show_bug.cgi?id=206569
and Qian Cai has also reported it here:
https://lore.kernel.org/lkml/[email protected]/
Quoted 5 lines
This is a rather intrusive way of fixing the problem, but has the advantage that I've actually been testing it with the THP patches, which means that it sees far more use than it does upstream -- indeed, Song has been entirely unable to reproduce it. It also has the advantage that it removes a few patches from my gargantuan backlog of THP patches.
ERROR: modpost: "xa_get_order" [lib/test_xarray.ko] undefined!
ERROR: modpost: "xas_split_alloc" [lib/test_xarray.ko] undefined!
xarray is an odd mixture of EXPORT_SYMBOL() and EXPORT_SYMBOL_GPL(). I did this:
lib/xarray.c renamed from lib/xarray.c~xarray-add-xas_split-fix +1 -0
@@ -1025,6 +1025,7 @@ nomem:xas_destroy (xas);xas_set_err (xas, -ENOMEM ); } +EXPORT_SYMBOL_GPL (xas_split_alloc);/** * xas_split() - Split a multi-index entry into smaller entries.
_
and
lib/xarray.c renamed from lib/xarray.c~xarray-add-xa_get_order-fix +1 -0
@@ -1631,6 +1631,7 @@ unlock: return order; } +EXPORT_SYMBOL (xa_get_order);#endif /* CONFIG_XARRAY_MULTI */ /**
_