Received: by 2002:ac0:a582:0:0:0:0:0 with SMTP id m2-v6csp1031077imm; Wed, 17 Oct 2018 12:08:04 -0700 (PDT) X-Google-Smtp-Source: ACcGV62SLcNNpbdEdRuIsTUmp4Qc1Lac9aOFUsBbJcdewNOWLcf9Au6jITsG0wTVwFf0BFzwjcCc X-Received: by 2002:a17:902:e111:: with SMTP id cc17-v6mr26973281plb.175.1539803284441; Wed, 17 Oct 2018 12:08:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539803284; cv=none; d=google.com; s=arc-20160816; b=AOjgI2XgXQ1ATUFAaV2L6XYeUy3gcipEuyg7H+JMABNbgivZqGYsnO8KKwnizkemS8 /wHcsS0s6HU8ArsZiw6INgxUGPdjeMBXvFuvo+PUwed6bk+5Mr3RlWNZ30+Jlo5RwPcO QGlKpDPKpAjceUIXT6kOYMjxR0iTmzwZxspRrapQygtCsnKLctUSdmxcmsOu7IfVO+/j jSddDQFnGRyXY6mnpzCgOhbn0Qg5uYhaFUurVk2hwGzXMSgndXX7cYLDLFL98ob0OPBZ X27Y3YZTnnI8suVgQkeICRtgSAyrhSrh6w0BBRhS2NYoRPE5x8j9TvoqprkHko8463J2 SWXg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=49mp/hFVla06kJ51ObWuhnsawKyKOQC7+X8DgMvI8YE=; b=PUiu4+vuVftcAwixGxyfPhwml19xQ8uMLCgc9QKYxDyw76jZgP3zMxtkN2SWV7QD5Z xP+jI+6rXsubEj+gW6g8DFvWGOxhrDNODesoeOdBQ80UOpWsriWvOxY0hzEj0PfTviC/ bvj003MFvb90XK+yEm+G7FSE8D7RHmuAWqzw9MFn9JJpm8ZJjizXDN9uiB2ct1p55AOE 75d5VT2HKgAnFv4cD4n8n8n0nTRIcsyChZTio4qViYy9AvrRUaFKZ89MagDTIu4qXX+L iQDP3RQUpHLJVdh7w8XEmHZDNHTmXCburSucy9t/LZEhwQEcL/4dugS2uEV2vtCogmR/ TTpQ== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p67-v6si1260051pfp.68.2018.10.17.12.07.48; Wed, 17 Oct 2018 12:08:04 -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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728330AbeJRDEF (ORCPT + 99 others); Wed, 17 Oct 2018 23:04:05 -0400 Received: from mx1.redhat.com ([209.132.183.28]:56162 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727128AbeJRDEF (ORCPT ); Wed, 17 Oct 2018 23:04:05 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 5D133804E3; Wed, 17 Oct 2018 19:07:01 +0000 (UTC) Received: from treble (ovpn-122-46.rdu2.redhat.com [10.10.122.46]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 3311D8AB40; Wed, 17 Oct 2018 19:06:59 +0000 (UTC) Date: Wed, 17 Oct 2018 14:06:57 -0500 From: Josh Poimboeuf To: Petr Mladek Cc: Jiri Kosina , Miroslav Benes , Jason Baron , Joe Lawrence , Evgenii Shatokhin , live-patching@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v13 06/12] livepatch: Simplify API by removing registration step Message-ID: <20181017190657.dv3kwx467brzhdnz@treble> References: <20181015123713.25868-1-pmladek@suse.com> <20181015123713.25868-7-pmladek@suse.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20181015123713.25868-7-pmladek@suse.com> User-Agent: NeoMutt/20180716 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Wed, 17 Oct 2018 19:07:01 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Oct 15, 2018 at 02:37:07PM +0200, Petr Mladek wrote: > @@ -319,96 +316,66 @@ forced it is guaranteed that no task sleeps or runs in the old code. > 5. Livepatch life-cycle > ======================= > > -Livepatching defines four basic operations that define the life cycle of each > -live patch: registration, enabling, disabling and unregistration. There are > -several reasons why it is done this way. > +Livepatches get automatically enabled when the respective module is loaded. (only true if the module enables the patch in its init function) > @@ -502,6 +483,9 @@ static void klp_free_objects(struct klp_patch *patch) > } > > /* > + * The synchronous variant is needed when the patch is freed in > + * the klp_enable_patch() error paths. > + * Hm? This comment seems confusingly out of context. > @@ -528,6 +512,23 @@ static void klp_free_patch_finish(struct klp_patch *patch) > kobject_put(&patch->kobj); > wait_for_completion(&patch->finish); > } > + > + /* Put the module after the last access to struct klp_patch. */ > + if (patch->module_put) > + module_put(patch->mod); > +} > + > +/* > + * 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_fn(struct work_struct *work) To clarify that it's a work function, how about calling it "klp_free_patch_work_fn"? > static int klp_init_func(struct klp_object *obj, struct klp_func *func) > @@ -642,116 +643,38 @@ static int klp_init_patch(struct klp_patch *patch) > struct klp_object *obj; > int ret; > > - if (!patch->objs) > - return -EINVAL; > - > - mutex_lock(&klp_mutex); > - > patch->enabled = false; > - patch->forced = false; > + patch->module_put = false; > INIT_LIST_HEAD(&patch->list); > + INIT_WORK(&patch->free_work, klp_free_patch_fn); > init_completion(&patch->finish); > > + if (!patch->objs) > + return -EINVAL; > + > + /* > + * A reference is taken on the patch module to prevent it from being > + * unloaded. > + */ > + if (!try_module_get(patch->mod)) > + return -ENODEV; This comment isn't needed. It describes what try_module_get() does, which is common kernel knowledge. > + patch->module_put = true; The naming and semantics of the 'module_put' field are a little confusing. It's false in two cases: 1) try_module_get() failure 2) forced patch Maybe we can get rid of the need for the first case by moving the try_module_get() call to klp_enable_patch(), before calling klp_init_lists(). Then klp_free_patch_finish() will always be called with a module reference, so it doesn't have to check the 'module_put' field. We'd still need it for the force case, but then it can just be called 'forced' again. > --- a/samples/livepatch/livepatch-callbacks-demo.c > +++ b/samples/livepatch/livepatch-callbacks-demo.c > @@ -184,22 +184,11 @@ static struct klp_patch patch = { > > static int livepatch_callbacks_demo_init(void) > { > - int ret; > - > - ret = klp_register_patch(&patch); > - if (ret) > - return ret; > - ret = klp_enable_patch(&patch); > - if (ret) { > - WARN_ON(klp_unregister_patch(&patch)); > - return ret; > - } > - return 0; > + return klp_enable_patch(&patch); > } > > static void livepatch_callbacks_demo_exit(void) > { > - WARN_ON(klp_unregister_patch(&patch)); > } This module exit function is no longer needed. > > module_init(livepatch_callbacks_demo_init); > diff --git a/samples/livepatch/livepatch-sample.c b/samples/livepatch/livepatch-sample.c > index de30d1ba4791..88afb708a48d 100644 > --- a/samples/livepatch/livepatch-sample.c > +++ b/samples/livepatch/livepatch-sample.c > @@ -66,22 +66,11 @@ static struct klp_patch patch = { > > static int livepatch_init(void) > { > - int ret; > - > - ret = klp_register_patch(&patch); > - if (ret) > - return ret; > - ret = klp_enable_patch(&patch); > - if (ret) { > - WARN_ON(klp_unregister_patch(&patch)); > - return ret; > - } > - return 0; > + return klp_enable_patch(&patch); > } > > static void livepatch_exit(void) > { > - WARN_ON(klp_unregister_patch(&patch)); > } Ditto. -- Josh