20 Dec, 2017

4 commits

  • A 'perf record' on an app continuously writing in the 'value'
    attribute show that most of the time is spent in kstrtol()

    --17.99%--value_store
    |
    |--10.17%--kstrtoint
    | |
    | |--8.82%--kstrtoll
    |
    |--2.50%--gpiod_set_value_cansleep
    |
    |--1.82%--u16_gpio_set
    |
    |--1.46%--value_store

    The normal case is to write 0 or 1 in the attribute, therefore
    this patch avoids the call to kstrtol() in the most common cases

    Then 'perf record' shows

    --7.21%--value_store
    |
    |--2.69%--u16_gpio_set
    |
    |--1.47%--value_store
    |
    |--1.08%--gpiod_set_value_cansleep
    |
    |--0.60%--mutex_lock
    |
    --0.58%--mutex_unlock

    Signed-off-by: Christophe Leroy
    Signed-off-by: Linus Walleij

    Christophe Leroy
     
  • A bench with 'perf record' shows that most of time spent in value_show()
    is spent in sprintf()

    --42.41%--sysfs_kf_read
    |
    |--39.73%--dev_attr_show
    | |
    | |--38.23%--value_show
    | | |
    | | |--29.22%--sprintf
    | | |
    | | |--2.94%--gpiod_get_value_cansleep
    | | |

    value_show() only returns "0\n" or "1\n", therefore the use of
    sprintf() can be avoided

    With this patch we get the following result with 'perf record'

    --13.89%--sysfs_kf_read
    |
    |--10.72%--dev_attr_show
    | |
    | |--9.44%--value_show
    | | |
    | | |--4.61%--gpiod_get_value_cansleep

    Signed-off-by: Christophe Leroy
    Signed-off-by: Linus Walleij

    Christophe Leroy
     
  • 'value' attribute is supposed to only return 0 or 1 according to
    the documentation.
    With today's implementation, if gpiod_get_value_cansleep() fails
    the printed 'value' is a negative value.

    This patch ensures that an error is returned on read instead.

    Signed-off-by: Christophe Leroy
    Signed-off-by: Linus Walleij

    Christophe Leroy
     
  • The GPIO 'value' attribute is time critical. A small bench with
    'perf record' on the app below shows that 80% of the time spent in
    sysfs_kf_seq_show() is spent in memset() for zeroising the buffer.

    |--67.48%--sysfs_kf_seq_show
    | |
    | |--54.40%--memset
    | |
    | |--11.49%--dev_attr_show
    | | |
    | | |--10.06%--value_show
    | | | |
    | | | |--4.75%--sprintf
    | | | | |

    This patch changes the attribute type to prealloc, eliminating the
    need to zeroise the buffer at each read. 'perf record' gives the
    following result.

    |--42.41%--sysfs_kf_read
    | |
    | |--39.73%--dev_attr_show
    | | |
    | | |--38.23%--value_show
    | | | |
    | | | |--29.22%--sprintf
    | | | | |

    Test done with the following small app:

    int main(int argc, char **argv)
    {
    int fd = open(argv[1], O_RDONLY);

    for (;;) {
    int buf[512];

    read(fd, buf, 512);
    lseek(fd, 0, SEEK_SET);
    }
    exit(0);
    }

    Signed-off-by: Christophe Leroy
    Signed-off-by: Linus Walleij

    Christophe Leroy
     

03 Dec, 2017

1 commit

  • General support for state persistence is added to gpiolib with the
    introduction of a new pinconf parameter to propagate the request to
    hardware. The existing persistence support for sleep is adapted to
    include hardware support if the GPIO driver provides it. Persistence
    continues to be enabled by default; in-kernel consumers can opt out, but
    userspace (currently) does not have a choice.

    The *_SLEEP_MAY_LOSE_VALUE and *_SLEEP_MAINTAIN_VALUE symbols are
    renamed, dropping the SLEEP prefix to reflect that the concept is no
    longer sleep-specific. I feel that renaming to just *_MAY_LOSE_VALUE
    could initially be misinterpreted, so I've further changed the symbols
    to *_TRANSITORY and *_PERSISTENT to address this.

    The sysfs interface is modified only to keep consistency with the
    chardev interface in enforcing persistence for userspace exports.

    Signed-off-by: Andrew Jeffery
    Reviewed-by: Charles Keepax
    Acked-by: Rob Herring
    Signed-off-by: Linus Walleij

    Andrew Jeffery
     

