summaryrefslogtreecommitdiffstats
path: root/samples/livepatch/livepatch-shadow-fix1.c
diff options
context:
space:
mode:
authorPetr Mladek <pmladek@suse.com>2018-04-16 07:36:47 -0400
committerJiri Kosina <jkosina@suse.cz>2018-04-17 07:42:48 -0400
commit3b2c77d000fe9f7d02e9e726e00dccf9f92b256f (patch)
treee2586a9d55451fecd9e83deadaebefb717fcbbb2 /samples/livepatch/livepatch-shadow-fix1.c
parente91c2518a5d22a07642f35d85f39001ad379dae4 (diff)
livepatch: Allow to call a custom callback when freeing shadow variables
We might need to do some actions before the shadow variable is freed. For example, we might need to remove it from a list or free some data that it points to. This is already possible now. The user can get the shadow variable by klp_shadow_get(), do the necessary actions, and then call klp_shadow_free(). This patch allows to do it a more elegant way. The user could implement the needed actions in a callback that is passed to klp_shadow_free() as a parameter. The callback usually does reverse operations to the constructor callback that can be called by klp_shadow_*alloc(). It is especially useful for klp_shadow_free_all(). There we need to do these extra actions for each found shadow variable with the given ID. Note that the memory used by the shadow variable itself is still released later by rcu callback. It is needed to protect internal structures that keep all shadow variables. But the destructor is called immediately. The shadow variable must not be access anyway after klp_shadow_free() is called. The user is responsible to protect this any suitable way. Be aware that the destructor is called under klp_shadow_lock. It is the same as for the contructor in klp_shadow_alloc(). Signed-off-by: Petr Mladek <pmladek@suse.com> Acked-by: Josh Poimboeuf <jpoimboe@redhat.com> Acked-by: Miroslav Benes <mbenes@suse.cz> Signed-off-by: Jiri Kosina <jkosina@suse.cz>
Diffstat (limited to 'samples/livepatch/livepatch-shadow-fix1.c')
-rw-r--r--samples/livepatch/livepatch-shadow-fix1.c25
1 files changed, 15 insertions, 10 deletions
diff --git a/samples/livepatch/livepatch-shadow-fix1.c b/samples/livepatch/livepatch-shadow-fix1.c
index 04151c7f2631..49b13553eaae 100644
--- a/samples/livepatch/livepatch-shadow-fix1.c
+++ b/samples/livepatch/livepatch-shadow-fix1.c
@@ -98,9 +98,19 @@ struct dummy *livepatch_fix1_dummy_alloc(void)
98 return d; 98 return d;
99} 99}
100 100
101static void livepatch_fix1_dummy_leak_dtor(void *obj, void *shadow_data)
102{
103 void *d = obj;
104 void **shadow_leak = shadow_data;
105
106 kfree(*shadow_leak);
107 pr_info("%s: dummy @ %p, prevented leak @ %p\n",
108 __func__, d, *shadow_leak);
109}
110
101void livepatch_fix1_dummy_free(struct dummy *d) 111void livepatch_fix1_dummy_free(struct dummy *d)
102{ 112{
103 void **shadow_leak, *leak; 113 void **shadow_leak;
104 114
105 /* 115 /*
106 * Patch: fetch the saved SV_LEAK shadow variable, detach and 116 * Patch: fetch the saved SV_LEAK shadow variable, detach and
@@ -109,15 +119,10 @@ void livepatch_fix1_dummy_free(struct dummy *d)
109 * was loaded.) 119 * was loaded.)
110 */ 120 */
111 shadow_leak = klp_shadow_get(d, SV_LEAK); 121 shadow_leak = klp_shadow_get(d, SV_LEAK);
112 if (shadow_leak) { 122 if (shadow_leak)
113 leak = *shadow_leak; 123 klp_shadow_free(d, SV_LEAK, livepatch_fix1_dummy_leak_dtor);
114 klp_shadow_free(d, SV_LEAK); 124 else
115 kfree(leak);
116 pr_info("%s: dummy @ %p, prevented leak @ %p\n",
117 __func__, d, leak);
118 } else {
119 pr_info("%s: dummy @ %p leaked!\n", __func__, d); 125 pr_info("%s: dummy @ %p leaked!\n", __func__, d);
120 }
121 126
122 kfree(d); 127 kfree(d);
123} 128}
@@ -163,7 +168,7 @@ static int livepatch_shadow_fix1_init(void)
163static void livepatch_shadow_fix1_exit(void) 168static void livepatch_shadow_fix1_exit(void)
164{ 169{
165 /* Cleanup any existing SV_LEAK shadow variables */ 170 /* Cleanup any existing SV_LEAK shadow variables */
166 klp_shadow_free_all(SV_LEAK); 171 klp_shadow_free_all(SV_LEAK, livepatch_fix1_dummy_leak_dtor);
167 172
168 WARN_ON(klp_unregister_patch(&patch)); 173 WARN_ON(klp_unregister_patch(&patch));
169} 174}