user2130295
user2130295

Reputation: 59

__verify_pcpu_ptr function in Linux Kernel - What does it do?

#define __verify_pcpu_ptr(ptr)                      
do {                                    
const void __percpu *__vpp_verify = (typeof((ptr) + 0))NULL;    
(void)__vpp_verify;                     
} while (0)

#define VERIFY_PERCPU_PTR(__p)                      
({                                  
__verify_pcpu_ptr(__p);                     
(typeof(*(__p)) __kernel __force *)(__p);           
})

What do these two functions do? What are they used for? How do they work?

Thanks.

Upvotes: 1

Views: 331

Answers (1)

Gil Hamilton
Gil Hamilton

Reputation: 12357

This is part of the scheme used by per_cpu_ptr to support a pointer that gets a different value for each CPU. There are two motives here:

  1. Ensure that accesses to the per-cpu data structure are only made via the per_cpu_ptr macro.
  2. Ensure that the argument given to the macro is of the correct type.

Restating, this ensures that (a) you don't accidentally access a per-cpu pointer without the macro (which would only reference the first of N members), and (b) that you don't inadvertently use the macro to cast a pointer that is not of the correct declared type to one that is.

By using these macros, you get the support of the compiler in type-checking without any runtime overhead. The compiler is smart enough to eventually recognize that all of these complex machinations result in no observable state change, yet the type-checking will have been performed. So you get the benefit of the type-checking, but no actual executable code will have been emitted by the compiler.

Upvotes: 1

Related Questions