From patchwork Thu Dec 28 07:04:00 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hou Tao X-Patchwork-Id: 10134429 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 4603860388 for ; Thu, 28 Dec 2017 06:56:49 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 34F7C2D59F for ; Thu, 28 Dec 2017 06:56:49 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2823F2D5B3; Thu, 28 Dec 2017 06:56:49 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 93E2E2D59F for ; Thu, 28 Dec 2017 06:56:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751280AbdL1G4r (ORCPT ); Thu, 28 Dec 2017 01:56:47 -0500 Received: from szxga07-in.huawei.com ([45.249.212.35]:45638 "EHLO huawei.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1751086AbdL1G4r (ORCPT ); Thu, 28 Dec 2017 01:56:47 -0500 Received: from DGGEMS406-HUB.china.huawei.com (unknown [172.30.72.59]) by Forcepoint Email with ESMTP id 993C5FE314AF7; Thu, 28 Dec 2017 14:56:43 +0800 (CST) Received: from huawei.com (10.175.124.28) by DGGEMS406-HUB.china.huawei.com (10.3.19.206) with Microsoft SMTP Server id 14.3.361.1; Thu, 28 Dec 2017 14:56:20 +0800 From: Hou Tao To: , CC: , , Subject: [PATCH] blktrace: output io cgroup name for cgroup v1 Date: Thu, 28 Dec 2017 15:04:00 +0800 Message-ID: <20171228070400.26328-1-houtao1@huawei.com> X-Mailer: git-send-email 2.9.5 MIME-Version: 1.0 X-Originating-IP: [10.175.124.28] X-CFilter-Loop: Reflected Sender: linux-block-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-block@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Now the output of io cgroup name in blktrace is controlled by blk_cgroup & blk_cgname options in trace_options files. When using cgroup v1 for io controller, there is no output of cgroup name in trace file, because cgroup_path_from_kernfs_id() uses cgrp_dfl_root.kf_root to find the cgroup file and cgrp_dfl_root is only valid for cgroup v2. So fix cgroup_path_from_kernfs_id() to support both cgroup v1 and v2. Fixes: 69fd5c3 ("blktrace: add an option to allow displaying cgroup path") Signed-off-by: Hou Tao --- include/linux/cgroup.h | 6 +++--- kernel/cgroup/cgroup.c | 39 ++++++++++++++++++++++++++++++++++++--- kernel/trace/blktrace.c | 4 ++-- 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/include/linux/cgroup.h b/include/linux/cgroup.h index 473e0c0..ed80490 100644 --- a/include/linux/cgroup.h +++ b/include/linux/cgroup.h @@ -651,7 +651,7 @@ static inline union kernfs_node_id *cgroup_get_kernfs_id(struct cgroup *cgrp) return &cgrp->kn->id; } -void cgroup_path_from_kernfs_id(const union kernfs_node_id *id, +void cgroup_path_from_kernfs_id(int ssid, const union kernfs_node_id *id, char *buf, size_t buflen); #else /* !CONFIG_CGROUPS */ @@ -686,8 +686,8 @@ static inline bool task_under_cgroup_hierarchy(struct task_struct *task, return true; } -static inline void cgroup_path_from_kernfs_id(const union kernfs_node_id *id, - char *buf, size_t buflen) {} +static inline void cgroup_path_from_kernfs_id(int ssid, + const union kernfs_node_id *id, char *buf, size_t buflen) {} #endif /* !CONFIG_CGROUPS */ /* diff --git a/kernel/cgroup/cgroup.c b/kernel/cgroup/cgroup.c index 0b1ffe1..49d63c6 100644 --- a/kernel/cgroup/cgroup.c +++ b/kernel/cgroup/cgroup.c @@ -5354,16 +5354,49 @@ static int __init cgroup_wq_init(void) } core_initcall(cgroup_wq_init); -void cgroup_path_from_kernfs_id(const union kernfs_node_id *id, +void cgroup_path_from_kernfs_id(int ssid, const union kernfs_node_id *id, char *buf, size_t buflen) { + struct kernfs_root *root; struct kernfs_node *kn; + struct cgroup *root_cgrp = NULL; - kn = kernfs_get_node_by_id(cgrp_dfl_root.kf_root, id); - if (!kn) + if (ssid >= CGROUP_SUBSYS_COUNT) return; + + if (likely(static_key_enabled(cgroup_subsys_on_dfl_key[ssid]))) { + root = cgrp_dfl_root.kf_root; + } else { + struct cgroup_subsys *subsys = cgroup_subsys[ssid]; + + /* + * It seems we can not use rcu_read_lock() to protect + * the liveness check of subsys->root->cgrp. Although + * root->cgrp is freed by RCU, when we dereference the + * old root, the old root may been destroying by + * cgroup_destroy_root(). + */ + mutex_lock(&cgroup_mutex); + if (percpu_ref_tryget_live(&subsys->root->cgrp.self.refcnt)) { + root_cgrp = &subsys->root->cgrp; + root = subsys->root->kf_root; + } + mutex_unlock(&cgroup_mutex); + + if (!root_cgrp) + goto out; + } + + kn = kernfs_get_node_by_id(root, id); + if (!kn) + goto out; + kernfs_path(kn, buf, buflen); kernfs_put(kn); + +out: + if (root_cgrp) + cgroup_put(root_cgrp); } /* diff --git a/kernel/trace/blktrace.c b/kernel/trace/blktrace.c index 987d9a9a..79890e0 100644 --- a/kernel/trace/blktrace.c +++ b/kernel/trace/blktrace.c @@ -1279,8 +1279,8 @@ static void blk_log_action(struct trace_iterator *iter, const char *act, if (blk_tracer_flags.val & TRACE_BLK_OPT_CGNAME) { char blkcg_name_buf[NAME_MAX + 1] = "<...>"; - cgroup_path_from_kernfs_id(id, blkcg_name_buf, - sizeof(blkcg_name_buf)); + cgroup_path_from_kernfs_id(io_cgrp_id, id, + blkcg_name_buf, sizeof(blkcg_name_buf)); trace_seq_printf(&iter->seq, "%3d,%-3d %s %2s %3s ", MAJOR(t->device), MINOR(t->device), blkcg_name_buf, act, rwbs);