From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 869D8C4332F for ; Thu, 8 Dec 2022 22:01:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 258068E0006; Thu, 8 Dec 2022 17:01:57 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2082E8E0001; Thu, 8 Dec 2022 17:01:57 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0D1098E0006; Thu, 8 Dec 2022 17:01:57 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id F08FE8E0001 for ; Thu, 8 Dec 2022 17:01:56 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id CBD17C08CE for ; Thu, 8 Dec 2022 22:01:56 +0000 (UTC) X-FDA: 80220512232.27.3618A21 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf01.hostedemail.com (Postfix) with ESMTP id D61444000F for ; Thu, 8 Dec 2022 22:01:54 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=GTs6eaJp; spf=pass (imf01.hostedemail.com: domain of longman@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=longman@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1670536915; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=9MgINozTqdgeXroc8MSLx72iMYD01LxCIBLOKEVWwEw=; b=Cib4ML/o07y2J7Fz3mE7vSiQ0XERvT+r98gpMJmiHmcZ8BN/ZEwah1OrV0t6ts3b7KIaJs t/80Z5IRA45kszNhwfR5A36qCAn+H5dGr8ekXYZUpbQoyRNj/lv0i0b+rDDlsEhaS3Moc6 Dh1N0GRJ2AdXDbm1t+empVxOTsvjwxY= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=GTs6eaJp; spf=pass (imf01.hostedemail.com: domain of longman@redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=longman@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1670536915; a=rsa-sha256; cv=none; b=C44Rx87sxqSBAk+uMqaO1tolNihI3hmlrTZDbIAlVMsXOUnkRWXqkN4SJKbaHQHntXzqwO 1mwDP5N5WewEa/ArrwoKeMPOjkuKVxWv83OHbLu+saNTosRvYZcx+1x6OE0mAhCzE7lzLO Hi3dFuMTZeL79eEUeRNkCbLx2680PnU= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1670536914; 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=9MgINozTqdgeXroc8MSLx72iMYD01LxCIBLOKEVWwEw=; b=GTs6eaJpLrrRYrmkpdHzvzs1X50fRz4hRU0REKXZbXIz/uOsW4NoQcOKg6Au1aYua9XmVn eWlHNi9g7zjMm59gOpm4r57WIuLtJbBZLQXZMhKb7Wc9gt0HL0Aw8GoPGmmQjHwskXaRFQ rtknAS0SE5JGThMn73BmOsF1gFfrfYg= 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-214-obYlP2n7OM-MaJNCMDODjg-1; Thu, 08 Dec 2022 17:01:52 -0500 X-MC-Unique: obYlP2n7OM-MaJNCMDODjg-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 278BF806003; Thu, 8 Dec 2022 22:01:52 +0000 (UTC) Received: from llong.com (dhcp-17-153.bos.redhat.com [10.18.17.153]) by smtp.corp.redhat.com (Postfix) with ESMTP id C6BE017582; Thu, 8 Dec 2022 22:01:51 +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 3/3] blk-cgroup: Flush stats at blkgs destruction path Date: Thu, 8 Dec 2022 17:01:41 -0500 Message-Id: <20221208220141.2625775-4-longman@redhat.com> In-Reply-To: <20221208220141.2625775-1-longman@redhat.com> References: <20221208220141.2625775-1-longman@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 X-Rspamd-Queue-Id: D61444000F X-Stat-Signature: bgcuqnxg97usk7q4gdzq4qfhzdy1s6o5 X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1670536914-651307 X-HE-Meta: U2FsdGVkX18x1aXnzTb7xfrCweCgJuo/GO8BrG24W6Kaw7+q18TUVpEyu2xURgyITwlHcJYHCjYviOSB22pXyJGF7kmgKASrKyOqMZ+BMemn34xRu8eaiw2ae7ivgCb4azeXR4jTI0WByHWP+vxEmxDSxAfJBX1BE/ouTx+UndhJ0TIb0ERy29XFVm0nE4ZLHkdBhazTrx88ZBBJURoY/b+kC0Ghfdutv0s3EWefpQo1Os0hvUjzTFf4MPuUhtsqyRtp+EozjKQmYv8CHhkI9DuNbiNE4P6hoaLrEMeP/NerBrnE+IKAwOSoJBUApFV/ekEv5QcxUTUIPPmwZfSH1bRGpVmGOg2xKi5xCvTU9KHPp54efL9ilv5vNfEddvjNAhiG5w4DFYRyzpYDvHxdiwnJBiaPOtAWnUVHaDG1gZhYFg5gxfmLb9Bx/2nneHqtFkAF4SapEWfvES3pfmkIdQgWzioYKutJxWB93JoJUTj0tceRcIRAe2i7U3VhsqkOakTKTk0LZ+3lXVl3kYO2MbmwjNWTF5IbDpC0dx3XUHyIpHZw7hJRLpyNKH+KN/KZlGOoB/zmA9yC0+4BmOgEbvt1ccyKolJ7uveOrv+eQbPUkhQk1VSFTFoDZynRu8b+d9eo/jkc2cScfw1/T+3kMbGQABZcD0v6+b6zpzIN5TNq/KyOJGPSq6p3oA7RFMqh7ZfAPCZlX9zYfBftjexcuyVKeMqsmmbRC1Ay32ZFK8j6Ra7BQn5h8cvsnGYz3sqec3nXkzq/TQK1FBY2VJ3ZKf3Xl5toxAGO5reLJJUGN4ovvUIwKaqnw2/y9YP/ex1ikR+dX02dqKU6X+HJbl36ckmBFn1GVbztHGyHzgBBC/t+MP/anG4WlnTc5h6wHOz6GEcj9QepVDv2mY42DLtucWvjn9UWv8Xc X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 | 12 ++++++++++++ include/linux/cgroup.h | 1 + kernel/cgroup/rstat.c | 20 ++++++++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/block/blk-cgroup.c b/block/blk-cgroup.c index c466aef0d467..534f3baeb84a 100644 --- a/block/blk-cgroup.c +++ b/block/blk-cgroup.c @@ -1090,6 +1090,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. @@ -1099,6 +1101,16 @@ static void blkcg_destroy_blkgs(struct blkcg *blkcg) might_sleep(); + /* + * 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); + } + 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..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