Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp4745382rwl; Mon, 10 Apr 2023 16:27:35 -0700 (PDT) X-Google-Smtp-Source: AKy350YFpSJUNFLpyKQIm15dkfpdIZSGql4NOU1Few0pCAA6RxaDxEFAbHc/EccXF6DYn9pQPh7O X-Received: by 2002:a17:906:b197:b0:92b:846d:8928 with SMTP id w23-20020a170906b19700b0092b846d8928mr8648371ejy.65.1681169255609; Mon, 10 Apr 2023 16:27:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1681169255; cv=none; d=google.com; s=arc-20160816; b=fIjSLlrOzp6HsLUqdpwgwKnGJ1y1ccKkcqIRF4em1lKK2EQSOhr56qYpZEYMO6uduR HsC45Ys0RNEfBGk6cZOma2ojVcw8ntKWIwFP2rm5FRJSp0bahEb+YJysl5/Z6vavcVil Nuy09EHgsQZZIulkO3DPT4j9IojyX5a9n/s4dtL6DYZdN15QitvtUT4m8rWhAkBN/opc vt5nf7iGyrSjHQk7Fox31pDPLCOWPYV8gCoYjpdHOgDLR6JZkYExU5Wt5a9YGI2HsrAM /83NbA49pg9DXF4Gbto2ejEogp1x+BIluGIh7lpt82gh0jlCZn7vn7IuJoxEuiLDIpj4 9fQw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=w1tH3cCWEn8tjZpJaqhgbOeNza+BQDMDB7Gu4heqh7E=; b=Rd0RPF4CXOT4CTq7XfgtugGF7+m3SsfizEjSaGfWDBrPKyPW7L2F51yG/fEO78X+HS 2Y4wFFe/+BEHJimICD9gf7IddNbnNmHLGD2dO14RSlYT2VzsAoRrUBASfpZahqoEQ1Me 7NAYaGC7kMoa1FQ48byjIHPF6HNDcXIettYOFNMMJ5hbxzmLZNkof+ax/iFae4mXyAgM qo5PZ7nxNz5u/1pYlAAw6fRmjnoH1QgyycxEsZrjBnW9pXAMRJbccaXSKV0MAdpyrLhq O67YU4eSrZarSONAR3KAH+zh/ord8u9zBe0C+WVr5yz5J2r1POWwP/YWx2eEEf4E5TVM tEcA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=QLCn1o5C; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bk10-20020a170906b0ca00b00940e715a40dsi9676269ejb.408.2023.04.10.16.27.11; Mon, 10 Apr 2023 16:27:35 -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=@kernel.org header.s=k20201202 header.b=QLCn1o5C; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229827AbjDJX0H (ORCPT + 99 others); Mon, 10 Apr 2023 19:26:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34128 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229523AbjDJX0G (ORCPT ); Mon, 10 Apr 2023 19:26:06 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9F7B32109 for ; Mon, 10 Apr 2023 16:26:01 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 3ACEA61FF5 for ; Mon, 10 Apr 2023 23:26:01 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 88396C433EF; Mon, 10 Apr 2023 23:26:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1681169160; bh=k5F1BKx94FTCrzsmWl/DeGGautLN/4tWce1bWlL7SUE=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=QLCn1o5CPzO1qNasSSrgJbwVbR3j1cOJOrWztz134v8nfj3/2p42N/XrNhAIPStet eAzUW4Ck+8jKdM5fJZQV+ri77zPTSBpZLqt4Zj3duDcXleTJ2Z+pLMGRtP0cHiWsRT AGBSMGC+JvqRzOdcukzFuryO2yE78d2M3/0VjEqjI/wRP+nCZsht0QmqFU3ilXxaw4 FswZMs6sFAE+E12FbM0IbOQdwPGvGHTvQIM2sisL0XAdoVA/cH3dt320QlcrzAk8dd 1A5m2PssfNqj0mmCsObgde11BgIM8vvBr3S4UUDzMqnQileOfnhEexnPNIzjFdhkri y1Fz6nVcUlBRg== Date: Mon, 10 Apr 2023 16:25:58 -0700 From: Jaegeuk Kim To: Chao Yu Cc: linux-f2fs-devel@lists.sourceforge.net, linux-kernel@vger.kernel.org Subject: Re: [PATCH RFC] f2fs: fix to use spinlock to avoid page.private update race Message-ID: References: <20230410093912.2184557-1-chao@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230410093912.2184557-1-chao@kernel.org> X-Spam-Status: No, score=-5.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI,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 On 04/10, Chao Yu wrote: > There may be subtle race condition, make PagePrivate and page_private > being inconsistent, result in decreasing page count incorrectly, > introduce a per-inode spinlock to avoid such condition. No...what have you found? The set/clear.. were supposed to be done in page_lock, and checking the flag should not corrupt any memory. > > Signed-off-by: Chao Yu > --- > fs/f2fs/f2fs.h | 19 ++++++++++++++++++- > fs/f2fs/super.c | 2 ++ > 2 files changed, 20 insertions(+), 1 deletion(-) > > diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h > index c378aedcadea..6b31bef5853e 100644 > --- a/fs/f2fs/f2fs.h > +++ b/fs/f2fs/f2fs.h > @@ -856,6 +856,8 @@ struct f2fs_inode_info { > > unsigned int atomic_write_cnt; > loff_t original_i_size; /* original i_size before atomic write */ > + > + spinlock_t private_lock; /* protect page->private */ > }; > > static inline void get_read_extent_info(struct extent_info *ext, > @@ -1413,21 +1415,28 @@ static inline bool page_private_##name(struct page *page) \ > test_bit(PAGE_PRIVATE_##flagname, &page_private(page)); \ > } > > +static inline struct f2fs_inode_info *F2FS_I(struct inode *inode); > #define PAGE_PRIVATE_SET_FUNC(name, flagname) \ > static inline void set_page_private_##name(struct page *page) \ > { \ > + unsigned long flags; \ > + spin_lock_irqsave(&F2FS_I(page->mapping->host)->private_lock, flags); \ > if (!PagePrivate(page)) \ > attach_page_private(page, (void *)page->private); \ > set_bit(PAGE_PRIVATE_NOT_POINTER, &page_private(page)); \ > set_bit(PAGE_PRIVATE_##flagname, &page_private(page)); \ > + spin_unlock_irqrestore(&F2FS_I(page->mapping->host)->private_lock, flags); \ > } > > #define PAGE_PRIVATE_CLEAR_FUNC(name, flagname) \ > static inline void clear_page_private_##name(struct page *page) \ > { \ > + unsigned long flags; \ > + spin_lock_irqsave(&F2FS_I(page->mapping->host)->private_lock, flags); \ > clear_bit(PAGE_PRIVATE_##flagname, &page_private(page)); \ > - if (page_private(page) == BIT(PAGE_PRIVATE_NOT_POINTER)) \ > + if (page_private(page) == (BIT(PAGE_PRIVATE_NOT_POINTER))) \ > detach_page_private(page); \ > + spin_unlock_irqrestore(&F2FS_I(page->mapping->host)->private_lock, flags); \ > } > > PAGE_PRIVATE_GET_FUNC(nonpointer, NOT_POINTER); > @@ -1456,17 +1465,25 @@ static inline unsigned long get_page_private_data(struct page *page) > > static inline void set_page_private_data(struct page *page, unsigned long data) > { > + unsigned long flags; > + > + spin_lock_irqsave(&F2FS_I(page->mapping->host)->private_lock, flags); > if (!PagePrivate(page)) > attach_page_private(page, 0); > set_bit(PAGE_PRIVATE_NOT_POINTER, &page_private(page)); > page_private(page) |= data << PAGE_PRIVATE_MAX; > + spin_unlock_irqrestore(&F2FS_I(page->mapping->host)->private_lock, flags); > } > > static inline void clear_page_private_data(struct page *page) > { > + unsigned long flags; > + > + spin_lock_irqsave(&F2FS_I(page->mapping->host)->private_lock, flags); > page_private(page) &= GENMASK(PAGE_PRIVATE_MAX - 1, 0); > if (page_private(page) == BIT(PAGE_PRIVATE_NOT_POINTER)) > detach_page_private(page); > + spin_unlock_irqrestore(&F2FS_I(page->mapping->host)->private_lock, flags); > } > > /* For compression */ > diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c > index a1b570a5e50f..555424dd85fd 100644 > --- a/fs/f2fs/super.c > +++ b/fs/f2fs/super.c > @@ -1419,6 +1419,8 @@ static struct inode *f2fs_alloc_inode(struct super_block *sb) > init_f2fs_rwsem(&fi->i_gc_rwsem[WRITE]); > init_f2fs_rwsem(&fi->i_xattr_sem); > > + spin_lock_init(&fi->private_lock); > + > /* Will be used by directory only */ > fi->i_dir_level = F2FS_SB(sb)->dir_level; > > -- > 2.25.1