kernelnewbies.kernelnewbies.org archive mirror
 help / color / mirror / Atom feed
* what's with "get_cpu_ptr" and "put_cpu_ptr"?
@ 2012-09-30 14:35 Robert P. J. Day
  2012-09-30 21:40 ` TJ
  0 siblings, 1 reply; 2+ messages in thread
From: Robert P. J. Day @ 2012-09-30 14:35 UTC (permalink / raw)
  To: kernelnewbies


  at the moment, i'm working my way through numerous kernel topics in
aid of tracking updates for LKD3 and making personal notes.  for
instance, here's my wiki page for per-CPU variables:

http://www.crashcourse.ca/wiki/index.php/Per-CPU_variables

  as you can see, lots of links to docs and tutorials and excerpts
from header and source files, just for my own benefit for the time
being and to be organized later.

  but toward the end, you can read where i'm a bit puzzled by two
macros i ran across just this morning that i had never seen before,
and defined in <linux/percpu.h>:

#define get_cpu_ptr(var) ({                             \
        preempt_disable();                              \
        this_cpu_ptr(var); })

#define put_cpu_ptr(var) do {                           \
        (void)(var);                                    \
        preempt_enable();                               \
} while (0)

  i don't recall ever seeing a reference to those two macros anywhere
i've read, and a tree-wide search shows *very* few uses:

$ grep -rw get_cpu_ptr *
include/linux/percpu.h:#define get_cpu_ptr(var) ({				\
kernel/events/core.c:		cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
kernel/events/core.c:		cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
kernel/events/core.c:		cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
$ grep -rw put_cpu_ptr *
include/linux/percpu.h:#define put_cpu_ptr(var) do {				\
kernel/events/core.c:		put_cpu_ptr(pmu->pmu_cpu_context);
kernel/events/core.c:		put_cpu_ptr(pmu->pmu_cpu_context);
kernel/events/core.c:		put_cpu_ptr(pmu->pmu_cpu_context);
$

  that's it, that appears to be the extent of usage in the entire
kernel source tree.  i'm sure i'll figure them out given a bit more
time, but anyone want to clarify the purpose of those macros?  and
perhaps whether they're rarely used because normal kernel programmers
shouldn't be using them?  thanks.

rday

--

========================================================================
Robert P. J. Day                                 Ottawa, Ontario, CANADA
                        http://crashcourse.ca

Twitter:                                       http://twitter.com/rpjday
LinkedIn:                               http://ca.linkedin.com/in/rpjday
========================================================================

^ permalink raw reply	[flat|nested] 2+ messages in thread

* what's with "get_cpu_ptr" and "put_cpu_ptr"?
  2012-09-30 14:35 what's with "get_cpu_ptr" and "put_cpu_ptr"? Robert P. J. Day
@ 2012-09-30 21:40 ` TJ
  0 siblings, 0 replies; 2+ messages in thread
From: TJ @ 2012-09-30 21:40 UTC (permalink / raw)
  To: kernelnewbies

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 30/09/12 15:35, Robert P. J. Day wrote:>
> but toward the end, you can read where i'm a bit puzzled by two macros i ran across just this morning that i had never seen before, and defined in <linux/percpu.h>:
> 
> #define get_cpu_ptr(var) ({                             \ preempt_disable();                              \ this_cpu_ptr(var); })
> 
> #define put_cpu_ptr(var) do {                           \ (void)(var);                                    \ preempt_enable();                               \ } while (0)

You may be aware of this already, but if not, it's a very useful technique to find out *why* some code was added. Comments in the kernel are usually kept sparse and the 'why' is often in the commit
history.

$ git blame include/linux/percpu.h | grep get_cpu_ptr
8b8e2ec1e (Peter Zijlstra        2010-09-16 19:21:28 +0200  42) #define get_cpu_ptr(var) ({	

$ git show 8b8e2ec1e
commit 8b8e2ec1eeca7f6941bc81cefc9663018d6ceb57
Author: Peter Zijlstra <a.p.zijlstra@chello.nl>
Date:   Thu Sep 16 19:21:28 2010 +0200

    percpu: Add {get,put}_cpu_ptr

    These are similar to {get,put}_cpu_var() except for dynamically
    allocated per-cpu memory.

    Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl>
    Acked-by: Tejun Heo <tj@kernel.org>
    LKML-Reference: <20100917093009.252867712@chello.nl>
    Signed-off-by: Ingo Molnar <mingo@elte.hu>

LKML (Linux Kernel Mailing List): https://lkml.org/lkml/2010/9/17/74


$ git blame kernel/events/core.c | grep 'cpuctx = get_cpu_ptr'
41945f6cc kernel/perf_event.c   (Peter Zijlstra           2010-09-16 19:17:24 +0200 4229) 		cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
41945f6cc kernel/perf_event.c   (Peter Zijlstra           2010-09-16 19:17:24 +0200 4375) 		cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
41945f6cc kernel/perf_event.c   (Peter Zijlstra           2010-09-16 19:17:24 +0200 4571) 		cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);

$ git show 41945f6cc
commit 41945f6ccf1e86f87fddf6b32db9cf431c05fb54
Author: Peter Zijlstra <a.p.zijlstra@chello.nl>
Date:   Thu Sep 16 19:17:24 2010 +0200

    perf: Avoid RCU vs preemption assumptions

    The per-pmu per-cpu context patch converted things from
    get_cpu_var() to this_cpu_ptr(), but that only works if
    rcu_read_lock() actually disables preemption, and since
    there is no such guarantee, we need to fix that.

    Use the newly introduced {get,put}_cpu_ptr().

    Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl>
    Cc: Tejun Heo <tj@kernel.org>
    LKML-Reference: <20100917093009.308453028@chello.nl>
    Signed-off-by: Ingo Molnar <mingo@elte.hu>


Hope that helps.

TJ.



-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://www.enigmail.net/

iEYEARECAAYFAlBovEUACgkQ7+w3pCnNYID+4ACgi4oUU3n/ac+eUHGevjqgbN85
e24AniE1ASr6nzSHSXoJFNRy0Y85H1ou
=F8AK
-----END PGP SIGNATURE-----

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2012-09-30 21:40 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-09-30 14:35 what's with "get_cpu_ptr" and "put_cpu_ptr"? Robert P. J. Day
2012-09-30 21:40 ` TJ

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).