Skip to contentSkip to conversation

IRQs and memory consistency

DISCUSSION Christopher Harvey 3 participants 7 messages Apr 10, 2012 – Apr 11, 2012

lore
Share & tools
lore
#0 lore raw

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

#1 lore raw

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 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.

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

#2 lore raw
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_t and then use atomic_set and atomic_read so 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.

#3 lore raw

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_t and then use atomic_set and atomic_read so 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

#5 lore raw
In reply to Christopher Harvey

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
#6 lore raw
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'