How to open/read a file during a function call in the networking kernel codes

How to open/read a file during a function call in the networking kernel codes

Post by tlin » Wed, 14 Feb 2001 09:52:45



I am trying to open and read a data file when a function in the
networking kernel code, which is named "ip_rcv()" (Linux 2.2.16 kernel),
is called.

I tried many ways, but all failed when I wanted to read the file.
The system crashed and some of the output information are:
"Scheduling in interrupt
Unable to handle kernel NULL pointer dereference at virtual address...
...
Aiee, killing ...
Kernel panic: Attempted to kill ...
In swapper task_not syncy"

I checked that the pointer of file structure and buffers are all not null.
I also switch to the user space by get_fs() and set_fs() (but not sure
whether
this is correct.)

Could anyone give me any suggestions? Thanks a lot!

 Tom

 
 
 

How to open/read a file during a function call in the networking kernel codes

Post by Kasper Dupon » Wed, 14 Feb 2001 19:00:09



> I am trying to open and read a data file when a function in the
> networking kernel code, which is named "ip_rcv()" (Linux 2.2.16 kernel),
> is called.

> I tried many ways, but all failed when I wanted to read the file.
> The system crashed and some of the output information are:
> "Scheduling in interrupt
> Unable to handle kernel NULL pointer dereference at virtual address...
> ...
> Aiee, killing ...
> Kernel panic: Attempted to kill ...
> In swapper task_not syncy"

> I checked that the pointer of file structure and buffers are all not null.
> I also switch to the user space by get_fs() and set_fs() (but not sure
> whether
> this is correct.)

> Could anyone give me any suggestions? Thanks a lot!

>  Tom

The problem is that the function is called inside an
interrupt. All of the filesystem assumes that it is
only being called from a process context.

You have to move the file access code to a process
context. You can create a kernel thread with the
kernel_thread function. The thread can either be
created at bootup or within the interrupt. You then
have to ensure that your kernel thread gets a high
priority so it is scheduled as soon as the interrupts
return. The interrupt can change the priority of your
kernel thread, I don't know what is the best way to
do it.

--
Kasper Dupont

 
 
 

How to open/read a file during a function call in the networking kernel codes

Post by tlin » Thu, 15 Feb 2001 02:11:44


Thanks for your reply!

Could you tell me whether there is a way to search functions that
we can use in kernel such as the "kernel_thread"? Or we have to go to
the kernel source code and check those files one by one? Thanks!!



> > I am trying to open and read a data file when a function in the
> > networking kernel code, which is named "ip_rcv()" (Linux 2.2.16 kernel),
> > is called.

> > I tried many ways, but all failed when I wanted to read the file.
> > The system crashed and some of the output information are:
> > "Scheduling in interrupt
> > Unable to handle kernel NULL pointer dereference at virtual address...
> > ...
> > Aiee, killing ...
> > Kernel panic: Attempted to kill ...
> > In swapper task_not syncy"

> > I checked that the pointer of file structure and buffers are all not
null.
> > I also switch to the user space by get_fs() and set_fs() (but not sure
> > whether
> > this is correct.)

> > Could anyone give me any suggestions? Thanks a lot!

> >  Tom

> The problem is that the function is called inside an
> interrupt. All of the filesystem assumes that it is
> only being called from a process context.

> You have to move the file access code to a process
> context. You can create a kernel thread with the
> kernel_thread function. The thread can either be
> created at bootup or within the interrupt. You then
> have to ensure that your kernel thread gets a high
> priority so it is scheduled as soon as the interrupts
> return. The interrupt can change the priority of your
> kernel thread, I don't know what is the best way to
> do it.

> --
> Kasper Dupont

 
 
 

How to open/read a file during a function call in the networking kernel codes

Post by Gene Soudlenko » Thu, 15 Feb 2001 04:54:46


To operatre on a file from the kernel space, direct calls to f_op members
can be used:

set_fs(KERNEL_DS);
error=file->f_op->read(file,buffer,sizeof(int),&offset);
set_fs(USER_DS);
.....

to get the proper file structure either way can be used:

file=fget(fd); where fd is file descriptor

or

file=filp_open(name,O_RDONLY,0);
,.....

Gene


> I am trying to open and read a data file when a function in the
> networking kernel code, which is named "ip_rcv()" (Linux 2.2.16 kernel),
> is called.

> I tried many ways, but all failed when I wanted to read the file.
> The system crashed and some of the output information are:
> "Scheduling in interrupt
> Unable to handle kernel NULL pointer dereference at virtual address...
> ...
> Aiee, killing ...
> Kernel panic: Attempted to kill ...
> In swapper task_not syncy"

> I checked that the pointer of file structure and buffers are all not null.
> I also switch to the user space by get_fs() and set_fs() (but not sure
> whether
> this is correct.)

> Could anyone give me any suggestions? Thanks a lot!

>  Tom

 
 
 

How to open/read a file during a function call in the networking kernel codes

Post by tlin » Thu, 15 Feb 2001 14:24:41


Thanks!

For the f_op->read() function:
What is the value of the offset, just use a 0?
And why use sizeof(int) as the 3 rd paramemter?
Thanks!


> To operatre on a file from the kernel space, direct calls to f_op members
> can be used:

> set_fs(KERNEL_DS);
> error=file->f_op->read(file,buffer,sizeof(int),&offset);
> set_fs(USER_DS);
> .....

> to get the proper file structure either way can be used:

> file=fget(fd); where fd is file descriptor

> or

> file=filp_open(name,O_RDONLY,0);
> ,.....

> Gene




- Show quoted text -

Quote:> > I am trying to open and read a data file when a function in the
> > networking kernel code, which is named "ip_rcv()" (Linux 2.2.16 kernel),
> > is called.

> > I tried many ways, but all failed when I wanted to read the file.
> > The system crashed and some of the output information are:
> > "Scheduling in interrupt
> > Unable to handle kernel NULL pointer dereference at virtual address...
> > ...
> > Aiee, killing ...
> > Kernel panic: Attempted to kill ...
> > In swapper task_not syncy"

> > I checked that the pointer of file structure and buffers are all not
null.
> > I also switch to the user space by get_fs() and set_fs() (but not sure
> > whether
> > this is correct.)

> > Could anyone give me any suggestions? Thanks a lot!

> >  Tom

 
 
 

How to open/read a file during a function call in the networking kernel codes

Post by Gene Soudlenko » Thu, 15 Feb 2001 12:53:29


sizeof(int) was an example - "write so many bytes".

offset is file offset after writing.

Gene


> Thanks!

> For the f_op->read() function:
> What is the value of the offset, just use a 0?
> And why use sizeof(int) as the 3 rd paramemter?
> Thanks!



> > To operatre on a file from the kernel space, direct calls to f_op
members
> > can be used:

> > set_fs(KERNEL_DS);
> > error=file->f_op->read(file,buffer,sizeof(int),&offset);
> > set_fs(USER_DS);
> > .....

> > to get the proper file structure either way can be used:

> > file=fget(fd); where fd is file descriptor

> > or

> > file=filp_open(name,O_RDONLY,0);
> > ,.....

> > Gene



> > > I am trying to open and read a data file when a function in the
> > > networking kernel code, which is named "ip_rcv()" (Linux 2.2.16
kernel),
> > > is called.

> > > I tried many ways, but all failed when I wanted to read the file.
> > > The system crashed and some of the output information are:
> > > "Scheduling in interrupt
> > > Unable to handle kernel NULL pointer dereference at virtual address...
> > > ...
> > > Aiee, killing ...
> > > Kernel panic: Attempted to kill ...
> > > In swapper task_not syncy"

> > > I checked that the pointer of file structure and buffers are all not
> null.
> > > I also switch to the user space by get_fs() and set_fs() (but not sure
> > > whether
> > > this is correct.)

> > > Could anyone give me any suggestions? Thanks a lot!

> > >  Tom

 
 
 

How to open/read a file during a function call in the networking kernel codes

Post by tlin » Thu, 15 Feb 2001 17:33:43


Here is my code, but it doesn't work (system crash):
in net/ipv4/ip_input.c: ip_rcv()
"
f=filp_open("/root/test", O_RDONLY, 0);
set_fs(KERNEL_DS);
buffer=(char *) kmalloc(255*sizeof(char), 0);
error=f->f_op->read(f, buffer, 255*sizeof(char), &offset);
kfree(buffer);
set_fs(USER_DS);
filp_close(f, NULL);
"
Can you help to have a look at it? Thanks a lot!


> To operatre on a file from the kernel space, direct calls to f_op members
> can be used:

> set_fs(KERNEL_DS);
> error=file->f_op->read(file,buffer,sizeof(int),&offset);
> set_fs(USER_DS);
> .....

> to get the proper file structure either way can be used:

> file=fget(fd); where fd is file descriptor

> or

> file=filp_open(name,O_RDONLY,0);
> ,.....

> Gene




- Show quoted text -

