LKML Archive on lore.kernel.org
 help / color / Atom feed
From: Petr Mladek <pmladek@suse.com>
To: Jiri Kosina <jikos@kernel.org>,
	Josh Poimboeuf <jpoimboe@redhat.com>,
	Miroslav Benes <mbenes@suse.cz>
Cc: Jason Baron <jbaron@akamai.com>,
	Joe Lawrence <joe.lawrence@redhat.com>,
	Evgenii Shatokhin <eshatokhin@virtuozzo.com>,
	live-patching@vger.kernel.org, linux-kernel@vger.kernel.org,
	Petr Mladek <pmladek@suse.com>
Subject: [PATCH v13 02/12] livepatch: Helper macros to define livepatch structures
Date: Mon, 15 Oct 2018 14:37:03 +0200
Message-ID: <20181015123713.25868-3-pmladek@suse.com> (raw)
In-Reply-To: <20181015123713.25868-1-pmladek@suse.com>

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 <pmladek@suse.com>
---
 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


  parent reply index

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-15 12:37 [PATCH v13 00/12] livepatch: Atomic replace feature Petr Mladek
2018-10-15 12:37 ` [PATCH v13 01/12] livepatch: Change void *new_func -> unsigned long new_addr in struct klp_func Petr Mladek
2018-10-15 12:37 ` Petr Mladek [this message]
2018-10-17 18:17   ` [PATCH v13 02/12] livepatch: Helper macros to define livepatch structures Josh Poimboeuf
2018-10-18 11:11     ` Petr Mladek
2018-10-18 12:58       ` Josh Poimboeuf
2018-10-24 11:28         ` Petr Mladek
2018-11-21 12:17           ` Miroslav Benes
2018-10-15 12:37 ` [PATCH v13 03/12] livepatch: Shuffle klp_enable_patch()/klp_disable_patch() code Petr Mladek
2018-10-15 12:37 ` [PATCH v13 04/12] livepatch: Consolidate klp_free functions Petr Mladek
2018-10-17 18:22   ` Josh Poimboeuf
2018-10-18 11:40     ` Petr Mladek
2018-11-21 13:59   ` Miroslav Benes
2018-11-21 14:40     ` Petr Mladek
2018-10-15 12:37 ` [PATCH v13 05/12] livepatch: Refuse to unload only livepatches available during a forced transition Petr Mladek
2018-10-17 18:35   ` Josh Poimboeuf
2018-10-18 12:09     ` Petr Mladek
2018-10-18 13:00       ` Josh Poimboeuf
2018-10-15 12:37 ` [PATCH v13 06/12] livepatch: Simplify API by removing registration step Petr Mladek
2018-10-17 19:06   ` Josh Poimboeuf
2018-10-18 12:33     ` Petr Mladek
2018-10-15 12:37 ` [PATCH v13 07/12] livepatch: Use lists to manage patches, objects and functions Petr Mladek
2018-10-17 20:31   ` Josh Poimboeuf
2018-10-18 12:34     ` Petr Mladek
2018-10-15 12:37 ` [PATCH v13 08/12] livepatch: Add atomic replace Petr Mladek
2018-11-23 12:00   ` Miroslav Benes
2018-10-15 12:37 ` [PATCH v13 09/12] livepatch: Remove Nop structures when unused Petr Mladek
2018-10-17 20:48   ` Josh Poimboeuf
2018-10-18 12:40     ` Petr Mladek
2018-10-15 12:37 ` [PATCH v13 10/12] livepatch: Atomic replace and cumulative patches documentation Petr Mladek
2018-10-15 12:37 ` [PATCH v13 11/12] livepatch: Remove ordering and refuse loading conflicting patches Petr Mladek
2018-10-15 12:37 ` [PATCH v13 12/12] selftests/livepatch: introduce tests Petr Mladek
2018-10-15 19:46   ` Joe Lawrence
2018-10-17 20:48   ` Josh Poimboeuf
2018-10-18 13:34 ` [PATCH v13 00/12] livepatch: Atomic replace feature Josh Poimboeuf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20181015123713.25868-3-pmladek@suse.com \
    --to=pmladek@suse.com \
    --cc=eshatokhin@virtuozzo.com \
    --cc=jbaron@akamai.com \
    --cc=jikos@kernel.org \
    --cc=joe.lawrence@redhat.com \
    --cc=jpoimboe@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=live-patching@vger.kernel.org \
    --cc=mbenes@suse.cz \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

LKML Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/lkml/0 lkml/git/0.git
	git clone --mirror https://lore.kernel.org/lkml/1 lkml/git/1.git
	git clone --mirror https://lore.kernel.org/lkml/2 lkml/git/2.git
	git clone --mirror https://lore.kernel.org/lkml/3 lkml/git/3.git
	git clone --mirror https://lore.kernel.org/lkml/4 lkml/git/4.git
	git clone --mirror https://lore.kernel.org/lkml/5 lkml/git/5.git
	git clone --mirror https://lore.kernel.org/lkml/6 lkml/git/6.git
	git clone --mirror https://lore.kernel.org/lkml/7 lkml/git/7.git
	git clone --mirror https://lore.kernel.org/lkml/8 lkml/git/8.git
	git clone --mirror https://lore.kernel.org/lkml/9 lkml/git/9.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 lkml lkml/ https://lore.kernel.org/lkml \
		linux-kernel@vger.kernel.org
	public-inbox-index lkml

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-kernel


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git