mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Joe Lawrence <joe.lawrence@redhat.com>
To: live-patching@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: Josh Poimboeuf <jpoimboe@redhat.com>,
	Jessica Yu <jeyu@redhat.com>, Jiri Kosina <jikos@kernel.org>,
	Miroslav Benes <mbenes@suse.cz>, Petr Mladek <pmladek@suse.com>
Subject: [PATCH 1/3] livepatch: introduce shadow variable API
Date: Thu,  1 Jun 2017 14:25:24 -0400	[thread overview]
Message-ID: <1496341526-19061-2-git-send-email-joe.lawrence@redhat.com> (raw)
In-Reply-To: <1496341526-19061-1-git-send-email-joe.lawrence@redhat.com>

Add three exported API for livepatch modules:

  void *klp_shadow_attach(void *obj, char *var, gfp_t gfp, void *data);
  void klp_shadow_detach(void *obj, char *var);
  void *klp_shadow_get(void *obj, char *var);

that implement "shadow" variables, which allow callers to associate new
shadow fields to existing data structures.

Signed-off-by: Joe Lawrence <joe.lawrence@redhat.com>
---
 include/linux/livepatch.h |   4 ++
 kernel/livepatch/Makefile |   2 +-
 kernel/livepatch/shadow.c | 115 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 120 insertions(+), 1 deletion(-)
 create mode 100644 kernel/livepatch/shadow.c

diff --git a/include/linux/livepatch.h b/include/linux/livepatch.h
index 194991ef9347..a0d068ecf7f8 100644
--- a/include/linux/livepatch.h
+++ b/include/linux/livepatch.h
@@ -164,6 +164,10 @@ static inline bool klp_have_reliable_stack(void)
 	       IS_ENABLED(CONFIG_HAVE_RELIABLE_STACKTRACE);
 }
 
+void *klp_shadow_attach(void *obj, char *var, gfp_t gfp, void *data);
+void klp_shadow_detach(void *obj, char *var);
+void *klp_shadow_get(void *obj, char *var);
+
 #else /* !CONFIG_LIVEPATCH */
 
 static inline int klp_module_coming(struct module *mod) { return 0; }
diff --git a/kernel/livepatch/Makefile b/kernel/livepatch/Makefile
index 2b8bdb1925da..b36ceda6488e 100644
--- a/kernel/livepatch/Makefile
+++ b/kernel/livepatch/Makefile
@@ -1,3 +1,3 @@
 obj-$(CONFIG_LIVEPATCH) += livepatch.o
 
