Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp2915434pxb; Mon, 1 Nov 2021 04:33:40 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwX0tVKEqsooe7VMLXaKSuWU2n0C8y1LIDJnIW0+6AkEJo3WDM2M7m0D8OELvMfGykINpXC X-Received: by 2002:a50:d885:: with SMTP id p5mr40641026edj.255.1635766419721; Mon, 01 Nov 2021 04:33:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635766419; cv=none; d=google.com; s=arc-20160816; b=afA+QHvaMnC87BK/5G6e9mnDIUV5kI+WUCrn/mbgNd2yc/CJgsl4ETGnUa6DzAH/68 BL+rlk60PCAmLosr1PLzaako+6N/+cY0g4FubN5Kyl1GxYFBFZrl8KctMfM8WXVnt+NK tj39+0COiw+zqXYM2j5nxiWOhSVQH8Q5eJg6iDAG9q13ld3Sg/QWJ0XPLaOXDWFtUGph 5CQ/Gsrnd9jc7f5ewbwhcxthiwinRut7K8SDv4u1qlEvgrdZF6WwfJ/h7BZ3GkRMX1hz PHiLuQaacTlkScezbcPafgIxi9Jh+mzhkM2j6Cu1LkzcwOjURXEuPvhd2xt5Q3hQjvco tgeQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=S0z5PALRTd6aTedCXiV3DvbVfyqyK3NZQUMnsILpX2g=; b=kNLDeUX0up27T5qb+l37npet2BiAV7vOSlitYZC0kOTK/hNaQsiqDWcNnCGfuPldOw 0zuAovkl6P+lmIvX9nUdhv6qxFC+wK4H5IN1siaYQvwYRljXmhnxnBPzdN5WIbc3NX5D gBazKSnDPma8hijeSuMR9iFB54HdsUGMSysvB3ahDgb0z1RsUgXjSIhX1FUClHuJatXQ RNW0cfD/FrfWPA4KLNhpYzcqMOxTBzoaGnyso6ilvJRfPzqiUT75zrDtbf/+lBoVblv2 4I6W9c47s/VFzbkjy3kd6OtPEpjEDboqT1VjZYgrdAT0W+FJDsWqYEXeI48yiS4ELYdT hgOg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=O6G17uPl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a9si9983197edx.464.2021.11.01.04.33.16; Mon, 01 Nov 2021 04:33:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=O6G17uPl; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231673AbhKALeA (ORCPT + 99 others); Mon, 1 Nov 2021 07:34:00 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:33363 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231485AbhKALeA (ORCPT ); Mon, 1 Nov 2021 07:34:00 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1635766286; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=S0z5PALRTd6aTedCXiV3DvbVfyqyK3NZQUMnsILpX2g=; b=O6G17uPl8uALadMV4vTsAowRMsPCxUo5h+HvcZnn4jR01nYuVoGr+wCJZXxJ7TqsBAtNmY AUXdHBvf/wTYGLqvKCj9Btlf12LgIs2ZI7QgsTmlASqWfnrMb8N54o6seSMBpCbV8Na1jr Ax+L0VhmKUU9Wn9vIuM822IbTP+w2Aw= 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-546-B6TjUZi_NPaAVMjiRq6mkw-1; Mon, 01 Nov 2021 07:31:23 -0400 X-MC-Unique: B6TjUZi_NPaAVMjiRq6mkw-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 6961B100C663; Mon, 1 Nov 2021 11:31:22 +0000 (UTC) Received: from T590 (ovpn-8-37.pek2.redhat.com [10.72.8.37]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0F20E4DA31; Mon, 1 Nov 2021 11:30:45 +0000 (UTC) Date: Mon, 1 Nov 2021 19:30:41 +0800 From: Ming Lei To: Joe Lawrence Cc: Josh Poimboeuf , Jiri Kosina , Miroslav Benes , Petr Mladek , live-patching@vger.kernel.org, linux-kernel@vger.kernel.org, Greg Kroah-Hartman , Luis Chamberlain , ming.lei@redhat.com Subject: Re: [PATCH 0/3] livepatch: cleanup kpl_patch kobject release Message-ID: References: <20211028125734.3134176-1-ming.lei@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, Oct 31, 2021 at 10:26:18PM -0400, Joe Lawrence wrote: > On Sat, Oct 30, 2021 at 12:36:28AM +0800, Ming Lei wrote: > > On Fri, Oct 29, 2021 at 09:51:54AM -0400, Joe Lawrence wrote: > > > On Thu, Oct 28, 2021 at 08:57:31PM +0800, Ming Lei wrote: > > > > Hello, > > > > > > > > The 1st patch moves module_put() to release handler of klp_patch > > > > kobject. > > > > > > > > The 2nd patch changes to free klp_patch and other kobjects without > > > > klp_mutex. > > > > > > > > The 3rd patch switches to synchronous kobject release for klp_patch. > > > > > > > > > > Hi Ming, > > > > > > I gave the patchset a spin on top of linus tree @ 1fc596a56b33 and ended > > > up with a stuck task: > > > > > > Test > > > ---- > > > Enable the livepatch selftests: > > > $ grep CONFIG_TEST_LIVEPATCH .config > > > CONFIG_TEST_LIVEPATCH=m > > > > > > Run a continuous kernel build in the background: > > > $ while (true); do make clean && make -j$(nproc); done > > > > > > While continuously executing the selftests: > > > $ while (true); do make -C tools/testing/selftests/livepatch/ run_tests; done > > > > > > Results > > > ------- > > > > Hello Joe, > > > > Thanks for the test! > > > > Can you replace the 3rd patch with the following one then running the test again? > > > > From 599e96f79aebc388ef3854134312c6039a7884bf Mon Sep 17 00:00:00 2001 > > From: Ming Lei > > Date: Thu, 28 Oct 2021 20:11:23 +0800 > > Subject: [PATCH 3/3] livepatch: free klp_patch object synchronously > > > > 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 | 32 +++++++++++++------------------- > > kernel/livepatch/core.h | 2 +- > > kernel/livepatch/transition.c | 2 +- > > 4 files changed, 15 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..6cfc54f6bdcc 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,14 @@ static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr, > > out: > > mutex_unlock(&klp_mutex); > > > > - klp_free_patches_async(&to_free); > > + if (list_empty(&to_free)) { > > + kn = sysfs_break_active_protection(kobj, &attr->attr); > > + WARN_ON_ONCE(!kn); > > + sysfs_remove_file(kobj, &attr->attr); > > + klp_free_patches(&to_free); > > + if (kn) > > + sysfs_unbreak_active_protection(kn); > > + } > > > > if (ret) > > return ret; > > @@ -684,32 +692,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 +868,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 > > > > > > Hi Ming, > > The previous test runs without hung tasks, but I noticed that is > probably because the selftests quickly wedge. A simple reproducer for > that: > > # (background load) > % while(true); do make clean && make -j$(nproc); done > vs. > # (selftests) > % while(true); do ./tools/testing/selftests/livepatch/test-livepatch.sh || break; done > TEST: basic function patching ... ok > TEST: multiple livepatches ... ok > TEST: atomic replace livepatch ... ok > TEST: basic function patching ... ok > TEST: multiple livepatches ... ok > TEST: atomic replace livepatch ... ok > TEST: basic function patching ... ok > TEST: multiple livepatches ... ERROR: failed to disable livepatch test_klp_livepatch > > % lsmod | grep test_klp > test_klp_livepatch 16384 1 > > % cat /sys/kernel/livepatch/test_klp_livepatch/enabled > 0 > > % rmmod test_klp_livepatch > rmmod: ERROR: Module test_klp_livepatch is in use > > [ 249.870587] ===== TEST: multiple livepatches ===== > [ 249.885520] % modprobe test_klp_livepatch > [ 249.916987] livepatch: enabling patch 'test_klp_livepatch' > [ 249.923131] livepatch: 'test_klp_livepatch': initializing patching transition > [ 249.925575] livepatch: 'test_klp_livepatch': starting patching transition > [ 249.934215] livepatch: 'test_klp_livepatch': completing patching transition > [ 249.934337] livepatch: 'test_klp_livepatch': patching complete > [ 249.946294] test_klp_livepatch: this has been live patched > [ 249.963337] % modprobe test_klp_atomic_replace replace=0 > [ 249.996371] livepatch: enabling patch 'test_klp_atomic_replace' > [ 250.002998] livepatch: 'test_klp_atomic_replace': initializing patching transition > [ 250.005333] livepatch: 'test_klp_atomic_replace': starting patching transition > [ 250.014364] livepatch: 'test_klp_atomic_replace': completing patching transition > [ 250.014471] livepatch: 'test_klp_atomic_replace': patching complete > [ 250.027259] test_klp_livepatch: this has been live patched > [ 250.036050] test_klp_atomic_replace: this has been live patched > [ 250.046347] % echo 0 > /sys/kernel/livepatch/test_klp_atomic_replace/enabled > [ 250.054403] livepatch: 'test_klp_atomic_replace': initializing unpatching transition > [ 250.054574] livepatch: 'test_klp_atomic_replace': starting unpatching transition > [ 251.764849] livepatch: 'test_klp_atomic_replace': completing unpatching transition > [ 251.799266] livepatch: 'test_klp_atomic_replace': unpatching complete > [ 251.911706] % rmmod test_klp_atomic_replace > [ 251.970438] test_klp_livepatch: this has been live patched > [ 251.980347] % echo 0 > /sys/kernel/livepatch/test_klp_livepatch/enabled > [ 251.987936] livepatch: 'test_klp_livepatch': initializing unpatching transition > [ 251.988115] livepatch: 'test_klp_livepatch': starting unpatching transition > [ 251.997033] livepatch: 'test_klp_livepatch': completing unpatching transition > [ 252.027090] livepatch: 'test_klp_livepatch': unpatching complete > [ 313.289932] ERROR: failed to disable livepatch test_klp_livepatch > > In this case, the "failed to disable" msg occurs because the sysfs > interface / module remain present. Hi Joe, Thanks for your test! Hammm, the check of list_empty() in enabled_store() is inverted, please try the V3: https://lore.kernel.org/lkml/20211101112548.3364086-4-ming.lei@redhat.com/T/#u Thanks, Ming