From nobody Fri Dec 19 03:11:15 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C4829C2BB3F for ; Mon, 20 Nov 2023 04:15:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229596AbjKTEPm (ORCPT ); Sun, 19 Nov 2023 23:15:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40596 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231836AbjKTEPf (ORCPT ); Sun, 19 Nov 2023 23:15:35 -0500 Received: from mail-pl1-x635.google.com (mail-pl1-x635.google.com [IPv6:2607:f8b0:4864:20::635]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 96094194 for ; Sun, 19 Nov 2023 20:15:31 -0800 (PST) Received: by mail-pl1-x635.google.com with SMTP id d9443c01a7336-1cf6373ce31so1723415ad.0 for ; Sun, 19 Nov 2023 20:15:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1700453731; x=1701058531; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=aS5flV+CfxePU4FZE8c9ZQB+Fy6Gmv6dv/iJq9bZJTo=; b=hpBQSCEu6DKzxrN2poyR/ek7s6jFa2UwBJG7ekUQi/iHIG/TaGw0qPTzcN3iZf5jPc 6Uurpu/F+lY4SdJ1qtu66s82xWBeX3an1IxbV5CtG9A7rsiRLKgdb5NrpzTsFSZEvA2e k/Lbldo9XAXcsTOVq4ihR/0koh3XcwyW4XBg+nNaihJla0yKfcZtLNd08Kg6sAPytLst u2g5tm4KAhGCwAU0eQZJ8KTJ4/fI/K71ewXD4ZPZD4hJX6fRKmgwkfcdCHOcNiPhPcLu zTnR1Wx8QibA9cAExX9CjqQ74ysBncKF+jjAjHdw2dbSOQZq5x3wFZ9T3fZNOm76MHs0 7OvQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700453731; x=1701058531; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=aS5flV+CfxePU4FZE8c9ZQB+Fy6Gmv6dv/iJq9bZJTo=; b=LgvNHUFiIyCeQws1qQWjZpk/K2EWRHT/hhAqYiGPNTUtUFCv/TKGEG5YIPU9EakwDy 3I3q7gEY1M5dSctFQ56PSbdOsOET9CbjqKbLioqBF2HgPHDuW5ExZvxBYPFFGUFgBDDZ ba77dyGqXPtKt4sfoDpxC+obW5Z7Q8RSOI477JqqE3qiRivFyP1hzpltYYtKJLSoI9WJ JrVf6g4zOrtuXB+hyrOFqHHyKBa/oYF0qfAT92J2FarSMn29p1eounQibKlb46IpeL4t bNHkKEsuq/SX+Otbtt0ZFL+Mr5xNDKUPcrV87UUtt5mmcmf6El5IFdqKpQZe43VLm14c 6bYQ== X-Gm-Message-State: AOJu0YzUIshfIM6dUS4AVCAJvq41iQJCkUt9rafjNsCP/NDLngQlU7C5 quKJptDuqoiORA/xvi4fEXWfhA== X-Google-Smtp-Source: AGHT+IHwWw9of5GVxBHPI7Gz5bET2Khh8mqHNxHnk/cuQWp7L3Pl1nAS6XV+csqH0/RZmAOyby2LSg== X-Received: by 2002:a17:902:fac4:b0:1cc:4b3d:1a8d with SMTP id ld4-20020a170902fac400b001cc4b3d1a8dmr14850687plb.17.1700453731015; Sun, 19 Nov 2023 20:15:31 -0800 (PST) Received: from C02G705SMD6V.bytedance.net ([61.213.176.5]) by smtp.gmail.com with ESMTPSA id h18-20020a170902f7d200b001cc4e477861sm5065266plw.212.2023.11.19.20.15.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Nov 2023 20:15:30 -0800 (PST) From: Jia Zhu To: dhowells@redhat.com, linux-cachefs@redhat.com Cc: linux-erofs@lists.ozlabs.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, jefflexu@linux.alibaba.com, hsiangkao@linux.alibaba.com, Jia Zhu Subject: [PATCH V6 RESEND 3/5] cachefiles: resend an open request if the read request's object is closed Date: Mon, 20 Nov 2023 12:14:20 +0800 Message-Id: <20231120041422.75170-4-zhujia.zj@bytedance.com> X-Mailer: git-send-email 2.37.1 (Apple Git-137.1) In-Reply-To: <20231120041422.75170-1-zhujia.zj@bytedance.com> References: <20231120041422.75170-1-zhujia.zj@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" When an anonymous fd is closed by user daemon, if there is a new read request for this file comes up, the anonymous fd should be re-opened to handle that read request rather than fail it directly. 1. Introduce reopening state for objects that are closed but have inflight/subsequent read requests. 2. No longer flush READ requests but only CLOSE requests when anonymous fd is closed. 3. Enqueue the reopen work to workqueue, thus user daemon could get rid of daemon_read context and handle that request smoothly. Otherwise, the user daemon will send a reopen request and wait for itself to process the request. Signed-off-by: Jia Zhu Reviewed-by: Jingbo Xu --- fs/cachefiles/internal.h | 3 ++ fs/cachefiles/ondemand.c | 98 ++++++++++++++++++++++++++++------------ 2 files changed, 72 insertions(+), 29 deletions(-) diff --git a/fs/cachefiles/internal.h b/fs/cachefiles/internal.h index b0fe76964bc0..b9a90f1a0c01 100644 --- a/fs/cachefiles/internal.h +++ b/fs/cachefiles/internal.h @@ -47,9 +47,11 @@ struct cachefiles_volume { enum cachefiles_object_state { CACHEFILES_ONDEMAND_OBJSTATE_CLOSE, /* Anonymous fd closed by daemon or i= nitial state */ CACHEFILES_ONDEMAND_OBJSTATE_OPEN, /* Anonymous fd associated with object= is available */ + CACHEFILES_ONDEMAND_OBJSTATE_REOPENING, /* Object that was closed and is = being reopened. */ }; =20 struct cachefiles_ondemand_info { + struct work_struct ondemand_work; int ondemand_id; enum cachefiles_object_state state; struct cachefiles_object *object; @@ -326,6 +328,7 @@ cachefiles_ondemand_set_object_##_state(struct cachefil= es_object *object) \ =20 CACHEFILES_OBJECT_STATE_FUNCS(open, OPEN); CACHEFILES_OBJECT_STATE_FUNCS(close, CLOSE); +CACHEFILES_OBJECT_STATE_FUNCS(reopening, REOPENING); #else static inline ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_ca= che *cache, char __user *_buffer, size_t buflen) diff --git a/fs/cachefiles/ondemand.c b/fs/cachefiles/ondemand.c index deb7e3007aa1..8e130de952f7 100644 --- a/fs/cachefiles/ondemand.c +++ b/fs/cachefiles/ondemand.c @@ -18,14 +18,10 @@ static int cachefiles_ondemand_fd_release(struct inode = *inode, info->ondemand_id =3D CACHEFILES_ONDEMAND_ID_CLOSED; cachefiles_ondemand_set_object_close(object); =20 - /* - * Flush all pending READ requests since their completion depends on - * anon_fd. - */ - xas_for_each(&xas, req, ULONG_MAX) { + /* Only flush CACHEFILES_REQ_NEW marked req to avoid race with daemon_rea= d */ + xas_for_each_marked(&xas, req, ULONG_MAX, CACHEFILES_REQ_NEW) { if (req->msg.object_id =3D=3D object_id && - req->msg.opcode =3D=3D CACHEFILES_OP_READ) { - req->error =3D -EIO; + req->msg.opcode =3D=3D CACHEFILES_OP_CLOSE) { complete(&req->done); xas_store(&xas, NULL); } @@ -179,6 +175,7 @@ int cachefiles_ondemand_copen(struct cachefiles_cache *= cache, char *args) trace_cachefiles_ondemand_copen(req->object, id, size); =20 cachefiles_ondemand_set_object_open(req->object); + wake_up_all(&cache->daemon_pollwq); =20 out: complete(&req->done); @@ -222,7 +219,6 @@ static int cachefiles_ondemand_get_fd(struct cachefiles= _req *req) =20 load =3D (void *)req->msg.data; load->fd =3D fd; - req->msg.object_id =3D object_id; object->ondemand->ondemand_id =3D object_id; =20 cachefiles_get_unbind_pincount(cache); @@ -238,6 +234,43 @@ static int cachefiles_ondemand_get_fd(struct cachefile= s_req *req) return ret; } =20 +static void ondemand_object_worker(struct work_struct *work) +{ + struct cachefiles_ondemand_info *info =3D + container_of(work, struct cachefiles_ondemand_info, ondemand_work); + + cachefiles_ondemand_init_object(info->object); +} + +/* + * If there are any inflight or subsequent READ requests on the + * closed object, reopen it. + * Skip read requests whose related object is reopening. + */ +static struct cachefiles_req *cachefiles_ondemand_select_req(struct xa_sta= te *xas, + unsigned long xa_max) +{ + struct cachefiles_req *req; + struct cachefiles_object *object; + struct cachefiles_ondemand_info *info; + + xas_for_each_marked(xas, req, xa_max, CACHEFILES_REQ_NEW) { + if (req->msg.opcode !=3D CACHEFILES_OP_READ) + return req; + object =3D req->object; + info =3D object->ondemand; + if (cachefiles_ondemand_object_is_close(object)) { + cachefiles_ondemand_set_object_reopening(object); + queue_work(fscache_wq, &info->ondemand_work); + continue; + } + if (cachefiles_ondemand_object_is_reopening(object)) + continue; + return req; + } + return NULL; +} + ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache, char __user *_buffer, size_t buflen) { @@ -248,16 +281,16 @@ ssize_t cachefiles_ondemand_daemon_read(struct cachef= iles_cache *cache, int ret =3D 0; XA_STATE(xas, &cache->reqs, cache->req_id_next); =20 + xa_lock(&cache->reqs); /* * Cyclically search for a request that has not ever been processed, * to prevent requests from being processed repeatedly, and make * request distribution fair. */ - xa_lock(&cache->reqs); - req =3D xas_find_marked(&xas, UINT_MAX, CACHEFILES_REQ_NEW); + req =3D cachefiles_ondemand_select_req(&xas, ULONG_MAX); if (!req && cache->req_id_next > 0) { xas_set(&xas, 0); - req =3D xas_find_marked(&xas, cache->req_id_next - 1, CACHEFILES_REQ_NEW= ); + req =3D cachefiles_ondemand_select_req(&xas, cache->req_id_next - 1); } if (!req) { xa_unlock(&cache->reqs); @@ -277,14 +310,18 @@ ssize_t cachefiles_ondemand_daemon_read(struct cachef= iles_cache *cache, xa_unlock(&cache->reqs); =20 id =3D xas.xa_index; - msg->msg_id =3D id; =20 if (msg->opcode =3D=3D CACHEFILES_OP_OPEN) { ret =3D cachefiles_ondemand_get_fd(req); - if (ret) + if (ret) { + cachefiles_ondemand_set_object_close(req->object); goto error; + } } =20 + msg->msg_id =3D id; + msg->object_id =3D req->object->ondemand->ondemand_id; + if (copy_to_user(_buffer, msg, n) !=3D 0) { ret =3D -EFAULT; goto err_put_fd; @@ -317,19 +354,23 @@ static int cachefiles_ondemand_send_req(struct cachef= iles_object *object, void *private) { struct cachefiles_cache *cache =3D object->volume->cache; - struct cachefiles_req *req; + struct cachefiles_req *req =3D NULL; XA_STATE(xas, &cache->reqs, 0); int ret; =20 if (!test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags)) return 0; =20 - if (test_bit(CACHEFILES_DEAD, &cache->flags)) - return -EIO; + if (test_bit(CACHEFILES_DEAD, &cache->flags)) { + ret =3D -EIO; + goto out; + } =20 req =3D kzalloc(sizeof(*req) + data_len, GFP_KERNEL); - if (!req) - return -ENOMEM; + if (!req) { + ret =3D -ENOMEM; + goto out; + } =20 req->object =3D object; init_completion(&req->done); @@ -367,7 +408,7 @@ static int cachefiles_ondemand_send_req(struct cachefil= es_object *object, /* coupled with the barrier in cachefiles_flush_reqs() */ smp_mb(); =20 - if (opcode !=3D CACHEFILES_OP_OPEN && + if (opcode =3D=3D CACHEFILES_OP_CLOSE && !cachefiles_ondemand_object_is_open(object)) { WARN_ON_ONCE(object->ondemand->ondemand_id =3D=3D 0); xas_unlock(&xas); @@ -392,7 +433,15 @@ static int cachefiles_ondemand_send_req(struct cachefi= les_object *object, wake_up_all(&cache->daemon_pollwq); wait_for_completion(&req->done); ret =3D req->error; + kfree(req); + return ret; out: + /* Reset the object to close state in error handling path. + * If error occurs after creating the anonymous fd, + * cachefiles_ondemand_fd_release() will set object to close. + */ + if (opcode =3D=3D CACHEFILES_OP_OPEN) + cachefiles_ondemand_set_object_close(object); kfree(req); return ret; } @@ -439,7 +488,6 @@ static int cachefiles_ondemand_init_close_req(struct ca= chefiles_req *req, if (!cachefiles_ondemand_object_is_open(object)) return -ENOENT; =20 - req->msg.object_id =3D object->ondemand->ondemand_id; trace_cachefiles_ondemand_close(object, &req->msg); return 0; } @@ -455,16 +503,7 @@ static int cachefiles_ondemand_init_read_req(struct ca= chefiles_req *req, struct cachefiles_object *object =3D req->object; struct cachefiles_read *load =3D (void *)req->msg.data; struct cachefiles_read_ctx *read_ctx =3D private; - int object_id =3D object->ondemand->ondemand_id; - - /* Stop enqueuing requests when daemon has closed anon_fd. */ - if (!cachefiles_ondemand_object_is_open(object)) { - WARN_ON_ONCE(object_id =3D=3D 0); - pr_info_once("READ: anonymous fd closed prematurely.\n"); - return -EIO; - } =20 - req->msg.object_id =3D object_id; load->off =3D read_ctx->off; load->len =3D read_ctx->len; trace_cachefiles_ondemand_read(object, &req->msg, load); @@ -513,6 +552,7 @@ int cachefiles_ondemand_init_obj_info(struct cachefiles= _object *object, return -ENOMEM; =20 object->ondemand->object =3D object; + INIT_WORK(&object->ondemand->ondemand_work, ondemand_object_worker); return 0; } =20 --=20 2.20.1