From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1750836AbdAWHi4 (ORCPT ); Mon, 23 Jan 2017 02:38:56 -0500 Received: from mail-pf0-f194.google.com ([209.85.192.194]:35802 "EHLO mail-pf0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750725AbdAWHix (ORCPT ); Mon, 23 Jan 2017 02:38:53 -0500 From: Dmitry Torokhov To: "Rafael J. Wysocki" Cc: linux-kernel@vger.kernel.org, Andy Shevchenko , Mika Westerberg , Hans de Goede Subject: [PATCH 1/2] device property: export code duplicating array of property entries Date: Sun, 22 Jan 2017 23:38:48 -0800 Message-Id: <20170123073849.28352-1-dmitry.torokhov@gmail.com> X-Mailer: git-send-email 2.11.0.483.g087da7b7c-goog Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 | 124 +++++++++++++++++++++++++++-------------------- include/linux/property.h | 3 ++ 2 files changed, 75 insertions(+), 52 deletions(-) diff --git a/drivers/base/property.c b/drivers/base/property.c index 43a36d68c3fd..ee22ded63e06 100644 --- a/drivers/base/property.c +++ b/drivers/base/property.c @@ -682,41 +682,8 @@ 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) -{ - const struct property_entry *prop; - size_t i, nval; - - if (!pset) - return; - - 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); - } - kfree(prop->name); - } - - kfree(pset->properties); - kfree(pset); -} - -static int pset_copy_entry(struct property_entry *dst, - const struct property_entry *src) +static int property_entry_copy(struct property_entry *dst, + const struct property_entry *src) { const char **d, **s; size_t i, nval; @@ -765,6 +732,71 @@ static int pset_copy_entry(struct property_entry *dst, } /** + * 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(&p[i], &properties[i]); + if (ret) { + kfree(p); + return ERR_PTR(ret); + } + } + + return p; +} +EXPORT_SYMBOL_GPL(property_entries_dup); + +/** + * 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) +{ + const struct property_entry *prop; + size_t i, nval; + + if (!pset) + return; + + 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); + } + kfree(prop->name); + } + + kfree(pset->properties); + kfree(pset); +} + +/** * pset_copy_set - copies property set * @pset: Property set to copy * @@ -776,32 +808,20 @@ static int pset_copy_entry(struct property_entry *dst, */ static struct property_set *pset_copy_set(const struct property_set *pset) { - const struct property_entry *entry; + 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 = kcalloc(n + 1, sizeof(*entry), 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(&p->properties[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 856e50b2140c..f7fa5891a8c3 100644 --- a/include/linux/property.h +++ b/include/linux/property.h @@ -241,6 +241,9 @@ struct property_entry { .name = _name_, \ } +struct property_entry *property_entries_dup( + const struct property_entry *properties); + int device_add_properties(struct device *dev, struct property_entry *properties); void device_remove_properties(struct device *dev); -- 2.11.0.483.g087da7b7c-goog