06 Sep, 2017

1 commit

  • Pull GPIO updates from Linus Walleij:
    "This is the bulk of the GPIO changes for the v4.14 cycle.

    Not so much changes this time, phew. David Daney and Bartosz
    Golaszewski did all the really interesting work in infrastructure
    improvement across GPIO and IRQ core, hats off for them and to tglx
    and Marc Z for general help with these patch sets.

    Core changes:

    - Allow the GPIO irqchip to allocate IRQs dynamically. This is an
    important change on systems where only a restricted number of IRQs,
    lesser than the number of GPIO lines, can be utilized. Now we can
    allocate these on a first-come-first-served basis instead of
    hogging up valuable IRQ lines.

    - Serious fix-up of the kerneldoc documentation and inclusion into
    the kerneldoc builds.

    - Pulled in the IRQ simulator from the IRQ core tree and use this in
    the GPIO mockup driver for exhaustive testing of interrupt
    abilities.

    New drivers:

    - New driver for ThunderX and OCTEON-TX. This is especially
    interesting as it picks up improvements from the IRQ core that
    allow us to handle fasteoi ACKs upwards in a hierarchy when there
    are IRQ flag latches on several levels in a hierarchy. Very
    interesting work here.

    - New subdriver for Renesas R-Car r8a7745 (RZ/G1E).

    Misc:

    - Several fixes and improvements for Xilinx Zynq GPIO.

    - Support an enablement GPIO for the 74x164 GPIO.

    - Switch a bunch of chips to use devres to allocate irq descriptors.

    - A bunch of constification fixes"

    * tag 'gpio-v4.14-1' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw/linux-gpio: (63 commits)
    gpio: mockup: remove unused variable gc
    gpio: pl061: constify amba_id
    Revert "gpiolib: request the gpio before querying its direction"
    gpio: twl6040: remove unneeded forward declaration
    gpio: zevio: make gpio_chip const
    gpio: add gpio_add_lookup_tables() to add several tables at once
    gpio: rcar: Add r8a7745 (RZ/G1E) support
    gpio: brcmstb: check return value of gpiochip_irqchip_add()
    MAINTAINERS: Add entry for THUNDERX GPIO Driver.
    gpio: Add gpio driver support for ThunderX and OCTEON-TX
    gpio: mockup: use irq_sim
    gpio: mxs: use devres for irq generic chip
    gpio: mxc: use devres for irq generic chip
    gpio: pch: use devres for irq generic chip
    gpio: ml-ioh: use devres for irq generic chip
    gpio: sta2x11: use devres for irq generic chip
    gpio: sta2x11: disallow unbinding the driver
    gpio: mxs: disallow unbinding the driver
    gpio: mxc: disallow unbinding the driver
    gpio: aspeed: Remove reference to clock name in debounce warning message
    ...

    Linus Torvalds
     

14 Aug, 2017

2 commits

  • Fix up some references to parameters to match the code.

    Signed-off-by: Thierry Reding
    Signed-off-by: Linus Walleij

    Thierry Reding
     
  • Check user-given gpio number and reject it before
    calling gpio_to_desc() because gpio_to_desc() is
    for kernel driver and it expects given gpio number
    is valid (means 0 to 511).
    If given number is invalid, gpio_to_desc() calls
    WARN() and dump registers and stack for debug.
    This means user can easily kick WARN() just by
    writing invalid gpio number (e.g. 512) to
    /sys/class/gpio/export.

    Fixes: 0e9a5edf5d01 ("gpio: fix deferred probe detection for legacy API")
    Signed-off-by: Masami Hiramatsu
    Reviewed-by: Andy Shevchenko
    Signed-off-by: Linus Walleij

    Masami Hiramatsu
     

