Skip to contentSkip to conversation

lib/string: introduce kbasename helper

PATCH Superseded Andy Shevchenko 4 participants 15 messages Oct 3, 2012 – Oct 5, 2012

Subsystems (8)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 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.

Since v1:
- fix changelog of the patch 1 - we are doing basname(3) alike helper
- usb related patch temporary excluded from series (under discussion with Greg)

Andy Shevchenko (6):
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 |   16 +++-------------
 fs/proc/proc_devtree.c            |    7 ++-----
 include/linux/string.h            |   11 +++++++++++
 kernel/trace/trace_uprobe.c       |    6 +++---
 lib/dynamic_debug.c               |    9 +--------
 mm/memory.c                       |    8 +++-----
 6 files changed, 23 insertions(+), 34 deletions(-)
Signature · 1 line
1.7.10.4
#1 lore raw

PATCH v2 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 b917881..b09a342 100644
@@ -147,5 +147,16 @@ 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 /* __KERNEL__ */
 #endif /* _LINUX_STRING_H_ */
Signature · 1 line
1.7.10.4
#2 lore raw

PATCH v2 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
#3 lore raw

PATCH v2 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.

Cc YAMANE Toshiaki <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
---
 drivers/staging/rts_pstor/trace.h |   16 +++-------------
 1 file changed, 3 insertions(+), 13 deletions(-)
drivers/staging/rts_pstor/trace.h +3 -13
index cf60a1b..59c5686 100644
@@ -24,26 +24,16 @@
 #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;
+	return kbasename(path);
 }
 
 #define TRACE_RET(chip, ret)   										\
Signature · 1 line
1.7.10.4
#4 lore raw

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

Quoted 32 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.

Cc YAMANE Toshiaki <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
---
 drivers/staging/rts_pstor/trace.h |   16 +++-------------
 1 file changed, 3 insertions(+), 13 deletions(-)
drivers/staging/rts_pstor/trace.h +2 -4
index cf60a1b..59c5686 100644
@@ -24,26 +24,16 @@
 #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;

drivers/staging/rts_pstor/trace.h line 36

Quoted 10 lines
drivers/staging/rts_pstor/trace.h +1 -9
-
-	while (*ptr != '\0') {
-		if ((*ptr == '\\') || (*ptr == '/'))
-			path = ptr + 1;
-		
-		ptr++;
-	}
-
-	return path;
+	return kbasename(path);

Looks like you silentely drop const qualifier here from kbasename() return value.

Signature · 1 line
 Kirill A. Shutemov
#5 lore raw

PATCH v2 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 0e3a516..6b101a2 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>
#6 lore raw

PATCH v2 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
#7 lore raw

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

In reply to Andy Shevchenko
Cc Steven Rostedt <[email protected]> (maintainer:TRACING)
Cc Frederic Weisbecker <[email protected]> (maintainer:TRACING)
---
 kernel/trace/trace_uprobe.c |    6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/trace/trace_uprobe.c +3 -3
index 03003cd..a2b2fab 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);
+		tail = ptr = kstrdup(kbasename(filename), GFP_KERNEL);
 		if (!ptr) {
 			ret = -ENOMEM;
 			goto fail_address_parse;
 		}
 
-		tail = ptr;
 		ptr = strpbrk(tail, ".-_");
 		if (ptr)
 			*ptr = '\0';
Signature · 1 line
1.7.10.4
#8 lore raw

[PATCHv2.5] trace: reuse kbasename() functionality

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
maintainer
#9 lore raw

Re: [PATCHv2.5] trace: reuse kbasename() functionality

Quoted 20 lines · 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 +1 -0
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"
 

kernel/trace/trace_uprobe.c static int create_trace_uprobe(int argc, char **argv) · line 270

Quoted 10 lines
kernel/trace/trace_uprobe.c +2 -3
@@ -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);

I don't see kbasename() anywhere. Is this based off of other patches?

-- Steve

Quoted 9 lines
kernel/trace/trace_uprobe.c +1 -1
+		if (!tail) {
 			ret = -ENOMEM;
 			goto fail_address_parse;
 		}
 
-		tail = ptr;
 		ptr = strpbrk(tail, ".-_");
 		if (ptr)
 			*ptr = '\0';
#10 lore raw

Re: [PATCHv2.5] trace: reuse kbasename() functionality

Quoted 28 lines · Steven Rostedt
Quoted 27 lines · 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 +2 -1
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;
 
Quoted 5 lines

kernel/trace/trace_uprobe.c static int create_trace_uprobe(int argc, char **argv) · line 270

Quoted 3 lines
kernel/trace/trace_uprobe.c +1 -2
-             ptr = kstrdup((tail ? tail + 1 : filename), GFP_KERNEL);
-             if (!ptr) {
+             tail = kstrdup(kbasename(filename), GFP_KERNEL);

I don't see kbasename() anywhere. Is this based off of other patches?

It's introduced by first patch in the series.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#11 lore raw

Re: [PATCHv2.5] trace: reuse kbasename() functionality

Quoted 3 lines · Andy Shevchenko
Quoted 1 line

I don't see kbasename() anywhere. Is this based off of other patches?

It's introduced by first patch in the series.

Usually a series has the format of:

[PATCH x/n] ....

Where x is the patch number and n is the total number of patches. I'm not sure what a v2.5 is. This is version 2 and a half?

-- Seve

maintainer
#12 lore raw

Re: [PATCHv2.5] trace: reuse kbasename() functionality

Quoted 1 line · Steven Rostedt

-- Seve

Error: SIGFAULT!

-- Steve ;-)

#13 lore raw

Re: [PATCHv2.5] trace: reuse kbasename() functionality

In reply to Steven Rostedt
Quoted 10 lines · Steven Rostedt
Quoted 2 lines · Andy Shevchenko
Quoted 1 line

I don't see kbasename() anywhere. Is this based off of other patches?

It's introduced by first patch in the series.

Usually a series has the format of:

[PATCH x/n] ....

Where x is the patch number and n is the total number of patches. I'm not sure what a v2.5 is. This is version 2 and a half?

Ah, it's an update to patch 6/6. You could see it by Message-Id/In-Reply-To chains.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#14 lore raw

Re: [PATCHv2.5] trace: reuse kbasename() functionality

Quoted 5 lines · Andy Shevchenko
Quoted 4 lines · Steven Rostedt
Quoted 2 lines · Andy Shevchenko
Quoted 1 line

I don't see kbasename() anywhere. Is this based off of other patches?

It's introduced by first patch in the series.

Quoted 9 lines
Quoted 6 lines

Usually a series has the format of:

[PATCH x/n] ....

Where x is the patch number and n is the total number of patches. I'm not sure what a v2.5 is. This is version 2 and a half?

Ah, it's an update to patch 6/6. You could see it by Message-Id/In-Reply-To chains.

OK, that's where the confusion stems from. The original patch 6/6 had in the patch:

Cc Steven Rostedt <[email protected]> (maintainer:TRACING)
Cc Frederic Weisbecker <[email protected]> (maintainer:TRACING)

But I neither I nor Frederic were on the actual Cc (I had to open my LKML folder to see it). Perhaps whatever tool you used to send the patches got confused by the (maintainer:TRACING) line.

Thus, the only thing that ended up in my INBOX was the [PATCHv2.5] one.

-- Steve