Received: by 10.223.176.5 with SMTP id f5csp499764wra; Tue, 6 Feb 2018 02:37:20 -0800 (PST) X-Google-Smtp-Source: AH8x226UDC//ysSScwW+HvU1b2hWrZJ7+QhBq5UkHmaaQnUZWMlee2rCliWKOOKLIyxKlzHMh4vp X-Received: by 2002:a17:902:7897:: with SMTP id q23-v6mr1935653pll.166.1517913440415; Tue, 06 Feb 2018 02:37:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1517913440; cv=none; d=google.com; s=arc-20160816; b=Il4WFJ2ZqcsWxh6IaFJDSZTai7+6GLDM9WTpqM+oaRFkhCX5qOLypnXGXHOUbgxPjL DqMR6peyfGjkRHp9mFvr1Aa0YrrnU0ac6qYt0DuhPkNXcRxDyTStSjEor0wQLICyXNfr 3mcK7RsLUi1koIaw6fEL015mY5QVUAmnRAXZjcnswD353nA0i+JOAYhLHD3ypn7NITwu EF5jSbkGW+yB+UvZJcxkUiGLIv0vkGjQqUc1OOPTGKoodx5YWiJOazehdwy7sjb3kjCg XvGV/XL45eK82/7DIWrI/E1pBXVLVfigi1v6IFvAxAyBqHrnnoVAi19t8xVtRY0+ukXq UxUA== 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=ySV3xwbemdCA9OkaVrJBFeH4z710NNLUhqYzyOHIYQs=; b=ByZU2DVNkDECdmUYvbDjmxQzEWK2nz25RkHkWVv58km7z7K/y0DXR3J3bOhMOZwL5C 3XI9maOaqKr2sd7ON+D9EEc8VDkIGX31AMSnlQIB5cx7tjHTu6QQXY5zd7GGtID0mcFL GE/MouzvNWubtdeJ7oBRokgoa8h11IF0yRKl7Z5LJ5FDh9NOs6fNkzwHT+GgyYITHTfq 2Af96mL7OakcQYNVqkubuB0yTThm8cGbG8GpH3RJwPp+KKqTenHLxkE75xHBoWnRIuYX y/dFccnLOpn0QH1pgfoukMbqNQKvLV2FILiW+wP0kPRhlrUveJv23AmXS9xgn1GFVRIk pFhw== 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 u6-v6si1494428plm.382.2018.02.06.02.37.06; Tue, 06 Feb 2018 02:37:20 -0800 (PST) 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 S1752967AbeBFKgJ (ORCPT + 99 others); Tue, 6 Feb 2018 05:36:09 -0500 Received: from mx2.suse.de ([195.135.220.15]:59976 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752807AbeBFKev (ORCPT ); Tue, 6 Feb 2018 05:34:51 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id D6249AE46; Tue, 6 Feb 2018 10:34:49 +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 v7 4/7] livepatch: Allow to unpatch only functions of the given type Date: Tue, 6 Feb 2018 11:34:21 +0100 Message-Id: <20180206103424.10829-5-pmladek@suse.com> X-Mailer: git-send-email 2.13.6 In-Reply-To: <20180206103424.10829-1-pmladek@suse.com> References: <20180206103424.10829-1-pmladek@suse.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jason Baron We are going to add a feature called atomic replace. It will allow to create a patch that would replace all already registered patches. For this, we will need to dynamically create funcs and objects for functions that are not longer patched. The dynamically allocated objects will not longer be needed once the patch is applied. This patch allows to unpatch functions of the given type. It might cause that the obj->patched flag is true even when some listed functions are not longer patched. This is fine as long as the unpatched funcs' structures are removed right after. It will be the case. Anyway, it is safe. In the worst case, it will not be possible to enable the disabled functions. Signed-off-by: Jason Baron [pmladek@suse.com: Split and modified to use the generic ftype] Signed-off-by: Petr Mladek Cc: Josh Poimboeuf Cc: Jessica Yu Cc: Jiri Kosina Cc: Miroslav Benes --- kernel/livepatch/core.c | 2 +- kernel/livepatch/patch.c | 29 ++++++++++++++++++++++------- kernel/livepatch/patch.h | 4 ++-- kernel/livepatch/transition.c | 2 +- 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/kernel/livepatch/core.c b/kernel/livepatch/core.c index ab3e13b71bfd..fccb603942f1 100644 --- a/kernel/livepatch/core.c +++ b/kernel/livepatch/core.c @@ -1060,7 +1060,7 @@ static void klp_cleanup_module_patches_limited(struct module *mod, pr_notice("reverting patch '%s' on unloading module '%s'\n", patch->mod->name, obj->mod->name); - klp_unpatch_object(obj); + klp_unpatch_object(obj, KLP_FUNC_ANY); klp_post_unpatch_callback(obj); } diff --git a/kernel/livepatch/patch.c b/kernel/livepatch/patch.c index 82d584225dc6..e5f2117e07b6 100644 --- a/kernel/livepatch/patch.c +++ b/kernel/livepatch/patch.c @@ -236,15 +236,29 @@ static int klp_patch_func(struct klp_func *func) return ret; } -void klp_unpatch_object(struct klp_object *obj) +/* + * Remove ftrace handler for the given object and function type. + * + * It keeps obj->patched flag true when any listed function is still patched. + * The caller is responsible for removing the unpatched functions to + * make the flag clean again. + */ +void klp_unpatch_object(struct klp_object *obj, enum klp_func_type ftype) { struct klp_func *func; + bool patched = false; - klp_for_each_func(obj, func) - if (func->patched) + klp_for_each_func(obj, func) { + if (!func->patched) + continue; + + if (klp_is_func_type(func, ftype)) klp_unpatch_func(func); + else + patched = true; + } - obj->patched = false; + obj->patched = patched; } int klp_patch_object(struct klp_object *obj) @@ -258,7 +272,7 @@ int klp_patch_object(struct klp_object *obj) klp_for_each_func(obj, func) { ret = klp_patch_func(func); if (ret) { - klp_unpatch_object(obj); + klp_unpatch_object(obj, KLP_FUNC_ANY); return ret; } } @@ -267,11 +281,12 @@ int klp_patch_object(struct klp_object *obj) return 0; } -void klp_unpatch_objects(struct klp_patch *patch) +/* Removes ftrace handler in all objects for the given function type. */ +void klp_unpatch_objects(struct klp_patch *patch, enum klp_func_type ftype) { struct klp_object *obj; klp_for_each_object(patch, obj) if (obj->patched) - klp_unpatch_object(obj); + klp_unpatch_object(obj, ftype); } diff --git a/kernel/livepatch/patch.h b/kernel/livepatch/patch.h index e72d8250d04b..885f644add4c 100644 --- a/kernel/livepatch/patch.h +++ b/kernel/livepatch/patch.h @@ -28,7 +28,7 @@ struct klp_ops { struct klp_ops *klp_find_ops(unsigned long old_addr); int klp_patch_object(struct klp_object *obj); -void klp_unpatch_object(struct klp_object *obj); -void klp_unpatch_objects(struct klp_patch *patch); +void klp_unpatch_object(struct klp_object *obj, enum klp_func_type ftype); +void klp_unpatch_objects(struct klp_patch *patch, enum klp_func_type ftype); #endif /* _LIVEPATCH_PATCH_H */ diff --git a/kernel/livepatch/transition.c b/kernel/livepatch/transition.c index 7c6631e693bc..6917100fbe79 100644 --- a/kernel/livepatch/transition.c +++ b/kernel/livepatch/transition.c @@ -92,7 +92,7 @@ static void klp_complete_transition(void) * All tasks have transitioned to KLP_UNPATCHED so we can now * remove the new functions from the func_stack. */ - klp_unpatch_objects(klp_transition_patch); + klp_unpatch_objects(klp_transition_patch, KLP_FUNC_ANY); /* * Make sure klp_ftrace_handler() can no longer see functions -- 2.13.6