From patchwork Fri Feb 3 01:41:27 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Torokhov X-Patchwork-Id: 9553403 X-Patchwork-Delegate: rjw@sisk.pl Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id C2A4560424 for ; Fri, 3 Feb 2017 01:41:39 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B5D1E271FD for ; Fri, 3 Feb 2017 01:41:39 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id AABB227D85; Fri, 3 Feb 2017 01:41:39 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0E360271FD for ; Fri, 3 Feb 2017 01:41:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752120AbdBCBlh (ORCPT ); Thu, 2 Feb 2017 20:41:37 -0500 Received: from mail-pf0-f196.google.com ([209.85.192.196]:33533 "EHLO mail-pf0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752195AbdBCBle (ORCPT ); Thu, 2 Feb 2017 20:41:34 -0500 Received: by mail-pf0-f196.google.com with SMTP id e4so394495pfg.0; Thu, 02 Feb 2017 17:41:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=SCccvOODLauRUfSrco6hjL6QTYVKbA3TS/TmTAm0r6Y=; b=TEnGiNQRX+ytbl/xPCfXlFX0rOkjnd18PEVD+zrlsF5abo1fFqsJE4zoJhRY7bIPyV OneXbavP9q0aCXde/uM1ImRJ6169ER9j4T9MnYNd3fj6ppwv8gJkgOmiwHKSqHnQYxsG OhvjAKtHCRTi52W5jfKOu9rAWUwXgWMdOP4DtyW3M1Jidr++t/Kr7MMX+CriYlerFWur XzUm0XwB0vk/udUM+NN1dtMbmib5E1txJhdI+F43UspirjEWhP/qzCnypErLxFBcabIk H9qqB6hqIn/Ijw79mLy7PTk2WppZq5L4FzlhJtsZjqheaphwUTtQ91Tz2vdzgFGPvz3h wnzA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=SCccvOODLauRUfSrco6hjL6QTYVKbA3TS/TmTAm0r6Y=; b=QP2ivAW9iN5aLQi/uLx5UURyTJYE+DMFsWLt2HQ5t5LmpYXZYr/Ot0Ter0qwLW6BrU x1YylaNiYmxlwJuV/TZZlfRKy9DJvs8PIclurREbIeZCI4kWkdz6kjiFHuzLNhy/qAnJ vKZ9v9yj3jcKGFs1rJlovhtlcX9XPLG1eh5a+AUe0E3DzvbJ+JeYQLPQH5DKx12b2r2q eqP01hno8Ne4XabaxKFc3vFSjcXCBEUbtlqSqAw4HeWLPrK/tWzGRqVgolsFWel5eUIK hmQ27zVi96OdGyXd7EkAmK8Jz8YXpnsfcgrRd8qT2bCwX2phz9xx6q3tTr7aAkLpOJqd vfQg== X-Gm-Message-State: AIkVDXK8UNbbeXuQj53wz6soBwE5M3iYXbTD9/vz00EDwJguQ6vCii66wqvws1UnlAgKaw== X-Received: by 10.84.194.37 with SMTP id g34mr17150410pld.105.1486086093375; Thu, 02 Feb 2017 17:41:33 -0800 (PST) Received: from dtor-ws.mtv.corp.google.com ([172.22.152.34]) by smtp.gmail.com with ESMTPSA id n28sm1252929pgd.5.2017.02.02.17.41.32 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 02 Feb 2017 17:41:32 -0800 (PST) From: Dmitry Torokhov To: "Rafael J. Wysocki" Cc: linux-acpi@vger.kernel.org, linux-kernel@vger.kernel.org, Andy Shevchenko , Mika Westerberg , Hans de Goede , Wolfram Sang Subject: [PATCH v5 3/4] device property: export code duplicating array of property entries Date: Thu, 2 Feb 2017 17:41:27 -0800 Message-Id: <20170203014128.317-4-dmitry.torokhov@gmail.com> X-Mailer: git-send-email 2.11.0.483.g087da7b7c-goog In-Reply-To: <20170203014128.317-1-dmitry.torokhov@gmail.com> References: <20170203014128.317-1-dmitry.torokhov@gmail.com> Sender: linux-acpi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-acpi@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP When augmenting ACPI-enumerated devices with additional property data based on DMI info, a module has often several potential property sets, with only one being active on a given box. In order to save memory it should be possible to mark everything and __initdata or __initconst, execute DMI match early, and duplicate relevant properties. Then kernel will discard the rest of them. Signed-off-by: Dmitry Torokhov --- drivers/base/property.c | 194 +++++++++++++++++++++++++++++++---------------- include/linux/property.h | 5 ++ 2 files changed, 134 insertions(+), 65 deletions(-) diff --git a/drivers/base/property.c b/drivers/base/property.c index 31b942a29fdc..c458c63e353f 100644 --- a/drivers/base/property.c +++ b/drivers/base/property.c @@ -682,77 +682,64 @@ int fwnode_property_match_string(struct fwnode_handle *fwnode, } EXPORT_SYMBOL_GPL(fwnode_property_match_string); -/** - * pset_free_set - releases memory allocated for copied property set - * @pset: Property set to release - * - * Function takes previously copied property set and releases all the - * memory allocated to it. - */ -static void pset_free_set(struct property_set *pset) +static int property_copy_string_array(struct property_entry *dst, + const struct property_entry *src) { - const struct property_entry *prop; - size_t i, nval; + char **d; + size_t nval = src->length / sizeof(*d); + int i; - if (!pset) - return; + d = kcalloc(nval, sizeof(*d), GFP_KERNEL); + if (!d) + return -ENOMEM; - for (prop = pset->properties; prop->name; prop++) { - if (prop->is_array) { - if (prop->is_string && prop->pointer.str) { - nval = prop->length / sizeof(const char *); - for (i = 0; i < nval; i++) - kfree(prop->pointer.str[i]); - } - kfree(prop->pointer.raw_data); - } else if (prop->is_string) { - kfree(prop->value.str); + for (i = 0; i < nval; i++) { + d[i] = kstrdup(src->pointer.str[i], GFP_KERNEL); + if (!d[i] && src->pointer.str[i]) { + while (--i >= 0) + kfree(d[i]); + kfree(d); + return -ENOMEM; } - kfree(prop->name); } - kfree(pset->properties); - kfree(pset); + dst->pointer.raw_data = d; + return 0; } -static int pset_copy_entry(struct property_entry *dst, - const struct property_entry *src) +static int property_entry_copy_data(struct property_entry *dst, + const struct property_entry *src) { - const char * const *s; - char **d; - size_t i, nval; + int error; dst->name = kstrdup(src->name, GFP_KERNEL); if (!dst->name) return -ENOMEM; if (src->is_array) { - if (!src->length) - return -ENODATA; + if (!src->length) { + error = -ENODATA; + goto out_free_name; + } if (src->is_string) { - nval = src->length / sizeof(const char *); - d = kcalloc(nval, sizeof(const char *), GFP_KERNEL); - if (!d) - return -ENOMEM; - - dst->pointer.raw_data = d; - s = src->pointer.str; - for (i = 0; i < nval; i++) { - d[i] = kstrdup(s[i], GFP_KERNEL); - if (!d[i] && s[i]) - return -ENOMEM; - } + error = property_copy_string_array(dst, src); + if (error) + goto out_free_name; } else { dst->pointer.raw_data = kmemdup(src->pointer.raw_data, src->length, GFP_KERNEL); - if (!dst->pointer.raw_data) - return -ENOMEM; + if (!dst->pointer.raw_data) { + error = -ENOMEM; + goto out_free_name; + } } } else if (src->is_string) { dst->value.str = kstrdup(src->value.str, GFP_KERNEL); - if (!dst->value.str && src->value.str) - return -ENOMEM; + if (!dst->value.str && src->value.str) { + error = -ENOMEM; + goto out_free_name; + } } else { dst->value.raw_data = src->value.raw_data; } @@ -762,6 +749,95 @@ static int pset_copy_entry(struct property_entry *dst, dst->is_string = src->is_string; return 0; + +out_free_name: + kfree(dst->name); + return error; +} + +static void property_entry_free_data(const struct property_entry *p) +{ + size_t i, nval; + + if (p->is_array) { + if (p->is_string && p->pointer.str) { + nval = p->length / sizeof(const char *); + for (i = 0; i < nval; i++) + kfree(p->pointer.str[i]); + } + kfree(p->pointer.raw_data); + } else if (p->is_string) { + kfree(p->value.str); + } + kfree(p->name); +} + +/** + * property_entries_dup - duplicate array of properties + * @properties: array of properties to copy + * + * This function creates a deep copy of the given NULL-terminated array + * of property entries. + */ +struct property_entry * +property_entries_dup(const struct property_entry *properties) +{ + struct property_entry *p; + int i, n = 0; + + while (properties[n].name) + n++; + + p = kcalloc(n + 1, sizeof(*p), GFP_KERNEL); + if (!p) + return ERR_PTR(-ENOMEM); + + for (i = 0; i < n; i++) { + int ret = property_entry_copy_data(&p[i], &properties[i]); + if (ret) { + while (--i >= 0) + property_entry_free_data(&p[i]); + kfree(p); + return ERR_PTR(ret); + } + } + + return p; +} +EXPORT_SYMBOL_GPL(property_entries_dup); + +/** + * property_entries_free - free previously allocated array of properties + * @properties: array of properties to destroy + * + * This function frees given NULL-terminated array of property entries, + * along with their data. + */ +void property_entries_free(const struct property_entry *properties) +{ + const struct property_entry *p; + + for (p = properties; p->name; p++) + property_entry_free_data(p); + + kfree(properties); +} +EXPORT_SYMBOL_GPL(property_entries_free); + +/** + * pset_free_set - releases memory allocated for copied property set + * @pset: Property set to release + * + * Function takes previously copied property set and releases all the + * memory allocated to it. + */ +static void pset_free_set(struct property_set *pset) +{ + if (!pset) + return; + + property_entries_free(pset->properties); + kfree(pset); } /** @@ -776,32 +852,20 @@ static int pset_copy_entry(struct property_entry *dst, */ static struct property_set *pset_copy_set(const struct property_set *pset) { - struct property_entry *props; + struct property_entry *properties; struct property_set *p; - size_t i, n = 0; p = kzalloc(sizeof(*p), GFP_KERNEL); if (!p) return ERR_PTR(-ENOMEM); - while (pset->properties[n].name) - n++; - - p->properties = props = kcalloc(n + 1, sizeof(*props), GFP_KERNEL); - if (!p->properties) { + properties = property_entries_dup(pset->properties); + if (IS_ERR(properties)) { kfree(p); - return ERR_PTR(-ENOMEM); - } - - for (i = 0; i < n; i++) { - int ret = pset_copy_entry(&props[i], - &pset->properties[i]); - if (ret) { - pset_free_set(p); - return ERR_PTR(ret); - } + return ERR_CAST(properties); } + p->properties = properties; return p; } diff --git a/include/linux/property.h b/include/linux/property.h index 7a0a1cce5165..64e3a9c6d95f 100644 --- a/include/linux/property.h +++ b/include/linux/property.h @@ -241,6 +241,11 @@ struct property_entry { .name = _name_, \ } +struct property_entry * +property_entries_dup(const struct property_entry *properties); + +void property_entries_free(const struct property_entry *properties); + int device_add_properties(struct device *dev, const struct property_entry *properties); void device_remove_properties(struct device *dev);