Commit af6e2b5b authored by David S. Miller's avatar David S. Miller

Merge branch 'ptp-attribute-cleanup'

Dmitry Torokhov says:

====================
PTP attribute handling cleanup

PTP core was creating some attributes, such as "period" and "fifo", and the
entire "pins" attribute group, after creating class deevice, which creates
a race for userspace: uevent may arrive before all attributes are created.

This series of patches switches PTP to use is_visible() to control
visibility of attributes in a group, and device_create_with_groups() to
ensure that attributes are created before we notify userspace of a new
device.

v2:
- added Richard's acked-by to patch #1
- removed use of kmalloc_array in favor of kcalloc in patch #2 at
  Richard's request
- added a cover letter

v1:
- initial patch set
====================
Acked-by: default avatarRichard Cochran <richardcochran@gmail.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parents 01f0f425 85a66e55
...@@ -221,18 +221,17 @@ struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info, ...@@ -221,18 +221,17 @@ struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
mutex_init(&ptp->pincfg_mux); mutex_init(&ptp->pincfg_mux);
init_waitqueue_head(&ptp->tsev_wq); init_waitqueue_head(&ptp->tsev_wq);
err = ptp_populate_pin_groups(ptp);
if (err)
goto no_pin_groups;
/* Create a new device in our class. */ /* Create a new device in our class. */
ptp->dev = device_create(ptp_class, parent, ptp->devid, ptp, ptp->dev = device_create_with_groups(ptp_class, parent, ptp->devid,
ptp, ptp->pin_attr_groups,
"ptp%d", ptp->index); "ptp%d", ptp->index);
if (IS_ERR(ptp->dev)) if (IS_ERR(ptp->dev))
goto no_device; goto no_device;
dev_set_drvdata(ptp->dev, ptp);
err = ptp_populate_sysfs(ptp);
if (err)
goto no_sysfs;
/* Register a new PPS source. */ /* Register a new PPS source. */
if (info->pps) { if (info->pps) {
struct pps_source_info pps; struct pps_source_info pps;
...@@ -260,10 +259,10 @@ struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info, ...@@ -260,10 +259,10 @@ struct ptp_clock *ptp_clock_register(struct ptp_clock_info *info,
if (ptp->pps_source) if (ptp->pps_source)
pps_unregister_source(ptp->pps_source); pps_unregister_source(ptp->pps_source);
no_pps: no_pps:
ptp_cleanup_sysfs(ptp);
no_sysfs:
device_destroy(ptp_class, ptp->devid); device_destroy(ptp_class, ptp->devid);
no_device: no_device:
ptp_cleanup_pin_groups(ptp);
no_pin_groups:
mutex_destroy(&ptp->tsevq_mux); mutex_destroy(&ptp->tsevq_mux);
mutex_destroy(&ptp->pincfg_mux); mutex_destroy(&ptp->pincfg_mux);
ida_simple_remove(&ptp_clocks_map, index); ida_simple_remove(&ptp_clocks_map, index);
...@@ -282,8 +281,9 @@ int ptp_clock_unregister(struct ptp_clock *ptp) ...@@ -282,8 +281,9 @@ int ptp_clock_unregister(struct ptp_clock *ptp)
/* Release the clock's resources. */ /* Release the clock's resources. */
if (ptp->pps_source) if (ptp->pps_source)
pps_unregister_source(ptp->pps_source); pps_unregister_source(ptp->pps_source);
ptp_cleanup_sysfs(ptp);
device_destroy(ptp_class, ptp->devid); device_destroy(ptp_class, ptp->devid);
ptp_cleanup_pin_groups(ptp);
posix_clock_unregister(&ptp->clock); posix_clock_unregister(&ptp->clock);
return 0; return 0;
......
...@@ -54,6 +54,8 @@ struct ptp_clock { ...@@ -54,6 +54,8 @@ struct ptp_clock {
struct device_attribute *pin_dev_attr; struct device_attribute *pin_dev_attr;
struct attribute **pin_attr; struct attribute **pin_attr;
struct attribute_group pin_attr_group; struct attribute_group pin_attr_group;
/* 1st entry is a pointer to the real group, 2nd is NULL terminator */
const struct attribute_group *pin_attr_groups[2];
}; };
/* /*
...@@ -94,8 +96,7 @@ uint ptp_poll(struct posix_clock *pc, ...@@ -94,8 +96,7 @@ uint ptp_poll(struct posix_clock *pc,
extern const struct attribute_group *ptp_groups[]; extern const struct attribute_group *ptp_groups[];
int ptp_cleanup_sysfs(struct ptp_clock *ptp); int ptp_populate_pin_groups(struct ptp_clock *ptp);
void ptp_cleanup_pin_groups(struct ptp_clock *ptp);
int ptp_populate_sysfs(struct ptp_clock *ptp);
#endif #endif
...@@ -46,27 +46,6 @@ PTP_SHOW_INT(n_periodic_outputs, n_per_out); ...@@ -46,27 +46,6 @@ PTP_SHOW_INT(n_periodic_outputs, n_per_out);
PTP_SHOW_INT(n_programmable_pins, n_pins); PTP_SHOW_INT(n_programmable_pins, n_pins);
PTP_SHOW_INT(pps_available, pps); PTP_SHOW_INT(pps_available, pps);
static struct attribute *ptp_attrs[] = {
&dev_attr_clock_name.attr,
&dev_attr_max_adjustment.attr,
&dev_attr_n_alarms.attr,
&dev_attr_n_external_timestamps.attr,
&dev_attr_n_periodic_outputs.attr,
&dev_attr_n_programmable_pins.attr,
&dev_attr_pps_available.attr,
NULL,
};
static const struct attribute_group ptp_group = {
.attrs = ptp_attrs,
};
const struct attribute_group *ptp_groups[] = {
&ptp_group,
NULL,
};
static ssize_t extts_enable_store(struct device *dev, static ssize_t extts_enable_store(struct device *dev,
struct device_attribute *attr, struct device_attribute *attr,
const char *buf, size_t count) const char *buf, size_t count)
...@@ -91,6 +70,7 @@ static ssize_t extts_enable_store(struct device *dev, ...@@ -91,6 +70,7 @@ static ssize_t extts_enable_store(struct device *dev,
out: out:
return err; return err;
} }
static DEVICE_ATTR(extts_enable, 0220, NULL, extts_enable_store);
static ssize_t extts_fifo_show(struct device *dev, static ssize_t extts_fifo_show(struct device *dev,
struct device_attribute *attr, char *page) struct device_attribute *attr, char *page)
...@@ -124,6 +104,7 @@ static ssize_t extts_fifo_show(struct device *dev, ...@@ -124,6 +104,7 @@ static ssize_t extts_fifo_show(struct device *dev,
mutex_unlock(&ptp->tsevq_mux); mutex_unlock(&ptp->tsevq_mux);
return cnt; return cnt;
} }
static DEVICE_ATTR(fifo, 0444, extts_fifo_show, NULL);
static ssize_t period_store(struct device *dev, static ssize_t period_store(struct device *dev,
struct device_attribute *attr, struct device_attribute *attr,
...@@ -151,6 +132,7 @@ static ssize_t period_store(struct device *dev, ...@@ -151,6 +132,7 @@ static ssize_t period_store(struct device *dev,
out: out:
return err; return err;
} }
static DEVICE_ATTR(period, 0220, NULL, period_store);
static ssize_t pps_enable_store(struct device *dev, static ssize_t pps_enable_store(struct device *dev,
struct device_attribute *attr, struct device_attribute *attr,
...@@ -177,6 +159,57 @@ static ssize_t pps_enable_store(struct device *dev, ...@@ -177,6 +159,57 @@ static ssize_t pps_enable_store(struct device *dev,
out: out:
return err; return err;
} }
static DEVICE_ATTR(pps_enable, 0220, NULL, pps_enable_store);
static struct attribute *ptp_attrs[] = {
&dev_attr_clock_name.attr,
&dev_attr_max_adjustment.attr,
&dev_attr_n_alarms.attr,
&dev_attr_n_external_timestamps.attr,
&dev_attr_n_periodic_outputs.attr,
&dev_attr_n_programmable_pins.attr,
&dev_attr_pps_available.attr,
&dev_attr_extts_enable.attr,
&dev_attr_fifo.attr,
&dev_attr_period.attr,
&dev_attr_pps_enable.attr,
NULL
};
static umode_t ptp_is_attribute_visible(struct kobject *kobj,
struct attribute *attr, int n)
{
struct device *dev = kobj_to_dev(kobj);
struct ptp_clock *ptp = dev_get_drvdata(dev);
struct ptp_clock_info *info = ptp->info;
umode_t mode = attr->mode;
if (attr == &dev_attr_extts_enable.attr ||
attr == &dev_attr_fifo.attr) {
if (!info->n_ext_ts)
mode = 0;
} else if (attr == &dev_attr_period.attr) {
if (!info->n_per_out)
mode = 0;
} else if (attr == &dev_attr_pps_enable.attr) {
if (!info->pps)
mode = 0;
}
return mode;
}
static const struct attribute_group ptp_group = {
.is_visible = ptp_is_attribute_visible,
.attrs = ptp_attrs,
};
const struct attribute_group *ptp_groups[] = {
&ptp_group,
NULL
};
static int ptp_pin_name2index(struct ptp_clock *ptp, const char *name) static int ptp_pin_name2index(struct ptp_clock *ptp, const char *name)
{ {
...@@ -235,47 +268,20 @@ static ssize_t ptp_pin_store(struct device *dev, struct device_attribute *attr, ...@@ -235,47 +268,20 @@ static ssize_t ptp_pin_store(struct device *dev, struct device_attribute *attr,
return count; return count;
} }
static DEVICE_ATTR(extts_enable, 0220, NULL, extts_enable_store); int ptp_populate_pin_groups(struct ptp_clock *ptp)
static DEVICE_ATTR(fifo, 0444, extts_fifo_show, NULL);
static DEVICE_ATTR(period, 0220, NULL, period_store);
static DEVICE_ATTR(pps_enable, 0220, NULL, pps_enable_store);
int ptp_cleanup_sysfs(struct ptp_clock *ptp)
{ {
struct device *dev = ptp->dev;
struct ptp_clock_info *info = ptp->info; struct ptp_clock_info *info = ptp->info;
int err = -ENOMEM, i, n_pins = info->n_pins;
if (info->n_ext_ts) { if (!n_pins)
device_remove_file(dev, &dev_attr_extts_enable);
device_remove_file(dev, &dev_attr_fifo);
}
if (info->n_per_out)
device_remove_file(dev, &dev_attr_period);
if (info->pps)
device_remove_file(dev, &dev_attr_pps_enable);
if (info->n_pins) {
sysfs_remove_group(&dev->kobj, &ptp->pin_attr_group);
kfree(ptp->pin_attr);
kfree(ptp->pin_dev_attr);
}
return 0; return 0;
}
static int ptp_populate_pins(struct ptp_clock *ptp) ptp->pin_dev_attr = kcalloc(n_pins, sizeof(*ptp->pin_dev_attr),
{
struct device *dev = ptp->dev;
struct ptp_clock_info *info = ptp->info;
int err = -ENOMEM, i, n_pins = info->n_pins;
ptp->pin_dev_attr = kzalloc(n_pins * sizeof(*ptp->pin_dev_attr),
GFP_KERNEL); GFP_KERNEL);
if (!ptp->pin_dev_attr) if (!ptp->pin_dev_attr)
goto no_dev_attr; goto no_dev_attr;
ptp->pin_attr = kzalloc((1 + n_pins) * sizeof(struct attribute *), ptp->pin_attr = kcalloc(1 + n_pins, sizeof(*ptp->pin_attr), GFP_KERNEL);
GFP_KERNEL);
if (!ptp->pin_attr) if (!ptp->pin_attr)
goto no_pin_attr; goto no_pin_attr;
...@@ -292,61 +298,18 @@ static int ptp_populate_pins(struct ptp_clock *ptp) ...@@ -292,61 +298,18 @@ static int ptp_populate_pins(struct ptp_clock *ptp)
ptp->pin_attr_group.name = "pins"; ptp->pin_attr_group.name = "pins";
ptp->pin_attr_group.attrs = ptp->pin_attr; ptp->pin_attr_group.attrs = ptp->pin_attr;
err = sysfs_create_group(&dev->kobj, &ptp->pin_attr_group); ptp->pin_attr_groups[0] = &ptp->pin_attr_group;
if (err)
goto no_group;
return 0; return 0;
no_group:
kfree(ptp->pin_attr);
no_pin_attr: no_pin_attr:
kfree(ptp->pin_dev_attr); kfree(ptp->pin_dev_attr);
no_dev_attr: no_dev_attr:
return err; return err;
} }
int ptp_populate_sysfs(struct ptp_clock *ptp) void ptp_cleanup_pin_groups(struct ptp_clock *ptp)
{ {
struct device *dev = ptp->dev; kfree(ptp->pin_attr);
struct ptp_clock_info *info = ptp->info; kfree(ptp->pin_dev_attr);
int err;
if (info->n_ext_ts) {
err = device_create_file(dev, &dev_attr_extts_enable);
if (err)
goto out1;
err = device_create_file(dev, &dev_attr_fifo);
if (err)
goto out2;
}
if (info->n_per_out) {
err = device_create_file(dev, &dev_attr_period);
if (err)
goto out3;
}
if (info->pps) {
err = device_create_file(dev, &dev_attr_pps_enable);
if (err)
goto out4;
}
if (info->n_pins) {
err = ptp_populate_pins(ptp);
if (err)
goto out5;
}
return 0;
out5:
if (info->pps)
device_remove_file(dev, &dev_attr_pps_enable);
out4:
if (info->n_per_out)
device_remove_file(dev, &dev_attr_period);
out3:
if (info->n_ext_ts)
device_remove_file(dev, &dev_attr_fifo);
out2:
if (info->n_ext_ts)
device_remove_file(dev, &dev_attr_extts_enable);
out1:
return err;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment