Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp504731rwb; Thu, 18 Aug 2022 07:31:44 -0700 (PDT) X-Google-Smtp-Source: AA6agR799ZmXpzEL/UKpeVc4XXpGBH0GzVMXdbdfyqi7wWbpo5eR2y+I3UNTrXUPyAlBeZl5Prpp X-Received: by 2002:a17:90a:8001:b0:1f4:fe95:c420 with SMTP id b1-20020a17090a800100b001f4fe95c420mr3522220pjn.146.1660833104300; Thu, 18 Aug 2022 07:31:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660833104; cv=none; d=google.com; s=arc-20160816; b=P/1e1i5ubTLoromwIa3ywQbtKGYu2MbI21tRUKVOdLU27XvwbtPxNuKbxFr4nraShm 9ouNrsbYjmehJnifbi3rRH4AVfGURrC+v2BwVKwuBm0FbkdUNKhrPV725UGZ/KFJ9Up0 ocJ5jGPvp1EgwtBkcUgY5D5D78PsvhaDSpbi3wyNvRmt1HkF+JSrN8n3KHJzjilxgUkv +lUcw3czHwlO8rvMlNF5KoG6JRM/Yc1CcXTyho1aYzUK1hMSbcj+ngohNknncJQdx7j9 zUkFFKdr/iFlYT0FtJO+pbhCNeycvQ6qqFk0EWMo3TxhEcyZFDMY6EKzXE5Fp00HV17U K9iA== 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=RqIFS3uQuz8ZCr2lEZPiUaSyj69GXTkqlo83y6u621E=; b=QIvSDNN0+qight3hBCSwTo+Y1cuBXJQzgWkG4IDgRs3GqM7KSLpfUcQCvVzvM1tg3V u0iPWypWLgKWZ7LIOKwpKbZWGfB2i9k+PYDDHHAHs7MUkI7qyQ5vBdACZF/k8NoddAcw TTPXzdeTzaZbAufFhO+9+YpmpZ/C9yYV9QPnUq408EvLvx8Of5ACf3FzFi6nr3jTaO5A Xbws14Aw4EOvpYt1qes0h7HnqSDnNzPhK3Nypx4tdoSIw8/yxWNiPluItkwry34LheUH pmUFdR3R2p9ksxhMwRZ4b1uurImYmnr5gKz9BEjBrE/wvs0UHbpm+YaTKuAeaVvY/dmL sQ4w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=2EvxctSr; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x138-20020a633190000000b0042890f36268si1437809pgx.857.2022.08.18.07.31.32; Thu, 18 Aug 2022 07:31:44 -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=2EvxctSr; 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 S244841AbiHRNwu (ORCPT + 99 others); Thu, 18 Aug 2022 09:52:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47998 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239171AbiHRNws (ORCPT ); Thu, 18 Aug 2022 09:52:48 -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 792F8642EE for ; Thu, 18 Aug 2022 06:52:44 -0700 (PDT) Received: by mail-pj1-x1034.google.com with SMTP id w11-20020a17090a380b00b001f73f75a1feso4808206pjb.2 for ; Thu, 18 Aug 2022 06:52:44 -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; bh=RqIFS3uQuz8ZCr2lEZPiUaSyj69GXTkqlo83y6u621E=; b=2EvxctSr5Q1/SEgne3HxcS5c+B/3gWCA39X7INMzilFPkSA9H+kby2xQzG7rcKBZFd f80LGnLpUjBedwg6SVbJeuaqRtPjTS5pUQdzctsm2V2wDR7Ny7N7r2XrKP3+Lj0S3bEW CJVROTC5Q24ZFdOkFCI4X/0WOHUhLkuglMyno53yIAjgarw+KlzOJhtLL3hITb8uelAC yeruTZjyM+f6056n4AFa/ZfbsSh1HNyL3hehweMvPJeMrMQ7OuWPTtoVqYNLLuyF1ZlA JN5kAYtC+JQLwCFeL+bsSh4gkqeOrteyAkrX5wg+MTnIyl3y7b5ZuavzGXb2JViNWRA/ tDJA== 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; bh=RqIFS3uQuz8ZCr2lEZPiUaSyj69GXTkqlo83y6u621E=; b=n0Gy9RwHQfpEoZmAYWu/gdLEMpEmtRQmPXmB6CSFOfCjCL+NwGKZGAjz3jUIncoCh7 BhQcmf9ZB3StQ4+4WTKi3D83FIVDJSjSU/jeZ1vT7uQVPZ+2GFXGo3b5LLX+RtS0Fvce Sr+Yy20ZlI7YKdl7WJvTDp06N0qplz/Jo4a5q8hD7Evevs2uU+v8ss9Fvb3Co21zLd9J W6SoRbKAP/gHa5JGoeAW5FcOkvUj9lPHwpR4q+fEzJB9BUcGamtslgBvdYPwGHiEnQDR 2tRTV++A7fll/wDf2FMio028f5eAJruXk44rFba0uWolmCICvgSkS1T33S0H9uokRaF8 /8Gg== X-Gm-Message-State: ACgBeo1lHqcgl30qXZ+xicR4qSM4mLD7XmrKbzDArzk+IH8q1Wv/E4Q3 LgEj8oO+0eUma6bQn9PTYOf54w== X-Received: by 2002:a17:90b:3ec2:b0:1f7:3f49:17c3 with SMTP id rm2-20020a17090b3ec200b001f73f4917c3mr8674863pjb.203.1660830763941; Thu, 18 Aug 2022 06:52:43 -0700 (PDT) Received: from C02G705SMD6V.bytedance.net ([139.177.225.242]) by smtp.gmail.com with ESMTPSA id k17-20020a170902ce1100b0016db0d877e4sm1385697plg.221.2022.08.18.06.52.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Aug 2022 06:52:43 -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: [RFC PATCH 1/5] cachefiles: introduce object ondemand state Date: Thu, 18 Aug 2022 21:52:00 +0800 Message-Id: <20220818135204.49878-2-zhujia.zj@bytedance.com> X-Mailer: git-send-email 2.32.0 (Apple Git-132) In-Reply-To: <20220818135204.49878-1-zhujia.zj@bytedance.com> References: <20220818135204.49878-1-zhujia.zj@bytedance.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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, T_SCC_BODY_TEXT_LINE 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 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 --- fs/cachefiles/internal.h | 33 +++++++++++++++++++++++++++++++++ fs/cachefiles/ondemand.c | 15 +++++++++------ 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/fs/cachefiles/internal.h b/fs/cachefiles/internal.h index 6cba2c6de2f9..6661b3e361da 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 }; @@ -295,6 +302,32 @@ 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) \ +{ \ + /* + * Pairs with smp_store_release() in set_object_##_state() + * I.e. another task can publish state concurrently, by executing + * a RELEASE barrier. We need to use smp_load_acquire() here + * to safely ACQUIRE the memory the other task published. + */ \ + return smp_load_acquire(&object->state) == CACHEFILES_ONDEMAND_OBJSTATE_##_state; \ +} \ + \ +static inline void \ +cachefiles_ondemand_set_object_##_state(struct cachefiles_object *object) \ +{ \ + /* + * Pairs with smp_load_acquire() in object_is_##_state() + * I.e. here we publish a state with a RELEASE barrier + * so that concurrent tasks can ACQUIRE it. + */ \ + smp_store_release(&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 1fee702d5529..e3155a5f32e4 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 @@ -172,6 +173,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; @@ -353,7 +356,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; @@ -420,7 +424,6 @@ 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 @@ -428,10 +431,10 @@ static int cachefiles_ondemand_init_close_req(struct cachefiles_req *req, * 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; } @@ -450,7 +453,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; @@ -475,7 +478,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; -- 2.20.1