Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp45552pxb; Wed, 14 Apr 2021 09:04:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJySqAWfyeSXruEb7X2LyaIAa6Vv85E9LncpeVsycoYjEkgElUh7HIpQtKkodCB4OlrvWpC3 X-Received: by 2002:aa7:c247:: with SMTP id y7mr42782910edo.305.1618416298819; Wed, 14 Apr 2021 09:04:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618416298; cv=none; d=google.com; s=arc-20160816; b=LcUppJhhnPG4PMR1KP+x2/HwYcr0Ml1m5ABpW9aQYn2S39iZPRUYuT2HNEi3aSb58k blB7FIcscmgNMTcjc3OEqWGFe0CEgHI3zuRYwzeImtmQfdzMHxU7erlxe5zBB2wbDdQn nFNXqHdQJOxfEDNnUqjBUbkRuRRDtPlj+CBkf1iJJkJKUO5hOyUMUQ6w6d9yeljX7et2 jBoLKX67l96Y1kJiCWT0EM+FIdF7oXu6q9JGK8lsVJDD+0dbPcxuVvD5jePU+HihRKRL EJb+/KZ+zq2Ygc7SYG8MydFGftaqiyHyLniUlAGiUTG9Po3VtDZUm7b5cXl/P/fGarND +m9Q== 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; bh=nczDNM1/6n2Qyp2FGn1DWzMUmD9aukwJFRcAtq8NZHg=; b=DYReDeLB1FJH+HaxyZaxVIqnhEqd0LorWNE/LWQtBUqfgwWAu9rJ3N4brD7o81h412 EwO15/FklY1hjqiw6KnPv8dubl2s9PMIgoAXbALqgklwKpfgDlcEhPp6/tfrKk/n5hLZ HRl0IdO9hBsgjK94F40nF4E7pRWE2xaFsJYNkyRGJraLLYJ+BH433IyiSCNnSNLA8kyd zsvq79Fok+3OUyckR6+9X/qDN04l8+b0Yp/ivqkd5WFbjn0OymqGNtigeUlgzktg5GfW wtJvej41sDAftk/pcvpED8sXtVPG8aqorPkNf66NUl3FcOCBg/15L4Iz2VGOcJ2kvVIx e4qA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=huawei.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h16si4743ede.206.2021.04.14.09.04.32; Wed, 14 Apr 2021 09:04:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351418AbhDNNkB (ORCPT + 99 others); Wed, 14 Apr 2021 09:40:01 -0400 Received: from szxga06-in.huawei.com ([45.249.212.32]:16916 "EHLO szxga06-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1351401AbhDNNkA (ORCPT ); Wed, 14 Apr 2021 09:40:00 -0400 Received: from DGGEMS411-HUB.china.huawei.com (unknown [172.30.72.59]) by szxga06-in.huawei.com (SkyGuard) with ESMTP id 4FL3ST6xz1zjZBp; Wed, 14 Apr 2021 21:37:45 +0800 (CST) Received: from huawei.com (10.175.127.227) by DGGEMS411-HUB.china.huawei.com (10.3.19.211) with Microsoft SMTP Server id 14.3.498.0; Wed, 14 Apr 2021 21:39:29 +0800 From: Zhang Yi To: CC: , , , , , Subject: [RFC PATCH v2 6/7] fs: introduce a usage count into the superblock Date: Wed, 14 Apr 2021 21:47:36 +0800 Message-ID: <20210414134737.2366971-7-yi.zhang@huawei.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: <20210414134737.2366971-1-yi.zhang@huawei.com> References: <20210414134737.2366971-1-yi.zhang@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.175.127.227] X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-ext4@vger.kernel.org Commit <87d8fe1ee6b8> ("add releasepage hooks to block devices which can be used by file systems") introduce a hook that used by ext4 filesystem to release journal buffers, but it doesn't add corresponding concurrency protection that ->bdev_try_to_free_page() could be raced by umount filesystem concurrently. This patch add a usage count on superblock that filesystem can use it to prevent above race and make invoke ->bdev_try_to_free_page() safe. Signed-off-by: Zhang Yi --- include/linux/fs.h | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/include/linux/fs.h b/include/linux/fs.h index ec8f3ddf4a6a..3c6a5c08c2df 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -41,6 +41,7 @@ #include #include #include +#include #include #include @@ -1547,6 +1548,13 @@ struct super_block { spinlock_t s_inode_wblist_lock; struct list_head s_inodes_wb; /* writeback inodes */ + + /* + * Count users who are using the super_block, used to protect + * umount filesystem concurrently with others. + */ + struct percpu_ref s_usage_counter; + wait_queue_head_t s_usage_waitq; } __randomize_layout; /* Helper functions so that in most cases filesystems will @@ -1765,6 +1773,27 @@ static inline bool sb_start_intwrite_trylock(struct super_block *sb) bool inode_owner_or_capable(struct user_namespace *mnt_userns, const struct inode *inode); +static inline void sb_usage_counter_release(struct percpu_ref *ref) +{ + struct super_block *sb; + + sb = container_of(ref, struct super_block, s_usage_counter); + wake_up(&sb->s_usage_waitq); +} + +static inline int sb_usage_counter_init(struct super_block *sb) +{ + init_waitqueue_head(&sb->s_usage_waitq); + return percpu_ref_init(&sb->s_usage_counter, sb_usage_counter_release, + 0, GFP_KERNEL); +} + +static inline void sb_usage_counter_wait(struct super_block *sb) +{ + percpu_ref_kill(&sb->s_usage_counter); + wait_event(sb->s_usage_waitq, percpu_ref_is_zero(&sb->s_usage_counter)); +} + /* * VFS helper functions.. */ -- 2.25.4