-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslab_pdc.c
101 lines (80 loc) · 1.76 KB
/
slab_pdc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
* SLAB-PDC
* A memory allocator with pre-distruction callback (PDC).
* Copyright (C) 2016-2017 - Paolo Missiaggia - All Rights Reserved
*
* Authors:
* Paolo Missiaggia, <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published by
* the Free Software Foundation.
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/moduleparam.h>
#include <linux/version.h>
#include <linux/types.h>
#include <linux/rcupdate.h>
#include "slab_pdc.h"
__always_inline static void
call_rcu_pdc(struct rcu_head *head)
{
pdc_func_f pdc_func;
pdc_t *p = NULL;
p = container_of(head, pdc_t, rcu);
if (!p)
return;
pdc_func = p->pdc_func;
if (pdc_func)
pdc_func(p);
kfree(p);
}
void *
kzalloc_pdc(size_t size, gfp_t flags, pdc_func_f pdc_func)
{
pdc_t *p = NULL;
p = kzalloc(size, flags);
if (unlikely(!p))
return (NULL);
p->pdc_func = pdc_func;
return (p);
}
EXPORT_SYMBOL(kzalloc_pdc);
void
kfree_pdc(const void *obj)
{
pdc_t *p = NULL;
if (!obj)
return;
p = (pdc_t *)obj;
if (p->pdc_func)
call_rcu(&p->rcu, call_rcu_pdc);
else
kfree_rcu(p, rcu);
}
EXPORT_SYMBOL(kfree_pdc);
void
pdc_wait_on_exit(void)
{
return(rcu_barrier());
}
EXPORT_SYMBOL(pdc_wait_on_exit);
int
slab_pdc_init(void)
{
printk(KERN_INFO "[%s] module loaded\n", SLAB_PDC_VERSION_STR);
return 0;
}
void
slab_pdc_exit(void)
{
pdc_wait_on_exit();
printk(KERN_INFO "[%s] module unloaded\n", SLAB_PDC_VERSION_STR);
}
module_init(slab_pdc_init);
module_exit(slab_pdc_exit);
MODULE_AUTHOR("Paolo Missiaggia <[email protected]>");
MODULE_DESCRIPTION("Kernel memory handling facilities");
MODULE_VERSION(SLAB_PDC_VERSION_STR);
MODULE_LICENSE("GPL");