Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752393AbdFNM7R (ORCPT ); Wed, 14 Jun 2017 08:59:17 -0400 Received: from mx2.suse.de ([195.135.220.15]:38673 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751771AbdFNM7P (ORCPT ); Wed, 14 Jun 2017 08:59:15 -0400 Date: Wed, 14 Jun 2017 14:59:13 +0200 From: Petr Mladek To: Joe Lawrence Cc: live-patching@vger.kernel.org, linux-kernel@vger.kernel.org, Josh Poimboeuf , Jessica Yu , Jiri Kosina , Miroslav Benes Subject: Re: [PATCH 1/3] livepatch: introduce shadow variable API Message-ID: <20170614125913.GC15013@pathway.suse.cz> References: <1496341526-19061-1-git-send-email-joe.lawrence@redhat.com> <1496341526-19061-2-git-send-email-joe.lawrence@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1496341526-19061-2-git-send-email-joe.lawrence@redhat.com> User-Agent: Mutt/1.5.21 (2010-09-15) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 4689 Lines: 164 On Thu 2017-06-01 14:25:24, 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. It would be great to explain what shadow variables mean here. Alternatively I would sqash the 2nd patch into this one. People might use git blame to search what this API/code is for and this commit not give much hints. > diff --git a/kernel/livepatch/shadow.c b/kernel/livepatch/shadow.c > new file mode 100644 > index 000000000000..72d5e567dff9 > --- /dev/null > +++ b/kernel/livepatch/shadow.c > @@ -0,0 +1,115 @@ > +/* > + * shadow.c - Shadow Variables > + * > + * Copyright (C) 2014 Josh Poimboeuf > + * Copyright (C) 2014 Seth Jennings > + * Copyright (C) 2017 Joe Lawrence > + * > + * This program is free software; you can redistribute it and/or > + * modify it under the terms of the GNU General Public License > + * as published by the Free Software Foundation; either version 2 > + * of the License, or (at your option) any later version. > + * > + * This program is distributed in the hope that it will be useful, > + * but WITHOUT ANY WARRANTY; without even the implied warranty of > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the > + * GNU General Public License for more details. > + * > + * You should have received a copy of the GNU General Public License > + * along with this program; if not, see . > + */ > + > +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt > + > +#include > +#include > +#include > + > +static DEFINE_HASHTABLE(klp_shadow_hash, 12); > +static DEFINE_SPINLOCK(klp_shadow_lock); > + Please, add comment that will explain the structure members. See include/linux/livepatch.h for explanation. > +struct klp_shadow { > + struct hlist_node node; > + struct rcu_head rcu_head; > + void *obj; > + char *var; > + void *data; I would make the meaning more obvious. What about renaming? var -> key or id data -> shadow_obj or new_obj > +}; > + Also the function would deserve a comment explaining the meaning and parameters. > +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); I would use int or long instead of "char *". You do not know in which context the API will be used. strdup() might be unnecessarily expensive. You could always use enum or #define to get readable names for the key. > + 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); > + > +static void klp_shadow_rcu_free(struct rcu_head *head) > +{ > + struct klp_shadow *shadow; > + > + shadow = container_of(head, struct klp_shadow, rcu_head); > + > + kfree(shadow->var); > + kfree(shadow); If we use int/long instead of the string, we will do not need a custom free function. free_rcu() will work then. > +} > + > +void klp_shadow_detach(void *obj, char *var) > +{ > + unsigned long flags; > + struct klp_shadow *shadow; > + > + spin_lock_irqsave(&klp_shadow_lock, flags); > + > + hash_for_each_possible(klp_shadow_hash, shadow, node, > + (unsigned long)obj) { > + if (shadow->obj == obj && !strcmp(shadow->var, var)) { Do we need to test "shadow->obj == obj" here? If it is not true, there would be a bug in the hashtable implementation or in klp_shadow_attach(). Well, it might make sense to add a consistency check: WARN_ON(shadow->obj != obj); > + hash_del_rcu(&shadow->node); > + spin_unlock_irqrestore(&klp_shadow_lock, flags); > + call_rcu(&shadow->rcu_head, klp_shadow_rcu_free); call_rcu() just queues the request. It does not wait for it. It can be called inside the lock and the code might be easier: hash_del_rcu(&shadow->node); call_rcu(&shadow->rcu_head, klp_shadow_rcu_free); break; } } spin_unlock_irqrestore(&klp_shadow_lock, flags); } Otherwise, I like that the API rather trivial and still useful. Best Regards, Petr