Received: by 2002:a05:6358:16cd:b0:dc:6189:e246 with SMTP id r13csp1744505rwl; Fri, 4 Nov 2022 19:00:21 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7Z/8Gq0fObrlXgnwhf6717kCqp/MeUDCtKQNRWJEZfRPnpveBjqbdhHex5uVOSPTCdOE6+ X-Received: by 2002:a17:907:3e92:b0:7ad:d4f1:11d8 with SMTP id hs18-20020a1709073e9200b007add4f111d8mr28290728ejc.291.1667613621784; Fri, 04 Nov 2022 19:00:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667613621; cv=none; d=google.com; s=arc-20160816; b=0TyiLvFzPL0bcIKVvAy32pY/Dvlw3vU9O4F/gtxgdFWpF0K7rIHNFwz1olwjibtwx2 0wG305SHbEFzYbuPGLMMR1acWwAhs2nziBqn0jKVF6afs4MFsZgoPLmtQ3zVVPFO791b gEcn97F+hNd7oPyFj/H3izn/Cntr8R1eDK1HZ8MnOosBfTqGLFjenl7yRuHvRkoKLln7 ZzbfRk1DfC25Ks+gKYukzdnhhDNMbLG4/JBz4pEDTzfyGMm0Z8LeHcjRJapdEnGVJug0 UjKDC7KneMlyXh3DxBwYrEFVAJzAHSHV4qdRiOYIKWxp/Z5ouFDmBRlQhb2TlVpLK4BA FWpg== 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=TbSqpBbgk+NOPaOLl9NnIjbIwkdm/eU7seDomB5Bqqc=; b=FeWC5g3YcXZKQ1nDsFp3dbz9OFpnowt3aHI4u6J9S2Ay2OOns6CNvRku68K8MJDOrK v8k7HMhcu1B+Gf1P5EI1bi9h1UunYT4+D+MtAdOHaQpFxdgfafuZLgNmJ2XNYcGIor0c aosLRSa2ZoxEnd0vznokuR8PfCJhrkC+4B3qFS6lODqntE+QJgXSxOYOzT75btF/dXce YNJ3+71V5i1XVpZOOnUy8Sd0Ui4Zi4D4uqm6HHG9fomBa9gOl9PTWM5lJKRcTK3wvMsF ASnUPgDL3kUUfPtt/tDyFpfbx97AZDP7FZO6B601XOXNXIyjgMzHJN4t5UXf/ouCpOUJ IK5A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="f/wLx26g"; 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=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z5-20020a509e05000000b00461c12f1f4csi1123959ede.478.2022.11.04.18.59.58; Fri, 04 Nov 2022 19:00:21 -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=@redhat.com header.s=mimecast20190719 header.b="f/wLx26g"; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229540AbiKEBBS (ORCPT + 97 others); Fri, 4 Nov 2022 21:01:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52590 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229516AbiKEBBQ (ORCPT ); Fri, 4 Nov 2022 21:01:16 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 650802D1CC for ; Fri, 4 Nov 2022 17:59:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1667609968; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=TbSqpBbgk+NOPaOLl9NnIjbIwkdm/eU7seDomB5Bqqc=; b=f/wLx26gbYlgPhxDoJEQWTBQnYaRqJMRUjFLKILI5AATs4c1nwIib414MFr4HEfP6bansC XxNFweQKnxq50n4j6oSLtuy2VTPUIyl1l7TxZPXCQWKVu4ldPNaDVGvj20txR6DCvx/iIZ MAK/kY835QVpx5xEhoR1mKtnrw6kLmA= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-121-2BACaGxTOzyXSMlgs6nv3w-1; Fri, 04 Nov 2022 20:59:23 -0400 X-MC-Unique: 2BACaGxTOzyXSMlgs6nv3w-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D566F85A5B6; Sat, 5 Nov 2022 00:59:22 +0000 (UTC) Received: from llong.com (unknown [10.22.16.12]) by smtp.corp.redhat.com (Postfix) with ESMTP id 60D952166B26; Sat, 5 Nov 2022 00:59:22 +0000 (UTC) From: Waiman Long To: Tejun Heo , Jens Axboe Cc: cgroups@vger.kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, Ming Lei , Andy Shevchenko , Andrew Morton , =?UTF-8?q?Michal=20Koutn=C3=BD?= , Hillf Danton , Waiman Long Subject: [PATCH v10 3/3] blk-cgroup: Flush stats at blkgs destruction path Date: Fri, 4 Nov 2022 20:59:02 -0400 Message-Id: <20221105005902.407297-4-longman@redhat.com> In-Reply-To: <20221105005902.407297-1-longman@redhat.com> References: <20221105005902.407297-1-longman@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.1 on 10.11.54.6 X-Spam-Status: No, score=-3.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE 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 As noted by Michal, the blkg_iostat_set's in the lockless list hold reference to blkg's to protect against their removal. Those blkg's hold reference to blkcg. When a cgroup is being destroyed, cgroup_rstat_flush() is only called at css_release_work_fn() which is called when the blkcg reference count reaches 0. This circular dependency will prevent blkcg from being freed until some other events cause cgroup_rstat_flush() to be called to flush out the pending blkcg stats. To prevent this delayed blkcg removal, add a new cgroup_rstat_css_flush() function to flush stats for a given css and cpu and call it at the blkgs destruction path, blkcg_destroy_blkgs(), whenever there are still some pending stats to be flushed. This will ensure that blkcg reference count can reach 0 ASAP. Signed-off-by: Waiman Long --- block/blk-cgroup.c | 15 ++++++++++++++- include/linux/cgroup.h | 1 + kernel/cgroup/rstat.c | 20 ++++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/block/blk-cgroup.c b/block/blk-cgroup.c index 3e03c0d13253..57941d2a8ba3 100644 --- a/block/blk-cgroup.c +++ b/block/blk-cgroup.c @@ -1084,10 +1084,12 @@ struct list_head *blkcg_get_cgwb_list(struct cgroup_subsys_state *css) */ static void blkcg_destroy_blkgs(struct blkcg *blkcg) { + int cpu; + might_sleep(); + css_get(&blkcg->css); spin_lock_irq(&blkcg->lock); - while (!hlist_empty(&blkcg->blkg_list)) { struct blkcg_gq *blkg = hlist_entry(blkcg->blkg_list.first, struct blkcg_gq, blkcg_node); @@ -1110,6 +1112,17 @@ static void blkcg_destroy_blkgs(struct blkcg *blkcg) } spin_unlock_irq(&blkcg->lock); + + /* + * Flush all the non-empty percpu lockless lists. + */ + for_each_possible_cpu(cpu) { + struct llist_head *lhead = per_cpu_ptr(blkcg->lhead, cpu); + + if (!llist_empty(lhead)) + cgroup_rstat_css_cpu_flush(&blkcg->css, cpu); + } + css_put(&blkcg->css); } /** diff --git a/include/linux/cgroup.h b/include/linux/cgroup.h index 528bd44b59e2..6c4e66b3fa84 100644 --- a/include/linux/cgroup.h +++ b/include/linux/cgroup.h @@ -766,6 +766,7 @@ void cgroup_rstat_flush(struct cgroup *cgrp); void cgroup_rstat_flush_irqsafe(struct cgroup *cgrp); void cgroup_rstat_flush_hold(struct cgroup *cgrp); void cgroup_rstat_flush_release(void); +void cgroup_rstat_css_cpu_flush(struct cgroup_subsys_state *css, int cpu); /* * Basic resource stats. diff --git a/kernel/cgroup/rstat.c b/kernel/cgroup/rstat.c index 793ecff29038..910e633869b0 100644 --- a/kernel/cgroup/rstat.c +++ b/kernel/cgroup/rstat.c @@ -281,6 +281,26 @@ void cgroup_rstat_flush_release(void) spin_unlock_irq(&cgroup_rstat_lock); } +/** + * cgroup_rstat_css_cpu_flush - flush stats for the given css and cpu + * @css: target css to be flush + * @cpu: the cpu that holds the stats to be flush + * + * A lightweight rstat flush operation for a given css and cpu. + * Only the cpu_lock is being held for mutual exclusion, the cgroup_rstat_lock + * isn't used. + */ +void cgroup_rstat_css_cpu_flush(struct cgroup_subsys_state *css, int cpu) +{ + raw_spinlock_t *cpu_lock = per_cpu_ptr(&cgroup_rstat_cpu_lock, cpu); + + raw_spin_lock_irq(cpu_lock); + rcu_read_lock(); + css->ss->css_rstat_flush(css, cpu); + rcu_read_unlock(); + raw_spin_unlock_irq(cpu_lock); +} + int cgroup_rstat_init(struct cgroup *cgrp) { int cpu; -- 2.31.1