Do per cpu variables need to be atomic?

Christoph Lameter cl at gentwo.org
Thu Jul 10 16:55:24 EDT 2014


On Thu, 10 Jul 2014, Pranith Kumar wrote:

> > Per cpu data structures are for the use of one cpu exclusively. If you use
> > the variable as intended then there is no atomic_t required. If you want
> > to update the percpu variable from other processors then atomic_t is
> > required. But then this is not a proper percpu variable anymroe.
> >
>
> That also makes using barriers while updating such variables
> meaningless I guess? like

Correct.

> >> struct data *p = this_cpu_ptr(&datap);
> >
> > This converts the offset into an address that can be generally used as
> > a pointer (but no longer with this_cpu operations).
> >
>
> So we can pass around this pointer to other cpus but not the previous
> __percpu pointer?

You can pass this pointer around to other cpus. Caveat: the other cpus
will be accessing the percpu data from another cpu if the actually use
this pointer. Not recommended.

A __percpu offset (dont call it pointer) is always relative to the current
cpu. So if the offset is used then the instance of the percpu variable for
the current cpu is used and not the instance for another processor. If you
pass the offset to other processors then this_cpu_ptr() will create the
address for the currrent instance and this_cpu ops will always refer to
the instance associated with the currently executing processor.

Use of __percpu offsets is recommended.

> I think adding this information to the documentation would be a great
> idea. Especially the "__percpu" annotation. Is it ok if I try?

Sure. But there is already some documentation in
linux/Documentation/this_cpu_ops.txt.




More information about the Kernelnewbies mailing list