diff mbox series

[2/7] coresight: perf: Add "sinks" group to PMU directory

Message ID 20190115230742.13730-3-mathieu.poirier@linaro.org
State New
Headers show
Series perf: Communicate sink via event::attr:config2 | expand

Commit Message

Mathieu Poirier Jan. 15, 2019, 11:07 p.m. UTC
Add a "sinks" directory entry so that users can see all the sinks
available in the system in a single place.  Individual sink are added
as they are registered with the coresight bus.

Signed-off-by: Mathieu Poirier <mathieu.poirier@linaro.org>

---
 .../hwtracing/coresight/coresight-etm-perf.c  | 43 +++++++++++++++++++
 .../hwtracing/coresight/coresight-etm-perf.h  |  1 +
 drivers/hwtracing/coresight/coresight.c       | 17 ++++++++
 3 files changed, 61 insertions(+)

-- 
2.17.1

Comments

Suzuki K Poulose Jan. 16, 2019, 10:29 a.m. UTC | #1
On 15/01/2019 23:07, Mathieu Poirier wrote:
> Add a "sinks" directory entry so that users can see all the sinks

> available in the system in a single place.  Individual sink are added

> as they are registered with the coresight bus.

> 

> Signed-off-by: Mathieu Poirier <mathieu.poirier@linaro.org>

> ---

>   .../hwtracing/coresight/coresight-etm-perf.c  | 43 +++++++++++++++++++

>   .../hwtracing/coresight/coresight-etm-perf.h  |  1 +

>   drivers/hwtracing/coresight/coresight.c       | 17 ++++++++

>   3 files changed, 61 insertions(+)

> 

> diff --git a/drivers/hwtracing/coresight/coresight-etm-perf.c b/drivers/hwtracing/coresight/coresight-etm-perf.c

> index f21eb28b6782..292bd409a68c 100644

> --- a/drivers/hwtracing/coresight/coresight-etm-perf.c

> +++ b/drivers/hwtracing/coresight/coresight-etm-perf.c

> @@ -14,6 +14,7 @@

>   #include <linux/perf_event.h>

>   #include <linux/percpu-defs.h>

>   #include <linux/slab.h>

> +#include <linux/stringhash.h>

>   #include <linux/types.h>

>   #include <linux/workqueue.h>

>   

> @@ -43,8 +44,18 @@ static const struct attribute_group etm_pmu_format_group = {

>   	.attrs  = etm_config_formats_attr,

>   };

>   

> +static struct attribute *etm_config_sinks_attr[] = {

> +	NULL,

> +};

> +

> +static const struct attribute_group etm_pmu_sinks_group = {

> +	.name   = "sinks",

> +	.attrs  = etm_config_sinks_attr,

> +};

> +

>   static const struct attribute_group *etm_pmu_attr_groups[] = {

>   	&etm_pmu_format_group,

> +	&etm_pmu_sinks_group,

>   	NULL,

>   };

>   

> @@ -479,6 +490,38 @@ int etm_perf_symlink(struct coresight_device *csdev, bool link)

>   	return 0;

>   }

>   

> +static ssize_t etm_perf_sink_name_show(struct device *dev,

> +				       struct device_attribute *dattr,

> +				       char *buf)

> +{

> +	/* See function coresight_sink_by_id() to know where this is used */

> +	u32 hash = hashlen_hash(hashlen_string(NULL, dattr->attr.name));

> +

> +	return scnprintf(buf, PAGE_SIZE, "%x\n", hash);

> +}

> +

> +int etm_perf_symlink_sink(struct coresight_device *csdev)

> +{

> +	struct device *pmu_dev = etm_pmu.dev;

> +	struct device *pdev = csdev->dev.parent;

> +	struct device_attribute *dev_attr;

> +

> +	if (csdev->type != CORESIGHT_DEV_TYPE_SINK &&

> +	    csdev->type != CORESIGHT_DEV_TYPE_LINKSINK)

> +		return -EINVAL;

> +

> +	if (!etm_perf_up)

> +		return -EPROBE_DEFER;

> +

> +	dev_attr = kzalloc(sizeof(*dev_attr), GFP_KERNEL);

> +	dev_attr->attr.name = kstrdup(dev_name(pdev), GFP_KERNEL);

> +	dev_attr->attr.mode = 0444;

> +	dev_attr->show = etm_perf_sink_name_show;


I would have  added the attribute to the csdev (say, sink_attr),
and add that to the group, so that it is easier to remove the
attribute when the sink device is removed from the system (when
we get there). It would be good to have something in place to remove the
attribute.

Otherwise, the overall approach looks fine to me.

Suzuki
Greg KH Jan. 16, 2019, 3:39 p.m. UTC | #2
On Tue, Jan 15, 2019 at 04:07:37PM -0700, Mathieu Poirier wrote:
> Add a "sinks" directory entry so that users can see all the sinks

> available in the system in a single place.  Individual sink are added

> as they are registered with the coresight bus.

> 

> Signed-off-by: Mathieu Poirier <mathieu.poirier@linaro.org>

> ---

>  .../hwtracing/coresight/coresight-etm-perf.c  | 43 +++++++++++++++++++

>  .../hwtracing/coresight/coresight-etm-perf.h  |  1 +

>  drivers/hwtracing/coresight/coresight.c       | 17 ++++++++

>  3 files changed, 61 insertions(+)

> 

> diff --git a/drivers/hwtracing/coresight/coresight-etm-perf.c b/drivers/hwtracing/coresight/coresight-etm-perf.c

> index f21eb28b6782..292bd409a68c 100644

> --- a/drivers/hwtracing/coresight/coresight-etm-perf.c

> +++ b/drivers/hwtracing/coresight/coresight-etm-perf.c

> @@ -14,6 +14,7 @@

>  #include <linux/perf_event.h>

>  #include <linux/percpu-defs.h>

>  #include <linux/slab.h>

> +#include <linux/stringhash.h>

>  #include <linux/types.h>

>  #include <linux/workqueue.h>

>  

> @@ -43,8 +44,18 @@ static const struct attribute_group etm_pmu_format_group = {

>  	.attrs  = etm_config_formats_attr,

>  };

>  

> +static struct attribute *etm_config_sinks_attr[] = {

> +	NULL,

> +};

> +

> +static const struct attribute_group etm_pmu_sinks_group = {

> +	.name   = "sinks",

> +	.attrs  = etm_config_sinks_attr,

> +};

> +

>  static const struct attribute_group *etm_pmu_attr_groups[] = {

>  	&etm_pmu_format_group,

> +	&etm_pmu_sinks_group,

>  	NULL,

>  };

>  

> @@ -479,6 +490,38 @@ int etm_perf_symlink(struct coresight_device *csdev, bool link)

>  	return 0;

>  }

