Skip to content

Commit f34d360

Browse files
committed
Merge branch 'for-4.9' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/cgroup
Pull cgroup updates from Tejun Heo: - tracepoints for basic cgroup management operations added - kernfs and cgroup path formatting functions updated to behave in the style of strlcpy() - non-critical bug fixes * 'for-4.9' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/cgroup: blkcg: Unlock blkcg_pol_mutex only once when cpd == NULL cgroup: fix error handling regressions in proc_cgroup_show() and cgroup_release_agent() cpuset: fix error handling regression in proc_cpuset_show() cgroup: add tracepoints for basic operations cgroup: make cgroup_path() and friends behave in the style of strlcpy() kernfs: remove kernfs_path_len() kernfs: make kernfs_path*() behave in the style of strlcpy() kernfs: add dummy implementation of kernfs_path_from_node()
2 parents b6daa51 + bbb427e commit f34d360

10 files changed

Lines changed: 266 additions & 130 deletions

File tree

‎block/blk-cgroup.c‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1340,10 +1340,8 @@ int blkcg_policy_register(struct blkcg_policy *pol)
13401340
struct blkcg_policy_data *cpd;
13411341

13421342
cpd = pol->cpd_alloc_fn(GFP_KERNEL);
1343-
if (!cpd) {
1344-
mutex_unlock(&blkcg_pol_mutex);
1343+
if (!cpd)
13451344
goto err_free_cpds;
1346-
}
13471345

13481346
blkcg->cpd[pol->plid] = cpd;
13491347
cpd->blkcg = blkcg;

‎fs/kernfs/dir.c‎

