Skip to content

Instantly share code, notes, and snippets.

@lixin9311
Created November 25, 2018 08:07
Show Gist options
  • Save lixin9311/19ce3a4cca894daa39337a6725ef9f66 to your computer and use it in GitHub Desktop.
Save lixin9311/19ce3a4cca894daa39337a6725ef9f66 to your computer and use it in GitHub Desktop.
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/percpu.h>
#include <linux/kthread.h>
#include <linux/sched.h>
#include <linux/delay.h>
#include <linux/smp.h>
#define PRINT_PREF "[PERCPU]: "
struct task_struct *thread1, *thread2, *thread3;
void *my_var2;
static int thread_function(void *data) {
while(!kthread_should_stop()) {
int cpu, *local_ptr;
local_ptr = get_cpu_ptr(my_var2);
cpu = smp_processor_id();
(*local_ptr)++;
printk("cpu[%d] = %d\n", cpu, *local_ptr);
put_cpu_ptr(my_var2);
msleep(500);
}
do_exit(0);
}
static int __init my_mod_init(void) {
int *local_ptr;
int cpu;
printk(PRINT_PREF "Entering module.\n");
my_var2 = alloc_percpu(int);
if (!my_var2) {
printk(PRINT_PREF "Failed to allocate var2.\n");
return -1;
}
for (cpu = 0; cpu < NR_CPUS; cpu++) {
local_ptr = per_cpu_ptr(my_var2, cpu);
*local_ptr = 0;
put_cpu();
}
wmb();
thread1 = kthread_run(thread_function, NULL, "percpu-thread1");
thread2 = kthread_run(thread_function, NULL, "percpu-thread2");
thread3 = kthread_run(thread_function, NULL, "percpu-thread3");
return 0;
}
static void __exit my_mod_exit(void) {
kthread_stop(thread1);
kthread_stop(thread2);
kthread_stop(thread3);
free_percpu(my_var2);
printk(PRINT_PREF "Exiting module.\n");
}
module_init(my_mod_init);
module_exit(my_mod_exit);
MODULE_LICENSE("GPL");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment