Received: by 2002:a05:6358:16cc:b0:ea:6187:17c9 with SMTP id r12csp11678288rwl; Tue, 3 Jan 2023 03:08:59 -0800 (PST) X-Google-Smtp-Source: AMrXdXvXOBNqUdyLvKzVRvpoU0CDRgq9gBAGPtX2KLXj6bXYmW4yk/Qxh3pPbKfU7qFNKI3FbKGG X-Received: by 2002:a17:903:40c2:b0:192:b93a:6cdb with SMTP id t2-20020a17090340c200b00192b93a6cdbmr12632029pld.64.1672744139473; Tue, 03 Jan 2023 03:08:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672744139; cv=none; d=google.com; s=arc-20160816; b=0fuBVDKn/U4V8rHCsIjxaYkjr5CmR9bQYdon8qj9La6UiN3Z05Gq7x7PfbFhQFgYVn OGZW/6cfuClKMAEI6ulC9btQv6sY5YP3JqTTMwW3pCUJkWJrpvM+FxjDZyRvn/yCn/+j LcIz0UztQvOoxwfFudT7cbMxuyTb3fwye76jVJ85nOxE8aaHSlvwwq6rqkj+uYCar3ww +P7s5XTVQ5aF9JG5DxGkEWBG72ajhnPqBTjJ6jfM+4zw5EafolO2wV5CgZteh8Rb9mVM qgQjJw1box/RwJQBfL1V7cB7hJ7MjlH2ATmwf4Hb1y6QLejwWJR3dMG3hx8XnJf2zg/t tBfw== 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=S0fpHD/aVGPATEkhmOtr6aAZx/NtaItvDcnKgARuLII=; b=alMZkm9kKZWAPl3RhUdB+BKXXme2VKQPiuPt4bHUyDHrXap3zOMusVBJRZSP02Befm FAnlexwB4M2I9j+KDq10hAr+CZc2C/HlPZlH692TXXciOJx0L/iph1Unm2+LhonJHTZ/ qfY1p2Vbc4wxfJGowp0Cvvr66JRj7c3wbT96e3rla8uA4lbciOCyG+/IJ7jm8Mq07izK 9rTNE2VAqB4hLnQJCS/ylZSFUhuzMcg7PwAegwmglXmM34yy/rBt/FC98Gku86eNqZLh /4HMNnrvoMcngcw5gRAguC4yd5ZYjTm/FyIoJ1IfZfGpFmCP46eK/QOy8bWjW2tQO48H gvlQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.cz header.s=susede2_rsa header.b=H4mcgN5i; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519 header.b=JkKdihts; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id jd2-20020a170903260200b0019278b1adffsi22736190plb.189.2023.01.03.03.08.51; Tue, 03 Jan 2023 03:08:59 -0800 (PST) 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=@suse.cz header.s=susede2_rsa header.b=H4mcgN5i; dkim=neutral (no key) header.i=@suse.cz header.s=susede2_ed25519 header.b=JkKdihts; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233103AbjACKak (ORCPT + 61 others); Tue, 3 Jan 2023 05:30:40 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55036 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232345AbjACKai (ORCPT ); Tue, 3 Jan 2023 05:30:38 -0500 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 52E4FF5BD; Tue, 3 Jan 2023 02:30:37 -0800 (PST) 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-out2.suse.de (Postfix) with ESMTPS id C4F6361233; Tue, 3 Jan 2023 10:30:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1672741835; 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=S0fpHD/aVGPATEkhmOtr6aAZx/NtaItvDcnKgARuLII=; b=H4mcgN5iwaCxNhoNI3/q08ZUacejngcauGzDH+dk+dpcF9FQBIIsXzQV1LrxwH0/ku6gAM pddlOUcy2LO9usoiT+Aw339rgv7i5minKnyUKngRPN7YVjuSwB59vodu9U9GcxjlitZdxA OJM8wz/kcVDn+sgiPbxZfzoVc3PjGYw= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1672741835; 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=S0fpHD/aVGPATEkhmOtr6aAZx/NtaItvDcnKgARuLII=; b=JkKdihtsGYCvv+QGCsisIZsWGCg6twEmkStVBFoz31bLwCuwwwCMa76eH88Tlt4BZ06zYd SuInez8hHXd36TAg== 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 AD5971392B; Tue, 3 Jan 2023 10:30:35 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id GUhPKssDtGOHRAAAMHmgww (envelope-from ); Tue, 03 Jan 2023 10:30:35 +0000 Received: by quack3.suse.cz (Postfix, from userid 1000) id 8AFEBA0742; Tue, 3 Jan 2023 11:30:34 +0100 (CET) Date: Tue, 3 Jan 2023 11:30:34 +0100 From: Jan Kara To: Yu Kuai Cc: jack@suse.cz, paolo.valente@linaro.org, axboe@kernel.dk, tj@kernel.org, josef@toxicpanda.com, linux-block@vger.kernel.org, cgroups@vger.kernel.org, linux-kernel@vger.kernel.org, yukuai3@huawei.com, yi.zhang@huawei.com, yangerkun@huawei.com Subject: Re: [PATCH] block, bfq: switch 'bfqg->ref' to use atomic refcount apis Message-ID: <20230103103034.6wtj7fzyoqdsqdqy@quack3> References: <20230103084755.1256479-1-yukuai1@huaweicloud.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230103084755.1256479-1-yukuai1@huaweicloud.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 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-kernel@vger.kernel.org On Tue 03-01-23 16:47:55, Yu Kuai wrote: > From: Yu Kuai > > The updating of 'bfqg->ref' should be protected by 'bfqd->lock', however, > during code review, we found that bfq_pd_free() update 'bfqg->ref' > without holding the lock, which is problematic: > > 1) bfq_pd_free() triggered by removing cgroup is called asynchronously; > 2) bfqq will grab bfqg reference, and exit bfqq will drop the reference, > which can concurrent with 1). > > Unfortunately, 'bfqd->lock' can't be held here because 'bfqd' might already > be freed in bfq_pd_free(). Fix the problem by using atomic refcount apis. > > Signed-off-by: Yu Kuai The patch looks good to me. Feel free to add: Reviewed-by: Jan Kara Honza > --- > Changes from RFC: > - refcount_dec_and_test(bfqg->ref) -> refcount_dec_and_test(&bfqg->ref) > > block/bfq-cgroup.c | 8 +++----- > block/bfq-iosched.h | 2 +- > 2 files changed, 4 insertions(+), 6 deletions(-) > > diff --git a/block/bfq-cgroup.c b/block/bfq-cgroup.c > index 1b2829e99dad..7d9b15f0dbd5 100644 > --- a/block/bfq-cgroup.c > +++ b/block/bfq-cgroup.c > @@ -316,14 +316,12 @@ struct bfq_group *bfqq_group(struct bfq_queue *bfqq) > > static void bfqg_get(struct bfq_group *bfqg) > { > - bfqg->ref++; > + refcount_inc(&bfqg->ref); > } > > static void bfqg_put(struct bfq_group *bfqg) > { > - bfqg->ref--; > - > - if (bfqg->ref == 0) > + if (refcount_dec_and_test(&bfqg->ref)) > kfree(bfqg); > } > > @@ -530,7 +528,7 @@ static struct blkg_policy_data *bfq_pd_alloc(gfp_t gfp, struct request_queue *q, > } > > /* see comments in bfq_bic_update_cgroup for why refcounting */ > - bfqg_get(bfqg); > + refcount_set(&bfqg->ref, 1); > return &bfqg->pd; > } > > diff --git a/block/bfq-iosched.h b/block/bfq-iosched.h > index 41aa151ccc22..466e4865ace6 100644 > --- a/block/bfq-iosched.h > +++ b/block/bfq-iosched.h > @@ -928,7 +928,7 @@ struct bfq_group { > char blkg_path[128]; > > /* reference counter (see comments in bfq_bic_update_cgroup) */ > - int ref; > + refcount_t ref; > /* Is bfq_group still online? */ > bool online; > > -- > 2.31.1 > -- Jan Kara SUSE Labs, CR