linux-arch.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Bernd Petrovitsch <bernd@sysprog.at>
To: Namhyung Kim <namhyung@gmail.com>
Cc: Arnd Bergmann <arnd@arndb.de>,
	linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [RFC][PATCH] introduce ptr_diff()
Date: Thu, 19 Aug 2010 13:57:38 +0200	[thread overview]
Message-ID: <1282219058.10440.26.camel@thorin> (raw)
In-Reply-To: <1282217856-8625-1-git-send-email-namhyung@gmail.com>

On Don, 2010-08-19 at 20:37 +0900, Namhyung Kim wrote:
> When I compiled allyesconfig'ed kernel with C=1, I got 1519 lines of
> following message:
> 
>  include/linux/mm.h:599:16: warning: potentially expensive pointer subtraction
> 
> which is around 10% of total warnings. this was caused by page_to_pfn() macro
> so I think it's worth to remove it by calculating pointer subtraction
> manually.
> 
> ptr_diff() does subtraction of two pointers as if they were integer values
> and divides it by the size of their base type. Currently it does not deal
> with a difference alignment requirement than the size of base type, it
> would be a trivial job.
> 
> Impact: remove a _lot_ of sparse warnings
> Signed-off-by: Namhyung Kim <namhyung@gmail.com>
> ---
> 
> I have no idea where the right place is.
> Please kindly point me to the place if here is not the one.
> And any comments are greatly welcomed also.
> 
> Thanks.
> 
>  include/asm-generic/memory_model.h |   23 +++++++++++++++++++----
>  1 files changed, 19 insertions(+), 4 deletions(-)
> 
> diff --git a/include/asm-generic/memory_model.h b/include/asm-generic/memory_model.h
> index fb2d63f..ffec625 100644
> --- a/include/asm-generic/memory_model.h
> +++ b/include/asm-generic/memory_model.h
> @@ -22,13 +22,28 @@
>  
>  #endif /* CONFIG_DISCONTIGMEM */
>  
> +#include <linux/log2.h>
> +
> +#define ptr_diff(p1, p2)					\
> +({	long __diff;						\
> +	unsigned long  __addr1 = (unsigned long) (p1);		\
> +	unsigned long  __addr2 = (unsigned long) (p2);		\
> +	unsigned __size = sizeof(typeof(*p1));			\
The typeof() is actually redundant here.
> +								\
> +	if (is_power_of_2(__size))				\
> +		__diff = (__addr1 - __addr2) >> ilog2(__size);	\
> +	else							\
> +		__diff = (__addr1 - __addr2) / __size;		\
> +	__diff;							\
> +})
> +

IMHO this kills gcc's type compatibility checks on p1 and p2 (even if
they are suboptimal).
There is the "__same_type" macro in compiler.h for this or the "else"
branch uses plain C
	__diff = p1 - p2;
to keep them (which probably also keeps sparse's warnings).

Actually one would assume that gcc internally just does the above as the
size of the type is statically known (and sparse could be improved to
not warn where sizeof(*p1) is a power of 2).

	Bernd
-- 
mobile: +43 664 4416156              http://www.sysprog.at/
    Linux Software Development, Consulting and Services

  reply	other threads:[~2010-08-19 11:57 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-08-19 11:37 [RFC][PATCH] introduce ptr_diff() Namhyung Kim
2010-08-19 11:57 ` Bernd Petrovitsch [this message]
2010-08-19 12:03 ` David Howells
2010-08-19 12:23 ` Andi Kleen
2010-08-19 12:40   ` Matthew Wilcox
2010-08-19 12:58     ` Andi Kleen
2010-08-19 12:58       ` Andi Kleen
2010-08-19 12:48   ` Bernd Petrovitsch
2010-08-19 12:48     ` Bernd Petrovitsch
2010-08-19 12:59     ` Andi Kleen
2010-08-19 13:05       ` Bernd Petrovitsch
2010-08-19 13:39         ` Andi Kleen
2010-08-20 12:12       ` Al Viro
2010-08-19 15:53   ` Namhyung Kim
2010-08-19 16:04     ` Bernd Petrovitsch
2010-08-19 16:12       ` Namhyung Kim
2010-08-20 12:07   ` Al Viro
2010-08-20 12:49     ` Matthew Wilcox
2010-08-20 12:57       ` Andi Kleen

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=1282219058.10440.26.camel@thorin \
    --to=bernd@sysprog.at \
    --cc=arnd@arndb.de \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=namhyung@gmail.com \
    /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).