09 Jun, 2017

1 commit


12 Sep, 2016

1 commit


25 Feb, 2016

1 commit


12 Feb, 2016

3 commits

  • Some information about the GPIO chip need to stay around also
    after the gpio_chip has been removed and only the gpio_device
    persist. The base and ngpio are such things, for example we
    don't want a new chip arriving to overlap the number space
    of a dangling gpio_device, and the chardev may still query
    the device for the number of lines etc.

    Note that the code that assigns base and insert gpio_device
    into the global list no longer check for a missing gpio_chip:
    we respect the number space allocated by any other gpio_device.

    As a consequence of the gdev being referenced directly from
    the gpio_desc, we need to verify it differently from all
    in-kernel API calls that fall through to direct queries to
    the gpio_chip vtable: we first check that desc is !NULL, then
    that desc->gdev is !NULL, then, if desc->gdev->chip is NULL,
    we *BAIL OUT* without any error, so as to manage the case
    where operations are requested on a device that is gone.

    These checks were non-uniform and partly missing in the past:
    so to simplify: create the macros VALIDATE_DESC() that will
    return -EINVAL if the desc or desc->gdev is missing and just
    0 if the chip is gone, and conversely VALIDATE_DESC_VOID()
    for the case where the function does not return an error.
    By using these macros, we get warning messages about missing
    gdev with reference to the right function in the kernel log.

    Despite the macro business this simplifies the code and make
    it more readable than if we copy/paste the same descriptor
    checking code into all code ABI call sites (IMHO).

    Signed-off-by: Linus Walleij

    Linus Walleij
     
  • We need gpio_device to hold the descriptors so that they can
    be lifecycled with the struct gpio_device held from userspace.
    Move the descriptor array into gpio_device. Also rename it from
    "desc" (singularis) to "descs" (pluralis) to reflect the fact
    that it is an array.

    Signed-off-by: Linus Walleij

    Linus Walleij
     
  • Since gpio_device is the struct that survives if the backing
    gpio_chip is removed, move the sysfs mock device to this state
    container so it becomes part of the dangling state of the
    GPIO device on removal.

    Signed-off-by: Linus Walleij

    Linus Walleij
     

09 Feb, 2016

1 commit

  • GPIO chips have been around for years, but were never real devices,
    instead they were piggy-backing on a parent device (such as a
    platform_device or amba_device) but this was always optional.
    GPIO chips could also exist without any device at all, with its
    struct device *parent (ex *dev) pointer being set to null.

    When sysfs was in use, a mock device would be created, with the
    optional parent assigned, or just floating orphaned with NULL
    as parent.

    If sysfs is active, it will use this device as parent.

    We now create a gpio_device struct containing a real
    struct device and move the subsystem over to using that. The
    list of struct gpio_chip:s is augmented to hold struct
    gpio_device:s and we find gpio_chips:s by first looking up
    the struct gpio_device.

    The struct gpio_device is designed to stay around even if the
    gpio_chip is removed, so as to satisfy users in userspace
    that need a backing data structure to hold the state of the
    session initiated with e.g. a character device even if there is
    no physical chip anymore.

    From this point on, gpiochips are devices.

    Cc: Johan Hovold
    Cc: Michael Welling
    Cc: Markus Pargmann
    Signed-off-by: Linus Walleij

    Linus Walleij
     

19 Nov, 2015

