1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Joe Lawrence <joe.lawrence@redhat.com>
4 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
6 #include <linux/module.h>
7 #include <linux/kernel.h>
8 #include <linux/list.h>
9 #include <linux/livepatch.h>
10 #include <linux/slab.h>
13 * Keep a small list of pointers so that we can print address-agnostic
14 * pointer values. Use a rolling integer count to differentiate the values.
15 * Ironically we could have used the shadow variable API to do this, but
16 * let's not lean too heavily on the very code we're testing.
18 static LIST_HEAD(ptr_list
);
22 struct list_head list
;
25 static void free_ptr_list(void)
27 struct shadow_ptr
*sp
, *tmp_sp
;
29 list_for_each_entry_safe(sp
, tmp_sp
, &ptr_list
, list
) {
35 static int ptr_id(void *ptr
)
37 struct shadow_ptr
*sp
;
40 list_for_each_entry(sp
, &ptr_list
, list
) {
45 sp
= kmalloc(sizeof(*sp
), GFP_ATOMIC
);
51 list_add(&sp
->list
, &ptr_list
);
57 * Shadow variable wrapper functions that echo the function and arguments
58 * to the kernel log for testing verification. Don't display raw pointers,
59 * but use the ptr_id() value instead.
61 static void *shadow_get(void *obj
, unsigned long id
)
65 sv
= klp_shadow_get(obj
, id
);
66 pr_info("klp_%s(obj=PTR%d, id=0x%lx) = PTR%d\n",
67 __func__
, ptr_id(obj
), id
, ptr_id(sv
));
72 static void *shadow_alloc(void *obj
, unsigned long id
, size_t size
,
73 gfp_t gfp_flags
, klp_shadow_ctor_t ctor
,
76 int **var
= ctor_data
;
79 sv
= klp_shadow_alloc(obj
, id
, size
, gfp_flags
, ctor
, var
);
80 pr_info("klp_%s(obj=PTR%d, id=0x%lx, size=%zx, gfp_flags=%pGg), ctor=PTR%d, ctor_data=PTR%d = PTR%d\n",
81 __func__
, ptr_id(obj
), id
, size
, &gfp_flags
, ptr_id(ctor
),
82 ptr_id(*var
), ptr_id(sv
));
87 static void *shadow_get_or_alloc(void *obj
, unsigned long id
, size_t size
,
88 gfp_t gfp_flags
, klp_shadow_ctor_t ctor
,
91 int **var
= ctor_data
;
94 sv
= klp_shadow_get_or_alloc(obj
, id
, size
, gfp_flags
, ctor
, var
);
95 pr_info("klp_%s(obj=PTR%d, id=0x%lx, size=%zx, gfp_flags=%pGg), ctor=PTR%d, ctor_data=PTR%d = PTR%d\n",
96 __func__
, ptr_id(obj
), id
, size
, &gfp_flags
, ptr_id(ctor
),
97 ptr_id(*var
), ptr_id(sv
));
102 static void shadow_free(void *obj
, unsigned long id
, klp_shadow_dtor_t dtor
)
104 klp_shadow_free(obj
, id
, dtor
);
105 pr_info("klp_%s(obj=PTR%d, id=0x%lx, dtor=PTR%d)\n",
106 __func__
, ptr_id(obj
), id
, ptr_id(dtor
));
109 static void shadow_free_all(unsigned long id
, klp_shadow_dtor_t dtor
)
111 klp_shadow_free_all(id
, dtor
);
112 pr_info("klp_%s(id=0x%lx, dtor=PTR%d)\n",
113 __func__
, id
, ptr_id(dtor
));
117 /* Shadow variable constructor - remember simple pointer data */
118 static int shadow_ctor(void *obj
, void *shadow_data
, void *ctor_data
)
120 int **sv
= shadow_data
;
121 int **var
= ctor_data
;
127 pr_info("%s: PTR%d -> PTR%d\n",
128 __func__
, ptr_id(sv
), ptr_id(*var
));
133 static void shadow_dtor(void *obj
, void *shadow_data
)
135 int **sv
= shadow_data
;
137 pr_info("%s(obj=PTR%d, shadow_data=PTR%d)\n",
138 __func__
, ptr_id(obj
), ptr_id(sv
));
141 static int test_klp_shadow_vars_init(void)
143 void *obj
= THIS_MODULE
;
145 gfp_t gfp_flags
= GFP_KERNEL
;
147 int var1
, var2
, var3
, var4
;
148 int *pv1
, *pv2
, *pv3
, *pv4
;
149 int **sv1
, **sv2
, **sv3
, **sv4
;
165 * With an empty shadow variable hash table, expect not to find
168 sv
= shadow_get(obj
, id
);
170 pr_info(" got expected NULL result\n");
173 * Allocate a few shadow variables with different <obj> and <id>.
175 sv1
= shadow_alloc(obj
, id
, sizeof(pv1
), gfp_flags
, shadow_ctor
, &pv1
);
179 sv2
= shadow_alloc(obj
+ 1, id
, sizeof(pv2
), gfp_flags
, shadow_ctor
, &pv2
);
183 sv3
= shadow_alloc(obj
, id
+ 1, sizeof(pv3
), gfp_flags
, shadow_ctor
, &pv3
);
188 * Verify we can find our new shadow variables and that they point
191 sv
= shadow_get(obj
, id
);
194 if (sv
== sv1
&& *sv1
== pv1
)
195 pr_info(" got expected PTR%d -> PTR%d result\n",
196 ptr_id(sv1
), ptr_id(*sv1
));
198 sv
= shadow_get(obj
+ 1, id
);
201 if (sv
== sv2
&& *sv2
== pv2
)
202 pr_info(" got expected PTR%d -> PTR%d result\n",
203 ptr_id(sv2
), ptr_id(*sv2
));
204 sv
= shadow_get(obj
, id
+ 1);
207 if (sv
== sv3
&& *sv3
== pv3
)
208 pr_info(" got expected PTR%d -> PTR%d result\n",
209 ptr_id(sv3
), ptr_id(*sv3
));
212 * Allocate or get a few more, this time with the same <obj>, <id>.
213 * The second invocation should return the same shadow var.
215 sv4
= shadow_get_or_alloc(obj
+ 2, id
, sizeof(pv4
), gfp_flags
, shadow_ctor
, &pv4
);
219 sv
= shadow_get_or_alloc(obj
+ 2, id
, sizeof(pv4
), gfp_flags
, shadow_ctor
, &pv4
);
222 if (sv
== sv4
&& *sv4
== pv4
)
223 pr_info(" got expected PTR%d -> PTR%d result\n",
224 ptr_id(sv4
), ptr_id(*sv4
));
227 * Free the <obj=*, id> shadow variables and check that we can no
230 shadow_free(obj
, id
, shadow_dtor
); /* sv1 */
231 sv
= shadow_get(obj
, id
);
233 pr_info(" got expected NULL result\n");
235 shadow_free(obj
+ 1, id
, shadow_dtor
); /* sv2 */
236 sv
= shadow_get(obj
+ 1, id
);
238 pr_info(" got expected NULL result\n");
240 shadow_free(obj
+ 2, id
, shadow_dtor
); /* sv4 */
241 sv
= shadow_get(obj
+ 2, id
);
243 pr_info(" got expected NULL result\n");
246 * We should still find an <id+1> variable.
248 sv
= shadow_get(obj
, id
+ 1);
251 if (sv
== sv3
&& *sv3
== pv3
)
252 pr_info(" got expected PTR%d -> PTR%d result\n",
253 ptr_id(sv3
), ptr_id(*sv3
));
256 * Free all the <id+1> variables, too.
258 shadow_free_all(id
+ 1, shadow_dtor
); /* sv3 */
259 sv
= shadow_get(obj
, id
);
261 pr_info(" shadow_get() got expected NULL result\n");
269 static void test_klp_shadow_vars_exit(void)
273 module_init(test_klp_shadow_vars_init
);
274 module_exit(test_klp_shadow_vars_exit
);
275 MODULE_LICENSE("GPL");
276 MODULE_AUTHOR("Joe Lawrence <joe.lawrence@redhat.com>");
277 MODULE_DESCRIPTION("Livepatch test: shadow variables");