Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp17355907rwd; Tue, 27 Jun 2023 01:40:46 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7izw4RjYwzIf+kRW5Ba8QSSWNyl8wQxuU+iEGVJXYkjzsq0lVwZ+irEp3xPmeZn92mtgrl X-Received: by 2002:a17:907:75e2:b0:98e:43ce:93fa with SMTP id jz2-20020a17090775e200b0098e43ce93famr5420084ejc.8.1687855246493; Tue, 27 Jun 2023 01:40:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687855246; cv=none; d=google.com; s=arc-20160816; b=uQkrExsTKSo4q+PSqYYUzxrrgkXwbHQDiqsgVViZgeTv23mf8KtFktp51IfZzDezx9 6lJNOoimEHKGuVQSIIyl8xmbhhQV6gDzaLpKoicsTwShn833UWnEG/hHKYvA+uCL2WyE 6b9FQn2vv4ajmkJmitHl7vE7nZUOjtfj++8e3u+1YTLI2L5AGE1Osdi928zKG0GfWfOY ofpjT/CSc84O1OSAkhwQeoiRcy41TQCQRfeb2tMuU+5+ijk3RR9OGP4Vqh3WVC1A24Rh F98hMnQE986+DV+dfDJihZctUCvcse839ht3pVzyrr48R/w1mvcp52AXdX5PpIzgBniC CePQ== 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 :dkim-signature; bh=60UzWG/75gz4NOILHLeD/9PkhGjuoyZUVYU3uGZdrp0=; fh=g9R67Jh3/bk856kmA8XVLoP0slgbpj3g5F2jAGnuQIc=; b=n7+2PB/7zElEEvFZizo/7oMHkSuLAAA6WakvzaHAKrbsynWjebOUcq4XYG8uObo7wS w3yxzXQZtDwCFPhGKcRqXOb/28VHM02dTKkkn52iZ18kYPIHtb8FoflZtmJSiX8/Ad8U PCHq+ockPGMPdmaMT5c51VSgtKCM4g830wWdNWDbyx5RmBvRJJ4DQ6os+g10QVw2i19S EdIc0IWX1LOWhQMftYW2O93W63Gjwz/F4A5fTrOsQzQKFmyopxqVNd12HFw2OOPu1JeH yCWa7I/qI0W0CkciQQfF3Pgx9022raj66BIqoTmNxsbH+AQuX9ox8gAedJhammR02aMO fJUg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=d4VoKrCd; dkim=neutral (no key) header.i=@suse.cz header.b=o83Na7FR; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ci8-20020a170906c34800b009889713506asi4050216ejb.304.2023.06.27.01.40.18; Tue, 27 Jun 2023 01:40:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-ext4-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=@suse.cz header.s=susede2_rsa header.b=d4VoKrCd; dkim=neutral (no key) header.i=@suse.cz header.b=o83Na7FR; spf=pass (google.com: domain of linux-ext4-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-ext4-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229459AbjF0Iee (ORCPT + 99 others); Tue, 27 Jun 2023 04:34:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40468 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229727AbjF0IeK (ORCPT ); Tue, 27 Jun 2023 04:34:10 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DEE7919A8; Tue, 27 Jun 2023 01:34:08 -0700 (PDT) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 79CA12185A; Tue, 27 Jun 2023 08:34:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1687854847; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=60UzWG/75gz4NOILHLeD/9PkhGjuoyZUVYU3uGZdrp0=; b=d4VoKrCdfYSufabLKfmo5vzGpuNe5gtVTH+CtlSi6IUeD4RCSmqX/svx4rQzuWyVPLQZ/8 WSrOkluHzHTjdkigUqBKFuJZ70nggDYOwym90vnbT8KCPaYff+JvbsCSr5a0cmYcMTaCu3 oCJMA59hUNMJTM5vfboHBqOhZUKnGVg= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1687854847; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=60UzWG/75gz4NOILHLeD/9PkhGjuoyZUVYU3uGZdrp0=; b=o83Na7FRpIdvCG2qsO+9Jb1VrUW06rkj81rLnH0lKvQ9hoDNQ655rTxof0Cgm4IITsVhwf SWY6NyFUNXJcZkCQ== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 6456013276; Tue, 27 Jun 2023 08:34:07 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id KrJ3GP+emmSiPwAAMHmgww (envelope-from ); Tue, 27 Jun 2023 08:34:07 +0000 Received: by quack3.suse.cz (Postfix, from userid 1000) id EA671A0762; Tue, 27 Jun 2023 10:34:06 +0200 (CEST) Date: Tue, 27 Jun 2023 10:34:06 +0200 From: Jan Kara To: Baokun Li Cc: Jan Kara , linux-fsdevel@vger.kernel.org, linux-ext4@vger.kernel.org, linux-kernel@vger.kernel.org, yi.zhang@huawei.com, yangerkun@huawei.com, chengzhihao1@huawei.com, yukuai3@huawei.com Subject: Re: [PATCH] quota: fix race condition between dqput() and dquot_mark_dquot_dirty() Message-ID: <20230627083406.hhjf55e2tqnwqaf6@quack3> References: <20230616085608.42435-1-libaokun1@huawei.com> <20230616152824.ndpgvkegvvip2ahh@quack3> <20230622145620.hk3bdjxtlr64gtzl@quack3> <20230626130957.kvfli23djxc2opkq@quack3> <2486ec73-55e0-00cb-fc76-97b9b285a9ce@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <2486ec73-55e0-00cb-fc76-97b9b285a9ce@huawei.com> X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham 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-ext4@vger.kernel.org Hello! On Mon 26-06-23 21:55:49, Baokun Li wrote: > On 2023/6/26 21:09, Jan Kara wrote: > > On Sun 25-06-23 15:56:10, Baokun Li wrote: > > > > > I think we can simply focus on the race between the DQ_ACTIVE_B flag and > > > > > the DQ_MOD_B flag, which is the core problem, because the same quota > > > > > should not have both flags. These two flags are protected by dq_list_lock > > > > > and dquot->dq_lock respectively, so it makes sense to add a > > > > > wait_on_dquot() to ensure the accuracy of DQ_ACTIVE_B. > > > > But the fundamental problem is not only the race with DQ_MOD_B setting. The > > > > dquot structure can be completely freed by the time > > > > dquot_claim_space_nodirty() calls dquot_mark_dquot_dirty() on it. That's > > > > why I think making __dquot_transfer() obey dquot_srcu rules is the right > > > > solution. > > > Yes, now I also think that making __dquot_transfer() obey dquot_srcu > > > rules is a better solution. But with inode->i_lock protection, why would > > > the dquot structure be completely freed? > > Well, when dquot_claim_space_nodirty() calls mark_all_dquot_dirty() it does > > not hold any locks (only dquot_srcu). So nothing prevents dquot_transfer() > > to go, swap dquot structure pointers and drop dquot references and after > > that mark_all_dquot_dirty() can use a stale pointer to call > > mark_dquot_dirty() on already freed memory. > > > No, this doesn't look like it's going to happen. The > mark_all_dquot_dirty() uses a pointer array pointer, the dquot in the > array is dynamically changing, so after swap dquot structure pointers, > mark_all_dquot_dirty() uses the new pointer, and the stale pointer is > always destroyed after swap, so there is no case of using the stale > pointer here. There is a case - CPU0 can prefetch the values from dquots[] array into its local cache, then CPU1 can update the dquots[] array (these writes can happily stay in CPU1 store cache invisible to other CPUs) and free the dquots via dqput(). Then CPU0 can pass the prefetched dquot pointers to mark_dquot_dirty(). There are no locks or memory barries preventing CPUs from ordering instructions and memory operations like this in the code... You can read Documentation/memory-barriers.txt about all the perils current CPU architecture brings wrt coordination of memory accesses among CPUs ;) Honza -- Jan Kara SUSE Labs, CR