When Are Free Pages Zeroed?

2011-11-09 Thread Patrick Simmons
I'm looking into the Linux memory management subsystem and need to know 
when freed pages are zeroed.  Specifically, are they zeroed just before 
they are reallocated, immediately after they are freed, or sometime 
in-between?  Also, what files/functions would I need to modify to change 
this behavior?

Thanks for any help and kindest regards,
--Patrick

-- 
If I'm not here, I've gone out to find myself.  If I get back before I return, 
please keep me here.


___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Re: Spinlocks and interrupts

2011-11-09 Thread Rajat Sharma
Hi Dave,

> Also, remember that spin-locks are no-ops on a single processor
> machine, so as coded, you have no protection on a single-processor
> machine if you're calling from thread context.

Not completely true, spinlock can still provide protection even in
this case i.e. two thread context sharing a resource on UP. Remember
that for peemptive kernel, it disables preemption, so it is guranteed
that scheduler does not through out this thread as long as spinlock is
held.

I agree that a mutex can as well be used for this example but it again
depends on situation, if you want to be quick about your task and are
100% sure that task does not sleep or takes too long processing
cycles, disabling preemption (i.e. spinlock in this case) can be a
better option.

Thanks,
Rajat

On Thu, Nov 10, 2011 at 10:17 AM, rohan puri  wrote:
>
>
> On Thu, Nov 10, 2011 at 3:10 AM, Dave Hylands  wrote:
>>
>> Hi Kai,
>>
>> On Wed, Nov 9, 2011 at 1:07 PM, Kai Meyer  wrote:
>> > When I readup on spinlocks, it seems like I need to choose between
>> > disabling interrupts and not. If a spinlock_t is never used during an
>> > interrupt, am I safe to leave interrupts enabled while I hold the lock?
>> > (Same question for read/write locks if it is different.)
>>
>> So the intention behind using a spinlock is to provide mutual exclusion.
>>
>> A spinlock by itself only really provides mutual exclusion between 2
>> cores, and not within the same core. To provide the mutual exclusion
>> within the same core, you need to disable interrupts.
>>
>> Normally, you would disable interrupts and acquire the spinlock to
>> guarantee that mutual exclusion, and the only reason you would
>> normally use the spinlock without disabling interrupts is when you
>> know that interrupts are already disabled.
>>
>> The danger of acquiring a spinlock with interrupts enabled is that if
>> another interrupt fired (or the same interrupt fired again) and it
>> tried to acquire the same spinlock, then you could have deadlock.
>>
>> If no interrupts touch the spinlock, then you're probably using the
>> wrong mutual exclusion mechanism. spinlocks are really intended to
>> provide mutual exclsion between interrupt context and non-interrupt
>> context.
>>
>> Also remember, that on a non-SMP (aka UP) build, spinlocks become
>> no-ops (except when certain debug checking code is enabled).
>>
>> --
>> Dave Hylands
>> Shuswap, BC, Canada
>> http://www.davehylands.com
>>
>> ___
>> Kernelnewbies mailing list
>> Kernelnewbies@kernelnewbies.org
>> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>
> Nice explanation Dave.
>
> Regards,
> Rohan Puri
>
> ___
> Kernelnewbies mailing list
> Kernelnewbies@kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>
>

___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Re: Guidance on writing device drivers

2011-11-09 Thread Prajosh Premdas
Hi Suraj

You can also download the softcopy of all the examples in LDD book. You can
also refer 2 other
books Venkateswaran_-_Essential_Linux_Device_Drivers_(Prentice,_2008)
and Professional Linux ® Kernel Architecture by Wolfgang Mauerer.

On Thu, Nov 10, 2011 at 10:08 AM, rohan puri  wrote:

>
>
> On Wed, Nov 9, 2011 at 10:40 PM, suraj khurana 
> wrote:
>
>> Hi all,
>> I am a newbie in linux world and want to learn linux device drivers.
>>
>> I am currently reading book - "Linux Device Drivers" by O'reilly.
>>
>> Does anyone has any problem statements based on device drivers for
>> practice
>> so that I can enhance my skills in writing device drivers.
>>
>>
>> Thanks,
>> Suraj
>>
>> ___
>> Kernelnewbies mailing list
>> Kernelnewbies@kernelnewbies.org
>> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>>
>> Hello Suraj,
> I would suggest complete reading the book with the examples given in the
> book.
>
> Regards,
> Rohan Puri
>
> ___
> Kernelnewbies mailing list
> Kernelnewbies@kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>
>


