Received: by 2002:a89:288:0:b0:1f7:eeee:6653 with SMTP id j8csp399473lqh; Tue, 7 May 2024 02:39:06 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUMMvlZwO9/nchwiQYNHfOqLaivFveggWCwRmMnKFXPBXj/2ADl2bBQWxHuJhk4QMkjSZ6lCoBsfNyRHLCr3NIKVmkx7tw4KXWfA3xIUQ== X-Google-Smtp-Source: AGHT+IHTq5Xte/b5s6wrchnXzB4t18d8UswkDHT5Cj91deXgDcqdVWANsLPUGW5OXezRRTkEfsDA X-Received: by 2002:a17:902:f689:b0:1e3:d4a2:3882 with SMTP id l9-20020a170902f68900b001e3d4a23882mr17180267plg.2.1715074746325; Tue, 07 May 2024 02:39:06 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715074746; cv=pass; d=google.com; s=arc-20160816; b=N+Hia7fb5NBwN8uM8ryC8WfopACUDbXK8zJ2ifIJz/raJCgMxbggacRRocBhEwS/Lk EgRaJloRvz7IxOJFlrjIq0mPQohQl0v9PKaDVcwEisUWT2w8/AcBgOWe14QqQ3IPgbuQ pfCy9iB+JiEKkfDLSGZpGRW8EudCZXR4WUQ4xNDcfA0Y7j4DK8PWDFbZGUHhaFBtZ1Is WrYztu/5PE8lsK2zhzLJdbP7/JKWFYEP65LDGaevbg2kKg66/Jht/GIEy2mvCW3DrCBY UKID2V3DIJWFaeuDnrICPIFe+eLiwlzFHtRTH1fWcOORMWdllzHqkVfUxQGJrkvmE8qN /UJA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id; bh=BxCGE07VYPK6SOpZSdYdxqwxBlUpdtwmG91WOlBCSkU=; fh=O7Xl5tY5XExjv7JMMxLHf+ly3l5pSzTffl3AjAOW1+I=; b=XFjZW/PFDAAVJmg8wez8HWWY1HS49WAYtMPN/rO59QMUD/xrtdPyRMQ3l1BVPWc0tu +O6ASMJbPR0CxnZXyufg3/YQwdWsDLFDJpoD1iSgt63VkGnC6Z1Du7HsmcQkfP5fMEze Ncm9uyRND/eMd7CGgDvrA0YArVw056r1F8JRSQNOl1+ywQ2LNJz/mss5IFhgVDkQtXf9 /o6avw0j1JmgXW1jvSq9GQwo0nvPnByARyPpSpLpQfRg+cEHmlv3JH+WyqZzvBT1Nny8 wQqMumQ2JGuPy9WBDT6m+l5HcTAUPAyydVC/+MbfxxHQqzqdUJZ3ayRk21nzppo+fTs3 f25A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=huaweicloud.com); spf=pass (google.com: domain of linux-kernel+bounces-170953-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-170953-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id k2-20020a170902ba8200b001e4e4f5a487si9702844pls.132.2024.05.07.02.39.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 May 2024 02:39:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-170953-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=huaweicloud.com); spf=pass (google.com: domain of linux-kernel+bounces-170953-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-170953-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 695DCB24467 for ; Tue, 7 May 2024 09:33:57 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C80D414E2C8; Tue, 7 May 2024 09:32:50 +0000 (UTC) Received: from dggsgout11.his.huawei.com (unknown [45.249.212.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0F6FF14E2E4; Tue, 7 May 2024 09:32:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.51 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715074370; cv=none; b=ljj0O4Znom4JSJ5ENlfjr9FWpcGKKye/4Fz/J9zvDhlyB0LLp1QIcqf8J5aMwTmUuiSRs4/k7xu/lOXz11Kc/WzF3m/rlNib/L4XjjGASNCG8nOgKZ9XTyE3gSbX64Z/wG7Pm1zL6qN7f+Bm1ymoxgsU0Ywj2bZFhizuox++Z5E= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1715074370; c=relaxed/simple; bh=QWEBtmOnxBgInrUWtYXy54jN0BcfjzLNlCGWLtloyVY=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=LppMt1UTSLaQxtIdZNbi18BY+gjdhuiY27J7gJtyyGtU0iMhX9UW5r7+ZtbVBuc/QPDlv6Etg5KEEWzSP+aE/xVnStPvCEJM/r/VTLEm8+Rr0oLdXKZsuWASRYuDeLvCLXG0Wox8Yi9Sh73AACs5oILkUPC6TWwKIOzv9vnOFv0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com; spf=pass smtp.mailfrom=huaweicloud.com; arc=none smtp.client-ip=45.249.212.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=huaweicloud.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huaweicloud.com Received: from mail.maildlp.com (unknown [172.19.93.142]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4VYY381TfDz4f3xsJ; Tue, 7 May 2024 17:32:36 +0800 (CST) Received: from mail02.huawei.com (unknown [10.116.40.112]) by mail.maildlp.com (Postfix) with ESMTP id 77FA51A016E; Tue, 7 May 2024 17:32:41 +0800 (CST) Received: from [10.174.177.174] (unknown [10.174.177.174]) by APP1 (Coremail) with SMTP id cCh0CgCXaBE19Tlmph79Lw--.16187S3; Tue, 07 May 2024 17:32:41 +0800 (CST) Message-ID: Date: Tue, 7 May 2024 17:32:37 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.1.2 Subject: Re: [PATCH 08/12] cachefiles: never get a new anon fd if ondemand_id is valid Content-Language: en-US To: Jingbo Xu , netfs@lists.linux.dev Cc: dhowells@redhat.com, jlayton@kernel.org, zhujia.zj@bytedance.com, linux-erofs@lists.ozlabs.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, yangerkun , libaokun@huaweicloud.com References: <20240424033916.2748488-1-libaokun@huaweicloud.com> <20240424033916.2748488-9-libaokun@huaweicloud.com> <625acc9e-b871-4912-965e-82fe3f9228d7@linux.alibaba.com> From: Baokun Li In-Reply-To: <625acc9e-b871-4912-965e-82fe3f9228d7@linux.alibaba.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-CM-TRANSID:cCh0CgCXaBE19Tlmph79Lw--.16187S3 X-Coremail-Antispam: 1UD129KBjvJXoWxAFW5Zr4kXFy8Zw4fZr1fZwb_yoWrur4DpF Way3W3KFyxWF1xWrZ7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 4/24/24 11:39 AM, libaokun@huaweicloud.com wrote: >> From: Baokun Li >> >> Now every time the daemon reads an open request, it requests a new anon fd >> and ondemand_id. With the introduction of "restore", it is possible to read >> the same open request more than once, and therefore have multiple anon fd's >> for the same object. >> >> To avoid this, allocate a new anon fd only if no anon fd has been allocated >> (ondemand_id == 0) or if the previously allocated anon fd has been closed >> (ondemand_id == -1). Returns an error if ondemand_id is valid, letting the >> daemon know that the current userland restore logic is abnormal and needs >> to be checked. > I have no obvious preference on strengthening this on kernel side or > not. Could you explain more about what will happen if the daemon gets > several distinct anon fd corresponding to one same object? IMHO the > daemon should expect the side effect if it issues a 'restore' command > when the daemon doesn't crash. IOW, it's something that shall be fixed > or managed either on the kernel side, or on the daemon side. If the anon_fd is not unique, the daemon will only close the anon_fd corresponding to the newest object_id during drop_object, and the other anon_fds will not be closed until the daemon exits. However, the anon_fd holds the reference count of the object, so the object will not be freed, and the cookie will also not be freed. So mounting a same-named image at this point will cause a hung task in fscache_hash_cookie() by waiting for the cookie to unhash. The object_id and anon_fd of an object are supposed to be unique under normal circumstances, this patch just provides that guarantee even in the case of an exception. Thank you very much for the review! Regards, Baokun >> --- >> fs/cachefiles/ondemand.c | 34 ++++++++++++++++++++++++++++------ >> 1 file changed, 28 insertions(+), 6 deletions(-) >> >> diff --git a/fs/cachefiles/ondemand.c b/fs/cachefiles/ondemand.c >> index b5e6a851ef04..0cf63bfedc9e 100644 >> --- a/fs/cachefiles/ondemand.c >> +++ b/fs/cachefiles/ondemand.c >> @@ -14,11 +14,18 @@ static int cachefiles_ondemand_fd_release(struct inode *inode, >> struct file *file) >> { >> struct cachefiles_object *object = file->private_data; >> - struct cachefiles_cache *cache = object->volume->cache; >> - struct cachefiles_ondemand_info *info = object->ondemand; >> + struct cachefiles_cache *cache; >> + struct cachefiles_ondemand_info *info; >> int object_id; >> struct cachefiles_req *req; >> - XA_STATE(xas, &cache->reqs, 0); >> + XA_STATE(xas, NULL, 0); >> + >> + if (!object) >> + return 0; >> + >> + info = object->ondemand; >> + cache = object->volume->cache; >> + xas.xa = &cache->reqs; >> >> xa_lock(&cache->reqs); >> spin_lock(&info->lock); >> @@ -269,22 +276,39 @@ static int cachefiles_ondemand_get_fd(struct cachefiles_req *req) >> goto err_put_fd; >> } >> >> + spin_lock(&object->ondemand->lock); >> + if (object->ondemand->ondemand_id > 0) { >> + spin_unlock(&object->ondemand->lock); >> + ret = -EEXIST; >> + /* Avoid performing cachefiles_ondemand_fd_release(). */ >> + file->private_data = NULL; >> + goto err_put_file; >> + } >> + >> file->f_mode |= FMODE_PWRITE | FMODE_LSEEK; >> fd_install(fd, file); >> >> load = (void *)req->msg.data; >> load->fd = fd; >> object->ondemand->ondemand_id = object_id; >> + spin_unlock(&object->ondemand->lock); >> >> cachefiles_get_unbind_pincount(cache); >> trace_cachefiles_ondemand_open(object, &req->msg, load); >> return 0; >> >> +err_put_file: >> + fput(file); >> err_put_fd: >> put_unused_fd(fd); >> err_free_id: >> xa_erase(&cache->ondemand_ids, object_id); >> err: >> + spin_lock(&object->ondemand->lock); >> + /* Avoid marking an opened object as closed. */ >> + if (object->ondemand->ondemand_id <= 0) >> + cachefiles_ondemand_set_object_close(object); >> + spin_unlock(&object->ondemand->lock); >> cachefiles_put_object(object, cachefiles_obj_put_ondemand_fd); >> return ret; >> } >> @@ -367,10 +391,8 @@ ssize_t cachefiles_ondemand_daemon_read(struct cachefiles_cache *cache, >> >> if (msg->opcode == CACHEFILES_OP_OPEN) { >> ret = cachefiles_ondemand_get_fd(req); >> - if (ret) { >> - cachefiles_ondemand_set_object_close(req->object); >> + if (ret) >> goto out; >> - } >> } >> >> msg->msg_id = xas.xa_index;