netfs: Miscellaneous fixes
David Howells
maintainercover netfs: Miscellaneous fixes
Hi Christian,
Here are some miscellaneous fixes for netfslib.
(1) Clear PG_private_2 on copy-to-cache append failure.
(2) Fix handling of rolling buffer allocation failure in single-object writeback. This is probably unnecessary with (4), but if we're only writing to the cache, we can skip the write.
(3) Fix cleanup of readeahead folios if iterator preparation fails.
(4) Fix folio_queue allocation failure in writeback by adding a mempool. This also improves request and subrequest allocation.
The patches can also be found here:
https://git.kernel.org/pub/scm/linux/kernel/git/dhowells/linux-fs.git/log/?h=netfs-fixes
Thanks,
David
David Howells (1):
netfs: Fix folio_queue ENOMEM in writeback by adding a mempool
Yichong Chen (3):
netfs: clear PG_private_2 on copy-to-cache append failure
netfs: handle single writeback rolling buffer allocation failure
netfs: release readahead folios on iterator preparation failure
fs/netfs/buffered_read.c | 10 ++++++---- fs/netfs/internal.h | 1 + fs/netfs/main.c | 7 +++++++ fs/netfs/objects.c | 31 +++++++++++++++++++------------ fs/netfs/read_pgpriv2.c | 3 ++- fs/netfs/rolling_buffer.c | 21 ++++++++++++--------- fs/netfs/write_issue.c | 15 ++++++++++----- include/linux/netfs.h | 1 + include/linux/rolling_buffer.h | 6 +++--- 9 files changed, 61 insertions(+), 34 deletions(-)
David Howells
maintainerPATCH 1/4 netfs: clear PG_private_2 on copy-to-cache append failure
From: Yichong Chen <[email protected]>
netfs_pgpriv2_copy_to_cache() marks the folio with PG_private_2 before netfs_pgpriv2_copy_folio() appends it to the copy-to-cache rolling buffer.
If the append fails, the folio is not queued for cache writeback, so the PG_private_2 state and its reference must be released immediately.
--- fs/netfs/read_pgpriv2.c | 1 + 1 file changed, 1 insertion(+)
fs/netfs/read_pgpriv2.c +1 -0
@@ -54,6 +54,7 @@ static void netfs_pgpriv2_copy_folio(struct netfs_io_request *creq, struct folio/* Attach the folio to the rolling buffer. */ if (rolling_buffer_append (&creq->buffer, folio,0 ) <0 ) { +folio_end_private_2 (folio);clear_bit (NETFS_RREQ_FOLIO_COPY_TO_CACHE , &creq->flags);return ; }
Re: [PATCH 1/4] netfs: clear PG_private_2 on copy-to-cache append failure
Quoted 6 lines · David Howells
/* Attach the folio to the rolling buffer. */
if (rolling_buffer_append(&creq->buffer, folio, 0) < 0) {
+ folio_end_private_2(folio);
clear_bit(NETFS_RREQ_FOLIO_COPY_TO_CACHE, &creq->flags);
return;
}David, I submitted the same patch 4 months ago already, why didn't you merge that?
https://lore.kernel.org/netfs/[email protected]/
The "Fixes" tag in Yuchon Chen's patch is incorrect. Commit e2d46f2ec332 ("netfs: Change the read result collector to only use one work item") does not introduce the bug. The bug has existed ever since netfs_pgpriv2_copy_folio() was initially added by commit ee4cdf7ba857 ("netfs: Speed up buffered reading"), as my older patch states in its "Fixes" tag.
Signature · 8 lines
Max Kellermann Principal Architect Hosting Technology cm4all | Im Mediapark 6a | 50670 Köln | Germany General information about the company can be found here: https://www.cm4all.com/impressum A member of the IONOS Group
David Howells
maintainerPATCH 2/4 netfs: handle single writeback rolling buffer allocation failure
In reply to David HowellsFrom: Yichong Chen <[email protected]>
netfs_write_folio_single() takes an extra folio reference before appending the folio to the rolling buffer.
rolling_buffer_append() can fail if it cannot allocate another folio_queue. Check the return value and drop the extra folio reference before returning the error.
--- fs/netfs/write_issue.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
fs/netfs/write_issue.c +6 -1
@@ -720,6 +720,7 @@ static int netfs_write_folio_single(struct netfs_io_request *wreq,size_t iter_off =0 ;size_t fsize =folio_size (folio), flen;loff_t fpos =folio_pos (folio); +ssize_t ret;bool to_eof = false;bool no_debug = false;
@@ -748,7 +749,11 @@ static int netfs_write_folio_single(struct netfs_io_request *wreq,/* Attach the folio to the rolling buffer. */ folio_get (folio); -rolling_buffer_append (&wreq->buffer, folio,NETFS_ROLLBUF_PUT_MARK ); + ret =rolling_buffer_append (&wreq->buffer, folio,NETFS_ROLLBUF_PUT_MARK ); +if (ret <0 ) { +folio_put (folio); +return ret; + }/* Move the submission point forward to allow for write-streaming data * not starting at the front of the page. We don't do write-streaming
David Howells
maintainerPATCH 3/4 netfs: release readahead folios on iterator preparation failure
In reply to David HowellsFrom: Yichong Chen <[email protected]>
netfs_prepare_read_iterator() batches readahead folios in put_batch so that the folio references can be dropped after the I/O iterator has been prepared.
If rolling_buffer_load_from_ra() fails after earlier folios have been batched, the function returns immediately and leaves those references held. Release the batch before returning the error.
--- fs/netfs/buffered_read.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
fs/netfs/buffered_read.c +3 -1
@@ -102,8 +102,10 @@ static ssize_t netfs_prepare_read_iterator(struct netfs_io_subrequest *subreq, added =rolling_buffer_load_from_ra (&rreq->buffer, ractl, &put_batch); -if (added <0 ) +if (added <0 ) { +folio_batch_release (&put_batch);return added; + } rreq->submitted += added; }folio_batch_release (&put_batch);
David Howells
maintainerPATCH 4/4 netfs: Fix folio_queue ENOMEM in writeback by adding a mempool
In reply to David HowellsFix the handling of folio_queue allocation failure in writeback by adding a mempool and passing in gfp_t flags to the rolling buffer functions that allocate memory, using the mempool if gfp != GFP_KERNEL.
This is then extended upwards and the gfp to be used for a request is stored in the netfs_io_request struct and is then used for both requests and subrequests, eliminating the sleeping loops there.
The failure caused:
folio != NULL
WARNING: fs/netfs/write_issue.c:603 at netfs_writepages+0x883/0xa10 fs/netfs/write_issue.c:603, CPU#3: syz.0.17/5919--- fs/netfs/buffered_read.c | 6 +++--- fs/netfs/internal.h | 1 + fs/netfs/main.c | 7 +++++++ fs/netfs/objects.c | 31 +++++++++++++++++++------------ fs/netfs/read_pgpriv2.c | 2 +- fs/netfs/rolling_buffer.c | 21 ++++++++++++--------- fs/netfs/write_issue.c | 10 +++++----- include/linux/netfs.h | 1 + include/linux/rolling_buffer.h | 6 +++--- 9 files changed, 52 insertions(+), 33 deletions(-)
fs/netfs/buffered_read.c +3 -3
@@ -361,7 +361,7 @@ void netfs_readahead(struct readahead_control *ractl)netfs_rreq_expand (rreq, ractl); rreq->submitted = rreq->start; -if (rolling_buffer_init (&rreq->buffer, rreq->debug_id,ITER_DEST ) <0 ) +if (rolling_buffer_init (&rreq->buffer, rreq->debug_id,ITER_DEST , rreq->gfp) <0 ) goto cleanup_free;netfs_read_to_pagecache (rreq, ractl);
@@ -380,10 +380,10 @@ static int netfs_create_singular_buffer(struct netfs_io_request *rreq, struct fo {ssize_t added; -if (rolling_buffer_init (&rreq->buffer, rreq->debug_id,ITER_DEST ) <0 ) +if (rolling_buffer_init (&rreq->buffer, rreq->debug_id,ITER_DEST , rreq->gfp) <0 )return -ENOMEM ; - added =rolling_buffer_append (&rreq->buffer, folio, rollbuf_flags); + added =rolling_buffer_append (&rreq->buffer, folio, rollbuf_flags, rreq->gfp);if (added <0 )return added; rreq->submitted = rreq->start + added;
fs/netfs/internal.h +1 -0
@@ -43,6 +43,7 @@ extern struct list_head netfs_io_requests;extern spinlock_t netfs_proc_lock;extern mempool_t netfs_request_pool;extern mempool_t netfs_subrequest_pool; +extern mempool_t netfs_folioq_pool;#ifdef CONFIG_PROC_FS static inline void netfs_proc_add_rreq (struct netfs_io_request *rreq)
fs/netfs/main.c +7 -0
@@ -28,6 +28,7 @@ static struct kmem_cache *netfs_request_slab;static struct kmem_cache *netfs_subrequest_slab;mempool_t netfs_request_pool;mempool_t netfs_subrequest_pool; +mempool_t netfs_folioq_pool;#ifdef CONFIG_PROC_FS LIST_HEAD (netfs_io_requests);
@@ -108,6 +109,9 @@ static int __init netfs_init(void) {int ret = -ENOMEM ; +if (mempool_init_kmalloc_pool (&netfs_folioq_pool,100 ,sizeof (struct folio_queue )) <0 ) + goto error_folioq_pool; + netfs_request_slab = kmem_cache_create("netfs_request", sizeof(struct netfs_io_request), 0, SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT,
@@ -160,6 +164,8 @@ static int __init netfs_init(void) error_reqpool:kmem_cache_destroy (netfs_request_slab); error_req: +mempool_exit (&netfs_folioq_pool); +error_folioq_pool:return ret; }fs_initcall (netfs_init);
@@ -172,5 +178,6 @@ static void __exit netfs_exit(void)kmem_cache_destroy (netfs_subrequest_slab);mempool_exit (&netfs_request_pool);kmem_cache_destroy (netfs_request_slab); +mempool_exit (&netfs_folioq_pool); }module_exit (netfs_exit);
fs/netfs/objects.c +19 -12
@@ -7,7 +7,6 @@#include <linux/slab.h> #include <linux/mempool.h> -#include <linux/delay.h> #include "internal.h" static void netfs_free_request (struct work_struct *work);
@@ -26,17 +25,23 @@ struct netfs_io_request *netfs_alloc_request(struct address_space *mapping,struct netfs_io_request *rreq;mempool_t *mempool = ctx->ops->request_pool ?: &netfs_request_pool;struct kmem_cache *cache = mempool->pool_data; +gfp_t gfp =GFP_KERNEL ;int ret; -for (;;) { - rreq =mempool_alloc (mempool,GFP_KERNEL ); -if (rreq) -break ; -msleep (10 ); +/* Writeback is part of memory reclaim and must not fail due to ENOMEM. */ +if (origin ==NETFS_WRITEBACK || origin ==NETFS_WRITEBACK_SINGLE ) { + gfp =GFP_NOFS ;/* Allows use of mempools. */ + + rreq =mempool_alloc (mempool, gfp); + }else { + rreq = mempool->alloc (gfp, mempool->pool_data); +if (!rreq) +return ERR_PTR (-ENOMEM ); }memset (rreq,0 ,kmem_cache_size (cache));INIT_WORK (&rreq->cleanup_work, netfs_free_request); + rreq->gfp = gfp; rreq->start = start; rreq->len = len; rreq->origin = origin;
@@ -200,12 +205,14 @@ struct netfs_io_subrequest *netfs_alloc_subrequest(struct netfs_io_request *rreqmempool_t *mempool = rreq->netfs_ops->subrequest_pool ?: &netfs_subrequest_pool;struct kmem_cache *cache = mempool->pool_data; -for (;;) { - subreq =mempool_alloc (rreq->netfs_ops->subrequest_pool ?: &netfs_subrequest_pool, -GFP_KERNEL ); -if (subreq) -break ; -msleep (10 ); +if (rreq->gfp ==GFP_KERNEL ) { + subreq = mempool->alloc (rreq->gfp, mempool->pool_data); +if (!subreq) +return ERR_PTR (-ENOMEM ); + }else { + subreq =mempool_alloc (mempool, rreq->gfp); +if (!subreq) +return NULL ; }memset (subreq,0 ,kmem_cache_size (cache));
fs/netfs/read_pgpriv2.c +1 -1
@@ -53,7 +53,7 @@ static void netfs_pgpriv2_copy_folio(struct netfs_io_request *creq, struct foliotrace_netfs_folio (folio, netfs_folio_trace_store_copy);/* Attach the folio to the rolling buffer. */ -if (rolling_buffer_append (&creq->buffer, folio,0 ) <0 ) { +if (rolling_buffer_append (&creq->buffer, folio,0 , creq->gfp) <0 ) {folio_end_private_2 (folio);clear_bit (NETFS_RREQ_FOLIO_COPY_TO_CACHE , &creq->flags);return ;
fs/netfs/rolling_buffer.c +12 -9
@@ -27,7 +27,10 @@ struct folio_queue *netfs_folioq_alloc(unsigned int rreq_id, gfp_t gfp, {struct folio_queue *fq; - fq =kmalloc_obj (*fq, gfp); +if (gfp ==GFP_KERNEL ) + fq = netfs_folioq_pool.alloc (gfp, netfs_folioq_pool.pool_data); +else + fq =mempool_alloc (&netfs_folioq_pool, gfp);if (fq) {netfs_stat (&netfs_n_folioq);folioq_init (fq, rreq_id);
@@ -50,7 +53,7 @@ void netfs_folioq_free(struct folio_queue *folioq, {trace_netfs_folioq (folioq, trace);netfs_stat_d (&netfs_n_folioq); -kfree (folioq); +mempool_free (folioq, &netfs_folioq_pool); }EXPORT_SYMBOL (netfs_folioq_free);
@@ -60,11 +63,11 @@ EXPORT_SYMBOL(netfs_folioq_free);* consumer. */ int rolling_buffer_init (struct rolling_buffer *roll,unsigned int rreq_id, -unsigned int direction) +unsigned int direction,gfp_t gfp) {struct folio_queue *fq; - fq =netfs_folioq_alloc (rreq_id,GFP_NOFS , netfs_trace_folioq_rollbuf_init); + fq =netfs_folioq_alloc (rreq_id, gfp, netfs_trace_folioq_rollbuf_init);if (!fq)return -ENOMEM ;
@@ -77,14 +80,14 @@ int rolling_buffer_init(struct rolling_buffer *roll, unsigned int rreq_id,/* * Add another folio_queue to a rolling buffer if there's no space left. */ -int rolling_buffer_make_space (struct rolling_buffer *roll) +int rolling_buffer_make_space (struct rolling_buffer *roll,gfp_t gfp) {struct folio_queue *fq, *head = roll->head;if (!folioq_full (head))return 0 ; - fq =netfs_folioq_alloc (head->rreq_id,GFP_NOFS , netfs_trace_folioq_make_space); + fq =netfs_folioq_alloc (head->rreq_id, gfp, netfs_trace_folioq_make_space);if (!fq)return -ENOMEM ; fq->prev = head;
@@ -122,7 +125,7 @@ ssize_t rolling_buffer_load_from_ra(struct rolling_buffer *roll,int nr, ix, to;ssize_t size =0 ; -if (rolling_buffer_make_space (roll) <0 ) +if (rolling_buffer_make_space (roll,GFP_KERNEL ) <0 )return -ENOMEM ; fq = roll->head;
@@ -153,12 +156,12 @@ ssize_t rolling_buffer_load_from_ra(struct rolling_buffer *roll,* Append a folio to the rolling buffer. */ ssize_t rolling_buffer_append (struct rolling_buffer *roll,struct folio *folio, -unsigned int flags) +unsigned int flags,gfp_t gfp) {ssize_t size =folio_size (folio);int slot; -if (rolling_buffer_make_space (roll) <0 ) +if (rolling_buffer_make_space (roll, gfp) <0 )return -ENOMEM ; slot =folioq_append (roll->head, folio);
fs/netfs/write_issue.c +5 -5
@@ -108,7 +108,7 @@ struct netfs_io_request *netfs_create_write_req(struct address_space *mapping, ictx =netfs_inode (wreq->inode);if (is_cacheable)fscache_begin_write_operation (&wreq->cache_resources,netfs_i_cookie (ictx)); -if (rolling_buffer_init (&wreq->buffer, wreq->debug_id,ITER_SOURCE ) <0 ) +if (rolling_buffer_init (&wreq->buffer, wreq->debug_id,ITER_SOURCE , wreq->gfp) <0 ) goto nomem; wreq->cleaned_to = wreq->start;
@@ -167,7 +167,7 @@ void netfs_prepare_write(struct netfs_io_request *wreq,*/ if (iov_iter_is_folioq (wreq_iter) && wreq_iter->folioq_slot >=folioq_nr_slots (wreq_iter->folioq)) -rolling_buffer_make_space (&wreq->buffer); +rolling_buffer_make_space (&wreq->buffer, wreq->gfp); subreq =netfs_alloc_subrequest (wreq); subreq->source = stream->source;
@@ -334,7 +334,7 @@ static int netfs_write_folio(struct netfs_io_request *wreq,_enter ("" ); -if (rolling_buffer_make_space (&wreq->buffer) <0 ) +if (rolling_buffer_make_space (&wreq->buffer, wreq->gfp) <0 )return -ENOMEM ;/* netfs_perform_write() may shift i_size around the page or from out
@@ -436,7 +436,7 @@ static int netfs_write_folio(struct netfs_io_request *wreq, }/* Attach the folio to the rolling buffer. */ -rolling_buffer_append (&wreq->buffer, folio,0 ); +rolling_buffer_append (&wreq->buffer, folio,0 , wreq->gfp);/* Move the submission point forward to allow for write-streaming data * not starting at the front of the page. We don't do write-streaming
@@ -749,7 +749,7 @@ static int netfs_write_folio_single(struct netfs_io_request *wreq,/* Attach the folio to the rolling buffer. */ folio_get (folio); - ret =rolling_buffer_append (&wreq->buffer, folio,NETFS_ROLLBUF_PUT_MARK ); + ret =rolling_buffer_append (&wreq->buffer, folio,NETFS_ROLLBUF_PUT_MARK , wreq->gfp);if (ret <0 ) {folio_put (folio);return ret;
include/linux/netfs.h +1 -0
@@ -255,6 +255,7 @@ struct netfs_io_request {unsigned long long cleaned_to;/* Position we've cleaned folios to */ unsigned long long 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) */
include/linux/rolling_buffer.h +3 -3
@@ -43,13 +43,13 @@ struct rolling_buffer_snapshot {#define ROLLBUF_MARK_2 BIT(1)int rolling_buffer_init (struct rolling_buffer *roll,unsigned int rreq_id, -unsigned int direction); -int rolling_buffer_make_space (struct rolling_buffer *roll); +unsigned int direction,gfp_t gfp); +int rolling_buffer_make_space (struct rolling_buffer *roll,gfp_t gfp);ssize_t rolling_buffer_load_from_ra (struct rolling_buffer *roll,struct readahead_control *ractl,struct folio_batch *put_batch);ssize_t rolling_buffer_append (struct rolling_buffer *roll,struct folio *folio, -unsigned int flags); +unsigned int flags,gfp_t gfp);struct folio_queue *rolling_buffer_delete_spent (struct rolling_buffer *roll);void rolling_buffer_clear (struct rolling_buffer *roll);
Re: [PATCH 4/4] netfs: Fix folio_queue ENOMEM in writeback by adding a mempool
Quoted 8 lines · David Howells
Fix the handling of
folio_queueallocation failure in writeback by adding a mempool and passing ingfp_tflags to the rolling buffer functions that allocate memory, using the mempool if gfp !=GFP_KERNEL.This is then extended upwards and the gfp to be used for a request is stored in the
netfs_io_requeststruct and is then used for both requests and subrequests, eliminating the sleeping loops there.
Have you considered splitting this into multiple patches based on different functionalities?
fs/netfs/objects.c struct netfs_io_subrequest *netfs_alloc_subrequest(struct netfs_io_request *rreq · line 208
Quoted 11 lines
fs/netfs/objects.c +1
-6@@ -200,12 +205,14 @@ struct netfs_io_subrequest *netfs_alloc_subrequest(struct netfs_io_request *rreqmempool_t *mempool = rreq->netfs_ops->subrequest_pool ?: &netfs_subrequest_pool;struct kmem_cache *cache = mempool->pool_data; -for (;;) { - subreq =mempool_alloc (rreq->netfs_ops->subrequest_pool ?: &netfs_subrequest_pool, -GFP_KERNEL ); -if (subreq) -break ; -msleep (10 ); +if (rreq->gfp ==GFP_KERNEL ) {
Direct equality checks are not reliable; using !(rreq->gfp & GFP_NOFS) for the check is more robust.
fs/netfs/objects.c struct netfs_io_subrequest *netfs_alloc_subrequest(struct netfs_io_request *rreq · line 211
Quoted 3 lines
fs/netfs/objects.c +3
-0+ subreq = mempool->alloc (rreq->gfp, mempool->pool_data); +if (!subreq) +return ERR_PTR (-ENOMEM );
Since some callers check for success by testing !NULL, we cannot return ENOMEM.
221 subreq = netfs_alloc_subrequest(rreq);
222 if (!subreq) {
223 ret = -ENOMEM;
224 break;
fs/netfs/objects.c struct netfs_io_subrequest *netfs_alloc_subrequest(struct netfs_io_request *rreq · line 215
Quoted 4 lines
fs/netfs/objects.c +4
-0+ }else { + subreq =mempool_alloc (mempool, rreq->gfp); +if (!subreq) +return NULL ;
Redundant check. If it is added to mirror the GFP_KERNEL path above, returning the same error code would be better for the caller to handle. Or perhaps this is an intentional design?
Quoted 9 lines · David Howells
netfs: Miscellaneous fixes
Hi Christian,
Here are some miscellaneous fixes for netfslib.
(1) Clear
PG_private_2on copy-to-cache append failure.[...]
Applied to the vfs.fixes branch of the vfs/vfs.git tree. Patches in the vfs.fixes branch should appear in linux-next soon.
Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.
It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.
Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.
tree: https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git branch: vfs.fixes
[1/4] netfs: clear PG_private_2 on copy-to-cache append failure
https://git.kernel.org/vfs/vfs/c/cbdd2f04d712 [2/4] netfs: handle single writeback rolling buffer allocation failure https://git.kernel.org/vfs/vfs/c/a8c5ec7cb06c [3/4] netfs: release readahead folios on iterator preparation failure https://git.kernel.org/vfs/vfs/c/655f48ea68ef [4/4] netfs: Fix folio_queue ENOMEM in writeback by adding a mempool https://git.kernel.org/vfs/vfs/c/fc6965676202