public inbox for linux-gpio@vger.kernel.org
 help / color / mirror / Atom feed
From: Kent Gibson <warthog618@gmail.com>
To: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: Linus Walleij <linus.walleij@linaro.org>,
	Bartosz Golaszewski <bgolaszewski@baylibre.com>,
	linux-gpio@vger.kernel.org, Arnd Bergmann <arnd@arndb.de>
Subject: Re: [PATCH v1 1/2] gpiolib: Fix line event handling in syscall compatible mode
Date: Fri, 11 Sep 2020 17:12:49 +0800	[thread overview]
Message-ID: <20200911091249.GA1874731@sol> (raw)
In-Reply-To: <20200911083109.GF1891694@smile.fi.intel.com>

On Fri, Sep 11, 2020 at 11:31:09AM +0300, Andy Shevchenko wrote:
> On Fri, Sep 11, 2020 at 11:05:39AM +0800, Kent Gibson wrote:
> > On Thu, Sep 10, 2020 at 01:19:34PM +0300, Andy Shevchenko wrote:
> > > The introduced line even handling ABI in the commit
> > > 
> > 
> > s/even/event/
> > 
> > >   61f922db7221 ("gpio: userspace ABI for reading GPIO line events")
> > > 
> > > missed the fact that 64-bit kernel may serve for 32-bit applications.
> > > In such case the very first check in the lineevent_read() will fail
> > > due to alignment differences.
> > > 
> > > To workaround this introduce lineeven_to_user() helper which returns actual
> > > size of the structure and copies its content to user if asked.
> > > 
> > 
> > And again here.
> 
> Thanks!
>
> ...
> 
> > > +#ifdef __x86_64__
> > > +	/* i386 has no padding after 'id' */
> > > +	if (in_ia32_syscall()) {
> > > +		struct compat_ge {
> > > +			compat_u64	timestamp __packed;
> > > +			u32		id;
> > > +		} cge;
> > > +
> > > +		if (buf && ge) {
> > > +			cge = (struct compat_ge){ ge->timestamp, ge->id };
> > > +			if (copy_to_user(buf, &cge, sizeof(cge)))
> > > +				return -EFAULT;
> > > +		}
> > > +
> > > +		return sizeof(cge);
> > > +	}
> > > +#endif
> > > +
> > > +	if (buf && ge) {
> > > +		if (copy_to_user(buf, ge, sizeof(*ge)))
> > > +			return -EFAULT;
> > > +	}
> > > +
> > > +	return sizeof(*ge);
> > > +}
> > >  
> > 
> > The dual-purpose nature of this function makes it more complicated than
> > it needs to be.
> > I was going to suggest splitting it into separate functions, but...
> > 
> > Given that struct compat_ge is a strict truncation of struct
> > gpioevent_data, how about reducing this function to just determining the
> > size of the event for user space, say lineevent_user_size(), and
> > replacing sizeof(ge) with that calculcated size throughout
> > lineevent_read()?
> 
> So you mean something like
> 
> 	struct compat_gpioeevent_data {
> 		compat_u64	timestamp;
> 		u32		id;
> 	} __packed;
> 
> #ifdef __x86_64__
> 	/* i386 has no padding after 'id' */
> 	size_t ge_size = in_ia32_syscall() ? sizeof(struct compat_gpioevent_data) : sizeof(struct gpioevent_data);
> #else
> 	size_t ge_size = sizeof(struct gpioevent_data) ;
> #endif
> 
> ?
> 

Pretty much, though I was suggesting keeping it in a helper function,
say lineevent_user_size(), not in lineevent_read() itself, to isolate
all the ugliness in one small place.

So in lineevent_read() you would:

   size_t ge_size = lineevent_user_size();

and then use that to replace all the sizeof(ge) occurrences.

Cheers,
Kent.

  reply	other threads:[~2020-09-11  9:13 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-10 10:19 [PATCH v1 1/2] gpiolib: Fix line event handling in syscall compatible mode Andy Shevchenko
2020-09-10 10:19 ` [PATCH v1 2/2] gpiolib: convert to use DEFINE_SEQ_ATTRIBUTE macro Andy Shevchenko
2020-09-11 15:45   ` Bartosz Golaszewski
2020-09-11  1:37 ` [PATCH v1 1/2] gpiolib: Fix line event handling in syscall compatible mode kernel test robot
2020-09-11  3:05 ` Kent Gibson
2020-09-11  8:31   ` Andy Shevchenko
2020-09-11  9:12     ` Kent Gibson [this message]
2020-09-11  9:53       ` Andy Shevchenko
2020-09-11 10:17         ` Kent Gibson
     [not found]           ` <20200911142846.GM1891694@smile.fi.intel.com>
2020-09-12  2:21             ` Kent Gibson
2020-09-14 12:44               ` Bartosz Golaszewski
2020-09-14 13:04                 ` Andy Shevchenko
2020-09-11 16:20 ` Arnd Bergmann
2020-09-14 14:26   ` Andy Shevchenko

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=20200911091249.GA1874731@sol \
    --to=warthog618@gmail.com \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=arnd@arndb.de \
    --cc=bgolaszewski@baylibre.com \
    --cc=linus.walleij@linaro.org \
    --cc=linux-gpio@vger.kernel.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