Linux 9p file system development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "David Howells" <dhowells@redhat.com>
Cc: v9fs@lists.linux.dev, "Chuck Lever" <cel@kernel.org>,
	"Jeff Layton" <jlayton@kernel.org>,
	"Anna Schumaker" <anna@kernel.org>
Subject: Re: [PATCH v19 11/17] afs: Use a bvecq to hold dir content rather than folioq
Date: Thu, 19 Nov 2026 09:29:12 +0000	[thread overview]
Message-ID: <sashiko-outbox-222836@kernel.org> (raw)
In-Reply-To: <20261119083851.3306418-12-dhowells@redhat.com>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] afs: inconsistent i_size and uninitialized buffer in
  afs_symlink_writepages()
- [Medium] afs: inconsistent directory state and size in
  afs_dir_writepages()
--

commit 38825177e122ffa7a8e67a676f8d6170b75ad709
Author: David Howells <dhowells@redhat.com>

afs: Use a bvecq to hold dir content rather than folioq

This commit replaces folio_queue buffers with bvecq buffers for AFS
directories and symlinks, and snapshots the buffer, buffer size, and
inode size under cb_lock during writeback.

Link: https://sashiko.dev/#/patchset/20261107160823.1465553-1-dhowells%40redhat.com

> diff --git a/fs/afs/dir.c b/fs/afs/dir.c
> index 51c158d8f723f..b75a26b312291 100644
> --- a/fs/afs/dir.c
> +++ b/fs/afs/dir.c
[ ... ]
> @@ -2220,12 +2220,11 @@ static int afs_dir_writepages(struct address_space *mapping,
>  			      struct writeback_control *wbc)
>  {
>  	struct afs_vnode *dvnode = AFS_FS_I(mapping->host);
> +	struct bvecq *directory = NULL;
>  	struct iov_iter iter;
>  	int ret = 0;
>  
> -	/* Need to lock to prevent the folio queue and folios from being thrown
> -	 * away.
> -	 */
> +	/* Need to lock to prevent the bvecq and folios from being discarded. */
>  	if (!down_read_trylock(&dvnode->validate_lock)) {
>  		if (wbc->sync_mode == WB_SYNC_NONE) {
>  			/* The VFS will have undirtied the inode. */
> @@ -2235,15 +2234,23 @@ static int afs_dir_writepages(struct address_space *mapping,
>  		down_read(&dvnode->validate_lock);
>  	}
>  
> +	read_seqlock_excl(&dvnode->cb_lock);
>  	if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) {
> -		iov_iter_folio_queue(&iter, ITER_SOURCE, dvnode->directory,
> -				     0, 0, dvnode->directory_size);
> -		ret = netfs_writeback_single(mapping, wbc, &iter,
> -					     i_size_read(&dvnode->netfs.inode));
> +		size_t i_size = i_size_read(&dvnode->netfs.inode);
> +		size_t d_size = dvnode->directory_size;
> +
> +		directory = bvecq_get(dvnode->directory);
> +		read_sequnlock_excl(&dvnode->cb_lock);
> +
> +		iov_iter_bvec_queue(&iter, ITER_SOURCE, directory, 0, 0, d_size);
> +		ret = netfs_writeback_single(mapping, wbc, &iter, i_size);
>  		if (ret == 1)
>  			ret = 0; /* Skipped write due to lock conflict. */
> +	} else {
> +		read_sequnlock_excl(&dvnode->cb_lock);
>  	}

[Severity: Medium]
Can afs_dir_writepages() still observe i_size > d_size or write an
unvalidated directory buffer to fscache?

In afs_read_dir(), set_bit(AFS_VNODE_DIR_VALID, &dvnode->flags) runs
outside cb_lock after afs_read_single() has dropped AFS_VNODE_IO_LOCK:

CPU0                              CPU1
afs_read_dir()
  afs_read_single()
    ... (allocates d_size)
                                  afs_do_lookup() (FS.InlineBulkStatus)
                                    afs_vnode_commit_status()
                                      write_seqlock(&dvnode->cb_lock)
                                      afs_apply_status()
                                        afs_invalidate_dir()
                                        i_size_write(..., new_size)
                                      write_sequnlock(&dvnode->cb_lock)
  set_bit(AFS_VNODE_DIR_VALID)
  downgrade_write(&validate_lock)
                                  afs_dir_writepages()
                                    netfs_writeback_single(..., i_size)
                                      <-- WARN_ON_ONCE(d_size < i_size)

Also, new directory vnodes start with AFS_VNODE_DIR_VALID set and
AFS_VNODE_DIR_READ clear.  If afs_do_read_single() fails in
afs_dir_check() after netfs_read_single() has already dirtied the inode,
afs_read_dir() unlocks validate_lock without clearing AFS_VNODE_DIR_VALID,
and afs_dir_writepages() (which does not check AFS_VNODE_DIR_READ) writes
the corrupt buffer into fscache.

[ ... ]

> diff --git a/fs/afs/symlink.c b/fs/afs/symlink.c
> index c18e4f8064234..823a9ba82eb7f 100644
> --- a/fs/afs/symlink.c
> +++ b/fs/afs/symlink.c
> @@ -56,7 +56,7 @@ void afs_evict_symlink(struct afs_vnode *vnode)
>  void afs_init_new_symlink(struct afs_vnode *vnode, struct afs_operation *op)
>  {
>  	struct afs_symlink *symlink = op->create.symlink;
> -	size_t dsize = 0;
> +	unsigned int alloc_flags = 0;
>  	size_t size = strlen(symlink->content) + 1;
>  	char *p;
>  
> @@ -66,12 +66,15 @@ void afs_init_new_symlink(struct afs_vnode *vnode, struct afs_operation *op)
>  	if (!fscache_cookie_enabled(netfs_i_cookie(&vnode->netfs)))
>  		return;
>  
> -	if (netfs_alloc_folioq_buffer(NULL, &vnode->directory, &dsize, size,
> -				      mapping_gfp_mask(vnode->netfs.inode.i_mapping)) < 0)
> +	vnode->directory =
> +		bvecq_alloc_buffer(PAGE_SIZE,
> +				   mapping_gfp_mask(vnode->netfs.inode.i_mapping),
> +				   &alloc_flags);
> +	if (!vnode->directory)
>  		return;
>  
> -	vnode->directory_size = dsize;
> -	p = kmap_local_folio(folioq_folio(vnode->directory, 0), 0);
> +	vnode->directory_size = PAGE_SIZE;
> +	p = bvec_kmap_partial(&vnode->directory->bv[0], 0);
>  	memcpy(p, symlink->content, size);
>  	kunmap_local(p);
>  	netfs_single_mark_inode_dirty(&vnode->netfs.inode);
[ ... ]
> @@ -249,27 +251,39 @@ int afs_symlink_writepages(struct address_space *mapping,
>  		down_read(&vnode->validate_lock);
>  	}
>  
> +	read_seqlock_excl(&vnode->cb_lock);
>  	if (vnode->directory &&
>  	    atomic64_read(&vnode->cb_expires_at) != AFS_NO_CB_PROMISE) {
> +		struct bvecq *bq = bvecq_get(vnode->directory);
>  		size_t i_size = i_size_read(&vnode->netfs.inode);
> +		size_t d_size = vnode->directory_size;
>  
> -		if (i_size < vnode->directory_size) {
> -			iov_iter_folio_queue(&iter, ITER_DEST, vnode->directory, 0, 0,
> -					     vnode->directory_size);
> +		read_sequnlock_excl(&vnode->cb_lock);
> +
> +		if (i_size < d_size) {
> +			iov_iter_bvec_queue(&iter, ITER_DEST, bq, 0, 0, d_size);
>  			iov_iter_advance(&iter, i_size);
> -			iov_iter_zero(vnode->directory_size - i_size, &iter);
> +			iov_iter_zero(d_size - i_size, &iter);
>  		}
> -		iov_iter_folio_queue(&iter, ITER_SOURCE, vnode->directory, 0, 0,
> -				     vnode->directory_size);
> +
> +		iov_iter_bvec_queue(&iter, ITER_SOURCE, bq, 0, 0, d_size);
>  		ret = netfs_writeback_single(mapping, wbc, &iter, i_size);
> +		bvecq_put(bq);
> +	} else {
> +		read_sequnlock_excl(&vnode->cb_lock);
>  	}

[Severity: High]
Can afs_symlink_writepages() write uninitialized page memory to fscache or
pass i_size > d_size to netfs_writeback_single()?

Both afs_init_new_symlink() and afs_do_read_symlink() allocate
vnode->directory via bvecq_alloc_buffer() without __GFP_ZERO and only
populate the initial symlink length.  Unlike directories (which clear
AFS_VNODE_DIR_VALID in afs_apply_status()), when a status update arrives
for a symlink with a new data version, afs_vnode_commit_status() sets
AFS_VNODE_ZAP_DATA, updates inode->i_size, and installs a new valid
cb_expires_at != AFS_NO_CB_PROMISE:

fs/afs/inode.c:afs_apply_status() {
    ...
		if (vnode->status.type == AFS_FTYPE_DIR)
			afs_invalidate_dir(vnode, afs_dir_invalid_dv_mismatch);
		else
			set_bit(AFS_VNODE_ZAP_DATA, &vnode->flags);
		change_size = true;
    ...
}

Because afs_symlink_writepages() does not check AFS_VNODE_ZAP_DATA (and
afs_invalidate_symlink() only clears vnode->symlink, not
vnode->directory), an updated i_size larger than the old symlink length
causes afs_symlink_writepages() to zero only from the new i_size to
d_size, writing stale content plus uninitialized kernel memory in
[old_size, i_size) into fscache.  If the server reports i_size >
PAGE_SIZE (including when afs_do_read_symlink() returns -EFBIG after
netfs_read_single() has updated i_size and dirtied the inode), i_size >
d_size triggers WARN_ON_ONCE(iov_iter_count(iter) < len) in
netfs_writeback_single().

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261119083851.3306418-1-dhowells@redhat.com?part=11

  reply	other threads:[~2026-11-19  9:29 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-19  8:38 [PATCH v19 00/17] netfs, iov_iter: Use a chain of bio_vec arrays instead of folio_queue David Howells
2026-11-19  8:38 ` [PATCH v19 01/17] iov_iter: Fix kunit test to remove __init and __refdata marks David Howells
2026-11-19  8:47   ` sashiko-bot
2026-11-19  9:21   ` Wang Yibo
2026-11-19  8:38 ` [PATCH v19 02/17] block: Add a function to kmap one page of a multipage bio_vec David Howells
2026-11-19  8:44   ` sashiko-bot
2026-11-19  8:38 ` [PATCH v19 03/17] iov_iter: Add a segmented queue of bio_vec[] David Howells
2026-11-19  8:52   ` sashiko-bot
2026-11-19  8:38 ` [PATCH v19 04/17] netfs: Add some tools for managing bvecq chains David Howells
2026-11-19  9:07   ` sashiko-bot
2026-11-19 11:49   ` David Howells
2026-11-19  8:38 ` [PATCH v19 05/17] iov_iter: Add an API document for the bvecq structure David Howells
2026-11-19  8:53   ` sashiko-bot
2026-11-19  8:38 ` [PATCH v19 06/17] iov_iter: Add a bvecq kunit test David Howells
2026-11-19  8:50   ` sashiko-bot
2026-11-19  8:38 ` [PATCH v19 07/17] afs: Fix an inconsistency in the symlink code David Howells
2026-11-19  8:57   ` sashiko-bot
2026-11-19  8:38 ` [PATCH v19 08/17] netfs: Explicitly pass tail padding to netfs_writeback_single() David Howells
2026-11-19  9:15   ` sashiko-bot
2026-11-19  8:38 ` [PATCH v19 09/17] netfs: Change netfs_writeback_single() to not look inside the iterator David Howells
2026-11-19  9:09   ` sashiko-bot
2026-11-19  8:38 ` [PATCH v19 10/17] netfs: Pass the write length to request creation where appropriate David Howells
2026-11-19  8:56   ` sashiko-bot
2026-11-19  8:38 ` [PATCH v19 11/17] afs: Use a bvecq to hold dir content rather than folioq David Howells
2026-11-19  9:29   ` sashiko-bot [this message]
2026-11-19  8:38 ` [PATCH v19 12/17] cifs: Use a bvecq for buffering instead of a folioq David Howells
2026-11-19  9:10   ` sashiko-bot
2026-11-19  8:38 ` [PATCH v19 13/17] smbdirect: Support ITER_BVECQ in smbdirect_map_sges_from_iter() David Howells
2026-11-19  9:15   ` sashiko-bot
2026-11-19  8:38 ` [PATCH v19 14/17] netfs: Switch folioq to bvecq David Howells
2026-11-19  9:38   ` sashiko-bot
2026-11-19 11:49   ` David Howells
2026-11-19  8:38 ` [PATCH v19 15/17] smbdirect: Remove support for ITER_FOLIOQ from smbdirect_map_sges_from_iter() David Howells
2026-11-19  9:13   ` sashiko-bot
2026-11-19  8:38 ` [PATCH v19 16/17] iov_iter: Remove ITER_FOLIOQ David Howells
2026-11-19  9:20   ` sashiko-bot
2026-11-19  8:38 ` [PATCH v19 17/17] netfs: Remove folio_queue David Howells
2026-11-19  9:18   ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-222836@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=anna@kernel.org \
    --cc=cel@kernel.org \
    --cc=dhowells@redhat.com \
    --cc=jlayton@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=v9fs@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox