Skip to contentSkip to conversation

copy_strtok_from_user

RFC PATCH Superseded Steven Rostedt 3 participants 13 messages Feb 26, 2009

Subsystems (5)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Steven Rostedt

author maintainer
#0 lore raw

v2 cover copy_strtok_from_user

This is the second series of the uaccess code.

Changes in v2:

- moved probe_kernel_* functions to lib/uaccess.c

- renamed copy_word_from_user to copy_strtok_from_user.

- changed copy_strtok_from_user to pass in a delimiter string. ftrace defines SPACE to be ' \t\r\n'.

Ingo,
I added your copy right to lib/uaccess.c since git blame shows you as the author of the probe_kernel_* code. Also, is it OK that I added the "GPL v2" line in that file as well?

Andrew,
Since you are, in essence, the memory maintainer, could you give your Acked-by: to the copy_strtok_from_user code.

The probe_kernel code is still EXPORT_SYMBOL_GPL, and I added that too to copy_strtok_from_user. Are there any objections to that?

-- Steve

The following patches are in:

git://git.kernel.org/pub/scm/linux/kernel/git/rostedt/linux-2.6-trace.git

branch: tip/tracing/uaccess

Steven Rostedt (5):
uaccess: move probe_kernel_* functions to lib/uaccess.c
uaccess: add copy_strtok_from_user
tracing: convert event_trace to use copy_strtok_from_user
tracing: convert ftrace_regex_write to use copy_strtok_from_user
tracing: convert ftrace_graph_write to use copy_strtok_from_user

----

 include/linux/uaccess.h     |    5 +
 kernel/trace/ftrace.c       |  155 +++++++++++++++---------------
 kernel/trace/trace.h        |    2 +
 kernel/trace/trace_events.c |   78 +++++++--------
 lib/Makefile                |    3 +-
 lib/uaccess.c               |  229 +++++++++++++++++++++++++++++++++++++++++++
 mm/Makefile                 |    2 +-
 mm/maccess.c                |   55 ----------
 8 files changed, 351 insertions(+), 178 deletions(-)
author maintainer
#1 lore raw

PATCH v2 1/5 uaccess: move probe_kernel_* functions to lib/uaccess.c

From: Steven Rostedt <[email protected]>

This patch moves the file mm/maccess.c to lib/uaccess.c. This is a more generic location to find lib utils. Since probe_kernel_* are declared in linux/uaccess.h, having them in a file named uaccess.c seems to be a better place.

---
 lib/Makefile  |    3 ++-
 lib/uaccess.c |   55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 mm/Makefile   |    2 +-
 mm/maccess.c  |   55 -------------------------------------------------------
 4 files changed, 58 insertions(+), 57 deletions(-)
 create mode 100644 lib/uaccess.c
 delete mode 100644 mm/maccess.c
lib/Makefile +2 -1
index 32b0e64..46ce28c 100644
@@ -11,7 +11,8 @@ lib-y := ctype.o string.o vsprintf.o cmdline.o \
 	 rbtree.o radix-tree.o dump_stack.o \
 	 idr.o int_sqrt.o extable.o prio_tree.o \
 	 sha1.o irq_regs.o reciprocal_div.o argv_split.o \
-	 proportions.o prio_heap.o ratelimit.o show_mem.o is_single_threaded.o
+	 proportions.o prio_heap.o ratelimit.o show_mem.o is_single_threaded.o \
+	 uaccess.o
 
 lib-$(CONFIG_MMU) += ioremap.o
 lib-$(CONFIG_SMP) += cpumask.o
lib/uaccess.c new file +55 -0

55 lines not on this page · raw message

mm/Makefile +1 -1
index 72255be..90323d1 100644
@@ -8,7 +8,7 @@ mmu-$(CONFIG_MMU)	:= fremap.o highmem.o madvise.o memory.o mincore.o \
 			   vmalloc.o
 
 obj-y			:= bootmem.o filemap.o mempool.o oom_kill.o fadvise.o \
-			   maccess.o page_alloc.o page-writeback.o pdflush.o \
+			   page_alloc.o page-writeback.o pdflush.o \
 			   readahead.o swap.o truncate.o vmscan.o shmem.o \
 			   prio_tree.o util.o mmzone.o vmstat.o backing-dev.o \
 			   page_isolation.o mm_init.o $(mmu-y)
mm/maccess.c deleted +0 -55

55 lines not on this page · raw message

Signature · 3 lines
1.5.6.5

-- 
author maintainer
#2 lore raw

PATCH v2 2/5 uaccess: add copy_strtok_from_user

In reply to Steven Rostedt

From: Steven Rostedt <[email protected]>

The ftrace utility reads delimited tokens from user space. Andrew Morton did not like how ftrace open coded this. He had a good point since more than one location performed this feature.

This patch creates a copy_strtok_from_user function that can copy a delimited token from user space. This puts the code in the lib/uaccess.c file. This keeps the code in a single location and may be optimized in the future.

---
 include/linux/uaccess.h |    5 ++
 lib/uaccess.c           |  176 ++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 180 insertions(+), 1 deletions(-)
include/linux/uaccess.h +5 -0
index 6b58367..08769ad 100644
@@ -106,4 +106,9 @@ extern long probe_kernel_read(void *dst, void *src, size_t size);
  */
 extern long probe_kernel_write(void *dst, void *src, size_t size);
 
+extern int copy_strtok_from_user(void *to, const void __user *from,
+				 unsigned int copy, unsigned int read,
+				 unsigned int *copied, int skip,
+				 const char *delim);
+
 #endif		/* __LINUX_UACCESS_H__ */
lib/uaccess.c +175 -1

186 lines not on this page · raw message

Signature · 3 lines
1.5.6.5

-- 
author maintainer
#3 lore raw

PATCH v2 3/5 tracing: convert event_trace to use copy_strtok_from_user

In reply to Steven Rostedt

From: Steven Rostedt <[email protected]>

Impact: clean up

This patch converts the open coded retrieving of a word from user space to use copy_strtok_from_user.

Also removed a cnt < 0 check that Andrew Morton pointed out saying that it was irrelevant since cnt is unsigned.

Also changed file->pos += to (*ppos) += which is the proper way to modify positions of the file.

---
 kernel/trace/trace.h        |    2 +
 kernel/trace/trace_events.c |   78 +++++++++++++++++++------------------------
 2 files changed, 36 insertions(+), 44 deletions(-)
kernel/trace/trace.h +2 -0
index 6321917..e75b673 100644
@@ -720,4 +720,6 @@ static inline void trace_branch_disable(void)
 }
 #endif /* CONFIG_BRANCH_TRACER */
 
+#define SPACES	" \t\r\n"
+
 #endif /* _LINUX_KERNEL_TRACE_H */
kernel/trace/trace_events.c +34 -44

96 lines not on this page · raw message

Signature · 3 lines
1.5.6.5

-- 
author maintainer
#4 lore raw

PATCH v2 4/5 tracing: convert ftrace_regex_write to use copy_strtok_from_user

In reply to Steven Rostedt

From: Steven Rostedt <[email protected]>

Impact: clean up

This removes the open coded parsing of a word sent in by the user and replaces it with copy_strtok_from_user.

Also removes cnt < 0 check since cnt is unsigned.

Also uses (*ppos) += cnt, instead of file->pos += cnt.

---
 kernel/trace/ftrace.c |  103 ++++++++++++++++++++++++++++---------------------
 1 files changed, 59 insertions(+), 44 deletions(-)
kernel/trace/ftrace.c +58 -43
index 5a3a06b..4ecce15 100644
@@ -1690,11 +1690,13 @@ ftrace_regex_write(struct file *file, const char __user *ubuf,
 		   size_t cnt, loff_t *ppos, int enable)
 {
 	struct ftrace_iterator *iter;
-	char ch;
+	unsigned int copied;
 	size_t read = 0;
+	int len, skip;
 	ssize_t ret;
+	char *buf;
 
-	if (!cnt || cnt < 0)
+	if (!cnt)
 		return 0;
 
 	mutex_lock(&ftrace_regex_lock);
@@ -1710,58 +1712,71 @@ ftrace_regex_write(struct file *file, const char __user *ubuf,
 		iter->buffer_idx = 0;
 	}
 
-	ret = get_user(ch, ubuf++);
-	if (ret)
-		goto out;
-	read++;
-	cnt--;
+	if (iter->flags & FTRACE_ITER_CONT) {
+		/*
+		 * We split on a word, continue from where
+		 * we left off.
+		*/
+		buf = &iter->buffer[iter->buffer_idx];
+		len = FTRACE_BUFF_MAX - iter->buffer_idx;
+		/* Stop on leading spaces */
+		skip = 0;
+	} else {
+		buf = iter->buffer;
+		/* iter->buffer is size of FTRACE_BUF_MAX + 1 */
+		len = FTRACE_BUFF_MAX;
+		iter->buffer_idx = 0;
+		/* Skip over any leading spaces */
+		skip = 1;
+	}
 
-	if (!(iter->flags & ~FTRACE_ITER_CONT)) {
-		/* skip white space */
-		while (cnt && isspace(ch)) {
-			ret = get_user(ch, ubuf++);
-			if (ret)
+	ret = copy_strtok_from_user(buf, ubuf, len, cnt, &copied, skip, SPACES);
+
+	if (ret == -EAGAIN) {
+		if (copied) {
+			/* we split on a word */
+			iter->buffer_idx += copied;
+			if (iter->buffer_idx >= FTRACE_BUFF_MAX) {
+				/* too big of a word */
+				iter->buffer_idx = 0;
+				iter->flags &= ~FTRACE_ITER_CONT;
+				ret = -EINVAL;
 				goto out;
-			read++;
-			cnt--;
-		}
+			}
 
-		if (isspace(ch)) {
-			file->f_pos += read;
-			ret = read;
+			(*ppos) += cnt;
+			ret = cnt;
+			iter->flags |= FTRACE_ITER_CONT;
 			goto out;
 		}
-
-		iter->buffer_idx = 0;
-	}
-
-	while (cnt && !isspace(ch)) {
-		if (iter->buffer_idx < FTRACE_BUFF_MAX)
-			iter->buffer[iter->buffer_idx++] = ch;
-		else {
-			ret = -EINVAL;
+		/* We only read white space. */
+		if (!(iter->flags & FTRACE_ITER_CONT)) {
+			(*ppos) += cnt;
+			ret = cnt;
 			goto out;
 		}
-		ret = get_user(ch, ubuf++);
-		if (ret)
-			goto out;
-		read++;
-		cnt--;
+
+		/*
+		 * We read white space but we are also at the end
+		 * of a word in the buffer. Continue processing.
+		 */
+		ret = cnt;
 	}
 
-	if (isspace(ch)) {
-		iter->filtered++;
-		iter->buffer[iter->buffer_idx] = 0;
-		ret = ftrace_process_regex(iter->buffer,
-					   iter->buffer_idx, enable);
-		if (ret)
-			goto out;
-		iter->buffer_idx = 0;
-	} else
-		iter->flags |= FTRACE_ITER_CONT;
+	if (ret < 0)
+		goto out;
 
+	/* NULL terminate the string */
+	buf[copied] = 0;
 
-	file->f_pos += read;
+	read = ret;
+
+	ret = ftrace_process_regex(iter->buffer, iter->buffer_idx, enable);
+	if (ret)
+		goto out;
+	iter->buffer_idx = 0;
+
+	(*ppos) += read;
 
 	ret = read;
  out:
Signature · 3 lines
1.5.6.5

-- 
author maintainer
#5 lore raw

PATCH v2 5/5 tracing: convert ftrace_graph_write to use copy_strtok_from_user

In reply to Steven Rostedt

From: Steven Rostedt <[email protected]>

Impact: clean up

This removes the open coded parsing of a word sent in by the user and replaces it with copy_strtok_from_user.

Also removes cnt < 0 check since cnt is unsigned.

Also uses (*ppos) += cnt, instead of file->pos += cnt.

---
 kernel/trace/ftrace.c |   52 +++++++++++++++++-------------------------------
 1 files changed, 19 insertions(+), 33 deletions(-)
kernel/trace/ftrace.c +19 -33
index 4ecce15..6204b3c 100644
@@ -2079,10 +2079,9 @@ ftrace_graph_write(struct file *file, const char __user *ubuf,
 	unsigned long *array;
 	size_t read = 0;
 	ssize_t ret;
-	int index = 0;
-	char ch;
+	int copied;
 
-	if (!cnt || cnt < 0)
+	if (!cnt)
 		return 0;
 
 	mutex_lock(&graph_lock);
@@ -2098,48 +2097,35 @@ ftrace_graph_write(struct file *file, const char __user *ubuf,
 	} else
 		array = file->private_data;
 
-	ret = get_user(ch, ubuf++);
-	if (ret)
-		goto out;
-	read++;
-	cnt--;
+	ret = copy_strtok_from_user(buffer, ubuf, FTRACE_BUFF_MAX,
+				    cnt, &copied, 1, SPACES);
 
-	/* skip white space */
-	while (cnt && isspace(ch)) {
-		ret = get_user(ch, ubuf++);
-		if (ret)
+	if (ret == -EAGAIN) {
+		if (copied) {
+			/* This do not deal with split words */
+			ret = -EINVAL;
 			goto out;
-		read++;
-		cnt--;
-	}
+		}
 
-	if (isspace(ch)) {
-		*ppos += read;
-		ret = read;
+		/* Just white space */
+		(*ppos) += cnt;
+		ret = cnt;
 		goto out;
 	}
 
-	while (cnt && !isspace(ch)) {
-		if (index < FTRACE_BUFF_MAX)
-			buffer[index++] = ch;
-		else {
-			ret = -EINVAL;
-			goto out;
-		}
-		ret = get_user(ch, ubuf++);
-		if (ret)
-			goto out;
-		read++;
-		cnt--;
-	}
-	buffer[index] = 0;
+	if (ret < 0)
+		goto out;
+
+	read = ret;
+
+	buffer[copied] = 0;
 
 	/* we allow only one expression at a time */
 	ret = ftrace_set_func(array, &ftrace_graph_count, buffer);
 	if (ret)
 		goto out;
 
-	file->f_pos += read;
+	(*ppos) += read;
 
 	ret = read;
  out:
Signature · 3 lines
1.5.6.5

-- 
#6 lore raw
In reply to Steven Rostedt
Quoted 16 lines · Steven Rostedt

This is the second series of the uaccess code.

Changes in v2:

- moved probe_kernel_* functions to lib/uaccess.c

- renamed copy_word_from_user to copy_strtok_from_user.

- changed copy_strtok_from_user to pass in a delimiter string. ftrace defines SPACE to be ' \t\r\n'.

Ingo,
I added your copy right to lib/uaccess.c since git blame shows you as the author of the probe_kernel_* code. Also, is it OK that I added the "GPL v2" line in that file as well?

Quoted 6 lines

Andrew,
Since you are, in essence, the memory maintainer, could you give your Acked-by: to the copy_strtok_from_user code.

The probe_kernel code is still EXPORT_SYMBOL_GPL, and I added that too to copy_strtok_from_user. Are there any objections to that?

I have to ask,..

cant this be done with a regular copy_from_user() followed by a regular strtok()? Do we really have to combine all that?

#7 lore raw
Quoted 21 lines · Peter Zijlstra
Quoted 20 lines · Steven Rostedt

This is the second series of the uaccess code.

Changes in v2:

- moved probe_kernel_* functions to lib/uaccess.c

- renamed copy_word_from_user to copy_strtok_from_user.

- changed copy_strtok_from_user to pass in a delimiter string. ftrace defines SPACE to be ' \t\r\n'.

Ingo,
I added your copy right to lib/uaccess.c since git blame shows you as the author of the probe_kernel_* code. Also, is it OK that I added the "GPL v2" line in that file as well?

Andrew,
Since you are, in essence, the memory maintainer, could you give your Acked-by: to the copy_strtok_from_user code.

Quoted 7 lines
Quoted 2 lines

The probe_kernel code is still EXPORT_SYMBOL_GPL, and I added that too to copy_strtok_from_user. Are there any objections to that?

I have to ask,..

cant this be done with a regular copy_from_user() followed by a regular strtok()? Do we really have to combine all that?

yes. Note that strsep() is the preferred API. (in fact it's the only such string API that is in the kernel)

Ingo

#8 lore raw
Quoted 27 lines · Ingo Molnar
Quoted 24 lines · Peter Zijlstra
Quoted 22 lines · Steven Rostedt

This is the second series of the uaccess code.

Changes in v2:

- moved probe_kernel_* functions to lib/uaccess.c

- renamed copy_word_from_user to copy_strtok_from_user.

- changed copy_strtok_from_user to pass in a delimiter string. ftrace defines SPACE to be ' \t\r\n'.

Ingo,
I added your copy right to lib/uaccess.c since git blame shows you as the author of the probe_kernel_* code. Also, is it OK that I added the "GPL v2" line in that file as well?

Andrew,
Since you are, in essence, the memory maintainer, could you give your Acked-by: to the copy_strtok_from_user code.

The probe_kernel code is still EXPORT_SYMBOL_GPL, and I added that too to copy_strtok_from_user. Are there any objections to that?

Quoted 8 lines
Quoted 5 lines

I have to ask,..

cant this be done with a regular copy_from_user() followed by a regular strtok()? Do we really have to combine all that?

yes. Note that strsep() is the preferred API. (in fact it's the only such string API that is in the kernel)

btw., for larger strings we cannot really copy into the kernel i suspect. We'd have to kmalloc() and that adds overhead, etc.

Nevertheless, copy_strsep_from_user() would be the symmetric API here.

Ingo

author maintainer
#9 lore raw
Quoted 35 lines · Ingo Molnar
Quoted 33 lines · Ingo Molnar
Quoted 29 lines · Peter Zijlstra
Quoted 16 lines · Steven Rostedt

This is the second series of the uaccess code.

Changes in v2:

- moved probe_kernel_* functions to lib/uaccess.c

- renamed copy_word_from_user to copy_strtok_from_user.

- changed copy_strtok_from_user to pass in a delimiter string. ftrace defines SPACE to be ' \t\r\n'.

Ingo,
I added your copy right to lib/uaccess.c since git blame shows you as the author of the probe_kernel_* code. Also, is it OK that I added the "GPL v2" line in that file as well?

Quoted 6 lines

Andrew,
Since you are, in essence, the memory maintainer, could you give your Acked-by: to the copy_strtok_from_user code.

The probe_kernel code is still EXPORT_SYMBOL_GPL, and I added that too to copy_strtok_from_user. Are there any objections to that?

I have to ask,..

cant this be done with a regular copy_from_user() followed by a regular strtok()? Do we really have to combine all that?

Quoted 8 lines
Quoted 2 lines

yes. Note that strsep() is the preferred API. (in fact it's the only such string API that is in the kernel)

btw., for larger strings we cannot really copy into the kernel i suspect. We'd have to kmalloc() and that adds overhead, etc.

Nevertheless, copy_strsep_from_user() would be the symmetric API here.

not really. strsep stops at the first delimiter. strtok skips multiple delimiters. I could implement the copy_strtok_from_user with a copy_from_user (all of read), and then use strtok_r (reentrant version) to find the next token. This would require implementing strtok_r for the kernel.

Having the copy_strtok_from_user will handle the copying and the caller would not need to worry about having a big enough buffer to hold delimiters and tokens. The copy_strtok_from_user would copy first to an internal buffer, then scan for tokens to copy into the callers buffer. Then repeat until read or copy limits have been hit.

-- Steve

author maintainer
#10 lore raw
Quoted 2 lines · Steven Rostedt

not really. strsep stops at the first delimiter. strtok skips multiple

I may have read the man page wrong. It does stop at the token not the delimiter.

Never mind :-p

-- Steve

Quoted 14 lines

delimiters. I could implement the copy_strtok_from_user with a copy_from_user (all of read), and then use strtok_r (reentrant version) to find the next token. This would require implementing strtok_r for the kernel.

Having the copy_strtok_from_user will handle the copying and the caller would not need to worry about having a big enough buffer to hold delimiters and tokens. The copy_strtok_from_user would copy first to an internal buffer, then scan for tokens to copy into the callers buffer. Then repeat until read or copy limits have been hit.

-- Steve

author maintainer
#11 lore raw
In reply to Ingo Molnar
Quoted 9 lines · Ingo Molnar
Quoted 3 lines

yes. Note that strsep() is the preferred API. (in fact it's the only such string API that is in the kernel)

btw., for larger strings we cannot really copy into the kernel i suspect. We'd have to kmalloc() and that adds overhead, etc.

Nevertheless, copy_strsep_from_user() would be the symmetric API here.

OK, I'll rebase again calling it copy_strsep_from_user. And I'll change the implementation to use copy_from_user/strsep

-- Steve

author maintainer
#12 lore raw
Quoted 6 lines · Steven Rostedt
Quoted 4 lines · Ingo Molnar
Quoted 3 lines

yes. Note that strsep() is the preferred API. (in fact it's the only such string API that is in the kernel)

Quoted 8 lines
Quoted 5 lines

btw., for larger strings we cannot really copy into the kernel i suspect. We'd have to kmalloc() and that adds overhead, etc.

Nevertheless, copy_strsep_from_user() would be the symmetric API here.

OK, I'll rebase again calling it copy_strsep_from_user. And I'll change the implementation to use copy_from_user/strsep

I need to have my first cup of coffee _before_ replying :-p

My first understanding of strsep was correct. strsep stops at the first delimiter. It may be time to add strtok_r into the kernel.

-- Steve