>  

> +static ssize_t etm_perf_sink_name_show(struct device *dev,

> +				       struct device_attribute *dattr,

> +				       char *buf)

> +{

> +	/* See function coresight_sink_by_id() to know where this is used */

> +	u32 hash = hashlen_hash(hashlen_string(NULL, dattr->attr.name));

> +

> +	return scnprintf(buf, PAGE_SIZE, "%x\n", hash);

> +}

> +

> +int etm_perf_symlink_sink(struct coresight_device *csdev)

> +{

> +	struct device *pmu_dev = etm_pmu.dev;

> +	struct device *pdev = csdev->dev.parent;

> +	struct device_attribute *dev_attr;

> +

> +	if (csdev->type != CORESIGHT_DEV_TYPE_SINK &&

> +	    csdev->type != CORESIGHT_DEV_TYPE_LINKSINK)

> +		return -EINVAL;

> +

> +	if (!etm_perf_up)

> +		return -EPROBE_DEFER;

> +

> +	dev_attr = kzalloc(sizeof(*dev_attr), GFP_KERNEL);

> +	dev_attr->attr.name = kstrdup(dev_name(pdev), GFP_KERNEL);

> +	dev_attr->attr.mode = 0444;

> +	dev_attr->show = etm_perf_sink_name_show;

> +

> +	return sysfs_add_file_to_group(&pmu_dev->kobj,

> +				       &dev_attr->attr, "sinks");


What is so odd about this call that you needed me to review this?

And what happens if this call fails, do you leak memory?

thanks,

greg k-h
Mathieu Poirier Jan. 16, 2019, 4:14 p.m. UTC | #3
On Wed, 16 Jan 2019 at 08:39, Greg KH <gregkh@linuxfoundation.org> wrote:
>

> On Tue, Jan 15, 2019 at 04:07:37PM -0700, Mathieu Poirier wrote:

> > Add a "sinks" directory entry so that users can see all the sinks

> > available in the system in a single place.  Individual sink are added

> > as they are registered with the coresight bus.

> >

> > Signed-off-by: Mathieu Poirier <mathieu.poirier@linaro.org>

> > ---

> >  .../hwtracing/coresight/coresight-etm-perf.c  | 43 +++++++++++++++++++

> >  .../hwtracing/coresight/coresight-etm-perf.h  |  1 +

> >  drivers/hwtracing/coresight/coresight.c       | 17 ++++++++

> >  3 files changed, 61 insertions(+)

> >

> > diff --git a/drivers/hwtracing/coresight/coresight-etm-perf.c b/drivers/hwtracing/coresight/coresight-etm-perf.c

> > index f21eb28b6782..292bd409a68c 100644

> > --- a/drivers/hwtracing/coresight/coresight-etm-perf.c

> > +++ b/drivers/hwtracing/coresight/coresight-etm-perf.c

> > @@ -14,6 +14,7 @@

> >  #include <linux/perf_event.h>

> >  #include <linux/percpu-defs.h>

> >  #include <linux/slab.h>

> > +#include <linux/stringhash.h>

> >  #include <linux/types.h>

> >  #include <linux/workqueue.h>

> >

> > @@ -43,8 +44,18 @@ static const struct attribute_group etm_pmu_format_group = {

> >       .attrs  = etm_config_formats_attr,

> >  };

> >

> > +static struct attribute *etm_config_sinks_attr[] = {

> > +     NULL,

> > +};

> > +

> > +static const struct attribute_group etm_pmu_sinks_group = {

> > +     .name   = "sinks",

> > +     .attrs  = etm_config_sinks_attr,

> > +};

> > +

> >  static const struct attribute_group *etm_pmu_attr_groups[] = {

> >       &etm_pmu_format_group,

> > +     &etm_pmu_sinks_group,

> >       NULL,

> >  };

> >

> > @@ -479,6 +490,38 @@ int etm_perf_symlink(struct coresight_device *csdev, bool link)

> >       return 0;

> >  }

> >

> > +static ssize_t etm_perf_sink_name_show(struct device *dev,

> > +                                    struct device_attribute *dattr,

> > +                                    char *buf)

> > +{

> > +     /* See function coresight_sink_by_id() to know where this is used */

> > +     u32 hash = hashlen_hash(hashlen_string(NULL, dattr->attr.name));

> > +

> > +     return scnprintf(buf, PAGE_SIZE, "%x\n", hash);

> > +}

> > +

> > +int etm_perf_symlink_sink(struct coresight_device *csdev)

