From patchwork Fri Feb 26 21:47:39 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yang Shi X-Patchwork-Id: 63110 Delivered-To: patch@linaro.org Received: by 10.112.199.169 with SMTP id jl9csp105924lbc; Fri, 26 Feb 2016 14:11:10 -0800 (PST) X-Received: by 10.98.74.17 with SMTP id x17mr5499526pfa.14.1456524670373; Fri, 26 Feb 2016 14:11:10 -0800 (PST) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b8si22393200pas.137.2016.02.26.14.11.10; Fri, 26 Feb 2016 14:11:10 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dkim=pass header.i=@linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755491AbcBZWLH (ORCPT + 30 others); Fri, 26 Feb 2016 17:11:07 -0500 Received: from mail-pf0-f175.google.com ([209.85.192.175]:33274 "EHLO mail-pf0-f175.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755267AbcBZWLF (ORCPT ); Fri, 26 Feb 2016 17:11:05 -0500 Received: by mail-pf0-f175.google.com with SMTP id q63so58538686pfb.0 for ; Fri, 26 Feb 2016 14:11:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=Ew+WdDRjXUNXToKWhEvS3JaSX4xfjk7k1oLyNVIR2OE=; b=KFBc0KVLdRYwxxDZULM2Jug+Pknw8aYVYx5HDU7hr6C7yFRYsGGVIBfo+0Gghlp48n DuKcfCATl5JRzbaFyGK+Mms6XVc8Dh9FS1IcTpwuXx8jmSMbyu2/OOznns1SjmXqEVrr ByG7W5lAQ5WBQ7itHmfHzMuA2l4ffuvRLH8YY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=Ew+WdDRjXUNXToKWhEvS3JaSX4xfjk7k1oLyNVIR2OE=; b=C7F0iqLyIzOZhHIfmZcYqDankPdnecdws4uXuq2Lp2mLBpVqd36RTYCDyYy4ZthikL vnWnKt34kZaA2oXS/aEXk/ZOg6qBZNy3H1gif/ZffSFjgY0bL5bCSM/gt7Iz6egVqIgF vOqaqtxTa2OQ8jJvSG1D3PFl9wGBUAt8YyElkWMPBDyaVto472D/2D6/bUfLK0p4tL6H 7GBaIO8ZKiVLON3vfRzulaRXLCdmWGrAdf4zs14cqanRmsEacITpa5Z9KPKqrrHJtdZC 8Q2tV0EkZe1RuFiN+OXLyKMjPtZQEAfR4yfb3EXf56FhxmbzOu+KQddphy3+5k9IsRfa nS4A== X-Gm-Message-State: AD7BkJKDc8x888UEOR5XamNRGDth2R5JP743PGHnyMaHXaFnKEm0Z3L83uM2nSnCKS5zaOob X-Received: by 10.98.1.85 with SMTP id 82mr5575673pfb.10.1456524663917; Fri, 26 Feb 2016 14:11:03 -0800 (PST) Received: from yshi-Precision-T5600.corp.ad.wrs.com (unknown-216-82.windriver.com. [147.11.216.82]) by smtp.gmail.com with ESMTPSA id xm10sm21544945pab.12.2016.02.26.14.11.02 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 26 Feb 2016 14:11:02 -0800 (PST) From: Yang Shi To: gregkh@linuxfoundation.org, tj@kernel.org, lizefan@huawei.com, tglx@linutronix.de, rostedt@goodmis.org, bigeasy@linutronix.de Cc: linux-kernel@vger.kernel.org, linux-rt-users@vger.kernel.org, linaro-kernel@lists.linaro.org, yang.shi@linaro.org Subject: [RFC V2 PATCH] kernfs: create raw version kernfs_path_len and kernfs_path Date: Fri, 26 Feb 2016 13:47:39 -0800 Message-Id: <1456523259-8588-1-git-send-email-yang.shi@linaro.org> X-Mailer: git-send-email 2.0.2 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org commit 5634cc2aa9aebc77bc862992e7805469dcf83dac ("writeback: update writeback tracepoints to report cgroup") made writeback tracepoints report cgroup writeback, but it may trigger the below bug on -rt kernel since kernfs_path and kernfs_path_len are called by tracepoints, which acquire sleeping lock. BUG: sleeping function called from invalid context at kernel/locking/rtmutex.c:930 in_atomic(): 1, irqs_disabled(): 0, pid: 625, name: kworker/u16:3 INFO: lockdep is turned off. Preemption disabled at:[] wb_writeback+0xec/0x830 CPU: 7 PID: 625 Comm: kworker/u16:3 Not tainted 4.4.1-rt5 #20 Hardware name: Freescale Layerscape 2085a RDB Board (DT) Workqueue: writeback wb_workfn (flush-7:0) Call trace: [] dump_backtrace+0x0/0x200 [] show_stack+0x24/0x30 [] dump_stack+0x88/0xa8 [] ___might_sleep+0x2ec/0x300 [] rt_spin_lock+0x38/0xb8 [] kernfs_path_len+0x30/0x90 [] trace_event_raw_event_writeback_work_class+0xe8/0x2e8 [] wb_writeback+0x620/0x830 [] wb_workfn+0x61c/0x950 [] process_one_work+0x3ac/0xb30 [] worker_thread+0x9c/0x7a8 [] kthread+0x190/0x1b0 [] ret_from_fork+0x10/0x30 Since kernfs_* functions are heavily used by cgroup, so it sounds not reasonable to convert kernfs_rename_lock to raw lock. Call synchronize_sched() when kernfs_node is updated since tracepoints are protected by rcu_read_lock_sched. Create raw version kernfs_path, kernfs_path_len and cgroup_path, which don't acquire lock and are used by tracepoints only. Signed-off-by: Yang Shi --- It should be applicable to both mainline and -rt kernel. The change survives ftrace stress test in ltp. v2: * Adopted Steven's suggestion to call synchronize_sched in kernfs_rename_ns. fs/kernfs/dir.c | 30 +++++++++++++++++++++++++++--- include/linux/cgroup.h | 10 ++++++++++ include/linux/kernfs.h | 10 ++++++++++ include/trace/events/writeback.h | 4 ++-- 4 files changed, 49 insertions(+), 5 deletions(-) -- 2.0.2 diff --git a/fs/kernfs/dir.c b/fs/kernfs/dir.c index 996b774..70a9687 100644 --- a/fs/kernfs/dir.c +++ b/fs/kernfs/dir.c @@ -44,7 +44,7 @@ static int kernfs_name_locked(struct kernfs_node *kn, char *buf, size_t buflen) return strlcpy(buf, kn->parent ? kn->name : "/", buflen); } -static char * __must_check kernfs_path_locked(struct kernfs_node *kn, char *buf, +static char * __must_check __kernfs_path(struct kernfs_node *kn, char *buf, size_t buflen) { char *p = buf + buflen; @@ -131,12 +131,33 @@ char *kernfs_path(struct kernfs_node *kn, char *buf, size_t buflen) char *p; spin_lock_irqsave(&kernfs_rename_lock, flags); - p = kernfs_path_locked(kn, buf, buflen); + p = __kernfs_path(kn, buf, buflen); spin_unlock_irqrestore(&kernfs_rename_lock, flags); return p; } EXPORT_SYMBOL_GPL(kernfs_path); +/* Raw version. Used by tracepoints only without acquiring lock */ +size_t _kernfs_path_len(struct kernfs_node *kn) +{ + size_t len = 0; + + do { + len += strlen(kn->name) + 1; + kn = kn->parent; + } while (kn && kn->parent); + + return len; +} + +char *_kernfs_path(struct kernfs_node *kn, char *buf, size_t buflen) +{ + char *p; + + p = __kernfs_path(kn, buf, buflen); + return p; +} + /** * pr_cont_kernfs_name - pr_cont name of a kernfs_node * @kn: kernfs_node of interest @@ -168,7 +189,7 @@ void pr_cont_kernfs_path(struct kernfs_node *kn) spin_lock_irqsave(&kernfs_rename_lock, flags); - p = kernfs_path_locked(kn, kernfs_pr_cont_buf, + p = __kernfs_path(kn, kernfs_pr_cont_buf, sizeof(kernfs_pr_cont_buf)); if (p) pr_cont("%s", p); @@ -1397,6 +1418,9 @@ int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent, kn->hash = kernfs_name_hash(kn->name, kn->ns); kernfs_link_sibling(kn); + /* Tracepoints are protected by rcu_read_lock_sched */ + synchronize_sched(); + kernfs_put(old_parent); kfree_const(old_name); diff --git a/include/linux/cgroup.h b/include/linux/cgroup.h index 2162dca..bbd88d3 100644 --- a/include/linux/cgroup.h +++ b/include/linux/cgroup.h @@ -538,6 +538,16 @@ static inline char * __must_check cgroup_path(struct cgroup *cgrp, char *buf, return kernfs_path(cgrp->kn, buf, buflen); } +/* + * Without acquiring kernfs_rename_lock in _kernfs_path. + * Used by tracepoints only. + */ +static inline char * __must_check _cgroup_path(struct cgroup *cgrp, char *buf, + size_t buflen) +{ + return _kernfs_path(cgrp->kn, buf, buflen); +} + static inline void pr_cont_cgroup_name(struct cgroup *cgrp) { pr_cont_kernfs_name(cgrp->kn); diff --git a/include/linux/kernfs.h b/include/linux/kernfs.h index af51df3..14c01cdc 100644 --- a/include/linux/kernfs.h +++ b/include/linux/kernfs.h @@ -267,8 +267,11 @@ static inline bool kernfs_ns_enabled(struct kernfs_node *kn) int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen); size_t kernfs_path_len(struct kernfs_node *kn); +size_t _kernfs_path_len(struct kernfs_node *kn); char * __must_check kernfs_path(struct kernfs_node *kn, char *buf, size_t buflen); +char * __must_check _kernfs_path(struct kernfs_node *kn, char *buf, + size_t buflen); void pr_cont_kernfs_name(struct kernfs_node *kn); void pr_cont_kernfs_path(struct kernfs_node *kn); struct kernfs_node *kernfs_get_parent(struct kernfs_node *kn); @@ -338,10 +341,17 @@ static inline int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen) static inline size_t kernfs_path_len(struct kernfs_node *kn) { return 0; } +static inline size_t _kernfs_path_len(struct kernfs_node *kn) +{ return 0; } + static inline char * __must_check kernfs_path(struct kernfs_node *kn, char *buf, size_t buflen) { return NULL; } +static inline char * __must_check _kernfs_path(struct kernfs_node *kn, char *buf, + size_t buflen) +{ return NULL; } + static inline void pr_cont_kernfs_name(struct kernfs_node *kn) { } static inline void pr_cont_kernfs_path(struct kernfs_node *kn) { } diff --git a/include/trace/events/writeback.h b/include/trace/events/writeback.h index fff846b..91ceced 100644 --- a/include/trace/events/writeback.h +++ b/include/trace/events/writeback.h @@ -136,7 +136,7 @@ DEFINE_EVENT(writeback_dirty_inode_template, writeback_dirty_inode, static inline size_t __trace_wb_cgroup_size(struct bdi_writeback *wb) { - return kernfs_path_len(wb->memcg_css->cgroup->kn) + 1; + return _kernfs_path_len(wb->memcg_css->cgroup->kn) + 1; } static inline void __trace_wb_assign_cgroup(char *buf, struct bdi_writeback *wb) @@ -144,7 +144,7 @@ static inline void __trace_wb_assign_cgroup(char *buf, struct bdi_writeback *wb) struct cgroup *cgrp = wb->memcg_css->cgroup; char *path; - path = cgroup_path(cgrp, buf, kernfs_path_len(cgrp->kn) + 1); + path = _cgroup_path(cgrp, buf, _kernfs_path_len(cgrp->kn) + 1); WARN_ON_ONCE(path != buf); }