* 2.6.10-mm3: lseek broken on /proc/self/maps
@ 2005-01-14 21:23 Jeremy Fitzhardinge
2005-01-15 0:46 ` Prasanna Meda
0 siblings, 1 reply; 3+ messages in thread
From: Jeremy Fitzhardinge @ 2005-01-14 21:23 UTC (permalink / raw)
To: Prasanna Meda; +Cc: Andrew Morton, linux-kernel
[-- Attachment #1: Type: text/plain, Size: 1250 bytes --]
lseek doesn't seem to have any effect on /proc/<pid>/maps. It should
either work as expected, or fail.
I've attached a test program which uses a doubling buffer policy to try
to read the whole buffer. If it runs out of buffer, it simply allocates
a new one, lseeks back to the beginning of the buffer, and tries again.
However, the lseek seems to have no effect, because the next read
continues from where the previous one (before the lseek) left off.
Re-opening the file between each attempt works as expected.
$ tm &
$ ./readmap $!
b7dea000-b7deb000 r-xp b7dea000 00:00 0
b7deb000-b7dec000 ---p b7deb000 00:00 0
b7dec000-b7ded000 r-xp b7dec000 00:00 0
b7ded000-b7dee000 ---p b7ded000 00:00 0
b7dee000-b7def000 r-xp b7dee000 00:00 0
b7def000-b7df0000 ---p b7def000 00:00 0
b7df0000-b7df1000 r-xp b7df0000 00:00 0
[...]
$ gcc -o readmap -DREOPEN readmap.c
$ ./readmap $!
08048000-08049000 r-xp 00000000 03:07 3868140 /home/jeremy/tm
08049000-0804a000 rwxp 00000000 03:07 3868140 /home/jeremy/tm
b7ae6000-b7ae7000 r-xp b7ae6000 00:00 0
b7ae7000-b7ae8000 ---p b7ae7000 00:00 0
b7ae8000-b7ae9000 r-xp b7ae8000 00:00 0
b7ae9000-b7aea000 ---p b7ae9000 00:00 0
b7aea000-b7aeb000 r-xp b7aea000 00:00 0
b7aeb000-b7aec000 ---p b7aeb000 00:00 0
J
[-- Attachment #2: readmap.c --]
[-- Type: text/x-csrc, Size: 1051 bytes --]
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#ifndef REOPEN
#define REOPEN 0
#endif
int main(int argc, char **argv)
{
int bufsize = 1024;
char *buf = NULL;
int bufused;
int got;
int fd = -1;
char path[100];
if (argc <= 1)
strcpy(path, "/proc/self/maps");
else
sprintf(path, "/proc/%s/maps", argv[1]);
again:
if (fd == -1)
fd = open(path, O_RDONLY);
if (fd == -1) {
perror("open");
exit(1);
}
if (lseek(fd, 0, SEEK_SET) == -1) {
perror("lseek");
exit(1);
}
bufused = 0;
if (buf == NULL)
buf = malloc(bufsize);
do {
int want = bufsize - bufused;
got = read(fd, &buf[bufused],
want > 4000 ? 4000 : want); /* work around other bug */
if (got < 0) {
perror("read");
exit(1);
}
bufused += got;
} while(bufused < bufsize && got != 0);
if (bufused == bufsize) {
free(buf);
buf = NULL;
bufsize *= 2;
if (REOPEN) {
/* reopen */
close(fd);
fd = -1;
}
goto again;
}
close(fd);
write(1, buf, bufused);
return 0;
}
[-- Attachment #3: tm.c --]
[-- Type: text/x-csrc, Size: 257 bytes --]
#include <sys/mman.h>
#include <unistd.h>
int main()
{
int i;
char *m = mmap(0, 1000*4096, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
char *p = m;
for(i = 0; i < 1000; i+=2) {
mprotect(p, 4096, PROT_READ);
p += 8192;
}
pause();
return 0;
}
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: 2.6.10-mm3: lseek broken on /proc/self/maps
2005-01-14 21:23 2.6.10-mm3: lseek broken on /proc/self/maps Jeremy Fitzhardinge
@ 2005-01-15 0:46 ` Prasanna Meda
2005-01-15 1:05 ` Jeremy Fitzhardinge
0 siblings, 1 reply; 3+ messages in thread
From: Prasanna Meda @ 2005-01-15 0:46 UTC (permalink / raw)
To: Jeremy Fitzhardinge; +Cc: Andrew Morton, linux-kernel
[-- Attachment #1: Type: text/plain, Size: 725 bytes --]
Jeremy Fitzhardinge wrote:
> lseek doesn't seem to have any effect on /proc/<pid>/maps. It should
> either work as expected, or fail.
>
An oversight, f_version is not reset by seq_lseek as done by generic_lseek.
Please try the attached patch.
>
> I've attached a test program which uses a doubling buffer policy to try
> to read the whole buffer. If it runs out of buffer, it simply allocates
> a new one, lseeks back to the beginning of the buffer, and tries again.
> However, the lseek seems to have no effect, because the next read
> continues from where the previous one (before the lseek) left off.
>
> Re-opening the file between each attempt works as expected.
>
Thanks a lot for testing all the corner cases.
[-- Attachment #2: seq_version.patch --]
[-- Type: text/plain, Size: 1180 bytes --]
I missed the point that seq_lseek did not reset the f_version,
like generic_lseek did, until Jeremy proved with an example.
Signed-off-by: Prasanna Meda <pmeda@akamai.com>
--- fs/seq_file.c Fri Jan 14 23:51:27 2005
+++ fs/seq_file.c Fri Jan 14 23:51:59 2005
@@ -117,6 +117,7 @@ ssize_t seq_read(struct file *file, char
if (!m->buf)
goto Enomem;
m->count = 0;
+ m->version = 0;
}
m->op->stop(m, p);
m->count = 0;
@@ -173,6 +174,7 @@ static int traverse(struct seq_file *m,
int error = 0;
void *p;
+ m->version = 0;
m->index = 0;
m->count = m->from = 0;
if (!offset)
@@ -227,6 +229,7 @@ loff_t seq_lseek(struct file *file, loff
long long retval = -EINVAL;
down(&m->sem);
+ m->version = file->f_version;
switch (origin) {
case 1:
offset += file->f_pos;
@@ -240,6 +243,7 @@ loff_t seq_lseek(struct file *file, loff
if (retval) {
/* with extreme prejudice... */
file->f_pos = 0;
+ m->version = 0;
m->index = 0;
m->count = 0;
} else {
@@ -248,6 +252,7 @@ loff_t seq_lseek(struct file *file, loff
}
}
up(&m->sem);
+ file->f_version = m->version;
return retval;
}
EXPORT_SYMBOL(seq_lseek);
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: 2.6.10-mm3: lseek broken on /proc/self/maps
2005-01-15 0:46 ` Prasanna Meda
@ 2005-01-15 1:05 ` Jeremy Fitzhardinge
0 siblings, 0 replies; 3+ messages in thread
From: Jeremy Fitzhardinge @ 2005-01-15 1:05 UTC (permalink / raw)
To: Prasanna Meda; +Cc: Andrew Morton, linux-kernel
On Fri, 2005-01-14 at 16:46 -0800, Prasanna Meda wrote:
> Thanks a lot for testing all the corner cases.
I'm actually trying to find bugs in Valgrind's memory map tracking code,
but so far all the problems have been in /proc/self/maps (which I had
been treating as a golden reference...).
I'll give this a go shortly.
BTW, your last patch fixed the >4k read problem, so I'm hoping this will
be the last one.
J
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2005-01-15 1:17 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-01-14 21:23 2.6.10-mm3: lseek broken on /proc/self/maps Jeremy Fitzhardinge
2005-01-15 0:46 ` Prasanna Meda
2005-01-15 1:05 ` Jeremy Fitzhardinge
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox