Re: copy-on-write overhead

2010-01-11 Thread Pete

 On Mon, Jan 11, 2010 at 6:22 AM, Mulyadi Santosa
 mulyadi.sant...@gmail.com wrote:
  On 1/11/10, Joel Fernandes agnel.j...@gmail.com wrote:
  Oh I'm sorry, if you were talking about copying of the address space
  information that can be avoided, that does not happen because it
  would've already been copied before exec() in the child gets a chance
  to execute.. the fork system call calls do_fork somewhere which calls
  copy_process which does this copying so it can't be avoided in any
  case. The book says copy-on-write itself has more overhead that is
  avoided with exec() in the child, but I'm trying to figure how.
 
  -Joel
 
 
  Hi Joel...
 
  Manish is right. Please notice that he talked about why do we do copy
  on write (COW) if soon after child is forked, it quickly does exec().
  So yes, COW has overhead, but imagine if parent ran first. COW will be
  triggered for parent address space, then child soon runs too. Then it
  issues exec(). Clearly, this waste certain amout of memory which can
  be fairly avoided if child runs first.


After going through this thread, I just tried out the following simple
code: 

int main (void) {

int pid;

int *testVar = (int *) malloc (sizeof (int));

*testVar = 10;

printf (%d [%d] Main \n, *testVar, testVar);

pid=vfork();// works fine if we use fork instead.

if (pid==0) {

printf (Child %d [%d]\n, *testVar, testVar);

return 1;

} else if (pid  0) {

printf (Parent %d [%d]\n, *testVar, testVar);

*testVar=11; // segfault if we use vfork, as vfork blocks until child
returns call exec/exits.

wait(NULL);

printf (Parent %d [%d]\n, *testVar, testVar);

return 1;

}

exit(0);
}

can someone let me know why this segfaults with vfork and not with fork?
From my understanding - it is because the parent is blocked until the
child exec's/exits AND in the mean time when the program is being
executed the child/parent process is trying to change the *testVar is
causing to modify the parents read-only memory. CMIAW. 

Another thing I noticed is on linux the child always gets to run first
in case of fork() and vfork()?

Cheers
~Pete 



--
To unsubscribe from this list: send an email with
unsubscribe kernelnewbies to ecar...@nl.linux.org
Please read the FAQ at http://kernelnewbies.org/FAQ



Re: copy-on-write overhead

2010-01-11 Thread askb
On Mon, 2010-01-11 at 15:01 +0530, Pete wrote:

 After going through this thread, I just tried out the following simple
 code: 
 
 int main (void) {
 
 int pid;
 
 int *testVar = (int *) malloc (sizeof (int));
 
 *testVar = 10;
 
 printf (%d [%d] Main \n, *testVar, testVar);
 
 pid=vfork();  // works fine if we use fork instead.
 
 if (pid==0) {
 
 printf (Child %d [%d]\n, *testVar, testVar);
 
 return 1;
 
 } else if (pid  0) {
 
 printf (Parent %d [%d]\n, *testVar, testVar);
 
 *testVar=11; // segfault if we use vfork, as vfork blocks until child
 returns call exec/exits.
 
 wait(NULL);
 
 printf (Parent %d [%d]\n, *testVar, testVar);
 
 return 1;
 
 }
 
 exit(0);
 }
 
 can someone let me know why this segfaults with vfork and not with fork?
 From my understanding - it is because the parent is blocked until the
 child exec's/exits AND in the mean time when the program is being
 executed the child/parent process is trying to change the *testVar is
 causing to modify the parents read-only memory. CMIAW. 
 
 Another thing I noticed is on linux the child always gets to run first
 in case of fork() and vfork()?

In my opinion - its because vfork() function has the same effect as
fork(), except that the behaviour is undefined if the process created by
vfork() either modifies any data other than a variable of type pid_t
used to store the return value from vfork(), or returns from the
function in which vfork() was called, or calls any other function before
successfully calling _exit() OR one of the exec family of functions.


--
To unsubscribe from this list: send an email with
unsubscribe kernelnewbies to ecar...@nl.linux.org
Please read the FAQ at http://kernelnewbies.org/FAQ



Re: copy-on-write overhead

2010-01-11 Thread Manish Katiyar
On Mon, Jan 11, 2010 at 3:01 PM, Pete greg.p...@gmail.com wrote:

 On Mon, Jan 11, 2010 at 6:22 AM, Mulyadi Santosa
 mulyadi.sant...@gmail.com wrote:
  On 1/11/10, Joel Fernandes agnel.j...@gmail.com wrote:
  Oh I'm sorry, if you were talking about copying of the address space
  information that can be avoided, that does not happen because it
  would've already been copied before exec() in the child gets a chance
  to execute.. the fork system call calls do_fork somewhere which calls
  copy_process which does this copying so it can't be avoided in any
  case. The book says copy-on-write itself has more overhead that is
  avoided with exec() in the child, but I'm trying to figure how.
 
  -Joel
 
 
  Hi Joel...
 
  Manish is right. Please notice that he talked about why do we do copy
  on write (COW) if soon after child is forked, it quickly does exec().
  So yes, COW has overhead, but imagine if parent ran first. COW will be
  triggered for parent address space, then child soon runs too. Then it
  issues exec(). Clearly, this waste certain amout of memory which can
  be fairly avoided if child runs first.


 After going through this thread, I just tried out the following simple
 code:

 int main (void) {

 int pid;

 int *testVar = (int *) malloc (sizeof (int));

 *testVar = 10;

 printf (%d [%d] Main \n, *testVar, testVar);

 pid=vfork();                    // works fine if we use fork instead.

 if (pid==0) {

 printf (Child %d [%d]\n, *testVar, testVar);

 return 1;

 } else if (pid  0) {

 printf (Parent %d [%d]\n, *testVar, testVar);

 *testVar=11; // segfault if we use vfork, as vfork blocks until child
 returns call exec/exits.

 wait(NULL);

 printf (Parent %d [%d]\n, *testVar, testVar);

 return 1;

 }

 exit(0);
 }

 can someone let me know why this segfaults with vfork and not with fork?

Man page says most of the stuff :-


The  use  of  vfork()  was  tricky:  for example, not modifying data
in the parent process  depended on knowing which variables are held in
a register.

In particular, the programmer cannot rely on  the  parent  remaining  blocked
until  the  child  either  terminates or calls execve(2), and cannot
rely on any specific behavior with  respect to shared memory.


Thanks -
Manish
 From my understanding - it is because the parent is blocked until the
 child exec's/exits AND in the mean time when the program is being
 executed the child/parent process is trying to change the *testVar is
 causing to modify the parents read-only memory. CMIAW.

 Another thing I noticed is on linux the child always gets to run first
 in case of fork() and vfork()?

 Cheers
 ~Pete



 --
 To unsubscribe from this list: send an email with
 unsubscribe kernelnewbies to ecar...@nl.linux.org
 Please read the FAQ at http://kernelnewbies.org/FAQ





-- 
Thanks -
Manish
==
[$\*.^ -- I miss being one of them
==

--
To unsubscribe from this list: send an email with
unsubscribe kernelnewbies to ecar...@nl.linux.org
Please read the FAQ at http://kernelnewbies.org/FAQ



Why can struct sock convert to struct tcp_sock?

2010-01-11 Thread Kapuddi
In funciton:

static int tcp_v4_init_sock(struct sock *sk)
{
struct inet_connection_sock *icsk = inet_csk(sk);
struct tcp_sock *tp = tcp_sk(sk);

...
}

Why can struct sock convert to struct tcp_sock?
Yes, 

static struct sock *sk_prot_alloc(struct proto *prot, gfp_t priority,
int family)
{
struct sock *sk;

sk = kmalloc(prot-obj_size, priority);
...
}
Although inn the function above, prot-obj_size equals to sizeof(struct
tcp_sock), I still can not understand this type conversion.


--
To unsubscribe from this list: send an email with
unsubscribe kernelnewbies to ecar...@nl.linux.org
Please read the FAQ at http://kernelnewbies.org/FAQ



Re: Suggestions for implementing a persistent binary tree

2010-01-11 Thread Bryan Donlan
On Sun, Jan 10, 2010 at 2:31 PM, Joel Fernandes agnel.j...@gmail.com wrote:
 Actually the unbounded delays is not an issue as the lookups are
 usually happening as deferred work, and most of my code is in kernel
 space so I'm not sure if pushing off the hashtable lookup code to
 userspace is a good idea..

 Thanks for pointing out that mmap wouldn't work in kernel space. But
 out of curiosity, does this have to do with the fact thatmm is set to
 NULL for a kernel thread task_struct? Why would page faults result in
 an OOPs because if I vmalloc something in the kernel, isn't it already
 mapped?

vmalloc won't result in page faults because it's mapped immediately,
yes. However that means it won't be file-backed. The file-backed
mapping support in the kernel relies on page faults to identify when
pages are dirtied, and the PF handler in the kernel will trigger an
oops unless the faulting address is registered as a userspace transfer
point.

While it might be possible to shoehorn in kernel support for
kernel-mode file-backed mappings, you'd have to be very careful that
you're not violating existing assumptions in the VM subsystem; it'd be
far easier and safer to just push it into userspace (and I can assure
you you'll never manage to upstream it if it's using file-backed
mappings in kernel mode :)

--
To unsubscribe from this list: send an email with
unsubscribe kernelnewbies to ecar...@nl.linux.org
Please read the FAQ at http://kernelnewbies.org/FAQ



Re: Why can struct sock convert to struct tcp_sock?

2010-01-11 Thread Michael Blizek
Hi!

On 15:02 Mon 11 Jan , Kapuddi wrote:
 In funciton:
 
 static int tcp_v4_init_sock(struct sock *sk)
 {
   struct inet_connection_sock *icsk = inet_csk(sk);
   struct tcp_sock *tp = tcp_sk(sk);
 
 ...
 }
 
 Why can struct sock convert to struct tcp_sock?

Because struct tcp_sock contains struct sock as the first member:

struct tcp_sock {
struct inet_connection_sock inet_conn;
...
};

struct inet_connection_sock {
struct inet_sock  icsk_inet;
...
};

struct inet_sock {
struct sock sk;
...
};

-Michi
-- 
programing a layer 3+4 network protocol for mesh networks
see http://michaelblizek.twilightparadox.com


--
To unsubscribe from this list: send an email with
unsubscribe kernelnewbies to ecar...@nl.linux.org
Please read the FAQ at http://kernelnewbies.org/FAQ



how to support module.param=val on kernel command line?

2010-01-11 Thread Robert P. J. Day

  if i might follow up on the earlier thread (because it reminded me
that i'm not clear *myself* on the mechanics here), what is the proper
way to support both manually-loadable module params that also extends
to building that module into the kernel?

  for out-of-tree modules, it's easy, i know that.  but the instant
you decide to incorporate that module/driver into the kernel, you want
to be able to initialize that driver the same way at boot time.

  as a real-life example, consider this snippet from
Documentation/kernel-parameters.txt:

i8042.debug [HW] Toggle i8042 debug mode
i8042.direct[HW] Put keyboard port into non-translated mode
i8042.dumbkbd   [HW] Pretend that controller can only read data from
 keyboard and cannot control its state
 (Don't attempt to blink the leds)
i8042.noaux [HW] Don't check for auxiliary (== mouse) port
i8042.nokbd [HW] Don't check/create keyboard port
i8042.noloop[HW] Disable the AUX Loopback command while probing
 for the AUX port

... and so on ...

  clearly(?), those commandline parms refer to the i8042 module
that's been built into the kernel, right?  the corresponding source
file, drivers/input/serio/i8042.c, defines those parms in the obvious
way:

static bool i8042_nokbd;
module_param_named(nokbd, i8042_nokbd, bool, 0);
MODULE_PARM_DESC(nokbd, Do not probe or use KBD port.);

static bool i8042_noaux;
module_param_named(noaux, i8042_noaux, bool, 0);
MODULE_PARM_DESC(noaux, Do not probe or use AUX (mouse) port.);

... etc etc ...

  but what is the underlying mechanism by which *that* particular
driver is identified by the prefix i8042?  if i look down that
source file, i eventually get to this:

...
static struct platform_driver i8042_driver = {
.driver = {
.name   = i8042,
.owner  = THIS_MODULE,
#ifdef CONFIG_PM
.pm = i8042_pm_ops,
#endif
},
.remove = __devexit_p(i8042_remove),
.shutdown   = i8042_shutdown,
};
...

  is that driver.name field value of i8042 what associates that
prefix i8042 with the settable driver parms?  i'm sure i could
figure this out if i read the source, but if someone else just wants
to clarify that, that would be terrific.  or explain to me what's
*actually* happening.  thanks.

rday
--



Robert P. J. Day   Waterloo, Ontario, CANADA

Linux Consulting, Training and Kernel Pedantry.

Web page:  http://crashcourse.ca
Twitter:   http://twitter.com/rpjday


--
To unsubscribe from this list: send an email with
unsubscribe kernelnewbies to ecar...@nl.linux.org
Please read the FAQ at http://kernelnewbies.org/FAQ



Re: 'No such device or address' when writing to device driver

2010-01-11 Thread Sam Carter
I've just installed strace and run as you advised although there is no extra
information on top of the 'No such device or address'. Does my driver and
loader code look right?

Thanks for the continued support, Sam

2010/1/8 Durga Prasad write...@yahoo.com

 On quick glance, do you know what is throwing up the error?
 Can you do 'strace -vv cat .. '



 --
 *From:* Sam Carter sam.w.car...@gmail.com
 *To:* Daniel Baluta daniel.bal...@gmail.com
 *Cc:* kernelnewbies kernelnewbies@nl.linux.org
 *Sent:* Fri, January 8, 2010 6:02:54 PM
 *Subject:* Re: 'No such device or address' when writing to device driver

 Hi all,

 Thanks for the responses. Here is the load/unload part of my driver.

 #include linux/init.h
 #include linux/module.h
 #include linux/kernel.h
 #include linux/sched.h
 #include linux/version.h
 #include linux/types.h
 #include linux/fs.h
 #include linux/string.h
 #include linux/dirent.h
 #include asm/uaccess.h
 #include linux/cdev.h

 #include memo.h

 static int __init memo(void);
 static void __exit cleanup(void);

 static int ioctlManager(int i);
 static int read(void);
 static int write(char *s);
 static int testDirent(int is64bit, int length, void *p);
 static int modulePrint(char *s);
 int dev, result;

 struct cdev *memoc;

 static struct file_operations fops = {
 .owner = THIS_MODULE,
 .llseek = NULL,
 .read = read,
 .write = write,
 .ioctl = ioctlManager,
 .open = NULL,
 .release = NULL,
 };

 extern void (*interceptor)(int, int, void *);

 static int __init memo(void){
 if(MAJOR){
 dev = MKDEV(MAJOR, MINOR);
 result = register_chrdev_region(dev, number_of_devices, memo);
 }else{
 result = alloc_chrdev_region(dev, MINOR, number_of_devices,
 memo);
 }
 memoc = cdev_alloc();
 cdev_init(memoc, fops);
 memoc-owner = THIS_MODULE;
 if(cdev_add(memoc, 249, 1)  0){
 printk(cdev device registration failed\n);
 }else{
 printk(cdev device registration success\n);
 }

 printk(KERN_INFO - Module interceptor loaded -\n);
 interceptor = testDirent;
 if(result  0){
 printk(KERN_WARNING memo: can't get major/minor numbers);
 return result;
 }else{
 return 0;
 }
 }

 static void __exit cleanup(void){
 int i = 0;
 unregister_chrdev_region(dev, number_of_devices);
 cdev_del(memoc);
 interceptor = NULL;
 printk(KERN_INFO - Module interceptor cleanup -%d\n,i);
 }

 And here is the load script I'm using to load the module.

 module=memo
 device=memo
 mode=664

 #if grep -q '^staff:' /etc/group; then
 #group=staff
 #else
 #group=wheel
 #fi
 group=root
 /sbin/insmod ./$module.ko $* || exit 1

 rm -f /dev/${device}

 major=$(awk \$2==\$module\ {print \$1} /proc/devices)

 mknod /dev/${device} c $major 0
 chgrp $group /dev/${device}
 chmod $mode /dev/${device}

 As far as I'm aware the Major/Minor numbers match up fine. With ls -l /dev,
 the major is 249, which is the same as in /proc/devices once I've loaded the
 module. Hope this helps someone find the problem.

 Sam

 2010/1/8 Daniel Baluta daniel.bal...@gmail.com

 On Thu, Jan 7, 2010 at 3:31 AM, Sam Carter sam.w.car...@gmail.com
 wrote:
  Hi all,
 
  I'm trying to write a simple char driver following the 'Linux Device
  Drivers' book from O' Reilly. My driver appears in /proc/devices and
 under
  lsmod. I've been trying to register the device fops table so I can
  read/write to it, however when I try to cat some data to it in
 /dev/myDevice
  I get the error 'No such device or address'. The device does appear on
 the
  list with ls /dev/.
 
  Can anyone help me find the source of this problem? Thanks in advance.


 Is the major/minor number of /dev/myDevice the same with major/minor
 allocated in your driver ?
 Can you post a pointer to your code?

 thanks,
 Daniel.






Re: 'No such device or address' when writing to device driver

2010-01-11 Thread Chetan Nanda
On Tue, Jan 12, 2010 at 4:55 AM, Sam Carter sam.w.car...@gmail.com wrote:

 I've just installed strace and run as you advised although there is no
 extra information on top of the 'No such device or address'. Does my driver
 and loader code look right?

 Thanks for the continued support, Sam


Where is your read/write function defined? there may be some error in these
functions.

~cnanda


 2010/1/8 Durga Prasad write...@yahoo.com

 On quick glance, do you know what is throwing up the error?
 Can you do 'strace -vv cat .. '



 --
 *From:* Sam Carter sam.w.car...@gmail.com
 *To:* Daniel Baluta daniel.bal...@gmail.com
 *Cc:* kernelnewbies kernelnewbies@nl.linux.org
 *Sent:* Fri, January 8, 2010 6:02:54 PM
 *Subject:* Re: 'No such device or address' when writing to device driver

 Hi all,

 Thanks for the responses. Here is the load/unload part of my driver.

 #include linux/init.h
 #include linux/module.h
 #include linux/kernel.h
 #include linux/sched.h
 #include linux/version.h
 #include linux/types.h
 #include linux/fs.h
 #include linux/string.h
 #include linux/dirent.h
 #include asm/uaccess.h
 #include linux/cdev.h

 #include memo.h

 static int __init memo(void);
 static void __exit cleanup(void);

 static int ioctlManager(int i);
 static int read(void);
 static int write(char *s);
 static int testDirent(int is64bit, int length, void *p);
 static int modulePrint(char *s);
 int dev, result;

 struct cdev *memoc;

 static struct file_operations fops = {
 .owner = THIS_MODULE,
 .llseek = NULL,
 .read = read,
 .write = write,
 .ioctl = ioctlManager,
 .open = NULL,
 .release = NULL,
 };

 extern void (*interceptor)(int, int, void *);

 static int __init memo(void){
 if(MAJOR){
 dev = MKDEV(MAJOR, MINOR);
 result = register_chrdev_region(dev, number_of_devices, memo);
 }else{
 result = alloc_chrdev_region(dev, MINOR, number_of_devices,
 memo);
 }
 memoc = cdev_alloc();
 cdev_init(memoc, fops);
 memoc-owner = THIS_MODULE;
 if(cdev_add(memoc, 249, 1)  0){
 printk(cdev device registration failed\n);
 }else{
 printk(cdev device registration success\n);
 }

 printk(KERN_INFO - Module interceptor loaded -\n);
 interceptor = testDirent;
 if(result  0){
 printk(KERN_WARNING memo: can't get major/minor numbers);
 return result;
 }else{
 return 0;
 }
 }

 static void __exit cleanup(void){
 int i = 0;
 unregister_chrdev_region(dev, number_of_devices);
 cdev_del(memoc);
 interceptor = NULL;
 printk(KERN_INFO - Module interceptor cleanup -%d\n,i);
 }

 And here is the load script I'm using to load the module.

 module=memo
 device=memo
 mode=664

 #if grep -q '^staff:' /etc/group; then
 #group=staff
 #else
 #group=wheel
 #fi
 group=root
 /sbin/insmod ./$module.ko $* || exit 1

 rm -f /dev/${device}

 major=$(awk \$2==\$module\ {print \$1} /proc/devices)

 mknod /dev/${device} c $major 0
 chgrp $group /dev/${device}
 chmod $mode /dev/${device}

 As far as I'm aware the Major/Minor numbers match up fine. With ls -l
 /dev, the major is 249, which is the same as in /proc/devices once I've
 loaded the module. Hope this helps someone find the problem.

 Sam

 2010/1/8 Daniel Baluta daniel.bal...@gmail.com

 On Thu, Jan 7, 2010 at 3:31 AM, Sam Carter sam.w.car...@gmail.com
 wrote:
  Hi all,
 
  I'm trying to write a simple char driver following the 'Linux Device
  Drivers' book from O' Reilly. My driver appears in /proc/devices and
 under
  lsmod. I've been trying to register the device fops table so I can
  read/write to it, however when I try to cat some data to it in
 /dev/myDevice
  I get the error 'No such device or address'. The device does appear on
 the
  list with ls /dev/.
 
  Can anyone help me find the source of this problem? Thanks in advance.


 Is the major/minor number of /dev/myDevice the same with major/minor
 allocated in your driver ?
 Can you post a pointer to your code?

 thanks,
 Daniel.







piping HID kbd handler to stdin - is a daemon needed ?

2010-01-11 Thread Microbit_Ubuntu
Hi all,

I've recently started using USB keyboard on my embedded SAM9-L9260
board. Part reason is that I can send certain hard codes that are less
practical to send via serial ttyS0.

Having studied the relevant code and docs for a few days, the
implication seems to be that after kernel setup of HID, a keyboard
should readily work, but it certainly doesn't on stdin.
I'm using kernel 2.6.31.6 - and FWIW, the docs
in /documentation/input.txt are outdated for a change - there is no
keybdev module anymore.. :-) (except for Mac I see)
Note : A ctrl-alt-del from the USB keyboard does cause a reboot on the
system, so some scan codes do find their way to stdin it seems ?

The idea is that stdin/stdout/stderr still flow through the serial,
ttyS0/115200, but the kdb handler should equally input keyboard data on
stdin.

I've avoided enabling kernel hiddev or hidraw, as I don't see any
benefit. I'm actually using a spare wireless keyboard/mouse device.
Hotplugging results in proper registration, and the device correctly
appears on /dev/input/event0  /dev/input/event1.
An od -x on event0 indicates proper raw scancode traffic.
I compiled evtest.c and that comes out 100% fine too when run on my
target. All keys from HID are properly picked up and displayed on
terminal by evtest.

Main question :
--

Do I really need to start a daemon in user space, similar to evtest ie.
run an event handler on kbd ?
It seems keyboard.c should already be converting the raw scan codes and
send them on ?

Or, perhaps, is it standard practice to enable atkbd in kernel and
'pretend' that stdin ascii traffic is entering the kernel via this
(virtual) PS/2 keyboard ?
(Note that I don't enable input devices - AT keyboard in kernel setup)

Connecting the same device to my Ubuntu 9.10 host results in immediate
entry of HID keyboard data onto a terminal etc. But I noticed in PC
syslog that a couple of rules around 'xkbd' activate and handle that
traffic redirecting.

Am I overlooking something silly, or does using a USB HID keyboard for
stdin on my embedded board really _does_ dictate a userspace utility to
convert scan code and redirect to stdin (so it displays on ttyS0
terminal output as echo AND results in system commands, as if it were
regular ttyS0 input) 

I would really appreciate any help, I've spent several days on this but
I don't seem to hit home on this one.


-- 
Best regards,
Kris



--
To unsubscribe from this list: send an email with
unsubscribe kernelnewbies to ecar...@nl.linux.org
Please read the FAQ at http://kernelnewbies.org/FAQ



How to get the number of free pages in a zone(2.6.18)?

2010-01-11 Thread Adam Jiang
Hello, list.

I want to backport mem_notify.patch to linux-2.6.18. Most of the things goes
well and simple, but a problem is in mem_notify.c there's a function named
mem_notify_poll() used the new zone_page_state() with NR_FREE_PAGES which is
not in 2.6.18. I just want to change the line to get free pages in a zone in
another way rather than patch the other headers in linux/. What should I do?
Is there a good method to get the number of free pages in 2.6.18? Any help
will be appreciate. Thanks you very much.

Best regards,
-- 
Adam Jiang
-☆-★-☆-★-☆-★-☆-★-☆-★-☆-★-☆-★-
e-mail:jiang.a...@gmail.com e-mail%3ajiang.a...@gmail.com
http://www.adamjiang.com
-☆-★-☆-★-☆-★-☆-★-☆-★-☆-★-☆-★-