Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Petr Mladek and committed by
Jiri Kosina
f46e49a9 be6da984

+16 -6
+16 -6
samples/livepatch/livepatch-shadow-fix1.c
··· 66 66 { 67 67 struct dummy *d; 68 68 int *leak; 69 + int **shadow_leak; 69 70 70 71 d = kzalloc(sizeof(*d), GFP_KERNEL); 71 72 if (!d) ··· 81 80 * pointer to handle resource release. 82 81 */ 83 82 leak = kzalloc(sizeof(*leak), GFP_KERNEL); 84 - if (!leak) { 85 - kfree(d); 86 - return NULL; 87 - } 83 + if (!leak) 84 + goto err_leak; 88 85 89 - klp_shadow_alloc(d, SV_LEAK, sizeof(leak), GFP_KERNEL, 90 - shadow_leak_ctor, &leak); 86 + shadow_leak = klp_shadow_alloc(d, SV_LEAK, sizeof(leak), GFP_KERNEL, 87 + shadow_leak_ctor, &leak); 88 + if (!shadow_leak) { 89 + pr_err("%s: failed to allocate shadow variable for the leaking pointer: dummy @ %p, leak @ %p\n", 90 + __func__, d, leak); 91 + goto err_shadow; 92 + } 91 93 92 94 pr_info("%s: dummy @ %p, expires @ %lx\n", 93 95 __func__, d, d->jiffies_expire); 94 96 95 97 return d; 98 + 99 + err_shadow: 100 + kfree(leak); 101 + err_leak: 102 + kfree(d); 103 + return NULL; 96 104 } 97 105 98 106 static void livepatch_fix1_dummy_leak_dtor(void *obj, void *shadow_data)