> > +{

> > +     struct device *pmu_dev = etm_pmu.dev;

> > +     struct device *pdev = csdev->dev.parent;

> > +     struct device_attribute *dev_attr;

> > +

> > +     if (csdev->type != CORESIGHT_DEV_TYPE_SINK &&

> > +         csdev->type != CORESIGHT_DEV_TYPE_LINKSINK)

> > +             return -EINVAL;

> > +

> > +     if (!etm_perf_up)

> > +             return -EPROBE_DEFER;

> > +

> > +     dev_attr = kzalloc(sizeof(*dev_attr), GFP_KERNEL);

> > +     dev_attr->attr.name = kstrdup(dev_name(pdev), GFP_KERNEL);

> > +     dev_attr->attr.mode = 0444;

> > +     dev_attr->show = etm_perf_sink_name_show;

> > +

> > +     return sysfs_add_file_to_group(&pmu_dev->kobj,

> > +                                    &dev_attr->attr, "sinks");

>

> What is so odd about this call that you needed me to review this?


As far as I can tell nobody is feeding a dynamic struct attribute to
the function and I wasn't sure if it was because they were told not to
or simply because it wasn't needed, hence asking for a second opinion.

>

> And what happens if this call fails, do you leak memory?

>

> thanks,

>

> greg k-h
Greg KH Jan. 16, 2019, 4:33 p.m. UTC | #4
On Wed, Jan 16, 2019 at 09:14:33AM -0700, Mathieu Poirier wrote:
> On Wed, 16 Jan 2019 at 08:39, Greg KH <gregkh@linuxfoundation.org> wrote:

> >

> > On Tue, Jan 15, 2019 at 04:07:37PM -0700, Mathieu Poirier wrote:

> > > Add a "sinks" directory entry so that users can see all the sinks

> > > available in the system in a single place.  Individual sink are added

> > > as they are registered with the coresight bus.

> > >

> > > Signed-off-by: Mathieu Poirier <mathieu.poirier@linaro.org>

> > > ---

> > >  .../hwtracing/coresight/coresight-etm-perf.c  | 43 +++++++++++++++++++

> > >  .../hwtracing/coresight/coresight-etm-perf.h  |  1 +

> > >  drivers/hwtracing/coresight/coresight.c       | 17 ++++++++

> > >  3 files changed, 61 insertions(+)

> > >

> > > diff --git a/drivers/hwtracing/coresight/coresight-etm-perf.c b/drivers/hwtracing/coresight/coresight-etm-perf.c

> > > index f21eb28b6782..292bd409a68c 100644

> > > --- a/drivers/hwtracing/coresight/coresight-etm-perf.c

> > > +++ b/drivers/hwtracing/coresight/coresight-etm-perf.c

> > > @@ -14,6 +14,7 @@

> > >  #include <linux/perf_event.h>

> > >  #include <linux/percpu-defs.h>

> > >  #include <linux/slab.h>

> > > +#include <linux/stringhash.h>

> > >  #include <linux/types.h>

> > >  #include <linux/workqueue.h>

> > >

> > > @@ -43,8 +44,18 @@ static const struct attribute_group etm_pmu_format_group = {

> > >       .attrs  = etm_config_formats_attr,

> > >  };

> > >

> > > +static struct attribute *etm_config_sinks_attr[] = {

> > > +     NULL,

> > > +};

> > > +

> > > +static const struct attribute_group etm_pmu_sinks_group = {

> > > +     .name   = "sinks",

> > > +     .attrs  = etm_config_sinks_attr,

> > > +};

> > > +

> > >  static const struct attribute_group *etm_pmu_attr_groups[] = {

> > >       &etm_pmu_format_group,

> > > +     &etm_pmu_sinks_group,

> > >       NULL,

> > >  };

> > >

> > > @@ -479,6 +490,38 @@ int etm_perf_symlink(struct coresight_device *csdev, bool link)

> > >       return 0;

> > >  }

> > >

> > > +static ssize_t etm_perf_sink_name_show(struct device *dev,

> > > +                                    struct device_attribute *dattr,

> > > +                                    char *buf)

> > > +{

> > > +     /* See function coresight_sink_by_id() to know where this is used */

> > > +     u32 hash = hashlen_hash(hashlen_string(NULL, dattr->attr.name));

> > > +

> > > +     return scnprintf(buf, PAGE_SIZE, "%x\n", hash);

> > > +}

> > > +

> > > +int etm_perf_symlink_sink(struct coresight_device *csdev)

> > > +{

> > > +     struct device *pmu_dev = etm_pmu.dev;

> > > +     struct device *pdev = csdev->dev.parent;

> > > +     struct device_attribute *dev_attr;

> > > +

> > > +     if (csdev->type != CORESIGHT_DEV_TYPE_SINK &&

> > > +         csdev->type != CORESIGHT_DEV_TYPE_LINKSINK)

> > > +             return -EINVAL;

> > > +

> > > +     if (!etm_perf_up)

> > > +             return -EPROBE_DEFER;

> > > +

> > > +     dev_attr = kzalloc(sizeof(*dev_attr), GFP_KERNEL);

> > > +     dev_attr->attr.name = kstrdup(dev_name(pdev), GFP_KERNEL);

> > > +     dev_attr->attr.mode = 0444;

> > > +     dev_attr->show = etm_perf_sink_name_show;

> > > +

> > > +     return sysfs_add_file_to_group(&pmu_dev->kobj,

> > > +                                    &dev_attr->attr, "sinks");

> >

> > What is so odd about this call that you needed me to review this?

> 

> As far as I can tell nobody is feeding a dynamic struct attribute to

> the function and I wasn't sure if it was because they were told not to

> or simply because it wasn't needed, hence asking for a second opinion.


Ah.  Well, again, this is a good question to answer:

> > And what happens if this call fails, do you leak memory?


And also, what happens when you unload the device, who frees the
attribute's memory?

thanks,

greg k-h
Mathieu Poirier Jan. 16, 2019, 4:38 p.m. UTC | #5
On Wed, 16 Jan 2019 at 09:33, Greg KH <gregkh@linuxfoundation.org> wrote:
>

> On Wed, Jan 16, 2019 at 09:14:33AM -0700, Mathieu Poirier wrote:

> > On Wed, 16 Jan 2019 at 08:39, Greg KH <gregkh@linuxfoundation.org> wrote:

> > >

> > > On Tue, Jan 15, 2019 at 04:07:37PM -0700, Mathieu Poirier wrote:

> > > > Add a "sinks" directory entry so that users can see all the sinks

> > > > available in the system in a single place.  Individual sink are added

> > > > as they are registered with the coresight bus.

> > > >

> > > > Signed-off-by: Mathieu Poirier <mathieu.poirier@linaro.org>

> > > > ---

> > > >  .../hwtracing/coresight/coresight-etm-perf.c  | 43 +++++++++++++++++++

> > > >  .../hwtracing/coresight/coresight-etm-perf.h  |  1 +

> > > >  drivers/hwtracing/coresight/coresight.c       | 17 ++++++++

> > > >  3 files changed, 61 insertions(+)

> > > >

> > > > diff --git a/drivers/hwtracing/coresight/coresight-etm-perf.c b/drivers/hwtracing/coresight/coresight-etm-perf.c

> > > > index f21eb28b6782..292bd409a68c 100644

> > > > --- a/drivers/hwtracing/coresight/coresight-etm-perf.c

> > > > +++ b/drivers/hwtracing/coresight/coresight-etm-perf.c

> > > > @@ -14,6 +14,7 @@

> > > >  #include <linux/perf_event.h>

> > > >  #include <linux/percpu-defs.h>

> > > >  #include <linux/slab.h>

> > > > +#include <linux/stringhash.h>

> > > >  #include <linux/types.h>

> > > >  #include <linux/workqueue.h>

> > > >

> > > > @@ -43,8 +44,18 @@ static const struct attribute_group etm_pmu_format_group = {

> > > >       .attrs  = etm_config_formats_attr,

> > > >  };

> > > >

> > > > +static struct attribute *etm_config_sinks_attr[] = {

> > > > +     NULL,

> > > > +};

> > > > +

> > > > +static const struct attribute_group etm_pmu_sinks_group = {

> > > > +     .name   = "sinks",

> > > > +     .attrs  = etm_config_sinks_attr,

> > > > +};

> > > > +

> > > >  static const struct attribute_group *etm_pmu_attr_groups[] = {

> > > >       &etm_pmu_format_group,

> > > > +     &etm_pmu_sinks_group,

> > > >       NULL,

> > > >  };

> > > >

> > > > @@ -479,6 +490,38 @@ int etm_perf_symlink(struct coresight_device *csdev, bool link)

> > > >       return 0;

> > > >  }

> > > >

> > > > +static ssize_t etm_perf_sink_name_show(struct device *dev,

> > > > +                                    struct device_attribute *dattr,

> > > > +                                    char *buf)

> > > > +{

> > > > +     /* See function coresight_sink_by_id() to know where this is used */

> > > > +     u32 hash = hashlen_hash(hashlen_string(NULL, dattr->attr.name));

> > > > +

> > > > +     return scnprintf(buf, PAGE_SIZE, "%x\n", hash);

> > > > +}

> > > > +

> > > > +int etm_perf_symlink_sink(struct coresight_device *csdev)

> > > > +{

> > > > +     struct device *pmu_dev = etm_pmu.dev;

> > > > +     struct device *pdev = csdev->dev.parent;

> > > > +     struct device_attribute *dev_attr;

> > > > +

> > > > +     if (csdev->type != CORESIGHT_DEV_TYPE_SINK &&

> > > > +         csdev->type != CORESIGHT_DEV_TYPE_LINKSINK)

> > > > +             return -EINVAL;

> > > > +

> > > > +     if (!etm_perf_up)

> > > > +             return -EPROBE_DEFER;

> > > > +

> > > > +     dev_attr = kzalloc(sizeof(*dev_attr), GFP_KERNEL);

> > > > +     dev_attr->attr.name = kstrdup(dev_name(pdev), GFP_KERNEL);

> > > > +     dev_attr->attr.mode = 0444;

> > > > +     dev_attr->show = etm_perf_sink_name_show;

> > > > +

> > > > +     return sysfs_add_file_to_group(&pmu_dev->kobj,

> > > > +                                    &dev_attr->attr, "sinks");

> > >

> > > What is so odd about this call that you needed me to review this?

> >

> > As far as I can tell nobody is feeding a dynamic struct attribute to

> > the function and I wasn't sure if it was because they were told not to

> > or simply because it wasn't needed, hence asking for a second opinion.

>

> Ah.  Well, again, this is a good question to answer:

>

> > > And what happens if this call fails, do you leak memory?


That's something I will fix in the next revision.

>

> And also, what happens when you unload the device, who frees the

> attribute's memory?


If configured, coresight devices aren't removable.

Thanks for the review,
Mathieu

>

> thanks,

>

> greg k-h
Greg KH Jan. 16, 2019, 5:12 p.m. UTC | #6
On Wed, Jan 16, 2019 at 09:38:09AM -0700, Mathieu Poirier wrote:
> On Wed, 16 Jan 2019 at 09:33, Greg KH <gregkh@linuxfoundation.org> wrote:

> >

> > On Wed, Jan 16, 2019 at 09:14:33AM -0700, Mathieu Poirier wrote:

> > > On Wed, 16 Jan 2019 at 08:39, Greg KH <gregkh@linuxfoundation.org> wrote:

> > > >

> > > > On Tue, Jan 15, 2019 at 04:07:37PM -0700, Mathieu Poirier wrote:

> > > > > Add a "sinks" directory entry so that users can see all the sinks

> > > > > available in the system in a single place.  Individual sink are added

> > > > > as they are registered with the coresight bus.

> > > > >

> > > > > Signed-off-by: Mathieu Poirier <mathieu.poirier@linaro.org>

> > > > > ---

> > > > >  .../hwtracing/coresight/coresight-etm-perf.c  | 43 +++++++++++++++++++

> > > > >  .../hwtracing/coresight/coresight-etm-perf.h  |  1 +

> > > > >  drivers/hwtracing/coresight/coresight.c       | 17 ++++++++

> > > > >  3 files changed, 61 insertions(+)

> > > > >

> > > > > diff --git a/drivers/hwtracing/coresight/coresight-etm-perf.c b/drivers/hwtracing/coresight/coresight-etm-perf.c

> > > > > index f21eb28b6782..292bd409a68c 100644

> > > > > --- a/drivers/hwtracing/coresight/coresight-etm-perf.c

> > > > > +++ b/drivers/hwtracing/coresight/coresight-etm-perf.c

> > > > > @@ -14,6 +14,7 @@

> > > > >  #include <linux/perf_event.h>

> > > > >  #include <linux/percpu-defs.h>

> > > > >  #include <linux/slab.h>

> > > > > +#include <linux/stringhash.h>

> > > > >  #include <linux/types.h>

> > > > >  #include <linux/workqueue.h>

> > > > >

> > > > > @@ -43,8 +44,18 @@ static const struct attribute_group etm_pmu_format_group = {

> > > > >       .attrs  = etm_config_formats_attr,

> > > > >  };

> > > > >

> > > > > +static struct attribute *etm_config_sinks_attr[] = {

> > > > > +     NULL,

> > > > > +};

> > > > > +

> > > > > +static const struct attribute_group etm_pmu_sinks_group = {

> > > > > +     .name   = "sinks",

> > > > > +     .attrs  = etm_config_sinks_attr,

> > > > > +};

> > > > > +

> > > > >  static const struct attribute_group *etm_pmu_attr_groups[] = {

> > > > >       &etm_pmu_format_group,

> > > > > +     &etm_pmu_sinks_group,

> > > > >       NULL,

> > > > >  };

> > > > >

> > > > > @@ -479,6 +490,38 @@ int etm_perf_symlink(struct coresight_device *csdev, bool link)

> > > > >       return 0;

> > > > >  }

