* Re: [PATCH 3/4] string: introduce memchr_inv
2011-08-22 20:52 ` [PATCH 3/4] string: introduce memchr_inv Andrew Morton
@ 2011-08-22 20:52 ` Andrew Morton
2011-08-22 21:50 ` Andrew Morton
` (2 subsequent siblings)
3 siblings, 0 replies; 6+ messages in thread
From: Andrew Morton @ 2011-08-22 20:52 UTC (permalink / raw)
To: Akinobu Mita
Cc: linux-kernel, linux-mm, Christoph Lameter, Pekka Enberg,
Matt Mackall, Joern Engel, logfs, Marcin Slusarz, Eric Dumazet,
linux-arch
On Tue, 23 Aug 2011 01:29:07 +0900
Akinobu Mita <akinobu.mita@gmail.com> wrote:
> memchr_inv() is mainly used to check whether the whole buffer is filled
> with just a specified byte.
>
> The function name and prototype are stolen from logfs and the
> implementation is from SLUB.
>
> ...
>
> +/**
> + * memchr_inv - Find a character in an area of memory.
> + * @s: The memory area
> + * @c: The byte to search for
> + * @n: The size of the area.
This text seems to be stolen from memchr(). I guess it's close enough.
> + * returns the address of the first character other than @c, or %NULL
> + * if the whole buffer contains just @c.
> + */
> +void *memchr_inv(const void *start, int c, size_t bytes)
> +{
> + u8 value = c;
> + u64 value64;
> + unsigned int words, prefix;
> +
> + if (bytes <= 16)
> + return check_bytes8(start, value, bytes);
> +
> + value64 = value | value << 8 | value << 16 | value << 24;
> + value64 = (value64 & 0xffffffff) | value64 << 32;
> + prefix = 8 - ((unsigned long)start) % 8;
> +
> + if (prefix) {
> + u8 *r = check_bytes8(start, value, prefix);
> + if (r)
> + return r;
> + start += prefix;
> + bytes -= prefix;
> + }
> +
> + words = bytes / 8;
> +
> + while (words) {
> + if (*(u64 *)start != value64)
OK, problem. This will explode if passed a misaligned address on
certain (non-x86) architectures. This is nasty because people will
develop and test code on x86 and it works. Much later, the
alpha/ia64/etc guys discover the problem.
One fix would be to use get_unaligned(). This might be slow on some
architectures, I don't know. Another fix is to restrict the caller's
alignment freedom; document this and add a runtime WARN_ON().
> + return check_bytes8(start, value, 8);
> + start += 8;
> + words--;
> + }
> +
> + return check_bytes8(start, value, bytes % 8);
> +}
> +EXPORT_SYMBOL(memchr_inv);
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH 3/4] string: introduce memchr_inv
2011-08-22 20:52 ` [PATCH 3/4] string: introduce memchr_inv Andrew Morton
2011-08-22 20:52 ` Andrew Morton
@ 2011-08-22 21:50 ` Andrew Morton
2011-08-22 21:50 ` Andrew Morton
2011-08-22 21:56 ` Eric Dumazet
3 siblings, 0 replies; 6+ messages in thread
From: Andrew Morton @ 2011-08-22 21:50 UTC (permalink / raw)
To: Akinobu Mita, linux-kernel, linux-mm, Christoph Lameter,
Pekka Enberg, Matt
On Mon, 22 Aug 2011 13:52:18 -0700
Andrew Morton <akpm@linux-foundation.org> wrote:
> > + value64 = value | value << 8 | value << 16 | value << 24;
> > + value64 = (value64 & 0xffffffff) | value64 << 32;
> > + prefix = 8 - ((unsigned long)start) % 8;
> > +
> > + if (prefix) {
> > + u8 *r = check_bytes8(start, value, prefix);
> > + if (r)
> > + return r;
> > + start += prefix;
> > + bytes -= prefix;
> > + }
> > +
> > + words = bytes / 8;
> > +
> > + while (words) {
> > + if (*(u64 *)start != value64)
>
> OK, problem. This will explode if passed a misaligned address on
> certain (non-x86) architectures.
pls ignore. As Marcin points out, I can't read.
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH 3/4] string: introduce memchr_inv
2011-08-22 20:52 ` [PATCH 3/4] string: introduce memchr_inv Andrew Morton
2011-08-22 20:52 ` Andrew Morton
2011-08-22 21:50 ` Andrew Morton
@ 2011-08-22 21:50 ` Andrew Morton
2011-08-22 21:56 ` Eric Dumazet
3 siblings, 0 replies; 6+ messages in thread
From: Andrew Morton @ 2011-08-22 21:50 UTC (permalink / raw)
To: Akinobu Mita, linux-kernel, linux-mm, Christoph Lameter,
Pekka Enberg, Matt Mackall, Joern Engel, logfs, Marcin Slusarz,
Eric Dumazet, linux-arch
On Mon, 22 Aug 2011 13:52:18 -0700
Andrew Morton <akpm@linux-foundation.org> wrote:
> > + value64 = value | value << 8 | value << 16 | value << 24;
> > + value64 = (value64 & 0xffffffff) | value64 << 32;
> > + prefix = 8 - ((unsigned long)start) % 8;
> > +
> > + if (prefix) {
> > + u8 *r = check_bytes8(start, value, prefix);
> > + if (r)
> > + return r;
> > + start += prefix;
> > + bytes -= prefix;
> > + }
> > +
> > + words = bytes / 8;
> > +
> > + while (words) {
> > + if (*(u64 *)start != value64)
>
> OK, problem. This will explode if passed a misaligned address on
> certain (non-x86) architectures.
pls ignore. As Marcin points out, I can't read.
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH 3/4] string: introduce memchr_inv
2011-08-22 20:52 ` [PATCH 3/4] string: introduce memchr_inv Andrew Morton
` (2 preceding siblings ...)
2011-08-22 21:50 ` Andrew Morton
@ 2011-08-22 21:56 ` Eric Dumazet
2011-08-22 21:56 ` Eric Dumazet
3 siblings, 1 reply; 6+ messages in thread
From: Eric Dumazet @ 2011-08-22 21:56 UTC (permalink / raw)
To: Andrew Morton
Cc: Akinobu Mita, linux-kernel, linux-mm, Christoph Lameter,
Pekka Enberg, Matt Mackall, Joern Engel, logfs, Marcin Slusarz,
linux-arch
Le lundi 22 août 2011 à 13:52 -0700, Andrew Morton a écrit :
> On Tue, 23 Aug 2011 01:29:07 +0900
> Akinobu Mita <akinobu.mita@gmail.com> wrote:
>
> > memchr_inv() is mainly used to check whether the whole buffer is filled
> > with just a specified byte.
> >
> > The function name and prototype are stolen from logfs and the
> > implementation is from SLUB.
> >
> > ...
> >
> > +/**
> > + * memchr_inv - Find a character in an area of memory.
> > + * @s: The memory area
> > + * @c: The byte to search for
> > + * @n: The size of the area.
>
> This text seems to be stolen from memchr(). I guess it's close enough.
>
> > + * returns the address of the first character other than @c, or %NULL
> > + * if the whole buffer contains just @c.
> > + */
> > +void *memchr_inv(const void *start, int c, size_t bytes)
> > +{
> > + u8 value = c;
> > + u64 value64;
> > + unsigned int words, prefix;
> > +
> > + if (bytes <= 16)
> > + return check_bytes8(start, value, bytes);
> > +
> > + value64 = value | value << 8 | value << 16 | value << 24;
> > + value64 = (value64 & 0xffffffff) | value64 << 32;
> > + prefix = 8 - ((unsigned long)start) % 8;
> > +
<snip>
> > + if (prefix) {
> > + u8 *r = check_bytes8(start, value, prefix);
> > + if (r)
> > + return r;
> > + start += prefix;
> > + bytes -= prefix;
> > + }
</snip>
Please note Andrew the previous code just make sure 'start' is aligned
on 8 bytes boundary. (It is suboptimal because if 'start' was already
aligned, we call the slow check_bytes(start, value, 8))
Code should probably do
prefix = (unsigned long)start % 8;
if (prefix) {
prefix = 8 - prefix;
r = check_bytes8(start, value, prefix);
...
> > +
> > + words = bytes / 8;
> > +
> > + while (words) {
> > + if (*(u64 *)start != value64)
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom internet charges in Canada: sign http://stopthemeter.ca/
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH 3/4] string: introduce memchr_inv
2011-08-22 21:56 ` Eric Dumazet
@ 2011-08-22 21:56 ` Eric Dumazet
0 siblings, 0 replies; 6+ messages in thread
From: Eric Dumazet @ 2011-08-22 21:56 UTC (permalink / raw)
To: Andrew Morton
Cc: Akinobu Mita, linux-kernel, linux-mm, Christoph Lameter,
Pekka Enberg, Matt Mackall, Joern Engel, logfs, Marcin Slusarz,
linux-arch
Le lundi 22 août 2011 à 13:52 -0700, Andrew Morton a écrit :
> On Tue, 23 Aug 2011 01:29:07 +0900
> Akinobu Mita <akinobu.mita@gmail.com> wrote:
>
> > memchr_inv() is mainly used to check whether the whole buffer is filled
> > with just a specified byte.
> >
> > The function name and prototype are stolen from logfs and the
> > implementation is from SLUB.
> >
> > ...
> >
> > +/**
> > + * memchr_inv - Find a character in an area of memory.
> > + * @s: The memory area
> > + * @c: The byte to search for
> > + * @n: The size of the area.
>
> This text seems to be stolen from memchr(). I guess it's close enough.
>
> > + * returns the address of the first character other than @c, or %NULL
> > + * if the whole buffer contains just @c.
> > + */
> > +void *memchr_inv(const void *start, int c, size_t bytes)
> > +{
> > + u8 value = c;
> > + u64 value64;
> > + unsigned int words, prefix;
> > +
> > + if (bytes <= 16)
> > + return check_bytes8(start, value, bytes);
> > +
> > + value64 = value | value << 8 | value << 16 | value << 24;
> > + value64 = (value64 & 0xffffffff) | value64 << 32;
> > + prefix = 8 - ((unsigned long)start) % 8;
> > +
<snip>
> > + if (prefix) {
> > + u8 *r = check_bytes8(start, value, prefix);
> > + if (r)
> > + return r;
> > + start += prefix;
> > + bytes -= prefix;
> > + }
</snip>
Please note Andrew the previous code just make sure 'start' is aligned
on 8 bytes boundary. (It is suboptimal because if 'start' was already
aligned, we call the slow check_bytes(start, value, 8))
Code should probably do
prefix = (unsigned long)start % 8;
if (prefix) {
prefix = 8 - prefix;
r = check_bytes8(start, value, prefix);
...
> > +
> > + words = bytes / 8;
> > +
> > + while (words) {
> > + if (*(u64 *)start != value64)
^ permalink raw reply [flat|nested] 6+ messages in thread