linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Andrew Morton <akpm@linux-foundation.org>
To: Akinobu Mita <akinobu.mita@gmail.com>
Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org,
	Christoph Lameter <cl@linux-foundation.org>,
	Pekka Enberg <penberg@kernel.org>, Matt Mackall <mpm@selenic.com>,
	Joern Engel <joern@logfs.org>,
	logfs@logfs.org, Marcin Slusarz <marcin.slusarz@gmail.com>,
	Eric Dumazet <eric.dumazet@gmail.com>,
	linux-arch@vger.kernel.org
Subject: Re: [PATCH 3/4] string: introduce memchr_inv
Date: Mon, 22 Aug 2011 13:52:18 -0700	[thread overview]
Message-ID: <20110822135218.f2d9f462.akpm@linux-foundation.org> (raw)
In-Reply-To: <1314030548-21082-4-git-send-email-akinobu.mita@gmail.com>

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);

--
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>

  parent reply	other threads:[~2011-08-22 20:52 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-08-22 16:29 [PATCH 0/4] debug-pagealloc improvements Akinobu Mita
2011-08-22 16:29 ` [PATCH 1/4] debug-pagealloc: use plain __ratelimit() instead of printk_ratelimit() Akinobu Mita
2011-08-22 16:29 ` [PATCH 2/4] debug-pagealloc: add support for highmem pages Akinobu Mita
2011-08-22 20:43   ` Andrew Morton
2011-08-23 15:26     ` Akinobu Mita
2011-08-22 16:29 ` [PATCH 3/4] string: introduce memchr_inv Akinobu Mita
2011-08-22 16:57   ` Pekka Enberg
2011-08-22 17:09     ` Jörn Engel
2011-08-22 20:52   ` Andrew Morton [this message]
2011-08-22 21:50     ` Andrew Morton
2011-08-22 21:56     ` Eric Dumazet
2011-08-22 20:59   ` Geert Uytterhoeven
2011-08-23 14:31     ` Christoph Lameter
2011-08-22 16:29 ` [PATCH 4/4] debug-pagealloc: use memchr_inv Akinobu Mita

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=20110822135218.f2d9f462.akpm@linux-foundation.org \
    --to=akpm@linux-foundation.org \
    --cc=akinobu.mita@gmail.com \
    --cc=cl@linux-foundation.org \
    --cc=eric.dumazet@gmail.com \
    --cc=joern@logfs.org \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=logfs@logfs.org \
    --cc=marcin.slusarz@gmail.com \
    --cc=mpm@selenic.com \
    --cc=penberg@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;
as well as URLs for NNTP newsgroup(s).