Received: by 10.213.65.68 with SMTP id h4csp301163imn; Fri, 23 Mar 2018 05:05:32 -0700 (PDT) X-Google-Smtp-Source: AG47ELuf1u28FddPKiDveyhrzH8hS5Jqbda9lcsGeLc9OkNpyMsqvb553NPdkqAacT7xoa6DHZak X-Received: by 2002:a17:902:2c83:: with SMTP id n3-v6mr17991205plb.317.1521806732158; Fri, 23 Mar 2018 05:05:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1521806732; cv=none; d=google.com; s=arc-20160816; b=lnB8XgwzPU+/BHDCSsEM/iCZAWjXABb7d/L7VjNAgY690G26jD51bg2LteNTZAOIP6 lXxwORmHS61n4MJxz6EPvAEHoQHSs6k6VM5cueR/EKFhV1SM+n/qABFgYtEcFsvnicxT cXoaP92dJ1DR63/B3IUctzuJRmIoYq7szJzac1aVpV7k1yL8NcdH4GSyLRhax3qC7hd4 m7HBOEPAjd0m95q1jHjOmEf3/AMdxmresM4lBaPxmrv8UuBw6ic7NvgLF+QhvcJCw29x pepVqaAQTPqniziX9vPXx6enIBSYa0mevXcSLoc+fcMEuGINP2dZxlVWFcM5VmUge7IC /kkA== 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=AMTjbOjSxwPOdF9LWhJIq6vKqyJ7wRrCJxwoN96YCD0=; b=awk2EQV12QG8cp80rpJkue7hgOTfAvT3+gdBlaivEIQSuFvRoychz3NTbKzkb7r+N4 lpTI4Ud9XU7HFeudFFirmXh1H41747xjBMn1+LV18X4adSkGAT7BqGaiHPdwxa2m0gUY jv9Qmc32SPeu+YCqkQZ6S03fG9t6M8kDXXWVlwXrYmCrn/YepqNBzfSC3o9IzgEAZIB0 yswRVBq61hjWfwWHshZJUfzylhQZPJUyhuMQv11sjja58ol7NTRUGa/Qrb17U1qL4jCB amUHWyE2y3gvB91hv2wVSeHcGQ4cA1L4eZYie99K1rzQQY1x3z15cmpM0fhzplylbV0D WeSw== 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 91-v6si8344018plh.296.2018.03.23.05.05.17; Fri, 23 Mar 2018 05:05:32 -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 S1754085AbeCWMC2 (ORCPT + 99 others); Fri, 23 Mar 2018 08:02:28 -0400 Received: from mx2.suse.de ([195.135.220.15]:39567 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753345AbeCWMAv (ORCPT ); Fri, 23 Mar 2018 08:00:51 -0400 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 1C3FDAED0; Fri, 23 Mar 2018 12:00:50 +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 2/8] livepatch: Free only structures with initialized kobject Date: Fri, 23 Mar 2018 13:00:22 +0100 Message-Id: <20180323120028.31451-3-pmladek@suse.com> X-Mailer: git-send-email 2.13.6 In-Reply-To: <20180323120028.31451-1-pmladek@suse.com> References: <20180323120028.31451-1-pmladek@suse.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 no longer patched. We will want to reuse the existing init() and free() functions. Up to now, the free() functions checked a limit and were called only for structures with initialized kobject. But we will want to call them also for structures that were allocated but where the kobject was not initialized yet. This patch removes the limit. It calls klp_free*() functions for all structures. But only the ones with initialized kobject are freed. The handling of un-initialized structures will be added later with the support for dynamic structures. This patch does not change the existing behavior. Signed-off-by: Petr Mladek Cc: Josh Poimboeuf Cc: Jessica Yu Cc: Jiri Kosina Cc: Jason Baron Cc: Miroslav Benes --- kernel/livepatch/core.c | 44 ++++++++++++++++++-------------------------- 1 file changed, 18 insertions(+), 26 deletions(-) diff --git a/kernel/livepatch/core.c b/kernel/livepatch/core.c index 56cb0574c59c..dcce028ecde8 100644 --- a/kernel/livepatch/core.c +++ b/kernel/livepatch/core.c @@ -642,17 +642,15 @@ static struct kobj_type klp_ktype_func = { .sysfs_ops = &kobj_sysfs_ops, }; -/* - * Free all functions' kobjects in the array up to some limit. When limit is - * NULL, all kobjects are freed. - */ -static void klp_free_funcs_limited(struct klp_object *obj, - struct klp_func *limit) +static void klp_free_funcs(struct klp_object *obj) { struct klp_func *func; - for (func = obj->funcs; func->old_name && func != limit; func++) - kobject_put(&func->kobj); + klp_for_each_func(obj, func) { + /* Might be called from klp_init_patch() error path. */ + if (func->kobj.state_initialized) + kobject_put(&func->kobj); + } } /* Clean up when a patched object is unloaded */ @@ -666,24 +664,23 @@ static void klp_free_object_loaded(struct klp_object *obj) func->old_addr = 0; } -/* - * Free all objects' kobjects in the array up to some limit. When limit is - * NULL, all kobjects are freed. - */ -static void klp_free_objects_limited(struct klp_patch *patch, - struct klp_object *limit) +static void klp_free_objects(struct klp_patch *patch) { struct klp_object *obj; - for (obj = patch->objs; obj->funcs && obj != limit; obj++) { - klp_free_funcs_limited(obj, NULL); - kobject_put(&obj->kobj); + klp_for_each_object(patch, obj) { + klp_free_funcs(obj); + + /* Might be called from klp_init_patch() error path. */ + if (obj->kobj.state_initialized) + kobject_put(&obj->kobj); } } static void klp_free_patch(struct klp_patch *patch) { - klp_free_objects_limited(patch, NULL); + klp_free_objects(patch); + if (!list_empty(&patch->list)) list_del(&patch->list); } @@ -780,21 +777,16 @@ static int klp_init_object(struct klp_patch *patch, struct klp_object *obj) klp_for_each_func(obj, func) { ret = klp_init_func(obj, func); if (ret) - goto free; + return ret; } if (klp_is_object_loaded(obj)) { ret = klp_init_object_loaded(patch, obj); if (ret) - goto free; + return ret; } return 0; - -free: - klp_free_funcs_limited(obj, func); - kobject_put(&obj->kobj); - return ret; } static int klp_init_patch(struct klp_patch *patch) @@ -831,7 +823,7 @@ static int klp_init_patch(struct klp_patch *patch) return 0; free: - klp_free_objects_limited(patch, obj); + klp_free_objects(patch); mutex_unlock(&klp_mutex); -- 2.13.6