From patchwork Thu Dec 8 22:01:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Waiman Long X-Patchwork-Id: 13068978 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 87C62C4332F for ; Thu, 8 Dec 2022 22:02:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B33568E0009; Thu, 8 Dec 2022 17:02:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A6F408E0008; Thu, 8 Dec 2022 17:02:00 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 899E58E0009; Thu, 8 Dec 2022 17:02:00 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 7050B8E0008 for ; Thu, 8 Dec 2022 17:02:00 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 2167D80F59 for ; Thu, 8 Dec 2022 22:02:00 +0000 (UTC) X-FDA: 80220512400.26.4218032 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf25.hostedemail.com (Postfix) with ESMTP id 877F4A001E for ; Thu, 8 Dec 2022 22:01:58 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=hCCgpFcu; spf=pass (imf25.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=1670536918; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=+MsWk87quHbEwHCR1sVYteGgGZjHdGBkMWIDf/Jp0nk=; b=knljS9mVehCjt8/EQMa4JphvnxxnVSCJHqMjv588MyoCcnBMPTIiPN/xVJt4lsnbDvGmb7 Nxc0OW6+JM7zCQaqemZkWaFwLlu4gzcC6lnbRS1PwLQWJnftboJVjHecleiSxb4V2AZOjP 93kmVyubcBcrmwuwowGjibvyIHJd0aU= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=hCCgpFcu; spf=pass (imf25.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=1670536918; a=rsa-sha256; cv=none; b=1zrao+NCPzdfdAW4EqDzUd8lg8C23xriyIAtorztyOuq98qIHJfRm2NYxKw61LDTWUplN2 PCcDeo4bRHnp75fvfjHUPRVRi8wDnkD7PmTJRPO1Rcx6kylo4qrUm+2WcoT32UQCKsCyPT xzNaq13fq8AfJhEcnSHeGe3KiLUBlKI= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1670536917; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=+MsWk87quHbEwHCR1sVYteGgGZjHdGBkMWIDf/Jp0nk=; b=hCCgpFcuNCNpZLax9qMCrlOc6ERZDGNA9nU96/psTvhIT9P/EtxGXBmozWXTfqpp5gi4Ts +qZTiH2xQPNuygMjuIOP0E9LNnLNdjglyxjAG6Pj8ME6YlFndHhFhLy6B3WbfaWldN3AN8 e5GuhTs6ln70BBP9A02zAoRaGNvSyqI= 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-658-PtZEtElbO9W4Ys0KShUpYA-1; Thu, 08 Dec 2022 17:01:52 -0500 X-MC-Unique: PtZEtElbO9W4Ys0KShUpYA-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 60E4785A588; Thu, 8 Dec 2022 22:01:51 +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 F079E17582; Thu, 8 Dec 2022 22:01:50 +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_Koutn?= =?utf-8?q?=C3=BD?= , "Dennis Zhou (Facebook)" , Waiman Long , Yi Zhang Subject: [PATCH-block 1/3] bdi, blk-cgroup: Fix potential UAF of blkcg Date: Thu, 8 Dec 2022 17:01:39 -0500 Message-Id: <20221208220141.2625775-2-longman@redhat.com> In-Reply-To: <20221208220141.2625775-1-longman@redhat.com> References: <20221208220141.2625775-1-longman@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 877F4A001E X-Rspam-User: X-Stat-Signature: 3sswxxuewttryt6rh4f19jqfbwt8ngoi X-HE-Tag: 1670536918-802565 X-HE-Meta: U2FsdGVkX1+LpBsTDCP50hlkdFS6Ez+kzIyXDstFDBZBba/ymeNXKWx2zv0FXjgo+/IQyC0gyryiaxhvOu5BdqNUecDNEEqDUXNbMkMnSRvsxZyPjWqWCSBcEKWlyoFTOM3u876NM7HoGxE21A+7YdHzs419daxLHSqAfhUKWUzXaMQD3cv6XLCBRvGxVvsKgFSpaaj6wxuCuoLedQyxZWy39GlAGFW6lUrNCGQGnYiYPSZbAZ7Cx/aEjyL4oZwO1I8dp+DOyL825kshyXxBkmJ39QsMHZVu/ezqjOp00XPUpOFHAAX1VtKGLglDO7ey9G17nM1cOzS4VeMVNCRh+0Nbm8HeiRwyj1WDzWLpRjhpWK8mjWBgH8Nn4ohWBSvikdfcxlJPUjrxq4KrTMUlhoPS7w6ls6oWqJHlBOntgl3dR3KJPAWFmoZ1LW5v3V91TxsdQrt2595eJkCC0P3q3EGynBR/204l5IeHQaPTPUuBQCrmgKzMlXfX6vIE4s8nAWUV+IYSBjI/9P0hTpNewfuTmmsUr+GZ8PeBE7jxOiKuBVFSank5t1S3PNUeBldclhbBUUz2CnrhQ2oH56204UuNdWg2oBC+bkZE3qEQVJky9Yn35YGqgBBHXE0LooC3VuSnmVR17glBo+WMcjGRZLiqPNRaW1DpjdnrcOBsVU5a0fw+Ep11H2tQHjW/BPSMuORoZTg4Li0wlgPMxhKXE6YW4PcCc2FVxYlRijjcg3RITaTGI05YTeZC4X2X92m4Kzh2/vT/2SUZB63113L0vnkvmMnZ4l7D9xdyoJYA47WjuuLUINRwpMAsR/wYQIgC18XRuc18ptxkCYo/Nwh3ywhe0Q/D1p5eN+/CbygfzMHuErRE61ItQLeFFsC2WhbX 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: Commit 59b57717fff8 ("blkcg: delay blkg destruction until after writeback has finished") delayed call to blkcg_destroy_blkgs() to cgwb_release_workfn(). However, it is done after a css_put() of blkcg which may be the final put that causes the blkcg to be freed as RCU read lock isn't held. Another place where blkcg_destroy_blkgs() can be called indirectly via blkcg_unpin_online() is from the offline_css() function called from css_killed_work_fn(). Over there, the potentially final css_put() call is issued after offline_css(). By adding a css_tryget() into blkcg_destroy_blkgs() and warning its failure, the following stack trace was produced in a test system on bootup. [ 34.254240] RIP: 0010:blkcg_destroy_blkgs+0x16a/0x1a0 : [ 34.339943] Call Trace: [ 34.344510] blkcg_unpin_online+0x38/0x60 [ 34.348523] cgwb_release_workfn+0x6a/0x200 [ 34.352708] process_one_work+0x1e5/0x3b0 [ 34.360758] worker_thread+0x50/0x3a0 [ 34.368447] kthread+0xd9/0x100 [ 34.376386] ret_from_fork+0x22/0x30 This confirms that a potential UAF situation can really happen in cgwb_release_workfn(). Fix that by delaying the css_put() until after the blkcg_unpin_online() call. Also use css_tryget() in blkcg_destroy_blkgs() and issue a warning if css_tryget() fails. The reproducing system can no longer produce a warning with this patch. All the runnable block/0* tests including block/027 were run successfully without failure. Fixes: 59b57717fff8 ("blkcg: delay blkg destruction until after writeback has finished") Suggested-by: Michal Koutný Reported-by: Yi Zhang Signed-off-by: Waiman Long --- block/blk-cgroup.c | 8 ++++++++ mm/backing-dev.c | 8 ++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/block/blk-cgroup.c b/block/blk-cgroup.c index 1bb939d3b793..21cc88349f21 100644 --- a/block/blk-cgroup.c +++ b/block/blk-cgroup.c @@ -1084,6 +1084,13 @@ struct list_head *blkcg_get_cgwb_list(struct cgroup_subsys_state *css) */ static void blkcg_destroy_blkgs(struct blkcg *blkcg) { + /* + * blkcg_destroy_blkgs() shouldn't be called with all the blkcg + * references gone. + */ + if (WARN_ON_ONCE(!css_tryget(&blkcg->css))) + return; + might_sleep(); spin_lock_irq(&blkcg->lock); @@ -1110,6 +1117,7 @@ static void blkcg_destroy_blkgs(struct blkcg *blkcg) } spin_unlock_irq(&blkcg->lock); + css_put(&blkcg->css); } /** diff --git a/mm/backing-dev.c b/mm/backing-dev.c index c30419a5e119..36f75b072325 100644 --- a/mm/backing-dev.c +++ b/mm/backing-dev.c @@ -390,11 +390,15 @@ static void cgwb_release_workfn(struct work_struct *work) wb_shutdown(wb); css_put(wb->memcg_css); - css_put(wb->blkcg_css); mutex_unlock(&wb->bdi->cgwb_release_mutex); - /* triggers blkg destruction if no online users left */ + /* + * Triggers blkg destruction if no online users left + * The final blkcg css_put() has to be done after blkcg_unpin_online() + * to avoid use-after-free. + */ blkcg_unpin_online(wb->blkcg_css); + css_put(wb->blkcg_css); fprop_local_destroy_percpu(&wb->memcg_completions); From patchwork Thu Dec 8 22:01:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Waiman Long X-Patchwork-Id: 13068977 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 D0F1EC001B2 for ; Thu, 8 Dec 2022 22:01:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B10958E0001; Thu, 8 Dec 2022 17:01:58 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A46AA8E0009; Thu, 8 Dec 2022 17:01:58 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7D6508E0008; Thu, 8 Dec 2022 17:01:58 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 686E08E0001 for ; Thu, 8 Dec 2022 17:01:58 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 4433580EBF for ; Thu, 8 Dec 2022 22:01:58 +0000 (UTC) X-FDA: 80220512316.25.A817258 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf13.hostedemail.com (Postfix) with ESMTP id B41BB20018 for ; Thu, 8 Dec 2022 22:01:56 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=ZWrHUMSa; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf13.hostedemail.com: domain of longman@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=longman@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1670536916; 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=s3VQJPU/3HC+t8N1wzyewiUON5nIdBJkilNRlkYPV+o=; b=aMCPxN7z1hiFtTC8ygUUF/gKOyodC0GuzlSnuvoOXLRhaMVwhHXnGM7kzqNiPuKhn9FuZk PJg5aHhkByOgB5WFG2QPFWbqIM56gI3e0AEfT2P9Y6Ua7x+NJQd8I+GtBGgWU+TdmFL2Fz iCbcyoTQf/F5wN41XyilhtTpsR/koII= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=ZWrHUMSa; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf13.hostedemail.com: domain of longman@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=longman@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1670536916; a=rsa-sha256; cv=none; b=IUDq62OmY49pGhSNFpwK4KWLMGzY9QoKyRj3aRG0R2G/mHd7RPWahmu1nieBuLW+bEMDHQ dMl9N8tcNSppEfhXZhBvkpDpOZbNWfhLy4zSrnrfN/CSC1lFeLmNPFV9t75bHXAj649rPc +hMoMLbE0P2xPbwpidhv7h9JTejeVBU= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1670536916; 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=s3VQJPU/3HC+t8N1wzyewiUON5nIdBJkilNRlkYPV+o=; b=ZWrHUMSaVnUKIbvCqVQcnWHUZEmlP4cyr+cBjBpS0U6cKYmoFiEtPIqQz2PdA4OKQ01B0s Df0ysUP0/dNhHW/tSE7+17cAAd/yYWpDvnbgRT/7FOrblCMffEISRh3bEHb89ufKrT14hp 9NYDNK/iYAdUyOnq+xf4zvKMygeVRu0= 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-353-RBCKYMz_NCKOObEEZe_ADg-1; Thu, 08 Dec 2022 17:01:52 -0500 X-MC-Unique: RBCKYMz_NCKOObEEZe_ADg-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 BE113806001; Thu, 8 Dec 2022 22:01:51 +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 6968842222; 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_Koutn?= =?utf-8?q?=C3=BD?= , "Dennis Zhou (Facebook)" , Waiman Long Subject: [PATCH-block 2/3] blk-cgroup: Don't flush a blkg if destroyed Date: Thu, 8 Dec 2022 17:01:40 -0500 Message-Id: <20221208220141.2625775-3-longman@redhat.com> In-Reply-To: <20221208220141.2625775-1-longman@redhat.com> References: <20221208220141.2625775-1-longman@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: B41BB20018 X-Stat-Signature: 94u4ms6ttezy85rytqash1skny3azzt4 X-HE-Tag: 1670536916-887995 X-HE-Meta: U2FsdGVkX18s/w92U2M937bdKfkpb10gVRT4+SfUddgOvqAna6yuxVyNF5C8hmTZKYgwfrcNgA8EP8egN2vlgNwU7sEBfAWa89hhMmUfy7lMUpKM+yjIF9YZr46USIpx3l4lw8dtogxa+aoUYPItNzmoq4an+cchq+tBtXbXAfCok4jqQGntHuiCkRuRos+Fj3NlYwve69htPIeyHq+7zF6Qqfsl3k6uLYcdK1R0x+l40P6kOQrRXq+hAGWs0nE15n0DBeDmxXF+GECp6BcHlKDKQBCWOq50ehiKTBtzM2ndG2nHdEWJxph2rc70jz59O5V5h+WLo8SvY9gIRzveNNK04LfJ3EPJA5hIUELtfTlbYjxxWm7Chj3BrneWgjPPm35Y2+gftEUA3G915cxCg0GerbUGlhHWHv8lPE+M4clkjppKHGRuM+AH1Jnn/uSvlI3TwidlPr8tlcndu1mSPVJVr3iiQPeeA1fZRk+rMp2wTsjllTgNdDDKlYWgGspg+IaZ1x4+tv44SFxHqKvdT0enUqTa7vx5JkoeD4OojD2LCIdwFwPp0K/O024GIGgRYF8H+ewXJCjgXzUy4tgnfaVCO1W/pq2keID5bPK5PGFVx5ah5cSQzTMUaDth2IGVChz0h+TLnK2H0t+VVVYFBoP4JiIL9G7Yibt/8MXT9+88+kmHcaVx3Ps0zw/8baHOoGOYy0GpkwbBhm4zQE0jz05huPKvO58X1UxXXB9rvdiUW8agBwJOzbVOHyPpG1m5HvBviMcgpny9oQ+ksyaMlgLw1cC8poGJeLXCIedwdn8FoIsheHOGDxCJN6qkQHYjuyuMGaKI86cqVS2OyGxpmEKnxhL8DoCXo/kXVNJOQJ9rAVBtUqzl8fXGKYe+qmIsjIZzYGtpH0lMdB9D+RrvEdMggDEeUs9L 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: Before commit 3b8cc6298724 ("blk-cgroup: Optimize blkcg_rstat_flush()"), blkg's stats is only flushed if they are online. In addition, the stat flushing of blkgs in blkcg_rstat_flush() includes propagating the rstat data to its parent. However, if a blkg has been destroyed (offline), the validity of its parent may be questionable. For safety, revert back to the old behavior by ignoring offline blkg's. Signed-off-by: Waiman Long --- block/blk-cgroup.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/block/blk-cgroup.c b/block/blk-cgroup.c index 21cc88349f21..c466aef0d467 100644 --- a/block/blk-cgroup.c +++ b/block/blk-cgroup.c @@ -885,6 +885,12 @@ static void blkcg_rstat_flush(struct cgroup_subsys_state *css, int cpu) WRITE_ONCE(bisc->lqueued, false); + /* Don't flush its stats if blkg is offline */ + if (unlikely(!blkg->online)) { + percpu_ref_put(&blkg->refcnt); + continue; + } + /* fetch the current per-cpu values */ do { seq = u64_stats_fetch_begin(&bisc->sync); From patchwork Thu Dec 8 22:01:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Waiman Long X-Patchwork-Id: 13068975 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_Koutn?= =?utf-8?q?=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 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;