Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp8025051ybi; Tue, 9 Jul 2019 07:59:37 -0700 (PDT) X-Google-Smtp-Source: APXvYqwsGAcmT5PdMBDokenQJQlPNKbcNdcGm0afgy4LctnuRP4BzxOImb4s+/0j5LvVoQDspEZx X-Received: by 2002:a17:90a:cb12:: with SMTP id z18mr576846pjt.82.1562684377148; Tue, 09 Jul 2019 07:59:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1562684377; cv=none; d=google.com; s=arc-20160816; b=P4iwqEBpdRbxhZqKQUsW2wJK5IKfsZfZ/GXLtpeN9+In4wHgK7P4wa11Gwm8m4Kz29 L/CCZe8oKRi3o2eEXDuSyFGRxsEg7JuPr5NDDv3jx7uoqvbjcSt06GsIjzX3tK7CbNxA hRFGIIPbTkTFVb1uH5YrUg4a195cwwLwzCRPi9603NVSqHySYE+bhqV2MEOu35ozl43A 4I0/7Y/XljlpVI4ybK6QvUZ/Qn3suql1NCyrB6NMS1uTkXtXVy2XClJvl3Vd5G2olPcG G3/jxqZ5/oG+8xr3i8D0k2gU5eHqrF3U+0BWnHIWVbu7yoL5WSKzjZrwdm8bSrAZ2OUp BbDA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=pzCb+Di/c9ZWTKEmAt46a9ynFYRh/T3Fr01SSbk8A/w=; b=faDI1hFfyO5VObyqifi/MnBo0v7a3h1CWVed/SyZYyivu+RjUxlBgH3R+whS7zIB6K o1sks6llfXgdO7zCT8Nm4qDfbQxYHgpjxawpuOvgHmCMHDB9yAqEoJsmspfj6NljGzST qHxjRJf0sH28LTpRcwlhkQwNZE5MTHpzy1PulHJ8VPI37CGjLMgIvYWurAPTHqpBoZGN Mc4uqaEknUg2pZzfhZZxDw3DblyWeiaCHt2WJGc4OoNglcGnBc5IP35okd979mpM+XRt /VyGfAl6sNFMW5vDPo9u0xBaOG8JrpAYcntq4LVgk61w6XBjC9HfVYAjhNm3hdmR1LsF DSRA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@baylibre-com.20150623.gappssmtp.com header.s=20150623 header.b=sITepeWj; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r1si3089820pjp.80.2019.07.09.07.59.21; Tue, 09 Jul 2019 07:59:37 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@baylibre-com.20150623.gappssmtp.com header.s=20150623 header.b=sITepeWj; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726449AbfGIO7B (ORCPT + 99 others); Tue, 9 Jul 2019 10:59:01 -0400 Received: from mail-oi1-f195.google.com ([209.85.167.195]:46674 "EHLO mail-oi1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726318AbfGIO7B (ORCPT ); Tue, 9 Jul 2019 10:59:01 -0400 Received: by mail-oi1-f195.google.com with SMTP id 65so15543663oid.13 for ; Tue, 09 Jul 2019 07:59:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=pzCb+Di/c9ZWTKEmAt46a9ynFYRh/T3Fr01SSbk8A/w=; b=sITepeWjRPevQm0BsKHCBzgNsIV/6+j349u/RLUlrHVpKZ6WQTBiV8xqZRUtlr6oM7 uX9+KEbwvwEZ4gfs+BaSmuvm5AegKonbf2TPNwG4OgxDjnSRrTI3moW3A77EH+/PGL0i gcIDPN4fJj6Mue6fk+LogZRrzPxkonUexTrfQagtsleWGr/+OaYifVvKs7hmAdoFAb4n tKdfDvzN+yaH6ZPkOqZPg0GrlAvdcn9p+u0GRHaLrmnNzq1sn1xx2uWlZI9JIMUhPukO JM0Fl7fpD52byEmmX3wBVeqyd2ukPVlF0FcRrMFElUlMc2oClC5wPSJ+YK2vmyEAxzP5 Xtbg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=pzCb+Di/c9ZWTKEmAt46a9ynFYRh/T3Fr01SSbk8A/w=; b=FYxsGuAM17ukfEqiD7qVN8xlS4d7FrbKRF9dNLbNoiRghul7aD2v1aMyuRoR3MoXMV BOPOwZcvguD0NJo9DsULwMrsbYM7kvUzz4+DZV4aRlCiEq3CD/2QgWWqOkj68XJ4SWap JhMo7YzGL9w2INMs8sK8ETlDcVep3diyPpZ1Lv15krzUSfYPWgkeToM+oqZ0DeB7kDln 2XPaAojdiRhaAdtMuEG++ZvtG75zM2o/qD+cChUJS4t4Gc5duR17w54Pf/KKiHdoMOYG tgABLzDPNtoxs7wWxLqctBznzewJkTfOrC/EHIrsQuV92DCwToCV6huY6cw6nQW40EGs Nmqg== X-Gm-Message-State: APjAAAXk4sIJpoaMpZRrss3PHW9aeHnVEx3/r72zIjBrIYl3oa21wDu9 2E/4lZwGXNVliBBJvpnqOwdxrg9g3895oxGQUgLYUA== X-Received: by 2002:aca:b58b:: with SMTP id e133mr234611oif.147.1562684339714; Tue, 09 Jul 2019 07:58:59 -0700 (PDT) MIME-Version: 1.0 References: <20190705160536.12047-1-geert+renesas@glider.be> In-Reply-To: From: Bartosz Golaszewski Date: Tue, 9 Jul 2019 16:58:48 +0200 Message-ID: Subject: Re: [PATCH RFC] gpio: Add Virtual Aggregator GPIO Driver To: Geert Uytterhoeven Cc: Geert Uytterhoeven , Linus Walleij , Alexander Graf , Peter Maydell , Paolo Bonzini , Magnus Damm , linux-gpio , QEMU Developers , Linux-Renesas , LKML Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org pon., 8 lip 2019 o 12:24 Geert Uytterhoeven napisa= =C5=82(a): > > Hi Bartosz, > > On Mon, Jul 8, 2019 at 11:45 AM Bartosz Golaszewski > wrote: > > pt., 5 lip 2019 o 18:05 Geert Uytterhoeven na= pisa=C5=82(a): > > > GPIO controllers are exported to userspace using /dev/gpiochip* > > > character devices. Access control to these devices is provided by > > > standard UNIX file system permissions, on an all-or-nothing basis: > > > either a GPIO controller is accessible for a user, or it is not. > > > Currently no mechanism exists to control access to individual GPIOs. > > > > > > Hence add a virtual GPIO driver to aggregate existing GPIOs (up to 32= ), > > > and expose them as a new gpiochip. This is useful for implementing > > > access control, and assigning a set of GPIOs to a specific user. > > > Furthermore, it would simplify and harden exporting GPIOs to a virtua= l > > > machine, as the VM can just grab the full virtual GPIO controller, an= d > > > no longer needs to care about which GPIOs to grab and which not, > > > reducing the attack surface. > > > > > > Virtual GPIO controllers are instantiated by writing to the "new_devi= ce" > > > attribute file in sysfs: > > > > > > $ echo " [ ...]" > > > "[, [ ...]] ...]" > > > > /sys/bus/platform/drivers/gpio-virt-agg/new_device > > > > > > Likewise, virtual GPIO controllers can be destroyed after use: > > > > > > $ echo gpio-virt-agg. \ > > > > /sys/bus/platform/drivers/gpio-virt-agg/delete_device > > > > > > Signed-off-by: Geert Uytterhoeven > > > I like the general idea and the interface looks mostly fine. Since > > this is new ABI I think it needs to be documented as well. > > Sure. > > > I'm having trouble building this module: > > > > CALL scripts/atomic/check-atomics.sh > > CALL scripts/checksyscalls.sh > > CHK include/generated/compile.h > > Kernel: arch/arm/boot/Image is ready > > Building modules, stage 2. > > MODPOST 235 modules > > ERROR: "gpiod_request" [drivers/gpio/gpio-virt-agg.ko] undefined! > > ERROR: "gpiochip_get_desc" [drivers/gpio/gpio-virt-agg.ko] undefined! > > ERROR: "gpiod_free" [drivers/gpio/gpio-virt-agg.ko] undefined! > > scripts/Makefile.modpost:91: recipe for target '__modpost' failed > > make[1]: *** [__modpost] Error 1 > > Makefile:1287: recipe for target 'modules' failed > > make: *** [modules] Error 2 > > make: *** Waiting for unfinished jobs.... > > > > I'm not sure what the problem is. > > Oops. As this is an RFC, I didn't bother trying to build this driver as > a module, only builtin. Apparently the 3 symbols above are not yet > exported using EXPORT_SYMBOL_GPL(). > Am I doing it right? I'm trying to create a device and am only getting this= : # echo gpiochip2 23 > new_device [ 707.507039] gpio-virt-agg gpio-virt-agg.0: Cannot find gpiochip gpiochip= 2 gpiochip2 *does* exist in the system. > > > --- /dev/null > > > +++ b/drivers/gpio/gpio-virt-agg.c > > > @@ -0,0 +1,390 @@ > > > +// SPDX-License-Identifier: GPL-2.0-only > > > +// > > > +// GPIO Virtual Aggregator > > > +// > > > +// Copyright (C) 2019 Glider bvba > > > + > > > +#include > > > +#include > > > +#include > > > +#include > > > +#include > > > +#include > > > + > > > +#include "gpiolib.h" > > > + > > > +#define DRV_NAME "gpio-virt-agg" > > > +#define MAX_GPIOS 32 > > > > Do we really need this limit? I see it simplifies the code, but maybe > > we can allocate the relevant arrays dynamically and not limit users? > > Sure. That limit can be lifted. > > > > +static int gpio_virt_agg_set_config(struct gpio_chip *chip, > > > + unsigned int offset, unsigned lon= g config) > > > +{ > > > + struct gpio_virt_agg_priv *priv =3D gpiochip_get_data(chip); > > > + > > > + chip =3D priv->desc[offset]->gdev->chip; > > > + if (chip->set_config) > > > + return chip->set_config(chip, offset, config); > > > + > > > + // FIXME gpiod_set_transitory() expects success if not implem= ented > > BTW, do you have a comment about this FIXME? > Ha! Interesting. I'll give it a thought and respond elsewhere as it's a different subject. > > > + return -ENOTSUPP; > > > +} > > > > +static int gpio_virt_agg_probe(struct platform_device *pdev) > > > +{ > > > + struct device *dev =3D &pdev->dev; > > > + const char *param =3D dev_get_platdata(dev); > > > + struct gpio_virt_agg_priv *priv; > > > + const char *label =3D NULL; > > > + struct gpio_chip *chip; > > > + struct gpio_desc *desc; > > > + unsigned int offset; > > > + int error, i; > > > > This 'i' here is reported as possibly not initialized: > > > > drivers/gpio/gpio-virt-agg.c: In function =E2=80=98gpio_virt_agg_probe= =E2=80=99: > > drivers/gpio/gpio-virt-agg.c:230:13: warning: =E2=80=98i=E2=80=99 may b= e used > > uninitialized in this function [-Wmaybe-uninitialized] > > int error, i; > > ^ > > Oops, should be preinitialized to zero. WIll fix. > > > > +static int gpio_virt_agg_remove(struct platform_device *pdev) > > > +{ > > > + struct gpio_virt_agg_priv *priv =3D platform_get_drvdata(pdev= ); > > > + unsigned int i; > > > + > > > + gpiochip_remove(&priv->chip); > > > + > > > + for (i =3D 0; i < priv->chip.ngpio; i++) > > > + gpiod_free(priv->desc[i]); > > Perhaps I should use gpiod_put() instead, which is exported to modules? > > > > + > > > + return 0; > > > +} > > > > You shouldn't need this function at all. It's up to users to free descr= iptors. > > This frees the upstream descriptors, not the descriptors used by users > of the virtual gpiochip. Shouldn't they be freed, as they are no longer > in use? > > Note that .probe() doesn't use devm_gpiochip_add_data(), as the upstream > descriptors need to be freed after the call to gpiochip_remove(). > > Thanks! I see. I'll try to review it more thoroughly once I get to play with it. So far I'm stuck on creating the virtual chip. Bart > > Gr{oetje,eeting}s, > > Geert > > -- > Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m6= 8k.org > > In personal conversations with technical people, I call myself a hacker. = But > when I'm talking to journalists I just say "programmer" or something like= that. > -- Linus Torvalds