All of lore.kernel.org
 help / color / mirror / Atom feed
From: Arnd Bergmann <arnd@arndb.de>
To: Ingo Molnar <mingo@elte.hu>
Cc: x86@kernel.org, linux-kernel@vger.kernel.org,
	Remis Lima Baima <remis.developer@googlemail.com>
Subject: Re: [PATCH v2] x86: fix ktermios-termio conversion
Date: Tue, 12 May 2009 11:04:30 +0200	[thread overview]
Message-ID: <200905121104.31275.arnd@arndb.de> (raw)
In-Reply-To: <20090512075513.GA14122@elte.hu>

On Tuesday 12 May 2009, Ingo Molnar wrote:
> Hm, that looks a bit ugly and it also adds 150 bytes of bloat to the 
> kernel:
> 
> drivers/char/tty_ioctl.o:
> 
>    text    data     bss     dec     hex filename
>    4704       0       0    4704    1260 tty_ioctl.o.before
>    4841       0       0    4841    12e9 tty_ioctl.o.after

It's easy to go back to a macro (or replace it by another inline
function if you prefer that stylistically) to make it look
nicer.

Regarding the bloat, I'm still looking for a better version.
The code I posted is basically what is in asm-generic/termios.h
traditionally, so at least I'm reasonably confident that it
is correct, while the x86 version currently fails to return
-EFAULT on incorrect pointers and misses c_line changes.

I now tried this version, which theoretically should be
fairly compact on most architectures:

static inline int set_low_termios_bits(unsigned int *termios,
			const short __user *termio)
{
	unsigned short tmp;
	int ret;

	ret = __get_user(tmp, termio);
	*termios = (0xffff0000 & *termios) | tmp;

	return ret;
}

/*
 * Translate a "termio" structure into a "termios". Ugh.
 */
static inline int user_termio_to_kernel_termios(struct ktermios *termios,
						const struct termio __user *termio)
{
	if (access_ok(VERIFY_READ, termio, sizeof (*termio)))
		return -EFAULT;

	return set_low_termios_bits(&termios->c_iflag, &termio->c_iflag) |
	       set_low_termios_bits(&termios->c_oflag, &termio->c_oflag) |
	       set_low_termios_bits(&termios->c_cflag, &termio->c_cflag) |
	       set_low_termios_bits(&termios->c_lflag, &termio->c_lflag) |
	       __get_user(termios->c_line, &termio->c_line) |
	       (__copy_from_user(termios->c_cc, termio->c_cc, NCC)
				? -EFAULT : 0);
}

/*
 * Translate a "termios" structure into a "termio". Ugh.
 */
static inline int kernel_termios_to_user_termio(struct termio __user *termio,
						struct ktermios *termios)
{
	if (access_ok(VERIFY_WRITE, termio, sizeof (*termio)))
		return -EFAULT;

	return __put_user(termios->c_iflag, &termio->c_iflag) |
	       __put_user(termios->c_oflag, &termio->c_oflag) |
	       __put_user(termios->c_cflag, &termio->c_cflag) |
	       __put_user(termios->c_lflag, &termio->c_lflag) |
	       __put_user(termios->c_line,  &termio->c_line) |
	       (__copy_to_user(termio->c_cc, termios->c_cc, NCC)
				? -EFAULT : 0);
}

Unfortunately, this is even more code on x86, where get_user/put_user
is out-of-line, while __get_user/__put_user is inline and produces
fixup code.
The best I could come up with is somewhat slower but also shorter:

static inline int set_low_termios_bits(unsigned int *termios,
			const short __user *termio)
{
	unsigned short tmp;
	int ret;

	ret = get_user(tmp, termio);
	*termios = (0xffff0000 & *termios) | tmp;

	return ret;
}

/*
 * Translate a "termio" structure into a "termios". Ugh.
 */
static inline int user_termio_to_kernel_termios(struct ktermios *termios,
						const struct termio __user *termio)
{
	return (set_low_termios_bits(&termios->c_iflag, &termio->c_iflag) ||
		set_low_termios_bits(&termios->c_oflag, &termio->c_oflag) ||
		set_low_termios_bits(&termios->c_cflag, &termio->c_cflag) ||
		set_low_termios_bits(&termios->c_lflag, &termio->c_lflag) ||
		get_user(termios->c_line, &termio->c_line) ||
		copy_from_user(termios->c_cc, termio->c_cc, NCC))
				? -EFAULT : 0;
}

/*
 * Translate a "termios" structure into a "termio". Ugh.
 */
static inline int kernel_termios_to_user_termio(struct termio __user *termio,
						struct ktermios *termios)
{
	return put_user(termios->c_iflag, &termio->c_iflag) ||
	       put_user(termios->c_oflag, &termio->c_oflag) ||
	       put_user(termios->c_cflag, &termio->c_cflag) ||
	       put_user(termios->c_lflag, &termio->c_lflag) ||
	       put_user(termios->c_line,  &termio->c_line) ||
	       (copy_to_user(termio->c_cc, termios->c_cc, NCC))
				? -EFAULT : 0;
}

If you think that looks better, I can send another patch. I have
not tested this code functionally though.

	Arnd <><

  reply	other threads:[~2009-05-12  9:06 UTC|newest]

Thread overview: 86+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20090511222702.352192505@arndb.de>
2009-05-11 22:37 ` [PATCH] mb862xxfb: use CONFIG_OF instead of CONFIG_PPC_OF Arnd Bergmann
2009-05-13 12:25   ` Anatolij Gustschin
2009-05-11 22:38 ` [PATCH] ata: libata depends on HAS_DMA Arnd Bergmann
2009-05-12  0:58   ` Jeff Garzik
2009-05-12 12:40     ` Arnd Bergmann
2009-05-12 13:05       ` Arnd Bergmann
2009-05-12 13:05         ` Arnd Bergmann
2009-05-12  8:06   ` Alan Cox
2009-05-12  9:23     ` Arnd Bergmann
2009-05-12  9:23       ` Arnd Bergmann
2009-05-13  3:30       ` Brad Boyer
2009-05-13  3:30         ` Brad Boyer
2009-05-13  4:12         ` Michael Schmitz
2009-05-13  4:12         ` Michael Schmitz
2009-05-13  4:12           ` Michael Schmitz
2009-05-13  4:34           ` Brad Boyer
2009-05-13  4:34             ` Brad Boyer
2009-05-13  8:51             ` Alan Cox
2009-05-13  8:55               ` Geert Uytterhoeven
2009-05-13 23:57               ` Robert Hancock
2009-05-14  0:18                 ` FUJITA Tomonori
2009-05-15  5:31                   ` Tejun Heo
2009-05-15 11:16                     ` Arnd Bergmann
2009-05-15 11:16                       ` Arnd Bergmann
2009-05-15 11:21                       ` Tejun Heo
2009-05-15 11:55                         ` Arnd Bergmann
2009-05-17  9:00                           ` Robert Hancock
2009-05-17 19:38                             ` Arnd Bergmann
2009-05-17 20:05                               ` Jeff Garzik
2009-05-17 22:45                                 ` [PATCH] asm-generic: add a dma-mapping.h file Arnd Bergmann
2009-05-18  6:03                                   ` Geert Uytterhoeven
2009-05-18  6:03                                     ` Geert Uytterhoeven
2009-05-18  8:28                                     ` Arnd Bergmann
2009-05-18 10:45                                   ` FUJITA Tomonori
2009-05-18 14:45                                     ` Arnd Bergmann
2009-05-18 22:44                                       ` FUJITA Tomonori
2009-05-19 16:22                                         ` Arnd Bergmann
2009-05-19 17:01                                           ` Grant Grundler
2009-05-19 17:40                                             ` Arnd Bergmann
2009-05-19 18:08                                               ` Grant Grundler
2009-05-19 18:08                                                 ` Grant Grundler
2009-05-22 12:12                                           ` FUJITA Tomonori
2009-05-22 14:07                                             ` Arnd Bergmann
2009-05-22 14:38                                               ` FUJITA Tomonori
2009-05-22 15:05                                                 ` Arnd Bergmann
2009-05-26  4:36                                                   ` FUJITA Tomonori
2009-05-26 12:35                                                     ` Arnd Bergmann
2009-05-27  3:58                                                       ` FUJITA Tomonori
2009-05-18 22:54                                   ` Jeff Garzik
2009-05-18 23:22                                     ` FUJITA Tomonori
2009-05-18 10:45                       ` [PATCH] ata: libata depends on HAS_DMA FUJITA Tomonori
2009-05-13 10:39         ` Arnd Bergmann
2009-05-13 10:39           ` Arnd Bergmann
2009-05-13 10:39         ` Arnd Bergmann
2009-05-11 22:40 ` [PATCH] scsi: libsas " Arnd Bergmann
2009-05-11 22:50   ` James Bottomley
2009-05-11 22:59     ` James Bottomley
2009-05-11 23:16       ` Arnd Bergmann
2009-05-11 22:43 ` [PATCH] arm: rename CLOCK_TICK_RATE to ARM_TICK_RATE Arnd Bergmann
2009-05-11 23:11   ` [PATCH v2] " Arnd Bergmann
2009-05-13 17:11   ` [PATCH] " Arnd Bergmann
2009-05-11 22:50 ` [PATCH] x86: use PIT_TICK_RATE consistently Arnd Bergmann
2009-05-11 23:05   ` Arnd Bergmann
2009-05-11 22:55 ` [PATCH] move PIT_TICK_RATE to linux/timex.h Arnd Bergmann
2009-05-11 22:55   ` Arnd Bergmann
2009-05-12  0:01   ` Andrew Morton
2009-05-12  0:36     ` Arnd Bergmann
2009-05-11 22:57 ` [PATCH] mips: use PIT_TICK_RATE in i8253 Arnd Bergmann
2009-05-11 22:58 ` [PATCH] input: use PIT_TICK_RATE in vt beep ioctl Arnd Bergmann
2009-05-12  9:31   ` Alan Cox
2009-05-11 22:59 ` [PATCH] x86: fix ktermios-termio conversion Arnd Bergmann
2009-05-11 23:19   ` [PATCH v2] " Arnd Bergmann
2009-05-12  7:55     ` Ingo Molnar
2009-05-12  9:04       ` Arnd Bergmann [this message]
2009-05-12  9:10         ` Ingo Molnar
2009-05-12  9:21           ` Alan Cox
2009-05-12  9:26             ` Ingo Molnar
2009-05-12 10:05               ` Alan Cox
2009-05-12 10:15                 ` Ingo Molnar
2009-05-12 11:33                 ` [PATCH v3] " Arnd Bergmann
2009-05-12 11:42           ` [PATCH v2] " Arnd Bergmann
2009-05-12  9:17         ` Alan Cox
2009-05-11 23:02 ` [PATCH] ipc: use __ARCH_WANT_IPC_PARSE_VERSION in ipc/util.h Arnd Bergmann
2009-05-12  0:19   ` Serge E. Hallyn
2009-05-11 23:03 ` [PATCH] syscalls.h add the missing sys_pipe2 declaration Arnd Bergmann
2009-05-11 23:08   ` Arnd Bergmann

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=200905121104.31275.arnd@arndb.de \
    --to=arnd@arndb.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=remis.developer@googlemail.com \
    --cc=x86@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.