Lines changed: 17 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -110,18 +110,18 @@ static struct kernfs_node *kernfs_common_ancestor(struct kernfs_node *a,
110110
* kn_to: /n1/n2/n3 [depth=3]
111111
* result: /../..
112112
*
113-
* return value: length of the string. If greater than buflen,
114-
* then contents of buf are undefined. On error, -1 is returned.
113+
* Returns the length of the full path. If the full length is equal to or
114+
* greater than @buflen, @buf contains the truncated path with the trailing
115+
* '\0'. On error, -errno is returned.
115116
*/
116117
static int kernfs_path_from_node_locked(struct kernfs_node *kn_to,
117118
struct kernfs_node *kn_from,
118119
char *buf, size_t buflen)
119120
{
120121
struct kernfs_node *kn, *common;
121122
const char parent_str[] = "/..";
122-
size_t depth_from, depth_to, len = 0, nlen = 0;
123-
char *p;
124-
int i;
123+
size_t depth_from, depth_to, len = 0;
124+
int i, j;
125125

126126
if (!kn_from)
127127
kn_from = kernfs_root(kn_to)->kn;
@@ -131,7 +131,7 @@ static int kernfs_path_from_node_locked(struct kernfs_node *kn_to,
131131

132132
common = kernfs_common_ancestor(kn_from, kn_to);
133133
if (WARN_ON(!common))
134-
return -1;
134+
return -EINVAL;
135135

136136
depth_to = kernfs_depth(common, kn_to);
137137
depth_from = kernfs_depth(common, kn_from);
@@ -144,22 +144,16 @@ static int kernfs_path_from_node_locked(struct kernfs_node *kn_to,
144144
len < buflen ? buflen - len : 0);
145145

146146
/* Calculate how many bytes we need for the rest */
147-
for (kn = kn_to; kn != common; kn = kn->parent)
148-
nlen += strlen(kn->name) + 1;
149-
150-
if (len + nlen >= buflen)
151-
return len + nlen;
152-
153-
p = buf + len + nlen;
154-
*p = '\0';
155-
for (kn = kn_to; kn != common; kn = kn->parent) {
156-
size_t tmp = strlen(kn->name);
157-
p -= tmp;
158-
memcpy(p, kn->name, tmp);
159-
*(--p) = '/';
147+
for (i = depth_to - 1; i >= 0; i--) {
148+
for (kn = kn_to, j = 0; j < i; j++)
149+
kn = kn->parent;
150+
len += strlcpy(buf + len, "/",
151+
len < buflen ? buflen - len : 0);
152+
len += strlcpy(buf + len, kn->name,
153+
len < buflen ? buflen - len : 0);
160154
}
161155

162-
return len + nlen;
156+
return len;
163157
}
164158

165159
/**
@@ -185,29 +179,6 @@ int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen)
185179
return ret;
186180
}
187181

188-
/**
189-
* kernfs_path_len - determine the length of the full path of a given node
190-
* @kn: kernfs_node of interest
191-
*
192-
* The returned length doesn't include the space for the terminating '\0'.
193-
*/
194-
size_t kernfs_path_len(struct kernfs_node *kn)
195-
{
196-
size_t len = 0;
197-
unsigned long flags;
198-
199-
spin_lock_irqsave(&kernfs_rename_lock, flags);
200-
201-
do {
202-
len += strlen(kn->name) + 1;
203-
kn = kn->parent;
204-
} while (kn && kn->parent);
205-
206-
spin_unlock_irqrestore(&kernfs_rename_lock, flags);
207-
208-
return len;
209-
}
210-
211182
/**
212183
* kernfs_path_from_node - build path of node @to relative to @from.
213184
* @from: parent kernfs_node relative to which we need to build the path
@@ -220,8 +191,9 @@ size_t kernfs_path_len(struct kernfs_node *kn)
220191
* path (which includes '..'s) as needed to reach from @from to @to is
221192
* returned.
222193
*
223-
* If @buf isn't long enough, the return value will be greater than @buflen
224-
* and @buf contents are undefined.
194+
* Returns the length of the full path. If the full length is equal to or
195+
* greater than @buflen, @buf contains the truncated path with the trailing
196+
* '\0'. On error, -errno is returned.
225197
*/
226198
int kernfs_path_from_node(struct kernfs_node *to, struct kernfs_node *from,
227199
char *buf, size_t buflen)
@@ -236,28 +208,6 @@ int kernfs_path_from_node(struct kernfs_node *to, struct kernfs_node *from,
236208
}
237209
EXPORT_SYMBOL_GPL(kernfs_path_from_node);
238210

239-
/**
240-
* kernfs_path - build full path of a given node
241-
* @kn: kernfs_node of interest
242-
* @buf: buffer to copy @kn's name into
243-
* @buflen: size of @buf
244-
*
245-
* Builds and returns the full path of @kn in @buf of @buflen bytes. The
246-
* path is built from the end of @buf so the returned pointer usually
247-
* doesn't match @buf. If @buf isn't long enough, @buf is nul terminated
248-
* and %NULL is returned.
249-
*/
250-
char *kernfs_path(struct kernfs_node *kn, char *buf, size_t buflen)
251-
{
252-
int ret;
253-
254-
ret = kernfs_path_from_node(kn, NULL, buf, buflen);
255-
if (ret < 0 || ret >= buflen)
256-
return NULL;
257-
return buf;
258-
}
259-
EXPORT_SYMBOL_GPL(kernfs_path);
260-
261211
/**
262212
* pr_cont_kernfs_name - pr_cont name of a kernfs_node
263213
* @kn: kernfs_node of interest

‎fs/sysfs/dir.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ DEFINE_SPINLOCK(sysfs_symlink_target_lock);
2121

2222
void sysfs_warn_dup(struct kernfs_node *parent, const char *name)
2323
{
24-
char *buf, *path = NULL;
24+
char *buf;
2525

2626
buf = kzalloc(PATH_MAX, GFP_KERNEL);
2727
if (buf)
28-
path = kernfs_path(parent, buf, PATH_MAX);
28+
kernfs_path(parent, buf, PATH_MAX);
2929

3030
WARN(1, KERN_WARNING "sysfs: cannot create duplicate filename '%s/%s'\n",
31-
path, name);
31+
buf, name);
3232

3333
kfree(buf);
3434
}

‎include/linux/blk-cgroup.h‎

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -343,16 +343,7 @@ static inline struct blkcg *cpd_to_blkcg(struct blkcg_policy_data *cpd)
343343
*/
344344
static inline int blkg_path(struct blkcg_gq *blkg, char *buf, int buflen)
345345
{
346-
char *p;
347-
348-
p = cgroup_path(blkg->blkcg->css.cgroup, buf, buflen);
349-
if (!p) {
350-
strncpy(buf, "<unavailable>", buflen);
351-
return -ENAMETOOLONG;
352-
}
353-
354-
memmove(buf, p, buf + buflen - p);
355-
return 0;
346+
return cgroup_path(blkg->blkcg->css.cgroup, buf, buflen);
356347
}
357348

358349
/**

‎include/linux/cgroup.h‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ int cgroup_add_legacy_cftypes(struct cgroup_subsys *ss, struct cftype *cfts);
9797
int cgroup_rm_cftypes(struct cftype *cfts);
9898
void cgroup_file_notify(struct cgroup_file *cfile);
9999

100-
char *task_cgroup_path(struct task_struct *task, char *buf, size_t buflen);
100+
int task_cgroup_path(struct task_struct *task, char *buf, size_t buflen);
101101
int cgroupstats_build(struct cgroupstats *stats, struct dentry *dentry);
102102
int proc_cgroup_show(struct seq_file *m, struct pid_namespace *ns,
103103
struct pid *pid, struct task_struct *tsk);
@@ -555,8 +555,7 @@ static inline int cgroup_name(struct cgroup *cgrp, char *buf, size_t buflen)
555555
return kernfs_name(cgrp->kn, buf, buflen);
556556
}
557557

558-
static inline char * __must_check cgroup_path(struct cgroup *cgrp, char *buf,
559-
size_t buflen)
558+
static inline int cgroup_path(struct cgroup *cgrp, char *buf, size_t buflen)
560559
{
561560
return kernfs_path(cgrp->kn, buf, buflen);
562561
}
@@ -658,8 +657,8 @@ struct cgroup_namespace *copy_cgroup_ns(unsigned long flags,
658657
struct user_namespace *user_ns,
659658
struct cgroup_namespace *old_ns);
660659

661-
char *cgroup_path_ns(struct cgroup *cgrp, char *buf, size_t buflen,
662-
struct cgroup_namespace *ns);
660+
int cgroup_path_ns(struct cgroup *cgrp, char *buf, size_t buflen,
661+
struct cgroup_namespace *ns);
663662

664663
#else /* !CONFIG_CGROUPS */
665664

‎include/linux/kernfs.h‎

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,8 @@ static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
269269
}
270270

271271
int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen);
272-
size_t kernfs_path_len(struct kernfs_node *kn);
273272
int kernfs_path_from_node(struct kernfs_node *root_kn, struct kernfs_node *kn,
274273
char *buf, size_t buflen);
275-
char *kernfs_path(struct kernfs_node *kn, char *buf, size_t buflen);
276274
void pr_cont_kernfs_name(struct kernfs_node *kn);
277275
void pr_cont_kernfs_path(struct kernfs_node *kn);
278276
struct kernfs_node *kernfs_get_parent(struct kernfs_node *kn);
@@ -341,12 +339,10 @@ static inline bool kernfs_ns_enabled(struct kernfs_node *kn)
341339
static inline int kernfs_name(struct kernfs_node *kn, char *buf, size_t buflen)
342340
{ return -ENOSYS; }
343341

344-
static inline size_t kernfs_path_len(struct kernfs_node *kn)
345-
{ return 0; }
346-
347-
static inline char *kernfs_path(struct kernfs_node *kn, char *buf,
348-
size_t buflen)
349-
{ return NULL; }
342+
static inline int kernfs_path_from_node(struct kernfs_node *root_kn,
343+
struct kernfs_node *kn,
344+
char *buf, size_t buflen)
345+
{ return -ENOSYS; }
350346

351347
static inline void pr_cont_kernfs_name(struct kernfs_node *kn) { }
352348
static inline void pr_cont_kernfs_path(struct kernfs_node *kn) { }
@@ -436,6 +432,22 @@ static inline void kernfs_init(void) { }
436432

437433
#endif /* CONFIG_KERNFS */
438434

435+
/**
436+
* kernfs_path - build full path of a given node
437+
* @kn: kernfs_node of interest
438+
* @buf: buffer to copy @kn's name into
439+
* @buflen: size of @buf
440+
*
441+
* Builds and returns the full path of @kn in @buf of @buflen bytes. The
442+
* path is built from the end of @buf so the returned pointer usually
443+
* doesn't match @buf. If @buf isn't long enough, @buf is nul terminated
444+
* and %NULL is returned.
445+
*/
446+
static inline int kernfs_path(struct kernfs_node *kn, char *buf, size_t buflen)
447+
{
448+
return kernfs_path_from_node(kn, NULL, buf, buflen);
449+
}
450+
439451
static inline struct kernfs_node *
440452
kernfs_find_and_get(struct kernfs_node *kn, const char *name)
441453
{

0 commit comments

Comments
 (0)