Unverified Commit d5cb81ba authored by Christian Brauner's avatar Christian Brauner
Browse files

Merge patch series "netfs, cifs: Fixes to retry-related code"

David Howells <dhowells@redhat.com> says:

Here are some miscellaneous fixes and changes for netfslib and cifs, if you
could consider pulling them.

Many of these were found because a bug in Samba was causing smbd to crash
and restart after about 1-2s and this was vigorously and abruptly
exercising the netfslib retry paths.

Subsequent testing of the cifs RDMA support showed up some more bugs, but
the fixes for those went via the cifs tree and have been removed from this set
as they're now upstream.

First, there are some netfs fixes:

 (1) Fix a hang due to missing case in final DIO read result collection
     not breaking out of a loop if the request finished, but there were no
     subrequests being processed and NETFS_RREQ_ALL_QUEUED wasn't yet set.

 (2) Fix a double put of the netfs_io_request struct if completion happened
     in the pause loop.

 (3) Provide some helpers to abstract out NETFS_RREQ_IN_PROGRESS flag
     wrangling.

 (4) Fix infinite looping in netfs_wait_for_pause/request() which wa caused
     by a loop waiting for NETFS_RREQ_ALL_QUEUED to get set - but which
     wouldn't get set until the looping function returned.  This uses patch
     (3) above.

 (5) Fix a ref leak on an extra subrequest inserted into a request's list
     of subreqs because more subreq records were needed for retrying than
     were needed for the original request (say, for instance, that the
     amount of cifs credit available was reduced and, subsequently, the ops
     had to be smaller).

Then a bunch of cifs fixes, some of which are from other people:

 (6-8) cifs: Fix various RPC callbacks to set NETFS_SREQ_NEED_RETRY if a
     subrequest fails retriably.

(10) Fix a warning in the workqueue code when reconnecting a channel.

Followed by some patches to deal with i_size handling:

(11) Fix the updating of i_size to use a lock to avoid a race between
     testing if we should have extended the file with a DIO write and
     changing i_size.

(12) A follow-up patch to (11) to merge the places in netfslib that update
     i_size on write.

And finally a couple of patches to improve tracing output, but that should
otherwise not affect functionality:

(13) Renumber the NETFS_RREQ_* flags to make the hex values easier to
     interpret by eye, including moving the main status flags down to the
     lowest bits, with IN_PROGRESS in bit 0.

(14) Update the tracepoints in a number of ways, including adding more
     tracepoints into the cifs read/write RPC callback so that differend
     MID_RESPONSE_* values can be differentiated.

* patches from https://lore.kernel.org/20250701163852.2171681-1-dhowells@redhat.com:
  netfs: Update tracepoints in a number of ways
  netfs: Renumber the NETFS_RREQ_* flags to make traces easier to read
  netfs: Merge i_size update functions
  netfs: Fix i_size updating
  smb: client: set missing retry flag in cifs_writev_callback()
  smb: client: set missing retry flag in cifs_readv_callback()
  smb: client: set missing retry flag in smb2_writev_callback()
  netfs: Fix ref leak on inserted extra subreq in write retry
  netfs: Fix looping in wait functions
  netfs: Provide helpers to perform NETFS_RREQ_IN_PROGRESS flag wangling
  netfs: Fix double put of request
  netfs: Fix hang due to missing case in final DIO read result collection

Link: https://lore.kernel.org/20250701163852.2171681-1-dhowells@redhat.com