-- 
Regards,

Prajosh Premdas
___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Re: Spinlocks and interrupts

2011-11-09 Thread rohan puri
On Thu, Nov 10, 2011 at 3:10 AM, Dave Hylands  wrote:

> Hi Kai,
>
> On Wed, Nov 9, 2011 at 1:07 PM, Kai Meyer  wrote:
> > When I readup on spinlocks, it seems like I need to choose between
> > disabling interrupts and not. If a spinlock_t is never used during an
> > interrupt, am I safe to leave interrupts enabled while I hold the lock?
> > (Same question for read/write locks if it is different.)
>
> So the intention behind using a spinlock is to provide mutual exclusion.
>
> A spinlock by itself only really provides mutual exclusion between 2
> cores, and not within the same core. To provide the mutual exclusion
> within the same core, you need to disable interrupts.
>
> Normally, you would disable interrupts and acquire the spinlock to
> guarantee that mutual exclusion, and the only reason you would
> normally use the spinlock without disabling interrupts is when you
> know that interrupts are already disabled.
>
> The danger of acquiring a spinlock with interrupts enabled is that if
> another interrupt fired (or the same interrupt fired again) and it
> tried to acquire the same spinlock, then you could have deadlock.
>
> If no interrupts touch the spinlock, then you're probably using the
> wrong mutual exclusion mechanism. spinlocks are really intended to
> provide mutual exclsion between interrupt context and non-interrupt
> context.
>
> Also remember, that on a non-SMP (aka UP) build, spinlocks become
> no-ops (except when certain debug checking code is enabled).
>
> --
> Dave Hylands
> Shuswap, BC, Canada
> http://www.davehylands.com
>
> ___
> Kernelnewbies mailing list
> Kernelnewbies@kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>

Nice explanation Dave.

Regards,
Rohan Puri
___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Re: Guidance on writing device drivers

2011-11-09 Thread rohan puri
On Wed, Nov 9, 2011 at 10:40 PM, suraj khurana wrote:

> Hi all,
> I am a newbie in linux world and want to learn linux device drivers.
>
> I am currently reading book - "Linux Device Drivers" by O'reilly.
>
> Does anyone has any problem statements based on device drivers for practice
> so that I can enhance my skills in writing device drivers.
>
>
> Thanks,
> Suraj
>
> ___
> Kernelnewbies mailing list
> Kernelnewbies@kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>
> Hello Suraj,
I would suggest complete reading the book with the examples given in the
book.

Regards,
Rohan Puri
___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Re: Spinlocks and interrupts

2011-11-09 Thread Dave Hylands
Hi Kai,

On Wed, Nov 9, 2011 at 3:12 PM, Kai Meyer  wrote:
> Ok, I need mutual exclusion on a data structure regardless of interrupts
> and core. It sounds like it can be done by using a spinlock and
> disabling interrupts, but you mention that "spinlocks are intended to
> provide mutual exclsion between interrupt context and non-interrupt
> context." Should I be using a semaphore (mutex) instead?

It depends. If the function is only called from thread context, then
you probably want to use a mutex. If there is a possibility that it
might be called from interrupt context, then you can't use a mutex.

Also, remember that spin-locks are no-ops on a single processor
machine, so as coded, you have no protection on a single-processor
machine if you're calling from thread context.

> Perhaps I could explain my problem with some code:
> struct my_struct *get_data(spinlock_t *mylock, int ALLOC_DATA)
> {
>     struct my_struct *mydata = NULL;
>     spin_lock(mylock);
>     if (test_bit(index, mybitmap))
>             mydata = retrieve_data();
>     if (!mydata && ALLOC_DATA) {
>             mydata = alloc_data();
>             set_bit(index, mybitmap);
>     }
>     spin_unlock(mylock);
>     return mydata;
> }
>
> I need to prevent retrieve_data from being called if the index bit is
> set in mybitmap and alloc_data has not completed, so I use a bitmap to
> indicate that alloc_data has completed. I also need to protect
> alloc_data from being run multiple times, so I use the spin_lock to
> ensure that test_bit (and possibly retrieve_data) is not run while
> alloc_data is being run (because it runs while the bit is cleared).

If alloc_data might block, then you can't disable interrupts and you
definitely shouldn't be using spinlocks.

-- 
Dave Hylands
Shuswap, BC, Canada
http://www.davehylands.com

___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


RE: Spinlocks and interrupts

2011-11-09 Thread Jeff Haran
> -Original Message-
> From: kernelnewbies-bounces+jharan=bytemobile@kernelnewbies.org
> [mailto:kernelnewbies-
> bounces+jharan=bytemobile@kernelnewbies.org] On Behalf Of Kai
> Meyer
> Sent: Wednesday, November 09, 2011 3:12 PM
> To: kernelnewbies@kernelnewbies.org
> Subject: Re: Spinlocks and interrupts
> 
> Ok, I need mutual exclusion on a data structure regardless of
interrupts
> and core. It sounds like it can be done by using a spinlock and
> disabling interrupts, but you mention that "spinlocks are intended to
> provide mutual exclsion between interrupt context and non-interrupt
> context." Should I be using a semaphore (mutex) instead?
> 
> Perhaps I could explain my problem with some code:
> struct my_struct *get_data(spinlock_t *mylock, int ALLOC_DATA)
> {
>  struct my_struct *mydata = NULL;
>  spin_lock(mylock);
>  if (test_bit(index, mybitmap))
>  mydata = retrieve_data();
>  if (!mydata && ALLOC_DATA) {
>  mydata = alloc_data();
>  set_bit(index, mybitmap);
>  }
>  spin_unlock(mylock);
>  return mydata;
> }
> 
> I need to prevent retrieve_data from being called if the index bit is
> set in mybitmap and alloc_data has not completed, so I use a bitmap to
> indicate that alloc_data has completed. I also need to protect
> alloc_data from being run multiple times, so I use the spin_lock to
> ensure that test_bit (and possibly retrieve_data) is not run while
> alloc_data is being run (because it runs while the bit is cleared).
> 
> -Kai Meyer

You probably want to lock with spin_lock_irqsave() and unlock with
spin_unlock_irqrestore() if you are not sure about what contexts
get_data() will be called in.

There's plenty of examples of how to use these in the kernel sources.

I note you are passing in the address of the spinlock itself. Be wary of
deadly embraces where two threads acquire two locks in different order.

Jeff Haran




___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Re: Spinlocks and interrupts

2011-11-09 Thread Kai Meyer
Ok, I need mutual exclusion on a data structure regardless of interrupts 
and core. It sounds like it can be done by using a spinlock and 
disabling interrupts, but you mention that "spinlocks are intended to 
provide mutual exclsion between interrupt context and non-interrupt 
context." Should I be using a semaphore (mutex) instead?

Perhaps I could explain my problem with some code:
struct my_struct *get_data(spinlock_t *mylock, int ALLOC_DATA)
{
 struct my_struct *mydata = NULL;
 spin_lock(mylock);
 if (test_bit(index, mybitmap))
 mydata = retrieve_data();
 if (!mydata && ALLOC_DATA) {
 mydata = alloc_data();
 set_bit(index, mybitmap);
 }
 spin_unlock(mylock);
 return mydata;
}

I need to prevent retrieve_data from being called if the index bit is 
set in mybitmap and alloc_data has not completed, so I use a bitmap to 
indicate that alloc_data has completed. I also need to protect 
alloc_data from being run multiple times, so I use the spin_lock to 
ensure that test_bit (and possibly retrieve_data) is not run while 
alloc_data is being run (because it runs while the bit is cleared).

-Kai Meyer


On 11/09/2011 02:40 PM, Dave Hylands wrote:
> Hi Kai,
>
> On Wed, Nov 9, 2011 at 1:07 PM, Kai Meyer  wrote:
>> When I readup on spinlocks, it seems like I need to choose between
>> disabling interrupts and not. If a spinlock_t is never used during an
>> interrupt, am I safe to leave interrupts enabled while I hold the lock?
>> (Same question for read/write locks if it is different.)
> So the intention behind using a spinlock is to provide mutual exclusion.
>
> A spinlock by itself only really provides mutual exclusion between 2
> cores, and not within the same core. To provide the mutual exclusion
> within the same core, you need to disable interrupts.
>
> Normally, you would disable interrupts and acquire the spinlock to
> guarantee that mutual exclusion, and the only reason you would
> normally use the spinlock without disabling interrupts is when you
> know that interrupts are already disabled.
>
> The danger of acquiring a spinlock with interrupts enabled is that if
> another interrupt fired (or the same interrupt fired again) and it
> tried to acquire the same spinlock, then you could have deadlock.
>
> If no interrupts touch the spinlock, then you're probably using the
> wrong mutual exclusion mechanism. spinlocks are really intended to
> provide mutual exclsion between interrupt context and non-interrupt
> context.
>
> Also remember, that on a non-SMP (aka UP) build, spinlocks become
> no-ops (except when certain debug checking code is enabled).
>

___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Re: Spinlocks and interrupts

2011-11-09 Thread Dave Hylands
Hi Kai,

On Wed, Nov 9, 2011 at 1:07 PM, Kai Meyer  wrote:
> When I readup on spinlocks, it seems like I need to choose between
> disabling interrupts and not. If a spinlock_t is never used during an
> interrupt, am I safe to leave interrupts enabled while I hold the lock?
> (Same question for read/write locks if it is different.)

So the intention behind using a spinlock is to provide mutual exclusion.

A spinlock by itself only really provides mutual exclusion between 2
cores, and not within the same core. To provide the mutual exclusion
within the same core, you need to disable interrupts.

Normally, you would disable interrupts and acquire the spinlock to
guarantee that mutual exclusion, and the only reason you would
normally use the spinlock without disabling interrupts is when you
know that interrupts are already disabled.

The danger of acquiring a spinlock with interrupts enabled is that if
another interrupt fired (or the same interrupt fired again) and it
tried to acquire the same spinlock, then you could have deadlock.

If no interrupts touch the spinlock, then you're probably using the
wrong mutual exclusion mechanism. spinlocks are really intended to
provide mutual exclsion between interrupt context and non-interrupt
context.

Also remember, that on a non-SMP (aka UP) build, spinlocks become
no-ops (except when certain debug checking code is enabled).

-- 
Dave Hylands
Shuswap, BC, Canada
http://www.davehylands.com

___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Spinlocks and interrupts

2011-11-09 Thread Kai Meyer
When I readup on spinlocks, it seems like I need to choose between 
disabling interrupts and not. If a spinlock_t is never used during an 
interrupt, am I safe to leave interrupts enabled while I hold the lock? 
(Same question for read/write locks if it is different.)

-Kai Meyer

___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Re: Char device initialization

2011-11-09 Thread Daniel Hilst Selli
On 11/09/2011 01:28 PM, rohan puri wrote:
>
>
> On Wed, Nov 9, 2011 at 7:22 PM, Alexandru Juncu  > wrote:
>
> On Wed, Nov 9, 2011 at 3:41 PM, Daniel Hilst Selli
> mailto:danielhi...@gmail.com>> wrote:
>  > I'm trying to create a example char device. The example compiles
> fine,
>  > but when I try to "cat" I got "No such device or address". I have
>  > reviewed the code thousend times and can't see what I'm missing
>  >
>  > Here is the code -> http://pastebin.com/Td03U0fK
>  >
>  > The read method is not good, I know, but is never called.
>  >
>  > I use my own running kenrel to test, I know that is danger. I'm
> building
>  > a qemu enviroment to test this better.
>  >
>  > Here is uname -a:
>  > Linux archlinux 3.0-ARCH #1 SMP PREEMPT Fri Oct 7 11:35:34 CEST 2011
>  > x86_64 Intel(R) Core(TM) i3 CPU M 370 @ 2.40GHz GenuineIntel
> GNU/Linux
>  >
>  > Any idea?
>  >
>  > Thanks
>
> You tried to 'cat' a /dev/my_device file, right?
> Was that device file created with the mknod command?
>
> --
> Alexandru Juncu
>
> ROSEdu
> http://rosedu.org
>
> ___
> Kernelnewbies mailing list
> Kernelnewbies@kernelnewbies.org 
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>
>
>
> Hello Daniel,
>
> I had the look at the code. The issue is with the cdev_add() call in
> init_gcdev() function.
>
> int cdev_add(struct cdev *p, dev_t dev, unsigned count) is the prototype
>
> Now the problem was *instead of passing second argument of type dev_t
> you were passing minor number macro.
>
> *Fix : - Do following additions : -
>
> 1. static int major; // Declare a global major no var.
>
> 2. In init_gcdev() after call to alloc_chrdev_region() get major no and
> store in major var.
>
> major = MAJOR(gcdev->dev);
>
> 3. Replace cdev_add() call like this : -
>
>   cdev_add(&gcdev->cdev, MKDEV(major, FIRST_MINOR), 1);
>
> Now its running and your read methos is getting called.
>
> Hello Alexandru,
>
> That error was due to improper args passed to cdev_add(). If device file
> is not present (no mknod done) error would be "No such file or dir"
>
> Regards,
> Rohan Puri
Thanks Rohan, my fault!



___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Guidance on writing device drivers

2011-11-09 Thread suraj khurana
Hi all,
I am a newbie in linux world and want to learn linux device drivers.

I am currently reading book - "Linux Device Drivers" by O'reilly.

Does anyone has any problem statements based on device drivers for practice
so that I can enhance my skills in writing device drivers.


Thanks,
Suraj
___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Re: Char device initialization

2011-11-09 Thread rohan puri
On Wed, Nov 9, 2011 at 7:22 PM, Alexandru Juncu wrote:

> On Wed, Nov 9, 2011 at 3:41 PM, Daniel Hilst Selli
>  wrote:
> > I'm trying to create a example char device. The example compiles fine,
> > but when I try to "cat" I got "No such device or address". I have
> > reviewed the code thousend times and can't see what I'm missing
> >
> > Here is the code -> http://pastebin.com/Td03U0fK
> >
> > The read method is not good, I know, but is never called.
> >
> > I use my own running kenrel to test, I know that is danger. I'm building
> > a qemu enviroment to test this better.
> >
> > Here is uname -a:
> > Linux archlinux 3.0-ARCH #1 SMP PREEMPT Fri Oct 7 11:35:34 CEST 2011
> > x86_64 Intel(R) Core(TM) i3 CPU M 370 @ 2.40GHz GenuineIntel GNU/Linux
> >
> > Any idea?
> >
> > Thanks
>
> You tried to 'cat' a /dev/my_device file, right?
> Was that device file created with the mknod command?
>
> --
> Alexandru Juncu
>
> ROSEdu
> http://rosedu.org
>
> ___
> Kernelnewbies mailing list
> Kernelnewbies@kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>


Hello Daniel,

I had the look at the code. The issue is with the cdev_add() call in
init_gcdev() function.

int cdev_add(struct cdev *p, dev_t dev, unsigned count) is the prototype

Now the problem was  *instead of passing second argument of type dev_t you
were passing minor number macro.

*Fix : - Do following additions : -

1. static int major; // Declare a global major no var.

2. In init_gcdev() after call to alloc_chrdev_region() get major no and
store in major var.

major = MAJOR(gcdev->dev);

3. Replace cdev_add() call like this : -

 cdev_add(&gcdev->cdev, MKDEV(major, FIRST_MINOR), 1);

Now its running and your read methos is getting called.

Hello Alexandru,

That error was due to improper args passed to cdev_add(). If device file is
not present (no mknod done) error would be "No such file or dir"

Regards,
Rohan Puri
___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Re: Char device initialization

2011-11-09 Thread Alexandru Juncu
On Wed, Nov 9, 2011 at 3:41 PM, Daniel Hilst Selli
 wrote:
> I'm trying to create a example char device. The example compiles fine,
> but when I try to "cat" I got "No such device or address". I have
> reviewed the code thousend times and can't see what I'm missing
>
> Here is the code -> http://pastebin.com/Td03U0fK
>
> The read method is not good, I know, but is never called.
>
> I use my own running kenrel to test, I know that is danger. I'm building
> a qemu enviroment to test this better.
>
> Here is uname -a:
> Linux archlinux 3.0-ARCH #1 SMP PREEMPT Fri Oct 7 11:35:34 CEST 2011
> x86_64 Intel(R) Core(TM) i3 CPU M 370 @ 2.40GHz GenuineIntel GNU/Linux
>
> Any idea?
>
> Thanks

You tried to 'cat' a /dev/my_device file, right?
Was that device file created with the mknod command?

-- 
Alexandru Juncu

ROSEdu
http://rosedu.org

___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Char device initialization

2011-11-09 Thread Daniel Hilst Selli
I'm trying to create a example char device. The example compiles fine, 
but when I try to "cat" I got "No such device or address". I have
reviewed the code thousend times and can't see what I'm missing

Here is the code -> http://pastebin.com/Td03U0fK

The read method is not good, I know, but is never called.

I use my own running kenrel to test, I know that is danger. I'm building 
a qemu enviroment to test this better.

Here is uname -a:
Linux archlinux 3.0-ARCH #1 SMP PREEMPT Fri Oct 7 11:35:34 CEST 2011 
x86_64 Intel(R) Core(TM) i3 CPU M 370 @ 2.40GHz GenuineIntel GNU/Linux

Any idea?

Thanks

___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies