LWN.net Logo

A new set of per-CPU operations

By Jonathan Corbet
December 16, 2009
Per-CPU variables are a performance-improving technology. They allow processors to work with data without having to worry about locking or cache contention. One would want these operations to be well optimized, but, as it turns out, they can be improved; Tejun Heo and Christoph Lameter have done just that for 2.6.33. In the process, they have changed the way developers work with these variables.

There is a set of new operations:

    this_cpu_read(scalar);
    this_cpu_write(scalar, value);
    this_cpu_add(scalar, value);
    this_cpu_sub(scalar, value);
    this_cpu_inc(scalar);
    this_cpu_dec(scalar);
    this_cpu_and(scalar, value);
    this_cpu_or(scalar, value);
    this_cpu_xor(scalar, value);

In each case, scalar is either a per-CPU variable obtained with a new allocator or a static per-CPU variable as obtained from per_cpu_var(). All of them are atomic, in that the operation will not be interrupted part-way through on the current processor. It is not necessary to call put_cpu() after using these operations.

See, for example, the VM statistics conversion for an example of how operations on per-CPU variables change under the new scheme.


(Log in to post comments)

A new set of per-CPU operations

Posted Dec 17, 2009 18:01 UTC (Thu) by cma (subscriber, #49905) [Link]

Are those operations available also on userspace?

A new set of per-CPU operations

Posted Dec 18, 2009 10:24 UTC (Fri) by iq-0 (subscriber, #36655) [Link]

That's largely irrelevant, for userspace runs in threads and processes and don't really know or care on which cpu they're running (also given the fact that they can be preempted at virtually any moment and could possibly resume on a different cpu.
The thing that comes closest to this kind of functionality would be thread-local variables.

Copyright © 2009, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds