linux-api.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Rob Herring <robherring2-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
To: Pantelis Antoniou
	<pantelis.antoniou-OWPKS81ov/FWk0Htik3J/w@public.gmane.org>
Cc: Grant Likely
	<grant.likely-s3s/WqlpOiPyB63q8FvJNQ@public.gmane.org>,
	Andrew Morton
	<akpm-de/tnXTf+JLsfHDXvbKv3WD2FQJk+8+b@public.gmane.org>,
	Matt Porter <mporter-OWPKS81ov/FWk0Htik3J/w@public.gmane.org>,
	Koen Kooi
	<koen-QLwJDigV5abLmq1fohREcCpxlwaOVQ5f@public.gmane.org>,
	Guenter Roeck <linux-0h96xk9xTtrk1uMJSBkQmQ@public.gmane.org>,
	"devicetree-u79uwXL29TY76Z2rM5mHXA@public.gmane.org"
	<devicetree-u79uwXL29TY76Z2rM5mHXA@public.gmane.org>,
	"linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org"
	<linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org>,
	"linux-api-u79uwXL29TY76Z2rM5mHXA@public.gmane.org"
	<linux-api-u79uwXL29TY76Z2rM5mHXA@public.gmane.org>,
	Pantelis Antoniou
	<panto-wVdstyuyKrO8r51toPun2/C9HSW9iNxf@public.gmane.org>
Subject: Re: [PATCH v2 3/4] of: overlay: Add sysfs attributes
Date: Tue, 14 Apr 2015 20:27:30 -0500	[thread overview]
Message-ID: <CAL_JsqLMVBfQpmSjg9qfOvtM3RXMY3Dm+knN8h54gDCzx513-g@mail.gmail.com> (raw)
In-Reply-To: <1428434632-13789-4-git-send-email-pantelis.antoniou-OWPKS81ov/FWk0Htik3J/w@public.gmane.org>

On Tue, Apr 7, 2015 at 2:23 PM, Pantelis Antoniou
<pantelis.antoniou-OWPKS81ov/FWk0Htik3J/w@public.gmane.org> wrote:
> Implement a number of sysfs attributes for overlays.
>
> * A throw once master enable switch to protect against any
> further overlay applications if the administrator desires so.

This one should be a separate patch.

> * A per overlay targets sysfs attribute listing the targets of
> the installed overlay.

What are targets? "targets lists targets" does not help me. The
documentation doesn't help me either.

> * A per overlay can_remove sysfs attribute that reports whether
> the overlay can be removed or not due to another overlapping overlay.
>
> Signed-off-by: Pantelis Antoniou <pantelis.antoniou-OWPKS81ov/FWk0Htik3J/w@public.gmane.org>
> ---
>  drivers/of/overlay.c | 167 ++++++++++++++++++++++++++++++++++++++++++++++++++-
>  1 file changed, 166 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/of/overlay.c b/drivers/of/overlay.c
> index f17f5ef..c54d097 100644
> --- a/drivers/of/overlay.c
> +++ b/drivers/of/overlay.c
> @@ -21,6 +21,7 @@
>  #include <linux/err.h>
>  #include <linux/idr.h>
>  #include <linux/sysfs.h>
> +#include <linux/atomic.h>
>
>  #include "of_private.h"
>
> @@ -55,8 +56,12 @@ struct of_overlay {
>         struct kobject kobj;
>  };
>
> +/* master enable switch; once set to 0 can't be re-enabled */
> +static atomic_t ov_enable = ATOMIC_INIT(1);
> +
>  static int of_overlay_apply_one(struct of_overlay *ov,
>                 struct device_node *target, const struct device_node *overlay);
> +static int overlay_removal_is_ok(struct of_overlay *ov);
>
>  static int of_overlay_apply_single_property(struct of_overlay *ov,
>                 struct device_node *target, struct property *prop)
> @@ -345,6 +350,144 @@ static struct kobj_type of_overlay_ktype = {
>
>  static struct kset *ov_kset;
>
> +static ssize_t enable_read(struct file *filp, struct kobject *kobj,
> +               struct bin_attribute *bin_attr, char *buf,
> +               loff_t offset, size_t count)
> +{
> +       char tbuf[3];
> +
> +       if (offset < 0)
> +               return -EINVAL;
> +
> +       if (offset >= sizeof(tbuf))
> +               return 0;
> +
> +       if (count > sizeof(tbuf) - offset)
> +               count = sizeof(tbuf) - offset;
> +
> +       /* fill in temp */
> +       tbuf[0] = '0' + atomic_read(&ov_enable);
> +       tbuf[1] = '\n';
> +       tbuf[2] = '\0';
> +
> +       /* copy to buffer */
> +       memcpy(buf, tbuf + offset, count);
> +
> +       return count;
> +}
> +
> +static ssize_t enable_write(struct file *filp, struct kobject *kobj,
> +               struct bin_attribute *bin_attr, char *buf,
> +               loff_t off, size_t count)
> +{
> +       unsigned int new_enable;
> +
> +       if (off != 0 || (buf[0] != '0' && buf[0] != '1'))
> +               return -EINVAL;
> +
> +       new_enable = (unsigned int)(buf[0] - '0');
> +       if (new_enable > 1)
> +               return -EINVAL;
> +
> +       /* NOP for same value */
> +       if (new_enable == atomic_read(&ov_enable))
> +               return count;
> +
> +       /* if we've disabled it, no going back */
> +       if (atomic_read(&ov_enable) == 0)
> +               return -EPERM;
> +
> +       atomic_set(&ov_enable, new_enable);
> +       return count;
> +}
> +
> +/* just a single char + '\n' + '\0' */
> +static BIN_ATTR_RW(enable, 3);

Why are you using bin attribute? You are complicating the
implementation needlessly.

> +
> +static ssize_t targets_read(struct file *filp, struct kobject *kobj,
> +               struct bin_attribute *bin_attr, char *buf,
> +               loff_t offset, size_t count)
> +{
> +       struct of_overlay *ov = kobj_to_overlay(kobj);
> +       struct of_overlay_info *ovinfo;
> +       char *tmpbuf, *s, *e;
> +       const char *name;
> +       ssize_t ret;
> +       int i, len;
> +
> +       /* allocate work buffer; we know that PAGE_SIZE is enough */
> +       tmpbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
> +       if (tmpbuf == NULL)
> +               return -ENOMEM;
> +
> +       s = tmpbuf;
> +       e = tmpbuf + PAGE_SIZE;
> +
> +       mutex_lock(&of_mutex);
> +
> +       /* targets */
> +       for (i = 0; i < ov->count; i++) {
> +               ovinfo = &ov->ovinfo_tab[i];
> +
> +               name = of_node_full_name(ovinfo->target);
> +               len = strlen(name);
> +               if (s + len + 1 >= e)
> +                       return -ENOMEM;

Leaking memory here and holding the mutex.

> +               memcpy(s, name, len);
> +               s += len;
> +               *s++ = '\n';
> +       }
> +       if (s + 1 >= e)
> +               return -ENOMEM;

And here.

> +       *s++ = '\0';
> +
> +       /* the buffer is zero terminated */
> +       len = s - tmpbuf;
> +
> +       mutex_unlock(&of_mutex);
> +
> +       /* perform the read */
> +       ret = memory_read_from_buffer(buf, count, &offset, tmpbuf, len);
> +
> +       /* free the temporary buffer */
> +       kfree(tmpbuf);
> +
> +       return ret;
> +}
> +
> +/* targets property */
> +static BIN_ATTR_RO(targets, PAGE_SIZE);
> +
> +static ssize_t can_remove_read(struct file *filp, struct kobject *kobj,
> +               struct bin_attribute *bin_attr, char *buf,
> +               loff_t offset, size_t count)
> +{
> +       struct of_overlay *ov = kobj_to_overlay(kobj);
> +       char tbuf[3];
> +
> +       if (offset < 0)
> +               return -EINVAL;
> +
> +       if (offset >= sizeof(tbuf))
> +               return 0;
> +
> +       if (count > sizeof(tbuf) - offset)
> +               count = sizeof(tbuf) - offset;
> +
> +       /* fill in temp */
> +       tbuf[0] = '0' + overlay_removal_is_ok(ov);
> +       tbuf[1] = '\n';
> +       tbuf[2] = '\0';
> +
> +       /* copy to buffer */
> +       memcpy(buf, tbuf + offset, count);
> +
> +       return count;
> +}
> +
> +/* can_remove property */
> +static BIN_ATTR_RO(can_remove, 3);

Same question about bin attr here.

> +
>  /**
>   * of_overlay_create() - Create and apply an overlay
>   * @tree:      Device node containing all the overlays
> @@ -360,6 +503,10 @@ int of_overlay_create(struct device_node *tree)
>         struct of_overlay *ov;
>         int err, id;
>
> +       /* administratively disabled */
> +       if (!atomic_read(&ov_enable))
> +               return -EPERM;
> +
>         /* allocate the overlay structure */
>         ov = kzalloc(sizeof(*ov), GFP_KERNEL);
>         if (ov == NULL)
> @@ -416,6 +563,22 @@ int of_overlay_create(struct device_node *tree)
>                 goto err_cancel_overlay;
>         }
>
> +       /* create targets file */
> +       err = sysfs_create_bin_file(&ov->kobj, &bin_attr_targets);
> +       if (err != 0) {
> +               pr_err("%s: sysfs_create_bin_file() failed for tree@%s\n",
> +                               __func__, tree->full_name);
> +               goto err_cancel_overlay;
> +       }
> +
> +       /* create can_remove file */
> +       err = sysfs_create_bin_file(&ov->kobj, &bin_attr_can_remove);
> +       if (err != 0) {
> +               pr_err("%s: sysfs_create_bin_file() failed for tree@%s\n",
> +                               __func__, tree->full_name);
> +               goto err_cancel_overlay;
> +       }
> +
>         /* add to the tail of the overlay list */
>         list_add_tail(&ov->node, &ov_list);
>
> @@ -596,5 +759,7 @@ int of_overlay_init(void)
>         if (!ov_kset)
>                 return -ENOMEM;
>
> -       return 0;
> +       rc = sysfs_create_bin_file(&ov_kset->kobj, &bin_attr_enable);
> +       WARN(rc, "%s: error adding enable attribute\n", __func__);
> +       return rc;
>  }
> --
> 1.7.12
>
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

  parent reply	other threads:[~2015-04-15  1:27 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-04-07 19:23 [PATCH v2 0/4] of: overlay: Assorted fixes Pantelis Antoniou
     [not found] ` <1428434632-13789-1-git-send-email-pantelis.antoniou-OWPKS81ov/FWk0Htik3J/w@public.gmane.org>
2015-04-07 19:23   ` [PATCH v2 1/4] of: unittest: overlay: Keep track of created overlays Pantelis Antoniou
     [not found]     ` <1428434632-13789-2-git-send-email-pantelis.antoniou-OWPKS81ov/FWk0Htik3J/w@public.gmane.org>
2015-04-15  1:29       ` Rob Herring
2015-04-07 19:23   ` [PATCH v2 3/4] of: overlay: Add sysfs attributes Pantelis Antoniou
     [not found]     ` <1428434632-13789-4-git-send-email-pantelis.antoniou-OWPKS81ov/FWk0Htik3J/w@public.gmane.org>
2015-04-15  1:27       ` Rob Herring [this message]
     [not found]         ` <CAL_JsqLMVBfQpmSjg9qfOvtM3RXMY3Dm+knN8h54gDCzx513-g-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2015-04-23 12:00           ` Pantelis Antoniou
     [not found]             ` <6E391B8F-936A-4FD1-B8CC-C9B5FCE0B291-OWPKS81ov/FWk0Htik3J/w@public.gmane.org>
2015-04-23 12:33               ` Greg KH
2015-04-23 12:39                 ` Pantelis Antoniou
     [not found]                   ` <98237E9E-04CB-4CD5-8B2F-63F070B54459-OWPKS81ov/FWk0Htik3J/w@public.gmane.org>
2015-04-23 12:54                     ` Greg KH
     [not found]                       ` <20150423125441.GA32303-U8xfFu+wG4EAvxtiuMwx3w@public.gmane.org>
2015-04-23 12:59                         ` Pantelis Antoniou
2015-04-23 13:04                           ` Greg KH
2015-04-07 19:23 ` [PATCH v2 2/4] of: overlay: kobjectify overlay objects Pantelis Antoniou
2015-04-07 19:23 ` [PATCH v2 4/4] Documentation: ABI: /sys/firmware/devicetree/overlays Pantelis Antoniou

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAL_JsqLMVBfQpmSjg9qfOvtM3RXMY3Dm+knN8h54gDCzx513-g@mail.gmail.com \
    --to=robherring2-re5jqeeqqe8avxtiumwx3w@public.gmane.org \
    --cc=akpm-de/tnXTf+JLsfHDXvbKv3WD2FQJk+8+b@public.gmane.org \
    --cc=devicetree-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=grant.likely-s3s/WqlpOiPyB63q8FvJNQ@public.gmane.org \
    --cc=koen-QLwJDigV5abLmq1fohREcCpxlwaOVQ5f@public.gmane.org \
    --cc=linux-0h96xk9xTtrk1uMJSBkQmQ@public.gmane.org \
    --cc=linux-api-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=mporter-OWPKS81ov/FWk0Htik3J/w@public.gmane.org \
    --cc=pantelis.antoniou-OWPKS81ov/FWk0Htik3J/w@public.gmane.org \
    --cc=panto-wVdstyuyKrO8r51toPun2/C9HSW9iNxf@public.gmane.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).