Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp683408imu; Wed, 9 Jan 2019 04:47:00 -0800 (PST) X-Google-Smtp-Source: ALg8bN7VVWdRHL8WS7yLQZ6S5WbwCH8A0J0FKyU0MNnFsdb/S3g6xt01xehqUGbLJU0PThmGKj7P X-Received: by 2002:a62:6799:: with SMTP id t25mr5806972pfj.139.1547038020495; Wed, 09 Jan 2019 04:47:00 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1547038020; cv=none; d=google.com; s=arc-20160816; b=jwb7uyiYIBQOLgB6nMBopKawFIjXY3LNi2nqnaYFk54reJ5DipkMQBbV/blVs74qa2 KhhVo1KuKY6vfnX1rBV3uVun6hBhNBD8I8vK8TSEYN67p0Vy5OKynPrjDaGEmfXiQKiu E+oNmtAvamm5QGeEBnGshNa6gwVMzVWlt4RY8xU21nj9pIXfYeHyQYKyyKdxU+pUfg0Q X+GKEMN5lgWUl62ukol2mxy9MF4LjHsX7iRcU9Wx2RvZHUpWGafiC0tCiQrbjMKATrdb SCxDdbefnJgKHZpah9KvrZouP1meChMmbt696J6CqJFZdLC+03qj3Gem/7++mVgkn352 t21A== 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; bh=O8V2FFjUsaMLwtKrihMmpHfF4bu6rlj4KqJFqWnF57c=; b=p91+G9smB9sq86JGxo/cUgBaa6BYRAD4RZ9b41+8jb98dfPM5PucyYKvyF1VJqaO8l AtOtWa5UJZANhkDFG7hpaoqOsuC+TDXSfkn7FSnq+Me8WRVMm95j6WMjPhR2Jy40o2oH wjbtAPS3s3/5AT5wIQxMxKj5yFCaU3AKoanfgQnCNwxuNcRKoBkAWtXDkgAaqAsQ6nPF 9CaY2OndMI/ALv1EX3XJZk1bqzFZJ+fXa7Pih8kVPiukWhUZKZLFljIAgo8EgONacezp 6mI7/Z7eCsNuJhM9/WDRydkOVeX91pYghSdipvxVdxRO80t8cw4YlhKtVRqWm3nw0W3H W3zg== 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 w8si8801844pgm.467.2019.01.09.04.46.45; Wed, 09 Jan 2019 04:47:00 -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 S1731210AbfAIMnq (ORCPT + 99 others); Wed, 9 Jan 2019 07:43:46 -0500 Received: from mx2.suse.de ([195.135.220.15]:42746 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1730145AbfAIMno (ORCPT ); Wed, 9 Jan 2019 07:43:44 -0500 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 902CBAECF; Wed, 9 Jan 2019 12:43:42 +0000 (UTC) From: Petr Mladek To: Jiri Kosina , Josh Poimboeuf , Miroslav Benes Cc: Jason Baron , Joe Lawrence , Evgenii Shatokhin , live-patching@vger.kernel.org, linux-kernel@vger.kernel.org, Petr Mladek Subject: [PATCH v15 04/11] livepatch: Don't block the removal of patches loaded after a forced transition Date: Wed, 9 Jan 2019 13:43:22 +0100 Message-Id: <20190109124329.21991-5-pmladek@suse.com> X-Mailer: git-send-email 2.13.7 In-Reply-To: <20190109124329.21991-1-pmladek@suse.com> References: <20190109124329.21991-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 give the impression 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 be confusing. 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 Acked-by: Joe Lawrence --- 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 6978785bc059..6a9165d9b090 100644 --- a/include/linux/livepatch.h +++ b/include/linux/livepatch.h @@ -143,6 +143,7 @@ struct klp_object { * @kobj: kobject for sysfs resources * @kobj_added: @kobj has been added and needs freeing * @enabled: the patch is enabled (but operation may be incomplete) + * @forced: was involved in a forced transition * @finish: for waiting till it is safe to remove the patch module */ struct klp_patch { @@ -155,6 +156,7 @@ struct klp_patch { struct kobject kobj; bool kobj_added; bool enabled; + bool forced; struct completion finish; }; diff --git a/kernel/livepatch/core.c b/kernel/livepatch/core.c index 6f0d9095f662..e77c5017ae0c 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; @@ -659,6 +660,7 @@ static int klp_init_patch_early(struct klp_patch *patch) INIT_LIST_HEAD(&patch->list); patch->kobj_added = false; patch->enabled = false; + patch->forced = false; init_completion(&patch->finish); klp_for_each_object(patch, obj) { 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 f27a378ad5e1..a4c921364003 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