1 commit

  • The name .dev in a struct is normally reserved for a struct device
    that is let us say a superclass to the thing described by the struct.
    struct gpio_chip stands out by confusingly using a struct device *dev
    to point to the parent device (such as a platform_device) that
    represents the hardware. As we want to give gpio_chip:s real devices,
    this is not working. We need to rename this member to parent.

    This was done by two coccinelle scripts, I guess it is possible to
    combine them into one, but I don't know such stuff. They look like
    this:

    @@
    struct gpio_chip *var;
    @@
    -var->dev
    +var->parent

    and:

    @@
    struct gpio_chip var;
    @@
    -var.dev
    +var.parent

    and:

    @@
    struct bgpio_chip *var;
    @@
    -var->gc.dev
    +var->gc.parent

    Plus a few instances of bgpio that I couldn't figure out how
    to teach Coccinelle to rewrite.

    This patch hits all over the place, but I *strongly* prefer this
    solution to any piecemal approaches that just exercise patch
    mechanics all over the place. It mainly hits drivers/gpio and
    drivers/pinctrl which is my own backyard anyway.

    Cc: Haavard Skinnemoen
    Cc: Rafał Miłecki
    Cc: Richard Purdie
    Cc: Mauro Carvalho Chehab
    Cc: Alek Du
    Cc: Jaroslav Kysela
    Cc: Takashi Iwai
    Acked-by: Dmitry Torokhov
    Acked-by: Greg Kroah-Hartman
    Acked-by: Lee Jones
    Acked-by: Jiri Kosina
    Acked-by: Hans-Christian Egtvedt
    Acked-by: Jacek Anaszewski
    Signed-off-by: Linus Walleij

    Linus Walleij
     

12 May, 2015

