Amit Sharma
Amit Sharma

Reputation: 2067

what is the purpose of pre-processor macros defined in files linux/compiler.h?

I am very new to Linux kernel. And I am using the sparse tool to clean the noise present in the code. I encountered these macros:

   # define __user      __attribute__((noderef, address_space(1)))
   # define __kernel    __attribute__((address_space(0)))
   # define __safe      __attribute__((safe))
   # define __force __attribute__((force))
   # define __nocast    __attribute__((nocast)) 
   # define __iomem __attribute__((noderef, address_space(2)))
   # define __must_hold(x)  __attribute__((context(x,1,1)))
   # define __acquires(x)   __attribute__((context(x,0,1)))
   # define __releases(x)   __attribute__((context(x,1,0)))
   # define __acquire(x)    __context__(x,1)
   # define __release(x)    __context__(x,-1)
   # define __cond_lock(x,c)    ((c) ? ({ __acquire(x); 1; }) : 0)
   # define __percpu    __attribute__((noderef, address_space(3)))

And now I want to know: how they are used by sparse to report the errors/warning?

My questions:

  1. I want the details how they help compiler and sparse to report the warnings.
  2. What are these address_space(x) context(X, x, x) and there purpose ?
  3. What is the purpose of __nocast, __force, __user, __safe ?

Upvotes: 9

Views: 2449

Answers (1)

dragosht
dragosht

Reputation: 3275

I think all these macros only have meaning in the context of sparse (manpages here - You can find the descriptions of all these attributes and sparse warnings there). If you take a look above in the source file you copied there is a big __CHECKER__ macro used to enable them.

I'm not sure if gcc does anything with them at this point - I think it kind of silently ignores them ... In one of his e-mails back in 2004 Linus Torvalds was saying that:

This is important to remember: for gcc, the sparse annotations are meaningless. They can still be useful just to tell the programmer that "hey, that pointer you got wasn't a normal pointer" in a fairly readable manner, but in the end, unless you use sparse, they don't actually do anything.

There are some tickets opened for this purpose - http://www.spinics.net/lists/linux-sparse/msg03366.html .

Upvotes: 5

Related Questions