netfs: Yet further miscellaneous fixes
Subsystems (5)
David Howells
maintainercover netfs: Yet further miscellaneous fixes
Hi Christian,
Here are yet more miscellaneous fixes for netfslib, one found by Mark Brown when integrating linux-next and the rest found by Sashiko.dev's AI review[1]:
(1) Fix a return statement that should have been removed from netfs_read_sizes() and only happens on 32-bit SMP.
(2) Fix read and write result collection use barriering correctly to access a request's subrequest lists without taking a lock.
This adds list_add_tail_release() and list_first_entry_acquire() to appropriate incorporate barriering into some list functions.
(3) Fix afs_get_link() to take the vnode->validate_lock around afs_read_single() to prevent a race with another caller of afs_get_link() also trying to read the symlink content.
(4) Fix the RCU handling of symlinks in RCU pathwalk. The problems were that afs_get_link() didn't use proper RCU pointer dereferencing and that AFS symlinks can be updated remotely, potentially causing the buffer memory to be changed.
These are applied on top of your vfs.fixes branch. Patch 1 should fix the problem in linux-next integration.
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
[1] https://sashiko.dev/#/patchset/20260326104544.509518-1-dhowells%40redhat.com
base-commit: 986563b3799fee66965811df3f46f5bb76736084
David Howells (4):
netfs: Fix wrong return from netfs_read_sizes() on 32-bit SMP arches
netfs: Fix missing barriers when accessing stream->subrequests
locklessly
afs: Fix afs_get_link() to take validate_lock around afs_read_single()
afs: Fix RCU handling of symlinks in RCU pathwalk
fs/afs/Makefile | 1 + fs/afs/dir.c | 33 ++++-- fs/afs/fsclient.c | 4 +- fs/afs/inode.c | 96 +--------------- fs/afs/internal.h | 35 ++++-- fs/afs/symlink.c | 242 +++++++++++++++++++++++++++++++++++++++ fs/afs/yfsclient.c | 4 +- fs/netfs/buffered_read.c | 9 +- fs/netfs/direct_read.c | 15 +-- fs/netfs/internal.h | 3 + fs/netfs/read_collect.c | 4 +- fs/netfs/read_single.c | 12 +- fs/netfs/write_collect.c | 4 +- fs/netfs/write_issue.c | 3 +- include/linux/list.h | 37 ++++++ include/linux/netfs.h | 1 - 16 files changed, 358 insertions(+), 145 deletions(-) create mode 100644 fs/afs/symlink.c
David Howells
maintainerPATCH 1/4 netfs: Fix wrong return from netfs_read_sizes() on 32-bit SMP arches
Fix netfs_read_sizes() for 32-bit SMP arches to not have a return value.
--- include/linux/netfs.h | 1 - 1 file changed, 1 deletion(-)
include/linux/netfs.h +0 -1
@@ -669,7 +669,6 @@ static inline void netfs_read_sizes(const struct netfs_inode *ictx, *remote_i_size = ictx->_remote_i_size; *zero_point = ictx->_zero_point; }while (read_seqcount_retry (&inode->i_size_seqcount, seq)); -return zero_point;#elif BITS_PER_LONG==32 && defined(CONFIG_PREEMPTION)unsigned long long zero_point;
David Howells
maintainerPATCH 2/4 netfs: Fix missing barriers when accessing stream->subrequests locklessly
In reply to David HowellsThe list of subrequests attached to stream->subrequests is accessed without locks by netfs_collect_read_results() and netfs_collect_write_results(), but they access subreq->flags without taking a barrier after getting the subreq pointer from the list. Relatedly, the functions that build the list don't use any sort of write barrier when constructing the list to make sure that the NETFS_SREQ_IN_PROGRESS flag is perceived to be set first if no lock is taken.
Fix this by:
(1) Add a new list_add_tail_release() function that uses a release barrier to set the pointer to the new member of the list.
(2) Add a new list_first_entry_acquire() function that uses an acquire barrier to read the pointer to the first member in a list (or return NULL).
(3) Use list_add_tail_release() when adding a subreq to ->subrequests.
(4) Make direct-read and read-single use netfs_queue_read() so that they share the relevant bit of code with buffered-read.
(5) Use list_first_entry_acquire() when initially accessing the front of the list (when an item is removed, the pointer to the new front iterm is obtained under the same lock).
--- fs/netfs/buffered_read.c | 9 +++++---- fs/netfs/direct_read.c | 15 +-------------- fs/netfs/internal.h | 3 +++ fs/netfs/read_collect.c | 4 +++- fs/netfs/read_single.c | 12 +----------- fs/netfs/write_collect.c | 4 +++- fs/netfs/write_issue.c | 3 ++- include/linux/list.h | 37 +++++++++++++++++++++++++++++++++++++ 8 files changed, 55 insertions(+), 32 deletions(-)
fs/netfs/buffered_read.c +5 -4
@@ -156,9 +156,9 @@ static void netfs_read_cache_to_pagecache(struct netfs_io_request *rreq, netfs_cache_read_terminated, subreq); } -static void netfs_queue_read (struct netfs_io_request *rreq, -struct netfs_io_subrequest *subreq, -bool last_subreq) +void netfs_queue_read (struct netfs_io_request *rreq, +struct netfs_io_subrequest *subreq, +bool last_subreq) {struct netfs_io_stream *stream = &rreq->io_streams[0 ];
@@ -169,7 +169,8 @@ static void netfs_queue_read(struct netfs_io_request *rreq,* remove entries off of the front. */ spin_lock (&rreq->lock); -list_add_tail (&subreq->rreq_link, &stream->subrequests); +/* Write IN_PROGRESS before pointer to new subreq */ +list_add_tail_release (&subreq->rreq_link, &stream->subrequests);if (list_is_first (&subreq->rreq_link, &stream->subrequests)) {if (!stream->active) { stream->collected_to = subreq->start;
fs/netfs/direct_read.c +1 -14
@@ -47,7 +47,6 @@ static void netfs_prepare_dio_read_iterator(struct netfs_io_subrequest *subreq)*/ static int netfs_dispatch_unbuffered_reads (struct netfs_io_request *rreq) { -struct netfs_io_stream *stream = &rreq->io_streams[0 ];unsigned long long start = rreq->start;ssize_t size = rreq->len;int ret =0 ;
@@ -66,19 +65,7 @@ static int netfs_dispatch_unbuffered_reads(struct netfs_io_request *rreq) subreq->start = start; subreq->len = size; -__set_bit (NETFS_SREQ_IN_PROGRESS , &subreq->flags); - -spin_lock (&rreq->lock); -list_add_tail (&subreq->rreq_link, &stream->subrequests); -if (list_is_first (&subreq->rreq_link, &stream->subrequests)) { -if (!stream->active) { - stream->collected_to = subreq->start; -/* Store list pointers before active flag */ -smp_store_release (&stream->active, true); - } - } -trace_netfs_sreq (subreq, netfs_sreq_trace_added); -spin_unlock (&rreq->lock); +netfs_queue_read (rreq, subreq, false);netfs_stat (&netfs_n_rh_download);if (rreq->netfs_ops->prepare_read) {
fs/netfs/internal.h +3 -0
@@ -23,6 +23,9 @@/* * buffered_read.c */ +void netfs_queue_read (struct netfs_io_request *rreq, +struct netfs_io_subrequest *subreq, +bool last_subreq);void netfs_cache_read_terminated (void *priv,ssize_t transferred_or_error);int netfs_prefetch_for_write (struct file *file,struct folio *folio,size_t offset,size_t len);
fs/netfs/read_collect.c +3 -1
@@ -205,8 +205,10 @@ static void netfs_collect_read_results(struct netfs_io_request *rreq)* in progress. The issuer thread may be adding stuff to the tail * whilst we're doing this. */ - front =list_first_entry_or_null (&stream->subrequests, + front =list_first_entry_acquire (&stream->subrequests, struct netfs_io_subrequest, rreq_link); +/* Read first subreq pointer before IN_PROGRESS flag. */ +while (front) {size_t transferred;
fs/netfs/read_single.c +1 -11
@@ -89,7 +89,6 @@ static void netfs_single_read_cache(struct netfs_io_request *rreq,*/ static int netfs_single_dispatch_read (struct netfs_io_request *rreq) { -struct netfs_io_stream *stream = &rreq->io_streams[0 ];struct netfs_io_subrequest *subreq;int ret =0 ;
@@ -102,14 +101,7 @@ static int netfs_single_dispatch_read(struct netfs_io_request *rreq) subreq->len = rreq->len; subreq->io_iter = rreq->buffer.iter; -__set_bit (NETFS_SREQ_IN_PROGRESS , &subreq->flags); - -spin_lock (&rreq->lock); -list_add_tail (&subreq->rreq_link, &stream->subrequests); -trace_netfs_sreq (subreq, netfs_sreq_trace_added); -/* Store list pointers before active flag */ -smp_store_release (&stream->active, true); -spin_unlock (&rreq->lock); +netfs_queue_read (rreq, subreq, true);netfs_single_cache_prepare_read (rreq, subreq);switch (subreq->source) {
@@ -137,8 +129,6 @@ static int netfs_single_dispatch_read(struct netfs_io_request *rreq)break ; } -smp_wmb ();/* Write lists before ALL_QUEUED. */ -set_bit (NETFS_RREQ_ALL_QUEUED , &rreq->flags);return ret; cancel:netfs_put_subrequest (subreq, netfs_sreq_trace_put_cancel);
fs/netfs/write_collect.c +3 -1
@@ -227,8 +227,10 @@ static void netfs_collect_write_results(struct netfs_io_request *wreq)if (!smp_load_acquire (&stream->active))continue ; - front =list_first_entry_or_null (&stream->subrequests, + front =list_first_entry_acquire (&stream->subrequests, struct netfs_io_subrequest, rreq_link); +/* Read first subreq pointer before IN_PROGRESS flag. */ +while (front) {trace_netfs_collect_sreq (wreq, front);//_debug("sreq [%x] %llx %zx/%zx",
fs/netfs/write_issue.c +2 -1
@@ -204,7 +204,8 @@ void netfs_prepare_write(struct netfs_io_request *wreq,* remove entries off of the front. */ spin_lock (&wreq->lock); -list_add_tail (&subreq->rreq_link, &stream->subrequests); +/* Write IN_PROGRESS before pointer to new subreq */ +list_add_tail_release (&subreq->rreq_link, &stream->subrequests);if (list_is_first (&subreq->rreq_link, &stream->subrequests)) {if (!stream->active) { stream->collected_to = subreq->start;
include/linux/list.h +37 -0
@@ -191,6 +191,29 @@ static inline void list_add_tail(struct list_head *new, struct list_head *head)__list_add (new, head->prev, head); } +/** +* list_add_tail_release - add a new entry with release barrier +* @new: new entry to be added +* @head: list head to add it before +* +* Insert a new entry before the specified head, using a release barrier to set +* the ->next pointer that points to it. This is useful for implementing +* queues, in particular one that the elements will be walked through forwards +* locklessly. +*/ +static inline void list_add_tail_release (struct list_head *new, +struct list_head *head) +{ +struct list_head *prev = head->prev; + +if (__list_add_valid (new, prev, head)) { + new->next = head; + new->prev = prev; + head->prev = new; +smp_store_release (&prev->next, new); + } +} +/* * Delete a list entry by making the prev/next entries * point to each other.
@@ -644,6 +667,20 @@ static inline void list_splice_tail_init(struct list_head *list, pos__ != head__ ?list_entry (pos__, type, member) :NULL ; \ }) +/** +* list_first_entry_acquire - get the first element from a list with barrier +* @ptr: the list head to take the element from. +* @type: the type of the struct this is embedded in. +* @member: the name of the list_head within the struct. +* +* Note that if the list is empty, it returns NULL. +*/ +#define list_first_entry_acquire (ptr, type, member) ({ \ + struct list_head *head__ = (ptr); \ + struct list_head *pos__ = smp_load_acquire(&head__->next); \ + pos__ != head__ ? list_entry(pos__, type, member) : NULL; \ +}) +/** * list_last_entry_or_null - get the last element from a list * @ptr: the list head to take the element from.
David Howells
maintainerPATCH 3/4 afs: Fix afs_get_link() to take validate_lock around afs_read_single()
In reply to David HowellsFix afs_get_link() to take the validate_lock around afs_read_single() to prevent races between multiple ->get_link() calls.
--- fs/afs/inode.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-)
fs/afs/inode.c +13 -4
@@ -78,10 +78,19 @@ const char *afs_get_link(struct dentry *dentry, struct inode *inode, goto good; fetch: - ret =afs_read_single (vnode,NULL ); -if (ret <0 ) -return ERR_PTR (ret); -set_bit (AFS_VNODE_DIR_READ , &vnode->flags); +if (down_write_killable (&vnode->validate_lock) <0 ) +return ERR_PTR (-ERESTARTSYS ); +if (test_and_clear_bit (AFS_VNODE_ZAP_DATA , &vnode->flags) || + !test_bit (AFS_VNODE_DIR_READ , &vnode->flags)) { + ret =afs_read_single (vnode,NULL ); +if (ret <0 ) { +up_write (&vnode->validate_lock); +return ERR_PTR (ret); + } +set_bit (AFS_VNODE_DIR_READ , &vnode->flags); + } + +up_write (&vnode->validate_lock); good: folio =folioq_folio (vnode->directory,0 );
David Howells
maintainerPATCH 4/4 afs: Fix RCU handling of symlinks in RCU pathwalk
In reply to David HowellsThe afs filesystem in the kernel doesn't handle RCU pathwalk of symlinks correctly. The problem is twofold: firstly, it doesn't treat the buffer pointers as RCU pointers with the appropriate barriering; and secondly, it can race with another thread updating the contents of the symlink because a third party updated it on the server.
Fix this by the following means:
(1) Keep a separate copy of the symlink contents with an rcu_head. This is always going to be a lot smaller than a page, so it can be kmalloc'd and save quite a bit of memory. It also needs a refcount for non-RCU pathwalk.
(2) Split the symlink read and write-to-cache routines in afs from those for directories.
(3) Discard the I/O buffer as soon as the write-to-cache completes as this is a full page (plus a folio_queue).
(4) If there's no cache, discard the I/O buffer immediately after reading and copying if there is no cache.
--- fs/afs/Makefile | 1 + fs/afs/dir.c | 33 +++++-- fs/afs/fsclient.c | 4 +- fs/afs/inode.c | 105 +------------------- fs/afs/internal.h | 35 +++++-- fs/afs/symlink.c | 242 +++++++++++++++++++++++++++++++++++++++++++++ fs/afs/yfsclient.c | 4 +- 7 files changed, 303 insertions(+), 121 deletions(-) create mode 100644 fs/afs/symlink.c
fs/afs/Makefile +1 -0
@@ -30,6 +30,7 @@ kafs-y := \ server.o \ server_list.o \ super.o \ + symlink.o \ validation.o \ vlclient.o \ vl_alias.o \
fs/afs/dir.c +25 -8
@@ -68,7 +68,7 @@ const struct inode_operations afs_dir_inode_operations = { };const struct address_space_operations afs_dir_aops = { - .writepages = afs_single_writepages, + .writepages = afs_dir_writepages, };const struct dentry_operations afs_fs_dentry_operations = {
@@ -294,7 +294,7 @@ static ssize_t afs_do_read_single(struct afs_vnode *dvnode, struct file *file)return ret; } -ssize_t afs_read_single (struct afs_vnode *dvnode,struct file *file) +static ssize_t afs_read_single (struct afs_vnode *dvnode,struct file *file) {ssize_t ret;
@@ -1763,13 +1763,20 @@ static int afs_link(struct dentry *from, struct inode *dir,return ret; } +static void afs_symlink_put (struct afs_operation *op) +{ +kfree (op->create.symlink); + op->create.symlink =NULL ; +afs_create_put (op); +} +static const struct afs_operation_ops afs_symlink_operation = { .issue_afs_rpc = afs_fs_symlink, .issue_yfs_rpc = yfs_fs_symlink, .success = afs_create_success, .aborted = afs_check_for_remote_deletion, .edit_dir = afs_create_edit_dir, - .put = afs_create_put, + .put = afs_symlink_put, };/*
@@ -1779,7 +1786,9 @@ static int afs_symlink(struct mnt_idmap *idmap, struct inode *dir, struct dentry *dentry, const char *content) { struct afs_operation *op; +struct afs_symlink *symlink;struct afs_vnode *dvnode =AFS_FS_I (dir); +size_t clen =strlen (content);int ret; _enter("{%llx:%llu},{%pd},%s",
@@ -1791,12 +1800,20 @@ static int afs_symlink(struct mnt_idmap *idmap, struct inode *dir, goto error; ret = -EINVAL ; -if (strlen (content) >=AFSPATHMAX ) +if (clen >=AFSPATHMAX ) + goto error; + + ret = -ENOMEM ; + symlink = kmalloc_flex(struct afs_symlink, content, clen + 1, GFP_KERNEL); +if (!symlink) goto error; +refcount_set (&symlink->ref,1 ); +memcpy (symlink->content, content, clen +1 ); op =afs_alloc_operation (NULL , dvnode->volume);if (IS_ERR (op)) { ret =PTR_ERR (op); +kfree (symlink); goto error; }
@@ -1808,7 +1825,7 @@ static int afs_symlink(struct mnt_idmap *idmap, struct inode *dir, op->dentry = dentry; op->ops = &afs_symlink_operation; op->create.reason = afs_edit_dir_for_symlink; - op->create.symlink = content; + op->create.symlink = symlink; op->mtime =current_time (dir); ret =afs_do_sync_operation (op);afs_dir_unuse_cookie (dvnode, ret);
@@ -2192,10 +2209,10 @@ static int afs_rename(struct mnt_idmap *idmap, struct inode *old_dir, }/* -* Write the file contents to the cache as a single blob. +* Write the directory contents to the cache as a single blob. */ -int afs_single_writepages (struct address_space *mapping, -struct writeback_control *wbc) +int afs_dir_writepages (struct address_space *mapping, +struct writeback_control *wbc) {struct afs_vnode *dvnode =AFS_FS_I (mapping->host);struct iov_iter iter;
fs/afs/fsclient.c +2 -2
@@ -886,7 +886,7 @@ void afs_fs_symlink(struct afs_operation *op) namesz = name->len; padsz = (4 - (namesz &3 )) &3 ; - c_namesz =strlen (op->create.symlink); + c_namesz =strlen (op->create.symlink->content); c_padsz = (4 - (c_namesz &3 )) &3 ; reqsz = (6 *4 ) + namesz + padsz + c_namesz + c_padsz + (6 *4 );
@@ -910,7 +910,7 @@ void afs_fs_symlink(struct afs_operation *op) bp = (void *) bp + padsz; } *bp++ =htonl (c_namesz); -memcpy (bp, op->create.symlink, c_namesz); +memcpy (bp, op->create.symlink->content, c_namesz); bp = (void *) bp + c_namesz;if (c_padsz >0 ) {memset (bp,0 , c_padsz);
fs/afs/inode.c +4 -101
128 lines not on this page · raw message
fs/afs/internal.h +27 -8
@@ -711,6 +711,7 @@ struct afs_vnode {#define AFS_VNODE_DIR_READ 11/* Set if we've read a dir's contents */ struct folio_queue *directory;/* Directory contents */ +struct afs_symlink __rcu *symlink;/* Symlink content */ struct list_head wb_keys;/* List of keys available for writeback */ struct list_head pending_locks;/* locks waiting to be granted */ struct list_head granted_locks;/* locks granted on this file */
@@ -777,6 +778,15 @@ struct afs_permits { struct afs_permit permits[] __counted_by(nr_permits); /* List of permits sorted by key pointer */ }; +/* +* Copy of symlink content for normal use. +*/ +struct afs_symlink { +struct rcu_head rcu; +refcount_t ref; +char content[]; +}; +/* * Error prioritisation and accumulation. */
@@ -888,7 +898,7 @@ struct afs_operation {struct {int reason;/* enum afs_edit_dir_reason */ mode_t mode; -const char *symlink; +struct afs_symlink *symlink; } create;struct {bool need_rehash;
@@ -1099,13 +1109,12 @@ extern const struct inode_operations afs_dir_inode_operations;extern const struct address_space_operations afs_dir_aops;extern const struct dentry_operations afs_fs_dentry_operations; -ssize_t afs_read_single (struct afs_vnode *dvnode,struct file *file); ssize_t afs_read_dir(struct afs_vnode *dvnode, struct file *file) __acquires(&dvnode->validate_lock);extern void afs_d_release (struct dentry *);extern void afs_check_for_remote_deletion (struct afs_operation *); -int afs_single_writepages (struct address_space *mapping, -struct writeback_control *wbc); +int afs_dir_writepages (struct address_space *mapping, +struct writeback_control *wbc);/* * dir_edit.c
@@ -1247,10 +1256,6 @@ extern void afs_fs_probe_cleanup(struct afs_net *);*/ extern const struct afs_operation_ops afs_fetch_status_operation; -void afs_init_new_symlink (struct afs_vnode *vnode,struct afs_operation *op); -const char *afs_get_link (struct dentry *dentry,struct inode *inode, -struct delayed_call *callback); -int afs_readlink (struct dentry *dentry,char __user *buffer,int buflen);extern void afs_vnode_commit_status (struct afs_operation *,struct afs_vnode_param *);extern int afs_fetch_status (struct afs_vnode *,struct key *,bool ,afs_access_t *);extern int afs_ilookup5_test_by_fid (struct inode *,void *);
@@ -1600,6 +1605,20 @@ void afs_detach_volume_from_servers(struct afs_volume *volume, struct afs_serverextern int __init afs_fs_init (void );extern void afs_fs_exit (void ); +/* +* symlink.c +*/ +extern const struct inode_operations afs_symlink_inode_operations; +extern const struct address_space_operations afs_symlink_aops; + +void afs_replace_symlink (struct afs_vnode *vnode,struct afs_symlink *symlink); +void afs_init_new_symlink (struct afs_vnode *vnode,struct afs_operation *op); +const char *afs_get_link (struct dentry *dentry,struct inode *inode, +struct delayed_call *callback); +int afs_readlink (struct dentry *dentry,char __user *buffer,int buflen); +int afs_symlink_writepages (struct address_space *mapping, +struct writeback_control *wbc); +/* * validation.c */
fs/afs/symlink.c new file +242 -0
242 lines not on this page · raw message
fs/afs/yfsclient.c +2 -2
@@ -960,7 +960,7 @@ void yfs_fs_symlink(struct afs_operation *op)_enter ("" ); - contents_sz =strlen (op->create.symlink); + contents_sz =strlen (op->create.symlink->content); call = afs_alloc_flat_call(op->net, &yfs_RXYFSSymlink, sizeof(__be32) + sizeof(struct yfs_xdr_RPCFlags) +
@@ -981,7 +981,7 @@ void yfs_fs_symlink(struct afs_operation *op) bp =xdr_encode_u32 (bp,0 );/* RPC flags */ bp =xdr_encode_YFSFid (bp, &dvp->fid); bp =xdr_encode_name (bp, name); - bp =xdr_encode_string (bp, op->create.symlink, contents_sz); + bp =xdr_encode_string (bp, op->create.symlink->content, contents_sz); bp =xdr_encode_YFSStoreStatus (bp, &mode, &op->mtime);yfs_check_req (call, bp);