copy_strtok_from_user
Subsystems (6)
Steven Rostedt
maintainerv3 cover copy_strtok_from_user
Changes in v3:
- created strtok_r for kernel.
- Simplified copy_strtok_from_user by using strtok_r
- updated the rest of the code to handle the above changes
I tested both the strtok_r and copy_strtok_from_user with this user app:
http://www.kernel.org/pub/linux/kernel/people/rostedt/copy_strtok_from_user.c
Results of the test are here:
http://www.kernel.org/pub/linux/kernel/people/rostedt/copy_strtok_test.txt
I added a "#undef strtok_r" after the strtok_r function and ran it again to make sure the output was still the same, and it was.
Andrew, If this version is fine with you, could you ack the patches that create the strtok_r and copy_strtok_from_user functions.
Thanks,
-- 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 (6):
uaccess: move probe_kernel_* functions to lib/uaccess.c
string: add strtok_r to kernel
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/string.h | 3 + include/linux/uaccess.h | 4 + kernel/trace/ftrace.c | 149 ++++++++++++++++--------------- kernel/trace/trace.h | 2 + kernel/trace/trace_events.c | 69 +++++--------- lib/Makefile | 3 +- lib/string.c | 43 +++++++++ lib/uaccess.c | 205 +++++++++++++++++++++++++++++++++++++++++++ mm/Makefile | 2 +- mm/maccess.c | 55 ------------ 10 files changed, 361 insertions(+), 174 deletions(-)
Steven Rostedt
maintainerPATCH v3 1/6 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
@@ -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
@@ -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 --
Steven Rostedt
maintainerPATCH v3 2/6 string: add strtok_r to kernel
In reply to Steven RostedtFrom: Steven Rostedt <[email protected]>
This patch adds the function strtok_r to the kernel. This acts just like the strtok_r that is implemented in glibc, but trimmed down. Note, this is strtok_r and not strtok. The _r version is reentrant, where as strtok is not, and thus not suitable for the kernel.
--- include/linux/string.h | 3 +++ lib/string.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 0 deletions(-)
include/linux/string.h +3 -0
@@ -76,6 +76,9 @@ extern char * strpbrk(const char *,const char *);#ifndef __HAVE_ARCH_STRSEPextern char *strsep (char **,const char *);#endif +#ifndef __HAVE_ARCH_STRTOK_R +extern char *strtok_r (char *,const char *,char **); +#endif #ifndef __HAVE_ARCH_STRSPNextern __kernel_size_t strspn (const char *,const char *);#endif
lib/string.c +43 -0
49 lines not on this page · raw message
Signature · 3 lines
1.5.6.5 --
Steven Rostedt
maintainerPATCH v3 3/6 uaccess: add copy_strtok_from_user
In reply to Steven RostedtFrom: 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 | 4 + lib/uaccess.c | 152 ++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 155 insertions(+), 1 deletions(-)
include/linux/uaccess.h +4 -0
@@ -106,4 +106,8 @@ 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,const char *delim); +#endif /* __LINUX_UACCESS_H__ */
lib/uaccess.c +151 -1
162 lines not on this page · raw message
Signature · 3 lines
1.5.6.5 --
Steven Rostedt
maintainerPATCH v3 4/6 tracing: convert event_trace to use copy_strtok_from_user
In reply to Steven RostedtFrom: 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 | 69 +++++++++++++++--------------------------- 2 files changed, 27 insertions(+), 44 deletions(-)
kernel/trace/trace.h +2 -0
@@ -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 +25 -44
87 lines not on this page · raw message
Signature · 3 lines
1.5.6.5 --
Steven Rostedt
maintainerPATCH v3 5/6 tracing: convert ftrace_regex_write to use copy_strtok_from_user
In reply to Steven RostedtFrom: 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 | 101 +++++++++++++++++++++++++++++-------------------- 1 files changed, 60 insertions(+), 41 deletions(-)
kernel/trace/ftrace.c +60 -41
@@ -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,75 @@ 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; +/* do not handle leading spaces. */ + skip =0 ; + }else { + buf = iter->buffer; +/* iter->buffer is size of FTRACE_BUFF_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) - goto out; - read++; - cnt--; - } + ret =copy_strtok_from_user (buf, ubuf, len, cnt, &copied,SPACES ); +if (ret <0 ) + goto out; -if (isspace (ch)) { - file->f_pos += read; - ret = read; - goto out; +if (!skip) { +/* +* This is a continuation. If we read spaces, then +* we need to truncate all that was read. +*/ +if (ret > copied) { +/* leading spaces were found */ + iter->buffer[iter->buffer_idx] =0 ; +/* make it look like we are ready to process */ + buf = iter->buffer; + copied = iter->buffer_idx +1 ; } + } - iter->buffer_idx =0 ; +/* We only read white space. */ +if (!copied) { + (*ppos) += cnt; + ret = cnt; + goto out; } -while (cnt && !isspace (ch)) { -if (iter->buffer_idx <FTRACE_BUFF_MAX ) - iter->buffer[iter->buffer_idx++] = ch; -else { +/* Check if we hit a terminating space */ +if (buf[copied -1 ] !=0 ) { +/* 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; } - ret =get_user (ch, ubuf++); -if (ret) - goto out; - read++; - 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 + (*ppos) += cnt; + ret = cnt; iter->flags |=FTRACE_ITER_CONT ; + goto out; + } + read = ret; - file->f_pos += read; + 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 --
Steven Rostedt
maintainerPATCH v3 6/6 tracing: convert ftrace_graph_write to use copy_strtok_from_user
In reply to Steven RostedtFrom: 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 | 48 ++++++++++++++++-------------------------------- 1 files changed, 16 insertions(+), 32 deletions(-)
kernel/trace/ftrace.c +16 -32
@@ -2083,10 +2083,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);
@@ -2102,48 +2101,33 @@ ftrace_graph_write(struct file *file, const char __user *ubuf, }else array = file->private_data; - ret =get_user (ch, ubuf++); -if (ret) + ret =copy_strtok_from_user (buffer, ubuf,FTRACE_BUFF_MAX +1 , + cnt, &copied,SPACES ); +if (ret <0 ) goto out; - read++; - cnt--; -/* skip white space */ -while (cnt &&isspace (ch)) { - ret =get_user (ch, ubuf++); -if (ret) - goto out; - read++; - cnt--; +/* We only read white space. */ +if (!copied) { + (*ppos) += cnt; + ret = cnt; + goto out; } -if (isspace (ch)) { - *ppos += read; - ret = read; +/* Check if we hit a terminating space */ +if (buffer[copied -1 ] !=0 ) { +/* This does not deal with split words */ + ret = -EINVAL ; 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 ; + read = ret;/* 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 --