From patchwork Wed Feb 1 17:31:22 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Torokhov X-Patchwork-Id: 9550345 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 96CEE60425 for ; Wed, 1 Feb 2017 17:33:59 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 86F2D2040D for ; Wed, 1 Feb 2017 17:33:59 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7A5C928442; Wed, 1 Feb 2017 17:33:59 +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.3 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID autolearn=unavailable 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 090CF2040D for ; Wed, 1 Feb 2017 17:33:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753901AbdBARdp (ORCPT ); Wed, 1 Feb 2017 12:33:45 -0500 Received: from mail-pg0-f65.google.com ([74.125.83.65]:36716 "EHLO mail-pg0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753186AbdBARb3 (ORCPT ); Wed, 1 Feb 2017 12:31:29 -0500 Received: by mail-pg0-f65.google.com with SMTP id 75so34735620pgf.3; Wed, 01 Feb 2017 09:31:28 -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=VHD54/bvsS5ob8E01iMIJzX4QJpKOhuvxOOQwMVuRhM=; b=QZvevSagxJ5ZQzJGigOgyfho+Jmv1dDxyDcXhK3U8MNhC3OIBiemnm5FGm339Od4oQ tO06h5C93awP0hHJ9ELM4ORTfo0PttwlgBOmjKVaSVBzy1rjS+CzcnWZolP/0OMpTcdy ewBPCg6xiMzgZIeuhjfayIa59ZKudhv+Ys4NI9nUCrYZz6uZjeeexG5fBSpSrQrxBySV X2M3GxruCRqQOUR7KTEG1oa7BYG/eax49v2ayFI85Ar7YuVO25AWdJJDDKYL7AyNjjmj OjF2ASenNs9C8K8uh1mgcXtAF8++ElX/IlYBXZZaTbiHhSr13X5+021TWhvpz3nVtS3i QzAQ== 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=VHD54/bvsS5ob8E01iMIJzX4QJpKOhuvxOOQwMVuRhM=; b=eKuzDl8as19KwEoYIzmKPP21UzjHGW48rqstK7UallRGFDYIEAi+3xu9pxYd4Nq+ls LSYSTZDLlFUySoIZL8bvwy9Gm8sF4Q3vW/iNBJ7B/wvXIVT+FoK6dAUZA6aU0TmfvmBQ 3o1s37V22z5gMiSmYPuwOo+rMjdNLpi0Zjc17bc2vz7By6XZcF6pCNLd6rNpEw2FBUrq PP4cg5uYvdRnQzL+lFknyk3cuP/0nUU+d5qq38wlnhK4XCAf3OdBtd09q8uvwVcW6Xup l4tmq2da9OYuODltt6HlHoXBTC5AlwpDSHYbxhLB7/w4P+sxQrso/yTCXm/an7MLz5HS PMgQ== X-Gm-Message-State: AIkVDXKDGYPxSUH9it6AI+/lJKTgA/3LJ6y/OMGPvcC+yAZVl4d3j/hrW5kuhi99IauCgg== X-Received: by 10.98.159.141 with SMTP id v13mr5034640pfk.50.1485970288350; Wed, 01 Feb 2017 09:31:28 -0800 (PST) Received: from dtor-ws.mtv.corp.google.com ([172.22.152.34]) by smtp.gmail.com with ESMTPSA id u14sm51170803pfg.18.2017.02.01.09.31.27 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 01 Feb 2017 09:31:27 -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 v3 1/4] device property: export code duplicating array of property entries Date: Wed, 1 Feb 2017 09:31:22 -0800 Message-Id: <20170201173125.40354-2-dmitry.torokhov@gmail.com> X-Mailer: git-send-email 2.11.0.483.g087da7b7c-goog In-Reply-To: <20170201173125.40354-1-dmitry.torokhov@gmail.com> References: <20170201173125.40354-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. Reviewed-by: Andy Shevchenko Reviewed-by: Mika Westerberg Signed-off-by: Dmitry Torokhov --- drivers/base/property.c | 137 +++++++++++++++++++++++++++++------------------ include/linux/property.h | 5 ++ 2 files changed, 90 insertions(+), 52 deletions(-) diff --git a/drivers/base/property.c b/drivers/base/property.c index 43a36d68c3fd..934845fc72e5 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,84 @@ 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); + +/** + * 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; + size_t i, nval; + + for (p = properties; p->name; p++) { + 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); + } + + 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); +} + +/** * pset_copy_set - copies property set * @pset: Property set to copy * @@ -776,32 +821,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..48f154ae7e99 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, struct property_entry *properties); void device_remove_properties(struct device *dev);