Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp7177926imm; Tue, 28 Aug 2018 07:38:27 -0700 (PDT) X-Google-Smtp-Source: ANB0VdaM4NrvbJXrOsZ3Y/hV+TIJEISXYqOym1IdFSe+Pxm4kzDL0Nr+XZ/4be/+Q7nyZrvzJM1u X-Received: by 2002:a17:902:6a83:: with SMTP id n3-v6mr1859974plk.246.1535467107051; Tue, 28 Aug 2018 07:38:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535467107; cv=none; d=google.com; s=arc-20160816; b=T0mX/5jzSk70kaUWjUTEo7UZ3i77cJg3fO0wYxfXyYU4oEIdyek/zK2awVF/+oERx6 ksGcjTPgCR9NIeFP35r3rK6/zt6RrQC0E0kwhcaIFhjwLF3aC64da8vpFAWfrBiUBnzO FNQ1NOlsxr7otVVONYS5Ec3O17lun4X7wyTK7gboHiILUJPFRk5HlHS+Rsgfse8vuCfm +rtNvDgrDwmpJfDOZm3hw2RjpYe7VG4E0Uel7I7vcbHEgsuz0uO9fLQAsKTF1NuQuUQ0 5j4HTehkrEEuWFD/sk3p8hEmJ9MfJf4omNHbwnxcvV3lf3G5npk6FBI7Qr/OerExm8FA EFPQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=CBHJxBq0+YZvSqWNPc46qjuNJowEMEk9r9ZLKADCKes=; b=uXms0ffg2HMUx9TEbWwEhKxGKvidkIUYOQyM+TpK9M6hqiQ32ed+SxxJqXszCiFt0F 47ijHjlcknyvrq6eeF2nUJYPpj+JhcYyf447QKdBIInWC7qsXrfOkqaki6n8vMKfFOtH K/DnkHgi9yjsm+qjt1kjaVbQPj06cZIN/Onpsw4HBjc9aLnatRkd3Z82q7/fiUVOPMTF XfW5o2JSbDQboabdPMqnMN6Dcslm+YR0PCbfb/WbajYk5Sv+VEPNzqWC3RIaxuvOn9eu Y3gipRB66i1Q32j/zT3z66Tjv/MbEFznxC58wjYGZrn7QEm9WBVYscM98C3MKqPLfzDU s+dw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v61-v6si1103712plb.448.2018.08.28.07.38.11; Tue, 28 Aug 2018 07:38:27 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728352AbeH1S2l (ORCPT + 99 others); Tue, 28 Aug 2018 14:28:41 -0400 Received: from mx2.suse.de ([195.135.220.15]:38936 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726120AbeH1S2k (ORCPT ); Tue, 28 Aug 2018 14:28:40 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay1.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 82A1EB01D; Tue, 28 Aug 2018 14:36:41 +0000 (UTC) From: Petr Mladek To: Jiri Kosina , Josh Poimboeuf , Miroslav Benes Cc: Jason Baron , Joe Lawrence , Jessica Yu , Evgenii Shatokhin , live-patching@vger.kernel.org, linux-kernel@vger.kernel.org, Petr Mladek Subject: [PATCH v12 02/12] livepatch: Helper macros to define livepatch structures Date: Tue, 28 Aug 2018 16:35:53 +0200 Message-Id: <20180828143603.4442-3-pmladek@suse.com> X-Mailer: git-send-email 2.13.7 In-Reply-To: <20180828143603.4442-1-pmladek@suse.com> References: <20180828143603.4442-1-pmladek@suse.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The definition of struct klp_func might be a bit confusing. The original function is defined by name as a string. The new function is defined by name as a function pointer casted to unsigned long. This patch adds helper macros that hide the different types. The functions are defined just by the name. For example: static struct klp_func funcs[] = { { .old_name = "function_A", .new_addr = (unsigned long)livepatch_function_A, }, { .old_name = "function_B", .new_addr = (unsigned long)livepatch_function_B, }, { } }; can be defined as: static struct klp_func funcs[] = { KLP_FUNC(function_A, livepatch_function_A), KLP_FUNC(function_B, livepatch_function_B), KLP_FUNC_END }; Just for completeness, this patch adds similar macros to define struct klp_object. For example, static struct klp_object objs[] = { { /* name being NULL means vmlinux */ .funcs = funcs_vmlinux, }, { .name = "module_A", .funcs = funcs_module_A, }, { .name = "module_B", .funcs = funcs_module_B, }, { } }; can be defined as: static struct klp_object objs[] = { KLP_VMLINUX(funcs_vmlinux), KLP_OBJECT(module_A, funcs_module_A), KLP_OBJECT(module_B, funcs_module_B), KLP_OBJECT_END }; Signed-off-by: Petr Mladek --- include/linux/livepatch.h | 40 ++++++++++++++++++++ samples/livepatch/livepatch-callbacks-demo.c | 55 +++++++++++----------------- samples/livepatch/livepatch-sample.c | 13 +++---- samples/livepatch/livepatch-shadow-fix1.c | 20 ++++------ samples/livepatch/livepatch-shadow-fix2.c | 20 ++++------ 5 files changed, 83 insertions(+), 65 deletions(-) diff --git a/include/linux/livepatch.h b/include/linux/livepatch.h index 817a737b49e8..1163742b27c0 100644 --- a/include/linux/livepatch.h +++ b/include/linux/livepatch.h @@ -152,6 +152,46 @@ struct klp_patch { struct completion finish; }; +#define KLP_FUNC(_old_func, _new_func) { \ + .old_name = #_old_func, \ + .new_addr = (unsigned long)(_new_func), \ + } +#define KLP_FUNC_POS(_old_func, _new_func, _sympos) { \ + .old_name = #_old_func, \ + .new_addr = (unsigned long)_new_func, \ + .sympos = _sympos, \ + } +#define KLP_FUNC_END { } + +#define KLP_OBJECT(_obj, _funcs) { \ + .name = #_obj, \ + .funcs = _funcs, \ + } +#define KLP_OBJECT_CALLBACKS(_obj, _funcs, \ + _pre_patch, _post_patch, \ + _pre_unpatch, _post_unpatch) { \ + .name = #_obj, \ + .funcs = _funcs, \ + .callbacks.pre_patch = _pre_patch, \ + .callbacks.post_patch = _post_patch, \ + .callbacks.pre_unpatch = _pre_unpatch, \ + .callbacks.post_unpatch = _post_unpatch, \ + } +/* name being NULL means vmlinux */ +#define KLP_VMLINUX(_funcs) { \ + .funcs = _funcs, \ + } +#define KLP_VMLINUX_CALLBACKS(_funcs, \ + _pre_patch, _post_patch, \ + _pre_unpatch, _post_unpatch) { \ + .funcs = _funcs, \ + .callbacks.pre_patch = _pre_patch, \ + .callbacks.post_patch = _post_patch, \ + .callbacks.pre_unpatch = _pre_unpatch, \ + .callbacks.post_unpatch = _post_unpatch, \ + } +#define KLP_OBJECT_END { } + #define klp_for_each_object(patch, obj) \ for (obj = patch->objs; obj->funcs || obj->name; obj++) diff --git a/samples/livepatch/livepatch-callbacks-demo.c b/samples/livepatch/livepatch-callbacks-demo.c index 4b1aec474bb7..001a0c672251 100644 --- a/samples/livepatch/livepatch-callbacks-demo.c +++ b/samples/livepatch/livepatch-callbacks-demo.c @@ -147,45 +147,34 @@ static void patched_work_func(struct work_struct *work) } static struct klp_func no_funcs[] = { - { } + KLP_FUNC_END }; static struct klp_func busymod_funcs[] = { - { - .old_name = "busymod_work_func", - .new_addr = (unsigned long)patched_work_func, - }, { } + KLP_FUNC(busymod_work_func, + patched_work_func), + KLP_FUNC_END }; static struct klp_object objs[] = { - { - .name = NULL, /* vmlinux */ - .funcs = no_funcs, - .callbacks = { - .pre_patch = pre_patch_callback, - .post_patch = post_patch_callback, - .pre_unpatch = pre_unpatch_callback, - .post_unpatch = post_unpatch_callback, - }, - }, { - .name = "livepatch_callbacks_mod", - .funcs = no_funcs, - .callbacks = { - .pre_patch = pre_patch_callback, - .post_patch = post_patch_callback, - .pre_unpatch = pre_unpatch_callback, - .post_unpatch = post_unpatch_callback, - }, - }, { - .name = "livepatch_callbacks_busymod", - .funcs = busymod_funcs, - .callbacks = { - .pre_patch = pre_patch_callback, - .post_patch = post_patch_callback, - .pre_unpatch = pre_unpatch_callback, - .post_unpatch = post_unpatch_callback, - }, - }, { } + KLP_VMLINUX_CALLBACKS(no_funcs, + pre_patch_callback, + post_patch_callback, + pre_unpatch_callback, + post_unpatch_callback), + KLP_OBJECT_CALLBACKS(livepatch_callbacks_mod, + no_funcs, + pre_patch_callback, + post_patch_callback, + pre_unpatch_callback, + post_unpatch_callback), + KLP_OBJECT_CALLBACKS(livepatch_callbacks_busymod, + busymod_funcs, + pre_patch_callback, + post_patch_callback, + pre_unpatch_callback, + post_unpatch_callback), + KLP_OBJECT_END }; static struct klp_patch patch = { diff --git a/samples/livepatch/livepatch-sample.c b/samples/livepatch/livepatch-sample.c index e470a052fb77..de30d1ba4791 100644 --- a/samples/livepatch/livepatch-sample.c +++ b/samples/livepatch/livepatch-sample.c @@ -49,17 +49,14 @@ static int livepatch_cmdline_proc_show(struct seq_file *m, void *v) } static struct klp_func funcs[] = { - { - .old_name = "cmdline_proc_show", - .new_addr = (unsigned long)livepatch_cmdline_proc_show, - }, { } + KLP_FUNC(cmdline_proc_show, + livepatch_cmdline_proc_show), + KLP_FUNC_END }; static struct klp_object objs[] = { - { - /* name being NULL means vmlinux */ - .funcs = funcs, - }, { } + KLP_VMLINUX(funcs), + KLP_OBJECT_END }; static struct klp_patch patch = { diff --git a/samples/livepatch/livepatch-shadow-fix1.c b/samples/livepatch/livepatch-shadow-fix1.c index ede0de7abe40..8f337b4a9108 100644 --- a/samples/livepatch/livepatch-shadow-fix1.c +++ b/samples/livepatch/livepatch-shadow-fix1.c @@ -128,21 +128,17 @@ void livepatch_fix1_dummy_free(struct dummy *d) } static struct klp_func funcs[] = { - { - .old_name = "dummy_alloc", - .new_addr = (unsigned long)livepatch_fix1_dummy_alloc, - }, - { - .old_name = "dummy_free", - .new_addr = (unsigned long)livepatch_fix1_dummy_free, - }, { } + KLP_FUNC(dummy_alloc, + livepatch_fix1_dummy_alloc), + KLP_FUNC(dummy_free, + livepatch_fix1_dummy_free), + KLP_FUNC_END }; static struct klp_object objs[] = { - { - .name = "livepatch_shadow_mod", - .funcs = funcs, - }, { } + KLP_OBJECT(livepatch_shadow_mod, + funcs), + KLP_OBJECT_END }; static struct klp_patch patch = { diff --git a/samples/livepatch/livepatch-shadow-fix2.c b/samples/livepatch/livepatch-shadow-fix2.c index 035ee0ef387f..e8c0c0467bc0 100644 --- a/samples/livepatch/livepatch-shadow-fix2.c +++ b/samples/livepatch/livepatch-shadow-fix2.c @@ -105,21 +105,17 @@ void livepatch_fix2_dummy_free(struct dummy *d) } static struct klp_func funcs[] = { - { - .old_name = "dummy_check", - .new_addr = (unsigned long)livepatch_fix2_dummy_check, - }, - { - .old_name = "dummy_free", - .new_addr = (unsigned long)livepatch_fix2_dummy_free, - }, { } + KLP_FUNC(dummy_check, + livepatch_fix2_dummy_check), + KLP_FUNC(dummy_free, + livepatch_fix2_dummy_free), + KLP_FUNC_END }; static struct klp_object objs[] = { - { - .name = "livepatch_shadow_mod", - .funcs = funcs, - }, { } + KLP_OBJECT(livepatch_shadow_mod, + funcs), + KLP_OBJECT_END }; static struct klp_patch patch = { -- 2.13.7