21 commits

  • Move irq trigger flags, which as sysfs-interface specific, to the class
    device data.

    This avoids accessing the gpio-descriptor flags field using non-atomic
    operations without any locking, and allows for a more clear separation
    of the sysfs interface from gpiolib core.

    Signed-off-by: Johan Hovold
    Reviewed-by: Alexandre Courbot
    Signed-off-by: Linus Walleij

    Johan Hovold
     
  • Remove FLAG_SYSFS_DIR, which is sysfs-interface specific, and store it
    in the class-device data instead.

    Note that the flag is only used during export.

    Signed-off-by: Johan Hovold
    Reviewed-by: Alexandre Courbot
    Signed-off-by: Linus Walleij

    Johan Hovold
     
  • Rename active-low helper using common prefix.

    Also remove unnecessary manipulation of value argument.

    Signed-off-by: Johan Hovold
    Reviewed-by: Alexandre Courbot
    Signed-off-by: Linus Walleij

    Johan Hovold
     
  • Make sure to deregister the class device (and release the irq) while
    holding the sysfs lock in gpio_unexport to prevent racing with
    gpio_export.

    Note that this requires the recently introduced per-gpio locking to
    avoid a deadlock with the kernfs active protection when waiting for the
    attribute operations to drain during deregistration.

    Signed-off-by: Johan Hovold
    Reviewed-by: Alexandre Courbot
    Signed-off-by: Linus Walleij

    Johan Hovold
     
  • Add a per-gpio mutex to serialise attribute operations rather than use
    one global mutex for all gpios and chips.

    Having a single global lock for all gpios in a system adds unnecessary
    latency to the sysfs interface, and especially when having gpio
    controllers connected over slow buses.

    Now that the global gpio-sysfs interrupt table is gone and with per-gpio
    data in place, we can easily switch to using a more fine-grained locking
    scheme.

    Keep the global mutex to serialise the global (class) operations of gpio
    export and unexport and chip removal.

    Also document the locking assumptions made.

    Note that this is also needed to fix a race between gpiod_export and
    gpiod_unexport.

    Signed-off-by: Johan Hovold
    Reviewed-by: Alexandre Courbot
    Signed-off-by: Linus Walleij

    Johan Hovold
     
  • Drop unnecessary locking from gpiod_export_link. If the class device has
    not already been unregistered, class_find_device returns the ref-counted
    class device so there's no need for locking.

    Signed-off-by: Johan Hovold
    Reviewed-by: Alexandre Courbot
    Signed-off-by: Linus Walleij

    Johan Hovold
     
  • Remove goto from success path.

    Signed-off-by: Johan Hovold
    Reviewed-by: Alexandre Courbot
    Signed-off-by: Linus Walleij

    Johan Hovold
     
  • Add separate helper functions for irq request and free.

    Signed-off-by: Johan Hovold
    Reviewed-by: Alexandre Courbot
    Signed-off-by: Linus Walleij

    Johan Hovold
     
  • Only call irq helper if actually reconfiguring interrupt state.

    This is a preparatory step in introducing separate gpio-irq request and
    free functions.

    Signed-off-by: Johan Hovold
    Reviewed-by: Alexandre Courbot
    Signed-off-by: Linus Walleij

    Johan Hovold
     
  • Store the value sysfs entry in the gpiod data rather than in a global
    table accessed through an index stored in the overloaded gpio-descriptor
    flag field.

    Signed-off-by: Johan Hovold
    Reviewed-by: Alexandre Courbot
    Signed-off-by: Linus Walleij

    Johan Hovold
     
  • Remove redundant gpio-descriptor parameters from sysfs_set_active_low and
    gpio_setup_irq.

    Signed-off-by: Johan Hovold
    Reviewed-by: Alexandre Courbot
    Signed-off-by: Linus Walleij

    Johan Hovold
     
  • Add gpiod class-device data.

    This is a first step in getting rid of the insane gpio-descriptor flag
    overloading, backward irq-interface implementation, and course grained
    sysfs-interface locking (a single static mutex for every operation on
    all exported gpios in a system).

    Signed-off-by: Johan Hovold
    Reviewed-by: Alexandre Courbot
    Signed-off-by: Linus Walleij

    Johan Hovold
     
  • The attribute operations will never be called for an unregistered device
    so remove redundant checks for FLAG_EXPORT.

    Note that kernfs will also guarantee that any active sysfs operation has
    finished before the attribute is removed during deregistration.

    Signed-off-by: Johan Hovold
    Reviewed-by: Alexandre Courbot
    Signed-off-by: Linus Walleij

    Johan Hovold
     
  • Make sure to release any irq only after the class device has been
    deregistered.

    This avoids a race between gpiod_unexport and edge_store, where an irq
    could be allocated just before the gpio class device is deregistered
    without relying on FLAG_EXPORT and the global sysfs lock.

    Note that there is no need to hold the sysfs lock when releasing the irq
    after the class device is gone as kernfs will prevent further attribute
    operations.

    Signed-off-by: Johan Hovold
    Reviewed-by: Alexandre Courbot
    Signed-off-by: Linus Walleij

    Johan Hovold
     
  • Use DEVICE_ATTR_RO and DEVICE_ATTR_RW rather than specifying masks and
    callbacks directly.

    Signed-off-by: Johan Hovold
    Reviewed-by: Alexandre Courbot
    Signed-off-by: Linus Walleij

    Johan Hovold
     
  • Remove gpiod_sysfs_set_active_low (and gpio_sysfs_set_active_low) which
    allowed code to change the polarity of a gpio line even after it had
    been exported through sysfs.

    Drivers should not care, and generally does not know, about gpio-line
    polarity which is a hardware feature that needs to be described by
    firmware.

    It is currently possible to define gpio-line polarity in device-tree and
    acpi firmware or using platform data. Userspace can also change the
    polarity through sysfs.

    Note that drivers using the legacy gpio interface could still use
    GPIOF_ACTIVE_LOW to change the polarity before exporting the gpio.

    There are no in-kernel users of this interface.

    Cc: Jonathan Corbet
    Cc: Harry Wei
    Cc: Arnd Bergmann
    Cc: linux-doc@vger.kernel.org
    Cc: linux-kernel@zh-kernel.org
    Cc: linux-arch@vger.kernel.org
    Signed-off-by: Johan Hovold
    Reviewed-by: Alexandre Courbot
    Signed-off-by: Linus Walleij

    Johan Hovold
     
  • Rename the gpio-chip export/unexport functions to the more descriptive
    names gpiochip_sysfs_register and gpiochip_sysfs_unregister.

    Signed-off-by: Johan Hovold
    Reviewed-by: Alexandre Courbot
    Signed-off-by: Linus Walleij

    Johan Hovold
     
  • Clean gpio-chip class device registration and deregistration.

    The class device is registered when a gpio-chip is added (or from
    gpiolib_sysfs_init post-core init call), and deregistered when the chip
    is removed.

    Store the class device in struct gpio_chip directly rather than do a
    class-device lookup on deregistration. This also removes the need for
    the exported flag.

    Signed-off-by: Johan Hovold
    Reviewed-by: Alexandre Courbot
    Signed-off-by: Linus Walleij

    Johan Hovold
     
  • Reduce scope of sysfs_lock protection during chip export and unexport,
    which is only needed to prevent gpiod (re-)exports during chip removal.

    Signed-off-by: Johan Hovold
    Reviewed-by: Alexandre Courbot
    Signed-off-by: Linus Walleij

    Johan Hovold
     
  • Put the recently introduced gpio-chip pointer to some more use in
    gpiod_export.

    Signed-off-by: Johan Hovold
    Reviewed-by: Alexandre Courbot
    Signed-off-by: Linus Walleij

    Johan Hovold
     
  • Drivers should call gpiochip_lock_as_irq (which prevents the pin
    direction from being changed) in their irq_request_resources callbacks
    but some drivers currently fail to do so.

    Instead a second, explicit and often redundant call to lock-as-irq is
    made by the sysfs-interface implementation after an irq has been
    requested.

    Move the explicit call before the irq-request to match the unlock done
    after the irq is later released. Note that this also fixes an irq leak,
    should the explicit call ever have failed.

    Also add a comment about removing the redundant call once the broken
    drivers have been fixed.

    Signed-off-by: Johan Hovold
    Reviewed-by: Alexandre Courbot
    Signed-off-by: Linus Walleij

    Johan Hovold
     

30 Apr, 2015

1 commit

  • Unregister GPIOs requested through sysfs at chip remove to avoid leaking
    the associated memory and sysfs entries.

    The stale sysfs entries prevented the gpio numbers from being exported
    when the gpio range was later reused (e.g. at device reconnect).

    This also fixes the related module-reference leak.

    Note that kernfs makes sure that any on-going sysfs operations finish
    before the class devices are unregistered and that further accesses
    fail.

    The chip exported flag is used to prevent gpiod exports during removal.
    This also makes it harder to trigger, but does not fix, the related race
    between gpiochip_remove and export_store, which is really a race with
    gpiod_request that needs to be addressed separately.

    Also note that this would prevent the crashes (e.g. NULL-dereferences)
    at reconnect that affects pre-3.18 kernels, as well as use-after-free on
    operations on open attribute files on pre-3.14 kernels (prior to
    kernfs).

    Fixes: d8f388d8dc8d ("gpio: sysfs interface")
    Cc: stable # v2.6.27: 01cca93a9491
    Signed-off-by: Johan Hovold
    Signed-off-by: Linus Walleij

    Johan Hovold
     

30 Jan, 2015

2 commits

  • Fix memory leak in the gpio sysfs interface due to failure to drop
    reference to device returned by class_find_device when setting the
    gpio-line polarity.

    Fixes: 0769746183ca ("gpiolib: add support for changing value polarity
    in sysfs")
    Cc: stable # v2.6.33
    Signed-off-by: Johan Hovold
    Signed-off-by: Linus Walleij

    Johan Hovold
     
  • Fix memory leak in the gpio sysfs interface due to failure to drop
    reference to device returned by class_find_device when creating a link.

    Fixes: a4177ee7f1a8 ("gpiolib: allow exported GPIO nodes to be named
    using sysfs links")
    Cc: stable # v2.6.32
    Signed-off-by: Johan Hovold
    Signed-off-by: Linus Walleij

    Johan Hovold