> > > > >

> > > > > +static ssize_t etm_perf_sink_name_show(struct device *dev,

> > > > > +                                    struct device_attribute *dattr,

> > > > > +                                    char *buf)

> > > > > +{

> > > > > +     /* See function coresight_sink_by_id() to know where this is used */

> > > > > +     u32 hash = hashlen_hash(hashlen_string(NULL, dattr->attr.name));

> > > > > +

> > > > > +     return scnprintf(buf, PAGE_SIZE, "%x\n", hash);

> > > > > +}

> > > > > +

> > > > > +int etm_perf_symlink_sink(struct coresight_device *csdev)

> > > > > +{

> > > > > +     struct device *pmu_dev = etm_pmu.dev;

> > > > > +     struct device *pdev = csdev->dev.parent;

> > > > > +     struct device_attribute *dev_attr;

> > > > > +

> > > > > +     if (csdev->type != CORESIGHT_DEV_TYPE_SINK &&

> > > > > +         csdev->type != CORESIGHT_DEV_TYPE_LINKSINK)

> > > > > +             return -EINVAL;

> > > > > +

> > > > > +     if (!etm_perf_up)

> > > > > +             return -EPROBE_DEFER;

> > > > > +

> > > > > +     dev_attr = kzalloc(sizeof(*dev_attr), GFP_KERNEL);

> > > > > +     dev_attr->attr.name = kstrdup(dev_name(pdev), GFP_KERNEL);

> > > > > +     dev_attr->attr.mode = 0444;

> > > > > +     dev_attr->show = etm_perf_sink_name_show;

> > > > > +

> > > > > +     return sysfs_add_file_to_group(&pmu_dev->kobj,

> > > > > +                                    &dev_attr->attr, "sinks");

> > > >

> > > > What is so odd about this call that you needed me to review this?

> > >

> > > As far as I can tell nobody is feeding a dynamic struct attribute to

> > > the function and I wasn't sure if it was because they were told not to

> > > or simply because it wasn't needed, hence asking for a second opinion.

> >

> > Ah.  Well, again, this is a good question to answer:

> >

> > > > And what happens if this call fails, do you leak memory?

> 

> That's something I will fix in the next revision.

> 

> >

> > And also, what happens when you unload the device, who frees the

> > attribute's memory?

> 

> If configured, coresight devices aren't removable.


But is the driver able to be unloaded?  Unbound from the device manually
through sysfs?  There's lots of ways devices are "removed" that don't
involved physical removal :)

thanks,

greg k-h
Mathieu Poirier Jan. 16, 2019, 5:30 p.m. UTC | #7
On Wed, 16 Jan 2019 at 10:13, Greg KH <gregkh@linuxfoundation.org> wrote:
>

> On Wed, Jan 16, 2019 at 09:38:09AM -0700, Mathieu Poirier wrote:

> > On Wed, 16 Jan 2019 at 09:33, Greg KH <gregkh@linuxfoundation.org> wrote:

> > >

> > > On Wed, Jan 16, 2019 at 09:14:33AM -0700, Mathieu Poirier wrote:

> > > > On Wed, 16 Jan 2019 at 08:39, Greg KH <gregkh@linuxfoundation.org> wrote:

> > > > >

> > > > > On Tue, Jan 15, 2019 at 04:07:37PM -0700, Mathieu Poirier wrote:

> > > > > > Add a "sinks" directory entry so that users can see all the sinks

> > > > > > available in the system in a single place.  Individual sink are added

> > > > > > as they are registered with the coresight bus.

> > > > > >

> > > > > > Signed-off-by: Mathieu Poirier <mathieu.poirier@linaro.org>

> > > > > > ---

