device property: export code duplicating array of property entries
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 <dmitry.torokhov@gmail.com> Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
This commit is contained in:
parent
9426998ce6
commit
2d479e1fa2
2 changed files with 134 additions and 65 deletions
|
@ -682,77 +682,64 @@ out:
|
||||||
}
|
}
|
||||||
EXPORT_SYMBOL_GPL(fwnode_property_match_string);
|
EXPORT_SYMBOL_GPL(fwnode_property_match_string);
|
||||||
|
|
||||||
/**
|
static int property_copy_string_array(struct property_entry *dst,
|
||||||
* pset_free_set - releases memory allocated for copied property set
|
const struct property_entry *src)
|
||||||
* @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;
|
char **d;
|
||||||
size_t i, nval;
|
size_t nval = src->length / sizeof(*d);
|
||||||
|
int i;
|
||||||
|
|
||||||
if (!pset)
|
d = kcalloc(nval, sizeof(*d), GFP_KERNEL);
|
||||||
return;
|
if (!d)
|
||||||
|
return -ENOMEM;
|
||||||
|
|
||||||
for (prop = pset->properties; prop->name; prop++) {
|
for (i = 0; i < nval; i++) {
|
||||||
if (prop->is_array) {
|
d[i] = kstrdup(src->pointer.str[i], GFP_KERNEL);
|
||||||
if (prop->is_string && prop->pointer.str) {
|
if (!d[i] && src->pointer.str[i]) {
|
||||||
nval = prop->length / sizeof(const char *);
|
while (--i >= 0)
|
||||||
for (i = 0; i < nval; i++)
|
kfree(d[i]);
|
||||||
kfree(prop->pointer.str[i]);
|
kfree(d);
|
||||||
}
|
return -ENOMEM;
|
||||||
kfree(prop->pointer.raw_data);
|
|
||||||
} else if (prop->is_string) {
|
|
||||||
kfree(prop->value.str);
|
|
||||||
}
|
}
|
||||||
kfree(prop->name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
kfree(pset->properties);
|
dst->pointer.raw_data = d;
|
||||||
kfree(pset);
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int pset_copy_entry(struct property_entry *dst,
|
static int property_entry_copy_data(struct property_entry *dst,
|
||||||
const struct property_entry *src)
|
const struct property_entry *src)
|
||||||
{
|
{
|
||||||
const char * const *s;
|
int error;
|
||||||
char **d;
|
|
||||||
size_t i, nval;
|
|
||||||
|
|
||||||
dst->name = kstrdup(src->name, GFP_KERNEL);
|
dst->name = kstrdup(src->name, GFP_KERNEL);
|
||||||
if (!dst->name)
|
if (!dst->name)
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
|
|
||||||
if (src->is_array) {
|
if (src->is_array) {
|
||||||
if (!src->length)
|
if (!src->length) {
|
||||||
return -ENODATA;
|
error = -ENODATA;
|
||||||
|
goto out_free_name;
|
||||||
|
}
|
||||||
|
|
||||||
if (src->is_string) {
|
if (src->is_string) {
|
||||||
nval = src->length / sizeof(const char *);
|
error = property_copy_string_array(dst, src);
|
||||||
d = kcalloc(nval, sizeof(const char *), GFP_KERNEL);
|
if (error)
|
||||||
if (!d)
|
goto out_free_name;
|
||||||
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;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
dst->pointer.raw_data = kmemdup(src->pointer.raw_data,
|
dst->pointer.raw_data = kmemdup(src->pointer.raw_data,
|
||||||
src->length, GFP_KERNEL);
|
src->length, GFP_KERNEL);
|
||||||
if (!dst->pointer.raw_data)
|
if (!dst->pointer.raw_data) {
|
||||||
return -ENOMEM;
|
error = -ENOMEM;
|
||||||
|
goto out_free_name;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else if (src->is_string) {
|
} else if (src->is_string) {
|
||||||
dst->value.str = kstrdup(src->value.str, GFP_KERNEL);
|
dst->value.str = kstrdup(src->value.str, GFP_KERNEL);
|
||||||
if (!dst->value.str && src->value.str)
|
if (!dst->value.str && src->value.str) {
|
||||||
return -ENOMEM;
|
error = -ENOMEM;
|
||||||
|
goto out_free_name;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
dst->value.raw_data = src->value.raw_data;
|
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;
|
dst->is_string = src->is_string;
|
||||||
|
|
||||||
return 0;
|
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)
|
static struct property_set *pset_copy_set(const struct property_set *pset)
|
||||||
{
|
{
|
||||||
struct property_entry *props;
|
struct property_entry *properties;
|
||||||
struct property_set *p;
|
struct property_set *p;
|
||||||
size_t i, n = 0;
|
|
||||||
|
|
||||||
p = kzalloc(sizeof(*p), GFP_KERNEL);
|
p = kzalloc(sizeof(*p), GFP_KERNEL);
|
||||||
if (!p)
|
if (!p)
|
||||||
return ERR_PTR(-ENOMEM);
|
return ERR_PTR(-ENOMEM);
|
||||||
|
|
||||||
while (pset->properties[n].name)
|
properties = property_entries_dup(pset->properties);
|
||||||
n++;
|
if (IS_ERR(properties)) {
|
||||||
|
|
||||||
p->properties = props = kcalloc(n + 1, sizeof(*props), GFP_KERNEL);
|
|
||||||
if (!p->properties) {
|
|
||||||
kfree(p);
|
kfree(p);
|
||||||
return ERR_PTR(-ENOMEM);
|
return ERR_CAST(properties);
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
p->properties = properties;
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -241,6 +241,11 @@ struct property_entry {
|
||||||
.name = _name_, \
|
.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,
|
int device_add_properties(struct device *dev,
|
||||||
const struct property_entry *properties);
|
const struct property_entry *properties);
|
||||||
void device_remove_properties(struct device *dev);
|
void device_remove_properties(struct device *dev);
|
||||||
|
|
Loading…
Reference in a new issue