Signed-off-by: default avatarChristian Brauner <brauner@kernel.org>
parents 8c44dac8 90b3ccf5
Loading
Loading
Loading
Loading
+23 −15
Original line number Diff line number Diff line
@@ -53,20 +53,28 @@ static struct folio *netfs_grab_folio_for_write(struct address_space *mapping,
 * data written into the pagecache until we can find out from the server what
 * the values actually are.
 */
static void netfs_update_i_size(struct netfs_inode *ctx, struct inode *inode,
				loff_t i_size, loff_t pos, size_t copied)
void netfs_update_i_size(struct netfs_inode *ctx, struct inode *inode,
			 loff_t pos, size_t copied)
{
	loff_t i_size, end = pos + copied;
	blkcnt_t add;
	size_t gap;

	if (end <= i_size_read(inode))
		return;

	if (ctx->ops->update_i_size) {
		ctx->ops->update_i_size(inode, pos);
		ctx->ops->update_i_size(inode, end);
		return;
	}

	i_size_write(inode, pos);
	spin_lock(&inode->i_lock);

	i_size = i_size_read(inode);
	if (end > i_size) {
		i_size_write(inode, end);
#if IS_ENABLED(CONFIG_FSCACHE)
	fscache_update_cookie(ctx->cache, NULL, &pos);
		fscache_update_cookie(ctx->cache, NULL, &end);
#endif

		gap = SECTOR_SIZE - (i_size & (SECTOR_SIZE - 1));
@@ -74,10 +82,12 @@ static void netfs_update_i_size(struct netfs_inode *ctx, struct inode *inode,
			add = DIV_ROUND_UP(copied - gap, SECTOR_SIZE);

			inode->i_blocks = min_t(blkcnt_t,
					DIV_ROUND_UP(pos, SECTOR_SIZE),
						DIV_ROUND_UP(end, SECTOR_SIZE),
						inode->i_blocks + add);
		}
	}
	spin_unlock(&inode->i_lock);
}

/**
 * netfs_perform_write - Copy data into the pagecache.
@@ -111,7 +121,7 @@ ssize_t netfs_perform_write(struct kiocb *iocb, struct iov_iter *iter,
	struct folio *folio = NULL, *writethrough = NULL;
	unsigned int bdp_flags = (iocb->ki_flags & IOCB_NOWAIT) ? BDP_ASYNC : 0;
	ssize_t written = 0, ret, ret2;
	loff_t i_size, pos = iocb->ki_pos;
	loff_t pos = iocb->ki_pos;
	size_t max_chunk = mapping_max_folio_size(mapping);
	bool maybe_trouble = false;

@@ -344,10 +354,8 @@ ssize_t netfs_perform_write(struct kiocb *iocb, struct iov_iter *iter,
		flush_dcache_folio(folio);

		/* Update the inode size if we moved the EOF marker */
		netfs_update_i_size(ctx, inode, pos, copied);
		pos += copied;
		i_size = i_size_read(inode);
		if (pos > i_size)
			netfs_update_i_size(ctx, inode, i_size, pos, copied);
		written += copied;

		if (likely(!wreq)) {
+0 −16
Original line number Diff line number Diff line
@@ -9,20 +9,6 @@
#include <linux/uio.h>
#include "internal.h"

static void netfs_cleanup_dio_write(struct netfs_io_request *wreq)
{
	struct inode *inode = wreq->inode;
	unsigned long long end = wreq->start + wreq->transferred;

	if (!wreq->error &&
	    i_size_read(inode) < end) {
		if (wreq->netfs_ops->update_i_size)
			wreq->netfs_ops->update_i_size(inode, end);
		else
			i_size_write(inode, end);
	}
}

/*
 * Perform an unbuffered write where we may have to do an RMW operation on an
 * encrypted file.  This can also be used for direct I/O writes.
@@ -98,7 +84,6 @@ ssize_t netfs_unbuffered_write_iter_locked(struct kiocb *iocb, struct iov_iter *
	if (async)
		wreq->iocb = iocb;
	wreq->len = iov_iter_count(&wreq->buffer.iter);
	wreq->cleanup = netfs_cleanup_dio_write;
	ret = netfs_unbuffered_write(wreq, is_sync_kiocb(iocb), wreq->len);
	if (ret < 0) {
		_debug("begin = %zd", ret);
@@ -106,7 +91,6 @@ ssize_t netfs_unbuffered_write_iter_locked(struct kiocb *iocb, struct iov_iter *
	}

	if (!async) {
		trace_netfs_rreq(wreq, netfs_rreq_trace_wait_ip);
		ret = netfs_wait_for_write(wreq);
		if (ret > 0)
			iocb->ki_pos += ret;
+25 −1
Original line number Diff line number Diff line
@@ -27,6 +27,12 @@ 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);

/*
 * buffered_write.c
 */
void netfs_update_i_size(struct netfs_inode *ctx, struct inode *inode,
			 loff_t pos, size_t copied);

/*
 * main.c
 */
@@ -267,13 +273,31 @@ static inline void netfs_wake_rreq_flag(struct netfs_io_request *rreq,
					enum netfs_rreq_trace trace)
{
	if (test_bit(rreq_flag, &rreq->flags)) {
		trace_netfs_rreq(rreq, trace);
		clear_bit_unlock(rreq_flag, &rreq->flags);
		smp_mb__after_atomic(); /* Set flag before task state */
		trace_netfs_rreq(rreq, trace);
		wake_up(&rreq->waitq);
	}
}

/*
 * Test the NETFS_RREQ_IN_PROGRESS flag, inserting an appropriate barrier.
 */
static inline bool netfs_check_rreq_in_progress(const struct netfs_io_request *rreq)
{
	/* Order read of flags before read of anything else, such as error. */
	return test_bit_acquire(NETFS_RREQ_IN_PROGRESS, &rreq->flags);
}

/*
 * Test the NETFS_SREQ_IN_PROGRESS flag, inserting an appropriate barrier.
 */
static inline bool netfs_check_subreq_in_progress(const struct netfs_io_subrequest *subreq)
{
	/* Order read of flags before read of anything else, such as error. */
	return test_bit_acquire(NETFS_SREQ_IN_PROGRESS, &subreq->flags);
}

/*
 * fscache-cache.c
 */
+3 −3
Original line number Diff line number Diff line
@@ -58,15 +58,15 @@ static int netfs_requests_seq_show(struct seq_file *m, void *v)

	if (v == &netfs_io_requests) {
		seq_puts(m,
			 "REQUEST  OR REF FL ERR  OPS COVERAGE\n"
			 "======== == === == ==== === =========\n"
			 "REQUEST  OR REF FLAG ERR  OPS COVERAGE\n"
			 "======== == === ==== ==== === =========\n"
			 );
		return 0;
	}

	rreq = list_entry(v, struct netfs_io_request, proc_link);
	seq_printf(m,
		   "%08x %s %3d %2lx %4ld %3d @%04llx %llx/%llx",
		   "%08x %s %3d %4lx %4ld %3d @%04llx %llx/%llx",
		   rreq->debug_id,
		   netfs_origins[rreq->origin],
		   refcount_read(&rreq->ref),
+31 −19
Original line number Diff line number Diff line
@@ -356,22 +356,22 @@ void netfs_wait_for_in_progress_stream(struct netfs_io_request *rreq,
	DEFINE_WAIT(myself);

	list_for_each_entry(subreq, &stream->subrequests, rreq_link) {
		if (!test_bit(NETFS_SREQ_IN_PROGRESS, &subreq->flags))
		if (!netfs_check_subreq_in_progress(subreq))
			continue;

		trace_netfs_rreq(rreq, netfs_rreq_trace_wait_queue);
		trace_netfs_rreq(rreq, netfs_rreq_trace_wait_quiesce);
		for (;;) {
			prepare_to_wait(&rreq->waitq, &myself, TASK_UNINTERRUPTIBLE);

			if (!test_bit(NETFS_SREQ_IN_PROGRESS, &subreq->flags))
			if (!netfs_check_subreq_in_progress(subreq))
				break;

			trace_netfs_sreq(subreq, netfs_sreq_trace_wait_for);
			schedule();
			trace_netfs_rreq(rreq, netfs_rreq_trace_woke_queue);
		}
	}

	trace_netfs_rreq(rreq, netfs_rreq_trace_waited_quiesce);
	finish_wait(&rreq->waitq, &myself);
}

@@ -381,7 +381,12 @@ void netfs_wait_for_in_progress_stream(struct netfs_io_request *rreq,
static int netfs_collect_in_app(struct netfs_io_request *rreq,
				bool (*collector)(struct netfs_io_request *rreq))
{
	bool need_collect = false, inactive = true;
	bool need_collect = false, inactive = true, done = true;

	if (!netfs_check_rreq_in_progress(rreq)) {
		trace_netfs_rreq(rreq, netfs_rreq_trace_recollect);
		return 1; /* Done */
	}

	for (int i = 0; i < NR_IO_STREAMS; i++) {
		struct netfs_io_subrequest *subreq;
@@ -395,14 +400,16 @@ static int netfs_collect_in_app(struct netfs_io_request *rreq,
						  struct netfs_io_subrequest,
						  rreq_link);
		if (subreq &&
		    (!test_bit(NETFS_SREQ_IN_PROGRESS, &subreq->flags) ||
		    (!netfs_check_subreq_in_progress(subreq) ||
		     test_bit(NETFS_SREQ_MADE_PROGRESS, &subreq->flags))) {
			need_collect = true;
			break;
		}
		if (subreq || !test_bit(NETFS_RREQ_ALL_QUEUED, &rreq->flags))
			done = false;
	}

	if (!need_collect && !inactive)
	if (!need_collect && !inactive && !done)
		return 0; /* Sleep */

	__set_current_state(TASK_RUNNING);
@@ -423,14 +430,13 @@ static int netfs_collect_in_app(struct netfs_io_request *rreq,
/*
 * Wait for a request to complete, successfully or otherwise.
 */
static ssize_t netfs_wait_for_request(struct netfs_io_request *rreq,
static ssize_t netfs_wait_for_in_progress(struct netfs_io_request *rreq,
					  bool (*collector)(struct netfs_io_request *rreq))
{
	DEFINE_WAIT(myself);
	ssize_t ret;

	for (;;) {
		trace_netfs_rreq(rreq, netfs_rreq_trace_wait_queue);
		prepare_to_wait(&rreq->waitq, &myself, TASK_UNINTERRUPTIBLE);

		if (!test_bit(NETFS_RREQ_OFFLOAD_COLLECTION, &rreq->flags)) {
@@ -440,18 +446,22 @@ static ssize_t netfs_wait_for_request(struct netfs_io_request *rreq,
			case 1:
				goto all_collected;
			case 2:
				if (!netfs_check_rreq_in_progress(rreq))
					break;
				cond_resched();
				continue;
			}
		}

		if (!test_bit(NETFS_RREQ_IN_PROGRESS, &rreq->flags))
		if (!netfs_check_rreq_in_progress(rreq))
			break;

		trace_netfs_rreq(rreq, netfs_rreq_trace_wait_ip);
		schedule();
		trace_netfs_rreq(rreq, netfs_rreq_trace_woke_queue);
	}

all_collected:
	trace_netfs_rreq(rreq, netfs_rreq_trace_waited_ip);
	finish_wait(&rreq->waitq, &myself);

	ret = rreq->error;
@@ -478,12 +488,12 @@ static ssize_t netfs_wait_for_request(struct netfs_io_request *rreq,

ssize_t netfs_wait_for_read(struct netfs_io_request *rreq)
{
	return netfs_wait_for_request(rreq, netfs_read_collection);
	return netfs_wait_for_in_progress(rreq, netfs_read_collection);
}

ssize_t netfs_wait_for_write(struct netfs_io_request *rreq)
{
	return netfs_wait_for_request(rreq, netfs_write_collection);
	return netfs_wait_for_in_progress(rreq, netfs_write_collection);
}

/*
@@ -494,10 +504,8 @@ static void netfs_wait_for_pause(struct netfs_io_request *rreq,
{
	DEFINE_WAIT(myself);

	trace_netfs_rreq(rreq, netfs_rreq_trace_wait_pause);

	for (;;) {
		trace_netfs_rreq(rreq, netfs_rreq_trace_wait_queue);
		trace_netfs_rreq(rreq, netfs_rreq_trace_wait_pause);
		prepare_to_wait(&rreq->waitq, &myself, TASK_UNINTERRUPTIBLE);

		if (!test_bit(NETFS_RREQ_OFFLOAD_COLLECTION, &rreq->flags)) {
@@ -507,19 +515,23 @@ static void netfs_wait_for_pause(struct netfs_io_request *rreq,
			case 1:
				goto all_collected;
			case 2:
				if (!netfs_check_rreq_in_progress(rreq) ||
				    !test_bit(NETFS_RREQ_PAUSE, &rreq->flags))
					break;
				cond_resched();
				continue;
			}
		}

		if (!test_bit(NETFS_RREQ_IN_PROGRESS, &rreq->flags) ||
		if (!netfs_check_rreq_in_progress(rreq) ||
		    !test_bit(NETFS_RREQ_PAUSE, &rreq->flags))
			break;

		schedule();
		trace_netfs_rreq(rreq, netfs_rreq_trace_woke_queue);
	}

all_collected:
	trace_netfs_rreq(rreq, netfs_rreq_trace_waited_pause);
	finish_wait(&rreq->waitq, &myself);
}

Loading