Quote:> > I am trying to open and read a data file when a function in the
> > networking kernel code, which is named "ip_rcv()" (Linux 2.2.16 kernel),
> > is called.

> > I tried many ways, but all failed when I wanted to read the file.
> > The system crashed and some of the output information are:
> > "Scheduling in interrupt
> > Unable to handle kernel NULL pointer dereference at virtual address...
> > ...
> > Aiee, killing ...
> > Kernel panic: Attempted to kill ...
> > In swapper task_not syncy"

> > I checked that the pointer of file structure and buffers are all not
null.
> > I also switch to the user space by get_fs() and set_fs() (but not sure
> > whether
> > this is correct.)

> > Could anyone give me any suggestions? Thanks a lot!

> >  Tom

 
 
 

How to open/read a file during a function call in the networking kernel codes

Post by Kasper Dupon » Thu, 15 Feb 2001 20:45:30



> Here is my code, but it doesn't work (system crash):
> in net/ipv4/ip_input.c: ip_rcv()
> "
> f=filp_open("/root/test", O_RDONLY, 0);
> set_fs(KERNEL_DS);
> buffer=(char *) kmalloc(255*sizeof(char), 0);
> error=f->f_op->read(f, buffer, 255*sizeof(char), &offset);
> kfree(buffer);
> set_fs(USER_DS);
> filp_close(f, NULL);
> "
> Can you help to have a look at it? Thanks a lot!

[...]

That code is probably correct. When the system
crashes it is not because of a problem with that
particular code, it is because it is called in a
context where filesystem access is not allowed.

The first bug message you get from the kernel
explains what happens: "Scheduling in interrupt".
Filesystem may have to schedule, or do other
things which is not possible inside interrupts.

Try the above piece of code in a process context,
eg. try the code inside a module initialization.

--
Kasper Dupont

 
 
 

How to open/read a file during a function call in the networking kernel codes

Post by Kasper Dupon » Thu, 15 Feb 2001 20:47:42



> Thanks for your reply!

> Could you tell me whether there is a way to search functions that
> we can use in kernel such as the "kernel_thread"? Or we have to go to
> the kernel source code and check those files one by one? Thanks!!

[...]

Personally I look in the source code, there might
exist documentation out there somewhere, I haven't
seen it though.

--
Kasper Dupont

 
 
 

How to open/read a file during a function call in the networking kernel codes

Post by Gene Soudlenko » Fri, 16 Feb 2001 04:45:26


First off, it is not a good idea to do something like that in an interrupt
handler - you just do not have any user context, not mentioning other things
(synchronization routines used, kernel locking, performance, etc.)

Better move this code off the handler.

Gene


> Here is my code, but it doesn't work (system crash):
> in net/ipv4/ip_input.c: ip_rcv()
> "
> f=filp_open("/root/test", O_RDONLY, 0);
> set_fs(KERNEL_DS);
> buffer=(char *) kmalloc(255*sizeof(char), 0);
> error=f->f_op->read(f, buffer, 255*sizeof(char), &offset);
> kfree(buffer);
> set_fs(USER_DS);
> filp_close(f, NULL);
> "
> Can you help to have a look at it? Thanks a lot!



> > To operatre on a file from the kernel space, direct calls to f_op
members
> > can be used:

> > set_fs(KERNEL_DS);
> > error=file->f_op->read(file,buffer,sizeof(int),&offset);
> > set_fs(USER_DS);
> > .....

> > to get the proper file structure either way can be used:

> > file=fget(fd); where fd is file descriptor

> > or

> > file=filp_open(name,O_RDONLY,0);
> > ,.....

> > Gene



> > > I am trying to open and read a data file when a function in the
> > > networking kernel code, which is named "ip_rcv()" (Linux 2.2.16
kernel),
> > > is called.

> > > I tried many ways, but all failed when I wanted to read the file.
> > > The system crashed and some of the output information are:
> > > "Scheduling in interrupt
> > > Unable to handle kernel NULL pointer dereference at virtual address...
> > > ...
> > > Aiee, killing ...
> > > Kernel panic: Attempted to kill ...
> > > In swapper task_not syncy"

> > > I checked that the pointer of file structure and buffers are all not
> null.
> > > I also switch to the user space by get_fs() and set_fs() (but not sure
> > > whether
> > > this is correct.)

> > > Could anyone give me any suggestions? Thanks a lot!

> > >  Tom

 
 
 

How to open/read a file during a function call in the networking kernel codes

Post by tlin » Fri, 16 Feb 2001 09:35:27