> > > > > >  .../hwtracing/coresight/coresight-etm-perf.c  | 43 +++++++++++++++++++

> > > > > >  .../hwtracing/coresight/coresight-etm-perf.h  |  1 +

> > > > > >  drivers/hwtracing/coresight/coresight.c       | 17 ++++++++

> > > > > >  3 files changed, 61 insertions(+)

> > > > > >

> > > > > > diff --git a/drivers/hwtracing/coresight/coresight-etm-perf.c b/drivers/hwtracing/coresight/coresight-etm-perf.c

> > > > > > index f21eb28b6782..292bd409a68c 100644

> > > > > > --- a/drivers/hwtracing/coresight/coresight-etm-perf.c

> > > > > > +++ b/drivers/hwtracing/coresight/coresight-etm-perf.c

> > > > > > @@ -14,6 +14,7 @@

> > > > > >  #include <linux/perf_event.h>

> > > > > >  #include <linux/percpu-defs.h>

> > > > > >  #include <linux/slab.h>

> > > > > > +#include <linux/stringhash.h>

> > > > > >  #include <linux/types.h>

> > > > > >  #include <linux/workqueue.h>

> > > > > >

> > > > > > @@ -43,8 +44,18 @@ static const struct attribute_group etm_pmu_format_group = {

> > > > > >       .attrs  = etm_config_formats_attr,

> > > > > >  };

> > > > > >

> > > > > > +static struct attribute *etm_config_sinks_attr[] = {

> > > > > > +     NULL,

> > > > > > +};

> > > > > > +

> > > > > > +static const struct attribute_group etm_pmu_sinks_group = {

> > > > > > +     .name   = "sinks",

> > > > > > +     .attrs  = etm_config_sinks_attr,

> > > > > > +};

> > > > > > +

> > > > > >  static const struct attribute_group *etm_pmu_attr_groups[] = {

> > > > > >       &etm_pmu_format_group,

> > > > > > +     &etm_pmu_sinks_group,

> > > > > >       NULL,

> > > > > >  };

> > > > > >

> > > > > > @@ -479,6 +490,38 @@ int etm_perf_symlink(struct coresight_device *csdev, bool link)

> > > > > >       return 0;

> > > > > >  }

> > > > > >

> > > > > > +static ssize_t etm_perf_sink_name_show(struct device *dev,

> > > > > > +                                    struct device_attribute *dattr,

> > > > > > +                                    char *buf)

> > > > > > +{

> > > > > > +     /* See function coresight_sink_by_id() to know where this is used */

> > > > > > +     u32 hash = hashlen_hash(hashlen_string(NULL, dattr->attr.name));

> > > > > > +

> > > > > > +     return scnprintf(buf, PAGE_SIZE, "%x\n", hash);

> > > > > > +}

> > > > > > +

> > > > > > +int etm_perf_symlink_sink(struct coresight_device *csdev)

> > > > > > +{

> > > > > > +     struct device *pmu_dev = etm_pmu.dev;

> > > > > > +     struct device *pdev = csdev->dev.parent;

> > > > > > +     struct device_attribute *dev_attr;

> > > > > > +

> > > > > > +     if (csdev->type != CORESIGHT_DEV_TYPE_SINK &&

> > > > > > +         csdev->type != CORESIGHT_DEV_TYPE_LINKSINK)

> > > > > > +             return -EINVAL;

> > > > > > +

> > > > > > +     if (!etm_perf_up)

> > > > > > +             return -EPROBE_DEFER;

> > > > > > +

> > > > > > +     dev_attr = kzalloc(sizeof(*dev_attr), GFP_KERNEL);

> > > > > > +     dev_attr->attr.name = kstrdup(dev_name(pdev), GFP_KERNEL);

> > > > > > +     dev_attr->attr.mode = 0444;

> > > > > > +     dev_attr->show = etm_perf_sink_name_show;

> > > > > > +

> > > > > > +     return sysfs_add_file_to_group(&pmu_dev->kobj,

> > > > > > +                                    &dev_attr->attr, "sinks");

> > > > >

> > > > > What is so odd about this call that you needed me to review this?

> > > >

> > > > As far as I can tell nobody is feeding a dynamic struct attribute to

> > > > the function and I wasn't sure if it was because they were told not to

> > > > or simply because it wasn't needed, hence asking for a second opinion.

> > >

> > > Ah.  Well, again, this is a good question to answer:

> > >

> > > > > And what happens if this call fails, do you leak memory?

> >

> > That's something I will fix in the next revision.

> >

> > >

> > > And also, what happens when you unload the device, who frees the

> > > attribute's memory?

> >

> > If configured, coresight devices aren't removable.

>

> But is the driver able to be unloaded?  Unbound from the device manually

> through sysfs?  There's lots of ways devices are "removed" that don't

> involved physical removal :)


All drivers are built-in and thus preventing them from being unloaded.
They also have their ".suppress_bin_attrs" field set to take care of
unbinding through sysfs.

>

> thanks,

>

> greg k-h
Mathieu Poirier Jan. 16, 2019, 11:43 p.m. UTC | #8
Good evening Suzuki,

On Wed, 16 Jan 2019 at 03:29, Suzuki K Poulose <suzuki.poulose@arm.com> wrote:
>

>

>

> On 15/01/2019 23:07, Mathieu Poirier wrote:

> > Add a "sinks" directory entry so that users can see all the sinks

> > available in the system in a single place.  Individual sink are added

> > as they are registered with the coresight bus.

> >

> > Signed-off-by: Mathieu Poirier <mathieu.poirier@linaro.org>

> > ---

> >   .../hwtracing/coresight/coresight-etm-perf.c  | 43 +++++++++++++++++++

> >   .../hwtracing/coresight/coresight-etm-perf.h  |  1 +

> >   drivers/hwtracing/coresight/coresight.c       | 17 ++++++++

