Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751832AbdFIPga (ORCPT ); Fri, 9 Jun 2017 11:36:30 -0400 Received: from mx1.redhat.com ([209.132.183.28]:54226 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751725AbdFIPg3 (ORCPT ); Fri, 9 Jun 2017 11:36:29 -0400 DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 6C4CD8047F Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=joe.lawrence@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 6C4CD8047F Subject: Re: [PATCH 1/3] livepatch: introduce shadow variable API To: Josh Poimboeuf References: <1496341526-19061-1-git-send-email-joe.lawrence@redhat.com> <1496341526-19061-2-git-send-email-joe.lawrence@redhat.com> <20170608164923.mkbiwporqw4i2kxy@treble> Cc: live-patching@vger.kernel.org, linux-kernel@vger.kernel.org, Jessica Yu , Jiri Kosina , Miroslav Benes , Petr Mladek From: Joe Lawrence Organization: Red Hat Message-ID: Date: Fri, 9 Jun 2017 11:36:27 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 MIME-Version: 1.0 In-Reply-To: <20170608164923.mkbiwporqw4i2kxy@treble> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Fri, 09 Jun 2017 15:36:28 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1874 Lines: 59 On 06/08/2017 12:49 PM, Josh Poimboeuf wrote: > On Thu, Jun 01, 2017 at 02:25:24PM -0400, Joe Lawrence wrote: >> Add three exported API for livepatch modules: >> >> void *klp_shadow_attach(void *obj, char *var, gfp_t gfp, void *data); >> void klp_shadow_detach(void *obj, char *var); >> void *klp_shadow_get(void *obj, char *var); >> >> that implement "shadow" variables, which allow callers to associate new >> shadow fields to existing data structures. >> >> Signed-off-by: Joe Lawrence > > Overall the patch looks good to me. It's a simple API but we've found > it to be very useful for certain patches. > > One comment below: > >> +void *klp_shadow_attach(void *obj, char *var, gfp_t gfp, void *data) >> +{ >> + unsigned long flags; >> + struct klp_shadow *shadow; >> + >> + shadow = kmalloc(sizeof(*shadow), gfp); >> + if (!shadow) >> + return NULL; >> + >> + shadow->obj = obj; >> + >> + shadow->var = kstrdup(var, gfp); >> + if (!shadow->var) { >> + kfree(shadow); >> + return NULL; >> + } >> + >> + shadow->data = data; >> + >> + spin_lock_irqsave(&klp_shadow_lock, flags); >> + hash_add_rcu(klp_shadow_hash, &shadow->node, (unsigned long)obj); >> + spin_unlock_irqrestore(&klp_shadow_lock, flags); >> + >> + return shadow->data; >> +} >> +EXPORT_SYMBOL_GPL(klp_shadow_attach); > > I wonder if we should worry about people misusing the API by calling > klp_shadow_attach() for a shadow variable that already exists. Maybe we > should add a check and return NULL if it already exists. > I don't think the API (the shadow or the underlying hash table calls) currently protects against double-adds... adding a check to do so would probably need to occur with the klp_shadow_lock to protect against concurrent detach calls. I could implement this protection in a v2, or leave it up to the caller. What do you think? -- Joe