From: Kent Gibson <warthog618@gmail.com>
To: Bartosz Golaszewski <brgl@bgdev.pl>
Cc: Linus Walleij <linus.walleij@linaro.org>,
Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
Viresh Kumar <viresh.kumar@linaro.org>,
linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 2/2] gpiolib: cdev: export the consumer's PID
Date: Tue, 13 Sep 2022 22:28:41 +0800 [thread overview]
Message-ID: <YyCTmZocN/CY4Pg6@sol> (raw)
In-Reply-To: <CAMRc=MfoZQV-aHKSkAw6d_jPPbjn==oR0LA=irjuWLGzQiRP-w@mail.gmail.com>
On Tue, Sep 13, 2022 at 10:54:26AM +0200, Bartosz Golaszewski wrote:
> On Tue, Sep 13, 2022 at 4:12 AM Kent Gibson <warthog618@gmail.com> wrote:
> >
> > On Mon, Sep 12, 2022 at 11:56:17AM +0200, Bartosz Golaszewski wrote:
> > > On Mon, Sep 12, 2022 at 11:53 AM Kent Gibson <warthog618@gmail.com> wrote:
> > > >
> > >
> > > [snip]
> > >
> > > > >
> > > > > Using -1 sounds good but I've just realized there's a different
> > > > > problem. A process holding a file descriptor may fork and both the
> > > > > parent and the child will keep the same file descriptors open. Now
> > > > > we'll have two processes (with different PIDs) holding the same GPIO
> > > > > lines (specifically holding a file descriptor to the same anonymous
> > > > > inode).
> > > > >
> > > > > This already poses a problem for this patch as we'd need to return an
> > > > > array of PIDs which we don't have the space for but also is a
> > > > > situation which we haven't discussed previously IIRC - two processes
> > > > > keeping the same GPIO lines requested.
> > > > >
> > > > > I don't have any good idea on how to address this yet. One thing off
> > > > > the top of my head is: close the parent's file descriptor from kernel
> > > > > space (is it even possible?) on fork() (kind of like the close() on
> > > > > exec flag).
> > > > >
> > > > > I need to think about it more.
> > > > >
> > > >
> > > > I thought the O_CLOEXEC was set on the request fds exactly to prevent this
> > > > case - only one process can hold the request fd.
> > > >
> > >
> > > O_CLOEXEC means "close on exec" not "close on fork". When you fork,
> > > you inherit all file descriptors from your parent. Only once you call
> > > execve() are the fds with this flag closed *in the child*.
> > >
> >
> > Ah, ok.
> > You want to pass request fd ownership from parent to child??
> > Why not lock ownership to the parent, so O_CLOFORK, were that
> > available?
> >
>
> Because what if we want to request a line and then daemonize i.e. fork
> and exit in parent? It makes much more sense to keep the lines
> requested in the child IMO.
>
Then you are doing it backwards - daemonize first ;-).
Generally speaking, doesn't transfer of resource ownership to the forked
child create havoc in multi-threaded apps? i.e. one thread requests a
resource, another forks. The parent thread unknowingly loses ownership,
and the forked child process only starts with a replica of the forking
thread.
> During the BoF at Linux Plumbers it was suggested to use
> /proc/$PID/fdinfo to expose the information about which lines are
> requested but I can't figure out a way to do it elegantly.
>
Yeah, missed that :-(.
Makes sense.
As each request fd can contain multiple lines on a particular chip,
you would need to identify the gpiochip and the offsets for that request.
So two fields - the gpiochip path, and the list of offsets.
Is that already too clunky or am I missing something?
Cheers,
Kent.
next prev parent reply other threads:[~2022-09-13 15:09 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-09-09 12:13 [PATCH 0/2] gpiolib: export the consumer's PID to user-space Bartosz Golaszewski
2022-09-09 12:13 ` [PATCH 1/2] gpiolib: un-inline gpiod_request_user() Bartosz Golaszewski
2022-09-09 13:45 ` Andy Shevchenko
2022-09-10 14:51 ` Kent Gibson
2022-09-09 12:13 ` [PATCH 2/2] gpiolib: cdev: export the consumer's PID Bartosz Golaszewski
2022-09-09 13:45 ` Andy Shevchenko
2022-09-09 13:48 ` Andy Shevchenko
2022-09-09 19:18 ` Bartosz Golaszewski
2022-09-10 14:52 ` Kent Gibson
2022-09-12 8:52 ` Bartosz Golaszewski
2022-09-12 9:53 ` Kent Gibson
2022-09-12 9:56 ` Bartosz Golaszewski
2022-09-13 2:12 ` Kent Gibson
2022-09-13 8:54 ` Bartosz Golaszewski
2022-09-13 14:28 ` Kent Gibson [this message]
2022-09-13 14:35 ` Bartosz Golaszewski
2022-09-13 14:55 ` Kent Gibson
2022-09-13 15:58 ` Bartosz Golaszewski
2022-09-13 16:17 ` Kent Gibson
2022-09-13 17:07 ` Andy Shevchenko
2022-09-13 19:35 ` Bartosz Golaszewski
2022-09-14 1:00 ` Kent Gibson
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=YyCTmZocN/CY4Pg6@sol \
--to=warthog618@gmail.com \
--cc=andriy.shevchenko@linux.intel.com \
--cc=brgl@bgdev.pl \
--cc=linus.walleij@linaro.org \
--cc=linux-gpio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=viresh.kumar@linaro.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).