But if I lock the kernel when these codes are called, the system will still
crash.
Do you mean this may not be possible to do this in the interrupt handler?
Thanks!


> First off, it is not a good idea to do something like that in an interrupt
> handler - you just do not have any user context, not mentioning other
things
> (synchronization routines used, kernel locking, performance, etc.)

> Better move this code off the handler.

> Gene



> > Here is my code, but it doesn't work (system crash):
> > in net/ipv4/ip_input.c: ip_rcv()
> > "
> > f=filp_open("/root/test", O_RDONLY, 0);
> > set_fs(KERNEL_DS);
> > buffer=(char *) kmalloc(255*sizeof(char), 0);
> > error=f->f_op->read(f, buffer, 255*sizeof(char), &offset);
> > kfree(buffer);
> > set_fs(USER_DS);
> > filp_close(f, NULL);
> > "
> > Can you help to have a look at it? Thanks a lot!



> > > To operatre on a file from the kernel space, direct calls to f_op
> members
> > > can be used:

> > > set_fs(KERNEL_DS);
> > > error=file->f_op->read(file,buffer,sizeof(int),&offset);
> > > set_fs(USER_DS);
> > > .....

> > > to get the proper file structure either way can be used:

> > > file=fget(fd); where fd is file descriptor

> > > or

> > > file=filp_open(name,O_RDONLY,0);
> > > ,.....

> > > Gene



> > > > I am trying to open and read a data file when a function in the
> > > > networking kernel code, which is named "ip_rcv()" (Linux 2.2.16
> kernel),
> > > > is called.

> > > > I tried many ways, but all failed when I wanted to read the file.
> > > > The system crashed and some of the output information are:
> > > > "Scheduling in interrupt
> > > > Unable to handle kernel NULL pointer dereference at virtual
address...
> > > > ...
> > > > Aiee, killing ...
> > > > Kernel panic: Attempted to kill ...
> > > > In swapper task_not syncy"

> > > > I checked that the pointer of file structure and buffers are all not
> > null.
> > > > I also switch to the user space by get_fs() and set_fs() (but not
sure
> > > > whether
> > > > this is correct.)

> > > > Could anyone give me any suggestions? Thanks a lot!

> > > >  Tom

 
 
 

How to open/read a file during a function call in the networking kernel codes

Post by Gene Soudlenko » Fri, 16 Feb 2001 10:14:33


Can you tell me where it crashes?

Gene


> But if I lock the kernel when these codes are called, the system will
still
> crash.
> Do you mean this may not be possible to do this in the interrupt handler?
> Thanks!



> > First off, it is not a good idea to do something like that in an
interrupt
> > handler - you just do not have any user context, not mentioning other
> things
> > (synchronization routines used, kernel locking, performance, etc.)

> > Better move this code off the handler.

> > Gene


> > > Here is my code, but it doesn't work (system crash):
> > > in net/ipv4/ip_input.c: ip_rcv()
> > > "
> > > f=filp_open("/root/test", O_RDONLY, 0);
> > > set_fs(KERNEL_DS);
> > > buffer=(char *) kmalloc(255*sizeof(char), 0);
> > > error=f->f_op->read(f, buffer, 255*sizeof(char), &offset);
> > > kfree(buffer);
> > > set_fs(USER_DS);
> > > filp_close(f, NULL);
> > > "
> > > Can you help to have a look at it? Thanks a lot!



> > > > To operatre on a file from the kernel space, direct calls to f_op
> > members
> > > > can be used:

> > > > set_fs(KERNEL_DS);
> > > > error=file->f_op->read(file,buffer,sizeof(int),&offset);
> > > > set_fs(USER_DS);
> > > > .....

> > > > to get the proper file structure either way can be used:

> > > > file=fget(fd); where fd is file descriptor

> > > > or

> > > > file=filp_open(name,O_RDONLY,0);
> > > > ,.....

> > > > Gene



> > > > > I am trying to open and read a data file when a function in the
> > > > > networking kernel code, which is named "ip_rcv()" (Linux 2.2.16
> > kernel),
> > > > > is called.

> > > > > I tried many ways, but all failed when I wanted to read the file.
> > > > > The system crashed and some of the output information are:
> > > > > "Scheduling in interrupt
> > > > > Unable to handle kernel NULL pointer dereference at virtual
> address...
> > > > > ...
> > > > > Aiee, killing ...
> > > > > Kernel panic: Attempted to kill ...
> > > > > In swapper task_not syncy"