-livepatch-objs := core.o patch.o transition.o
+livepatch-objs := core.o patch.o shadow.o transition.o
diff --git a/kernel/livepatch/shadow.c b/kernel/livepatch/shadow.c
new file mode 100644
index 000000000000..72d5e567dff9
--- /dev/null
+++ b/kernel/livepatch/shadow.c
@@ -0,0 +1,115 @@
+/*
+ * shadow.c - Shadow Variables
+ *
+ * Copyright (C) 2014 Josh Poimboeuf <jpoimboe@redhat.com>
+ * Copyright (C) 2014 Seth Jennings <sjenning@redhat.com>
+ * Copyright (C) 2017 Joe Lawrence <joe.lawrence@redhat.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <linux/hashtable.h>
+#include <linux/slab.h>
+#include <linux/livepatch.h>
+
+static DEFINE_HASHTABLE(klp_shadow_hash, 12);
+static DEFINE_SPINLOCK(klp_shadow_lock);
+
+struct klp_shadow {
+	struct hlist_node node;
+	struct rcu_head rcu_head;
+	void *obj;
+	char *var;
+	void *data;
+};
+
+void *klp_shadow_attach(void *obj, char *var, gfp_t gfp, void *data)
+{
+	unsigned long flags;
+	struct klp_shadow *shadow;
+
+	shadow = kmalloc(sizeof(*shadow), gfp);
+	if (!shadow)
+		return NULL;
+
+	shadow->obj = obj;
+
+	shadow->var = kstrdup(var, gfp);
+	if (!shadow->var) {
+		kfree(shadow);
+		return NULL;
+	}
+
+	shadow->data = data;
+
+	spin_lock_irqsave(&klp_shadow_lock, flags);
+	hash_add_rcu(klp_shadow_hash, &shadow->node, (unsigned long)obj);
+	spin_unlock_irqrestore(&klp_shadow_lock, flags);
+
+	return shadow->data;
+}
+EXPORT_SYMBOL_GPL(klp_shadow_attach);
+
+static void klp_shadow_rcu_free(struct rcu_head *head)
+{
+	struct klp_shadow *shadow;
+
+	shadow = container_of(head, struct klp_shadow, rcu_head);
+
+	kfree(shadow->var);
+	kfree(shadow);
+}
+
+void klp_shadow_detach(void *obj, char *var)
+{
+	unsigned long flags;
+	struct klp_shadow *shadow;
+
+	spin_lock_irqsave(&klp_shadow_lock, flags);
+
+	hash_for_each_possible(klp_shadow_hash, shadow, node,
+			       (unsigned long)obj) {
+		if (shadow->obj == obj && !strcmp(shadow->var, var)) {
+			hash_del_rcu(&shadow->node);
+			spin_unlock_irqrestore(&klp_shadow_lock, flags);
+			call_rcu(&shadow->rcu_head, klp_shadow_rcu_free);
+			return;
+		}
+	}
+
+	spin_unlock_irqrestore(&klp_shadow_lock, flags);
+}
+EXPORT_SYMBOL_GPL(klp_shadow_detach);
+
+void *klp_shadow_get(void *obj, char *var)
+{
+	struct klp_shadow *shadow;
+
+	rcu_read_lock();
+
+	hash_for_each_possible_rcu(klp_shadow_hash, shadow, node,
+				   (unsigned long)obj) {
+		if (shadow->obj == obj && !strcmp(shadow->var, var)) {
+			rcu_read_unlock();
+			return shadow->data;
+		}
+	}
+
+	rcu_read_unlock();
+
+	return NULL;
+}
+EXPORT_SYMBOL_GPL(klp_shadow_get);
-- 
1.8.3.1

  reply	other threads:[~2017-06-01 18:25 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-06-01 18:25 [PATCH 0/3] livepatch: add " Joe Lawrence
2017-06-01 18:25 ` Joe Lawrence [this message]
2017-06-08 16:49   ` [PATCH 1/3] livepatch: introduce " Josh Poimboeuf
2017-06-09 15:36     ` Joe Lawrence
2017-06-09 18:34       ` Josh Poimboeuf
2017-06-12 13:49         ` Joe Lawrence
2017-06-14 12:59   ` Petr Mladek
2017-06-14 13:17     ` Josh Poimboeuf
2017-06-14 14:26       ` Petr Mladek
2017-06-19 16:09     ` Miroslav Benes
2017-06-01 18:25 ` [PATCH 2/3] livepatch: add shadow variable documentation Joe Lawrence
2017-06-14 13:10   ` Petr Mladek
2017-06-01 18:25 ` [PATCH 3/3] livepatch: add shadow variable sample program Joe Lawrence
2017-06-09 18:38   ` Josh Poimboeuf
2017-06-09 19:05     ` Joe Lawrence
2017-06-13 11:00   ` Miroslav Benes
2017-06-13 17:32     ` Joe Lawrence
2017-06-14  7:45       ` Miroslav Benes
2017-06-14 14:21   ` Petr Mladek
2017-06-14 14:57     ` Josh Poimboeuf
2017-06-15 10:59       ` Petr Mladek
2017-06-15 13:49         ` Josh Poimboeuf
     [not found]           ` <20170615143800.d6f5p5bdm4ueecly@redhat.com>
2017-06-15 15:23             ` Josh Poimboeuf
2017-06-19 16:56           ` Miroslav Benes
2017-06-30 20:32             ` Josh Poimboeuf
2017-07-01  6:53               ` Miroslav Benes
2017-06-14 15:15     ` Joe Lawrence
2017-06-19 16:43       ` Miroslav Benes
2017-06-01 20:05 ` [PATCH 0/3] livepatch: add shadow variable API Jiri Kosina
2017-06-01 20:23   ` Joe Lawrence
2017-06-01 20:32     ` Jiri Kosina
2017-06-13 10:19     ` Miroslav Benes
2017-06-13 17:29       ` Joe Lawrence
2017-06-14  8:02         ` Miroslav Benes

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=1496341526-19061-2-git-send-email-joe.lawrence@redhat.com \
    --to=joe.lawrence@redhat.com \
    --cc=jeyu@redhat.com \
    --cc=jikos@kernel.org \
    --cc=jpoimboe@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=live-patching@vger.kernel.org \
    --cc=mbenes@suse.cz \
    --cc=pmladek@suse.com \
    /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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®