From nobody Mon Sep 15 19:28:22 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 8BFA5C77B71 for ; Fri, 14 Apr 2023 17:23:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229995AbjDNRX3 (ORCPT ); Fri, 14 Apr 2023 13:23:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45560 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229469AbjDNRXY (ORCPT ); Fri, 14 Apr 2023 13:23:24 -0400 Received: from mail-pj1-x102b.google.com (mail-pj1-x102b.google.com [IPv6:2607:f8b0:4864:20::102b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C98016A5E for ; Fri, 14 Apr 2023 10:23:22 -0700 (PDT) Received: by mail-pj1-x102b.google.com with SMTP id hg14-20020a17090b300e00b002471efa7a8fso5678574pjb.0 for ; Fri, 14 Apr 2023 10:23:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1681493002; x=1684085002; 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=iyRNuAUEDwU7cFP/FwiLl7ToUiN3TCE52oxG0Yaf7cc=; b=AIipzAJf7XKNk+M8FHlQLcwlGgE+JSGaHPiF8CepatLjwqRTV/bqqk9I2Rf7HKREqD BZdO597TmZQf6gGGYTKbQ+mXvRDF4+eTRvAFDcKXYxHtQfZYjLN43SLF9a0/+AhUmcvZ WmZnJZWycAHdLpCdjfFiwErskCDLNcnM4O2/elLIgoVJGUhTs7ACP1egFxOoSpWk9xqg OXG7bwxJ+uSAxNzqD1iky0G/xG8WMqCtt4DXd9TgnKh7T7494+xRGRexcHBri6llRm6t N8wlisxjx3mV+7T/q+tnGQWC3h4pv8fQW+Sl1V/QVBg0YofvhGII9lA5uu/brRKxQF+N hWYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681493002; x=1684085002; 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=iyRNuAUEDwU7cFP/FwiLl7ToUiN3TCE52oxG0Yaf7cc=; b=HWf7w4hrO/teD8OA33KCMiECDgNjxDRSgx8JrR1cHMHpW4StD2wQ4ToM8KkNcBY+Bu nJX3qXfj6YZ3EM6+kUBm284YHlH3CvSppkVT+N2/nq4Srnk4hC04pHVkUoeRj1IlIRqM L4RktjnK9UjNpXv1yrql59L4ku4gGf9/kc8r0q/1zUQjkVjKM69m8x8vZEHe6zlDpMpU 4YXp/VVpVqV1kein+q/yay3LbbMaxXOH1LoXAfPqJ1gL4kffAxUIYNLtJ8ga9Q06mTqM JhG2tVRT7Rfyyg+pdOScYMkglzZg6viWqmUA4vJ4+X+w8wwFLNCQJEuPBD7O4xky4JgS 3KwA== X-Gm-Message-State: AAQBX9ejA9Ix4/epjnkxDtgfqH0wnCikRDjPiKbADnkvI0iLWoWuvUUS Ag0RWxNKZp36cwX2JjlXBAOtYQ== X-Google-Smtp-Source: AKy350aIPblA8tfrG5mWHSCOjSP2O30nR/2sxf2v5N98DXc+br+GS6qokMe/aTTbtrOUBAfkyEz2nw== X-Received: by 2002:a17:903:200b:b0:1a5:2640:10fe with SMTP id s11-20020a170903200b00b001a5264010femr2818290pla.67.1681493002303; Fri, 14 Apr 2023 10:23:22 -0700 (PDT) Received: from localhost.localdomain ([139.177.225.243]) by smtp.gmail.com with ESMTPSA id q12-20020a631f4c000000b0051b8172fa68sm370315pgm.38.2023.04.14.10.23.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Apr 2023 10:23:21 -0700 (PDT) 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 1/5] cachefiles: introduce object ondemand state Date: Sat, 15 Apr 2023 01:22:35 +0800 Message-Id: <20230414172239.33743-2-zhujia.zj@bytedance.com> X-Mailer: git-send-email 2.37.1 (Apple Git-137.1) In-Reply-To: <20230414172239.33743-1-zhujia.zj@bytedance.com> References: <20230414172239.33743-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" Previously, @ondemand_id field was used not only to identify ondemand state of the object, but also to represent the index of the xarray. This commit introduces @state field to decouple the role of @ondemand_id and adds helpers to access it. Signed-off-by: Jia Zhu --- fs/cachefiles/internal.h | 21 +++++++++++++++++++++ fs/cachefiles/ondemand.c | 21 +++++++++------------ 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/fs/cachefiles/internal.h b/fs/cachefiles/internal.h index 2ad58c4652084..00beedeaec183 100644 --- a/fs/cachefiles/internal.h +++ b/fs/cachefiles/internal.h @@ -44,6 +44,11 @@ struct cachefiles_volume { struct dentry *fanout[256]; /* Fanout subdirs */ }; =20 +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 */ +}; + /* * Backing file state. */ @@ -62,6 +67,7 @@ struct cachefiles_object { #define CACHEFILES_OBJECT_USING_TMPFILE 0 /* Have an unlinked tmpfile */ #ifdef CONFIG_CACHEFILES_ONDEMAND int ondemand_id; + enum cachefiles_object_state state; #endif }; =20 @@ -296,6 +302,21 @@ extern void cachefiles_ondemand_clean_object(struct ca= chefiles_object *object); extern int cachefiles_ondemand_read(struct cachefiles_object *object, loff_t pos, size_t len); =20 +#define CACHEFILES_OBJECT_STATE_FUNCS(_state, _STATE) \ +static inline bool \ +cachefiles_ondemand_object_is_##_state(const struct cachefiles_object *obj= ect) \ +{ \ + return object->state =3D=3D CACHEFILES_ONDEMAND_OBJSTATE_##_STATE; \ +} \ + \ +static inline void \ +cachefiles_ondemand_set_object_##_state(struct cachefiles_object *object) \ +{ \ + object->state =3D CACHEFILES_ONDEMAND_OBJSTATE_##_STATE; \ +} + +CACHEFILES_OBJECT_STATE_FUNCS(open, OPEN); +CACHEFILES_OBJECT_STATE_FUNCS(close, CLOSE); #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 0254ed39f68ce..90456b8a4b3e0 100644 --- a/fs/cachefiles/ondemand.c +++ b/fs/cachefiles/ondemand.c @@ -15,6 +15,7 @@ static int cachefiles_ondemand_fd_release(struct inode *i= node, =20 xa_lock(&cache->reqs); object->ondemand_id =3D CACHEFILES_ONDEMAND_ID_CLOSED; + cachefiles_ondemand_set_object_close(object); =20 /* * Flush all pending READ requests since their completion depends on @@ -176,6 +177,8 @@ int cachefiles_ondemand_copen(struct cachefiles_cache *= cache, char *args) set_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags); trace_cachefiles_ondemand_copen(req->object, id, size); =20 + cachefiles_ondemand_set_object_open(req->object); + out: complete(&req->done); return ret; @@ -363,7 +366,8 @@ 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 && object->ondemand_id <=3D 0) { + if (opcode !=3D CACHEFILES_OP_OPEN && + !cachefiles_ondemand_object_is_open(object)) { WARN_ON_ONCE(object->ondemand_id =3D=3D 0); xas_unlock(&xas); ret =3D -EIO; @@ -430,18 +434,11 @@ static int cachefiles_ondemand_init_close_req(struct = cachefiles_req *req, void *private) { struct cachefiles_object *object =3D req->object; - int object_id =3D object->ondemand_id; =20 - /* - * It's possible that object id is still 0 if the cookie looking up - * phase failed before OPEN request has ever been sent. Also avoid - * sending CLOSE request for CACHEFILES_ONDEMAND_ID_CLOSED, which means - * anon_fd has already been closed. - */ - if (object_id <=3D 0) + if (!cachefiles_ondemand_object_is_open(object)) return -ENOENT; =20 - req->msg.object_id =3D object_id; + req->msg.object_id =3D object->ondemand_id; trace_cachefiles_ondemand_close(object, &req->msg); return 0; } @@ -460,7 +457,7 @@ static int cachefiles_ondemand_init_read_req(struct cac= hefiles_req *req, int object_id =3D object->ondemand_id; =20 /* Stop enqueuing requests when daemon has closed anon_fd. */ - if (object_id <=3D 0) { + 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; @@ -485,7 +482,7 @@ int cachefiles_ondemand_init_object(struct cachefiles_o= bject *object) * creating a new tmpfile as the cache file. Reuse the previously * allocated object ID if any. */ - if (object->ondemand_id > 0) + if (cachefiles_ondemand_object_is_open(object)) return 0; =20 volume_key_size =3D volume->key[0] + 1; --=20 2.20.1 From nobody Mon Sep 15 19:28:22 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 11089C77B72 for ; Fri, 14 Apr 2023 17:24:02 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230347AbjDNRYA (ORCPT ); Fri, 14 Apr 2023 13:24:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45642 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230271AbjDNRXv (ORCPT ); Fri, 14 Apr 2023 13:23:51 -0400 Received: from mail-pj1-x1034.google.com (mail-pj1-x1034.google.com [IPv6:2607:f8b0:4864:20::1034]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 48EA97ED0 for ; Fri, 14 Apr 2023 10:23:26 -0700 (PDT) Received: by mail-pj1-x1034.google.com with SMTP id pm7-20020a17090b3c4700b00246f00dace2so11448104pjb.2 for ; Fri, 14 Apr 2023 10:23:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1681493005; x=1684085005; 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=ipz6nTPRZ3Cs9OsNmMIh4U9rG9HmGZtivZr/8M/3lr4=; b=UJtia1KQNMp+f1pQPPMLsnb+GlxRBpCYjWKbpTipdlVqr4IFLObia0bV2jaSv2k/UO BcgIYDiTRKUX97NTnRegq++/W8nQxMFHZacpOG4jyOg7kqNWUIc5zClsm1l4Qf859C7l DTAGC5pleH34yyBMpv+2WSBi4AvwYIqWVlgUoekKhhpx+DzxEzIPZ+nw4LxkZnPCdmL9 M5n6Yois3ueil+35lzARM2SN6hvYCN/WPL47hdpURqKLVN12QTBq58kYDxJEmKNc+E4M KQvP7AZDZ9OPEF65/w8MZzMb83YfpVqwkzQ0kBwuolYmEx4KZ46owHZh+zvpH8QHIsjQ O7Mw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681493005; x=1684085005; 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=ipz6nTPRZ3Cs9OsNmMIh4U9rG9HmGZtivZr/8M/3lr4=; b=UoBbtKPhWocGhSfmDgGqEX5yVZPsUPg02Bn7dnSqBl3JkxvLv4TcMbJay2+h31BKKF avWVGK/HQvP3m/2oIuShMth8VJGSvm/p7oGzlk5fpCek3lPgERnR7pPTiqBxW1cDqCCe JYeEWUv/jGUM7deplVIK0vaD+jMakIRrOX/6tdNYhlGLq6VTPu6X0hiVX2nCDNSstkep jcSIQJ0nBrHf2pPp/AdlkUIItFR1/5KbBngc4W4B+4dEwQcVPDMWKoqoFRKf9uIXyRx8 8XjLjjFhG0m1mXRv262rRXECQvQ8HyXGwofDF+RJOWpl5cBS/hGswvuz4dKrXptNVTd3 2cvg== X-Gm-Message-State: AAQBX9dUpE+Zsz+G4CpvRzLxnqX2riiujwGwmFEP0cP6Me0n0jZX7VtE rcdBZpwvfDOfyHNsgvNLzufbzw== X-Google-Smtp-Source: AKy350bQmej5oN9HI0IU11IlEP/9x2ufpFK+o2Abgu/ZkZdz4tbF9Wu6v4eJ8btTafN+eQoyRPPeIg== X-Received: by 2002:a05:6a20:4694:b0:dc:925f:62f1 with SMTP id el20-20020a056a20469400b000dc925f62f1mr5685706pzb.6.1681493005739; Fri, 14 Apr 2023 10:23:25 -0700 (PDT) Received: from localhost.localdomain ([139.177.225.243]) by smtp.gmail.com with ESMTPSA id q12-20020a631f4c000000b0051b8172fa68sm370315pgm.38.2023.04.14.10.23.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Apr 2023 10:23:25 -0700 (PDT) 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 2/5] cachefiles: extract ondemand info field from cachefiles_object Date: Sat, 15 Apr 2023 01:22:36 +0800 Message-Id: <20230414172239.33743-3-zhujia.zj@bytedance.com> X-Mailer: git-send-email 2.37.1 (Apple Git-137.1) In-Reply-To: <20230414172239.33743-1-zhujia.zj@bytedance.com> References: <20230414172239.33743-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" We'll introduce a @work_struct field for @object in subsequent patches, it will enlarge the size of @object. As the result of that, this commit extracts ondemand info field from @object. Signed-off-by: Jia Zhu --- fs/cachefiles/interface.c | 7 ++++++- fs/cachefiles/internal.h | 26 ++++++++++++++++++++++---- fs/cachefiles/ondemand.c | 34 ++++++++++++++++++++++++++++------ 3 files changed, 56 insertions(+), 11 deletions(-) diff --git a/fs/cachefiles/interface.c b/fs/cachefiles/interface.c index 40052bdb33655..35ba2117a6f65 100644 --- a/fs/cachefiles/interface.c +++ b/fs/cachefiles/interface.c @@ -31,6 +31,11 @@ struct cachefiles_object *cachefiles_alloc_object(struct= fscache_cookie *cookie) if (!object) return NULL; =20 + if (cachefiles_ondemand_init_obj_info(object, volume)) { + kmem_cache_free(cachefiles_object_jar, object); + return NULL; + } + refcount_set(&object->ref, 1); =20 spin_lock_init(&object->lock); @@ -88,7 +93,7 @@ void cachefiles_put_object(struct cachefiles_object *obje= ct, ASSERTCMP(object->file, =3D=3D, NULL); =20 kfree(object->d_name); - + cachefiles_ondemand_deinit_obj_info(object); cache =3D object->volume->cache->cache; fscache_put_cookie(object->cookie, fscache_cookie_put_object); object->cookie =3D NULL; diff --git a/fs/cachefiles/internal.h b/fs/cachefiles/internal.h index 00beedeaec183..b0fe76964bc0d 100644 --- a/fs/cachefiles/internal.h +++ b/fs/cachefiles/internal.h @@ -49,6 +49,12 @@ enum cachefiles_object_state { CACHEFILES_ONDEMAND_OBJSTATE_OPEN, /* Anonymous fd associated with object= is available */ }; =20 +struct cachefiles_ondemand_info { + int ondemand_id; + enum cachefiles_object_state state; + struct cachefiles_object *object; +}; + /* * Backing file state. */ @@ -66,8 +72,7 @@ struct cachefiles_object { unsigned long flags; #define CACHEFILES_OBJECT_USING_TMPFILE 0 /* Have an unlinked tmpfile */ #ifdef CONFIG_CACHEFILES_ONDEMAND - int ondemand_id; - enum cachefiles_object_state state; + struct cachefiles_ondemand_info *ondemand; #endif }; =20 @@ -302,17 +307,21 @@ extern void cachefiles_ondemand_clean_object(struct c= achefiles_object *object); extern int cachefiles_ondemand_read(struct cachefiles_object *object, loff_t pos, size_t len); =20 +extern int cachefiles_ondemand_init_obj_info(struct cachefiles_object *obj, + struct cachefiles_volume *volume); +extern void cachefiles_ondemand_deinit_obj_info(struct cachefiles_object *= obj); + #define CACHEFILES_OBJECT_STATE_FUNCS(_state, _STATE) \ static inline bool \ cachefiles_ondemand_object_is_##_state(const struct cachefiles_object *obj= ect) \ { \ - return object->state =3D=3D CACHEFILES_ONDEMAND_OBJSTATE_##_STATE; \ + return object->ondemand->state =3D=3D CACHEFILES_ONDEMAND_OBJSTATE_##_STA= TE; \ } \ \ static inline void \ cachefiles_ondemand_set_object_##_state(struct cachefiles_object *object) \ { \ - object->state =3D CACHEFILES_ONDEMAND_OBJSTATE_##_STATE; \ + object->ondemand->state =3D CACHEFILES_ONDEMAND_OBJSTATE_##_STATE; \ } =20 CACHEFILES_OBJECT_STATE_FUNCS(open, OPEN); @@ -338,6 +347,15 @@ static inline int cachefiles_ondemand_read(struct cach= efiles_object *object, { return -EOPNOTSUPP; } + +static inline int cachefiles_ondemand_init_obj_info(struct cachefiles_obje= ct *obj, + struct cachefiles_volume *volume) +{ + return 0; +} +static inline void cachefiles_ondemand_deinit_obj_info(struct cachefiles_o= bject *obj) +{ +} #endif =20 /* diff --git a/fs/cachefiles/ondemand.c b/fs/cachefiles/ondemand.c index 90456b8a4b3e0..deb7e3007aa1d 100644 --- a/fs/cachefiles/ondemand.c +++ b/fs/cachefiles/ondemand.c @@ -9,12 +9,13 @@ static int cachefiles_ondemand_fd_release(struct inode *i= node, { struct cachefiles_object *object =3D file->private_data; struct cachefiles_cache *cache =3D object->volume->cache; - int object_id =3D object->ondemand_id; + struct cachefiles_ondemand_info *info =3D object->ondemand; + int object_id =3D info->ondemand_id; struct cachefiles_req *req; XA_STATE(xas, &cache->reqs, 0); =20 xa_lock(&cache->reqs); - object->ondemand_id =3D CACHEFILES_ONDEMAND_ID_CLOSED; + info->ondemand_id =3D CACHEFILES_ONDEMAND_ID_CLOSED; cachefiles_ondemand_set_object_close(object); =20 /* @@ -222,7 +223,7 @@ static int cachefiles_ondemand_get_fd(struct cachefiles= _req *req) load =3D (void *)req->msg.data; load->fd =3D fd; req->msg.object_id =3D object_id; - object->ondemand_id =3D object_id; + object->ondemand->ondemand_id =3D object_id; =20 cachefiles_get_unbind_pincount(cache); trace_cachefiles_ondemand_open(object, &req->msg, load); @@ -368,7 +369,7 @@ static int cachefiles_ondemand_send_req(struct cachefil= es_object *object, =20 if (opcode !=3D CACHEFILES_OP_OPEN && !cachefiles_ondemand_object_is_open(object)) { - WARN_ON_ONCE(object->ondemand_id =3D=3D 0); + WARN_ON_ONCE(object->ondemand->ondemand_id =3D=3D 0); xas_unlock(&xas); ret =3D -EIO; goto out; @@ -438,7 +439,7 @@ 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_id; + req->msg.object_id =3D object->ondemand->ondemand_id; trace_cachefiles_ondemand_close(object, &req->msg); return 0; } @@ -454,7 +455,7 @@ static int cachefiles_ondemand_init_read_req(struct cac= hefiles_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_id; + int object_id =3D object->ondemand->ondemand_id; =20 /* Stop enqueuing requests when daemon has closed anon_fd. */ if (!cachefiles_ondemand_object_is_open(object)) { @@ -500,6 +501,27 @@ void cachefiles_ondemand_clean_object(struct cachefile= s_object *object) cachefiles_ondemand_init_close_req, NULL); } =20 +int cachefiles_ondemand_init_obj_info(struct cachefiles_object *object, + struct cachefiles_volume *volume) +{ + if (!cachefiles_in_ondemand_mode(volume->cache)) + return 0; + + object->ondemand =3D kzalloc(sizeof(struct cachefiles_ondemand_info), + GFP_KERNEL); + if (!object->ondemand) + return -ENOMEM; + + object->ondemand->object =3D object; + return 0; +} + +void cachefiles_ondemand_deinit_obj_info(struct cachefiles_object *object) +{ + kfree(object->ondemand); + object->ondemand =3D NULL; +} + int cachefiles_ondemand_read(struct cachefiles_object *object, loff_t pos, size_t len) { --=20 2.20.1 From nobody Mon Sep 15 19:28:22 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 88C53C77B71 for ; Fri, 14 Apr 2023 17:24:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230390AbjDNRYV (ORCPT ); Fri, 14 Apr 2023 13:24:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45792 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230233AbjDNRX4 (ORCPT ); Fri, 14 Apr 2023 13:23:56 -0400 Received: from mail-pj1-x1030.google.com (mail-pj1-x1030.google.com [IPv6:2607:f8b0:4864:20::1030]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C8265902A for ; Fri, 14 Apr 2023 10:23:29 -0700 (PDT) Received: by mail-pj1-x1030.google.com with SMTP id my14-20020a17090b4c8e00b0024708e8e2ddso8441003pjb.4 for ; Fri, 14 Apr 2023 10:23:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1681493009; x=1684085009; 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=ySOXSnP6FL6oSMQRzhqbuOuGsOFMLOtSkAgA+zIYAhs=; b=GwvrOTtJ/qpLMxFnCQ9BNbH6VmRUx6fBicNS3ilywFLg7uhNJHZz/euh0PpduinHca mVzeJETlDXk9MICZ41xp5R4ySy026Ji6VYxLBAELLM6IhUgIbfvKzp1UUQfy9TQI3rAs bDYDGhZlQDqnkkxs2W8GWKCYc3Le1t4guIjxa9WgT4v86RnE/BlnhIKikjdlcAQxwmqZ exQcS4bmfvdRg9CP2O796tHKjkq4+cgfKoChX8biLVopb9T2bjwkclYioHRyD9lMgwEM SEoEICJqmtNABkTdegIgsBEQw3uzB8vrJAl2Zz715GwJE2h23zcUW/HD5TnNeou8K/7G Q7Uw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681493009; x=1684085009; 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=ySOXSnP6FL6oSMQRzhqbuOuGsOFMLOtSkAgA+zIYAhs=; b=Ak4IotQbHzncAZe+icA2UTXJs0qGlLce5Z8CLLiZxhtiul7mZejRqBY38QL3g7ZeAc VHpWmV8AAlrwY0JKWIZmy7TwTFmtnfkgdA2uTQIyhhx/ZlsjJ+2GWjgZmA/yuNWdN7+3 y6WX1iyCAPsUkoPJL9fXVE/SSJHeOEItfPbSTgGuDemBLnal9xvYdA2iOrS/fcKFid/G cLXVkjyQWwvGTBDbSn4/q+slSXWpv/JQhCsLJkk4wZOEcWp5As8L6lWd7JhrpvUnNaML 2xwKLBQHClt7Hr90k/6+IcgjzLRxTi8iy26sN1znnW/01J/MXGOlFYG1mb2aYXUg03gd 9zEQ== X-Gm-Message-State: AAQBX9dQK3w8nyzZ/kG0gSAUqybgimpj8HMVDs9eq3GUopNobDCjjd+M /QxpbAtanDctwYkYmzd4Rw/cepw8Sv9dLZYKNw5+pg== X-Google-Smtp-Source: AKy350aUIfhABV2qJLYSpnCZf5HFsHPszz3L9dpjR1MFYKPvX5SrRzhJXnuQwExqdOthzsjNm9reyg== X-Received: by 2002:a17:90a:460e:b0:23f:7e2e:fe13 with SMTP id w14-20020a17090a460e00b0023f7e2efe13mr6442173pjg.0.1681493009218; Fri, 14 Apr 2023 10:23:29 -0700 (PDT) Received: from localhost.localdomain ([139.177.225.243]) by smtp.gmail.com with ESMTPSA id q12-20020a631f4c000000b0051b8172fa68sm370315pgm.38.2023.04.14.10.23.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Apr 2023 10:23:28 -0700 (PDT) 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 3/5] cachefiles: resend an open request if the read request's object is closed Date: Sat, 15 Apr 2023 01:22:37 +0800 Message-Id: <20230414172239.33743-4-zhujia.zj@bytedance.com> X-Mailer: git-send-email 2.37.1 (Apple Git-137.1) In-Reply-To: <20230414172239.33743-1-zhujia.zj@bytedance.com> References: <20230414172239.33743-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 --- 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 b0fe76964bc0d..b9a90f1a0c015 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 deb7e3007aa1d..8e130de952f7d 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 From nobody Mon Sep 15 19:28:22 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 CE0D4C77B6E for ; Fri, 14 Apr 2023 17:23:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230126AbjDNRXm (ORCPT ); Fri, 14 Apr 2023 13:23:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45804 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229469AbjDNRXk (ORCPT ); Fri, 14 Apr 2023 13:23:40 -0400 Received: from mail-pj1-x102f.google.com (mail-pj1-x102f.google.com [IPv6:2607:f8b0:4864:20::102f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B0959AD2D for ; Fri, 14 Apr 2023 10:23:33 -0700 (PDT) Received: by mail-pj1-x102f.google.com with SMTP id y11-20020a17090a600b00b0024693e96b58so18264622pji.1 for ; Fri, 14 Apr 2023 10:23:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1681493013; x=1684085013; 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=jLxzEPuRQOpnZLaEq6SfAsMRmsFnFoLhYkxVpQU9ODc=; b=BRc8Yf2ckf5lO1OvMNsi2GkZVI1sUHHIDltwlGPKACNCk2hxZYP/2q9ufEFgfGe1Or xu4A8QcOoUfxEbg2hhp7/ABNsHHUmPVfdSW6w+bSy1/eAwuabrxosHFmZprBL+3okRGB jFUBYONWr5DWLd5EzT82YnoOYRtfeVPw3ykOmYXtk59XYAa7kM2DcIJNbMnvZWrN2VsO sNpcT7ALv+c0DAvVkTbO+7v5ixjLbGDC54ch1VQiLWOKbryDhpiVBCpBpXONm36qMKDN umkVSBDGT5zvnuhKVqqetE4d0+mbpfT1lH0hjGbZvIWkAkfy4cWGt4GMiwjhyD56ljra Om0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681493013; x=1684085013; 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=jLxzEPuRQOpnZLaEq6SfAsMRmsFnFoLhYkxVpQU9ODc=; b=OEXU5wGnNKfynb7PAfEL8PisRjYl239CTLNM4bW6a7iJ9dTjd6jDdvbeOONnWZNRYv snNbizVs0gH01BDnqikmYIZOYJq55Y+AotQFs+h2yewmAFpRexCp9ne6lU8s9oCfxReF evY0Nk2vDe5d5JCDZ8ATK0pp5/gnTbHghDE/HFieqltb88xp2B5yJ+O4A+6mnmSZ/vZW 8gU1m7D8iPFn6mVITjNWZQ9BF6Dm69bsbiCfSH7e6Y6Dg5hQ0TZ+cQBaNmIXFvAgaGE/ FH1cbq8fTESXL79L8UKa4A67lqRACs6hemGFQjpNvvnTfle1YK8jSZBixpKxD3//3Jq5 z67w== X-Gm-Message-State: AAQBX9fJ/m0p9A0H8ouKTLCZiupYl+S933NZN1EdTaqxBZUgCnNSh+ZV p+JD47qhEiKT2Plsm1oPzkv5Zg== X-Google-Smtp-Source: AKy350ZlZsrNyqeh3U1slp9rUpz0KUQlUzMaBR9xIRDcSaraCogNI9a9HQahcjC0ZgcUIst42pl5Dw== X-Received: by 2002:a05:6a20:7347:b0:ee:818a:3497 with SMTP id v7-20020a056a20734700b000ee818a3497mr1941553pzc.35.1681493013176; Fri, 14 Apr 2023 10:23:33 -0700 (PDT) Received: from localhost.localdomain ([139.177.225.243]) by smtp.gmail.com with ESMTPSA id q12-20020a631f4c000000b0051b8172fa68sm370315pgm.38.2023.04.14.10.23.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Apr 2023 10:23:32 -0700 (PDT) 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 , Xin Yin Subject: [PATCH V6 4/5] cachefiles: narrow the scope of triggering EPOLLIN events in ondemand mode Date: Sat, 15 Apr 2023 01:22:38 +0800 Message-Id: <20230414172239.33743-5-zhujia.zj@bytedance.com> X-Mailer: git-send-email 2.37.1 (Apple Git-137.1) In-Reply-To: <20230414172239.33743-1-zhujia.zj@bytedance.com> References: <20230414172239.33743-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" Don't trigger EPOLLIN when there are only reopening read requests in xarray. Suggested-by: Xin Yin Signed-off-by: Jia Zhu --- fs/cachefiles/daemon.c | 14 ++++++++++++-- fs/cachefiles/internal.h | 12 ++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/fs/cachefiles/daemon.c b/fs/cachefiles/daemon.c index aa4efcabb5e37..70caa1946207d 100644 --- a/fs/cachefiles/daemon.c +++ b/fs/cachefiles/daemon.c @@ -355,14 +355,24 @@ static __poll_t cachefiles_daemon_poll(struct file *f= ile, struct poll_table_struct *poll) { struct cachefiles_cache *cache =3D file->private_data; + XA_STATE(xas, &cache->reqs, 0); + struct cachefiles_req *req; __poll_t mask; =20 poll_wait(file, &cache->daemon_pollwq, poll); mask =3D 0; =20 if (cachefiles_in_ondemand_mode(cache)) { - if (!xa_empty(&cache->reqs)) - mask |=3D EPOLLIN; + if (!xa_empty(&cache->reqs)) { + rcu_read_lock(); + xas_for_each_marked(&xas, req, ULONG_MAX, CACHEFILES_REQ_NEW) { + if (!cachefiles_ondemand_is_reopening_read(req)) { + mask |=3D EPOLLIN; + break; + } + } + rcu_read_unlock(); + } } else { if (test_bit(CACHEFILES_STATE_CHANGED, &cache->flags)) mask |=3D EPOLLIN; diff --git a/fs/cachefiles/internal.h b/fs/cachefiles/internal.h index b9a90f1a0c015..26e5f8f123ef1 100644 --- a/fs/cachefiles/internal.h +++ b/fs/cachefiles/internal.h @@ -329,6 +329,13 @@ cachefiles_ondemand_set_object_##_state(struct cachefi= les_object *object) \ CACHEFILES_OBJECT_STATE_FUNCS(open, OPEN); CACHEFILES_OBJECT_STATE_FUNCS(close, CLOSE); CACHEFILES_OBJECT_STATE_FUNCS(reopening, REOPENING); + +static inline bool cachefiles_ondemand_is_reopening_read(struct cachefiles= _req *req) +{ + return cachefiles_ondemand_object_is_reopening(req->object) && + req->msg.opcode =3D=3D CACHEFILES_OP_READ; +} + #else static inline ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_ca= che *cache, char __user *_buffer, size_t buflen) @@ -359,6 +366,11 @@ static inline int cachefiles_ondemand_init_obj_info(st= ruct cachefiles_object *ob static inline void cachefiles_ondemand_deinit_obj_info(struct cachefiles_o= bject *obj) { } + +static inline bool cachefiles_ondemand_is_reopening_read(struct cachefiles= _req *req) +{ + return false; +} #endif =20 /* --=20 2.20.1 From nobody Mon Sep 15 19:28:22 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 1087BC77B71 for ; Fri, 14 Apr 2023 17:24:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230365AbjDNRY3 (ORCPT ); Fri, 14 Apr 2023 13:24:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45918 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230344AbjDNRYI (ORCPT ); Fri, 14 Apr 2023 13:24:08 -0400 Received: from mail-pl1-x62c.google.com (mail-pl1-x62c.google.com [IPv6:2607:f8b0:4864:20::62c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1BF5183D2 for ; Fri, 14 Apr 2023 10:23:37 -0700 (PDT) Received: by mail-pl1-x62c.google.com with SMTP id o2so18964415plg.4 for ; Fri, 14 Apr 2023 10:23:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1681493017; x=1684085017; 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=qgUvM997QJXfBj7OoFl5jxJ2P02ysazlJ3MfSoqKoTg=; b=T3zZn09jU30v3X8gm7zDVFka4YxfNrNYqbfTW7odCAvrYSgeHu3+TWK/Uex+lInrl0 ZR1SLxQQ2glTEULCFiZ8A/qnkb3MFe92XInJENc/PcgdijaRXP2jIN6X1kDLUnP++6IQ QxplK7Pa9BQFppNM64xGUztMw2VnSlZCV2zATAGfF/9q2HqmOLCVZbagxFdEGTigAVE7 RRP7UH3QmA5SfzjT8wMk+gaqYPYHMimKUVP4jk4csTXP/FrA5rfpwAOvMO2aZza7K1yL I+DdpAxZdgWcfk37FG8L7ECQ6ETodw8zhueKCA6Z7daSTQ1CBJEUdu45EQFDRCXK1SVa kMig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681493017; x=1684085017; 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=qgUvM997QJXfBj7OoFl5jxJ2P02ysazlJ3MfSoqKoTg=; b=i+Tt5l1SFhYqzoe4mnSLr4llv5hYaWBAv3SxCjBpzWDP+EG73i44JLPCQWoL/KGfer nWU8WDz3UfqnHbs0x+a4eyVlcRxmh42sBLY2bEuGmmsbjkj7hrhC+gz0gAjcrIroMCdD OHZW7XuU/KoFm1tFBfHGPKjI2BlSCCknvt0kY7o0118320mexBrW3u3mFR9VOubYSiSw KGpfg9xh3yXq2tE6Bpkl32CeZYc5XNrCKkOr7HqdAq3bMuOs6OiM9SqwvQedXY1Jm2Zc voTQ4TElAXS3UjnsUOEcX4KtiPgISmZMz/vbNIMfW64nJ6YOdA3lTl5Z+5lJsDRF1ieo EBcA== X-Gm-Message-State: AAQBX9ff7DO+ub3FGwfQG9/gKhUvOWg+ebqnXhLMVAqlSd0jkfCLbvdH pPD8CVOlcvwSOY9qqtWHszKzIQ== X-Google-Smtp-Source: AKy350aBgZHg/laFWLHOBjKb0DZ6Hi2flL729hh8PovcVeBU4tyJFAUENy+tE8fTCKXuvB2+OR9kVQ== X-Received: by 2002:a05:6a20:8f2a:b0:ee:b24e:a40b with SMTP id b42-20020a056a208f2a00b000eeb24ea40bmr335653pzk.53.1681493017046; Fri, 14 Apr 2023 10:23:37 -0700 (PDT) Received: from localhost.localdomain ([139.177.225.243]) by smtp.gmail.com with ESMTPSA id q12-20020a631f4c000000b0051b8172fa68sm370315pgm.38.2023.04.14.10.23.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 14 Apr 2023 10:23:36 -0700 (PDT) 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 , Xin Yin Subject: [PATCH V6 5/5] cachefiles: add restore command to recover inflight ondemand read requests Date: Sat, 15 Apr 2023 01:22:39 +0800 Message-Id: <20230414172239.33743-6-zhujia.zj@bytedance.com> X-Mailer: git-send-email 2.37.1 (Apple Git-137.1) In-Reply-To: <20230414172239.33743-1-zhujia.zj@bytedance.com> References: <20230414172239.33743-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" Previously, in ondemand read scenario, if the anonymous fd was closed by user daemon, inflight and subsequent read requests would return EIO. As long as the device connection is not released, user daemon can hold and restore inflight requests by setting the request flag to CACHEFILES_REQ_NEW. Suggested-by: Gao Xiang Signed-off-by: Jia Zhu Signed-off-by: Xin Yin Reviewed-by: Jingbo Xu --- fs/cachefiles/daemon.c | 1 + fs/cachefiles/internal.h | 3 +++ fs/cachefiles/ondemand.c | 23 +++++++++++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/fs/cachefiles/daemon.c b/fs/cachefiles/daemon.c index 70caa1946207d..3f24905f40661 100644 --- a/fs/cachefiles/daemon.c +++ b/fs/cachefiles/daemon.c @@ -77,6 +77,7 @@ static const struct cachefiles_daemon_cmd cachefiles_daem= on_cmds[] =3D { { "tag", cachefiles_daemon_tag }, #ifdef CONFIG_CACHEFILES_ONDEMAND { "copen", cachefiles_ondemand_copen }, + { "restore", cachefiles_ondemand_restore }, #endif { "", NULL } }; diff --git a/fs/cachefiles/internal.h b/fs/cachefiles/internal.h index 26e5f8f123ef1..4a87c9d714a94 100644 --- a/fs/cachefiles/internal.h +++ b/fs/cachefiles/internal.h @@ -303,6 +303,9 @@ extern ssize_t cachefiles_ondemand_daemon_read(struct c= achefiles_cache *cache, extern int cachefiles_ondemand_copen(struct cachefiles_cache *cache, char *args); =20 +extern int cachefiles_ondemand_restore(struct cachefiles_cache *cache, + char *args); + extern int cachefiles_ondemand_init_object(struct cachefiles_object *objec= t); extern void cachefiles_ondemand_clean_object(struct cachefiles_object *obj= ect); =20 diff --git a/fs/cachefiles/ondemand.c b/fs/cachefiles/ondemand.c index 8e130de952f7d..b8fbbb1961bbc 100644 --- a/fs/cachefiles/ondemand.c +++ b/fs/cachefiles/ondemand.c @@ -182,6 +182,29 @@ int cachefiles_ondemand_copen(struct cachefiles_cache = *cache, char *args) return ret; } =20 +int cachefiles_ondemand_restore(struct cachefiles_cache *cache, char *args) +{ + struct cachefiles_req *req; + + XA_STATE(xas, &cache->reqs, 0); + + if (!test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags)) + return -EOPNOTSUPP; + + /* + * Reset the requests to CACHEFILES_REQ_NEW state, so that the + * requests have been processed halfway before the crash of the + * user daemon could be reprocessed after the recovery. + */ + xas_lock(&xas); + xas_for_each(&xas, req, ULONG_MAX) + xas_set_mark(&xas, CACHEFILES_REQ_NEW); + xas_unlock(&xas); + + wake_up_all(&cache->daemon_pollwq); + return 0; +} + static int cachefiles_ondemand_get_fd(struct cachefiles_req *req) { struct cachefiles_object *object; --=20 2.20.1