Modify the sample livepatch to demonstrate the shadow variable API.

Signed-off-by: Joe Lawrence <joe.lawre...@redhat.com>
---
 samples/livepatch/livepatch-sample.c | 39 +++++++++++++++++++++++++++++++++++-
 1 file changed, 38 insertions(+), 1 deletion(-)

diff --git a/samples/livepatch/livepatch-sample.c 
b/samples/livepatch/livepatch-sample.c
index 84795223f15f..e0236750cefb 100644
--- a/samples/livepatch/livepatch-sample.c
+++ b/samples/livepatch/livepatch-sample.c
@@ -25,26 +25,57 @@
 
 /*
  * This (dumb) live patch overrides the function that prints the
- * kernel boot cmdline when /proc/cmdline is read.
+ * kernel boot cmdline when /proc/cmdline is read.  It also
+ * demonstrates a contrived shadow-variable usage.
  *
  * Example:
  *
  * $ cat /proc/cmdline
  * <your cmdline>
+ * current=<current task pointer> count=<shadow variable counter>
  *
  * $ insmod livepatch-sample.ko
  * $ cat /proc/cmdline
  * this has been live patched
+ * current=ffff8800331c9540 count=1
+ * $ cat /proc/cmdline
+ * this has been live patched
+ * current=ffff8800331c9540 count=2
  *
  * $ echo 0 > /sys/kernel/livepatch/livepatch_sample/enabled
  * $ cat /proc/cmdline
  * <your cmdline>
  */
 
+static LIST_HEAD(shadow_list);
+
+struct task_ctr {
+       struct list_head list;
+       int count;
+};
+
 #include <linux/seq_file.h>
+#include <linux/slab.h>
 static int livepatch_cmdline_proc_show(struct seq_file *m, void *v)
 {
+       struct task_ctr *nd;
+
+       nd = klp_shadow_get(current, "task_ctr");
+       if (!nd) {
+               nd = kzalloc(sizeof(*nd), GFP_KERNEL);
+               if (nd) {
+                       list_add(&nd->list, &shadow_list);
+                       klp_shadow_attach(current, "task_ctr", GFP_KERNEL, nd);
+               }
+       }
+
        seq_printf(m, "%s\n", "this has been live patched");
+
+       if (nd) {
+               nd->count++;
+               seq_printf(m, "current=%p count=%d\n", current, nd->count);
+       }
+
        return 0;
 }
 
@@ -99,6 +130,12 @@ static int livepatch_init(void)
 
 static void livepatch_exit(void)
 {
+       struct task_ctr *nd, *tmp;
+
+       list_for_each_entry_safe(nd, tmp, &shadow_list, list) {
+               list_del(&nd->list);
+               kfree(nd);
+       }
        WARN_ON(klp_unregister_patch(&patch));
 }
 
-- 
1.8.3.1

Reply via email to