Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp2256279rwi; Tue, 11 Oct 2022 06:47:54 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4E9r46Uxg77woyarnCSKXH1ImGgthVn7GNQTHgCiGIm93Gl8yy03KpVR9fgyx1jhlqcIhZ X-Received: by 2002:a17:907:9816:b0:787:4350:3a19 with SMTP id ji22-20020a170907981600b0078743503a19mr18023559ejc.627.1665496074156; Tue, 11 Oct 2022 06:47:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665496074; cv=none; d=google.com; s=arc-20160816; b=cN+HhftG/KjspJ+/J0WVcgpJ+uVf3+rAZQ4Aj9IQPJJKuFMtQd0dD8oDc59OQlLQoJ vsrdHMA8QdHv18RWWNw3QJKQWq+MCYznKUskUHDM0dlIrsY/X+b1WGjnIfQ0HTEBaT/4 nv4xeU4thFmDLF9bwIKJlKppl0vTf9UozUXKVGA8K5h+9edBJvY25VjLbgxyl6bnuAGw S8ls9QiAQBodidpmrAd8bcKoy7noPrkCIWhFKEQqnsgL+weldIu7CV5gCqFCf+OdtfCY zrkcwprgdS6CPirQfwd5KC5IkY9L0THbkPusp+kxOxL66TZzcmy3hHBlJzvYJc7teVKv tWLA== 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=54QPvCbeqKVRwur1XuwQUipFUpWsraNrpJ/eYY/JquQ=; b=eJfEmFWwGEiSI6wrxPVX1npP0/b3HxfecMybWDxrt78Ki+zJEpP66JvtRqDj9MUWZ9 HVxGutOaC1PXISNxyvpRzlkU6nivUHktoSAnboYnkyyQmsJTNmwbYzYGNsGyIU0l9R20 EjsJW0SSmx7268piSrXP7gkyRL3DaisehGzXycyeji6LH+EZdISzaJSZltdqheyqQXmc m/QAmXpqHWRt0fWXMfkWQo4zJkSfM3Y8qwLpD+qhSas/yjVcdfOrOtAyP6+qWgxccPr8 pXzyQmLWmY0GEHYHOTvO+lJ99DONnYQ4VV8YVUBbO5vA4r+784H0t5tUgRieyIR7EL4Z noKg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bytedance-com.20210112.gappssmtp.com header.s=20210112 header.b=fKvLGB3S; 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 x3-20020a056402414300b0045c9313faf7si682416eda.353.2022.10.11.06.47.04; Tue, 11 Oct 2022 06:47: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=fKvLGB3S; 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 S230144AbiJKNRz (ORCPT + 99 others); Tue, 11 Oct 2022 09:17:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42566 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229843AbiJKNRP (ORCPT ); Tue, 11 Oct 2022 09:17:15 -0400 Received: from mail-pl1-x62b.google.com (mail-pl1-x62b.google.com [IPv6:2607:f8b0:4864:20::62b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B0E5895AC3 for ; Tue, 11 Oct 2022 06:16:23 -0700 (PDT) Received: by mail-pl1-x62b.google.com with SMTP id f23so13160975plr.6 for ; Tue, 11 Oct 2022 06:16: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=54QPvCbeqKVRwur1XuwQUipFUpWsraNrpJ/eYY/JquQ=; b=fKvLGB3SZItCPbPrbZsi52+JvbWx/k68t7yivMdDZE/97uYier3Dzbw9wUqiyF/BWj PhRl0k+YKfUz8r1oN0bSHpRwvWFzmYtItFt99jG5wH1TP9Io9CEQvmiKaNvQZsN5XTmw Wv/5oLzyT8c6/jJZSm5SrtOL/ggmV+yf79ehkla0RXgDNSmSSYd6M7CTwDiOlDUsqiAB 5MFoj8PDx1XTblsAMzYHNFNut1+JEIt4RfW49r3AGeNb8W0xTCaUFKGgy7NgMbjT9nZS 3WlkpCuXl5BCTiFlbntkAdC1grr2YczoG6fNHLlHClKrSio4uO5hy++0FiPyXFn0JdBR l3rQ== 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=54QPvCbeqKVRwur1XuwQUipFUpWsraNrpJ/eYY/JquQ=; b=etxhBlDBHQ0YVNpYZ2WLjc+zq7kK3lbtO9cm1BD57sPvhLKghDCKSe2kEwfKHeqZ+J KFTeW/fWit6ZEp+ApteB/3i4Z/wCVyOr2inrv0oZuEAXLn/qp4GgG3y+SNq3zDoTqRuY CKsA2FNDPLVucKKqYjb7zZ1P+LJz4NDdpNbgRGZCGGIBMWn3YAS02oEuLP5CAemVejKI FbdllJ0vCOswPoCmNkWjs5BgEWau+xB61Nm90h3YjucFMRQ71D0s+6jSKbbLQl+ofxEn sVOICCc1MwuBj2H+LCX2cdMPdrBrxXARmTc5lZXzKvgvJsgDZJ2GYV9XqNxPkCjf3wyU nr6g== X-Gm-Message-State: ACrzQf15ayi+Eabzn39l7ao4miCuReL5BLNOFsX94fOZhaFYNUvFjK/H QMi9CSlzK36VqvfFSbKD6qoAHg== X-Received: by 2002:a17:90b:2812:b0:205:cdc9:2ccf with SMTP id qb18-20020a17090b281200b00205cdc92ccfmr26144875pjb.97.1665494176123; Tue, 11 Oct 2022 06:16:16 -0700 (PDT) Received: from C02G705SMD6V.bytedance.net ([63.216.146.190]) by smtp.gmail.com with ESMTPSA id d14-20020a170902654e00b00181f8523f60sm4773415pln.225.2022.10.11.06.16.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Oct 2022 06:16:15 -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 3/5] cachefiles: resend an open request if the read request's object is closed Date: Tue, 11 Oct 2022 21:15:50 +0800 Message-Id: <20221011131552.23833-4-zhujia.zj@bytedance.com> X-Mailer: git-send-email 2.37.0 (Apple Git-136) In-Reply-To: <20221011131552.23833-1-zhujia.zj@bytedance.com> References: <20221011131552.23833-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 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 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 --- fs/cachefiles/internal.h | 3 ++ fs/cachefiles/ondemand.c | 99 ++++++++++++++++++++++++++++------------ 2 files changed, 73 insertions(+), 29 deletions(-) diff --git a/fs/cachefiles/internal.h b/fs/cachefiles/internal.h index f6cc9a89b6d4..a9f45972945d 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; @@ -337,6 +339,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 54581d59847a..69bf5446cc9c 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,36 @@ 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 bool cachefiles_ondemand_skip_req(struct cachefiles_req *req) +{ + struct cachefiles_ondemand_info *info = req->object->private; + + if (!req || req->msg.opcode != CACHEFILES_OP_READ) + return false; + + if (info->state == CACHEFILES_ONDEMAND_OBJSTATE_close) { + cachefiles_ondemand_set_object_reopening(req->object); + queue_work(fscache_wq, &info->work); + return true; + } else if (info->state == CACHEFILES_ONDEMAND_OBJSTATE_reopening) { + return true; + } + + return false; +} + ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache, char __user *_buffer, size_t buflen) { @@ -247,6 +274,7 @@ ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache, size_t n; int ret = 0; XA_STATE(xas, &cache->reqs, cache->req_id_next); + unsigned long xa_max = ULONG_MAX; /* * Cyclically search for a request that has not ever been processed, @@ -254,12 +282,18 @@ ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache, * request distribution fair. */ xa_lock(&cache->reqs); - req = xas_find_marked(&xas, UINT_MAX, CACHEFILES_REQ_NEW); - if (!req && cache->req_id_next > 0) { - xas_set(&xas, 0); - req = xas_find_marked(&xas, cache->req_id_next - 1, CACHEFILES_REQ_NEW); +retry: + xas_for_each_marked(&xas, req, xa_max, CACHEFILES_REQ_NEW) { + if (cachefiles_ondemand_skip_req(req)) + continue; + break; } if (!req) { + if (cache->req_id_next > 0 && xa_max == ULONG_MAX) { + xas_set(&xas, 0); + xa_max = cache->req_id_next - 1; + goto retry; + } xa_unlock(&cache->reqs); return 0; } @@ -277,14 +311,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 +355,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 +409,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,8 +434,16 @@ 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: kfree(req); + /* 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); return ret; } @@ -445,7 +495,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; } @@ -461,16 +510,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); @@ -519,6 +559,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; } -- 2.20.1