IRQs and memory consistency
This is a high level question about how IRQs work in the kernel.
I have a struct that was kmalloc'd into ram. Within this struct there is an int, called devid. When I set this devid to a number that isn't 0, I print the following:
checking devid value....327683 in 0xcb6953d4
where 327683 is the value of the int and 0xcb6953d4 is the address of the struct that the devid value is in.
Then an interrupt happens and when I print this value again at the beginning of the interrupt handler I get the following printed text:
checking devid value....0 in 0xcb6953d4
notice that the devid has been set to 0. I can't find any code on my end that would do this. Is there something, maybe related to memory address spaces in IRQ handlers that I don't know about?
The IRQ and the setting of the devid value happen fairly close to each other in time. (like less than a second, or closer)
Any ideas or guesses are appreciated.
-Chris
Sounds to me like there needs to be a flush of the processor cache by using memory barriers.
I'm guessing that the IRQ is taken on a different thread and possibly a different processor and the value needs to be flushed. You might try having devid be an atomic_t and then use atomic_set and atomic_read so that the "proper" memory barriers are used.
See: http://www.kernel.org/doc/Documentation/atomic_ops.txt
-- Wink
Quoted 25 lines · Christopher Harvey
This is a high level question about how IRQs work in the kernel.
I have a struct that was kmalloc'd into ram. Within this struct there is an int, called devid. When I set this devid to a number that isn't 0, I print the following:
checking devid value....327683 in
0xcb6953d4where 327683 is the value of the int and
0xcb6953d4is the address of the struct that the devid value is in.Then an interrupt happens and when I print this value again at the beginning of the interrupt handler I get the following printed text:
checking devid value....0 in
0xcb6953d4notice that the devid has been set to 0. I can't find any code on my end that would do this. Is there something, maybe related to memory address spaces in IRQ handlers that I don't know about?
The IRQ and the setting of the devid value happen fairly close to each other in time. (like less than a second, or closer)
Any ideas or guesses are appreciated.
Quoted 8 lines
-Chris
_______________________________________________
Kernelnewbies mailing list
Kernelnewbies at kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20120410/885355e9/attachment.html
Quoted 2 lines · Wink Saville
Sounds to me like there needs to be a flush of the processor cache by using memory barriers.
I used a wmb(); right after I set the value I wanted.
Quoted 4 lines
I'm guessing that the IRQ is taken on a different thread and possibly a different processor and the value needs to be flushed. You might try having devid be an
atomic_tand then useatomic_setandatomic_readso that the "proper" memory barriers are used.
should I use atomic ops instead of a wmb?
The interrupt can't happen until the value is assigned completely in one thread. I'm sure.
Do you have a read barrier in the IRQ?
See "SMP BARRIER PAIRING" in:
http://www.kernel.org/doc/Documentation/memory-barriers.txt
-- Wink
Quoted 16 lines · Christopher Harvey
Quoted 2 lines · Wink Saville
Sounds to me like there needs to be a flush of the processor cache by using memory barriers.
I used a
wmb(); right after I set the value I wanted.Quoted 4 lines
I'm guessing that the IRQ is taken on a different thread and possibly a different processor and the value needs to be flushed. You might try having devid be an
atomic_tand then useatomic_setandatomic_readso that the "proper" memory barriers are used.should I use atomic ops instead of a wmb?
The interrupt can't happen until the value is assigned completely in one thread. I'm sure.
Quoted 5 lines
_______________________________________________
Kernelnewbies mailing list
Kernelnewbies at kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20120411/2406c3e0/attachment.html
Quoted 1 line · Wink Saville
Do you have a read barrier in the IRQ?
Ah, no I don't.
Quoted 3 lines
See "SMP BARRIER PAIRING" in:
http://www.kernel.org/doc/Documentation/memory-barriers.txt [3]
I saw that but only skimmed it.
I'll let the mailing list know if it worked when I get a chance to try it.
thanks,
-Chris
Hi....
Quoted 2 lines · Christopher Harvey
The IRQ and the setting of the devid value happen fairly close to each other in time. (like less than a second, or closer)
Hmmm, and how about the order? which one do you guess go first? setting value? or the IRQ handler?
I had a sense that your code flow might (in reality) goes like this
allocating struct RAM --> interrupted --> printing struct content --> back to initialize struct
If that's true, then no wonder 0 (zero) is printed.
I suggest to grab a spin lock if you really need atomicity during allocation and setting initial value, which in IRQ handler, you grab the lock before printing. Oh and since memory allocation could go slow, you might need to do allocation with GFP_ATOMIC.
Signature · 7 lines
regards, Mulyadi Santosa Freelance Linux trainer and consultant blog: the-hydra.blogspot.com training: mulyaditraining.blogspot.com
Quoted 15 lines · Mulyadi Santosa
Hi....
Quoted 2 lines · Christopher Harvey
The IRQ and the setting of the devid value happen fairly close to each other in time. (like less than a second, or closer)
Hmmm, and how about the order? which one do you guess go first? setting value? or the IRQ handler?
I had a sense that your code flow might (in reality) goes like this
allocating struct RAM --> interrupted --> printing struct content --> back to initialize struct
Quoted 6 lines
If that's true, then no wonder 0 (zero) is printed.
I suggest to grab a spin lock if you really need atomicity during allocation and setting initial value, which in IRQ handler, you grab the lock before printing. Oh and since memory allocation could go slow, you might need to do allocation with
GFP_ATOMIC.
well, actually my does does this:
set variable->init hardware->interrupt
the interrupt can't happen until 'init hardware'