Skip to contentSkip to conversation

lib/string: introduce kbasename helper

PATCH In review Andy Shevchenko 2 participants 9 messages Oct 16, 2012 – Oct 22, 2012

Subsystems (8)
lore
Share & tools
lore
#0 lore raw

v3 cover lib/string: introduce kbasename helper

There are several places in kernel that duplicate code to get last part of the pathname. This patchset introduces a helper and few fixes.

Since v2:
- fixed Cc list in the patch 6/6 ("trace: ...")
- update patch 3/6 ("rts_pstor: ...") to not hide const qualifier
- send patch 1/1 to all people involved to the series
- rebase against recent linux-next
Since v1:
- fix changelog of the patch 1 - we are doing basename(3) alike helper
- usb related patch temporary excluded from series (under discussion with Greg)

Andy Shevchenko (6):
lib/string: introduce helper to get base file name from given path
lib: dynamic_debug: reuse kbasename()
staging: rts_pstor: reuse kbasename()
mm: reuse kbasename() functionality
procfs: reuse kbasename() functionality
trace: reuse kbasename() functionality

 drivers/staging/rts_pstor/trace.h |   24 ++++--------------------
 fs/proc/proc_devtree.c            |    7 ++-----
 include/linux/string.h            |   11 +++++++++++
 kernel/trace/trace_uprobe.c       |    8 ++++----
 lib/dynamic_debug.c               |    9 +--------
 mm/memory.c                       |    8 +++-----
 6 files changed, 25 insertions(+), 42 deletions(-)
Signature · 1 line
1.7.10.4
#1 lore raw

PATCH v3 1/6 string: introduce helper to get base file name from given path

There are several places in the kernel that use functionality like basename(3) with the exception: in case of '/foo/bar/' we expect to get an empty string. Let's do it common helper for them.

---
 include/linux/string.h |   11 +++++++++++
 1 file changed, 11 insertions(+)
include/linux/string.h +11 -0
index 6301258..ac889c5 100644
@@ -143,4 +143,15 @@ static inline bool strstarts(const char *str, const char *prefix)
 
 extern size_t memweight(const void *ptr, size_t bytes);
 
+/**
+ * kbasename - return the last part of a pathname.
+ *
+ * @path: path to extract the filename from.
+ */
+static inline const char *kbasename(const char *path)
+{
+	const char *tail = strrchr(path, '/');
+	return tail ? tail + 1 : path;
+}
+
 #endif /* _LINUX_STRING_H_ */
Signature · 1 line
1.7.10.4
#2 lore raw

PATCH v3 1/6 lib/string: introduce helper to get base file name from given path

In reply to Andy Shevchenko

There are several places in the kernel that use functionality like basename(3) with an exception: in case of '/foo/bar/' we expect to get an empty string. Let's do it common helper for them.

Cc Jason Baron <[email protected]>
Cc YAMANE Toshiaki <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Frederic Weisbecker <[email protected]>
---
 include/linux/string.h |   11 +++++++++++
 1 file changed, 11 insertions(+)
include/linux/string.h +11 -0
index 6301258..ac889c5 100644
@@ -143,4 +143,15 @@ static inline bool strstarts(const char *str, const char *prefix)
 
 extern size_t memweight(const void *ptr, size_t bytes);
 
+/**
+ * kbasename - return the last part of a pathname.
+ *
+ * @path: path to extract the filename from.
+ */
+static inline const char *kbasename(const char *path)
+{
+	const char *tail = strrchr(path, '/');
+	return tail ? tail + 1 : path;
+}
+
 #endif /* _LINUX_STRING_H_ */
Signature · 7 lines
1.7.10.4

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#3 lore raw

PATCH v3 2/6 lib: dynamic_debug: reuse kbasename()

In reply to Andy Shevchenko

Remove the custom implementation of the functionality similar to kbasename().

---
 lib/dynamic_debug.c |    9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)
lib/dynamic_debug.c +1 -8
index e7f7d99..1db1fc6 100644
@@ -62,13 +62,6 @@ static LIST_HEAD(ddebug_tables);
 static int verbose = 0;
 module_param(verbose, int, 0644);
 
