From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 60660C433F5 for ; Thu, 28 Oct 2021 12:58:19 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 413B9610CB for ; Thu, 28 Oct 2021 12:58:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230340AbhJ1NAo (ORCPT ); Thu, 28 Oct 2021 09:00:44 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:31330 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230243AbhJ1NAk (ORCPT ); Thu, 28 Oct 2021 09:00:40 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1635425893; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=XfV7qkk1FwqGMEmGrThmZ8dj/XAbRI9RjpO/DMpPpZQ=; b=NjOgluGewKMmr66OZEqzZ11w7AsR3vxGq36GewGlvf8b6Hh7x3tENXpb7+7k5LrszTmsdi 4SYoz+Kbff77l5PQf4zWfyHDWhI3TxIv0SBMI9eToHSaXxEV0o6/l8KIFc/PdFaS/abBkc R4PmT6RXEcpKTlILih03mjOAJ+1FsiU= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-334-hrz8r8tBPGWbGTHlSvUk2Q-1; Thu, 28 Oct 2021 08:58:10 -0400 X-MC-Unique: hrz8r8tBPGWbGTHlSvUk2Q-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 4DBDA8144E0; Thu, 28 Oct 2021 12:57:54 +0000 (UTC) Received: from localhost (ovpn-8-40.pek2.redhat.com [10.72.8.40]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9014219D9F; Thu, 28 Oct 2021 12:57:53 +0000 (UTC) From: Ming Lei To: Josh Poimboeuf , Jiri Kosina , Miroslav Benes , Petr Mladek , live-patching@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Greg Kroah-Hartman , Luis Chamberlain , Ming Lei Subject: [PATCH 3/3] livepatch: free klp_patch object synchronously Date: Thu, 28 Oct 2021 20:57:34 +0800 Message-Id: <20211028125734.3134176-4-ming.lei@redhat.com> In-Reply-To: <20211028125734.3134176-1-ming.lei@redhat.com> References: <20211028125734.3134176-1-ming.lei@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org klp_mutex isn't acquired before calling kobject_put(klp_patch), so it is fine to free klp_patch object synchronously. One issue is that enabled store() method, in which the klp_patch kobject itself is deleted & released. However, sysfs has provided APIs for dealing with this corner case, so use sysfs_break_active_protection() and sysfs_unbreak_active_protection() for releasing klp_patch kobject from enabled_store(). Signed-off-by: Ming Lei --- include/linux/livepatch.h | 1 - kernel/livepatch/core.c | 29 ++++++++++------------------- kernel/livepatch/core.h | 2 +- kernel/livepatch/transition.c | 2 +- 4 files changed, 12 insertions(+), 22 deletions(-) diff --git a/include/linux/livepatch.h b/include/linux/livepatch.h index 9712818997c5..4dcebf52fac5 100644 --- a/include/linux/livepatch.h +++ b/include/linux/livepatch.h @@ -169,7 +169,6 @@ struct klp_patch { struct list_head obj_list; bool enabled; bool forced; - struct work_struct free_work; }; #define klp_for_each_object_static(patch, obj) \ diff --git a/kernel/livepatch/core.c b/kernel/livepatch/core.c index 9ede093d699a..7fba5d47ffdd 100644 --- a/kernel/livepatch/core.c +++ b/kernel/livepatch/core.c @@ -337,6 +337,7 @@ static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr, int ret; bool enabled; LIST_HEAD(to_free); + struct kernfs_node *kn = NULL; ret = kstrtobool(buf, &enabled); if (ret) @@ -369,7 +370,11 @@ static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr, out: mutex_unlock(&klp_mutex); - klp_free_patches_async(&to_free); + kn = sysfs_break_active_protection(kobj, &attr->attr); + WARN_ON_ONCE(!kn); + klp_free_patches(&to_free); + if (kn) + sysfs_unbreak_active_protection(kn); if (ret) return ret; @@ -684,32 +689,19 @@ static void klp_free_patch_finish(struct klp_patch *patch) kobject_put(&patch->kobj); } -/* - * The livepatch might be freed from sysfs interface created by the patch. - * This work allows to wait until the interface is destroyed in a separate - * context. - */ -static void klp_free_patch_work_fn(struct work_struct *work) -{ - struct klp_patch *patch = - container_of(work, struct klp_patch, free_work); - - klp_free_patch_finish(patch); -} - -static void klp_free_patch_async(struct klp_patch *patch) +static void klp_free_patch(struct klp_patch *patch) { klp_free_patch_start(patch); - schedule_work(&patch->free_work); + klp_free_patch_finish(patch); } -void klp_free_patches_async(struct list_head *to_free) +void klp_free_patches(struct list_head *to_free) { struct klp_patch *patch, *tmp_patch; list_for_each_entry_safe(patch, tmp_patch, to_free, list) { list_del_init(&patch->list); - klp_free_patch_async(patch); + klp_free_patch(patch); } } @@ -873,7 +865,6 @@ static int klp_init_patch_early(struct klp_patch *patch) kobject_init(&patch->kobj, &klp_ktype_patch); patch->enabled = false; patch->forced = false; - INIT_WORK(&patch->free_work, klp_free_patch_work_fn); klp_for_each_object_static(patch, obj) { if (!obj->funcs) diff --git a/kernel/livepatch/core.h b/kernel/livepatch/core.h index 8ff97745ba40..ea593f370049 100644 --- a/kernel/livepatch/core.h +++ b/kernel/livepatch/core.h @@ -13,7 +13,7 @@ extern struct list_head klp_patches; #define klp_for_each_patch(patch) \ list_for_each_entry(patch, &klp_patches, list) -void klp_free_patches_async(struct list_head *to_free); +void klp_free_patches(struct list_head *to_free); void klp_unpatch_replaced_patches(struct klp_patch *new_patch); void klp_discard_nops(struct klp_patch *new_patch); diff --git a/kernel/livepatch/transition.c b/kernel/livepatch/transition.c index a9ebc9c5db02..3eff5fc0deee 100644 --- a/kernel/livepatch/transition.c +++ b/kernel/livepatch/transition.c @@ -41,7 +41,7 @@ static void klp_transition_work_fn(struct work_struct *work) mutex_unlock(&klp_mutex); - klp_free_patches_async(&to_free); + klp_free_patches(&to_free); } static DECLARE_DELAYED_WORK(klp_transition_work, klp_transition_work_fn); -- 2.31.1