netfs: Use a bvecq chain position abstraction
Subsystems (12)
David Howells
maintainerv17 cover netfs: Use a bvecq chain position abstraction
Hi Christian,
Could you pull these patches please into your vfs-7.4.netfs branch? This is the fourth of five batches. The primary purpose of this batch is to add an abstraction for a position in a bvecq chain and make use of it.
With that in mind, this patchset does the following:
(1) Add a struct, bvecq_pos, to refer to a position in a bvecq chain. This is a tuple of ( bvecq pointer, slot and offset } and the position struct holds (for now) a ref on the bvecq pointed to.
(2) Change unbuffered/DIO to extract the supplied user buffers into a bvecq chain rather than a bio_vec[] up front. This means that netfslib then uses bvecq chains for everything.
(3) Add a folio count to the readahead_control struct and then makes readahead pre-create its buffer using that count to determine how many slots are required.
(4) Change netfslib to use the bvecq_pos struct to keep track of the whole request buffer and various points in it where loading, dispatch and collection are taking place and to also keep track of the slice given to each subrequest.
This was split from v11 of a larger netfslib series[1].
The patches can also be found here:
https://git.kernel.org/pub/scm/linux/kernel/git/dhowells/linux-fs.git/log/?h=netfs-next-4
Thanks,
David
Changes
=======
ver #17)
- Fixed issues brought up by sashiko[5]:
- In bvecq_slice(), return 0 immediately if max_size or max_slots are 0.
- Make sure that sreq_max_{len,segs} are initialised before calling prepare_read.
- Fixed an issue brought up by the kernel test robot[6]:
- In netfs_read_gaps(), use umax() rather than max() when comparing a signed and an unsigned int.
ver #16)
- Fixed issues brought up by sashiko[4]:
- In bvecq_pos_nudge(), recheck the old bvecq, not the new one.
- In bvecq_slice(), recheck the old bvecq.
- In bvecq_pos_step(), don't assume the new bvecq has slots, but rather check it.
ver #15)
- In netfs_extract_iter(), set BVECQ_MEM_EXTERNAL rather than BVECQ_MEM_PAGECACHE for unpinned memory.
- In netfs_clear_unread_dio(), don't need to update subreq->transferred as the caller overwrites it after.
- Fixed issues brought up by sashiko[3]:
- In netfs_extract_iter(), remove redundant max_len check.
- In bvecq_pos_nudge(), make sure the recheck is checking the old bvecq, not the new.
- In netfs_unlock_abandoned_read_pages(), start from collect_cursor.slot, not from slot zero.
- In netfs_pgpriv2_copy_to_cache(), cancel the caching also if we fail to allocate a bvecq.
- In netfs_retry_write_stream(), use the same superfluity detection as the read side.
- In netfs_write_subrequest_terminated(), only set the RETRY flag on short uploads, not short cache writes.
ver #14)
- Fixed issues brought up by sashiko[2]:
- In netfs_extract_iter(), limit the number of slots per bvecq to what will fit inside a 4KiB allocation.
- In netfs_rreq_assess_dio(), only set the page dirty if it's from GUP or it's from the pagecache.
- In bvecq_zero(), skip the call to bvec_zero() if part == 0.
- In bvecq_delete_spent(), prefer returning true because more slots got added to returning false because we hit the chain end.
- In netfs_read_gaps(), fix a leak of already allocated sink folios if we get OOM when allocating a sink folios.
- In netfs_clear_unread_dio(), fix double call to iov_iter_zero().
ver #13)
- Rebased on -rc6 to account for some conflicting fixes.
- Fixed a bug in netfs_prepare_read_iterator() where comparison was being made against the wrong thing when limiting a subreq's size.
ver #12)
- Split from v11 of "netfs: Keep track of folios in a segmented bio_vec[] chain"[1]
[1] https://lore.kernel.org/r/[email protected]/
[2] https://sashiko.dev/#/patchset/20261006205947.608356-1-dhowells%40redhat.com
[3] https://sashiko.dev/#/patchset/20261007142001.851620-1-dhowells%40redhat.com
[4] https://sashiko.dev/#/patchset/20261008074346.936909-1-dhowells%40redhat.com
[5] https://sashiko.dev/#/patchset/20261008084722.963948-1-dhowells%40redhat.com
[6] https://lore.kernel.org/oe-kbuild-all/[email protected]/
David Howells (8):
mm: Make readahead store folio count in readahead_control
netfs: Add a function to extract from an iter into a bvecq
netfs: Make unbuffered/DIO read and write use bvecq
netfs: Add some tools for managing a position in a bvecq chain
netfs: Provide a func to load the readahead buffers into a bvecq chain
netfs: Use bvecq_pos to hold the buffer positions
netfs: Remove the rolling_buffer implementation
netfs: Remove netfs_extract_user_iter()
fs/afs/dir.c | 5 +- fs/afs/symlink.c | 6 +- fs/cachefiles/io.c | 12 - fs/netfs/Makefile | 1 - fs/netfs/buffered_read.c | 182 ++++++++------- fs/netfs/bvecq.c | 327 +++++++++++++++++++++++++++ fs/netfs/direct_read.c | 81 +++---- fs/netfs/direct_write.c | 67 +++--- fs/netfs/internal.h | 4 +- fs/netfs/iterator.c | 398 ++++++++++----------------------- fs/netfs/misc.c | 18 -- fs/netfs/objects.c | 16 +- fs/netfs/read_collect.c | 106 +++++---- fs/netfs/read_pgpriv2.c | 88 +++++--- fs/netfs/read_retry.c | 87 ++++--- fs/netfs/read_single.c | 19 +- fs/netfs/rolling_buffer.c | 182 --------------- fs/netfs/write_collect.c | 73 +++--- fs/netfs/write_issue.c | 166 +++++++++----- fs/netfs/write_retry.c | 73 +++--- include/linux/bvecq.h | 185 +++++++++++++++ include/linux/netfs.h | 25 ++- include/linux/pagemap.h | 10 + include/linux/rolling_buffer.h | 47 ---- include/trace/events/netfs.h | 32 ++- mm/readahead.c | 5 + 26 files changed, 1264 insertions(+), 951 deletions(-) delete mode 100644 fs/netfs/rolling_buffer.c delete mode 100644 include/linux/rolling_buffer.h
David Howells
maintainerPATCH v17 1/8 mm: Make readahead store folio count in readahead_control
Make readahead store folio count in readahead_control so that the filesystem can know in advance how many folios it needs to keep track of.
This is cleared by read_pages() in case it is called from a loop.
The count is accessed by the filesystem with readahead_folio_count().
--- include/linux/pagemap.h | 10 ++++++++++ mm/readahead.c | 5 +++++ 2 files changed, 15 insertions(+)
include/linux/pagemap.h +10 -0
@@ -1411,6 +1411,7 @@ struct readahead_control {struct file_ra_state *ra;/* private: use the readahead_* accessors instead */ pgoff_t _index; +unsigned int _nr_folios;unsigned int _nr_pages;unsigned int _batch_count;bool dropbehind;
@@ -1590,6 +1591,15 @@ static inline size_t readahead_batch_length(const struct readahead_control *rac)return rac->_batch_count *PAGE_SIZE ; } +/** +* readahead_folio_count - Get the number of folios in this readahead request. +* @rac: The readahead request. +*/ +static inline unsigned int readahead_folio_count (const struct readahead_control *rac) +{ +return rac->_nr_folios; +} +static inline unsigned long dir_pages (const struct inode *inode) { return (unsigned long)(inode->i_size + PAGE_SIZE - 1) >>
mm/readahead.c +5 -0
@@ -188,6 +188,7 @@ static void read_pages(struct readahead_control *rac)if (unlikely (rac->_workingset))psi_memstall_leave (&rac->_pflags); rac->_workingset = false; + rac->_nr_folios =0 ;BUG_ON (readahead_count (rac)); }
@@ -303,6 +304,7 @@ void page_cache_ra_unbounded(struct readahead_control *ractl,if (i == mark)folio_set_readahead (folio); ractl->_workingset |=folio_test_workingset (folio); + ractl->_nr_folios++; ractl->_nr_pages += min_nrpages; i += min_nrpages; }
@@ -473,6 +475,7 @@ static inline int ra_alloc_folio(struct readahead_control *ractl, pgoff_t index,return err; } + ractl->_nr_folios++; ractl->_nr_pages +=1UL << order; ractl->_workingset |=folio_test_workingset (folio);return 0 ;
@@ -822,6 +825,7 @@ void readahead_expand(struct readahead_control *ractl, ractl->_workingset = true;psi_memstall_enter (&ractl->_pflags); } + ractl->_nr_folios++; ractl->_nr_pages += min_nrpages; ractl->_index = folio->index; }
@@ -851,6 +855,7 @@ void readahead_expand(struct readahead_control *ractl, ractl->_workingset = true;psi_memstall_enter (&ractl->_pflags); } + ractl->_nr_folios++; ractl->_nr_pages += min_nrpages;if (ra) { ra->size += min_nrpages;
David Howells
maintainerPATCH v17 2/8 netfs: Add a function to extract from an iter into a bvecq
In reply to David HowellsAdd a function to extract a slice of data from an iterator of any type into a bvec queue chain.
--- fs/netfs/iterator.c | 143 ++++++++++++++++++++++++++++++++++++++++++ include/linux/netfs.h | 3 + 2 files changed, 146 insertions(+)
fs/netfs/iterator.c +143 -0
@@ -13,6 +13,149 @@#include <linux/netfs.h> #include "internal.h" +/** +* netfs_extract_iter - Extract virtually contiguous pages from an iterator into a bvecq +* @orig: The original iterator +* @max_len: Maximum number of bytes to extract +* @max_pages: Maximum number of pages to extract +* @_bvecq_head: Where to cache the bvec queue +* @extraction_flags: Flags to qualify the request +* @gfp: Allocation mode for bvecq structs. +* +* Extract virtually contiguous page fragments from the source iterator up to +* the given maxima and build bvec queue that refers to all of those bits. +* This allows the original iterator to disposed of. +* +* @extraction_flags can have ITER_ALLOW_P2PDMA set to request peer-to-peer DMA +* be allowed on the pages extracted. +* +* On success or partial success, the amount of data in the bvec is returned, +* the original iterator will have been advanced by the amount extracted. +* +* If an error occurs and no pages are extracted, an error will be returned and +* any allocated bvecq will be freed. If there is no data to be extracted (or +* @max_len or @max_pages are zero), a single empty bvecq will be returned. +* +* The bvecq segments are marked with indications on how to get clean up the +* extracted fragments. +*/ +ssize_t netfs_extract_iter (struct iov_iter *orig,size_t max_len,size_t max_pages, +struct bvecq **_bvecq_head, +iov_iter_extraction_t extraction_flags,gfp_t gfp) +{ +struct bvecq *bq_tail =NULL , *bq; +ssize_t ret =0 ; +size_t extracted =0 ; + +_enter ("{%u,%zx},%zx" , orig->iter_type, orig->count, max_len); + + *_bvecq_head =NULL ; +if (max_len > orig->count) + max_len = orig->count; +if (!max_len || !max_pages) + goto alloc_empty; +if (WARN_ON_ONCE (max_pages >INT_MAX )) + max_pages =INT_MAX ;/* Protect iov_iter_npages(). */ + + max_pages =iov_iter_npages (orig, max_pages); +if (!max_pages) + goto alloc_empty; + +do { + bq =bvecq_alloc_one (min (max_pages,BVECQ_4KB_SLOTS ), gfp, false); +if (!bq) { + ret = -ENOMEM ; +break ; + } +if (user_backed_iter (orig)) + bq->mem_type =iov_iter_extract_will_pin (orig) ? +BVECQ_MEM_GUP :BVECQ_MEM_EXTERNAL ; + +if (bq_tail) +bvecq_append (bq_tail, bq); +else + *_bvecq_head = bq; + bq_tail = bq; + +struct bio_vec *bv = bq->bv; +unsigned int slot =0 ; +do { +struct page **pages; +ssize_t got; +size_t offset; +size_t space = bq->max_slots - slot; +size_t bv_size =array_size (bq->max_slots,sizeof (*bv)); +size_t pg_size =array_size (space,sizeof (*pages)); + +/* Put the page list at the end of the bvec list +* storage. bvec elements are larger than page +* pointers, so as long as we work 0->last, we should +* be fine. +*/ + pages = (void *)bv + bv_size - pg_size; + + got =iov_iter_extract_pages (orig, &pages, max_len, +min (space, max_pages), + extraction_flags, &offset); +if (got <0 ) { + ret = got; + goto out; + } + +if (got ==0 ) { +pr_err ("extract_pages gave nothing from %zx, %zx\n" , + extracted, max_len); + ret = -EIO ; + goto out; + } + +if (WARN (got > max_len, +"%s: extract_pages overrun %zx > %zx bytes\n" , + __func__, got, max_len)) { +/* Abandon what we got as very likely the +* overrun corrupted memory. +*/ + ret = -EIO ; + goto out; + } + + extracted += got; + max_len -= got; + +do { +size_t len =umin (got,PAGE_SIZE - offset); + +BUG_ON (slot >= bq->max_slots); + +bvec_set_page (&bq->bv[slot], *pages++, len, offset); + slot++; + max_pages--; + got -= len; + offset =0 ; + }while (got >0 ); + +bvecq_filled_to (bq, slot); + }while (max_len >0 && max_pages >0 && !bvecq_is_full (bq)); + + }while (max_len >0 && max_pages >0 ); + +out: +if (extracted || ret ==0 ) +return extracted; +bvecq_put (*_bvecq_head); + *_bvecq_head =NULL ; +return ret; + +alloc_empty: + bq =bvecq_alloc_one (1 , gfp, false); +if (!bq) +return -ENOMEM ; + *_bvecq_head = bq; +return 0 ; + +} +EXPORT_SYMBOL_GPL (netfs_extract_iter); +/** * netfs_extract_user_iter - Extract the pages from a user iterator into a bvec * @orig: The original iterator
include/linux/netfs.h +3 -0
@@ -454,6 +454,9 @@ void netfs_get_subrequest(struct netfs_io_subrequest *subreq, enum netfs_sreq_ref_trace what);void netfs_put_subrequest (struct netfs_io_subrequest *subreq,enum netfs_sreq_ref_trace what); +ssize_t netfs_extract_iter (struct iov_iter *orig,size_t max_len,size_t max_pages, +struct bvecq **_bvecq_head, +iov_iter_extraction_t extraction_flags,gfp_t gfp);ssize_t netfs_extract_user_iter (struct iov_iter *orig,size_t orig_len,struct iov_iter *new,iov_iter_extraction_t extraction_flags);
David Howells
maintainerPATCH v17 3/8 netfs: Make unbuffered/DIO read and write use bvecq
In reply to David HowellsMake unbuffered and DIO read and write in netfslib use bvecq. The idea is to only use bvecq in talking to the filesystem and not pass iterators at all as the filesystem may just end up extracting them.
--- fs/netfs/direct_read.c | 31 +++++++++++++------------------ fs/netfs/direct_write.c | 33 ++++++++++++++------------------- fs/netfs/objects.c | 11 +---------- fs/netfs/read_collect.c | 17 +++++++++++------ include/linux/netfs.h | 4 +--- 5 files changed, 40 insertions(+), 56 deletions(-)
fs/netfs/direct_read.c +13 -18
@@ -177,25 +177,20 @@ ssize_t netfs_unbuffered_read_iter_locked(struct kiocb *iocb, struct iov_iter *i* buffer for ourselves as the caller's iterator will be trashed when * we return. * -* In such a case, extract an iterator to represent as much of the the -* output buffer as we can manage. Note that the extraction might not -* be able to allocate a sufficiently large bvec array and may shorten -* the request. +* Extract a buffer queue to represent as much of the output buffer as +* we can manage. The fragments are extracted into a bvecq which will +* have sufficient nodes allocated to hold all the data, though this +* may end up truncated if ENOMEM is encountered. */ -if (user_backed_iter (iter)) { - ret =netfs_extract_user_iter (iter, rreq->len, &rreq->buffer.iter,0 ); -if (ret <0 ) - goto error_put; - rreq->direct_bv = (struct bio_vec *)rreq->buffer.iter.bvec; - rreq->direct_bv_count = ret; - rreq->direct_bv_unpin =iov_iter_extract_will_pin (iter); - rreq->len =iov_iter_count (&rreq->buffer.iter); - }else { - rreq->buffer.iter = *iter; - rreq->len = orig_count; - rreq->direct_bv_unpin = false; -iov_iter_advance (iter, orig_count); - } + ret =netfs_extract_iter (iter, rreq->len,INT_MAX , + &rreq->direct_bq,0 , rreq->gfp); +if (ret <0 ) + goto error_put; + + rreq->len = ret; + +iov_iter_bvec_queue (&rreq->buffer.iter,ITER_DEST , rreq->direct_bq, +0 ,0 , rreq->len);// TODO: Set up bounce buffer if needed
fs/netfs/direct_write.c +14 -19
@@ -222,10 +222,10 @@ static void netfs_unbuffered_write_async(struct work_struct *work)* encrypted file. This can also be used for direct I/O writes. */ ssize_t netfs_unbuffered_write_iter_locked (struct kiocb *iocb,struct iov_iter *iter, -struct netfs_group *netfs_group) +struct netfs_group *netfs_group) {struct netfs_io_request *wreq; -ssize_t ret, n; +ssize_t ret;uoff_t start = iocb->ki_pos;uoff_t end = start +iov_iter_count (iter);size_t len =iov_iter_count (iter);
@@ -261,25 +261,20 @@ ssize_t netfs_unbuffered_write_iter_locked(struct kiocb *iocb, struct iov_iter ** allocate a sufficiently large bvec array and may shorten the * request. */ -if (user_backed_iter (iter)) { - n =netfs_extract_user_iter (iter, len, &wreq->buffer.iter,0 ); -if (n <0 ) { - ret = n; - goto error_put; - } - wreq->direct_bv = (struct bio_vec *)wreq->buffer.iter.bvec; - wreq->direct_bv_count = n; - wreq->direct_bv_unpin =iov_iter_extract_will_pin (iter); - }else { -/* If this is a kernel-generated async DIO request, -* assume that any resources the iterator points to -* (eg. a bio_vec array) will persist till the end of -* the op. -*/ - wreq->buffer.iter = *iter; +ssize_t n =netfs_extract_iter (iter, len,INT_MAX , + &wreq->direct_bq,0 , wreq->gfp); + +if (n <0 ) { + ret = n; + goto error_put; } + wreq->len = n; +_debug ("dio-write %zx/%zx %u/%u" , + n, len, wreq->direct_bq->nr_slots, + wreq->direct_bq->max_slots); - wreq->len =iov_iter_count (&wreq->buffer.iter); +iov_iter_bvec_queue (&wreq->buffer.iter,ITER_SOURCE , + wreq->direct_bq,0 ,0 , wreq->len); }__set_bit (NETFS_RREQ_USE_IO_ITER , &wreq->flags);
fs/netfs/objects.c +1 -10
@@ -133,7 +133,6 @@ static void netfs_free_request_rcu(struct rcu_head *rcu)static void netfs_deinit_request (struct netfs_io_request *rreq) {struct netfs_inode *ictx =netfs_inode (rreq->inode); -unsigned int i;trace_netfs_rreq (rreq, netfs_rreq_trace_free);
@@ -148,15 +147,7 @@ static void netfs_deinit_request(struct netfs_io_request *rreq) rreq->netfs_ops->free_request (rreq);if (rreq->cache_resources.ops) rreq->cache_resources.ops->end_operation (&rreq->cache_resources); -if (rreq->direct_bv) { -for (i =0 ; i < rreq->direct_bv_count; i++) { -if (rreq->direct_bv[i].bv_page) { -if (rreq->direct_bv_unpin) -unpin_user_page (rreq->direct_bv[i].bv_page); - } - } -kvfree (rreq->direct_bv); - } +bvecq_put (rreq->direct_bq);rolling_buffer_clear (&rreq->buffer);if (atomic_dec_and_test (&ictx->io_count))
fs/netfs/read_collect.c +11 -6
@@ -422,12 +422,17 @@ static void netfs_rreq_assess_dio(struct netfs_io_request *rreq)if (rreq->origin ==NETFS_UNBUFFERED_READ || rreq->origin ==NETFS_DIO_READ ) { -for (i =0 ; i < rreq->direct_bv_count; i++) { -flush_dcache_page (rreq->direct_bv[i].bv_page); -// TODO: cifs marks pages in the destination buffer -// dirty under some circumstances after a read. Do we -// need to do that too? -set_page_dirty (rreq->direct_bv[i].bv_page); +for (struct bvecq *bq = rreq->direct_bq; bq; bq =bvecq_next (bq)) { +unsigned int nr_slots =bvecq_nr_slots_acquire (bq); +/* Read the slot count before the slots. */ + +/* Mark the target buffers dirty. */ +for (i =0 ; i < nr_slots; i++) { +flush_dcache_page (bq->bv[i].bv_page); +if (bq->mem_type ==BVECQ_MEM_GUP || + bq->mem_type ==BVECQ_MEM_PAGECACHE ) +set_page_dirty (bq->bv[i].bv_page); + } } }
include/linux/netfs.h +1 -3
@@ -251,7 +251,7 @@ struct netfs_io_request {wait_queue_head_t waitq;/* Processor waiter */ void *netfs_priv;/* Private data for the netfs */ void *netfs_priv2;/* Private data for the netfs */ -struct bio_vec *direct_bv;/* DIO buffer list (when handling iovec-iter) */ +struct bvecq *direct_bq;/* DIO buffer list (when handling iovec-iter) */ uoff_t submitted;/* Amount submitted for I/O so far */ uoff_t len;/* Length of the request */ size_t transferred;/* Amount to be indicated as transferred */
@@ -266,7 +266,6 @@ struct netfs_io_request {uoff_t abandon_to;/* Position to abandon folios to */ const struct folio *no_unlock_folio;/* Don't unlock this folio after read */ gfp_t gfp;/* GFP flags to use */ -unsigned int direct_bv_count;/* Number of elements in direct_bv[] */ unsigned int debug_id;unsigned int rsize;/* Maximum read size (0 for none) */ unsigned int wsize;/* Maximum write size (0 for none) */
@@ -274,7 +273,6 @@ struct netfs_io_request {unsigned int nr_group_rel;/* Number of refs to release on ->group */ spinlock_t lock;/* Lock for queuing subreqs */ enum netfs_io_origin origin;/* Origin of the request */ -bool direct_bv_unpin;/* T if direct_bv[] must be unpinned */ refcount_t ref;unsigned long flags;#define NETFS_RREQ_IN_PROGRESS 0/* Unlocked when the request completes (has ref) */
David Howells
maintainerPATCH v17 4/8 netfs: Add some tools for managing a position in a bvecq chain
In reply to David HowellsProvide a selection of tools for holding and managing a position within a bvec queue chain.
--- fs/netfs/bvecq.c | 267 ++++++++++++++++++++++++++++++++++++++++++ include/linux/bvecq.h | 184 +++++++++++++++++++++++++++++ 2 files changed, 451 insertions(+)
fs/netfs/bvecq.c +267 -0
@@ -342,3 +342,270 @@ int bvecq_expand_buffer(struct bvecq **_buffer, size_t *_cur_size, size_t size,return 0 ; }EXPORT_SYMBOL (bvecq_expand_buffer); + +/** +* bvecq_buffer_init - Initialise a buffer and set position +* @pos: The position to point at the new buffer. +* @gfp: The allocation constraints. +* @for_writeback: True if allocating for writeback +* +* Initialise a rolling buffer. We allocate an unpopulated bvecq node to so +* that the pointers can be independently driven by the producer and the +* consumer. +* +* Return 0 if successful; -ENOMEM on allocation failure. +*/ +int bvecq_buffer_init (struct bvecq_pos *pos,gfp_t gfp,bool for_writeback) +{ +struct bvecq *bq; + + bq =bvecq_alloc_one (BVECQ_POOL_SLOTS , gfp, for_writeback); +if (!bq) +return -ENOMEM ; + + pos->bvecq = bq;/* Comes with a ref. */ + pos->slot =0 ; + pos->offset =0 ; +return 0 ; +} + +/** +* bvecq_buffer_append - Append a new bvecq node to a buffer +* @pos: The position of the last node. +* @bq: The buffer to add. +* +* Add a new node on to the buffer chain at the specified position, either +* because the previous one is full or because we have a discontiguity to +* contend with, and update @pos to point to it. +*/ +void bvecq_buffer_append (struct bvecq_pos *pos,struct bvecq *bq) +{ +struct bvecq *head = pos->bvecq; + + pos->bvecq =bvecq_get (bq); + pos->slot =0 ; + pos->offset =0 ; + +/* [!] NOTE: After we set head->next, the consumer is at liberty to +* immediately delete the old head. +*/ +bvecq_append (head, bq); +bvecq_put (head); +} + +/** +* bvecq_pos_advance - Advance a bvecq position +* @pos: The position to advance. +* @amount: The amount of bytes to advance by. +* +* Advance the specified bvecq position by @amount bytes. @pos is updated and +* bvecq ref counts may have been manipulated. If the position hits the end of +* the queue, then it is left pointing beyond the last slot of the last bvecq +* so that it doesn't break the chain. +*/ +void bvecq_pos_advance (struct bvecq_pos *pos,size_t amount) +{ +struct bvecq *bq = pos->bvecq, *next; +unsigned int slot = pos->slot; +size_t offset = pos->offset; + +while (amount) { +size_t part; + +if (!bvecq_acquire_slot (bq, slot)) { + next =bvecq_next (bq); +if (!next) { +/* We should not see the end of the list yet as +* the chain must already have been populated +* with sufficient slots to provide @amount of +* bufferage before calling this function. +*/ +WARN_ON_ONCE (amount >0 ); +break ; + } +if (bvecq_acquire_slot (bq, slot)) +continue ;/* More slots got added. */ + bq = next; + slot =0 ; + offset =0 ; +continue ; + } + + part = bq->bv[slot].bv_len - offset; + +if (part > amount) { + offset += amount; +break ; + } + amount -= part; + offset =0 ; + slot++; + } + + pos->slot = slot; + pos->offset = offset; +bvecq_pos_move (pos, bq); +} + +/* +* Clear part of the memory pointed to by a bio_vec. +*/ +static void bvec_zero (const struct bio_vec *bv,size_t offset,size_t len) +{ +struct page *page = bv->bv_page; + + offset += bv->bv_offset; + + page += offset /PAGE_SIZE ; + offset = offset %PAGE_SIZE ; + +while (len) { +size_t part =min (len,PAGE_SIZE - offset); +char *p =kmap_local_page (page); + +memset (p + offset,0 , part); +kunmap_local (p); + + len -= part; + offset =0 ; + page++; + } +} + +/** +* bvecq_zero - Clear memory starting at the bvecq position. +* @pos: The position in the bvecq chain to start clearing. +* @amount: The number of bytes to clear. +* +* Clear memory fragments pointed to by a bvec queue. @pos is updated and +* bvecq ref counts may have been manipulated. If the position hits the end of +* the queue, then it is left pointing beyond the last slot of the last bvecq +* so that it doesn't break the chain. +* +* Return: The number of bytes cleared. +*/ +ssize_t bvecq_zero (struct bvecq_pos *pos,size_t amount) +{ +struct bvecq *bq = pos->bvecq, *next; +unsigned int slot = pos->slot; +ssize_t cleared =0 ; +size_t offset = pos->offset; + +while (amount) { +const struct bio_vec *bv; +size_t part; + +if (!bvecq_acquire_slot (bq, slot)) { + next =bvecq_next (bq); +if (!next) { +/* We should not see the end of the list yet as +* the chain must already have been populated +* with sufficient slots to provide @amount of +* bufferage before calling this function. +*/ +WARN_ON_ONCE (amount >0 ); +break ; + } +if (bvecq_acquire_slot (bq, slot)) +continue ;/* More slots got added. */ + bq = next; + slot =0 ; + offset =0 ; +continue ; + } + + bv = &bq->bv[slot]; +if (offset >= bv->bv_len) { + slot++; + offset =0 ; +continue ; + } + + part =min (bv->bv_len - offset, amount); +if (part >0 ) +bvec_zero (bv, offset, part); + cleared += part; + offset += part; + amount -= part; + } + + pos->slot = slot; + pos->offset = offset; +bvecq_pos_move (pos, bq); +return cleared; +} + +/** +* bvecq_slice - Find a slice of a bvecq queue +* @pos: The position to start at. +* @max_size: The maximum size of the slice (or ULONG_MAX). +* @max_slots: The maximum number of slots in the slice (or INT_MAX). +* @_nr_slots: Where to put the number of slots (updated). +* +* Determine the size and number of slots that can be obtained the next slice +* of bvec queue up to the maximum size and slot count specified. +* +* @pos is updated to the end of the slice. If the position hits the end of +* the queue, then it is left pointing beyond the last slot of the last bvecq +* so that it doesn't break the chain. +* +* Return: The number of bytes in the slice. +*/ +size_t bvecq_slice (struct bvecq_pos *pos,size_t max_size, +unsigned int max_slots,unsigned int *_nr_slots) +{ +struct bvecq *bq, *next; +unsigned int slot = pos->slot, nslots =0 ; +size_t size =0 , offset = pos->offset; + +if (unlikely (!max_size || !max_slots)) { + *_nr_slots =0 ; +return 0 ; + } + + bq = pos->bvecq; +for (;;) { +for (; slot <bvecq_nr_slots_acquire (bq); slot++) { +const struct bio_vec *bvec = &bq->bv[slot]; + +if (offset < bvec->bv_len && bvec->bv_page) { +size_t part =min (bvec->bv_len - offset, max_size); + + size += part; + offset += part; + max_size -= part; + nslots++; +if (!max_size || nslots >= max_slots) + goto out; + } + offset =0 ; + } + +/* pos->bvecq isn't allowed to go NULL as the queue may get +* extended and we would lose our place. +*/ + next =bvecq_next (bq); +if (!next) +break ; +if (bvecq_acquire_slot (bq, slot)) +continue ;/* More slots got added. */ + slot =0 ; + bq = next; + } + +out: + *_nr_slots = nslots; +if (slot >=bvecq_nr_slots_acquire (bq)) { + next =bvecq_next (bq); +/* Recheck nr_slots as more slots may have been added. */ +if (next && slot >=bvecq_nr_slots_acquire (bq)) { + bq = next; + slot =0 ; + offset =0 ; + } + } +bvecq_pos_move (pos, bq); + pos->slot = slot; + pos->offset = offset; +return size; +}
include/linux/bvecq.h +184 -0
200 lines not on this page · raw message
David Howells
maintainerPATCH v17 5/8 netfs: Provide a func to load the readahead buffers into a bvecq chain
In reply to David HowellsIn netfslib, provide a function to load the readahead buffers into a bvecq chain by preallocating the bvecq chain and then iterating directly over the pagecache to extract all the folios. This builds on the preceding patch to note the folio count in the readahead_control struct.
--- fs/netfs/bvecq.c | 60 +++++++++++++++++++++++++++++++++++++++++++ include/linux/bvecq.h | 1 + 2 files changed, 61 insertions(+)
fs/netfs/bvecq.c +60 -0
@@ -609,3 +609,63 @@ size_t bvecq_slice(struct bvecq_pos *pos, size_t max_size, pos->offset = offset;return size; } + +/** +* bvecq_load_from_ra - Allocate a bvecq chain and load from readahead +* @pos: Blank position object to attach the new chain to. +* @ractl: The readahead control context. +* +* Decant the set of folios to be read from the readahead context into a bvecq +* chain. Each folio occupies one bio_vec element. +* +* Return: Amount of data loaded or -ENOMEM on allocation failure. +*/ +ssize_t bvecq_load_from_ra (struct bvecq_pos *pos,struct readahead_control *ractl) +{ +XA_STATE (xas, &ractl->mapping->i_pages, ractl->_index); +struct folio *folio; +struct bvecq *bq; +unsigned int slot =0 ; +size_t loaded =0 ; + + bq =bvecq_alloc_chain (ractl->_nr_folios,GFP_KERNEL , false); +if (!bq) +return -ENOMEM ; + + pos->bvecq = bq; + pos->slot =0 ; + pos->offset =0 ; + +rcu_read_lock (); + +xas_for_each (&xas, folio, ractl->_index + ractl->_nr_pages -1 ) { +size_t len; + +if (xas_retry (&xas, folio)) +continue ; +VM_BUG_ON_FOLIO (!folio_test_locked (folio), folio); + + len =folio_size (folio); +bvec_set_folio (&bq->bv[slot], folio, len,0 ); + loaded += len; + slot++; +trace_netfs_folio (folio, netfs_folio_trace_read); + +if (slot >= bq->max_slots) { +bvecq_filled_to (bq, slot); + bq = bq->next; +if (!bq) +break ; + slot =0 ; + } + } + +rcu_read_unlock (); + +if (bq) +bvecq_filled_to (bq, slot); + + ractl->_index += ractl->_nr_pages; + ractl->_nr_pages =0 ; +return loaded; +}
include/linux/bvecq.h +1 -0
@@ -77,6 +77,7 @@ void bvecq_pos_advance(struct bvecq_pos *pos, size_t amount);ssize_t bvecq_zero (struct bvecq_pos *pos,size_t amount);size_t bvecq_slice (struct bvecq_pos *pos,size_t max_size,unsigned int max_slots,unsigned int *_nr_slots); +ssize_t bvecq_load_from_ra (struct bvecq_pos *pos,struct readahead_control *ractl);/** * bvecq_alloc_buffer - Allocate a bvecq chain and populate with buffers
David Howells
maintainerPATCH v17 6/8 netfs: Use bvecq_pos to hold the buffer positions
In reply to David HowellsUse struct bvecq_pos to hold the position in a buffer for various things, including the point at which memory segments are loaded into a rolling buffer or are cleaned up from that buffer and the point at which a subrequest's slice of buffer begins.
The position recorded by a bvecq_pos is a tuple of { bvecq, slot, offset }. This is lighter weight than using a full iov_iter, though that would also suffice. If not NULL, the position also holds a reference on the bvecq it is pointing to.
For unbuffered/DIO reads and writes and various buffered reads including readahead, the iterator is extracted into the queue up front. For buffered writeback, the folios are added to the queue as the operation proceeds, much as it does now with folio_queues. For buffered reads and writes through the pagecache, bv_len is set to the length of the entire folio and then slices of the relevant[*] parts are passed to subrequests.
[*] What's relevant for writing to the cache isn't necessarily the same as
what's relevant for uploading to the server.
When setting up a subrequest, netfslib will furnish it with a slice of the main buffer queue as a bvecq_pos, and, for the moment, an ITER_BVECQ iterator is set to cover the slice in subreq->io_iter - though this will be moved down into the filesystem in a later patch.
Notes on the implementation:
(1) There are three positions on the netfs_io_request struct. Not all are used by every request type.
Firstly, there's ->load_cursor, which is used by buffered write and pgpriv2-copy to point to the next slot to have a folio inserted into it (e.g. when loading from writeback_iter()).
Secondly, there's ->dispatch_cursor, which is used to provide the position in the buffer from which we start dispatching a subrequest.
Thirdly, there's the ->collect_cursor, which is used by the collection routines to point to the next memory segment to be cleaned up.
(2) There's one position on the netfs_io_subrequest struct, ->io_buffer, that records the position from which a subrequest's buffer begins. This is also used as the base of the position from which to retry (advanced by ->transfer). For that reason, the filesystem is not allowed to change ->io_buffer for the moment.
(3) Maintenance of the positions is done with helper functions, such as bvecq_pos_attach(), to abstract the refcounting.
(4) When sending a read or a write to the cache, the ends of the slice will be rounded up/down to the backing file's DIO block alignment.
(5) bvecq_slice() is used to select a slice of the source buffer and assign it to a subrequest. The source buffer position is advanced.
(6) netfs_extract_iter() is used by unbuffered/direct I/O API functions to decant a chunk of the iov_iter supplied by the VFS into a bvecq chain
- and to label the bvecqs with appropriate disposal information (e.g. unpin, free, nothing).
There are further options that can be explored in the future:
(1) Allow the provision of a duplicated bvecq chain for a specific slice so that the filesystem can add bits on either end (such as adding protocol headers and trailers and gluing several things together into a compound operation).
(2) If a filesystem supports vectored/sparse read and write ops, it can be given a chain with discontiguities in it to perform in a single op (Ceph, for example, can do this).
(3) Make MSG_SPLICE_PAGES handling read the disposal info in the bvecq and use that to indicate how it should get rid of the stuff it pasted into a sk_buff.
(4) If a bounce buffer is needed (encryption, for example), the bounce buffer can be held in a bvecq and sliced up instead of the main buffer queue.
(5) Get rid of subreq->io_iter and move the iov_iter stuff down into the filesystem. The I/O iterators are normally only needed transitorily, and the one currently in netfs_io_subrequest is unnecessary most of the time.
rolling_buffer will be removed in a follow up patch.
--- fs/afs/dir.c | 5 +- fs/afs/symlink.c | 6 +- fs/cachefiles/io.c | 12 --- fs/netfs/Makefile | 1 - fs/netfs/buffered_read.c | 182 ++++++++++++++++++++--------------- fs/netfs/direct_read.c | 58 +++++------ fs/netfs/direct_write.c | 44 +++++---- fs/netfs/internal.h | 4 +- fs/netfs/iterator.c | 2 + fs/netfs/misc.c | 18 ---- fs/netfs/objects.c | 7 +- fs/netfs/read_collect.c | 91 +++++++++--------- fs/netfs/read_pgpriv2.c | 88 ++++++++++++----- fs/netfs/read_retry.c | 87 ++++++++++------- fs/netfs/read_single.c | 19 +++- fs/netfs/write_collect.c | 73 ++++++++------ fs/netfs/write_issue.c | 166 +++++++++++++++++++++----------- fs/netfs/write_retry.c | 73 ++++++++------ include/linux/netfs.h | 15 ++- include/trace/events/netfs.h | 32 +++++- 20 files changed, 585 insertions(+), 398 deletions(-)
fs/afs/dir.c +3 -2
@@ -2229,8 +2229,9 @@ static int afs_dir_writepages(struct address_space *mapping,if (test_bit (AFS_VNODE_DIR_VALID , &dvnode->flags)) {iov_iter_bvec_queue (&iter,ITER_SOURCE , dvnode->directory,0 ,0 , -i_size_read (&dvnode->netfs.inode)); - ret =netfs_writeback_single (mapping, wbc, &iter); + dvnode->directory_size); + ret =netfs_writeback_single (mapping, wbc, &iter, +i_size_read (&dvnode->netfs.inode));if (ret ==1 ) ret =0 ;/* Skipped write due to lock conflict. */ }
fs/afs/symlink.c +3 -3
@@ -248,9 +248,9 @@ int afs_symlink_writepages(struct address_space *mapping,if (vnode->directory &&atomic64_read (&vnode->cb_expires_at) !=AFS_NO_CB_PROMISE ) { -iov_iter_bvec_queue (&iter,ITER_SOURCE , vnode->directory,0 ,0 , -i_size_read (&vnode->netfs.inode)); - ret =netfs_writeback_single (mapping, wbc, &iter); +iov_iter_bvec_queue (&iter,ITER_SOURCE , vnode->directory,0 ,0 ,PAGE_SIZE ); + ret =netfs_writeback_single (mapping, wbc, &iter, +i_size_read (&vnode->netfs.inode)); }if (ret ==0 ) {
fs/cachefiles/io.c +0 -12
@@ -546,7 +546,6 @@ static void cachefiles_issue_write(struct netfs_io_subrequest *subreq)struct netfs_cache_resources *cres = &wreq->cache_resources;struct cachefiles_object *object =cachefiles_cres_object (cres);struct cachefiles_cache *cache = object->volume->cache; -struct netfs_io_stream *stream = &wreq->io_streams[subreq->stream_nr];const struct cred *saved_cred;size_t off, pre, post, len = subreq->len;uoff_t start = subreq->start;
@@ -571,17 +570,6 @@ static void cachefiles_issue_write(struct netfs_io_subrequest *subreq) }/* We also need to end on the cache granularity boundary */ -if (start + len == wreq->i_size) { -size_t part = len & (cache->bsize -1 ); -size_t need = cache->bsize - part; - -if (part && stream->submit_extendable_to >= need) { - len += need; - subreq->len += need; - subreq->io_iter.count += need; - } - } - post = len & (cache->bsize -1 );if (post) { len -= post;
fs/netfs/Makefile +0 -1
@@ -14,7 +14,6 @@ netfs-y := \ read_collect.o \ read_retry.o \ read_single.o \ - rolling_buffer.o \ write_collect.o \ write_issue.o \ write_retry.o
fs/netfs/buffered_read.c +105 -77
328 lines not on this page · raw message
fs/netfs/direct_read.c +22 -36
109 lines not on this page · raw message
fs/netfs/direct_write.c +27 -17
107 lines not on this page · raw message
fs/netfs/internal.h +1 -3
@@ -70,7 +70,6 @@ static inline void netfs_proc_del_rreq(struct netfs_io_request *rreq) {}/* * misc.c */ -void netfs_reset_iter (struct netfs_io_subrequest *subreq);void netfs_wake_collector (struct netfs_io_request *rreq);void netfs_subreq_clear_in_progress (struct netfs_io_subrequest *subreq); void netfs_wait_for_in_progress_stream(struct netfs_io_request *rreq,
@@ -239,8 +238,7 @@ void netfs_prepare_write(struct netfs_io_request *wreq,struct netfs_io_stream *stream, uoff_t start);void netfs_reissue_write (struct netfs_io_stream *stream, -struct netfs_io_subrequest *subreq, -struct iov_iter *source); +struct netfs_io_subrequest *subreq);void netfs_issue_write (struct netfs_io_request *wreq,struct netfs_io_stream *stream); size_t netfs_advance_write(struct netfs_io_request *wreq,
fs/netfs/iterator.c +2 -0
@@ -156,6 +156,7 @@ ssize_t netfs_extract_iter(struct iov_iter *orig, size_t max_len, size_t max_pag }EXPORT_SYMBOL_GPL (netfs_extract_iter); +#if 0 // TODO: Remove in subsequent patch /** * netfs_extract_user_iter - Extract the pages from a user iterator into a bvec * @orig: The original iterator
@@ -450,3 +451,4 @@ size_t netfs_limit_iter(const struct iov_iter *iter, size_t start_offset, BUG(); }EXPORT_SYMBOL (netfs_limit_iter); +#endif
fs/netfs/misc.c +0 -18
@@ -9,24 +9,6 @@#include <linux/rmap.h> #include "internal.h" -/* -* Reset the subrequest iterator to refer just to the region remaining to be -* read. The iterator may or may not have been advanced by socket ops or -* extraction ops to an extent that may or may not match the amount actually -* read. -*/ -void netfs_reset_iter (struct netfs_io_subrequest *subreq) -{ -struct iov_iter *io_iter = &subreq->io_iter; -size_t remain = subreq->len - subreq->transferred; - -if (io_iter->count > remain) -iov_iter_advance (io_iter, io_iter->count - remain); -else if (io_iter->count < remain) -iov_iter_revert (io_iter, remain - io_iter->count); -iov_iter_truncate (&subreq->io_iter, remain); -} -/** * netfs_dirty_folio - Mark folio dirty and pin a cache object for writeback * @mapping: The mapping the folio belongs to.
fs/netfs/objects.c +5 -2
@@ -147,8 +147,10 @@ static void netfs_deinit_request(struct netfs_io_request *rreq) rreq->netfs_ops->free_request (rreq);if (rreq->cache_resources.ops) rreq->cache_resources.ops->end_operation (&rreq->cache_resources); -bvecq_put (rreq->direct_bq); -rolling_buffer_clear (&rreq->buffer); +bvecq_pos_unset (&rreq->load_cursor); +bvecq_pos_unset (&rreq->dispatch_cursor); +bvecq_pos_unset (&rreq->collect_cursor); +bvecq_put (rreq->spare);if (atomic_dec_and_test (&ictx->io_count))wake_up_var (&ictx->io_count);
@@ -242,6 +244,7 @@ static void netfs_free_subrequest(struct netfs_io_subrequest *subreq)trace_netfs_sreq (subreq, netfs_sreq_trace_free);if (rreq->netfs_ops->free_subrequest) rreq->netfs_ops->free_subrequest (subreq); +bvecq_pos_unset (&subreq->io_buffer);mempool_free (subreq, rreq->netfs_ops->subrequest_pool ?: &netfs_subrequest_pool);netfs_stat_d (&netfs_n_rh_sreq);netfs_put_request (rreq, netfs_rreq_trace_put_subreq);
fs/netfs/read_collect.c +48 -43
166 lines not on this page · raw message
fs/netfs/read_pgpriv2.c +62 -26
157 lines not on this page · raw message
fs/netfs/read_retry.c +50 -37
199 lines not on this page · raw message
fs/netfs/read_single.c +17 -2
@@ -101,10 +101,17 @@ static int netfs_single_dispatch_read(struct netfs_io_request *rreq) subreq->start =0 ; subreq->len = rreq->len; - subreq->io_iter = rreq->buffer.iter; + +bvecq_pos_set (&subreq->io_buffer, &rreq->dispatch_cursor); + +iov_iter_bvec_queue (&subreq->io_iter,ITER_DEST , subreq->io_buffer.bvecq, + subreq->io_buffer.slot, subreq->io_buffer.offset, subreq->len);netfs_queue_read (rreq, subreq); + rreq->io_streams[0 ].sreq_max_len = subreq->len; + rreq->io_streams[0 ].sreq_max_segs =INT_MAX ; +switch (subreq->source) {case NETFS_DOWNLOAD_FROM_SERVER :netfs_stat (&netfs_n_rh_download);
@@ -174,6 +181,15 @@ ssize_t netfs_read_single(struct inode *inode, struct file *file, struct iov_iteif (IS_ERR (rreq))return PTR_ERR (rreq); + ret =netfs_extract_iter (iter, rreq->len,INT_MAX , &rreq->dispatch_cursor.bvecq, +0 , rreq->gfp); +if (ret <0 ) + goto cleanup_free; +if (ret < rreq->len) { + ret = -EIO ; + goto cleanup_free; + } + rreq->progress_at = rreq->len; ret =netfs_single_begin_cache_read (rreq, ictx);
@@ -183,7 +199,6 @@ ssize_t netfs_read_single(struct inode *inode, struct file *file, struct iov_itenetfs_stat (&netfs_n_rh_read_single);trace_netfs_read (rreq,0 , rreq->len, netfs_read_trace_read_single); - rreq->buffer.iter = *iter;netfs_single_dispatch_read (rreq); ret =netfs_wait_for_read (rreq);
fs/netfs/write_collect.c +44 -29
124 lines not on this page · raw message
fs/netfs/write_issue.c +112 -54
312 lines not on this page · raw message
fs/netfs/write_retry.c +45 -28
172 lines not on this page · raw message
include/linux/netfs.h +10 -5
@@ -19,10 +19,12 @@#include <linux/pagemap.h> #include <linux/bvecq.h> #include <linux/uio.h> -#include <linux/rolling_buffer.h> enum netfs_sreq_ref_trace ;typedef struct mempool mempool_t ; +struct readahead_control ; +struct netfs_io_request ; +struct netfs_io_subrequest ;struct fscache_occupancy ;/**
@@ -144,7 +146,6 @@ struct netfs_io_stream {unsigned int sreq_max_segs;/* 0 or max number of segments in an iterator */ unsigned int submit_off;/* Folio offset we're submitting from */ unsigned int submit_len;/* Amount of data left to submit */ -unsigned int submit_extendable_to;/* Amount I/O can be rounded up to */ void (*prepare_write)(struct netfs_io_subrequest *subreq);void (*issue_write)(struct netfs_io_subrequest *subreq);/* Collection tracking */
@@ -187,6 +188,7 @@ struct netfs_io_subrequest {struct netfs_io_request *rreq;/* Supervising I/O request */ struct work_struct work;struct list_head rreq_link;/* Link in rreq->subrequests */ +struct bvecq_pos io_buffer;/* Bookmark in the combined queue of the start */ struct iov_iter io_iter;/* Iterator for this subrequest */ uoff_t start;/* Where to start the I/O */ size_t len;/* Size of the I/O */
@@ -247,11 +249,14 @@ struct netfs_io_request {struct netfs_io_stream io_streams[2 ];/* Streams of parallel I/O operations */ #define NR_IO_STREAMS 2 //wreq->nr_io_streamsstruct netfs_group *group;/* Writeback group being written back */ -struct rolling_buffer buffer;/* Unencrypted buffer */ +struct bvecq *spare;/* Advance allocation of bvecq */ +struct bvecq_pos load_cursor;/* Point at which new folios are loaded in */ +struct bvecq_pos dispatch_cursor;/* Point from which buffers are dispatched */ +struct bvecq_pos collect_cursor;/* Clear-up point of I/O buffer */ wait_queue_head_t waitq;/* Processor waiter */ void *netfs_priv;/* Private data for the netfs */ void *netfs_priv2;/* Private data for the netfs */ -struct bvecq *direct_bq;/* DIO buffer list (when handling iovec-iter) */ +uoff_t last_end;/* End pos of last folio submitted */ uoff_t submitted;/* Amount submitted for I/O so far */ uoff_t len;/* Length of the request */ size_t transferred;/* Amount to be indicated as transferred */
@@ -425,7 +430,7 @@ void netfs_single_mark_inode_dirty(struct inode *inode);ssize_t netfs_read_single (struct inode *inode,struct file *file,struct iov_iter *iter);int netfs_writeback_single (struct address_space *mapping,struct writeback_control *wbc, -struct iov_iter *iter); +struct iov_iter *iter,size_t len);/* Address operations API */ struct readahead_control ;
include/trace/events/netfs.h +29 -3
@@ -229,7 +229,9 @@ EM(netfs_folio_trace_sched_copy, "sched-copy") \ EM(netfs_folio_trace_store, "store") \ EM(netfs_folio_trace_store_copy, "store-copy") \ - E_(netfs_folio_trace_store_plus, "store+") + EM(netfs_folio_trace_store_plus, "store+") \ + EM(netfs_folio_trace_zero, "zero") \ + E_(netfs_folio_trace_zero_ra, "zero-ra")#define netfs_collect_contig_traces \ EM(netfs_contig_trace_collect, "Collect") \
@@ -386,10 +388,10 @@ TRACE_EVENT(netfs_sreq, __entry->len = sreq->len; __entry->transferred = sreq->transferred; __entry->start = sreq->start; - __entry->slot = sreq->io_iter.bvecq_slot; + __entry->slot = sreq->io_buffer.slot; ), - TP_printk("R=%08x[%x] %s %s f=%03x s=%llx %zx/%zx s=%u e=%d", + TP_printk("R=%08x[%x] %s %s f=%03x s=%llx %zx/%zx bv=%u e=%d", __entry->rreq, __entry->index, __print_symbolic(__entry->source, netfs_sreq_sources), __print_symbolic(__entry->what, netfs_sreq_traces),
@@ -782,6 +784,30 @@ TRACE_EVENT(netfs_read_progress_at, __entry->rreq, __entry->cleaned_to, __entry->progress_at) ); +TRACE_EVENT (netfs_bv_slot, + TP_PROTO(const struct bvecq *bq, int slot), + +TP_ARGS (bq, slot), + +TP_STRUCT__entry ( + __field(unsigned long, pfn) + __field(unsigned int, offset) + __field(unsigned int, len) + __field(unsigned int, slot) + ), + +TP_fast_assign ( + __entry->slot = slot; + __entry->pfn = page_to_pfn(bq->bv[slot].bv_page); + __entry->offset = bq->bv[slot].bv_offset; + __entry->len = bq->bv[slot].bv_len; + ), + +TP_printk ("bq[%x] p=%lx %x-%x" , + __entry->slot, + __entry->pfn, __entry->offset, __entry->offset + __entry->len) + ); +#undef EM#undef E_#endif /* _TRACE_NETFS_H */
David Howells
maintainerPATCH v17 7/8 netfs: Remove the rolling_buffer implementation
In reply to David HowellsRemove the rolling_buffer implementation as it has been replaced with the use of bvecq_pos.
--- fs/netfs/rolling_buffer.c | 182 --------------------------------- include/linux/rolling_buffer.h | 47 --------- 2 files changed, 229 deletions(-) delete mode 100644 fs/netfs/rolling_buffer.c delete mode 100644 include/linux/rolling_buffer.h
fs/netfs/rolling_buffer.c deleted +0 -182
@@ -1,182 +0,0 @@ -// SPDX-License-Identifier: GPL-2.0-or-later -/* Rolling buffer helpers -* -* Copyright (C) 2024 Red Hat, Inc. All Rights Reserved. -* Written by David Howells ([email protected]) -*/ - -#include <linux/bitops.h> -#include <linux/mempool.h> -#include <linux/pagemap.h> -#include <linux/rolling_buffer.h> -#include <linux/slab.h> -#include "internal.h" - -/* -* Initialise a rolling buffer. We allocate an empty folio queue struct to so -* that the pointers can be independently driven by the producer and the -* consumer. -*/ -int rolling_buffer_init (struct rolling_buffer *roll,unsigned int direction, -gfp_t gfp,bool for_writeback) -{ -struct bvecq *bq; - - roll->for_writeback = for_writeback; - - bq =bvecq_alloc_one (BVECQ_POOL_SLOTS , gfp, for_writeback); -if (!bq) -return -ENOMEM ; - - roll->head = bq; - roll->tail = bq; -iov_iter_bvec_queue (&roll->iter, direction, bq,0 ,0 ,0 ); -return 0 ; -} - -/* -* Add another bvecq to a rolling buffer if there's no space left. -*/ -int rolling_buffer_make_space (struct rolling_buffer *roll,gfp_t gfp) -{ -struct bvecq *bq, *head = roll->head; - -if (!bvecq_is_full (head)) -return 0 ; - - bq =bvecq_alloc_one (BVECQ_POOL_SLOTS , gfp, roll->for_writeback); -if (!bq) -return -ENOMEM ; - - roll->head = bq; -if (bvecq_is_full (head)) { -/* Make sure we don't leave the master iterator pointing to a -* block that might get immediately consumed. -*/ -if (roll->iter.bvecq == head && - roll->iter.bvecq_slot == head->nr_slots) { - roll->iter.bvecq = bq; - roll->iter.bvecq_slot =0 ; - } - } - -/* Make sure the initialisation is stored before the next pointer. -* -* [!] NOTE: After we set head->next, the consumer is at liberty to -* immediately delete the old head. -*/ -bvecq_append (head, bq); -return 0 ; -} - -/* -* Decant the entire list of folios to read into a rolling buffer. -*/ -ssize_t rolling_buffer_bulk_load_from_ra (struct rolling_buffer *roll, -struct readahead_control *ractl, -gfp_t gfp) -{ -struct bvecq *bq; -size_t loaded =0 ; - -while (ractl->_nr_pages - ractl->_batch_count >0 ) { -struct page **pages; -unsigned int nr; - -/* Allocate a bvecq to put some folios into and attach it to -* the rolling buffer. -*/ - bq =bvecq_alloc_one (BVECQ_POOL_SLOTS , gfp, false); -if (!bq) - goto nomem_unlock; - bq->mem_type =BVECQ_MEM_EXTERNAL ;/* Folio cleanup handled separately. */ - -if (!roll->tail) - roll->tail = bq; -else -bvecq_append (roll->head, bq); - roll->head = bq; - -/* Get a bunch of folios and note their sizes. */ - pages = (struct page **)(bq->bv + bq->max_slots); - pages -= bq->max_slots; - nr =__readahead_batch (ractl, pages, bq->max_slots); -if (WARN_ON_ONCE (!nr)) -break ; - -for (int slot =0 ; slot < nr; slot++) { -struct folio *folio =page_folio (pages[slot]); -size_t len =folio_size (folio); - -bvec_set_folio (&bq->bv[slot], folio, len,0 ); - loaded += len; -trace_netfs_folio (folio, netfs_folio_trace_read); - } - -bvecq_filled_to (bq, nr); - } - -WRITE_ONCE (roll->iter.count, loaded); -iov_iter_bvec_queue (&roll->iter,ITER_DEST , roll->tail,0 ,0 , loaded); -return loaded; - -nomem_unlock: -for (bq = roll->tail; bq; bq = bq->next) { -for (int slot =0 ; slot < bq->nr_slots; slot++) { -struct folio *folio =bvec_folio (&bq->bv[slot]); - -folio_unlock (folio); -folio_put (folio); - } - } -rolling_buffer_clear (roll); - roll->head =NULL ; - roll->tail =NULL ; -return -ENOMEM ; -} - -/* -* Append a folio to the rolling buffer. -*/ -ssize_t rolling_buffer_append (struct rolling_buffer *roll,struct folio *folio, -gfp_t gfp) -{ -ssize_t size =folio_size (folio); -int slot; - -if (rolling_buffer_make_space (roll, gfp) <0 ) -return -ENOMEM ; - - slot = roll->head->nr_slots; -bvec_set_folio (&roll->head->bv[slot], folio, size,0 ); -bvecq_filled_to (roll->head, slot +1 ); - -WRITE_ONCE (roll->iter.count, roll->iter.count + size); -return size; -} - -/* -* Delete a spent buffer from a rolling queue and return the next in line. We -* don't return the last buffer to keep the pointers independent, but return -* NULL instead. -*/ -struct bvecq *rolling_buffer_delete_spent (struct rolling_buffer *roll) -{ -struct bvecq *spent = roll->tail, *next =bvecq_next (spent); - -if (!next) -return NULL ; - next->prev =NULL ; - roll->tail = next; - spent->next =NULL ; -bvecq_put (spent); -return next; -} - -/* -* Clear out a rolling queue. -*/ -void rolling_buffer_clear (struct rolling_buffer *roll) -{ -bvecq_put (roll->tail); -}
include/linux/rolling_buffer.h deleted +0 -47
47 lines not on this page · raw message
David Howells
maintainerPATCH v17 8/8 netfs: Remove netfs_extract_user_iter()
In reply to David HowellsRemove netfs_extract_user_iter() as it has been replaced with netfs_extract_iter().
--- fs/netfs/iterator.c | 297 ------------------------------------------ include/linux/netfs.h | 5 - 2 files changed, 302 deletions(-)
fs/netfs/iterator.c +0 -297
@@ -155,300 +155,3 @@ ssize_t netfs_extract_iter(struct iov_iter *orig, size_t max_len, size_t max_pag }EXPORT_SYMBOL_GPL (netfs_extract_iter); - -#if 0 // TODO: Remove in subsequent patch -/** -* netfs_extract_user_iter - Extract the pages from a user iterator into a bvec -* @orig: The original iterator -* @orig_len: The amount of iterator to copy -* @new: The iterator to be set up -* @extraction_flags: Flags to qualify the request -* -* Extract the page fragments from the given amount of the source iterator and -* build up a second iterator that refers to all of those bits. This allows -* the original iterator to be disposed of. -* -* @extraction_flags can have ITER_ALLOW_P2PDMA set to request peer-to-peer DMA be -* allowed on the pages extracted. -* -* On success, the number of elements in the bvec is returned, the original -* iterator will have been advanced by the amount extracted. -* -* The iov_iter_extract_mode() function should be used to query how cleanup -* should be performed. -*/ -ssize_t netfs_extract_user_iter (struct iov_iter *orig,size_t orig_len, -struct iov_iter *new, -iov_iter_extraction_t extraction_flags) -{ -struct bio_vec *bv =NULL ; -struct page **pages; -unsigned int cur_npages; -unsigned int max_pages; -unsigned int npages =0 ; -unsigned int i; -ssize_t ret =0 ; -size_t count = orig_len, offset, len; -size_t bv_size, pg_size; - -if (WARN_ON_ONCE (!iter_is_ubuf (orig) && !iter_is_iovec (orig))) -return -EIO ; - - max_pages =iov_iter_npages (orig,INT_MAX ); - bv_size =array_size (max_pages,sizeof (*bv)); - bv =kvmalloc (bv_size,GFP_KERNEL ); -if (!bv) -return -ENOMEM ; - -/* Put the page list at the end of the bvec list storage. bvec -* elements are larger than page pointers, so as long as we work -* 0->last, we should be fine. -*/ - pg_size =array_size (max_pages,sizeof (*pages)); - pages = (void *)bv + bv_size - pg_size; - -while (count && npages < max_pages) { - ret =iov_iter_extract_pages (orig, &pages, count, - max_pages - npages, extraction_flags, - &offset); -if (unlikely (ret <=0 )) { - ret = ret ?: -EIO ; -break ; - } - -if (WARN (ret > count, -"%s: extract_pages overrun %zd > %zu bytes\n" , - __func__, ret, count)) { - ret = -EIO ; -break ; - } - - cur_npages =DIV_ROUND_UP (offset + ret,PAGE_SIZE ); -if (WARN (cur_npages > max_pages - npages, -"%s: extract_pages overrun %u > %u pages\n" , - __func__, npages + cur_npages, max_pages)) { - ret = -EIO ; -break ; - } - - count -= ret; - ret += offset; - -for (i =0 ; i < cur_npages; i++) { - len = ret >PAGE_SIZE ?PAGE_SIZE : ret; -bvec_set_page (bv + npages + i, *pages++, len - offset, offset); - ret -= len; - offset =0 ; - } - - npages += cur_npages; - } - -/* Note: Don't try to clean up after EIO. Either we got no pages, so -* nothing to clean up, or we got a buffer overrun, memory corruption -* and can't trust the stuff in the buffer (a WARN was emitted). -*/ - -if (ret <0 && (ret == -ENOMEM || npages ==0 )) { -for (i =0 ; i < npages; i++) -unpin_user_page (bv[i].bv_page); -kvfree (bv); -return ret; - } - -iov_iter_bvec (new, orig->data_source, bv, npages, orig_len - count); -return npages; -} -EXPORT_SYMBOL_GPL (netfs_extract_user_iter); - -/* -* Select the span of a bvec iterator we're going to use. Limit it by both maximum -* size and maximum number of segments. Returns the size of the span in bytes. -*/ -static size_t netfs_limit_bvec (const struct iov_iter *iter,size_t start_offset, -size_t max_size,size_t max_segs) -{ -const struct bio_vec *bvecs = iter->bvec; -unsigned int nbv = iter->nr_segs, ix =0 , nsegs =0 ; -size_t len, span =0 , n = iter->count; -size_t skip = iter->iov_offset + start_offset; - -if (WARN_ON (!iov_iter_is_bvec (iter)) || -WARN_ON (start_offset > n) || - n ==0 ) -return 0 ; - -while (n && ix < nbv && skip) { - len = bvecs[ix].bv_len; -if (skip < len) -break ; - skip -= len; - n -= len; - ix++; - } - -while (n && ix < nbv) { - len =min3 (n, bvecs[ix].bv_len - skip, max_size); - span += len; - nsegs++; - ix++; -if (span >= max_size || nsegs >= max_segs) -break ; - skip =0 ; - n -= len; - } - -return min (span, max_size); -} - -/* -* Select the span of a kvec iterator we're going to use. Limit it by both -* maximum size and maximum number of segments. Returns the size of the span -* in bytes. -*/ -static size_t netfs_limit_kvec (const struct iov_iter *iter,size_t start_offset, -size_t max_size,size_t max_segs) -{ -const struct kvec *kvecs = iter->kvec; -unsigned int nkv = iter->nr_segs, ix =0 , nsegs =0 ; -size_t len, span =0 , n = iter->count; -size_t skip = iter->iov_offset + start_offset; - -if (WARN_ON (!iov_iter_is_kvec (iter)) || -WARN_ON (start_offset > n) || - n ==0 ) -return 0 ; - -while (n && ix < nkv && skip) { - len = kvecs[ix].iov_len; -if (skip < len) -break ; - skip -= len; - n -= len; - ix++; - } - -while (n && ix < nkv) { - len =min3 (n, kvecs[ix].iov_len - skip, max_size); - span += len; - nsegs++; - ix++; -if (span >= max_size || nsegs >= max_segs) -break ; - skip =0 ; - n -= len; - } - -return min (span, max_size); -} - -/* -* Select the span of an xarray iterator we're going to use. Limit it by both -* maximum size and maximum number of segments. It is assumed that segments -* can be larger than a page in size, provided they're physically contiguous. -* Returns the size of the span in bytes. -*/ -static size_t netfs_limit_xarray (const struct iov_iter *iter,size_t start_offset, -size_t max_size,size_t max_segs) -{ -struct folio *folio; -unsigned int nsegs =0 ; -uoff_t pos = iter->xarray_start + iter->iov_offset; -pgoff_t index = pos /PAGE_SIZE ; -size_t span =0 , n = iter->count; - -XA_STATE (xas, iter->xarray, index); - -if (WARN_ON (!iov_iter_is_xarray (iter)) || -WARN_ON (start_offset > n) || - n ==0 ) -return 0 ; - max_size =min (max_size, n - start_offset); - -rcu_read_lock (); -xas_for_each (&xas, folio,ULONG_MAX ) { -size_t offset, flen, len; -if (xas_retry (&xas, folio)) -continue ; -if (WARN_ON (xa_is_value (folio))) -break ; -if (WARN_ON (folio_test_hugetlb (folio))) -break ; - - flen =folio_size (folio); - offset =offset_in_folio (folio, pos); - len =min (max_size, flen - offset); - span += len; - nsegs++; -if (span >= max_size || nsegs >= max_segs) -break ; - } - -rcu_read_unlock (); -return min (span, max_size); -} - -/* -* Select the span of a bvecq iterator we're going to use. Limit it by both -* maximum size and maximum number of segments. Returns the size of the span -* in bytes. -*/ -static size_t netfs_limit_bvecq (const struct iov_iter *iter,size_t start_offset, -size_t max_size,size_t max_segs) -{ -const struct bvecq *bq = iter->bvecq; -unsigned int nsegs =0 ; -unsigned int slot = iter->bvecq_slot; -size_t span =0 , n = iter->count; - -if (WARN_ON (!iov_iter_is_bvecq (iter)) || -WARN_ON (start_offset > n) || - n ==0 ) -return 0 ; - max_size =umin (max_size, n - start_offset); - -if (!bvecq_acquire_slot (bq, slot)) { - bq =bvecq_next (bq); - slot =0 ; - } - - start_offset += iter->iov_offset; -do { -size_t flen; - - flen = bq->bv[slot].bv_len; -if (start_offset < flen) { - span += flen - start_offset; - nsegs++; - start_offset =0 ; - }else { - start_offset -= flen; - } -if (span >= max_size || nsegs >= max_segs) -break ; - - slot++; -if (!bvecq_acquire_slot (bq, slot)) { - bq =bvecq_next (bq); - slot =0 ; - } - }while (bq); - -return umin (span, max_size); -} - -size_t netfs_limit_iter (const struct iov_iter *iter,size_t start_offset, -size_t max_size,size_t max_segs) -{ -if (iov_iter_is_bvecq (iter)) -return netfs_limit_bvecq (iter, start_offset, max_size, max_segs); -if (iov_iter_is_bvec (iter)) -return netfs_limit_bvec (iter, start_offset, max_size, max_segs); -if (iov_iter_is_xarray (iter)) -return netfs_limit_xarray (iter, start_offset, max_size, max_segs); -if (iov_iter_is_kvec (iter)) -return netfs_limit_kvec (iter, start_offset, max_size, max_segs); -BUG (); -} -EXPORT_SYMBOL (netfs_limit_iter); -#endif
include/linux/netfs.h +0 -5
@@ -460,11 +460,6 @@ void netfs_put_subrequest(struct netfs_io_subrequest *subreq,ssize_t netfs_extract_iter (struct iov_iter *orig,size_t max_len,size_t max_pages,struct bvecq **_bvecq_head,iov_iter_extraction_t extraction_flags,gfp_t gfp); -ssize_t netfs_extract_user_iter (struct iov_iter *orig,size_t orig_len, -struct iov_iter *new, -iov_iter_extraction_t extraction_flags); -size_t netfs_limit_iter (const struct iov_iter *iter,size_t start_offset, -size_t max_size,size_t max_segs);void netfs_prepare_write_failed (struct netfs_io_subrequest *subreq);void netfs_write_subrequest_terminated (void *_op,ssize_t transferred_or_error);