-/* Return the last part of a pathname */
-static inline const char *basename(const char *path)
-{
-	const char *tail = strrchr(path, '/');
-	return tail ? tail+1 : path;
-}
-
 /* Return the path relative to source root */
 static inline const char *trim_prefix(const char *path)
 {
@@ -154,7 +147,7 @@ static int ddebug_change(const struct ddebug_query *query,
 			/* match against the source filename */
 			if (query->filename &&
 			    strcmp(query->filename, dp->filename) &&
-			    strcmp(query->filename, basename(dp->filename)) &&
+			    strcmp(query->filename, kbasename(dp->filename)) &&
 			    strcmp(query->filename, trim_prefix(dp->filename)))
 				continue;
 
Signature · 1 line
1.7.10.4
#4 lore raw

PATCH v3 3/6 staging: rts_pstor: reuse kbasename()

In reply to Andy Shevchenko

The custom filename function mostly repeats the kernel's kbasename. This patch simplifies it. The updated filename() will not check for the '\' in the filenames. It seems redundant in Linux. The __FILE__ macro always defined if we compile an existing file. Thus, NULL check is not needed there as well.

Cc YAMANE Toshiaki <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
---
 drivers/staging/rts_pstor/trace.h |   24 ++++--------------------
 1 file changed, 4 insertions(+), 20 deletions(-)
drivers/staging/rts_pstor/trace.h +4 -20
index cf60a1b..bff99bf 100644
@@ -24,31 +24,15 @@
 #ifndef __REALTEK_RTSX_TRACE_H
 #define __REALTEK_RTSX_TRACE_H
 
+#include <linux/string.h>
+
 #define _MSG_TRACE
 
 #ifdef _MSG_TRACE
-static inline char *filename(char *path)
-{
-	char *ptr;
-
-	if (path == NULL)
-		return NULL;
-
-	ptr = path;
-
-	while (*ptr != '\0') {
-		if ((*ptr == '\\') || (*ptr == '/'))
-			path = ptr + 1;
-		
-		ptr++;
-	}
-
-	return path;
-}
 
 #define TRACE_RET(chip, ret)   										\
 do {													\
-	char *_file = filename(__FILE__);								\
+	const char *_file = kbasename(__FILE__);								\
 	RTSX_DEBUGP("[%s][%s]:[%d]\n", _file, __func__, __LINE__);					\
 	(chip)->trace_msg[(chip)->msg_idx].line = (u16)(__LINE__);					\
 	strncpy((chip)->trace_msg[(chip)->msg_idx].func, __func__, MSG_FUNC_LEN-1);			\
@@ -64,7 +48,7 @@ do {													\
 
 #define TRACE_GOTO(chip, label)   									\
 do {													\
-	char *_file = filename(__FILE__);								\
+	const char *_file = kbasename(__FILE__);								\
 	RTSX_DEBUGP("[%s][%s]:[%d]\n", _file, __func__, __LINE__);					\
 	(chip)->trace_msg[(chip)->msg_idx].line = (u16)(__LINE__);					\
 	strncpy((chip)->trace_msg[(chip)->msg_idx].func, __func__, MSG_FUNC_LEN-1);			\
Signature · 1 line
1.7.10.4
maintainer Acked-by on v3 3/6
#5 lore raw

Re: [PATCHv3 3/6] staging: rts_pstor: reuse kbasename()

Quoted 8 lines · Andy Shevchenko

The custom filename function mostly repeats the kernel's kbasename. This patch simplifies it. The updated filename() will not check for the '\' in the filenames. It seems redundant in Linux. The __FILE__ macro always defined if we compile an existing file. Thus, NULL check is not needed there as well.

Cc YAMANE Toshiaki <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
#6 lore raw

PATCH v3 4/6 mm: reuse kbasename() functionality

In reply to Andy Shevchenko
---
 mm/memory.c |    8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
mm/memory.c +3 -5
index 5823f29..06158b7 100644
@@ -58,6 +58,7 @@
 #include <linux/elf.h>
 #include <linux/gfp.h>
 #include <linux/migrate.h>
+#include <linux/string.h>
 
 #include <asm/io.h>
 #include <asm/pgalloc.h>
@@ -4034,15 +4035,12 @@ void print_vma_addr(char *prefix, unsigned long ip)
 		struct file *f = vma->vm_file;
 		char *buf = (char *)__get_free_page(GFP_KERNEL);
 		if (buf) {
-			char *p, *s;
+			char *p;
 
 			p = d_path(&f->f_path, buf, PAGE_SIZE);
 			if (IS_ERR(p))
 				p = "?";
-			s = strrchr(p, '/');
-			if (s)
-				p = s+1;
-			printk("%s%s[%lx+%lx]", prefix, p,
+			printk("%s%s[%lx+%lx]", prefix, kbasename(p),
 					vma->vm_start,
 					vma->vm_end - vma->vm_start);
 			free_page((unsigned long)buf);
Signature · 7 lines
1.7.10.4

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#7 lore raw

PATCH v3 5/6 procfs: reuse kbasename() functionality

In reply to Andy Shevchenko
---
 fs/proc/proc_devtree.c |    7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
fs/proc/proc_devtree.c +2 -5
index df7dd08..3d9fd66 100644
@@ -13,6 +13,7 @@
 #include <linux/of.h>
 #include <linux/module.h>
 #include <linux/slab.h>
+#include <linux/string.h>
 #include <asm/prom.h>
 #include <asm/uaccess.h>
 #include "internal.h"
@@ -195,11 +196,7 @@ void proc_device_tree_add_node(struct device_node *np,
 	set_node_proc_entry(np, de);
 	for (child = NULL; (child = of_get_next_child(np, child));) {
 		/* Use everything after the last slash, or the full name */
-		p = strrchr(child->full_name, '/');
-		if (!p)
-			p = child->full_name;
-		else
-			++p;
+		p = kbasename(child->full_name);
 
 		if (duplicate_name(de, p))
 			p = fixup_name(np, de, p);
Signature · 1 line
1.7.10.4
#8 lore raw

PATCH v3 6/6 trace: reuse kbasename() functionality

In reply to Andy Shevchenko
Cc Steven Rostedt <[email protected]>
Cc Frederic Weisbecker <[email protected]>
---
 kernel/trace/trace_uprobe.c |    8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
kernel/trace/trace_uprobe.c +4 -4
index 03003cd..c7ba4f6 100644
@@ -22,6 +22,7 @@
 #include <linux/uaccess.h>
 #include <linux/uprobes.h>
 #include <linux/namei.h>
+#include <linux/string.h>
 
 #include "trace_probe.h"
 
@@ -263,16 +264,15 @@ static int create_trace_uprobe(int argc, char **argv)
 
 	/* setup a probe */
 	if (!event) {
-		char *tail = strrchr(filename, '/');
+		char *tail;
 		char *ptr;
 
-		ptr = kstrdup((tail ? tail + 1 : filename), GFP_KERNEL);
-		if (!ptr) {
+		tail = kstrdup(kbasename(filename), GFP_KERNEL);
+		if (!tail) {
 			ret = -ENOMEM;
 			goto fail_address_parse;
 		}
 
-		tail = ptr;
 		ptr = strpbrk(tail, ".-_");
 		if (ptr)
 			*ptr = '\0';
Signature · 1 line
1.7.10.4