Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp7178126imm; Tue, 28 Aug 2018 07:38:39 -0700 (PDT) X-Google-Smtp-Source: ANB0VdYiktn7MzecmpseCuzoSu24GZHT4s3m6noPWdVR4n3u1nyIGcdV9iTOatgr6NQ/B3WPPJhk X-Received: by 2002:a63:e4b:: with SMTP id 11-v6mr355110pgo.320.1535467119027; Tue, 28 Aug 2018 07:38:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535467119; cv=none; d=google.com; s=arc-20160816; b=K7Bmll7zTG8ai7Zb8piYlGkuCGgEsx2MlmwURKUT5DprqJ59IZKEdgjbINrd59EPOy crqIJm55r1OchT43LEg/kY2afjSH8VYnQOyUWs+7SaNIsBe0F8NoPV1HxleK8/c4GgyH WnzUxpGil5fj+YYlR4QHrGv7n8DyYyuYgQSVxLDNPFe2DtbHindb6TfqBL3zPL+gFJNM AhVu6jUcaeIeJw8k68sP2rNj4l2towNxSDRjezpAeIYpJ4q90kN4tBsReYeyAuC+IpA7 xa58+wdBQSV3FpIjtyzg0169rnsGf4TEYDe0W7pYkaoeN7qGc1XvPpgThSEkzkIWhfQX qCEQ== 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=6qbGrGdDfOkHY7k+cxHOf7PRi6jYE3WTvcgFtvSe/LA=; b=FqPGVs8mpdmjTVa0HEM3OZStyLPvMVdhV0ERBV6KMUOJh3vl6Id4QaXO83H5UR/J/d vKARpEyI0ifJAOKOJkMwYOgW82Lkm9SWy4N0njGudqLimthjjOkcmi1HBOOtzzTK38H5 rUglSy0EvRXlSwNo7X0d8jv3a1DJSIJdNmUGzxPSrq6+HNMhpQ04D8mvUPTjBBxkUxzS PooOrH95OEssli0uG2Xip8Nc2+c/ayyjCkxZA2r9pM4R4NrE6MA0JOtledYCSR1BhLLD BjVtQjyS8fxvdBLiH7hyS5OS6zVMVfsfuOc/4G6YzeWwU9gHrnFXW+VfNi5JyFGoSVwW T02A== 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 j37-v6si1206928pgl.432.2018.08.28.07.38.23; Tue, 28 Aug 2018 07:38:38 -0700 (PDT) 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 S1728439AbeH1S2s (ORCPT + 99 others); Tue, 28 Aug 2018 14:28:48 -0400 Received: from mx2.suse.de ([195.135.220.15]:39008 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728373AbeH1S2r (ORCPT ); Tue, 28 Aug 2018 14:28:47 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay1.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 586D5B01C; Tue, 28 Aug 2018 14:36:48 +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 v12 05/12] livepatch: Refuse to unload only livepatches available during a forced transition Date: Tue, 28 Aug 2018 16:35:56 +0200 Message-Id: <20180828143603.4442-6-pmladek@suse.com> X-Mailer: git-send-email 2.13.7 In-Reply-To: <20180828143603.4442-1-pmladek@suse.com> References: <20180828143603.4442-1-pmladek@suse.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org module_put() is currently never called in klp_complete_transition() when klp_force is set. As a result, we might keep the reference count even when klp_enable_patch() fails and klp_cancel_transition() is called. This might make an assumption that a module might get blocked in some strange init state. Fortunately, it is not the case. The reference count is ignored when mod->init fails and erroneous modules are always removed. Anyway, this might make some confusion. Instead, this patch moves the global klp_forced flag into struct klp_patch. As a result, we block only modules that might still be in use after a forced transition. Newly loaded livepatches might be eventually completely removed later. It is not a big deal. But the code is at least consistent with the reality. Signed-off-by: Petr Mladek --- include/linux/livepatch.h | 2 ++ kernel/livepatch/core.c | 4 +++- kernel/livepatch/core.h | 1 + kernel/livepatch/transition.c | 10 +++++----- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/include/linux/livepatch.h b/include/linux/livepatch.h index 22e0767d64b0..86b484b39326 100644 --- a/include/linux/livepatch.h +++ b/include/linux/livepatch.h @@ -138,6 +138,7 @@ struct klp_object { * @list: list node for global list of registered patches * @kobj: kobject for sysfs resources * @enabled: the patch is enabled (but operation may be incomplete) + * @forced: was involved in a forced transition * @wait_free: wait until the patch is freed * @finish: for waiting till it is safe to remove the patch module */ @@ -150,6 +151,7 @@ struct klp_patch { struct list_head list; struct kobject kobj; bool enabled; + bool forced; bool wait_free; struct completion finish; }; diff --git a/kernel/livepatch/core.c b/kernel/livepatch/core.c index 3ca404545150..18af1dc0e199 100644 --- a/kernel/livepatch/core.c +++ b/kernel/livepatch/core.c @@ -45,7 +45,8 @@ */ DEFINE_MUTEX(klp_mutex); -static LIST_HEAD(klp_patches); +/* Registered patches */ +LIST_HEAD(klp_patches); static struct kobject *klp_root_kobj; @@ -660,6 +661,7 @@ static int klp_init_patch(struct klp_patch *patch) mutex_lock(&klp_mutex); patch->enabled = false; + patch->forced = false; INIT_LIST_HEAD(&patch->list); init_completion(&patch->finish); diff --git a/kernel/livepatch/core.h b/kernel/livepatch/core.h index 48a83d4364cf..d0cb5390e247 100644 --- a/kernel/livepatch/core.h +++ b/kernel/livepatch/core.h @@ -5,6 +5,7 @@ #include extern struct mutex klp_mutex; +extern struct list_head klp_patches; static inline bool klp_is_object_loaded(struct klp_object *obj) { diff --git a/kernel/livepatch/transition.c b/kernel/livepatch/transition.c index 982a2e4c6120..30a28634c88c 100644 --- a/kernel/livepatch/transition.c +++ b/kernel/livepatch/transition.c @@ -33,8 +33,6 @@ struct klp_patch *klp_transition_patch; static int klp_target_state = KLP_UNDEFINED; -static bool klp_forced = false; - /* * This work can be performed periodically to finish patching or unpatching any * "straggler" tasks which failed to transition in the first attempt. @@ -137,10 +135,10 @@ static void klp_complete_transition(void) klp_target_state == KLP_PATCHED ? "patching" : "unpatching"); /* - * klp_forced set implies unbounded increase of module's ref count if + * patch->forced set implies unbounded increase of module's ref count if * the module is disabled/enabled in a loop. */ - if (!klp_forced && klp_target_state == KLP_UNPATCHED) + if (!klp_transition_patch->forced && klp_target_state == KLP_UNPATCHED) module_put(klp_transition_patch->mod); klp_target_state = KLP_UNDEFINED; @@ -620,6 +618,7 @@ void klp_send_signals(void) */ void klp_force_transition(void) { + struct klp_patch *patch; struct task_struct *g, *task; unsigned int cpu; @@ -633,5 +632,6 @@ void klp_force_transition(void) for_each_possible_cpu(cpu) klp_update_patch_state(idle_task(cpu)); - klp_forced = true; + list_for_each_entry(patch, &klp_patches, list) + patch->forced = true; } -- 2.13.7