Skip to contentSkip to conversation

tracing: dont reset set_ftrace_filter/notrace when opened with r/w perm

PATCH In review Jiri Olsa 4 participants 9 messages Jul 16, 2009 – Jul 22, 2009

Subsystems (2)
lore
Share & tools
lore

Jiri Olsa

author
#0 lore raw

PATCH 1/1 tracing: dont reset set_ftrace_filter/notrace when opened with r/w perm

If user setup set_ftrace_filter/set_ftrace_notrace files and then opens them with read&write permissions, the previous setup will be removed.

Tested with following program:

[snip]
#include <stdio.h>
#include <string.h>

int main(int argc, char **argv)

{
	FILE *f;
	char *mode = "w+";
	char *file = argv[1];

	if (argc == 3)
		mode = argv[2];

	if (NULL == (f = fopen(file, mode))) {
		perror("fopen failed");
		return -1;
	}

	while(!feof(f)) {
#define BUFLEN 100
		char buf[BUFLEN];
		memset(buf, 0, BUFLEN);
		fgets(buf, BUFLEN, f);
		printf(buf);
	}

	fclose(f);
	return 0;
}

[snip]

wbr,
jirka

---

kernel/trace/ftrace.c +3 -4
index 4521c77..11394bc 100644
@@ -1661,10 +1661,6 @@ ftrace_regex_open(struct inode *inode, struct file *file, int enable)
 		return -ENOMEM;
 
 	mutex_lock(&ftrace_regex_lock);
