Skip to content

Commit f46e49a

Browse files
pmladekJiri Kosina
authored andcommitted
livepatch: Handle allocation failure in the sample of shadow variable API
klp_shadow_alloc() is not handled in the sample of shadow variable API. It is not strictly necessary because livepatch_fix1_dummy_free() is able to handle the potential failure. But it is an example and it should use the API a clean way. Signed-off-by: Petr Mladek <pmladek@suse.com> Reviewed-by: Joe Lawrence <joe.lawrence@redhat.com> Acked-by: Miroslav Benes <mbenes@suse.cz> Reviewed-by: Kamalesh Babulal <kamalesh@linux.vnet.ibm.com> Signed-off-by: Jiri Kosina <jkosina@suse.cz>
1 parent be6da98 commit f46e49a

1 file changed

Lines changed: 16 additions & 6 deletions

File tree

samples/livepatch/livepatch-shadow-fix1.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ static struct dummy *livepatch_fix1_dummy_alloc(void)
6666
{
6767
struct dummy *d;
6868
int *leak;
69+
int **shadow_leak;
6970

7071
d = kzalloc(sizeof(*d), GFP_KERNEL);
7172
if (!d)
@@ -80,18 +81,27 @@ static struct dummy *livepatch_fix1_dummy_alloc(void)
8081
* pointer to handle resource release.
8182
*/
8283
leak = kzalloc(sizeof(*leak), GFP_KERNEL);
83-
if (!leak) {
84-
kfree(d);
85-
return NULL;
84+
if (!leak)
85+
goto err_leak;
86+
87+
shadow_leak = klp_shadow_alloc(d, SV_LEAK, sizeof(leak), GFP_KERNEL,
88+
shadow_leak_ctor, &leak);
89+
if (!shadow_leak) {
90+
pr_err("%s: failed to allocate shadow variable for the leaking pointer: dummy @ %p, leak @ %p\n",
91+
__func__, d, leak);
92+
goto err_shadow;
8693
}
8794

88-
klp_shadow_alloc(d, SV_LEAK, sizeof(leak), GFP_KERNEL,
89-
shadow_leak_ctor, &leak);
90-
9195
pr_info("%s: dummy @ %p, expires @ %lx\n",
9296
__func__, d, d->jiffies_expire);
9397

9498
return d;
99+
100+
err_shadow:
101+
kfree(leak);
102+
err_leak:
103+
kfree(d);
104+
return NULL;
95105
}
96106

97107
static void livepatch_fix1_dummy_leak_dtor(void *obj, void *shadow_data)

0 commit comments

Comments
 (0)