From patchwork Fri Oct 14 03:07:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jia Zhu X-Patchwork-Id: 2474 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp590464wrs; Thu, 13 Oct 2022 20:11:26 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7uhWvrXO/tY/V++MYogu8tUtFzVIehkuftBGDybCtOQb+Qu61RRAkOc1+gtrTz0PQ8jeDO X-Received: by 2002:a05:6a00:1410:b0:528:5a5a:d846 with SMTP id l16-20020a056a00141000b005285a5ad846mr2894348pfu.9.1665717086443; Thu, 13 Oct 2022 20:11:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665717086; cv=none; d=google.com; s=arc-20160816; b=aRws8wrfmbtBBn0DxOWfp0QiFO7QjB9SD3zI4uibm882araURkTSmHdUuni+iMeP3t 1svxJScjltLoJzEornrXRbRCK7dGcD/jtMHMJTpLODnYlUynW8MGUt0UN1+8QEQy4Aj7 imFIS2W4K667hr+8JnKb8Cdu85vH4nmvoNjsDAuWUBFn0m7HU8eWLFXy9oFrdr585NhO Sqe9bO5qDZfjCHLd0y2lkDyDHR2bEyzjqN1CMXA/xyenl/3HL5Zr++loFNvBiJk8GJAh BdX7f3tyTMcVeO0UBxr6MGCxWAL3CneUD1BXtErJSTmCo/0rM1otWBZyP9z5fCLCtcG9 Jl8g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=jexCPXMD1tiAWyMpE7kFB+4P8+aQ3ICPFBRets/3erM=; b=e0SOBwIrGdgv9mM7SjEOgcx4OeSE0EWSImbeaBfKe/yN9ob2GqgYU4V/kt+r4usujL 6URZSbRVx/Kk89Ow/GpTB+4+tXJdUXT+bGjAjTHfqX8cFlW0CUvvx99G332tIaeOGHyz pDLuKKYvvqKngc2gssuzaHW6wPrezWQgL+2F0n1LT7HHAtVnZENF8TJhpUd+dAJABqgw X8h4fw/wQ9PqdUu2h9mI1ny35GrCjBeh0DpLUftVHNa+lJonopCbGwjlu4rc0N9LLgMW 3yW5hPveUE3Nuwm3wYTL19207uA0D/OkIJF4+7KGW8QdqcjjdyJvQg4MV/9DjLxil9on s2Yw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=fQKg0y8Y; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g9-20020a1709026b4900b0017c59fa6b65si1365738plt.597.2022.10.13.20.11.11; Thu, 13 Oct 2022 20:11:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=fQKg0y8Y; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229723AbiJNDIj (ORCPT + 99 others); Thu, 13 Oct 2022 23:08:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43642 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229583AbiJNDIc (ORCPT ); Thu, 13 Oct 2022 23:08:32 -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 7520819B647 for ; Thu, 13 Oct 2022 20:08:22 -0700 (PDT) Received: by mail-pj1-x102f.google.com with SMTP id q10-20020a17090a304a00b0020b1d5f6975so3641040pjl.0 for ; Thu, 13 Oct 2022 20:08:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; 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=jexCPXMD1tiAWyMpE7kFB+4P8+aQ3ICPFBRets/3erM=; b=fQKg0y8YL2d+gPSn3U4m+mr6kpYvDbcL8R79MD7a8yt/UjbteM2AL2JO0itdPoCRGq yOq+0MuO6QP9xkt+pa4kyPCSjPZSVPNGBsC7DSlL2vnJhrvaF0PZACNsmkucaOkdPrfs HbOBqneW95mVZ1xhrNFcLKza2gYsSj7YR8Ak629NYES0ZlBDkIQaq1UkF8cMqgh7tTqW 821pnTxHRtAyTd//3M/lQzRIv+p3T3SOs6pSd+9qXRKaks1qULNxxvJp+saZ16zZXnBA 1ifgn/iQenujEW5MygPT0hXqfUFW+mXdSKA86vtibFC20EqerxRixNYPGrA010QGI2Ei mbKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=jexCPXMD1tiAWyMpE7kFB+4P8+aQ3ICPFBRets/3erM=; b=FLK+FpgQa91MQ7IWvDoibGvFnU2ooTKnpX603xRmZj5DuSB/UI2RA1Vi2UAWly5j5d 8qXs2Um+ozWw9IDKTFLKIhhgrK9KYbaydcjm7T7Je2qfxL9/FeaG4EQ3AapKHHEdMK9n niFnCP416JyURlW/8+Jn08OeA2lxGvU27YvAtZl50QWsq5Vvr1YinLzsH89nISEXm6W7 9W3oanYcynL/cgXEWKX1CIEFX8lVTq+hlqqYw8xOuP4eGas0iUZrrP5Gjk5H27IWEvKP XW9whi1zbSCUxWO+XxDbyIIGMc2yj6IletSoV04IObVN3IAwjoyyPALXyPCkaim7SCED tBPQ== X-Gm-Message-State: ACrzQf0MjZNDq9DVEapL6rbWcU1l8JsLibrIzmiCSdex3feO8uWiCp09 t83y8nF6zgz+sRyS5TlB2CQ8JA== X-Received: by 2002:a17:90b:17c4:b0:20d:4d79:55b1 with SMTP id me4-20020a17090b17c400b0020d4d7955b1mr3240422pjb.125.1665716884249; Thu, 13 Oct 2022 20:08:04 -0700 (PDT) Received: from C02G705SMD6V.bytedance.net ([63.216.146.183]) by smtp.gmail.com with ESMTPSA id h4-20020a17090a710400b0020ae09e9724sm425524pjk.53.2022.10.13.20.07.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Oct 2022 20:08:03 -0700 (PDT) From: Jia Zhu To: dhowells@redhat.com, xiang@kernel.org, jefflexu@linux.alibaba.com Cc: linux-cachefs@redhat.com, linux-erofs@lists.ozlabs.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, yinxin.x@bytedance.com, Jia Zhu Subject: [PATCH V2 1/5] cachefiles: introduce object ondemand state Date: Fri, 14 Oct 2022 11:07:41 +0800 Message-Id: <20221014030745.25748-2-zhujia.zj@bytedance.com> X-Mailer: git-send-email 2.37.0 (Apple Git-136) In-Reply-To: <20221014030745.25748-1-zhujia.zj@bytedance.com> References: <20221014030745.25748-1-zhujia.zj@bytedance.com> MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1746630959790370912?= X-GMAIL-MSGID: =?utf-8?q?1746630959790370912?= 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 Reviewed-by: Xin Yin Reviewed-by: Jingbo Xu --- fs/cachefiles/internal.h | 22 ++++++++++++++++++++++ fs/cachefiles/ondemand.c | 21 +++++++++------------ 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/fs/cachefiles/internal.h b/fs/cachefiles/internal.h index 2ad58c465208..39895bbd149a 100644 --- a/fs/cachefiles/internal.h +++ b/fs/cachefiles/internal.h @@ -17,6 +17,7 @@ #include #include #include +#include #define CACHEFILES_DIO_BLOCK_SIZE 4096 @@ -44,6 +45,11 @@ struct cachefiles_volume { struct dentry *fanout[256]; /* Fanout subdirs */ }; +enum cachefiles_object_state { + CACHEFILES_ONDEMAND_OBJSTATE_close, /* Anonymous fd closed by daemon or initial state */ + CACHEFILES_ONDEMAND_OBJSTATE_open, /* Anonymous fd associated with object is available */ +}; + /* * Backing file state. */ @@ -62,6 +68,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 }; @@ -296,6 +303,21 @@ extern void cachefiles_ondemand_clean_object(struct cachefiles_object *object); extern int cachefiles_ondemand_read(struct cachefiles_object *object, loff_t pos, size_t len); +#define CACHEFILES_OBJECT_STATE_FUNCS(_state) \ +static inline bool \ +cachefiles_ondemand_object_is_##_state(const struct cachefiles_object *object) \ +{ \ + return object->state == CACHEFILES_ONDEMAND_OBJSTATE_##_state; \ +} \ + \ +static inline void \ +cachefiles_ondemand_set_object_##_state(struct cachefiles_object *object) \ +{ \ + object->state = CACHEFILES_ONDEMAND_OBJSTATE_##_state; \ +} + +CACHEFILES_OBJECT_STATE_FUNCS(open); +CACHEFILES_OBJECT_STATE_FUNCS(close); #else static inline ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache, char __user *_buffer, size_t buflen) diff --git a/fs/cachefiles/ondemand.c b/fs/cachefiles/ondemand.c index 0254ed39f68c..90456b8a4b3e 100644 --- a/fs/cachefiles/ondemand.c +++ b/fs/cachefiles/ondemand.c @@ -15,6 +15,7 @@ static int cachefiles_ondemand_fd_release(struct inode *inode, xa_lock(&cache->reqs); object->ondemand_id = CACHEFILES_ONDEMAND_ID_CLOSED; + cachefiles_ondemand_set_object_close(object); /* * 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); + cachefiles_ondemand_set_object_open(req->object); + out: complete(&req->done); return ret; @@ -363,7 +366,8 @@ static int cachefiles_ondemand_send_req(struct cachefiles_object *object, /* coupled with the barrier in cachefiles_flush_reqs() */ smp_mb(); - if (opcode != CACHEFILES_OP_OPEN && object->ondemand_id <= 0) { + if (opcode != CACHEFILES_OP_OPEN && + !cachefiles_ondemand_object_is_open(object)) { WARN_ON_ONCE(object->ondemand_id == 0); xas_unlock(&xas); ret = -EIO; @@ -430,18 +434,11 @@ static int cachefiles_ondemand_init_close_req(struct cachefiles_req *req, void *private) { struct cachefiles_object *object = req->object; - int object_id = object->ondemand_id; - /* - * 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 <= 0) + if (!cachefiles_ondemand_object_is_open(object)) return -ENOENT; - req->msg.object_id = object_id; + req->msg.object_id = object->ondemand_id; trace_cachefiles_ondemand_close(object, &req->msg); return 0; } @@ -460,7 +457,7 @@ static int cachefiles_ondemand_init_read_req(struct cachefiles_req *req, int object_id = object->ondemand_id; /* Stop enqueuing requests when daemon has closed anon_fd. */ - if (object_id <= 0) { + if (!cachefiles_ondemand_object_is_open(object)) { WARN_ON_ONCE(object_id == 0); pr_info_once("READ: anonymous fd closed prematurely.\n"); return -EIO; @@ -485,7 +482,7 @@ int cachefiles_ondemand_init_object(struct cachefiles_object *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; volume_key_size = volume->key[0] + 1; From patchwork Fri Oct 14 03:07:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jia Zhu X-Patchwork-Id: 2470 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp590117wrs; Thu, 13 Oct 2022 20:10:11 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4QyhEgCrOznd1WDlJfDBDpPPVBLoM4SyLmn2aNEszja4c9uBxibB118DFMK1iPu98VZrxM X-Received: by 2002:a17:903:32c4:b0:17f:be8e:5987 with SMTP id i4-20020a17090332c400b0017fbe8e5987mr3049141plr.119.1665717011312; Thu, 13 Oct 2022 20:10:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665717011; cv=none; d=google.com; s=arc-20160816; b=sS4Yg7YKG2s9KCgORFT0ZXz3qv3jlPCtieqJitN3DgmUuBMc0uP16twOQc8SOtpeHN shnHiVbaZYOcsKAioYx+4KeP/y/1F8ffFU1pkgDS9+YRQMj1DF2nKcPFZbrHNDW0ZUM+ vB77z34fiOh3lZ2agGTIadT4LXgv63CDT/lw7Qqr7IOHWzlfGiS/uafCOPhrSvnIzgUn 56CknUK5/vRB3DhJExq7uCBGuadseawzkSXHtiaGfrzg+ZaYTFlQA4Fqu0he9qZI1E0m EkzHTuLcqOx6UkhdD8l1ZsL/zw3HJA177w0/WAOHvOaVQjTLbaBIwe6I3I8KjGjf9Bw6 x1YQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=aSVa2F/ebCCk4BjmPs6WDOXWP+s2jItqlfRtZrqeNzw=; b=F0cS3a5+Qic5xCFoxYBFP3QcjmSjQhALByBHWiVqwETn0FS8iLA3nhfOo06xaEBot2 2Yoo3a1bLLeVM1IA3U0TyhVY8XaE/6X4OJEIFvTD/LZdIwG+uyV7qF9Sbzt2zlPMufkC CCICGoXKXoBL5imDwDw0ReWQA4ff5CulbeRghdmYtShYlEjl/rw7Q595pHhDr2n6/5Ro QEmqaLfLZe+Ul8WRuEiIfxqK4+1KTDhBmEA7SeJqIybAt7nICDfoFhj0pM9csnqhsRIL knVZTnz99J6X+Y3yqoavkSYTp3ZahL1ygoKT7/B/vlAMMvFDdfb/g9IwMXo+/m5qzElJ Dxog== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=YWIx2Ki8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u30-20020a056a00099e00b00562a956d979si1221901pfg.67.2022.10.13.20.09.58; Thu, 13 Oct 2022 20:10:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=YWIx2Ki8; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229784AbiJNDIp (ORCPT + 99 others); Thu, 13 Oct 2022 23:08:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43662 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229593AbiJNDIc (ORCPT ); Thu, 13 Oct 2022 23:08:32 -0400 Received: from mail-pl1-x634.google.com (mail-pl1-x634.google.com [IPv6:2607:f8b0:4864:20::634]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 787F819C06B for ; Thu, 13 Oct 2022 20:08:22 -0700 (PDT) Received: by mail-pl1-x634.google.com with SMTP id h10so3575626plb.2 for ; Thu, 13 Oct 2022 20:08:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; 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=aSVa2F/ebCCk4BjmPs6WDOXWP+s2jItqlfRtZrqeNzw=; b=YWIx2Ki8eHWnee4J69UfYiBa8EluGMcAzDUQNhmDxOIQ5fkf5wXK4AW5I7O64Do3GG DaRjzl/mqfqcLxdUb5EExyBNOzFZ8Mtq5aggwXnXV2rzhGIF86UVsGGx5PC3CY9qgoKM 8TkuGm/BuGIxdxLdvvVYDdQ14etBGQNZlMSiabAWh9qb1gnXAQn4uZocGqMSnywXvcWH 6Ui71cBGZRx9kCUO8hQUZ9nYSv4sI7kLwS+51gwua1l35TKXh/ulpp93T3FZDQtJCsN7 9w9yuoEAdSqVUwrmHoT18/xy0bMCZv4oGZvy9zzhf2JfBCihWuidGheB4eWD9aGdVZG0 2NJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=aSVa2F/ebCCk4BjmPs6WDOXWP+s2jItqlfRtZrqeNzw=; b=RXQGIcm7rPEipIwcyPKugF80ViuEOgjae5/ZB25auYq6Zwa2kGKu/DMJa0/GwWQrIJ 2y+j3PvvmlLXr7WAsLPMhUC7yrbl8TsHt7wbHlgPuT8UpND5oRYzi26a83j9+WoMcFIM HmPlIgCFmiwSqETFUgDw9D3gymFDo1ayNdV2xpTdmcEv5KevfgPS5hOoxoaabbK9d5J8 XN2RtMBWuQoEe8OUZkXluea1S9wM6x2uPCWvbmS7cih6RB9SUT4+oWCHoajb+Kh3NgwE rCTNqFUeAOaVzGTq6YOSfDhcMTyGTiJAduQcCPbNUZNl2FSoFQdOWymenmX9ggSpn2wZ Jc/A== X-Gm-Message-State: ACrzQf0/BF5nv8j8rMFxYvu4mrozygNQklrNAeASCP51TdXoICkc94E5 Gohzbi6wuQI9URLY9UJXiM+yiw== X-Received: by 2002:a17:902:e750:b0:17f:71fa:d695 with SMTP id p16-20020a170902e75000b0017f71fad695mr3155945plf.105.1665716889703; Thu, 13 Oct 2022 20:08:09 -0700 (PDT) Received: from C02G705SMD6V.bytedance.net ([63.216.146.183]) by smtp.gmail.com with ESMTPSA id h4-20020a17090a710400b0020ae09e9724sm425524pjk.53.2022.10.13.20.08.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Oct 2022 20:08:09 -0700 (PDT) From: Jia Zhu To: dhowells@redhat.com, xiang@kernel.org, jefflexu@linux.alibaba.com Cc: linux-cachefs@redhat.com, linux-erofs@lists.ozlabs.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, yinxin.x@bytedance.com, Jia Zhu Subject: [PATCH V2 2/5] cachefiles: extract ondemand info field from cachefiles_object Date: Fri, 14 Oct 2022 11:07:42 +0800 Message-Id: <20221014030745.25748-3-zhujia.zj@bytedance.com> X-Mailer: git-send-email 2.37.0 (Apple Git-136) In-Reply-To: <20221014030745.25748-1-zhujia.zj@bytedance.com> References: <20221014030745.25748-1-zhujia.zj@bytedance.com> MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1746630880693751518?= X-GMAIL-MSGID: =?utf-8?q?1746630880693751518?= 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 Reviewed-by: Jingbo Xu --- fs/cachefiles/interface.c | 6 ++++++ fs/cachefiles/internal.h | 24 ++++++++++++++++++------ fs/cachefiles/ondemand.c | 28 ++++++++++++++++++++++------ 3 files changed, 46 insertions(+), 12 deletions(-) diff --git a/fs/cachefiles/interface.c b/fs/cachefiles/interface.c index a69073a1d3f0..690e3e1ee661 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; + if (cachefiles_ondemand_init_obj_info(object, volume)) { + kmem_cache_free(cachefiles_object_jar, object); + return NULL; + } + refcount_set(&object->ref, 1); spin_lock_init(&object->lock); @@ -88,6 +93,7 @@ void cachefiles_put_object(struct cachefiles_object *object, ASSERTCMP(object->file, ==, NULL); kfree(object->d_name); + kfree(object->private); cache = object->volume->cache->cache; fscache_put_cookie(object->cookie, fscache_cookie_put_object); diff --git a/fs/cachefiles/internal.h b/fs/cachefiles/internal.h index 39895bbd149a..be1e7fbdb444 100644 --- a/fs/cachefiles/internal.h +++ b/fs/cachefiles/internal.h @@ -50,6 +50,12 @@ enum cachefiles_object_state { CACHEFILES_ONDEMAND_OBJSTATE_open, /* Anonymous fd associated with object is available */ }; +struct cachefiles_ondemand_info { + int ondemand_id; + enum cachefiles_object_state state; + struct cachefiles_object *object; +}; + /* * Backing file state. */ @@ -66,10 +72,7 @@ struct cachefiles_object { enum cachefiles_content content_info:8; /* Info about content presence */ 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; -#endif + struct cachefiles_ondemand_info *private; }; #define CACHEFILES_ONDEMAND_ID_CLOSED -1 @@ -303,17 +306,20 @@ extern void cachefiles_ondemand_clean_object(struct cachefiles_object *object); extern int cachefiles_ondemand_read(struct cachefiles_object *object, loff_t pos, size_t len); +extern int cachefiles_ondemand_init_obj_info(struct cachefiles_object *object, + struct cachefiles_volume *volume); + #define CACHEFILES_OBJECT_STATE_FUNCS(_state) \ static inline bool \ cachefiles_ondemand_object_is_##_state(const struct cachefiles_object *object) \ { \ - return object->state == CACHEFILES_ONDEMAND_OBJSTATE_##_state; \ + return object->private->state == CACHEFILES_ONDEMAND_OBJSTATE_##_state; \ } \ \ static inline void \ cachefiles_ondemand_set_object_##_state(struct cachefiles_object *object) \ { \ - object->state = CACHEFILES_ONDEMAND_OBJSTATE_##_state; \ + object->private->state = CACHEFILES_ONDEMAND_OBJSTATE_##_state; \ } CACHEFILES_OBJECT_STATE_FUNCS(open); @@ -339,6 +345,12 @@ static inline int cachefiles_ondemand_read(struct cachefiles_object *object, { return -EOPNOTSUPP; } + +static inline int cachefiles_ondemand_init_obj_info(struct cachefiles_object *object, + struct cachefiles_volume *volume) +{ + return 0; +} #endif /* diff --git a/fs/cachefiles/ondemand.c b/fs/cachefiles/ondemand.c index 90456b8a4b3e..6e47667c6690 100644 --- a/fs/cachefiles/ondemand.c +++ b/fs/cachefiles/ondemand.c @@ -9,12 +9,13 @@ static int cachefiles_ondemand_fd_release(struct inode *inode, { struct cachefiles_object *object = file->private_data; struct cachefiles_cache *cache = object->volume->cache; - int object_id = object->ondemand_id; + struct cachefiles_ondemand_info *info = object->private; + int object_id = info->ondemand_id; struct cachefiles_req *req; XA_STATE(xas, &cache->reqs, 0); xa_lock(&cache->reqs); - object->ondemand_id = CACHEFILES_ONDEMAND_ID_CLOSED; + info->ondemand_id = CACHEFILES_ONDEMAND_ID_CLOSED; cachefiles_ondemand_set_object_close(object); /* @@ -222,7 +223,7 @@ static int cachefiles_ondemand_get_fd(struct cachefiles_req *req) load = (void *)req->msg.data; load->fd = fd; req->msg.object_id = object_id; - object->ondemand_id = object_id; + object->private->ondemand_id = object_id; cachefiles_get_unbind_pincount(cache); trace_cachefiles_ondemand_open(object, &req->msg, load); @@ -368,7 +369,7 @@ static int cachefiles_ondemand_send_req(struct cachefiles_object *object, if (opcode != CACHEFILES_OP_OPEN && !cachefiles_ondemand_object_is_open(object)) { - WARN_ON_ONCE(object->ondemand_id == 0); + WARN_ON_ONCE(object->private->ondemand_id == 0); xas_unlock(&xas); ret = -EIO; goto out; @@ -438,7 +439,7 @@ static int cachefiles_ondemand_init_close_req(struct cachefiles_req *req, if (!cachefiles_ondemand_object_is_open(object)) return -ENOENT; - req->msg.object_id = object->ondemand_id; + req->msg.object_id = object->private->ondemand_id; trace_cachefiles_ondemand_close(object, &req->msg); return 0; } @@ -454,7 +455,7 @@ static int cachefiles_ondemand_init_read_req(struct cachefiles_req *req, struct cachefiles_object *object = req->object; struct cachefiles_read *load = (void *)req->msg.data; struct cachefiles_read_ctx *read_ctx = private; - int object_id = object->ondemand_id; + int object_id = object->private->ondemand_id; /* Stop enqueuing requests when daemon has closed anon_fd. */ if (!cachefiles_ondemand_object_is_open(object)) { @@ -500,6 +501,21 @@ void cachefiles_ondemand_clean_object(struct cachefiles_object *object) cachefiles_ondemand_init_close_req, NULL); } +int cachefiles_ondemand_init_obj_info(struct cachefiles_object *object, + struct cachefiles_volume *volume) +{ + if (!cachefiles_in_ondemand_mode(volume->cache)) + return 0; + + object->private = kzalloc(sizeof(struct cachefiles_ondemand_info), + GFP_KERNEL); + if (!object->private) + return -ENOMEM; + + object->private->object = object; + return 0; +} + int cachefiles_ondemand_read(struct cachefiles_object *object, loff_t pos, size_t len) { From patchwork Fri Oct 14 03:07:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jia Zhu X-Patchwork-Id: 2471 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp590140wrs; Thu, 13 Oct 2022 20:10:16 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6F8xgUkyaP7fkDoKbvQFfmZZa2o5OZQs1WjDfLAgghoidtWNyQ2HyMGo20g+aw4bezvnIG X-Received: by 2002:a63:6c84:0:b0:43c:700f:6218 with SMTP id h126-20020a636c84000000b0043c700f6218mr2682650pgc.420.1665717016139; Thu, 13 Oct 2022 20:10:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665717016; cv=none; d=google.com; s=arc-20160816; b=itJkSk9YVOIdyf7Ne0T3n+SnL7Xv3vhqJZfdlYWsq+C5+NPD+0n8mOSZg3L0I3fYKv q1hJ6en85P+AOt2EZlyxbTpqdjwS9cNnzNoPcLq/DxQlBIx4v3XekDEoi78PL7ihvcDm MnCYGo6N/yp4IWXVHJXgZQeYnqy+TILD8mGn7Z2+CUBSqzTaiQ7lkZX9DYmURIVEo3Y5 OiIcxMp/PpE/N00CGcxzGZgdIiG/paokqk+CYEfhCT+X9T3dISCUagn4/+vIIbuOJLND 1+yRW00X+g+eLqwfz9it86yGD8hiE9lXelUsC0DlWaU58I2F9ESdH9+VtZCfubMX8WeP 3A2g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=1FT3pqXaLpgHD46SI9bXcLzjkfeKWYrP6QJHIa7fSgI=; b=euhR9684Hl0W1PeTGHY3hhat+YfIUXwiI3/CjTg+isgfRuCt1rRcHjQxDhiNmOmI6j NKjCt22bBORSIwSh39KuFzmvs2KKq2iSFanz3fhYS5g0Qy9drvnPAzodT0F4pTTus99p BT2jSsl6UIWDRR275jmfJ8TqEfLCXKkJwe7rlJi8JTgrFW+VyCuh8kAPJrKOfpkPXxCv MCD74C7X3dKkT/FffEKOCbH5MlQUUPEdyjhZL9gxBGUraHK8wscTRcDk6evO+ca0wAWE szfDgsBUlCNfZzaT4w8HwNcvy7qNOImCnCRZWVsni92pYM4vv+x/tm7pJ+1N0rqaQeXn sfOg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=kP1rDwaQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v10-20020a63b64a000000b00438e839828bsi1416789pgt.72.2022.10.13.20.10.03; Thu, 13 Oct 2022 20:10:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=kP1rDwaQ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229617AbiJNDIv (ORCPT + 99 others); Thu, 13 Oct 2022 23:08:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43772 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229679AbiJNDIe (ORCPT ); Thu, 13 Oct 2022 23:08:34 -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 EBE581A2095 for ; Thu, 13 Oct 2022 20:08:23 -0700 (PDT) Received: by mail-pj1-x102f.google.com with SMTP id o17-20020a17090aac1100b0020d98b0c0f4so5298277pjq.4 for ; Thu, 13 Oct 2022 20:08:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; 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=1FT3pqXaLpgHD46SI9bXcLzjkfeKWYrP6QJHIa7fSgI=; b=kP1rDwaQBKUF2dx9dU7gfWxrzs2OIZnr2Yr6Wbk5ixu8d0RGev1Q7fli4Nt2Lb5ABo eP/UBvKONsjfVlL61cE7OZVkfTNA7XjwAN7ULKQnz+oimLxOFNZJr6Hhc/c8ycMBXN69 OK4mmQwh5G7DwyBPl+OwYZJd8fTHD2IT9WMBOmte2Szv55YmwXIHEcE6YxGbRwkePNlx rqmlPEy7a+78cjXo8yu2RVolq5cn2kEx/jHfzhcSjiz9e6/l1naSy5O0fAjOxdsvad8B Gd+0u9t1996iFmDHbjbdsWtSAqRFH8FhgwXkH8lxXcSGejXogWdzw9G56Ve6EsNEK6lp O8mw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=1FT3pqXaLpgHD46SI9bXcLzjkfeKWYrP6QJHIa7fSgI=; b=4mHix+Dk+X+uJbbAceM4xtH3a2H2/1e+iHxL2V3pqHnF7lOBy4R+/7NvOpbtdDCWh0 9cjLbUffEp4SmCCa4euCR3wDPCasi9DNMEdc1cL2aRT0z38zbcyDvONKPwPuv7E7RA3l AaV0FQL71A+wqSwUSgX+bb+Vnk3orrrZaIN4v5i8QnJIExaBQXhsiMsjCGj4E6aNYIJX /uZxcFvMT6AmP4KjrXWLm0osi/rugxhno/iXuZI7WpL6umNiV+jJXYt9xAgYV6/vpUXN dU0egvE/Fj3OTYzxbu/KWBzgELh9Qs6yKmM8+Iuu/40JJqLbHzMrrMi7xqrynAeYGrdu tKeA== X-Gm-Message-State: ACrzQf2DAbkVpFErG1p3htxdK/jQYaSjZQChfgZHxmKeUUqVXTk2N9E3 9pHHhJ/Ra0kc0C0g2hZCdtJd4w== X-Received: by 2002:a17:90b:3b8d:b0:20d:5c7a:abf1 with SMTP id pc13-20020a17090b3b8d00b0020d5c7aabf1mr3368685pjb.118.1665716898605; Thu, 13 Oct 2022 20:08:18 -0700 (PDT) Received: from C02G705SMD6V.bytedance.net ([63.216.146.183]) by smtp.gmail.com with ESMTPSA id h4-20020a17090a710400b0020ae09e9724sm425524pjk.53.2022.10.13.20.08.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Oct 2022 20:08:18 -0700 (PDT) From: Jia Zhu To: dhowells@redhat.com, xiang@kernel.org, jefflexu@linux.alibaba.com Cc: linux-cachefs@redhat.com, linux-erofs@lists.ozlabs.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, yinxin.x@bytedance.com, Jia Zhu Subject: [PATCH V2 3/5] cachefiles: resend an open request if the read request's object is closed Date: Fri, 14 Oct 2022 11:07:43 +0800 Message-Id: <20221014030745.25748-4-zhujia.zj@bytedance.com> X-Mailer: git-send-email 2.37.0 (Apple Git-136) In-Reply-To: <20221014030745.25748-1-zhujia.zj@bytedance.com> References: <20221014030745.25748-1-zhujia.zj@bytedance.com> MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1746630886251516428?= X-GMAIL-MSGID: =?utf-8?q?1746630886251516428?= 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: Xin Yin Reviewed-by: Jingbo Xu --- fs/cachefiles/internal.h | 3 ++ fs/cachefiles/ondemand.c | 94 ++++++++++++++++++++++++++++------------ 2 files changed, 69 insertions(+), 28 deletions(-) diff --git a/fs/cachefiles/internal.h b/fs/cachefiles/internal.h index be1e7fbdb444..21ef5007f488 100644 --- a/fs/cachefiles/internal.h +++ b/fs/cachefiles/internal.h @@ -48,9 +48,11 @@ struct cachefiles_volume { enum cachefiles_object_state { CACHEFILES_ONDEMAND_OBJSTATE_close, /* Anonymous fd closed by daemon or initial state */ CACHEFILES_ONDEMAND_OBJSTATE_open, /* Anonymous fd associated with object is available */ + CACHEFILES_ONDEMAND_OBJSTATE_reopening, /* Object that was closed and is being reopened. */ }; struct cachefiles_ondemand_info { + struct work_struct work; int ondemand_id; enum cachefiles_object_state state; struct cachefiles_object *object; @@ -324,6 +326,7 @@ cachefiles_ondemand_set_object_##_state(struct cachefiles_object *object) \ CACHEFILES_OBJECT_STATE_FUNCS(open); CACHEFILES_OBJECT_STATE_FUNCS(close); +CACHEFILES_OBJECT_STATE_FUNCS(reopening); #else static inline ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache, char __user *_buffer, size_t buflen) diff --git a/fs/cachefiles/ondemand.c b/fs/cachefiles/ondemand.c index 6e47667c6690..c9eea89befec 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 = CACHEFILES_ONDEMAND_ID_CLOSED; cachefiles_ondemand_set_object_close(object); - /* - * 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_read */ + xas_for_each_marked(&xas, req, ULONG_MAX, CACHEFILES_REQ_NEW) { if (req->msg.object_id == object_id && - req->msg.opcode == CACHEFILES_OP_READ) { - req->error = -EIO; + req->msg.opcode == 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); cachefiles_ondemand_set_object_open(req->object); + wake_up_all(&cache->daemon_pollwq); out: complete(&req->done); @@ -238,6 +235,40 @@ static int cachefiles_ondemand_get_fd(struct cachefiles_req *req) return ret; } +static void ondemand_object_worker(struct work_struct *work) +{ + struct cachefiles_object *object = + ((struct cachefiles_ondemand_info *)work)->object; + + cachefiles_ondemand_init_object(object); +} + +/* + * Reopen the closed object with associated read request. + * Skip read requests whose related object are reopening. + */ +static struct cachefiles_req *cachefiles_ondemand_select_req(struct xa_state *xas, + unsigned long xa_max) +{ + struct cachefiles_req *req; + struct cachefiles_ondemand_info *info; + + xas_for_each_marked(xas, req, xa_max, CACHEFILES_REQ_NEW) { + if (req->msg.opcode != CACHEFILES_OP_READ) + return req; + info = req->object->private; + if (info->state == CACHEFILES_ONDEMAND_OBJSTATE_close) { + cachefiles_ondemand_set_object_reopening(req->object); + queue_work(fscache_wq, &info->work); + continue; + } else if (info->state == CACHEFILES_ONDEMAND_OBJSTATE_reopening) { + continue; + } + return req; + } + return NULL; +} + ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache, char __user *_buffer, size_t buflen) { @@ -248,16 +279,16 @@ ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache, int ret = 0; XA_STATE(xas, &cache->reqs, cache->req_id_next); + 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 = xas_find_marked(&xas, UINT_MAX, CACHEFILES_REQ_NEW); + req = cachefiles_ondemand_select_req(&xas, ULONG_MAX); if (!req && cache->req_id_next > 0) { xas_set(&xas, 0); - req = xas_find_marked(&xas, cache->req_id_next - 1, CACHEFILES_REQ_NEW); + req = cachefiles_ondemand_select_req(&xas, cache->req_id_next - 1); } if (!req) { xa_unlock(&cache->reqs); @@ -277,14 +308,18 @@ ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache, xa_unlock(&cache->reqs); id = xas.xa_index; - msg->msg_id = id; if (msg->opcode == CACHEFILES_OP_OPEN) { ret = cachefiles_ondemand_get_fd(req); - if (ret) + if (ret) { + cachefiles_ondemand_set_object_close(req->object); goto error; + } } + msg->msg_id = id; + msg->object_id = req->object->private->ondemand_id; + if (copy_to_user(_buffer, msg, n) != 0) { ret = -EFAULT; goto err_put_fd; @@ -317,19 +352,23 @@ static int cachefiles_ondemand_send_req(struct cachefiles_object *object, void *private) { struct cachefiles_cache *cache = object->volume->cache; - struct cachefiles_req *req; + struct cachefiles_req *req = NULL; XA_STATE(xas, &cache->reqs, 0); int ret; if (!test_bit(CACHEFILES_ONDEMAND_MODE, &cache->flags)) return 0; - if (test_bit(CACHEFILES_DEAD, &cache->flags)) - return -EIO; + if (test_bit(CACHEFILES_DEAD, &cache->flags)) { + ret = -EIO; + goto out; + } req = kzalloc(sizeof(*req) + data_len, GFP_KERNEL); - if (!req) - return -ENOMEM; + if (!req) { + ret = -ENOMEM; + goto out; + } req->object = object; init_completion(&req->done); @@ -367,7 +406,7 @@ static int cachefiles_ondemand_send_req(struct cachefiles_object *object, /* coupled with the barrier in cachefiles_flush_reqs() */ smp_mb(); - if (opcode != CACHEFILES_OP_OPEN && + if (opcode == CACHEFILES_OP_CLOSE && !cachefiles_ondemand_object_is_open(object)) { WARN_ON_ONCE(object->private->ondemand_id == 0); xas_unlock(&xas); @@ -392,7 +431,15 @@ static int cachefiles_ondemand_send_req(struct cachefiles_object *object, wake_up_all(&cache->daemon_pollwq); wait_for_completion(&req->done); ret = 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 == CACHEFILES_OP_OPEN) + cachefiles_ondemand_set_object_close(req->object); kfree(req); return ret; } @@ -439,7 +486,6 @@ static int cachefiles_ondemand_init_close_req(struct cachefiles_req *req, if (!cachefiles_ondemand_object_is_open(object)) return -ENOENT; - req->msg.object_id = object->private->ondemand_id; trace_cachefiles_ondemand_close(object, &req->msg); return 0; } @@ -455,16 +501,7 @@ static int cachefiles_ondemand_init_read_req(struct cachefiles_req *req, struct cachefiles_object *object = req->object; struct cachefiles_read *load = (void *)req->msg.data; struct cachefiles_read_ctx *read_ctx = private; - int object_id = object->private->ondemand_id; - - /* Stop enqueuing requests when daemon has closed anon_fd. */ - if (!cachefiles_ondemand_object_is_open(object)) { - WARN_ON_ONCE(object_id == 0); - pr_info_once("READ: anonymous fd closed prematurely.\n"); - return -EIO; - } - req->msg.object_id = object_id; load->off = read_ctx->off; load->len = read_ctx->len; trace_cachefiles_ondemand_read(object, &req->msg, load); @@ -513,6 +550,7 @@ int cachefiles_ondemand_init_obj_info(struct cachefiles_object *object, return -ENOMEM; object->private->object = object; + INIT_WORK(&object->private->work, ondemand_object_worker); return 0; } From patchwork Fri Oct 14 03:07:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jia Zhu X-Patchwork-Id: 2473 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp590311wrs; Thu, 13 Oct 2022 20:10:54 -0700 (PDT) X-Google-Smtp-Source: AMsMyM63Dbf/UmLc5MGDkwNBYQuzsVnf+ep09jUe/At/Rgfv247/6FwmdrIxfxotD/KUpv4mWO8Y X-Received: by 2002:a17:902:d4ce:b0:177:fe49:19eb with SMTP id o14-20020a170902d4ce00b00177fe4919ebmr2912094plg.170.1665717054129; Thu, 13 Oct 2022 20:10:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665717054; cv=none; d=google.com; s=arc-20160816; b=0mm8oOPCy5J3D3lEh6B1wH/Fspu8NlPttA45I7zLW1vYLRWhK9z3yS0F2t+pMWzwjr 0pwhcxM0+PLU9NopuqoajK2sPI3zhniCHRkZJQg1GEYPgpMOKrZTrKJtDVodcFow+isO VLt21hpMi/NGR3rO4y6dmR7hz2uIXYM+/OKpEGMENmQvVM2jPkmu56x6mck12YDtm0ZQ hgBDahoNY+Ym9HSC5OveMqk24VQel+v1rhCTThJ26Jkpjx/vhrb+lYcPfaHxL2LjgmF2 CNSYxavMkO7cC8a+24WuvN06prdcrD76TBOnSpE3E6797Al0PQVfRf9Yn0rKQ9kQ4RL7 NACA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=i1R7QPYLS3htwC9+m+eMmhkdxMHPXqw45xmXhqmncaA=; b=ZM/UPG5muywtC82QYk/hSwiULW5PA9bUkngy3GpWBwUIuyNKWXG9ThQTqJfIWbBHxl Qhg9qUEQL7kZQNeylyKGGU5TAjyFpFt3kIY6O0J7yKkVizonlvUFELS34R95aoHFtFCp I89dd7uQv4W799GHsop+IYyZf71yl6PT/kxnumSAMZc+sRX1o1lS2hhTohkI9R4niYJY Pj2e4ZucCKaKQ+5tTYOmdcZ5lrCLM6uIeNmhfebEpNbMeL5bhC/ay0+RGV2jywqVNYtT UHmEZalZP7ygRYj+a5lQjT5/fR8xldw+KeEEU8gj1xG8bXD3LoPcjisK+YMOPTfWIy0P pk4g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=22pn9Os6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l2-20020a170902f68200b001786d7bc2dfsi1938900plg.400.2022.10.13.20.10.40; Thu, 13 Oct 2022 20:10:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=22pn9Os6; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229679AbiJNDJ1 (ORCPT + 99 others); Thu, 13 Oct 2022 23:09:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43834 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229732AbiJNDIk (ORCPT ); Thu, 13 Oct 2022 23:08:40 -0400 Received: from mail-qv1-xf30.google.com (mail-qv1-xf30.google.com [IPv6:2607:f8b0:4864:20::f30]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9DA5BEF589 for ; Thu, 13 Oct 2022 20:08:38 -0700 (PDT) Received: by mail-qv1-xf30.google.com with SMTP id y10so2497699qvo.11 for ; Thu, 13 Oct 2022 20:08:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; 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=i1R7QPYLS3htwC9+m+eMmhkdxMHPXqw45xmXhqmncaA=; b=22pn9Os6EXT6UWfc9ZeKQ71GM5Bhmq9AlSEpgZrKl0IUo/yufgpYJMahaOmGApuHFQ 0HjgOrbOd3sJY2g4HJFuY1sgb4TaAlQP3aRsz10/ANjJZl4lMgHQsK7sWfd8Y0DMkh+C Dnl17PUMBTdKkOZPaPAnFJ/golfWNeftyDK3tNB7RN8JVIyFOYWbhGQkwRm4hg/oIKKh XWwkEhfZvnj2wPG3jNFSKte8oOALG7BsAxuzpit/Iy/IXoZtVY0BmdVKoGN2ayiG8T6J v607OE1Wb3Zg/vDA9PphT7Zvanzlont5JanwatBkMlDh5o+XUUb8SSx2FdIDrQQBfhxj TvmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=i1R7QPYLS3htwC9+m+eMmhkdxMHPXqw45xmXhqmncaA=; b=v8eFqwasSqR6d6qnnZUA3+fD9lePasmA8LOWHABddfNqiQ33yxVrt/JKLum33vYO96 fCSI/mmRBM5UIeMUALWMJ4YntWKqNFZ4K87ZUZcIYtSBI7l24dRw9o7+11FQGK78Jv/s bkvtV+ZSMcHNPQagG1Ro0TpWs49SrsjcH8gQCQh+ONXCh2Q69l3NOdiPBlyNn4ZUURFo j8iJazB6cM2Ib/qOiN8kFMIcmaZ2RhPYw+UzhHnhXcO68/ijta6ysS8AdiiChPtWEb1R PonYFGQ4yXSXoHCP8rc1Grv/8IQ7UGrVoatI44PKyShoNVUgl/3ClnmW4L1wiBGY8bKf dAOw== X-Gm-Message-State: ACrzQf1uvsFB4U4/B0aU3Q7Isya9b9OPAb97WawJgzDI5N7K8SS+8xhx T4TnmZk8zYdxCWFRih5CHaCVwZFbThkO5dOh X-Received: by 2002:a17:903:1003:b0:181:6c26:1114 with SMTP id a3-20020a170903100300b001816c261114mr2921922plb.75.1665716906763; Thu, 13 Oct 2022 20:08:26 -0700 (PDT) Received: from C02G705SMD6V.bytedance.net ([63.216.146.183]) by smtp.gmail.com with ESMTPSA id h4-20020a17090a710400b0020ae09e9724sm425524pjk.53.2022.10.13.20.08.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Oct 2022 20:08:26 -0700 (PDT) From: Jia Zhu To: dhowells@redhat.com, xiang@kernel.org, jefflexu@linux.alibaba.com Cc: linux-cachefs@redhat.com, linux-erofs@lists.ozlabs.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, yinxin.x@bytedance.com, Jia Zhu Subject: [PATCH V2 4/5] cachefiles: narrow the scope of triggering EPOLLIN events in ondemand mode Date: Fri, 14 Oct 2022 11:07:44 +0800 Message-Id: <20221014030745.25748-5-zhujia.zj@bytedance.com> X-Mailer: git-send-email 2.37.0 (Apple Git-136) In-Reply-To: <20221014030745.25748-1-zhujia.zj@bytedance.com> References: <20221014030745.25748-1-zhujia.zj@bytedance.com> MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1746630925621180419?= X-GMAIL-MSGID: =?utf-8?q?1746630925621180419?= Don't trigger EPOLLIN when there are only reopening read requests in xarray. Suggested-by: Xin Yin Signed-off-by: Jia Zhu Reviewed-by: Jingbo Xu --- fs/cachefiles/daemon.c | 13 +++++++++++-- fs/cachefiles/internal.h | 12 ++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/fs/cachefiles/daemon.c b/fs/cachefiles/daemon.c index aa4efcabb5e3..c74bd1f4ecf5 100644 --- a/fs/cachefiles/daemon.c +++ b/fs/cachefiles/daemon.c @@ -355,14 +355,23 @@ static __poll_t cachefiles_daemon_poll(struct file *file, struct poll_table_struct *poll) { struct cachefiles_cache *cache = file->private_data; + struct xarray *xa = &cache->reqs; + struct cachefiles_req *req; + unsigned long index; __poll_t mask; poll_wait(file, &cache->daemon_pollwq, poll); mask = 0; if (cachefiles_in_ondemand_mode(cache)) { - if (!xa_empty(&cache->reqs)) - mask |= EPOLLIN; + if (!xa_empty(xa)) { + xa_for_each_marked(xa, index, req, CACHEFILES_REQ_NEW) { + if (!cachefiles_ondemand_is_reopening_read(req)) { + mask |= EPOLLIN; + break; + } + } + } } else { if (test_bit(CACHEFILES_STATE_CHANGED, &cache->flags)) mask |= EPOLLIN; diff --git a/fs/cachefiles/internal.h b/fs/cachefiles/internal.h index 21ef5007f488..98d6cf58db11 100644 --- a/fs/cachefiles/internal.h +++ b/fs/cachefiles/internal.h @@ -327,6 +327,13 @@ cachefiles_ondemand_set_object_##_state(struct cachefiles_object *object) \ CACHEFILES_OBJECT_STATE_FUNCS(open); CACHEFILES_OBJECT_STATE_FUNCS(close); CACHEFILES_OBJECT_STATE_FUNCS(reopening); + +static inline bool cachefiles_ondemand_is_reopening_read(struct cachefiles_req *req) +{ + return cachefiles_ondemand_object_is_reopening(req->object) && + req->msg.opcode == CACHEFILES_OP_READ; +} + #else static inline ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache, char __user *_buffer, size_t buflen) @@ -354,6 +361,11 @@ static inline int cachefiles_ondemand_init_obj_info(struct cachefiles_object *ob { return 0; } + +static inline bool cachefiles_ondemand_is_reopening_read(struct cachefiles_req *req) +{ + return false; +} #endif /* From patchwork Fri Oct 14 03:07:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jia Zhu X-Patchwork-Id: 2472 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp590282wrs; Thu, 13 Oct 2022 20:10:45 -0700 (PDT) X-Google-Smtp-Source: AMsMyM46gEddRX/y76N4kJ+W/mqvNfI+nkCdzo0WbD3g0kFGd3z6JKdFuamAgVK+y3cjjCCloDDl X-Received: by 2002:a17:902:f689:b0:179:fdac:c4e2 with SMTP id l9-20020a170902f68900b00179fdacc4e2mr2964735plg.119.1665717045354; Thu, 13 Oct 2022 20:10:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665717045; cv=none; d=google.com; s=arc-20160816; b=rjD59s0u7Qz+Pz4FiFGKQBRH8b90Aoo5NihHkVTA1jL3EuHVGQFLYs62gggT+PM42J Cf1TIwy7iiJv7J1cdMIszL0jqM3B5SD3PStpEeCqHWmO3gzWU1rth3mqo08Uk3F5FEDY ofeUvJ9cPCN/b9ODcytNDmUHNGpRGFLFPtJxJwqeBvY4JfrjH5ZY+QS2kENUg85irfDg RV95SZ6c6GT5PkWt/k257Rd3IF89r/6ESLHVT51SNnXe4UYRK2p3iswWuTqmRYo4Zkcb QpZ+fJrIkCyrkUnhTVz+skUJ44jb8nHGzQ/u3g69iF7pUW5mEbZOUn/bM3lFHMr+lkS1 9aiw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=RPYvhf6hYj1gmxvw7dTnMnSbUK/V1sm26ubqcw4lSFY=; b=Kgsmvo7qHeJw59Kjpf0OF1IgpdQ5imqslIU+0cObmBDMrB0SKiNjy7FbWE+oMsVhrl D0LDewXRmBubWwkY4NVp9+N+7YTWBkhC/FzxjN6vMMbHvr5dffALM+WEvsQRFNElRkQP mXk1T49DhhIQ+NyGXd0FMJFgwNjZN2qJY/KsJHaLBnL+6v6gefnBgiZ/M2b75CRrHRQK MFdlZ2Eun+EQRQIqreQlZ+RZlw2O+O3lqzJwb3/wjsvqYNFLlOWPDt7NM2B/6qSwtuy+ pE+G/Vi7g5OrbbCasPIc04RvsMjpkjs3r7o0/z+wkUEfnXm5iaaF4SUopBkjlqNm0DPK YnJg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=uR1wnjzo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z5-20020aa785c5000000b0054307ac90f1si1117220pfn.319.2022.10.13.20.10.31; Thu, 13 Oct 2022 20:10:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=uR1wnjzo; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=bytedance.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229876AbiJNDJR (ORCPT + 99 others); Thu, 13 Oct 2022 23:09:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43928 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229722AbiJNDIj (ORCPT ); Thu, 13 Oct 2022 23:08:39 -0400 Received: from mail-pl1-x632.google.com (mail-pl1-x632.google.com [IPv6:2607:f8b0:4864:20::632]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8085EEF5A4 for ; Thu, 13 Oct 2022 20:08:34 -0700 (PDT) Received: by mail-pl1-x632.google.com with SMTP id n7so3579596plp.1 for ; Thu, 13 Oct 2022 20:08:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20210112.gappssmtp.com; s=20210112; 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=RPYvhf6hYj1gmxvw7dTnMnSbUK/V1sm26ubqcw4lSFY=; b=uR1wnjzoh66Ntwurmw7mKwF1tShfnXY9UWfot3dsRbBXUbtebLzF0fV/OdZO1YB5Yw PcIMj+Ydd5Hn2ryM/TVGAKfZN0TZjdE6wCe+F3ctIO6FHCtRgJFCsBZIabYASTtj7hln XLsFKwqQlgniZqtFO6A/gIowSCRYZAj2hcvGo8DCes9+7qUqhO1J776gtuITkdeK0N+R UCWdGHLsKwtvYhGN+SfYD2BTsK0or91WpNavAyzl3NsTvh4cFtEMEUkWSbpWAPS3BfS5 gfLNCK1IEWcc2/kryI9FvqKKmhhxnFa5vC2Pe9soKZt87Sg2tIscUDwTaTd9N+OwA2hF baMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=RPYvhf6hYj1gmxvw7dTnMnSbUK/V1sm26ubqcw4lSFY=; b=69tHbWsI3kh0SNef8OaoMwdsQr5ovRVoIpe6tB1n6yt3A7PxqrP84pKib2XhvlpkXv Y9I+bj7M9KzP2Xxc3hGIkQUGg79UZ7q4eXWxLSl0HkLIFLH2MQHOVymKip/BPThvZlN4 0pFOypV36HaoeE96nBPURBLpzuxWYSKP4um1noXX9tO8Cq1rqBRewBj0k39chEV/z1iY T7zV/w4OWbITXgB9z8uj2BLLrYmBIEM5oKoXvM8jw/2cExsZI9F/G6wC1SXXm0qr2xAq k8OYjR+fq8aIqVXw6+GFR7xlclFd5H1JGrjhJbejT35ZFTorbwPHng0MKp2sel/5ZXFM wmZw== X-Gm-Message-State: ACrzQf2+72zDM0we4vPRdKs0Rb9fmAQgCYFIs/M62nDaZ3a/KENXho+h /Ft8nRFTb4wkc5pzTrYWdX88LA== X-Received: by 2002:a17:902:db11:b0:17d:5e67:c51c with SMTP id m17-20020a170902db1100b0017d5e67c51cmr2829795plx.64.1665716912810; Thu, 13 Oct 2022 20:08:32 -0700 (PDT) Received: from C02G705SMD6V.bytedance.net ([63.216.146.183]) by smtp.gmail.com with ESMTPSA id h4-20020a17090a710400b0020ae09e9724sm425524pjk.53.2022.10.13.20.08.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Oct 2022 20:08:32 -0700 (PDT) From: Jia Zhu To: dhowells@redhat.com, xiang@kernel.org, jefflexu@linux.alibaba.com Cc: linux-cachefs@redhat.com, linux-erofs@lists.ozlabs.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, yinxin.x@bytedance.com, Jia Zhu , Gao Xiang Subject: [PATCH V2 5/5] cachefiles: add restore command to recover inflight ondemand read requests Date: Fri, 14 Oct 2022 11:07:45 +0800 Message-Id: <20221014030745.25748-6-zhujia.zj@bytedance.com> X-Mailer: git-send-email 2.37.0 (Apple Git-136) In-Reply-To: <20221014030745.25748-1-zhujia.zj@bytedance.com> References: <20221014030745.25748-1-zhujia.zj@bytedance.com> MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1746630916923214049?= X-GMAIL-MSGID: =?utf-8?q?1746630916923214049?= 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 c74bd1f4ecf5..014369266cb2 100644 --- a/fs/cachefiles/daemon.c +++ b/fs/cachefiles/daemon.c @@ -77,6 +77,7 @@ static const struct cachefiles_daemon_cmd cachefiles_daemon_cmds[] = { { "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 98d6cf58db11..a3cacba57def 100644 --- a/fs/cachefiles/internal.h +++ b/fs/cachefiles/internal.h @@ -302,6 +302,9 @@ extern ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache, extern int cachefiles_ondemand_copen(struct cachefiles_cache *cache, char *args); +extern int cachefiles_ondemand_restore(struct cachefiles_cache *cache, + char *args); + extern int cachefiles_ondemand_init_object(struct cachefiles_object *object); extern void cachefiles_ondemand_clean_object(struct cachefiles_object *object); diff --git a/fs/cachefiles/ondemand.c b/fs/cachefiles/ondemand.c index c9eea89befec..08677c9d0004 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; } +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;