Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
refcount api test
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/refcount.h>
#include <linux/smp.h>
#include <linux/slab.h>
MODULE_DESCRIPTION("refcount api test");
MODULE_AUTHOR("masami256");
MODULE_LICENSE("GPL");
static refcount_t counter1 = REFCOUNT_INIT(1);
static refcount_t *counter2;
static int non_atomic_counter;
static void refcounter_test_run(void *info)
{
pr_info("%s: cpu %d\n", __func__, smp_processor_id());
refcount_inc(&counter1);
refcount_inc(counter2);
non_atomic_counter++;
}
static int refcounter_test_init(void)
{
pr_info("%s start\n", __func__);
counter2 = kmalloc(sizeof(refcount_t), GFP_KERNEL);
if (!counter2)
return -ENOMEM;
refcount_set(counter2, 1);
on_each_cpu(&refcounter_test_run, NULL, 1);
pr_info("counter1: %d\n", refcount_read(&counter1));
pr_info("counter2: %d\n", refcount_read(counter2));
pr_info("non_atomic_counter: %d\n", non_atomic_counter);
return 0;
}
static void refcounter_test_cleanup(void)
{
pr_info("%s bye\n", __func__);
kfree(counter2);
}
module_init(refcounter_test_init);
module_exit(refcounter_test_cleanup);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment