mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "H. Peter Anvin" <hpa@zytor.com>
To: Masahiro Yamada <yamada.masahiro@socionext.com>,
	Andrew Morton <akpm@linux-foundation.org>
Cc: "Niklas Söderlund" <niklas.soderlund+renesas@ragnatech.se>,
	"Jessica Yu" <jeyu@redhat.com>,
	"Johannes Berg" <johannes.berg@intel.com>,
	"Petr Mladek" <pmladek@suse.com>,
	"Larry Finger" <Larry.Finger@lwfinger.net>,
	linux-kernel@vger.kernel.org,
	"Gustavo Padovan" <gustavo.padovan@collabora.co.uk>,
	"Rasmus Villemoes" <linux@rasmusvillemoes.dk>,
	"Peter Zijlstra" <peterz@infradead.org>,
	"Luis de Bethencourt" <luisbg@osg.samsung.com>,
	"Josh Triplett" <josh@joshtriplett.org>
Subject: Re: [PATCH] kernel.h: add IS_PTR_ALIGNED() macro
Date: Wed, 29 Mar 2017 14:24:47 -0700	[thread overview]
Message-ID: <bed3e1c2-7d2f-1883-edc6-263f21365340@zytor.com> (raw)
In-Reply-To: <1490782930-30429-1-git-send-email-yamada.masahiro@socionext.com>

[-- Attachment #1: Type: text/plain, Size: 1256 bytes --]

On 03/29/17 03:22, Masahiro Yamada wrote:
> We often check if a pointer has a specific alignment.  Because the
> '&' (bitwise AND) operator cannot take a pointer for the operand,
> so we need a cast like, IS_ALIGNED((unsigned long)p, a).
> 
> IS_PTR_ALIGNED will be useful as a shorthand.
> 
> Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
> ---
> 
>  include/linux/kernel.h | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/include/linux/kernel.h b/include/linux/kernel.h
> index e5edd55..a810e4b 100644
> --- a/include/linux/kernel.h
> +++ b/include/linux/kernel.h
> @@ -50,6 +50,7 @@
>  #define __ALIGN_MASK(x, mask)	__ALIGN_KERNEL_MASK((x), (mask))
>  #define PTR_ALIGN(p, a)		((typeof(p))ALIGN((unsigned long)(p), (a)))
>  #define IS_ALIGNED(x, a)		(((x) & ((typeof(x))(a) - 1)) == 0)
> +#define IS_PTR_ALIGNED(p, a)		(IS_ALIGNED((unsigned long)p, a))
>  

No need for two macros; make one work for both.

You could move the __inttype() macro from arch/x86/include/asm/uaccess.h
into this file and replace typeof(x) with __inttype(x) in the above macro.

Attached is a set of slightly improved (safer and a bit more
generalized) versions of the same macro that might be more appropriate
to include in <linux/kernel.h>.

	-hpa

[-- Attachment #2: uinttype.c --]
[-- Type: text/plain, Size: 1602 bytes --]

/*
 * These are types: corresponding to the smallest unsigned/signed
 * integer type no smaller than int or long, respectively, capable of
 * holding the argument (which can be anything that can be validly
 * passed to sizeof().)
 *
 * If the size is too large even for a long long, they return an
 * unnamed structure type with the right sizeof(), which if used for a
 * cast will generate the error message:
 *
 * error: conversion to non-scalar type requested
 */
#define __uinttype(x)							\
  __typeof__(__builtin_choose_expr(sizeof(x) <= sizeof(0U), 0U,		\
             __builtin_choose_expr(sizeof(x) <= sizeof(0UL), 0UL,	\
	     __builtin_choose_expr(sizeof(x) <= sizeof(0ULL), 0ULL,	\
	     ({ struct { unsigned char __bad[sizeof(x)]; } __bad = {{0}}; \
	        __bad; })))))
#define __sinttype(x)							\
  __typeof__(__builtin_choose_expr(sizeof(x) <= sizeof(0), 0,		\
             __builtin_choose_expr(sizeof(x) <= sizeof(0L), 0L,		\
	     __builtin_choose_expr(sizeof(x) <= sizeof(0LL), 0LL,	\
	     ({ struct { signed char __bad[sizeof(x)]; } __bad = {{0}}; \
	        __bad; })))))
#define __ulongtype(x)							\
  __typeof__(__builtin_choose_expr(sizeof(x) <= sizeof(0UL), 0UL,	\
	     __builtin_choose_expr(sizeof(x) <= sizeof(0ULL), 0ULL,	\
	     ({ struct { unsigned char __bad[sizeof(x)]; } __bad = {{0}}; \
	        __bad; }))))
#define __slongtype(x)							\
  __typeof__(__builtin_choose_expr(sizeof(x) <= sizeof(0L), 0L,		\
	     __builtin_choose_expr(sizeof(x) <= sizeof(0LL), 0LL,	\
	     ({ struct { signed char __bad[sizeof(x)]; } __bad = {{0}}; \
	        __bad; })))))

  reply	other threads:[~2017-03-29 21:38 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-29 10:22 Masahiro Yamada
2017-03-29 21:24 ` H. Peter Anvin [this message]
2017-03-30  1:57   ` Masahiro Yamada
2017-03-30  3:29     ` H. Peter Anvin
2017-03-30 15:22       ` Masahiro Yamada
2017-03-29 21:36 ` Andrew Morton

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=bed3e1c2-7d2f-1883-edc6-263f21365340@zytor.com \
    --to=hpa@zytor.com \
    --cc=Larry.Finger@lwfinger.net \
    --cc=akpm@linux-foundation.org \
    --cc=gustavo.padovan@collabora.co.uk \
    --cc=jeyu@redhat.com \
    --cc=johannes.berg@intel.com \
    --cc=josh@joshtriplett.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    --cc=luisbg@osg.samsung.com \
    --cc=niklas.soderlund+renesas@ragnatech.se \
    --cc=peterz@infradead.org \
    --cc=pmladek@suse.com \
    --cc=yamada.masahiro@socionext.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

all inboxes | Powered by JetHome®