Received: by 2002:a19:771d:0:0:0:0:0 with SMTP id s29csp1275027lfc; Wed, 1 Jun 2022 13:53:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxVzezvFy3huV84TI0rZgqeRMbEQw3lYe9xB3TIMFzeiKyC1TcpawUObUwTBoWD104OpHL5 X-Received: by 2002:a17:902:e947:b0:163:91e5:d3c with SMTP id b7-20020a170902e94700b0016391e50d3cmr1235325pll.39.1654116805523; Wed, 01 Jun 2022 13:53:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654116805; cv=none; d=google.com; s=arc-20160816; b=iKX0OqeCB0x2vTUIwUgs1gUTary1wxV3dIeWuI6oZ1nb2BVksltrCJMqqR4+hZygoI THiYxUx2U39cdqHLSLAw4HiUPlh01Jxzqhxjy2IYHQ8kOOAB5Bsa1AyUaqY1wJncqmxj TbP1fDqGzu+BIWzM8pvjX8U6yjQsUctM/HAApAOmi/0ls5gOKiniQcEzno2mTAOBbKDQ 0d4V/LOK97ad/cRISQhRM4bvoD09JVeSn8wWh2ponOOe+uHcOkqxZ2ga0GMYyDddvCTU R55kQgES6UCdMbS4BDdQYE9uec4j+kI80Rw8s7WqSOghs10xhlK49G6Nj4bP3WHk5nkQ R39w== 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=6zB4k8HCgWXfZMWhhHx3oaP+QO4uDjlOYW5CoGqwerk=; b=NmxCEIEsP6Y7Kv3f5l06a+WEykNqXRlNwPM5EGDbLO6mEcr75AVfx6YV0XJxrcQ47+ 7Rk06/vu8+XzqV/Dh/fsZ8oHRxJ3d+xpOdg76NonMUHMzPSUbEvqYc+4/NF5mS/AftRs Mgmz6PrP6KvrUTzPSQgmf3a2EEQn6ZW/HIwPn1vwBD3hwzizIUZTTGG92l2pSLvVzIEt 4rbSK3nm4xTeZk++mIjVfE8WS5+cdIjFS6vouriCHjaQFcKLVBvKNEUo37sPIHJ3JnK6 vzPsh9UJ/vdN606DlL/XXWMiL0jLb3HqW0KXVZCfBW9QJLWYcepyWw2Ia4FbWM5WyjXN dUAQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Lbq7WXgQ; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id c14-20020aa7880e000000b004fae00be7c5si2955680pfo.40.2022.06.01.13.53.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Jun 2022 13:53:25 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Lbq7WXgQ; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 5247A2A5529; Wed, 1 Jun 2022 12:58:56 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348907AbiFABL3 (ORCPT + 99 others); Tue, 31 May 2022 21:11:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43612 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348905AbiFABLW (ORCPT ); Tue, 31 May 2022 21:11:22 -0400 Received: from mail-pf1-x42b.google.com (mail-pf1-x42b.google.com [IPv6:2607:f8b0:4864:20::42b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7220B941B4 for ; Tue, 31 May 2022 18:11:21 -0700 (PDT) Received: by mail-pf1-x42b.google.com with SMTP id e11so515095pfj.5 for ; Tue, 31 May 2022 18:11:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=6zB4k8HCgWXfZMWhhHx3oaP+QO4uDjlOYW5CoGqwerk=; b=Lbq7WXgQ/WC3otO2Va6VnCJPaH/mjOaNwN17SFcjO6TTUjkrrWkXKU4/I7E8aSOylz rygmw+G6kHV1S7NEhQtCElRekSTDNEoEdDkORdvEPnh5Iri8rR/f2iuW1o+F2zc6bU/o C0/HWydp8AK1kopJfiZ4dy+RMO8dwKo1J5t3k= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=6zB4k8HCgWXfZMWhhHx3oaP+QO4uDjlOYW5CoGqwerk=; b=KP24JWiElAzdb8WBot+SH6Li3sbtWRLwDXZiLqXzjdvsGASY1B6MvP233Ky2qRjEgk +VgmCYSF8ysnT1HZbMy9aa9H+u6yjEFbqtughTRmit7Cl61H+ZQ84fRjncWGJbGrA/Kh CK6JQ6wZTrdina9bpxDUyyNyCrzpVMI6jToba0VB9frZkVBecWI3rtRC1QP73Ly56kEQ 3K/M3o9SgFkn5j1nrT4CNvP9R1NNpnCCh5S5szvPkt2kihm6DEuzMs64akfNCYRiFkEl UN1znb2oLQ5KSKtQ3yZ2F/WzoVqDKpqeSPq/tJDtrdnKQgZDpdgeMRKPBCFqE+mwjRV+ 8cLQ== X-Gm-Message-State: AOAM531jHplUC1A7q39Gm5wamSJeck3Eb5skZBGJ1rEdnKMariT1y8qg cdtFEidYeAmKxujZTFNFwgG8Qg== X-Received: by 2002:a63:401:0:b0:3fc:8810:f0ae with SMTP id 1-20020a630401000000b003fc8810f0aemr2585858pge.335.1654045880991; Tue, 31 May 2022 18:11:20 -0700 (PDT) Received: from dlunevwfh.roam.corp.google.com (n122-107-196-14.sbr2.nsw.optusnet.com.au. [122.107.196.14]) by smtp.gmail.com with ESMTPSA id mi16-20020a17090b4b5000b001df6173700dsm2621916pjb.49.2022.05.31.18.11.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 May 2022 18:11:20 -0700 (PDT) From: Daniil Lunev X-Google-Original-From: Daniil Lunev To: linux-fsdevel@vger.kernel.org, miklos@szeredi.hu, viro@zeniv.linux.org.uk, hch@infradead.org, tytso@mit.edu Cc: linux-kernel@vger.kernel.org, fuse-devel@lists.sourceforge.net, Daniil Lunev , Daniil Lunev Subject: [PATCH v4 1/2] fs/super: function to prevent super re-use Date: Wed, 1 Jun 2022 11:11:02 +1000 Message-Id: <20220601111059.v4.1.I0e579520b03aa244906b8fe2ef1ec63f2ab7eecf@changeid> X-Mailer: git-send-email 2.31.0 In-Reply-To: <20220601011103.12681-1-dlunev@google.com> References: <20220601011103.12681-1-dlunev@google.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE 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 From: Daniil Lunev The function is to be called from filesystem-specific code to mark a superblock to be ignored by superblock test and thus never re-used. The function also unregisters bdi if the bdi is per-superblock to avoid collision if a new superblock is created to represent the filesystem. generic_shutdown_super() skips unregistering bdi for a retired superlock as it assumes retire function has already done it. Signed-off-by: Daniil Lunev Signed-off-by: Daniil Lunev --- Changes in v4: - Simplify condition according to Christoph Hellwig's comments. Changes in v3: - Back to state tracking from v1 - Use s_iflag to mark superblocked ignored - Only unregister private bdi in retire, without freeing Changes in v2: - Remove super from list of superblocks instead of using a flag fs/super.c | 28 ++++++++++++++++++++++++++-- include/linux/fs.h | 2 ++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/fs/super.c b/fs/super.c index f1d4a193602d6..3fb9fc8d61160 100644 --- a/fs/super.c +++ b/fs/super.c @@ -422,6 +422,30 @@ bool trylock_super(struct super_block *sb) return false; } +/** + * retire_super - prevernts superblock from being reused + * @sb: superblock to retire + * + * The function marks superblock to be ignored in superblock test, which + * prevents it from being reused for any new mounts. If the superblock has + * a private bdi, it also unregisters it, but doesn't reduce the refcount + * of the superblock to prevent potential races. The refcount is reduced + * by generic_shutdown_super(). The function can not be called concurrently + * with generic_shutdown_super(). It is safe to call the function multiple + * times, subsequent calls have no effect. + */ +void retire_super(struct super_block *sb) +{ + down_write(&sb->s_umount); + if (sb->s_iflags & SB_I_PERSB_BDI) { + bdi_unregister(sb->s_bdi); + sb->s_iflags &= ~SB_I_PERSB_BDI; + } + sb->s_iflags |= SB_I_RETIRED; + up_write(&sb->s_umount); +} +EXPORT_SYMBOL(retire_super); + /** * generic_shutdown_super - common helper for ->kill_sb() * @sb: superblock to kill @@ -1216,7 +1240,7 @@ static int set_bdev_super_fc(struct super_block *s, struct fs_context *fc) static int test_bdev_super_fc(struct super_block *s, struct fs_context *fc) { - return s->s_bdev == fc->sget_key; + return !(s->s_iflags & SB_I_RETIRED) && s->s_bdev == fc->sget_key; } /** @@ -1307,7 +1331,7 @@ EXPORT_SYMBOL(get_tree_bdev); static int test_bdev_super(struct super_block *s, void *data) { - return (void *)s->s_bdev == data; + return !(s->s_iflags & SB_I_RETIRED) && (void *)s->s_bdev == data; } struct dentry *mount_bdev(struct file_system_type *fs_type, diff --git a/include/linux/fs.h b/include/linux/fs.h index bbde95387a23a..ef392fd2361bd 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -1411,6 +1411,7 @@ extern int send_sigurg(struct fown_struct *fown); #define SB_I_SKIP_SYNC 0x00000100 /* Skip superblock at global sync */ #define SB_I_PERSB_BDI 0x00000200 /* has a per-sb bdi */ #define SB_I_TS_EXPIRY_WARNED 0x00000400 /* warned about timestamp range expiry */ +#define SB_I_RETIRED 0x00000800 /* superblock shouldn't be reused */ /* Possible states of 'frozen' field */ enum { @@ -2424,6 +2425,7 @@ extern struct dentry *mount_nodev(struct file_system_type *fs_type, int flags, void *data, int (*fill_super)(struct super_block *, void *, int)); extern struct dentry *mount_subtree(struct vfsmount *mnt, const char *path); +void retire_super(struct super_block *sb); void generic_shutdown_super(struct super_block *sb); void kill_block_super(struct super_block *sb); void kill_anon_super(struct super_block *sb); -- 2.31.0