Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?
like image 284
Amit Sharma Avatar asked Oct 17 '14 13:10

Amit Sharma


People also ask

What is the role of macro preprocessor?

Macros allow you to write commonly used PL/I code in a way that hides implementation details and the data that is manipulated and exposes only the operations. In contrast with a generalized subroutine, macros allow generation of only the code that is needed for each individual use.

What is the purpose of preprocessor?

In computer science, a preprocessor (or precompiler) is a program that processes its input data to produce output that is used as input to another program. The output is said to be a preprocessed form of the input data, which is often used by some subsequent programs like compilers.

What is the purpose of preprocessor directive Stdio h in C program?

The preprocessor is a part of the compiler which performs preliminary operations (conditionally compiling code, including files etc...) to your code before the compiler sees it. These transformations are lexical, meaning that the output of the preprocessor is still text.

What is role of pre processor in C program?

The C preprocessor is a macro processor that is used automatically by the C compiler to transform your program before actual compilation. It is called a macro processor because it allows you to define macros, which are brief abbreviations for longer constructs.

What is the role of the pre processor in C explain the use of pre processor directives like #include and #define with suitable examples?

It is a pre-process of execution of a program using c/c++ language. To initialize a process of preprocessor commands, it's mandated to define with a hash symbol (#). It can preferably be the non-blank character, and for better readability, a preprocessor directive should start in the first column.


1 Answers

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 .

like image 154
dragosht Avatar answered Sep 22 '22 08:09

dragosht