Stupid RCU Tricks: Compiler-Initialized Structures

To recap...

An earlier posting discussed safely accessing an RCU-protected pointer that initially referenced a compile-time initialized structure, but might later be set to NULL. This posting looks at the opposite situation, where an RCU-protected pointer is initially NULL, but might later be set to point to a compile-time initialized structure.

In short, we have the following data definitions:

struct foo {
    int a;
    int b;
};
struct foo static_foo = { 42, 17 };
struct foo *foo_p = NULL;

At some random point during runtime, the following code executes:

foo_p = &static_foo;

From that earlier posting, we know that readers must at least use ACCESS_ONCE() to safely fetch the pointer. But must readers go all the way, using rcu_dereference()?

Because the structure was initialized at compile time, the answer is “no”. However, in the Linux kernel, recent diagnostic features require use of rcu_dereference_protected(). Furthermore, if the store to foo_p had been preceded by stores the the structure itself, rcu_dereference() would be required for correct operation on Alpha.

This leads to the further question “after all these years, why should anyone care about Alpha?” The answer at present is that there are quite a few people who do care. So the Linux kernel's rcu_dereference() will need to accommodate Alpha for the foreseeable future.