linux-arch.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: David Miller <davem@davemloft.net>
To: torvalds@linux-foundation.org
Cc: linux-arch@vger.kernel.org
Subject: Re: Mostly portable strnlen_user()
Date: Fri, 25 May 2012 19:06:25 -0400 (EDT)	[thread overview]
Message-ID: <20120525.190625.1306293406531703607.davem@davemloft.net> (raw)
In-Reply-To: <alpine.LFD.2.02.1205251528570.26378@i5.linux-foundation.org>

From: Linus Torvalds <torvalds@linux-foundation.org>
Date: Fri, 25 May 2012 15:35:38 -0700 (PDT)

> So it's not truly generic, but it's in lib/strnlen_user.c and under the 
> CONFIG_GENERIC_STRNLEN_USER config option, because I'm convinced it should 
> be possible to do something that does the right thing for everybody. 

It should be relatively easy, especially since you work with aligned
words.

> What's the appropriate *clean* interface to allow architectures 
> to tweak the little details?

Maybe something like:

#define HAS_ZERO_CONST_DECLARE	...
#define HAS_ZERO_VAR_DECLARE	...
#define HAS_ZERO_ARGS		...

static inline unsigned long has_zero(unsigned long c ...)
{
}

#define FIND_ZERO_ARGS		...

static inline long find_zero(unsigned long c, unsigned long mask ...)
{
}

So at the use sites it's something like:

long do_strwhatever_user()
{
	HAS_ZERO_CONST_DECLARE;

	...

	while (max >= sizeof(unsigned long)) {
		unsigned long mask, c;
		HAS_ZERO_VAR_DECLARE;

		mask = has_zero(c HAS_ZERO_ARGS);
		if (mask)
			return res + find_zero(c, mask FIND_ZERO_ARGS);
	...
	}
	...
}

A little ugly.  The main problem is the magic args macros.

We can kill off the HAS_ZERO_CONST_DECLARE booger by instead
exposing the magic constants used by the particular has_zero()
implementation.  Then we'll have:

#define HAVE_ZERO_CONST1	REPEAT_BYTE(X)
#define HAVE_ZERO_CONST2	REPEAT_BYTE(X)

static inline unsigned long has_zero(unsigned long c,
				     const unsigned long magic1,
				     const unsigned long magic2);

and then it becomes something like:

long do_strwhatever_user()
{
	const unsigned long magic1 = HAVE_ZERO_CONST1;
	const unsigned long magic2 = HAVE_ZERO_CONST2;

	...

	while (max >= sizeof(unsigned long)) {
		unsigned long mask, c;
		HAS_ZERO_VAR_DECLARE;

		mask = has_zero(c, magic1, magic2);
		if (mask)
			return res + find_zero(c, mask, magic1, magic2);

The remaining problem, which I was trying to accomodate with the funky
ARGS macros, is propagating part of the has_zero() calculation into the
find_zero() call.

Hmmm...

  reply	other threads:[~2012-05-25 23:06 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-05-25 22:35 Mostly portable strnlen_user() Linus Torvalds
2012-05-25 23:06 ` David Miller [this message]
2012-05-25 23:11   ` Linus Torvalds
2012-05-25 23:14     ` David Miller
2012-05-25 23:37       ` Linus Torvalds
2012-05-25 23:41         ` David Miller
2012-05-26  0:41           ` David Miller
2012-05-26  1:09             ` Linus Torvalds
2012-05-26  1:21               ` David Miller
2012-05-26  2:13               ` Linus Torvalds
2012-05-26  2:43                 ` David Miller
2012-05-26  3:56                   ` Linus Torvalds
2012-05-26  4:15                     ` David Miller
2012-05-26  4:19                       ` Linus Torvalds
2012-05-26  4:34                         ` David Miller
2012-05-26  4:44                         ` David Miller
2012-05-26  5:06                           ` Linus Torvalds
2012-05-26  5:59                             ` David Miller
2012-05-26  8:32                         ` Jonas Bonn
2012-05-26 18:39                           ` Linus Torvalds
2012-05-26 23:46                             ` David Miller
2012-05-27  8:28                             ` Jonas Bonn
2012-05-28  3:07                             ` Paul Mackerras
2012-05-28  3:47                               ` Linus Torvalds
2012-05-28  3:53                                 ` Linus Torvalds
2012-05-26  4:16                     ` Linus Torvalds

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=20120525.190625.1306293406531703607.davem@davemloft.net \
    --to=davem@davemloft.net \
    --cc=linux-arch@vger.kernel.org \
    --cc=torvalds@linux-foundation.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).