-	if ((file->f_mode & FMODE_WRITE) &&
-	    !(file->f_flags & O_APPEND))
-		ftrace_filter_reset(enable);
-
 	if (file->f_mode & FMODE_READ) {
 		iter->pg = ftrace_pages_start;
 		iter->flags = enable ? FTRACE_ITER_FILTER :
@@ -2260,6 +2256,9 @@ ftrace_regex_write(struct file *file, const char __user *ubuf,
 		return 0;
 
 	mutex_lock(&ftrace_regex_lock);
+	if ((file->f_mode & FMODE_WRITE) &&
+	    !(file->f_flags & O_APPEND))
+		ftrace_filter_reset(enable);
 
 	if (file->f_mode & FMODE_READ) {
 		struct seq_file *m = file->private_data;
#1 lore raw
Quoted 3 lines · Jiri Olsa

If user setup set_ftrace_filter/set_ftrace_notrace files and then opens them with read&write permissions, the previous setup will be removed.

Currently:

 # echo 'sys_open sys_close' > set_ftrace_filter
 # cat set_ftrace_filter
 sys_open
 sys_close

After your patch:

 # echo 'sys_open sys_close' > set_ftrace_filter
 # cat set_ftrace_filter
 sys_close
Quoted 1 line

Tested with following program:

...

Quoted 30 lines

---

kernel/trace/ftrace.c +3 -5
index 4521c77..11394bc 100644
@@ -1661,10 +1661,6 @@ ftrace_regex_open(struct inode *inode, struct file *file, int enable)
 		return -ENOMEM;
 
 	mutex_lock(&ftrace_regex_lock);
-	if ((file->f_mode & FMODE_WRITE) &&
-	    !(file->f_flags & O_APPEND))
-		ftrace_filter_reset(enable);
-
 	if (file->f_mode & FMODE_READ) {
 		iter->pg = ftrace_pages_start;
 		iter->flags = enable ? FTRACE_ITER_FILTER :
@@ -2260,6 +2256,9 @@ ftrace_regex_write(struct file *file, const char __user *ubuf,
 		return 0;
 
 	mutex_lock(&ftrace_regex_lock);
+	if ((file->f_mode & FMODE_WRITE) &&
+	    !(file->f_flags & O_APPEND))
+		ftrace_filter_reset(enable);
 
 	if (file->f_mode & FMODE_READ) {
 		struct seq_file *m = file->private_data;
--
author
#2 lore raw
Quoted 12 lines · Li Zefan
Quoted 3 lines · Jiri Olsa

If user setup set_ftrace_filter/set_ftrace_notrace files and then opens them with read&write permissions, the previous setup will be removed.

Currently:

 # echo 'sys_open sys_close' > set_ftrace_filter
 # cat set_ftrace_filter
 sys_open
 sys_close
Quoted 6 lines

After your patch:

 # echo 'sys_open sys_close' > set_ftrace_filter
 # cat set_ftrace_filter
 sys_close

oops, sry I missed this..

Following patch adds new FTRACE_ITER_RESET flag, as the decision needs to be taken in "open" and applied in "write". I'm not sure whats the policy for adding new flags, but it looks ok to me.

wbr,
jirka

kernel/trace/ftrace.c +8 -2
index 4521c77..29c4b67 100644
@@ -1364,6 +1364,7 @@ enum {
 	FTRACE_ITER_FAILURES	= (1 << 3),
 	FTRACE_ITER_PRINTALL	= (1 << 4),
 	FTRACE_ITER_HASH	= (1 << 5),
+	FTRACE_ITER_RESET	= (1 << 6),
 };
 
 #define FTRACE_BUFF_MAX (KSYM_SYMBOL_LEN+4) /* room for wildcards */
@@ -1663,11 +1664,11 @@ ftrace_regex_open(struct inode *inode, struct file *file, int enable)
 	mutex_lock(&ftrace_regex_lock);
 	if ((file->f_mode & FMODE_WRITE) &&
 	    !(file->f_flags & O_APPEND))
-		ftrace_filter_reset(enable);
+		iter->flags |= FTRACE_ITER_RESET;
 
 	if (file->f_mode & FMODE_READ) {
 		iter->pg = ftrace_pages_start;
-		iter->flags = enable ? FTRACE_ITER_FILTER :
+		iter->flags |= enable ? FTRACE_ITER_FILTER :
 			FTRACE_ITER_NOTRACE;
 
 		ret = seq_open(file, &show_ftrace_seq_ops);
@@ -2267,6 +2268,11 @@ ftrace_regex_write(struct file *file, const char __user *ubuf,
 	} else
 		iter = file->private_data;
 
+	if (iter->flags & FTRACE_ITER_RESET) {
+		ftrace_filter_reset(enable);
+		iter->flags &= ~FTRACE_ITER_RESET;
+	}
+
 	if (!*ppos) {
 		iter->flags &= ~FTRACE_ITER_CONT;
 		iter->buffer_idx = 0;
#3 lore raw
Quoted 19 lines · Jiri Olsa
Quoted 11 lines · Li Zefan
Quoted 3 lines · Jiri Olsa

If user setup set_ftrace_filter/set_ftrace_notrace files and then opens them with read&write permissions, the previous setup will be removed.

Currently:

 # echo 'sys_open sys_close' > set_ftrace_filter
 # cat set_ftrace_filter
 sys_open
 sys_close
Quoted 6 lines

After your patch:

 # echo 'sys_open sys_close' > set_ftrace_filter
 # cat set_ftrace_filter
 sys_close
Quoted 6 lines

oops, sry I missed this..

Following patch adds new FTRACE_ITER_RESET flag, as the decision needs to be taken in "open" and applied in "write". I'm not sure whats the policy for adding new flags, but it looks ok to me.

I have no strong opinion whether to do the reset in "open" or in first "write".

All said, I think this is cleaner, without introducing a new flag:

@@ -2260,6 +2256,9 @@ ftrace_regex_write(struct file *file, const char __user *ubuf,
return 0;

mutex_lock(&ftrace_regex_lock);
+ if (file->f_pos == 0 &&
+ (file->f_mode & FMODE_WRITE) && !(file->f_flags & O_APPEND))
+ ftrace_filter_reset(enable);

if (file->f_mode & FMODE_READ) {
struct seq_file *m = file->private_data;

#4 lore raw
Quoted 33 lines · Li Zefan
Quoted 19 lines · Jiri Olsa
Quoted 11 lines · Li Zefan
Quoted 3 lines · Jiri Olsa

If user setup set_ftrace_filter/set_ftrace_notrace files and then opens them with read&write permissions, the previous setup will be removed.

Currently:

 # echo 'sys_open sys_close' > set_ftrace_filter
 # cat set_ftrace_filter
 sys_open
 sys_close
Quoted 6 lines

After your patch:

 # echo 'sys_open sys_close' > set_ftrace_filter
 # cat set_ftrace_filter
 sys_close
Quoted 6 lines

oops, sry I missed this..

Following patch adds new FTRACE_ITER_RESET flag, as the decision needs to be taken in "open" and applied in "write". I'm not sure whats the policy for adding new flags, but it looks ok to me.

I have no strong opinion whether to do the reset in "open" or in first "write".

All said, I think this is cleaner, without introducing a new flag:

Review ftrace_regex_write(struct file *file, const char __user *ubuf, · line 2264

Quoted 9 lines
(quoted diff) +3 -0
@@ -2260,6 +2256,9 @@ ftrace_regex_write(struct file *file, const char __user *ubuf,
 		return 0;
 
 	mutex_lock(&ftrace_regex_lock);
+	if (file->f_pos == 0 &&
+	    (file->f_mode & FMODE_WRITE) && !(file->f_flags & O_APPEND))
+		ftrace_filter_reset(enable);
 
 	if (file->f_mode & FMODE_READ) {
 		struct seq_file *m = file->private_data;

Yeah, that avoids this need of creating a new flag. Looks like a good solution.

author
#5 lore raw
Quoted 41 lines · Frederic Weisbecker
Quoted 40 lines · Li Zefan
Quoted 19 lines · Jiri Olsa
Quoted 11 lines · Li Zefan
Quoted 3 lines · Jiri Olsa

If user setup set_ftrace_filter/set_ftrace_notrace files and then opens them with read&write permissions, the previous setup will be removed.

Currently:

 # echo 'sys_open sys_close' > set_ftrace_filter
 # cat set_ftrace_filter
 sys_open
 sys_close
Quoted 6 lines

After your patch:

 # echo 'sys_open sys_close' > set_ftrace_filter
 # cat set_ftrace_filter
 sys_close
Quoted 6 lines

oops, sry I missed this..

Following patch adds new FTRACE_ITER_RESET flag, as the decision needs to be taken in "open" and applied in "write". I'm not sure whats the policy for adding new flags, but it looks ok to me.

I have no strong opinion whether to do the reset in "open" or in first "write".

All said, I think this is cleaner, without introducing a new flag:

(quoted diff) +3 -0
@@ -2260,6 +2256,9 @@ ftrace_regex_write(struct file *file, const char __user *ubuf,
 		return 0;
 
 	mutex_lock(&ftrace_regex_lock);
+	if (file->f_pos == 0 &&
+	    (file->f_mode & FMODE_WRITE) && !(file->f_flags & O_APPEND))
+		ftrace_filter_reset(enable);
 
Quoted 7 lines

Review ftrace_regex_write(struct file *file, const char __user *ubuf, · line 2264

Quoted 2 lines
(quoted diff) +0 -0
 	if (file->f_mode & FMODE_READ) {
 		struct seq_file *m = file->private_data;

Yeah, that avoids this need of creating a new flag. Looks like a good solution.

yes, but this wont work unless the *ppos value is updated instead of the file->f_pos, because of:

SYSCALL_DEFINE3(write, unsigned int, fd, const char __user *, buf,
size_t, count)

  {
	[snip]
                loff_t pos = file_pos_read(file);
                ret = vfs_write(file, buf, count, &pos);
                file_pos_write(file, pos);
	[snip]

Together with this fix I included fix for FTRACE_ITER_CONT flag, since it does not seem to work correctly.
As far as I understand the reason for FTRACE_ITER_CONT flag is when there's one regexp. in more write calls. I made a program to test it.

[snip]
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>

int main(int argc, char **argv)

{
        int fd, i;
        char *file = argv[1];

        if (-1 == (fd = open(file, O_WRONLY))) {
                perror("open failed");
                return -1;
        }

        for(i = 0; i < (argc - 2); i++) {
                int len = strlen(argv[2+i]);
                int cnt, off = 0;

                while(len) {
                        cnt = write(fd, argv[2+i] + off, len);
                        len -= cnt;
                        off += cnt;
                }
        }

        close(fd);
        return 0;
}

[snip]

I got following behaviour on the current tip master:

sh-4.0# /tst ./set_ftrace_filter sys_o "pen "
sh-4.0# cat ./set_ftrace_filter
#### all functions enabled ####

wbr,
jirka

---

kernel/trace/ftrace.c +15 -10
index 24e3ff5..041a327 100644
@@ -1616,10 +1616,6 @@ ftrace_regex_open(struct inode *inode, struct file *file, int enable)
 		return -ENOMEM;
 
 	mutex_lock(&ftrace_regex_lock);
-	if ((file->f_mode & FMODE_WRITE) &&
-	    !(file->f_flags & O_APPEND))
-		ftrace_filter_reset(enable);
-
 	if (file->f_mode & FMODE_READ) {
 		iter->pg = ftrace_pages_start;
 		iter->flags = enable ? FTRACE_ITER_FILTER :
@@ -2225,6 +2221,10 @@ ftrace_regex_write(struct file *file, const char __user *ubuf,
 	if (!*ppos) {
 		iter->flags &= ~FTRACE_ITER_CONT;
 		iter->buffer_idx = 0;
+
+		if ((file->f_mode & FMODE_WRITE) &&
+		    !(file->f_flags & O_APPEND))
+			ftrace_filter_reset(enable);
 	}
 
 	ret = get_user(ch, ubuf++);
@@ -2233,7 +2233,11 @@ ftrace_regex_write(struct file *file, const char __user *ubuf,
 	read++;
 	cnt--;
 
-	if (!(iter->flags & ~FTRACE_ITER_CONT)) {
+	/*
+	 * If the parses havent finished with the last write,
+	 * continue reading the user input without skipping spaces.
+	 */
+	if (!(iter->flags & FTRACE_ITER_CONT)) {
 		/* skip white space */
 		while (cnt && isspace(ch)) {
 			ret = get_user(ch, ubuf++);
@@ -2243,8 +2247,9 @@ ftrace_regex_write(struct file *file, const char __user *ubuf,
 			cnt--;
 		}
 
+		/* only spaces were written */
 		if (isspace(ch)) {
-			file->f_pos += read;
+			*ppos += read;
 			ret = read;
 			goto out;
 		}
@@ -2273,12 +2278,12 @@ ftrace_regex_write(struct file *file, const char __user *ubuf,
 		if (ret)
 			goto out;
 		iter->buffer_idx = 0;
-	} else
+	} else {
 		iter->flags |= FTRACE_ITER_CONT;
+		iter->buffer[iter->buffer_idx++] = ch;
+	}
 
-
-	file->f_pos += read;
-
+	*ppos += read;
 	ret = read;
  out:
 	mutex_unlock(&ftrace_regex_lock);
maintainer
#6 lore raw
In reply to Jiri Olsa
Quoted 2 lines · Jiri Olsa

If user setup set_ftrace_filter/set_ftrace_notrace files and then opens them with read&write permissions, the previous setup will be removed.

This is exactly what it was suppose to do.

man fopen:

w+ Open for reading and writing. The file is created if it does
not exist, otherwise it is truncated. The stream is positioned at the beginning of the file.

Which means that if you open a file for "w+" it will truncate it. Hence, you will remove all previous settings.

What you want is:

a+ Open for reading and appending (writing at end of file). The
file is created if it does not exist. The initial file position for reading is at the beginning of the file, but output is always appended to the end of the file.

Change the belowe code from "w+" to "a+" and you get your expected result.

Thus, the current code is correct.

-- Steve

Quoted 67 lines

Tested with following program:

[snip]
#include <stdio.h>
#include <string.h>

int main(int argc, char **argv)

{
	FILE *f;
	char *mode = "w+";
	char *file = argv[1];

	if (argc == 3)
		mode = argv[2];

	if (NULL == (f = fopen(file, mode))) {
		perror("fopen failed");
		return -1;
	}

	while(!feof(f)) {
#define BUFLEN 100
		char buf[BUFLEN];
		memset(buf, 0, BUFLEN);
		fgets(buf, BUFLEN, f);
		printf(buf);
	}

	fclose(f);
	return 0;
}

[snip]

wbr,
jirka

---

kernel/trace/ftrace.c +3 -4
index 4521c77..11394bc 100644
@@ -1661,10 +1661,6 @@ ftrace_regex_open(struct inode *inode, struct file *file, int enable)
 		return -ENOMEM;
 
 	mutex_lock(&ftrace_regex_lock);
-	if ((file->f_mode & FMODE_WRITE) &&
-	    !(file->f_flags & O_APPEND))
-		ftrace_filter_reset(enable);
-
 	if (file->f_mode & FMODE_READ) {
 		iter->pg = ftrace_pages_start;
 		iter->flags = enable ? FTRACE_ITER_FILTER :
@@ -2260,6 +2256,9 @@ ftrace_regex_write(struct file *file, const char __user *ubuf,
 		return 0;
 
 	mutex_lock(&ftrace_regex_lock);
+	if ((file->f_mode & FMODE_WRITE) &&
+	    !(file->f_flags & O_APPEND))
+		ftrace_filter_reset(enable);
 
 	if (file->f_mode & FMODE_READ) {
 		struct seq_file *m = file->private_data;
author
#7 lore raw
Quoted 17 lines · Steven Rostedt
Quoted 2 lines · Jiri Olsa

If user setup set_ftrace_filter/set_ftrace_notrace files and then opens them with read&write permissions, the previous setup will be removed.

This is exactly what it was suppose to do.

man fopen:

w+ Open for reading and writing. The file is created if it does
not exist, otherwise it is truncated. The stream is positioned at the beginning of the file.

Which means that if you open a file for "w+" it will truncate it. Hence, you will remove all previous settings.

Quoted 8 lines

What you want is:

a+ Open for reading and appending (writing at end of file). The
file is created if it does not exist. The initial file position for reading is at the beginning of the file, but output is always appended to the end of the file.

Change the belowe code from "w+" to "a+" and you get your expected result.

My point was that if you open set_ftrace_filter/set_ftrace_notrace with just O_RDWR perm. and will use the file just for reading, the filter will reset.

You're right about the "w+", there's the O_CREAT|O_TRUNC, sry I missed that.. ;) Anyway with "r+" you'll get O_RDWR perm. only, showing the issue:

sh-4.0# echo "sys_open sys_write" > ./set_ftrace_filter sh-4.0# cat ./set_ftrace_filter
sys_open
sys_write
sh-4.0# /ft ./set_ftrace_filter r+
#### all functions enabled ####
sh-4.0# cat ./set_ftrace_filter
#### all functions enabled ####
sh-4.0#

wbr,
jirka

Quoted 74 lines

Thus, the current code is correct.

-- Steve

Quoted 67 lines

Tested with following program:

[snip]
#include <stdio.h>
#include <string.h>

int main(int argc, char **argv)

{
	FILE *f;
	char *mode = "w+";
	char *file = argv[1];

	if (argc == 3)
		mode = argv[2];

	if (NULL == (f = fopen(file, mode))) {
		perror("fopen failed");
		return -1;
	}

	while(!feof(f)) {
#define BUFLEN 100
		char buf[BUFLEN];
		memset(buf, 0, BUFLEN);
		fgets(buf, BUFLEN, f);
		printf(buf);
	}

	fclose(f);
	return 0;
}

[snip]

wbr,
jirka

---

kernel/trace/ftrace.c +3 -4
index 4521c77..11394bc 100644
@@ -1661,10 +1661,6 @@ ftrace_regex_open(struct inode *inode, struct file *file, int enable)
 		return -ENOMEM;
 
 	mutex_lock(&ftrace_regex_lock);
-	if ((file->f_mode & FMODE_WRITE) &&
-	    !(file->f_flags & O_APPEND))
-		ftrace_filter_reset(enable);
-
 	if (file->f_mode & FMODE_READ) {
 		iter->pg = ftrace_pages_start;
 		iter->flags = enable ? FTRACE_ITER_FILTER :
@@ -2260,6 +2256,9 @@ ftrace_regex_write(struct file *file, const char __user *ubuf,
 		return 0;
 
 	mutex_lock(&ftrace_regex_lock);
+	if ((file->f_mode & FMODE_WRITE) &&
+	    !(file->f_flags & O_APPEND))
+		ftrace_filter_reset(enable);
 
 	if (file->f_mode & FMODE_READ) {
 		struct seq_file *m = file->private_data;
maintainer
#8 lore raw
Quoted 25 lines · Jiri Olsa
Quoted 24 lines · Steven Rostedt
Quoted 2 lines · Jiri Olsa

If user setup set_ftrace_filter/set_ftrace_notrace files and then opens them with read&write permissions, the previous setup will be removed.

This is exactly what it was suppose to do.

man fopen:

w+ Open for reading and writing. The file is created if it does
not exist, otherwise it is truncated. The stream is positioned at the beginning of the file.

Which means that if you open a file for "w+" it will truncate it. Hence, you will remove all previous settings.

What you want is:

a+ Open for reading and appending (writing at end of file). The
file is created if it does not exist. The initial file position for reading is at the beginning of the file, but output is always appended to the end of the file.

Quoted 7 lines
Quoted 1 line

Change the belowe code from "w+" to "a+" and you get your expected result.

My point was that if you open set_ftrace_filter/set_ftrace_notrace with just O_RDWR perm. and will use the file just for reading, the filter will reset.

You're right about the "w+", there's the O_CREAT|O_TRUNC, sry I missed that.. ;) Anyway with "r+" you'll get O_RDWR perm. only, showing the issue:

Ah, then the fix would be to check against O_TRUNC.

-- Steve