Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp8868880rwb; Tue, 13 Dec 2022 11:27:58 -0800 (PST) X-Google-Smtp-Source: AA0mqf7/maeEUy7QQsAFWF89rfY80kdQp6bxh1SJnZoI1dHrF18vPCoumgJahwCbB8ulONREAL4B X-Received: by 2002:a05:6a20:b295:b0:ac:6a79:29a with SMTP id ei21-20020a056a20b29500b000ac6a79029amr20693155pzb.54.1670959678012; Tue, 13 Dec 2022 11:27:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670959677; cv=none; d=google.com; s=arc-20160816; b=KREN9ikW5GLz/7aY06pU+rwtmlh2mz3k+0mSNTgW8KbPqE0e5odRt1U1Ek4vwwJfHg Vkkjod7nCUfJ+VPEJk8ZxKfbelGsnyHbt5Jm+XNUGNf9TrI0GjdI6UdFlv6ALFXNbImR LMBwpmOa8sGJ5eqJLk1vNqYHXvwQu8V0wf/xV+Qul5CUNbwicEpA0h+BhTupIBPH2oVF ncScAOa1jclUjjM5qeSLSSRykU6vj6QaIjO2AJVpyQKcIPA9PNYUB2JX6yjlkMHA/Scl 7C9Yrq5anknI414elfgiD613TnsG21N2y1gw7zeChB4Ra+it5ihbl+NDw/zij4BX+wJL rJYw== 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=h+iH+gyUSP9RmZybF0rRU4rA5/id1U9+eBsl2PUnAPQ=; b=TeJq59MP3G5cbloUM4BugVhbvOYlVJL+CJs++3eg8Lee5thverqzc8KzKTeYHnMzUI US14/DT4g77OUwYqWEE2HpMZsGJFyrQEHyOZM57IDli1Tu/cV2XwH/wx8WsYrWV1BUob 1XJjUofsRcBO4XBYXdoiOFIOQDr02w5NwJrMN6sldlpvU/pBDcXSSp8TOr5A3OTLU68L ya4w0OLWRecQp4br7r0ylLpJXg6ARJIVJyv8eZoTOEwvBlGoRDJMTTDUsWnOlkDlfH9X 8c3tZglkuM9oUhH0KrIVa8z1k2nok+/Ngfy5/pCTcdquJmI+lDThTsOHAHHt/EYcxIW3 1cNw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Li7zuSVU; 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 v64-20020a638943000000b00478e7f82dd3si12787052pgd.543.2022.12.13.11.27.48; Tue, 13 Dec 2022 11:27:57 -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=@redhat.com header.s=mimecast20190719 header.b=Li7zuSVU; 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 S236412AbiLMSp7 (ORCPT + 72 others); Tue, 13 Dec 2022 13:45:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40310 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236636AbiLMSpt (ORCPT ); Tue, 13 Dec 2022 13:45:49 -0500 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 870CB24F20 for ; Tue, 13 Dec 2022 10:45:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1670957103; 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=h+iH+gyUSP9RmZybF0rRU4rA5/id1U9+eBsl2PUnAPQ=; b=Li7zuSVUekrYZlysizQSX2K/ojAmuTLtWF8FBzUEQPm0tOmJcTGKF1Dt15m1icV5uOrak0 ZSYcilF01D/lKa5Ul19ZqPCyuRymM84xB9Sexcey/bn20g42FAUGHX3e5uIYuAtqpPZ3Iu 5wOHNMl21zU0h/fCSTdB7ZuFjHdtnC0= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-617-cFUaBO_WOZqWlEoTA3QSDA-1; Tue, 13 Dec 2022 13:45:00 -0500 X-MC-Unique: cFUaBO_WOZqWlEoTA3QSDA-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 66E001C008A7; Tue, 13 Dec 2022 18:44:59 +0000 (UTC) Received: from llong.com (unknown [10.22.32.205]) by smtp.corp.redhat.com (Postfix) with ESMTP id A8242C15BA0; Tue, 13 Dec 2022 18:44:58 +0000 (UTC) From: Waiman Long To: Jens Axboe , Tejun Heo , Josef Bacik , Zefan Li , Johannes Weiner , Andrew Morton Cc: cgroups@vger.kernel.org, linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, =?UTF-8?q?Michal=20Koutn=C3=BD?= , "Dennis Zhou (Facebook)" , Waiman Long Subject: [PATCH-block v3 2/2] blk-cgroup: Flush stats at blkgs destruction path Date: Tue, 13 Dec 2022 13:44:46 -0500 Message-Id: <20221213184446.50181-3-longman@redhat.com> In-Reply-To: <20221213184446.50181-1-longman@redhat.com> References: <20221213184446.50181-1-longman@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Spam-Status: No, score=-2.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 Acked-by: Tejun Heo --- block/blk-cgroup.c | 15 +++++++++++++++ include/linux/cgroup.h | 1 + kernel/cgroup/rstat.c | 18 ++++++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/block/blk-cgroup.c b/block/blk-cgroup.c index ca28306aa1b1..ddd27a714d3e 100644 --- a/block/blk-cgroup.c +++ b/block/blk-cgroup.c @@ -1084,6 +1084,8 @@ struct list_head *blkcg_get_cgwb_list(struct cgroup_subsys_state *css) */ static void blkcg_destroy_blkgs(struct blkcg *blkcg) { + int cpu; + /* * blkcg_destroy_blkgs() shouldn't be called with all the blkcg * references gone. @@ -1093,6 +1095,19 @@ static void blkcg_destroy_blkgs(struct blkcg *blkcg) might_sleep(); + /* + * Flush all the non-empty percpu lockless lists so as to release + * the blkg references held by those lists which, in turn, may + * allow the blkgs to be freed and release their references to + * blkcg speeding up its freeing. + */ + 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); + } + spin_lock_irq(&blkcg->lock); while (!hlist_empty(&blkcg->blkg_list)) { 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..2e44be44351f 100644 --- a/kernel/cgroup/rstat.c +++ b/kernel/cgroup/rstat.c @@ -281,6 +281,24 @@ 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); + css->ss->css_rstat_flush(css, cpu); + raw_spin_unlock_irq(cpu_lock); +} + int cgroup_rstat_init(struct cgroup *cgrp) { int cpu; -- 2.31.1