> > > > > I checked that the pointer of file structure and buffers are all
not
> > > null.
> > > > > I also switch to the user space by get_fs() and set_fs() (but not
> sure
> > > > > whether
> > > > > this is correct.)

> > > > > Could anyone give me any suggestions? Thanks a lot!

> > > > >  Tom

 
 
 

How to open/read a file during a function call in the networking kernel codes

Post by tlin » Fri, 16 Feb 2001 15:56:07


I put this code in the ip_input.c: ip_rcv().
After ip_rcv() defines some variables, I will check whether or not we need
to open/read a file.
When a packet come from eth0, and ip_rcv() is called, the code will go to
file branch. Once
it goes into this branch, the system will crash.


> Can you tell me where it crashes?

> Gene



> > But if I lock the kernel when these codes are called, the system will
> still
> > crash.
> > Do you mean this may not be possible to do this in the interrupt
handler?
> > Thanks!



> > > First off, it is not a good idea to do something like that in an
> interrupt
> > > handler - you just do not have any user context, not mentioning other
> > things
> > > (synchronization routines used, kernel locking, performance, etc.)

> > > Better move this code off the handler.

> > > Gene


> > > > Here is my code, but it doesn't work (system crash):
> > > > in net/ipv4/ip_input.c: ip_rcv()
> > > > "
> > > > f=filp_open("/root/test", O_RDONLY, 0);
> > > > set_fs(KERNEL_DS);
> > > > buffer=(char *) kmalloc(255*sizeof(char), 0);
> > > > error=f->f_op->read(f, buffer, 255*sizeof(char), &offset);
> > > > kfree(buffer);
> > > > set_fs(USER_DS);
> > > > filp_close(f, NULL);
> > > > "
> > > > Can you help to have a look at it? Thanks a lot!



> > > > > To operatre on a file from the kernel space, direct calls to f_op
> > > members
> > > > > can be used:

> > > > > set_fs(KERNEL_DS);
> > > > > error=file->f_op->read(file,buffer,sizeof(int),&offset);
> > > > > set_fs(USER_DS);
> > > > > .....

> > > > > to get the proper file structure either way can be used:

> > > > > file=fget(fd); where fd is file descriptor

> > > > > or

> > > > > file=filp_open(name,O_RDONLY,0);
> > > > > ,.....

> > > > > Gene



> > > > > > I am trying to open and read a data file when a function in the
> > > > > > networking kernel code, which is named "ip_rcv()" (Linux 2.2.16
> > > kernel),
> > > > > > is called.

> > > > > > I tried many ways, but all failed when I wanted to read the
file.
> > > > > > The system crashed and some of the output information are:
> > > > > > "Scheduling in interrupt
> > > > > > Unable to handle kernel NULL pointer dereference at virtual
> > address...
> > > > > > ...
> > > > > > Aiee, killing ...
> > > > > > Kernel panic: Attempted to kill ...
> > > > > > In swapper task_not syncy"

> > > > > > I checked that the pointer of file structure and buffers are all
> not
> > > > null.
> > > > > > I also switch to the user space by get_fs() and set_fs() (but
not
> > sure
> > > > > > whether
> > > > > > this is correct.)

> > > > > > Could anyone give me any suggestions? Thanks a lot!

> > > > > >  Tom

 
 
 

How to open/read a file during a function call in the networking kernel codes

Post by Matthew Impet » Fri, 16 Feb 2001 23:49:02


why not create a character device?? In ip_rcv, instead of writing the data
to a file, write it to the memory space of your character device, and have a
user process (which is easy to program to open/read/write files) read from the
device and dump to file???

Matt

: I put this code in the ip_input.c: ip_rcv().
: After ip_rcv() defines some variables, I will check whether or not we need
: to open/read a file.
: When a packet come from eth0, and ip_rcv() is called, the code will go to
: file branch. Once
: it goes into this branch, the system will crash.

 
 
 

How to open/read a file during a function call in the networking kernel codes

Post by tlin » Sat, 17 Feb 2001 02:26:29


Can you give me some reference web link to how to make a character device?
Thanks!


> why not create a character device?? In ip_rcv, instead of writing the data
> to a file, write it to the memory space of your character device, and have
a
> user process (which is easy to program to open/read/write files) read from
the
> device and dump to file???

> Matt


> : I put this code in the ip_input.c: ip_rcv().
> : After ip_rcv() defines some variables, I will check whether or not we
need
> : to open/read a file.
> : When a packet come from eth0, and ip_rcv() is called, the code will go
to
> : file branch. Once
> : it goes into this branch, the system will crash.