kernfs: handle null pointers while printing node name and path

Null kernfs nodes could be found at cgroups during construction.
It seems safer to handle these null pointers right in kernfs in
the same way as printf prints "(null)" for null pointer string.

Signed-off-by: Konstantin Khlebnikov <khlebnikov@yandex-team.ru>
Acked-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
This commit is contained in:
Konstantin Khlebnikov 2017-02-08 14:28:55 +03:00 committed by Greg Kroah-Hartman
parent 64e90a8acb
commit 17627157cd
2 changed files with 16 additions and 14 deletions

View File

@ -41,6 +41,9 @@ static bool kernfs_lockdep(struct kernfs_node *kn)
static int kernfs_name_locked(struct kernfs_node *kn, char *buf, size_t buflen) static int kernfs_name_locked(struct kernfs_node *kn, char *buf, size_t buflen)
{ {
if (!kn)
return strlcpy(buf, "(null)", buflen);
return strlcpy(buf, kn->parent ? kn->name : "/", buflen); return strlcpy(buf, kn->parent ? kn->name : "/", buflen);
} }
@ -110,6 +113,8 @@ static struct kernfs_node *kernfs_common_ancestor(struct kernfs_node *a,
* kn_to: /n1/n2/n3 [depth=3] * kn_to: /n1/n2/n3 [depth=3]
* result: /../.. * result: /../..
* *
* [3] when @kn_to is NULL result will be "(null)"
*
* Returns the length of the full path. If the full length is equal to or * Returns the length of the full path. If the full length is equal to or
* greater than @buflen, @buf contains the truncated path with the trailing * greater than @buflen, @buf contains the truncated path with the trailing
* '\0'. On error, -errno is returned. * '\0'. On error, -errno is returned.
@ -123,6 +128,9 @@ static int kernfs_path_from_node_locked(struct kernfs_node *kn_to,
size_t depth_from, depth_to, len = 0; size_t depth_from, depth_to, len = 0;
int i, j; int i, j;
if (!kn_to)
return strlcpy(buf, "(null)", buflen);
if (!kn_from) if (!kn_from)
kn_from = kernfs_root(kn_to)->kn; kn_from = kernfs_root(kn_to)->kn;
@ -166,6 +174,8 @@ static int kernfs_path_from_node_locked(struct kernfs_node *kn_to,
* similar to strlcpy(). It returns the length of @kn's name and if @buf * similar to strlcpy(). It returns the length of @kn's name and if @buf
* isn't long enough, it's filled upto @buflen-1 and nul terminated. * isn't long enough, it's filled upto @buflen-1 and nul terminated.
* *
* Fills buffer with "(null)" if @kn is NULL.
*
* This function can be called from any context. * This function can be called from any context.
*/ */
int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen) int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen)

View File

@ -61,19 +61,15 @@ DECLARE_EVENT_CLASS(cgroup,
__field( int, id ) __field( int, id )
__field( int, level ) __field( int, level )
__dynamic_array(char, path, __dynamic_array(char, path,
cgrp->kn ? cgroup_path(cgrp, NULL, 0) + 1 cgroup_path(cgrp, NULL, 0) + 1)
: strlen("(null)"))
), ),
TP_fast_assign( TP_fast_assign(
__entry->root = cgrp->root->hierarchy_id; __entry->root = cgrp->root->hierarchy_id;
__entry->id = cgrp->id; __entry->id = cgrp->id;
__entry->level = cgrp->level; __entry->level = cgrp->level;
if (cgrp->kn)
cgroup_path(cgrp, __get_dynamic_array(path), cgroup_path(cgrp, __get_dynamic_array(path),
__get_dynamic_array_len(path)); __get_dynamic_array_len(path));
else
__assign_str(path, "(null)");
), ),
TP_printk("root=%d id=%d level=%d path=%s", TP_printk("root=%d id=%d level=%d path=%s",
@ -119,8 +115,7 @@ DECLARE_EVENT_CLASS(cgroup_migrate,
__field( int, dst_id ) __field( int, dst_id )
__field( int, dst_level ) __field( int, dst_level )
__dynamic_array(char, dst_path, __dynamic_array(char, dst_path,
dst_cgrp->kn ? cgroup_path(dst_cgrp, NULL, 0) + 1 cgroup_path(dst_cgrp, NULL, 0) + 1)
: strlen("(null)"))
__field( int, pid ) __field( int, pid )
__string( comm, task->comm ) __string( comm, task->comm )
), ),
@ -129,11 +124,8 @@ DECLARE_EVENT_CLASS(cgroup_migrate,
__entry->dst_root = dst_cgrp->root->hierarchy_id; __entry->dst_root = dst_cgrp->root->hierarchy_id;
__entry->dst_id = dst_cgrp->id; __entry->dst_id = dst_cgrp->id;
__entry->dst_level = dst_cgrp->level; __entry->dst_level = dst_cgrp->level;
if (dst_cgrp->kn)
cgroup_path(dst_cgrp, __get_dynamic_array(dst_path), cgroup_path(dst_cgrp, __get_dynamic_array(dst_path),
__get_dynamic_array_len(dst_path)); __get_dynamic_array_len(dst_path));
else
__assign_str(dst_path, "(null)");
__entry->pid = task->pid; __entry->pid = task->pid;
__assign_str(comm, task->comm); __assign_str(comm, task->comm);
), ),