Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751399AbdFASZx (ORCPT ); Thu, 1 Jun 2017 14:25:53 -0400 Received: from mx1.redhat.com ([209.132.183.28]:40963 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751339AbdFASZk (ORCPT ); Thu, 1 Jun 2017 14:25:40 -0400 DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com ADC083D959 Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=joe.lawrence@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com ADC083D959 From: Joe Lawrence To: live-patching@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Josh Poimboeuf , Jessica Yu , Jiri Kosina , Miroslav Benes , Petr Mladek Subject: [PATCH 3/3] livepatch: add shadow variable sample program Date: Thu, 1 Jun 2017 14:25:26 -0400 Message-Id: <1496341526-19061-4-git-send-email-joe.lawrence@redhat.com> In-Reply-To: <1496341526-19061-1-git-send-email-joe.lawrence@redhat.com> References: <1496341526-19061-1-git-send-email-joe.lawrence@redhat.com> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Thu, 01 Jun 2017 18:25:35 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2118 Lines: 85 Modify the sample livepatch to demonstrate the shadow variable API. Signed-off-by: Joe Lawrence --- 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 * + * current= count= * * $ 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 * */ +static LIST_HEAD(shadow_list); + +struct task_ctr { + struct list_head list; + int count; +}; + #include +#include 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