From nobody Sun Nov 24 01:53:26 2024 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CD405231C91 for ; Fri, 8 Nov 2024 17:36:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731087375; cv=none; b=VWyVO7O3i7oa45qokBKR9OawH5ORNEPMjQYvexJBWOAcW1QIufNgLJ76tCb2TcpGcaaqYvkcK2TMKf7ICA78juQ9ligElnvoOLEBDQosVenG7idUm3uSN43C+024L/o/CpFVRYvAvmsDQrIVcv3GL/2J3eI65XTOVzlaAinamsM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731087375; c=relaxed/simple; bh=h3oEXD8an+gSfwAJ1BNpZvfjOrsYuP671z36GJVvdrk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZjyvvGh0TXwpXS8fNqJZbsNTrkr4uSz7hXSQEFi4rG05Y1/GwVEjGDoqVsi1vpyoq9LWr9hkMa8gFQ9x0VZ4RcnMAoC+4IrKD0zjauUJJr4Kg/tzPmuo2t+raZahp4G8eosfpd1ANwmz6YoEzixyPZHOm5/BCnhH25WHxJW/cPY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=RHsYjXPd; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="RHsYjXPd" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1731087372; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Y9YoXfs4wfvSpy+hTW2AnFT9uTx6/UW2p5ODgYs94QM=; b=RHsYjXPdk+Bv11JHoAzDsSODVTvaztky8DKd2W7RoDGD2YQ/UIFytlSL196RBzrvR6QtSZ pt7D4fI+pWqQBKLdT7m/wy7Kruwn8N/npPbdBsQMlV4q92naUTecWKHbsBeEmkGk6p9l9u 7HTNvzAQu2vMpHtqFB2OwRuf1VxcM4A= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-304-N1m8BlRoONe5xG7c0J0v7w-1; Fri, 08 Nov 2024 12:36:05 -0500 X-MC-Unique: N1m8BlRoONe5xG7c0J0v7w-1 X-Mimecast-MFC-AGG-ID: N1m8BlRoONe5xG7c0J0v7w Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 4D6711955F68; Fri, 8 Nov 2024 17:36:01 +0000 (UTC) Received: from warthog.procyon.org.uk.com (unknown [10.42.28.231]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id CFCF7300019E; Fri, 8 Nov 2024 17:35:55 +0000 (UTC) From: David Howells To: Christian Brauner , Steve French , Matthew Wilcox Cc: David Howells , Jeff Layton , Gao Xiang , Dominique Martinet , Marc Dionne , Paulo Alcantara , Shyam Prasad N , Tom Talpey , Eric Van Hensbergen , Ilya Dryomov , netfs@lists.linux.dev, linux-afs@lists.infradead.org, linux-cifs@vger.kernel.org, linux-nfs@vger.kernel.org, ceph-devel@vger.kernel.org, v9fs@lists.linux.dev, linux-erofs@lists.ozlabs.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 27/33] afs: Make {Y,}FS.FetchData an asynchronous operation Date: Fri, 8 Nov 2024 17:32:28 +0000 Message-ID: <20241108173236.1382366-28-dhowells@redhat.com> In-Reply-To: <20241108173236.1382366-1-dhowells@redhat.com> References: <20241108173236.1382366-1-dhowells@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 Content-Type: text/plain; charset="utf-8" Make FS.FetchData and YFS.FetchData an asynchronous operation in that the request is queued in AF_RXRPC and then we return to the caller rather than waiting. Processing of the returning packets is then done inline if it's a synchronous VFS/VM call (readdir, read_folio, sync DIO, prep for write) or offloaded to a workqueue if asynchronous VM calls (eg. readahead, async DIO). This reduces the chain of workqueues invoking workqueues and cuts out some of the overhead, driving rxrpc data extraction and netfslib read collection from a thread that's going to block to completion anyway if possible. The ->done() call op is also split with ->immediate_cancel() handling the cancellation on failure to begin the call and ->done() handling the rest. This means that the AFS async FetchData code doesn't try to terminate the netfs subrequest twice. Signed-off-by: David Howells cc: Marc Dionne cc: linux-afs@lists.infradead.org --- fs/afs/file.c | 126 +++++++++++++++++++++++++++++++++++++----- fs/afs/fs_operation.c | 2 +- fs/afs/fsclient.c | 9 ++- fs/afs/internal.h | 24 ++++++++ fs/afs/main.c | 2 +- fs/afs/rxrpc.c | 25 ++------- fs/afs/vlclient.c | 1 + fs/afs/write.c | 12 ++++ fs/afs/yfsclient.c | 6 +- 9 files changed, 170 insertions(+), 37 deletions(-) diff --git a/fs/afs/file.c b/fs/afs/file.c index b996f4419c0c..c296efebb491 100644 --- a/fs/afs/file.c +++ b/fs/afs/file.c @@ -225,26 +225,111 @@ static void afs_fetch_data_aborted(struct afs_operat= ion *op) afs_fetch_data_notify(op); } =20 -static void afs_fetch_data_put(struct afs_operation *op) -{ - op->fetch.subreq->error =3D afs_op_error(op); -} - const struct afs_operation_ops afs_fetch_data_operation =3D { .issue_afs_rpc =3D afs_fs_fetch_data, .issue_yfs_rpc =3D yfs_fs_fetch_data, .success =3D afs_fetch_data_success, .aborted =3D afs_fetch_data_aborted, .failed =3D afs_fetch_data_notify, - .put =3D afs_fetch_data_put, }; =20 +static void afs_issue_read_call(struct afs_operation *op) +{ + op->call_responded =3D false; + op->call_error =3D 0; + op->call_abort_code =3D 0; + if (test_bit(AFS_SERVER_FL_IS_YFS, &op->server->flags)) + yfs_fs_fetch_data(op); + else + afs_fs_fetch_data(op); +} + +static void afs_end_read(struct afs_operation *op) +{ + if (op->call_responded && op->server) + set_bit(AFS_SERVER_FL_RESPONDING, &op->server->flags); + + if (!afs_op_error(op)) + afs_fetch_data_success(op); + else if (op->cumul_error.aborted) + afs_fetch_data_aborted(op); + else + afs_fetch_data_notify(op); + + afs_end_vnode_operation(op); + afs_put_operation(op); +} + +/* + * Perform I/O processing on an asynchronous call. The work item carries = a ref + * to the call struct that we either need to release or to pass on. + */ +static void afs_read_receive(struct afs_call *call) +{ + struct afs_operation *op =3D call->op; + enum afs_call_state state; + + _enter(""); + + state =3D READ_ONCE(call->state); + if (state =3D=3D AFS_CALL_COMPLETE) + return; + + while (state < AFS_CALL_COMPLETE && READ_ONCE(call->need_attention)) { + WRITE_ONCE(call->need_attention, false); + afs_deliver_to_call(call); + state =3D READ_ONCE(call->state); + } + + if (state < AFS_CALL_COMPLETE) { + netfs_read_subreq_progress(op->fetch.subreq); + if (rxrpc_kernel_check_life(call->net->socket, call->rxcall)) + return; + /* rxrpc terminated the call. */ + afs_set_call_complete(call, call->error, call->abort_code); + } + + op->call_abort_code =3D call->abort_code; + op->call_error =3D call->error; + op->call_responded =3D call->responded; + op->call =3D NULL; + call->op =3D NULL; + afs_put_call(call); + + /* If the call failed, then we need to crank the server rotation + * handle and try the next. + */ + if (afs_select_fileserver(op)) { + afs_issue_read_call(op); + return; + } + + afs_end_read(op); +} + +void afs_fetch_data_async_rx(struct work_struct *work) +{ + struct afs_call *call =3D container_of(work, struct afs_call, async_work); + + afs_read_receive(call); + afs_put_call(call); +} + +void afs_fetch_data_immediate_cancel(struct afs_call *call) +{ + if (call->async) { + afs_get_call(call, afs_call_trace_wake); + if (!queue_work(afs_async_calls, &call->async_work)) + afs_deferred_put_call(call); + flush_work(&call->async_work); + } +} + /* * Fetch file data from the volume. */ -static void afs_read_worker(struct work_struct *work) +static void afs_issue_read(struct netfs_io_subrequest *subreq) { - struct netfs_io_subrequest *subreq =3D container_of(work, struct netfs_io= _subrequest, work); struct afs_operation *op; struct afs_vnode *vnode =3D AFS_FS_I(subreq->rreq->inode); struct key *key =3D subreq->rreq->netfs_priv; @@ -269,13 +354,26 @@ static void afs_read_worker(struct work_struct *work) op->ops =3D &afs_fetch_data_operation; =20 trace_netfs_sreq(subreq, netfs_sreq_trace_submit); - afs_do_sync_operation(op); -} =20 -static void afs_issue_read(struct netfs_io_subrequest *subreq) -{ - INIT_WORK(&subreq->work, afs_read_worker); - queue_work(system_long_wq, &subreq->work); + if (subreq->rreq->origin =3D=3D NETFS_READAHEAD || + subreq->rreq->iocb) { + op->flags |=3D AFS_OPERATION_ASYNC; + + if (!afs_begin_vnode_operation(op)) { + subreq->error =3D afs_put_operation(op); + netfs_read_subreq_terminated(subreq); + return; + } + + if (!afs_select_fileserver(op)) { + afs_end_read(op); + return; + } + + afs_issue_read_call(op); + } else { + afs_do_sync_operation(op); + } } =20 static int afs_init_request(struct netfs_io_request *rreq, struct file *fi= le) diff --git a/fs/afs/fs_operation.c b/fs/afs/fs_operation.c index 8488ff8183fa..0b1338d65ae6 100644 --- a/fs/afs/fs_operation.c +++ b/fs/afs/fs_operation.c @@ -256,7 +256,7 @@ bool afs_begin_vnode_operation(struct afs_operation *op) /* * Tidy up a filesystem cursor and unlock the vnode. */ -static void afs_end_vnode_operation(struct afs_operation *op) +void afs_end_vnode_operation(struct afs_operation *op) { _enter(""); =20 diff --git a/fs/afs/fsclient.c b/fs/afs/fsclient.c index d9d224c95454..1d9ecd5418d8 100644 --- a/fs/afs/fsclient.c +++ b/fs/afs/fsclient.c @@ -352,7 +352,6 @@ static int afs_deliver_fs_fetch_data(struct afs_call *c= all) ret =3D afs_extract_data(call, true); subreq->transferred +=3D count_before - call->iov_len; call->remaining -=3D count_before - call->iov_len; - netfs_read_subreq_progress(subreq); if (ret < 0) return ret; =20 @@ -409,14 +408,18 @@ static int afs_deliver_fs_fetch_data(struct afs_call = *call) static const struct afs_call_type afs_RXFSFetchData =3D { .name =3D "FS.FetchData", .op =3D afs_FS_FetchData, + .async_rx =3D afs_fetch_data_async_rx, .deliver =3D afs_deliver_fs_fetch_data, + .immediate_cancel =3D afs_fetch_data_immediate_cancel, .destructor =3D afs_flat_call_destructor, }; =20 static const struct afs_call_type afs_RXFSFetchData64 =3D { .name =3D "FS.FetchData64", .op =3D afs_FS_FetchData64, + .async_rx =3D afs_fetch_data_async_rx, .deliver =3D afs_deliver_fs_fetch_data, + .immediate_cancel =3D afs_fetch_data_immediate_cancel, .destructor =3D afs_flat_call_destructor, }; =20 @@ -436,6 +439,9 @@ static void afs_fs_fetch_data64(struct afs_operation *o= p) if (!call) return afs_op_nomem(op); =20 + if (op->flags & AFS_OPERATION_ASYNC) + call->async =3D true; + /* marshall the parameters */ bp =3D call->request; bp[0] =3D htonl(FSFETCHDATA64); @@ -1730,6 +1736,7 @@ static const struct afs_call_type afs_RXFSGetCapabili= ties =3D { .op =3D afs_FS_GetCapabilities, .deliver =3D afs_deliver_fs_get_capabilities, .done =3D afs_fileserver_probe_result, + .immediate_cancel =3D afs_fileserver_probe_result, .destructor =3D afs_fs_get_capabilities_destructor, }; =20 diff --git a/fs/afs/internal.h b/fs/afs/internal.h index 96fc466efd10..cd2c4f85117d 100644 --- a/fs/afs/internal.h +++ b/fs/afs/internal.h @@ -202,11 +202,17 @@ struct afs_call_type { /* clean up a call */ void (*destructor)(struct afs_call *call); =20 + /* Async receive processing function */ + void (*async_rx)(struct work_struct *work); + /* Work function */ void (*work)(struct work_struct *work); =20 /* Call done function (gets called immediately on success or failure) */ void (*done)(struct afs_call *call); + + /* Handle a call being immediately cancelled. */ + void (*immediate_cancel)(struct afs_call *call); }; =20 /* @@ -942,6 +948,7 @@ struct afs_operation { #define AFS_OPERATION_TRIED_ALL 0x0400 /* Set if we've tried all the file= servers */ #define AFS_OPERATION_RETRY_SERVER 0x0800 /* Set if we should retry the cu= rrent server */ #define AFS_OPERATION_DIR_CONFLICT 0x1000 /* Set if we detected a 3rd-part= y dir change */ +#define AFS_OPERATION_ASYNC 0x2000 /* Set if should run asynchronously */ }; =20 /* @@ -1104,6 +1111,8 @@ extern int afs_cache_wb_key(struct afs_vnode *, struc= t afs_file *); extern void afs_put_wb_key(struct afs_wb_key *); extern int afs_open(struct inode *, struct file *); extern int afs_release(struct inode *, struct file *); +void afs_fetch_data_async_rx(struct work_struct *work); +void afs_fetch_data_immediate_cancel(struct afs_call *call); =20 /* * flock.c @@ -1155,6 +1164,7 @@ extern void afs_fs_store_acl(struct afs_operation *); extern struct afs_operation *afs_alloc_operation(struct key *, struct afs_= volume *); extern int afs_put_operation(struct afs_operation *); extern bool afs_begin_vnode_operation(struct afs_operation *); +extern void afs_end_vnode_operation(struct afs_operation *op); extern void afs_wait_for_operation(struct afs_operation *); extern int afs_do_sync_operation(struct afs_operation *); =20 @@ -1326,6 +1336,7 @@ extern void afs_charge_preallocation(struct work_stru= ct *); extern void afs_put_call(struct afs_call *); void afs_deferred_put_call(struct afs_call *call); void afs_make_call(struct afs_call *call, gfp_t gfp); +void afs_deliver_to_call(struct afs_call *call); void afs_wait_for_call_to_complete(struct afs_call *call); extern struct afs_call *afs_alloc_flat_call(struct afs_net *, const struct afs_call_type *, @@ -1336,6 +1347,19 @@ extern void afs_send_simple_reply(struct afs_call *,= const void *, size_t); extern int afs_extract_data(struct afs_call *, bool); extern int afs_protocol_error(struct afs_call *, enum afs_eproto_cause); =20 +static inline struct afs_call *afs_get_call(struct afs_call *call, + enum afs_call_trace why) +{ + int r; + + __refcount_inc(&call->ref, &r); + + trace_afs_call(call->debug_id, why, r + 1, + atomic_read(&call->net->nr_outstanding_calls), + __builtin_return_address(0)); + return call; +} + static inline void afs_see_call(struct afs_call *call, enum afs_call_trace= why) { int r =3D refcount_read(&call->ref); diff --git a/fs/afs/main.c b/fs/afs/main.c index a14f6013e316..1ae0067f772d 100644 --- a/fs/afs/main.c +++ b/fs/afs/main.c @@ -177,7 +177,7 @@ static int __init afs_init(void) afs_wq =3D alloc_workqueue("afs", 0, 0); if (!afs_wq) goto error_afs_wq; - afs_async_calls =3D alloc_workqueue("kafsd", WQ_MEM_RECLAIM, 0); + afs_async_calls =3D alloc_workqueue("kafsd", WQ_MEM_RECLAIM | WQ_UNBOUND,= 0); if (!afs_async_calls) goto error_async; afs_lock_manager =3D alloc_workqueue("kafs_lockd", WQ_MEM_RECLAIM, 0); diff --git a/fs/afs/rxrpc.c b/fs/afs/rxrpc.c index a122c6366ce1..886416ea1d96 100644 --- a/fs/afs/rxrpc.c +++ b/fs/afs/rxrpc.c @@ -149,7 +149,8 @@ static struct afs_call *afs_alloc_call(struct afs_net *= net, call->net =3D net; call->debug_id =3D atomic_inc_return(&rxrpc_debug_id); refcount_set(&call->ref, 1); - INIT_WORK(&call->async_work, afs_process_async_call); + INIT_WORK(&call->async_work, type->async_rx ?: afs_process_async_call); + INIT_WORK(&call->work, call->type->work); INIT_WORK(&call->free_work, afs_deferred_free_worker); init_waitqueue_head(&call->waitq); spin_lock_init(&call->state_lock); @@ -235,27 +236,12 @@ void afs_deferred_put_call(struct afs_call *call) schedule_work(&call->free_work); } =20 -static struct afs_call *afs_get_call(struct afs_call *call, - enum afs_call_trace why) -{ - int r; - - __refcount_inc(&call->ref, &r); - - trace_afs_call(call->debug_id, why, r + 1, - atomic_read(&call->net->nr_outstanding_calls), - __builtin_return_address(0)); - return call; -} - /* * Queue the call for actual work. */ static void afs_queue_call_work(struct afs_call *call) { if (call->type->work) { - INIT_WORK(&call->work, call->type->work); - afs_get_call(call, afs_call_trace_work); if (!queue_work(afs_wq, &call->work)) afs_put_call(call); @@ -452,8 +438,8 @@ void afs_make_call(struct afs_call *call, gfp_t gfp) error_kill_call: if (call->async) afs_see_call(call, afs_call_trace_async_kill); - if (call->type->done) - call->type->done(call); + if (call->type->immediate_cancel) + call->type->immediate_cancel(call); =20 /* We need to dispose of the extra ref we grabbed for an async call. * The call, however, might be queued on afs_async_calls and we need to @@ -508,7 +494,7 @@ static void afs_log_error(struct afs_call *call, s32 re= mote_abort) /* * deliver messages to a call */ -static void afs_deliver_to_call(struct afs_call *call) +void afs_deliver_to_call(struct afs_call *call) { enum afs_call_state state; size_t len; @@ -809,6 +795,7 @@ static int afs_deliver_cm_op_id(struct afs_call *call) return -ENOTSUPP; =20 trace_afs_cb_call(call); + call->work.func =3D call->type->work; =20 /* pass responsibility for the remainer of this message off to the * cache manager op */ diff --git a/fs/afs/vlclient.c b/fs/afs/vlclient.c index cac75f89b64a..adc617a82a86 100644 --- a/fs/afs/vlclient.c +++ b/fs/afs/vlclient.c @@ -370,6 +370,7 @@ static const struct afs_call_type afs_RXVLGetCapabiliti= es =3D { .name =3D "VL.GetCapabilities", .op =3D afs_VL_GetCapabilities, .deliver =3D afs_deliver_vl_get_capabilities, + .immediate_cancel =3D afs_vlserver_probe_result, .done =3D afs_vlserver_probe_result, .destructor =3D afs_destroy_vl_get_capabilities, }; diff --git a/fs/afs/write.c b/fs/afs/write.c index 17d188aaf101..e87b55792aa8 100644 --- a/fs/afs/write.c +++ b/fs/afs/write.c @@ -193,6 +193,18 @@ void afs_retry_request(struct netfs_io_request *wreq, = struct netfs_io_stream *st list_first_entry(&stream->subrequests, struct netfs_io_subrequest, rreq_link); =20 + switch (wreq->origin) { + case NETFS_READAHEAD: + case NETFS_READPAGE: + case NETFS_READ_GAPS: + case NETFS_READ_SINGLE: + case NETFS_READ_FOR_WRITE: + case NETFS_DIO_READ: + return; + default: + break; + } + switch (subreq->error) { case -EACCES: case -EPERM: diff --git a/fs/afs/yfsclient.c b/fs/afs/yfsclient.c index 3718d852fabc..f57c089f26ee 100644 --- a/fs/afs/yfsclient.c +++ b/fs/afs/yfsclient.c @@ -397,7 +397,6 @@ static int yfs_deliver_fs_fetch_data64(struct afs_call = *call) =20 ret =3D afs_extract_data(call, true); subreq->transferred +=3D count_before - call->iov_len; - netfs_read_subreq_progress(subreq); if (ret < 0) return ret; =20 @@ -457,7 +456,9 @@ static int yfs_deliver_fs_fetch_data64(struct afs_call = *call) static const struct afs_call_type yfs_RXYFSFetchData64 =3D { .name =3D "YFS.FetchData64", .op =3D yfs_FS_FetchData64, + .async_rx =3D afs_fetch_data_async_rx, .deliver =3D yfs_deliver_fs_fetch_data64, + .immediate_cancel =3D afs_fetch_data_immediate_cancel, .destructor =3D afs_flat_call_destructor, }; =20 @@ -486,6 +487,9 @@ void yfs_fs_fetch_data(struct afs_operation *op) if (!call) return afs_op_nomem(op); =20 + if (op->flags & AFS_OPERATION_ASYNC) + call->async =3D true; + /* marshall the parameters */ bp =3D call->request; bp =3D xdr_encode_u32(bp, YFSFETCHDATA64);