improve pagecache PSI annotations
Subsystems (8)
Hi all,
currently the VM tries to abuse the block layer submission path for the page cache PSI annotations. This series instead annotates the ->read_folio and ->readahead calls in the core VM code, and then only deals with the odd direct add_to_page_cache_lru calls manually.
Diffstat:
block/bio.c | 8 -------- block/blk-core.c | 17 ----------------- fs/btrfs/compression.c | 14 ++++++++++++-- fs/direct-io.c | 2 -- fs/erofs/zdata.c | 13 ++++++++++++- include/linux/blk_types.h | 1 - include/linux/pagemap.h | 2 ++ kernel/sched/psi.c | 2 ++ mm/filemap.c | 7 +++++++ mm/readahead.c | 22 ++++++++++++++++++---- 10 files changed, 53 insertions(+), 35 deletions(-)
PATCH 1/5 mm: add PSI accounting around ->read_folio and ->readahead calls
PSI tries to account for the cost of bringing back in pages discarded by the MM LRU management. Currently the prime place for that is hooked into the bio submission path, which is a rather bad place:
- it does not actually account I/O for non-block file systems, of which we have many
- it adds overhead and a layering violation to the block layer
Add the accounting into the two places in the core MM code that read pages into an address space by calling into ->read_folio and ->readahead so that the entire file system operations are covered, to broaden the coverage and allow removing the accounting in the block layer going forward.
As psi_memstall_enter can deal with nested calls this will not lead to double accounting even while the bio annotations are still present.
--- include/linux/pagemap.h | 2 ++ mm/filemap.c | 7 +++++++ mm/readahead.c | 22 ++++++++++++++++++---- 3 files changed, 27 insertions(+), 4 deletions(-)
include/linux/pagemap.h +2 -0
@@ -1173,6 +1173,8 @@ struct readahead_control {pgoff_t _index;unsigned int _nr_pages;unsigned int _batch_count; +bool _workingset; +unsigned long _pflags; };#define DEFINE_READAHEAD (ractl, f, r, m, i) \
mm/filemap.c +7 -0
@@ -2382,6 +2382,8 @@ static void filemap_get_read_batch(struct address_space *mapping,static int filemap_read_folio (struct file *file,filler_t filler,struct folio *folio) { +bool workingset =folio_test_workingset (folio); +unsigned long pflags;int error;/*
@@ -2390,8 +2392,13 @@ static int filemap_read_folio(struct file *file, filler_t filler,* fails. */ folio_clear_error (folio); +/* Start the actual read. The read will unlock the page. */ +if (unlikely (workingset)) +psi_memstall_enter (&pflags); error =filler (file, folio); +if (unlikely (workingset)) +psi_memstall_leave (&pflags);if (error)return error;
mm/readahead.c +18 -4
@@ -122,6 +122,7 @@#include <linux/task_io_accounting_ops.h> #include <linux/pagevec.h> #include <linux/pagemap.h> +#include <linux/psi.h> #include <linux/syscalls.h> #include <linux/file.h> #include <linux/mm_inline.h>
@@ -152,6 +153,8 @@ static void read_pages(struct readahead_control *rac)if (!readahead_count (rac))return ; +if (unlikely (rac->_workingset)) +psi_memstall_enter (&rac->_pflags);blk_start_plug (&plug);if (aops->readahead) {
@@ -179,6 +182,9 @@ static void read_pages(struct readahead_control *rac) }blk_finish_plug (&plug); +if (unlikely (rac->_workingset)) +psi_memstall_leave (&rac->_pflags); + rac->_workingset = false;BUG_ON (readahead_count (rac)); }
@@ -252,6 +258,7 @@ void page_cache_ra_unbounded(struct readahead_control *ractl, }if (i == nr_to_read - lookahead_size)folio_set_readahead (folio); + ractl->_workingset |=folio_test_workingset (folio); ractl->_nr_pages++; }
@@ -480,11 +487,14 @@ static inline int ra_alloc_folio(struct readahead_control *ractl, pgoff_t index,if (index == mark)folio_set_readahead (folio); err =filemap_add_folio (ractl->mapping, folio, index, gfp); -if (err) +if (err) {folio_put (folio); -else - ractl->_nr_pages +=1UL << order; -return err; +return err; + } + + ractl->_nr_pages +=1UL << order; + ractl->_workingset =folio_test_workingset (folio); +return 0 ; } void page_cache_ra_order(struct readahead_control *ractl,
@@ -826,6 +836,10 @@ void readahead_expand(struct readahead_control *ractl,put_page (page);return ; } +if (unlikely (PageWorkingset (page)) && !ractl->_workingset) { + ractl->_workingset = true; +psi_memstall_enter (&ractl->_pflags); + } ractl->_nr_pages++;if (ra) { ra->size++;
Signature · 1 line
2.30.2
Jens Axboe
maintainerRe: [PATCH 1/5] mm: add PSI accounting around ->read_folio and ->readahead calls
mm/filemap.c static int filemap_read_folio(struct file *file, filler_t filler, · line 2403
Quoted 13 lines · Christoph Hellwig
mm/filemap.c +5
-0@@ -2390,8 +2392,13 @@ static int filemap_read_folio(struct file *file, filler_t filler,* fails. */ folio_clear_error (folio); +/* Start the actual read. The read will unlock the page. */ +if (unlikely (workingset)) +psi_memstall_enter (&pflags); error =filler (file, folio); +if (unlikely (workingset)) +psi_memstall_leave (&pflags);if (error)return error;
I think this would read better as:
/* Start the actual read. The read will unlock the page. */
if (unlikely(workingset)) {
psi_memstall_enter(&pflags);
error = filler(file, folio);
psi_memstall_leave(&pflags);
} else {
error = filler(file, folio);
} if (error)
return error;
Signature · 1 line
Jens Axboe
Re: [PATCH 1/5] mm: add PSI accounting around ->read_folio and ->readahead calls
Quoted 19 lines · Jens Axboe
mm/filemap.c static int filemap_read_folio(struct file *file, filler_t filler, · line 2403
Quoted 8 lines
mm/filemap.c +4
-0/* Start the actual read. The read will unlock the page. */ +if (unlikely (workingset)) +psi_memstall_enter (&pflags); error =filler (file, folio); +if (unlikely (workingset)) +psi_memstall_leave (&pflags);if (error)return error;I think this would read better as:
/* Start the actual read. The read will unlock the page. */
if (unlikely(workingset)) { psi_memstall_enter(&pflags); error = filler(file, folio); psi_memstall_leave(&pflags); } else { error = filler(file, folio); }
Quoted 2 lines
if (error)
return error;
I had it both ways. For any non-trivial code in the conditionals I tend to go with your version all the time. But for two times a single lines both variants tends to suck, so I can live with either one.
Matthew Wilcox
maintainerRe: [PATCH 1/5] mm: add PSI accounting around ->read_folio and ->readahead calls
In reply to Christoph HellwigQuoted 5 lines · Christoph Hellwig
mm/readahead.c +0
-1@@ -480,11 +487,14 @@ static inline int ra_alloc_folio(struct readahead_control *ractl, pgoff_t index,if (index == mark)folio_set_readahead (folio); err =filemap_add_folio (ractl->mapping, folio, index, gfp); -if (err)
mm/readahead.c static inline int ra_alloc_folio(struct readahead_control *ractl, pgoff_t index, · line 496
Quoted 10 lines
mm/readahead.c +6
-3+if (err) {folio_put (folio); - else - ractl->_nr_pages += 1UL << order; -return err; +return err; + } + + ractl->_nr_pages +=1UL << order; + ractl->_workingset =folio_test_workingset (folio);
I don't have time to look at this properly right now (about to catch a bus to the plane), but I think this should be |=, not =?
Re: [PATCH 1/5] mm: add PSI accounting around ->read_folio and ->readahead calls
Quoted 2 lines · Matthew Wilcox
I don't have time to look at this properly right now (about to catch a bus to the plane), but I think this should be |=, not =?
Yes.
Re: [PATCH 1/5] mm: add PSI accounting around ->read_folio and ->readahead calls
In reply to Christoph HellwigQuoted 14 lines · Christoph Hellwig
PSI tries to account for the cost of bringing back in pages discarded by the MM LRU management. Currently the prime place for that is hooked into the bio submission path, which is a rather bad place:
- it does not actually account I/O for non-block file systems, of which we have many
- it adds overhead and a layering violation to the block layerAdd the accounting into the two places in the core MM code that read pages into an address space by calling into ->read_folio and ->readahead so that the entire file system operations are covered, to broaden the coverage and allow removing the accounting in the block layer going forward.
Quoted 4 lines
As
psi_memstall_entercan deal with nested calls this will not lead to double accounting even while the bio annotations are still present.Signed-off-by Christoph Hellwig <[email protected]>
This is much cleaner. With the fixlet Willy pointed out:
PATCH 2/5 sched/psi: export psi_memstall_{enter,leave}
In reply to Christoph HellwigTo properly account for all refaults from file system logic, file systems need to call psi_memstall_enter directly, so export it.
--- kernel/sched/psi.c | 2 ++ 1 file changed, 2 insertions(+)
kernel/sched/psi.c +2 -0
@@ -917,6 +917,7 @@ void psi_memstall_enter(unsigned long *flags)rq_unlock_irq (rq, &rf); } +EXPORT_SYMBOL_GPL (psi_memstall_enter);/** * psi_memstall_leave - mark the end of an memory stall section
@@ -946,6 +947,7 @@ void psi_memstall_leave(unsigned long *flags)rq_unlock_irq (rq, &rf); } +EXPORT_SYMBOL_GPL (psi_memstall_leave);#ifdef CONFIG_CGROUPS int psi_cgroup_alloc (struct cgroup *cgroup)
Signature · 1 line
2.30.2
Re: [PATCH 2/5] sched/psi: export psi_memstall_{enter,leave}
Quoted 4 lines · Christoph Hellwig
To properly account for all refaults from file system logic, file systems need to call
psi_memstall_enterdirectly, so export it.Signed-off-by Christoph Hellwig <[email protected]>
PATCH 3/5 btrfs: add manual PSI accounting for compressed reads
In reply to Christoph Hellwigbtrfs compressed reads try to always read the entire compressed chunk, even if only a subset is requested. Currently this is covered by the magic PSI accounting underneath submit_bio, but that is about to go away. Instead add manual psi_memstall_{enter,leave} annotations.
Note that for readahead this really should be using readahead_expand, but the additionals reads are also done for plain ->read_folio where readahead_expand can't work, so this overall logic is left as-is for now.
--- fs/btrfs/compression.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)
fs/btrfs/compression.c +12 -2
@@ -15,6 +15,7 @@#include <linux/string.h> #include <linux/backing-dev.h> #include <linux/writeback.h> +#include <linux/psi.h> #include <linux/slab.h> #include <linux/sched/mm.h> #include <linux/log2.h>
@@ -519,7 +520,8 @@ static u64 bio_end_offset(struct bio *bio)*/ static noinline int add_ra_bio_pages(struct inode *inode,u64 compressed_end, -struct compressed_bio *cb) +struct compressed_bio *cb, +unsigned long *pflags) {struct btrfs_fs_info *fs_info =btrfs_sb (inode->i_sb);unsigned long end_index;
@@ -588,6 +590,9 @@ static noinline int add_ra_bio_pages(struct inode *inode,continue ; } +if (unlikely (PageWorkingset (page))) +psi_memstall_enter (pflags); + ret =set_page_extent_mapped (page);if (ret <0 ) {unlock_page (page);
@@ -674,6 +679,8 @@ void btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,u64 em_len;u64 em_start;struct extent_map *em; +/* initialize to 1 to make skip psi_memstall_leave unless needed */ +unsigned long pflags =1 ;blk_status_t ret;int ret2;int i;
@@ -729,7 +736,7 @@ void btrfs_submit_compressed_read(struct inode *inode, struct bio *bio, goto fail; } -add_ra_bio_pages (inode, em_start + em_len, cb); +add_ra_bio_pages (inode, em_start + em_len, cb, &pflags);/* include any pages we added in add_ra-bio_pages */ cb->len = bio->bi_iter.bi_size;
@@ -810,6 +817,9 @@ void btrfs_submit_compressed_read(struct inode *inode, struct bio *bio, } } +if (!pflags) +psi_memstall_leave (&pflags); +if (refcount_dec_and_test (&cb->pending_ios))finish_compressed_bio_read (cb);return ;
Signature · 1 line
2.30.2
Re: [PATCH 3/5] btrfs: add manual PSI accounting for compressed reads
Quoted 11 lines · Christoph Hellwig
btrfs compressed reads try to always read the entire compressed chunk, even if only a subset is requested. Currently this is covered by the magic PSI accounting underneath
submit_bio, but that is about to go away. Instead add manual psi_memstall_{enter,leave} annotations.Note that for readahead this really should be using
readahead_expand, but the additionals reads are also done for plain ->read_folio wherereadahead_expandcan't work, so this overall logic is left as-is for now.Signed-off-by Christoph Hellwig <[email protected]>
With some small fixups,
Quoted 26 lines
fs/btrfs/compression.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)fs/btrfs/compression.c +3
-1@@ -15,6 +15,7 @@#include <linux/string.h> #include <linux/backing-dev.h> #include <linux/writeback.h> +#include <linux/psi.h> #include <linux/slab.h> #include <linux/sched/mm.h> #include <linux/log2.h> @@ -519,7 +520,8 @@ static u64 bio_end_offset(struct bio *bio)*/ static noinline int add_ra_bio_pages(struct inode *inode,u64 compressed_end, -struct compressed_bio *cb) +struct compressed_bio *cb, +unsigned long *pflags) {struct btrfs_fs_info *fs_info =btrfs_sb (inode->i_sb);unsigned long end_index;
fs/btrfs/compression.c static noinline int add_ra_bio_pages(struct inode *inode, · line 593
Quoted 4 lines
fs/btrfs/compression.c +1
-0@@ -588,6 +590,9 @@ static noinline int add_ra_bio_pages(struct inode *inode,continue ; } +if (unlikely (PageWorkingset (page)))
Please drop the 'unlikely', in this case it does not seem to make much sense.
fs/btrfs/compression.c void btrfs_submit_compressed_read(struct inode *inode, struct bio *bio, · line 682
Quoted 10 lines
fs/btrfs/compression.c +3
-0+psi_memstall_enter (pflags); + ret =set_page_extent_mapped (page);if (ret <0 ) {unlock_page (page);@@ -674,6 +679,8 @@ void btrfs_submit_compressed_read(struct inode *inode, struct bio *bio,u64 em_len;u64 em_start;struct extent_map *em; +/* initialize to 1 to make skip psi_memstall_leave unless needed */
First letter in comment should be upper case unless it's an identifier.
Quoted 26 lines
fs/btrfs/compression.c +5
-1+unsigned long pflags =1 ;blk_status_t ret;int ret2;int i;@@ -729,7 +736,7 @@ void btrfs_submit_compressed_read(struct inode *inode, struct bio *bio, goto fail; } -add_ra_bio_pages (inode, em_start + em_len, cb); +add_ra_bio_pages (inode, em_start + em_len, cb, &pflags);/* include any pages we added in add_ra-bio_pages */ cb->len = bio->bi_iter.bi_size;@@ -810,6 +817,9 @@ void btrfs_submit_compressed_read(struct inode *inode, struct bio *bio, } } +if (!pflags) +psi_memstall_leave (&pflags); +if (refcount_dec_and_test (&cb->pending_ios))finish_compressed_bio_read (cb);return ;Signature · 1 line
2.30.2
Re: [PATCH 3/5] btrfs: add manual PSI accounting for compressed reads
In reply to Christoph HellwigQuoted 11 lines · Christoph Hellwig
btrfs compressed reads try to always read the entire compressed chunk, even if only a subset is requested. Currently this is covered by the magic PSI accounting underneath
submit_bio, but that is about to go away. Instead add manual psi_memstall_{enter,leave} annotations.Note that for readahead this really should be using
readahead_expand, but the additionals reads are also done for plain ->read_folio wherereadahead_expandcan't work, so this overall logic is left as-is for now.Signed-off-by Christoph Hellwig <[email protected]>
PATCH 4/5 erofs: add manual PSI accounting for the compressed address space
In reply to Christoph Hellwigerofs uses an additional address space for compressed data read from disk in addition to the one directly associated with the inode. Reading into the lower address space is open coded using add_to_page_cache_lru instead of using the filemap.c helper for page allocation micro-optimizations, which means it is not covered by the MM PSI annotations for ->read_folio and ->readahead, so add manual ones instead.
--- fs/erofs/zdata.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)
fs/erofs/zdata.c +12 -1
@@ -7,6 +7,7 @@#include "zdata.h" #include "compress.h" #include <linux/prefetch.h> +#include <linux/psi.h> #include <trace/events/erofs.h>
@@ -1365,6 +1366,8 @@ static void z_erofs_submit_queue(struct z_erofs_decompress_frontend *f,struct block_device *last_bdev;unsigned int nr_bios =0 ;struct bio *bio =NULL ; +/* initialize to 1 to make skip psi_memstall_leave unless needed */ +unsigned long pflags =1 ; bi_private =jobqueueset_init (sb, q, fgq, force_fg); qtail[JQ_BYPASS ] = &q[JQ_BYPASS ]->head;
@@ -1414,10 +1417,15 @@ static void z_erofs_submit_queue(struct z_erofs_decompress_frontend *f,if (bio && (cur != last_index +1 || last_bdev != mdev.m_bdev)) { submit_bio_retry: +if (!pflags) +psi_memstall_leave (&pflags);submit_bio (bio); bio =NULL ; } +if (unlikely (PageWorkingset (page))) +psi_memstall_enter (&pflags); +if (!bio) { bio =bio_alloc (mdev.m_bdev,BIO_MAX_VECS ,REQ_OP_READ ,GFP_NOIO );
@@ -1445,8 +1453,11 @@ static void z_erofs_submit_queue(struct z_erofs_decompress_frontend *f,move_to_bypass_jobqueue (pcl, qtail, owned_head); }while (owned_head !=Z_EROFS_PCLUSTER_TAIL ); -if (bio) +if (bio) { +if (!pflags) +psi_memstall_leave (&pflags);submit_bio (bio); + }/* * although background is preferred, no one is pending for submission.
Signature · 1 line
2.30.2
Re: [PATCH 4/5] erofs: add manual PSI accounting for the compressed address space
Quoted 8 lines · Christoph Hellwig
erofs uses an additional address space for compressed data read from disk in addition to the one directly associated with the inode. Reading into the lower address space is open coded using
add_to_page_cache_lruinstead of using thefilemap.chelper for page allocation micro-optimizations, which means it is not covered by the MM PSI annotations for ->read_folio and ->readahead, so add manual ones instead.Signed-off-by Christoph Hellwig <[email protected]>
Re: [PATCH 4/5] erofs: add manual PSI accounting for the compressed address space
In reply to Christoph HellwigQuoted 8 lines · Christoph Hellwig
erofs uses an additional address space for compressed data read from disk in addition to the one directly associated with the inode. Reading into the lower address space is open coded using
add_to_page_cache_lruinstead of using thefilemap.chelper for page allocation micro-optimizations, which means it is not covered by the MM PSI annotations for ->read_folio and ->readahead, so add manual ones instead.Signed-off-by Christoph Hellwig <[email protected]>
Thanks, Looks good to me (Although I don't have chance to seek more time digging into PSI internal...)
Thanks,
Gao Xiang,
PATCH 5/5 block: remove PSI accounting from the bio layer
In reply to Christoph HellwigPSI accounting is now done by the VM code, where it should have been since the beginning.
--- block/bio.c | 8 -------- block/blk-core.c | 17 ----------------- fs/direct-io.c | 2 -- include/linux/blk_types.h | 1 - 4 files changed, 28 deletions(-)
block/bio.c +0 -8
@@ -1065,9 +1065,6 @@ void __bio_add_page(struct bio *bio, struct page *page, bio->bi_iter.bi_size += len; bio->bi_vcnt++; - -if (!bio_flagged (bio,BIO_WORKINGSET ) &&unlikely (PageWorkingset (page))) -bio_set_flag (bio,BIO_WORKINGSET ); }EXPORT_SYMBOL_GPL (__bio_add_page);
@@ -1276,9 +1273,6 @@ static int __bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter)* fit into the bio, or are requested in @iter, whatever is smaller. If * MM encounters an error pinning the requested pages, it stops. Error * is returned only if 0 pages could be pinned. -* -* It's intended for direct IO, so doesn't do PSI tracking, the caller is -* responsible for setting BIO_WORKINGSET if necessary. */ int bio_iov_iter_get_pages (struct bio *bio,struct iov_iter *iter) {
@@ -1294,8 +1288,6 @@ int bio_iov_iter_get_pages(struct bio *bio, struct iov_iter *iter) ret =__bio_iov_iter_get_pages (bio, iter); }while (!ret &&iov_iter_count (iter) && !bio_full (bio,0 )); -/* don't account direct I/O as memory stall */ -bio_clear_flag (bio,BIO_WORKINGSET );return bio->bi_vcnt ?0 : ret; }EXPORT_SYMBOL_GPL (bio_iov_iter_get_pages);
block/blk-core.c +0 -17
@@ -37,7 +37,6 @@#include <linux/t10-pi.h> #include <linux/debugfs.h> #include <linux/bpf.h> -#include <linux/psi.h> #include <linux/part_stat.h> #include <linux/sched/sysctl.h> #include <linux/blk-crypto.h>
@@ -829,22 +828,6 @@ void submit_bio(struct bio *bio)count_vm_events (PGPGOUT ,bio_sectors (bio)); } -/* -* If we're reading data that is part of the userspace workingset, count -* submission time as memory stall. When the device is congested, or -* the submitting cgroup IO-throttled, submission can be a significant -* part of overall IO time. -*/ -if (unlikely (bio_op (bio) ==REQ_OP_READ && -bio_flagged (bio,BIO_WORKINGSET ))) { -unsigned long pflags; - -psi_memstall_enter (&pflags); -submit_bio_noacct (bio); -psi_memstall_leave (&pflags); -return ; - } -submit_bio_noacct (bio); }EXPORT_SYMBOL (submit_bio);
fs/direct-io.c +0 -2
@@ -421,8 +421,6 @@ static inline void dio_bio_submit(struct dio *dio, struct dio_submit *sdio)unsigned long flags; bio->bi_private = dio; -/* don't account direct I/O as memory stall */ -bio_clear_flag (bio,BIO_WORKINGSET );spin_lock_irqsave (&dio->bio_lock, flags); dio->refcount++;
include/linux/blk_types.h +0 -1
@@ -321,7 +321,6 @@ enum {BIO_NO_PAGE_REF ,/* don't put release vec pages */ BIO_CLONED ,/* doesn't own data */ BIO_BOUNCED ,/* bio is a bounce bio */ -BIO_WORKINGSET ,/* contains userspace workingset pages */ BIO_QUIET ,/* Make BIO Quiet */ BIO_CHAIN ,/* chained bio, ->bi_remaining in effect */ BIO_REFFED, /* bio has elevated ->bi_cnt */
Signature · 1 line
2.30.2
Re: [PATCH 5/5] block: remove PSI accounting from the bio layer
Quoted 4 lines · Christoph Hellwig
PSI accounting is now done by the VM code, where it should have been since the beginning.
Signed-off-by Christoph Hellwig <[email protected]>
Nice!
Jens Axboe
maintainerQuoted 8 lines · Christoph Hellwig
Hi all,
currently the VM tries to abuse the block layer submission path for the page cache PSI annotations. This series instead annotates the ->read_folio and ->readahead calls in the core VM code, and then only deals with the odd direct
add_to_page_cache_lrucalls manually.Diffstat:
Quoted 11 lines
block/bio.c | 8 -------- block/blk-core.c | 17 ----------------- fs/btrfs/compression.c | 14 ++++++++++++-- fs/direct-io.c | 2 -- fs/erofs/zdata.c | 13 ++++++++++++- include/linux/blk_types.h | 1 - include/linux/pagemap.h | 2 ++ kernel/sched/psi.c | 2 ++ mm/filemap.c | 7 +++++++ mm/readahead.c | 22 ++++++++++++++++++---- 10 files changed, 53 insertions(+), 35 deletions(-)
Nice! It's always bothered me that we have this weird layering here.
Signature · 1 line
Jens Axboe