As an example, if we want to demonstrate a synchronization technique called per-CPU variables, we are required to create two kernel threads and guarantee that each of them runs on a separate CPU core. To do so, we must set the CPU affinity mask of each kernel thread (the first one to 0, the second to 1, in order to have them execute on only CPUs 0 and 1 respectively). The thing is, it's not a clean job – quite a hack, to be honest, and definitely not recommended. The following comment from that code shows why:
/* ch17/6_percpuvar/6_percpuvar.c */
/* WARNING! This is considered a hack.
* As sched_setaffinity() isn't exported, we don't have access to it
* within this kernel module. So, here we resort to a hack: we use
* kallsyms_lookup_name() (which works when CONFIG_KALLSYMS is defined)
* to retrieve the function pointer, subsequently calling the function
...