From nobody Thu Apr 3 10:38:49 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=quarantine dis=none) header.from=qq.com ARC-Seal: i=1; a=rsa-sha256; t=1742060673; cv=none; d=zohomail.com; s=zohoarc; b=MObPvq0pSw/x9a+VlScXUpBqmIhyeloNjLBUj9FtBrn1P2N2kc8Wjjv+1+FCx4Ym+LtGaf//YlN30rsRcfPP8lgOnrtE6O6WxLae0GNe9kMRXHBpXolKLMTaevkLif5iKmToIwHP3M7EE0t3S9F3Dw7Ly1M7cDCXbSxXZa9Idfc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1742060673; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=zcrxujq8oAr4C4FYOrWGAxou0NszWeHnYrryhZrzvGk=; b=Rq0O2PEOT720ZOFfBXjlnAuCmcHXolzN8VIxPzrlJgzJoT6qpN4RMnIMXJonWLKzGGrzhJ75YkpfCNWd9reV9Gwhh4L5nhhEicjJaG/wlxoZ4mRqxCaLH8AQLUBOpz0RYNSi43yNjYw81oNNP5foH4vL+K6U/siH6y4zuKu8P6I= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1742060673919444.1099660810785; Sat, 15 Mar 2025 10:44:33 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ttVXY-00087b-8j; Sat, 15 Mar 2025 13:43:17 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ttVXL-0007yK-TR; Sat, 15 Mar 2025 13:43:07 -0400 Received: from xmbghk7.mail.qq.com ([43.163.128.54]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ttVXG-00021n-FK; Sat, 15 Mar 2025 13:43:03 -0400 Received: from localhost.localdomain ([120.46.176.49]) by newxmesmtplogicsvrszb16-1.qq.com (NewEsmtp) with SMTP id 799AC488; Sun, 16 Mar 2025 01:30:25 +0800 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qq.com; s=s201512; t=1742060568; bh=zcrxujq8oAr4C4FYOrWGAxou0NszWeHnYrryhZrzvGk=; h=From:To:Cc:Subject:Date; b=rRHZFSLTyjZl39xX2mS+swkFcS6YphWhQcVQbb2pUM5alLvoTgIKK37eF+fBlRwAD GGC7/ankQlWz0aH3wBf11KfPBGPOAc6Ig1GoJKc5TWzKv35heK3weeVVxffBP9PDH3 RAWTZDgNS5ROBaL2kwYLimfxDxm8ODMpQvDLKR/Y= X-QQ-mid: xmsmtpt1742059825til0gwr29 Message-ID: X-QQ-XMAILINFO: NhJmSc9h8ndd8ukIdUi1fCOcmz27QYj9qtVMSPLvPeYC6fX6Aosj0defr9LV6w bIpWp4cWvTcNQRuao3bDVedjCSuUxr+xs3ZIf7CIc9LnuMGiR+V4pmW04JwktJmKoz6I42nBN4D9 Q2xjfjw6UKd9zNQsTFowI7+YvBbEWc24Pa02MP5Rka6JtKq7IiYEOSifYx39dhNA2Y7IqDw1wOrO mQDMAL5D1XM20qt7wTTTkRT1+wDKwkuEc+7J8vojsUOyL1kSxoEJmnZbGqL0YYkXC/Ln0fXFLN2G ewCMJq2VaXo91eBKxQQebOBBxrmVieJ50LnNKXoy/Ux28m2pYpiBYbwFavGBeRAp4NoqPjZzW2CU 9ZXXIh0lsKkmINdFf1Ul8gnRPVu/ICcE3GdMvE9/esakNFLTnw9JTrzCV+kDwtzYrhsx2+Njdsyn ZzTLHY9sexatbfZekgQLfV/30YU0fqfSZg5ruLxfEcjjhIxfEm9RCBaCMngNZPnc/EgFKC3SRRa+ WFLTnHFHHrPGpuHzdhq0qGYtz82G1gfLbslWzCoA3ic2XwPU/YPvyHs6tJI/xYEVnh2qtVAEefQY dPdW8lS0lZfZ2gXkgIQUGG/GIyq2DZBDDeDHUcUaKqxKgqyiJU3/zGpPYiuAjGd7I5VgME4Q+XBz BmcXbiw/7RyNFHN5XE1P8pGQve9Wyldk/2WBH+cfML3ZKancB8hw3opPQdlvRy1h0qleioGwv/Ju XBhLAPFAJBYGPySREJg7WYb4vthrYmzBCik3T1dQdS49wvD+oTwQgDj8FKE5LyJZUqhT13FD2SuK ianIiAXiYfqZ0xy2uWpy4jk5TpvQvMj7RK+WzzSZqY19XPoE4wUtR9oWmWTJiad1RhE4MOGG0qo6 EbbXL3E+M1y3D4nYoAbeh+Xj6NBTvxtHvrk8MMLI3ZOodWglbEIm/RDnUCmOMAz8Q3/Wuc6IYpN0 yzpwkhbfM/9t+Q8qmXsg== X-QQ-XMRINFO: Nq+8W0+stu50PRdwbJxPCL0= From: saz97 To: qemu-devel@nongnu.org Cc: hreitz@redhat.com, kwolf@redhat.com, stefanha@redhat.com, qemu-block@nongnu.org, saz97 Subject: [PATCH RFC v2] Integration coroutines into fuse export Date: Sun, 16 Mar 2025 01:30:20 +0800 X-OQ-MSGID: <20250315173020.52011-1-sa.z@qq.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=43.163.128.54; envelope-from=sa.z@qq.com; helo=xmbghk7.mail.qq.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @qq.com) X-ZM-MESSAGEID: 1742060677388019100 Content-Type: text/plain; charset="utf-8" Signed-off-by: Changzhi Xie This commit refactors the FUSE export to process read and write operations using coroutines, improving concurrency and avoiding blocking the main loop. The main changes include: 1. Introduce FuseIORequest structure to encapsulate I/O parameters and sta= te 2. Move read/write processing into coroutine fuse_read_coroutine and fuse_= write_coroutine 3. Use blk_co_pread/pwrite for async block layer access --- block/export/fuse.c | 189 +++++++++++++++++++++++++++++++------------- 1 file changed, 132 insertions(+), 57 deletions(-) diff --git a/block/export/fuse.c b/block/export/fuse.c index 465cc9891d..3314f64706 100644 --- a/block/export/fuse.c +++ b/block/export/fuse.c @@ -39,6 +39,7 @@ =20 #ifdef __linux__ #include +#include #endif =20 /* Prevent overly long bounce buffer allocations */ @@ -49,7 +50,6 @@ typedef struct FuseExport { BlockExport common; =20 struct fuse_session *fuse_session; - struct fuse_buf fuse_buf; unsigned int in_flight; /* atomic */ bool mounted, fd_handler_set_up; =20 @@ -64,6 +64,14 @@ typedef struct FuseExport { gid_t st_gid; } FuseExport; =20 +typedef struct FuseIORequest { + fuse_req_t req; + size_t size; + off_t offset; + FuseExport *exp; + char *write_buf; +} FuseIORequest; + static GHashTable *exports; static const struct fuse_lowlevel_ops fuse_ops; =20 @@ -288,6 +296,7 @@ fail: static void read_from_fuse_export(void *opaque) { FuseExport *exp =3D opaque; + struct fuse_buf buf =3D {}; int ret; =20 blk_exp_ref(&exp->common); @@ -295,20 +304,30 @@ static void read_from_fuse_export(void *opaque) qatomic_inc(&exp->in_flight); =20 do { - ret =3D fuse_session_receive_buf(exp->fuse_session, &exp->fuse_buf= ); + ret =3D fuse_session_receive_buf(exp->fuse_session, &buf); } while (ret =3D=3D -EINTR); if (ret < 0) { goto out; } =20 - fuse_session_process_buf(exp->fuse_session, &exp->fuse_buf); + fuse_session_process_buf(exp->fuse_session, &buf); =20 out: + struct fuse_in_header *in =3D (struct fuse_in_header *)buf.mem; + + if (in->opcode =3D=3D FUSE_WRITE || in->opcode =3D=3D FUSE_READ) { + g_free(buf.mem); + return; + } + if (qatomic_fetch_dec(&exp->in_flight) =3D=3D 1) { aio_wait_kick(); /* wake AIO_WAIT_WHILE() */ } =20 blk_exp_unref(&exp->common); + + g_free(buf.mem); + } =20 static void fuse_export_shutdown(BlockExport *blk_exp) @@ -347,7 +366,6 @@ static void fuse_export_delete(BlockExport *blk_exp) fuse_session_destroy(exp->fuse_session); } =20 - free(exp->fuse_buf.mem); g_free(exp->mountpoint); } =20 @@ -570,102 +588,159 @@ static void fuse_open(fuse_req_t req, fuse_ino_t in= ode, fuse_reply_open(req, fi); } =20 -/** - * Handle client reads from the exported image. - */ -static void fuse_read(fuse_req_t req, fuse_ino_t inode, - size_t size, off_t offset, struct fuse_file_info *fi) +static void coroutine_fn fuse_read_coroutine(void *opaque) { - FuseExport *exp =3D fuse_req_userdata(req); + FuseIORequest *io_req =3D opaque; + FuseExport *exp =3D io_req->exp; int64_t length; - void *buf; + void *buffer; int ret; =20 - /* Limited by max_read, should not happen */ - if (size > FUSE_MAX_BOUNCE_BYTES) { - fuse_reply_err(req, EINVAL); - return; + if (io_req->size > FUSE_MAX_BOUNCE_BYTES) { + fuse_reply_err(io_req->req, EINVAL); + goto cleanup; } =20 - /** - * Clients will expect short reads at EOF, so we have to limit - * offset+size to the image length. - */ length =3D blk_getlength(exp->common.blk); if (length < 0) { - fuse_reply_err(req, -length); - return; + fuse_reply_err(io_req->req, -length); + goto cleanup; } =20 - if (offset + size > length) { - size =3D length - offset; + if (io_req->offset + io_req->size > length) { + io_req->size =3D length - io_req->offset; } =20 - buf =3D qemu_try_blockalign(blk_bs(exp->common.blk), size); - if (!buf) { - fuse_reply_err(req, ENOMEM); - return; + if (io_req->size =3D=3D 0) { + fuse_reply_buf(io_req->req, NULL, 0); + goto cleanup; } =20 - ret =3D blk_pread(exp->common.blk, offset, size, buf, 0); + buffer =3D qemu_try_blockalign(blk_bs(exp->common.blk), io_req->size); + if (!buffer) { + fuse_reply_err(io_req->req, ENOMEM); + goto cleanup; + } + + ret =3D blk_co_pread(exp->common.blk, io_req->offset, + io_req->size, buffer, 0); if (ret >=3D 0) { - fuse_reply_buf(req, buf, size); + fuse_reply_buf(io_req->req, buffer, io_req->size); } else { - fuse_reply_err(req, -ret); + fuse_reply_err(io_req->req, -ret); + } + + qemu_vfree(buffer); + +cleanup: + if (qatomic_fetch_dec(&exp->in_flight) =3D=3D 1) { + aio_wait_kick(); /* wake AIO_WAIT_WHILE() */ } =20 - qemu_vfree(buf); + blk_exp_unref(&exp->common); + + g_free(io_req); } =20 -/** - * Handle client writes to the exported image. - */ -static void fuse_write(fuse_req_t req, fuse_ino_t inode, const char *buf, - size_t size, off_t offset, struct fuse_file_info *f= i) +static void coroutine_fn fuse_write_coroutine(void *opaque) { - FuseExport *exp =3D fuse_req_userdata(req); + FuseIORequest *io_req =3D opaque; + FuseExport *exp =3D io_req->exp; int64_t length; int ret; =20 - /* Limited by max_write, should not happen */ - if (size > BDRV_REQUEST_MAX_BYTES) { - fuse_reply_err(req, EINVAL); - return; + if (io_req->size > BDRV_REQUEST_MAX_BYTES) { + fuse_reply_err(io_req->req, EINVAL); + goto cleanup; } =20 if (!exp->writable) { - fuse_reply_err(req, EACCES); - return; + fuse_reply_err(io_req->req, EACCES); + goto cleanup; } =20 - /** - * Clients will expect short writes at EOF, so we have to limit - * offset+size to the image length. - */ length =3D blk_getlength(exp->common.blk); if (length < 0) { - fuse_reply_err(req, -length); - return; + fuse_reply_err(io_req->req, -length); + goto cleanup; } =20 - if (offset + size > length) { + if (io_req->offset + io_req->size > length) { if (exp->growable) { - ret =3D fuse_do_truncate(exp, offset + size, true, PREALLOC_MO= DE_OFF); + ret =3D fuse_do_truncate(exp, io_req->offset + io_req->size, + true, PREALLOC_MODE_OFF); if (ret < 0) { - fuse_reply_err(req, -ret); - return; + fuse_reply_err(io_req->req, -ret); + goto cleanup; } } else { - size =3D length - offset; + io_req->size =3D MAX(0, length - io_req->offset); + if (io_req->size =3D=3D 0) { + fuse_reply_write(io_req->req, 0); + goto cleanup; + } } } =20 - ret =3D blk_pwrite(exp->common.blk, offset, size, buf, 0); + ret =3D blk_co_pwrite(exp->common.blk, io_req->offset, io_req->size, + io_req->write_buf, 0); if (ret >=3D 0) { - fuse_reply_write(req, size); + fuse_reply_write(io_req->req, io_req->size); } else { - fuse_reply_err(req, -ret); + fuse_reply_err(io_req->req, -ret); + } + +cleanup: + if (qatomic_fetch_dec(&exp->in_flight) =3D=3D 1) { + aio_wait_kick(); /* wake AIO_WAIT_WHILE() */ } + + blk_exp_unref(&exp->common); + + g_free(io_req->write_buf); + g_free(io_req); +} + +/** + * Handle client reads from the exported image. + */ +static void fuse_read(fuse_req_t req, fuse_ino_t inode, + size_t size, off_t offset, struct fuse_file_info *fi) +{ + FuseExport *exp =3D fuse_req_userdata(req); + FuseIORequest *io_req =3D g_new(FuseIORequest, 1); + + *io_req =3D (FuseIORequest) { + .req =3D req, + .size =3D size, + .offset =3D offset, + .exp =3D exp, + }; + + Coroutine *co =3D qemu_coroutine_create(fuse_read_coroutine, io_req); + qemu_coroutine_enter(co); +} + + +/** + * Handle client writes to the exported image. + */ +static void fuse_write(fuse_req_t req, fuse_ino_t inode, const char *buf, + size_t size, off_t offset, struct fuse_file_info *f= i) +{ + FuseExport *exp =3D fuse_req_userdata(req); + FuseIORequest *io_req =3D g_new(FuseIORequest, 1); + + *io_req =3D (FuseIORequest) { + .req =3D req, + .size =3D size, + .offset =3D offset, + .exp =3D exp, + .write_buf =3D g_memdup2_qemu(buf, size), + }; + + Coroutine *co =3D qemu_coroutine_create(fuse_write_coroutine, io_req); + qemu_coroutine_enter(co); } =20 /** --=20 2.34.1