> >   3 files changed, 61 insertions(+)

> >

> > diff --git a/drivers/hwtracing/coresight/coresight-etm-perf.c b/drivers/hwtracing/coresight/coresight-etm-perf.c

> > index f21eb28b6782..292bd409a68c 100644

> > --- a/drivers/hwtracing/coresight/coresight-etm-perf.c

> > +++ b/drivers/hwtracing/coresight/coresight-etm-perf.c

> > @@ -14,6 +14,7 @@

> >   #include <linux/perf_event.h>

> >   #include <linux/percpu-defs.h>

> >   #include <linux/slab.h>

> > +#include <linux/stringhash.h>

> >   #include <linux/types.h>

> >   #include <linux/workqueue.h>

> >

> > @@ -43,8 +44,18 @@ static const struct attribute_group etm_pmu_format_group = {

> >       .attrs  = etm_config_formats_attr,

> >   };

> >

> > +static struct attribute *etm_config_sinks_attr[] = {

> > +     NULL,

> > +};

> > +

> > +static const struct attribute_group etm_pmu_sinks_group = {

> > +     .name   = "sinks",

> > +     .attrs  = etm_config_sinks_attr,

> > +};

> > +

> >   static const struct attribute_group *etm_pmu_attr_groups[] = {

> >       &etm_pmu_format_group,

> > +     &etm_pmu_sinks_group,

> >       NULL,

> >   };

> >

> > @@ -479,6 +490,38 @@ int etm_perf_symlink(struct coresight_device *csdev, bool link)

> >       return 0;

> >   }

> >

> > +static ssize_t etm_perf_sink_name_show(struct device *dev,

> > +                                    struct device_attribute *dattr,

> > +                                    char *buf)

> > +{

> > +     /* See function coresight_sink_by_id() to know where this is used */

> > +     u32 hash = hashlen_hash(hashlen_string(NULL, dattr->attr.name));

> > +

> > +     return scnprintf(buf, PAGE_SIZE, "%x\n", hash);

> > +}

> > +

> > +int etm_perf_symlink_sink(struct coresight_device *csdev)

> > +{

> > +     struct device *pmu_dev = etm_pmu.dev;

> > +     struct device *pdev = csdev->dev.parent;

> > +     struct device_attribute *dev_attr;

> > +

> > +     if (csdev->type != CORESIGHT_DEV_TYPE_SINK &&

> > +         csdev->type != CORESIGHT_DEV_TYPE_LINKSINK)

> > +             return -EINVAL;

> > +

> > +     if (!etm_perf_up)

> > +             return -EPROBE_DEFER;

> > +

> > +     dev_attr = kzalloc(sizeof(*dev_attr), GFP_KERNEL);

> > +     dev_attr->attr.name = kstrdup(dev_name(pdev), GFP_KERNEL);

> > +     dev_attr->attr.mode = 0444;

> > +     dev_attr->show = etm_perf_sink_name_show;

>

> I would have  added the attribute to the csdev (say, sink_attr),

> and add that to the group, so that it is easier to remove the

> attribute when the sink device is removed from the system (when

> we get there). It would be good to have something in place to remove the

> attribute.


My hope was to avoid introducing a new field in the already bloated
coresight_device structure, and on top of things a component specific
field.  I think it would be worth it if we'd envision making the
coresight drivers removable in a not so distant future.  But doing
something like that is quite tricky (as Kim quickly found out) and
skirts the bottom of the list of priorities, if on it at all.

I'll change it if you're really keen on it but it would be code that
is never used.

Mathieu

>

> Otherwise, the overall approach looks fine to me.

>

> Suzuki
Suzuki K Poulose Jan. 17, 2019, 11:55 a.m. UTC | #9
Hi Mathieu,

On 16/01/2019 23:43, Mathieu Poirier wrote:
> Good evening Suzuki,

> 

> On Wed, 16 Jan 2019 at 03:29, Suzuki K Poulose <suzuki.poulose@arm.com> wrote:

>>

>>

>>

>> On 15/01/2019 23:07, Mathieu Poirier wrote:

>>> Add a "sinks" directory entry so that users can see all the sinks

>>> available in the system in a single place.  Individual sink are added

>>> as they are registered with the coresight bus.

>>>

>>> Signed-off-by: Mathieu Poirier <mathieu.poirier@linaro.org>


...

>>> +static ssize_t etm_perf_sink_name_show(struct device *dev,

>>> +                                    struct device_attribute *dattr,

>>> +                                    char *buf)

>>> +{

>>> +     /* See function coresight_sink_by_id() to know where this is used */

>>> +     u32 hash = hashlen_hash(hashlen_string(NULL, dattr->attr.name));

>>> +

>>> +     return scnprintf(buf, PAGE_SIZE, "%x\n", hash);

>>> +}

>>> +

>>> +int etm_perf_symlink_sink(struct coresight_device *csdev)

>>> +{

>>> +     struct device *pmu_dev = etm_pmu.dev;

>>> +     struct device *pdev = csdev->dev.parent;

>>> +     struct device_attribute *dev_attr;

>>> +

>>> +     if (csdev->type != CORESIGHT_DEV_TYPE_SINK &&

>>> +         csdev->type != CORESIGHT_DEV_TYPE_LINKSINK)

>>> +             return -EINVAL;

>>> +

>>> +     if (!etm_perf_up)

>>> +             return -EPROBE_DEFER;

>>> +

>>> +     dev_attr = kzalloc(sizeof(*dev_attr), GFP_KERNEL);

>>> +     dev_attr->attr.name = kstrdup(dev_name(pdev), GFP_KERNEL);

>>> +     dev_attr->attr.mode = 0444;

>>> +     dev_attr->show = etm_perf_sink_name_show;

>>

>> I would have  added the attribute to the csdev (say, sink_attr),

>> and add that to the group, so that it is easier to remove the

>> attribute when the sink device is removed from the system (when

>> we get there). It would be good to have something in place to remove the

>> attribute.

> 

> My hope was to avoid introducing a new field in the already bloated

> coresight_device structure, and on top of things a component specific

> field.  I think it would be worth it if we'd envision making the


I agree. May be we could add a union for the fields specific to the "Type" of
the device.

> coresight drivers removable in a not so distant future.  But doing

> something like that is quite tricky (as Kim quickly found out) and

> skirts the bottom of the list of priorities, if on it at all.


> 

> I'll change it if you're really keen on it but it would be code that

> is never used.


Yes, I understand. But we would want to get there sometime in the future,
in order to allow using the Coresight out of the box on Enterprise systems.
So it would be good to prepare towards that as we go.


Also, it makes the failure handling easier.

Cheers
Suzuki
diff mbox series

Patch

diff --git a/drivers/hwtracing/coresight/coresight-etm-perf.c b/drivers/hwtracing/coresight/coresight-etm-perf.c
index f21eb28b6782..292bd409a68c 100644
--- a/drivers/hwtracing/coresight/coresight-etm-perf.c
+++ b/drivers/hwtracing/coresight/coresight-etm-perf.c
@@ -14,6 +14,7 @@ 
 #include <linux/perf_event.h>
 #include <linux/percpu-defs.h>
 #include <linux/slab.h>
+#include <linux/stringhash.h>
 #include <linux/types.h>
 #include <linux/workqueue.h>
 
@@ -43,8 +44,18 @@  static const struct attribute_group etm_pmu_format_group = {
 	.attrs  = etm_config_formats_attr,
 };
 
+static struct attribute *etm_config_sinks_attr[] = {
+	NULL,
+};
+
+static const struct attribute_group etm_pmu_sinks_group = {
+	.name   = "sinks",
+	.attrs  = etm_config_sinks_attr,
+};
+
 static const struct attribute_group *etm_pmu_attr_groups[] = {
 	&etm_pmu_format_group,
+	&etm_pmu_sinks_group,
 	NULL,
 };
 
@@ -479,6 +490,38 @@  int etm_perf_symlink(struct coresight_device *csdev, bool link)
 	return 0;
 }
 
+static ssize_t etm_perf_sink_name_show(struct device *dev,
+				       struct device_attribute *dattr,
+				       char *buf)
+{
+	/* See function coresight_sink_by_id() to know where this is used */
+	u32 hash = hashlen_hash(hashlen_string(NULL, dattr->attr.name));
+
+	return scnprintf(buf, PAGE_SIZE, "%x\n", hash);
+}
+
+int etm_perf_symlink_sink(struct coresight_device *csdev)
+{
+	struct device *pmu_dev = etm_pmu.dev;
+	struct device *pdev = csdev->dev.parent;
+	struct device_attribute *dev_attr;
+
+	if (csdev->type != CORESIGHT_DEV_TYPE_SINK &&
+	    csdev->type != CORESIGHT_DEV_TYPE_LINKSINK)
+		return -EINVAL;
+
+	if (!etm_perf_up)
+		return -EPROBE_DEFER;
+
+	dev_attr = kzalloc(sizeof(*dev_attr), GFP_KERNEL);
+	dev_attr->attr.name = kstrdup(dev_name(pdev), GFP_KERNEL);
+	dev_attr->attr.mode = 0444;
+	dev_attr->show = etm_perf_sink_name_show;
+
+	return sysfs_add_file_to_group(&pmu_dev->kobj,
+				       &dev_attr->attr, "sinks");
+}
+
 static int __init etm_perf_init(void)
 {
 	int ret;
diff --git a/drivers/hwtracing/coresight/coresight-etm-perf.h b/drivers/hwtracing/coresight/coresight-etm-perf.h
index da7d9336a15c..529a47285c0f 100644
--- a/drivers/hwtracing/coresight/coresight-etm-perf.h
+++ b/drivers/hwtracing/coresight/coresight-etm-perf.h
@@ -59,6 +59,7 @@  struct etm_event_data {
 
 #ifdef CONFIG_CORESIGHT
 int etm_perf_symlink(struct coresight_device *csdev, bool link);
+int etm_perf_symlink_sink(struct coresight_device *csdev);
 static inline void *etm_perf_sink_config(struct perf_output_handle *handle)
 {
 	struct etm_event_data *data = perf_get_aux(handle);
diff --git a/drivers/hwtracing/coresight/coresight.c b/drivers/hwtracing/coresight/coresight.c
index 2b0df1a0a8df..526f122a38ee 100644
--- a/drivers/hwtracing/coresight/coresight.c
+++ b/drivers/hwtracing/coresight/coresight.c
@@ -18,6 +18,7 @@ 
 #include <linux/delay.h>
 #include <linux/pm_runtime.h>
 
+#include "coresight-etm-perf.h"
 #include "coresight-priv.h"
 
 static DEFINE_MUTEX(coresight_mutex);
@@ -1167,6 +1168,22 @@  struct coresight_device *coresight_register(struct coresight_desc *desc)
 		goto err_out;
 	}
 
+	if (csdev->type == CORESIGHT_DEV_TYPE_SINK ||
+	    csdev->type == CORESIGHT_DEV_TYPE_LINKSINK) {
+		ret = etm_perf_symlink_sink(csdev);
+
+		if (ret) {
+			device_unregister(&csdev->dev);
+			/*
+			 * As with the above, all resources are free'd
+			 * explicitly via coresight_device_release() triggered
+			 * from put_device(), which is in turn called from
+			 * function device_unregister().
+			 */
+			goto err_out;
+		}
+	}
+
 	mutex_lock(&coresight_mutex);
 
 	coresight_fixup_device_conns(csdev);