linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [RFC PATCH 01/20] Create generic alignment API (v8)
       [not found] <20100817231619.277457797@efficios.com>
@ 2010-08-17 23:16 ` Mathieu Desnoyers
  2010-08-18  0:00   ` Kirill A. Shutemov
  2010-08-18  1:25   ` Steven Rostedt
  0 siblings, 2 replies; 5+ messages in thread
From: Mathieu Desnoyers @ 2010-08-17 23:16 UTC (permalink / raw)
  To: linux-arm-kernel

An embedded and charset-unspecified text was scrubbed...
Name: create-generic-alignment-api.patch
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20100817/03b2937e/attachment.ksh>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [RFC PATCH 01/20] Create generic alignment API (v8)
  2010-08-17 23:16 ` [RFC PATCH 01/20] Create generic alignment API (v8) Mathieu Desnoyers
@ 2010-08-18  0:00   ` Kirill A. Shutemov
  2010-08-18  1:05     ` Mathieu Desnoyers
  2010-08-18  1:25   ` Steven Rostedt
  1 sibling, 1 reply; 5+ messages in thread
From: Kirill A. Shutemov @ 2010-08-18  0:00 UTC (permalink / raw)
  To: linux-arm-kernel

On Tue, Aug 17, 2010 at 07:16:20PM -0400, Mathieu Desnoyers wrote:
> Rather than re-doing the "alignment on a type size" trick all over again at
> different levels, import the "ltt_align" from LTTng into kernel.h and make this
> available to everyone. Renaming to:
> 
> - object_align()
> - object_align_floor()
> - offset_align()
> - offset_align_floor()
> 
> Changelog since v7:
> - Add missing include/linux/Kconfig header-y.
> 
> Changelog since v6:
> - Adapt to changes introduced by
>   commit a79ff731a1b277d0e92d9453bdf374e04cec717a
> - Use __alignof__() instead of sizeof() to support compound types.
> 
> Changelog since v5:
> - moved alignment apis to a separate header file so that it is possible
>   to use them from other header files which are, for example, included
>   from kernel.h.
> 
> Changelog since v4:
> - add missing ( ) around parameters within object_align() and
>   object_align_floor().
> - More coding style cleanups to ALIGN() (checkpatch.pl is happy now).
> 
> Changelog since v3:
> - optimize object_align*() so fewer instructions are needed for alignment of
>   addresses known dynamically. Use the (already existing) "ALIGN()", and create
>   the "ALIGN_FLOOR()" macro.
> - While we are there, let's clean up the ALIGN() macros wrt coding style. e.g.
>   missing parenthesis around the first use of the "x" parameter in ALIGN().
> 
> Changelog since v2:
> - Fix object_align*(): should use object size alignment, not pointer alignment.
> 
> Changelog since v1:
> - Align on the object natural alignment
>     (rather than min(arch word alignment, natural alignment))
> 
> The advantage of separating the API in "object alignment" and "offset alignment"
> is that it gives more freedom to play with offset alignment. Very useful to
> implement a tracer ring-buffer alignment. (hint hint)
> 
> Typical users will use "object alignment", but infrastructures like tracers
> which need to perform alignment of statically known base+offsets will typically
> use "offset alignment", because it allows to align with respect to a base rather
> than to pass an absolute address.
> 
> We use "sizeof(object)" rather than "__alignof__()" object because alignof
> returns "recommended" object alignment for the architecture, which can be
> sub-optimal on some architectures. By ensuring alignment on the object size, we
> are sure to make the right choice.
> 
> Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
> Signed-off-by: Alexander Shishkin <virtuoso@slind.org>
> CC: Russell King - ARM Linux <linux@arm.linux.org.uk>
> CC: linux-arm-kernel at lists.infradead.org
> CC: Imre Deak <imre.deak@nokia.com>
> CC: Jamie Lokier <jamie@shareable.org>
> CC: rostedt at goodmis.org
> CC: mingo at elte.hu
> CC: Alexey Dobriyan <adobriyan@gmail.com>
> ---
>  include/linux/Kbuild   |    1 
>  include/linux/align.h  |   56 +++++++++++++++++++++++++++++++++++++++++++++++++
>  include/linux/kernel.h |    8 -------
>  3 files changed, 58 insertions(+), 7 deletions(-)
>  create mode 100644 include/linux/align.h
> 
> Index: linux.trees.git/include/linux/align.h
> ===================================================================
> --- /dev/null	1970-01-01 00:00:00.000000000 +0000
> +++ linux.trees.git/include/linux/align.h	2010-08-17 16:31:50.000000000 -0400
> @@ -0,0 +1,56 @@
> +#ifndef _LINUX_ALIGN_H
> +#define _LINUX_ALIGN_H
> +
> +#define __ALIGN_KERNEL(x, a)	__ALIGN_KERNEL_MASK((x), (typeof(x))(a) - 1)
                                                    ^^^
Unnecessary braces. And many below.

> +#define __ALIGN_KERNEL_MASK(x, mask) \
> +				(((x) + (mask)) & ~(mask))
> +
> +#ifdef __KERNEL__
> +
> +#include <linux/types.h>
> +
> +#define ALIGN(x, a)		__ALIGN_KERNEL((x), (a))
> +#define __ALIGN_MASK(x, mask)	__ALIGN_KERNEL_MASK((x), (mask))
> +#define PTR_ALIGN(p, a)		((typeof(p)) ALIGN((unsigned long) (p), (a)))
> +#define ALIGN_FLOOR(x, a)	__ALIGN_FLOOR_MASK((x), (typeof(x)) (a) - 1)
> +#define __ALIGN_FLOOR_MASK(x, mask)	((x) & ~(mask))
> +#define PTR_ALIGN_FLOOR(p, a) \
> +			((typeof(p)) ALIGN_FLOOR((unsigned long) (p), (a)))
> +#define IS_ALIGNED(x, a)	(((x) & ((typeof(x)) (a) - 1)) == 0)
> +
> +/*
> + * Align pointer on natural object alignment. Object size must be power of two.
> + */
> +#define object_align(obj)	PTR_ALIGN((obj), __alignof__(*(obj)))
> +#define object_align_floor(obj)	PTR_ALIGN_FLOOR((obj), __alignof__(*(obj)))
> +
> +/**
> + * offset_align - Calculate the offset needed to align an object on its natural
> + *                alignment towards higher addresses.
> + * @align_drift:  object offset from an "alignment"-aligned address.
> + * @alignment:    natural object alignment. Must be non-zero, power of 2.
> + *
> + * Returns the offset that must be added to align towards higher
> + * addresses.
> + */
> +static inline size_t offset_align(size_t align_drift, size_t alignment)
> +{
> +	return (alignment - align_drift) & (alignment - 1);
> +}
> +
> +/**
> + * offset_align_floor - Calculate the offset needed to align an object
> + *                      on its natural alignment towards lower addresses.
> + * @align_drift:  object offset from an "alignment"-aligned address.
> + * @alignment:    natural object alignment. Must be non-zero, power of 2.
> + *
> + * Returns the offset that must be substracted to align towards lower addresses.
> + */
> +static inline size_t offset_align_floor(size_t align_drift, size_t alignment)
> +{
> +	return (align_drift - alignment) & (alignment - 1);
> +}
> +
> +#endif /* __KERNEL__ */
> +
> +#endif
> Index: linux.trees.git/include/linux/kernel.h
> ===================================================================
> --- linux.trees.git.orig/include/linux/kernel.h	2010-08-17 16:28:25.000000000 -0400
> +++ linux.trees.git/include/linux/kernel.h	2010-08-17 16:31:50.000000000 -0400
> @@ -4,8 +4,7 @@
>  /*
>   * 'kernel.h' contains some often-used function prototypes etc
>   */
> -#define __ALIGN_KERNEL(x, a)		__ALIGN_KERNEL_MASK(x, (typeof(x))(a) - 1)
> -#define __ALIGN_KERNEL_MASK(x, mask)	(((x) + (mask)) & ~(mask))
> +#include <linux/align.h>
>  
>  #ifdef __KERNEL__
>  
> @@ -39,11 +38,6 @@ extern const char linux_proc_banner[];
>  
>  #define STACK_MAGIC	0xdeadbeef
>  
> -#define ALIGN(x, a)		__ALIGN_KERNEL((x), (a))
> -#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 ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
>  
>  /*
> Index: linux.trees.git/include/linux/Kbuild
> ===================================================================
> --- linux.trees.git.orig/include/linux/Kbuild	2010-08-17 16:28:25.000000000 -0400
> +++ linux.trees.git/include/linux/Kbuild	2010-08-17 16:32:30.000000000 -0400
> @@ -38,6 +38,7 @@ header-y += adfs_fs.h
>  header-y += affs_hardblocks.h
>  header-y += agpgart.h
>  header-y += aio_abi.h
> +header-y += align.h
>  header-y += apm_bios.h
>  header-y += arcfb.h
>  header-y += atalk.h
> 
> 
> _______________________________________________
> linux-arm-kernel mailing list
> linux-arm-kernel at lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

-- 
 Kirill A. Shutemov

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [RFC PATCH 01/20] Create generic alignment API (v8)
  2010-08-18  0:00   ` Kirill A. Shutemov
@ 2010-08-18  1:05     ` Mathieu Desnoyers
  0 siblings, 0 replies; 5+ messages in thread
From: Mathieu Desnoyers @ 2010-08-18  1:05 UTC (permalink / raw)
  To: linux-arm-kernel

* Kirill A. Shutemov (kirill at shutemov.name) wrote:
> > Index: linux.trees.git/include/linux/align.h
> > ===================================================================
> > --- /dev/null	1970-01-01 00:00:00.000000000 +0000
> > +++ linux.trees.git/include/linux/align.h	2010-08-17 16:31:50.000000000 -0400
> > @@ -0,0 +1,56 @@
> > +#ifndef _LINUX_ALIGN_H
> > +#define _LINUX_ALIGN_H
> > +
> > +#define __ALIGN_KERNEL(x, a)	__ALIGN_KERNEL_MASK((x), (typeof(x))(a) - 1)
>                                                     ^^^
> Unnecessary braces. And many below.

Good catch, thanks !

I also found some in:

Ring buffer backend
Ring buffer frontend

patches. I fixed them already, they should be OK in the next round.

Thanks,

Mathieu

-- 
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [RFC PATCH 01/20] Create generic alignment API (v8)
  2010-08-17 23:16 ` [RFC PATCH 01/20] Create generic alignment API (v8) Mathieu Desnoyers
  2010-08-18  0:00   ` Kirill A. Shutemov
@ 2010-08-18  1:25   ` Steven Rostedt
  2010-08-18  2:09     ` Mathieu Desnoyers
  1 sibling, 1 reply; 5+ messages in thread
From: Steven Rostedt @ 2010-08-18  1:25 UTC (permalink / raw)
  To: linux-arm-kernel

On Tue, 2010-08-17 at 19:16 -0400, Mathieu Desnoyers wrote:

> +/*
> + * Align pointer on natural object alignment. Object size must be power of two.
> + */

Hmm, I wonder if we should add a compiler bug here too.

extern void __bug_obj_not_power_of_two(void);

({
	if ((sizeof(*obj) - 1) & sizeof(*obj))
		__bug_obj_not_power_of_two();
	PTR_ALIGN((obj), __alignof__(*(obj)));
})

> +#define object_align(obj)	PTR_ALIGN((obj), __alignof__(*(obj)))
> +#define object_align_floor(obj)	PTR_ALIGN_FLOOR((obj), __alignof__(*(obj)))
> +
> +/**
> + * offset_align - Calculate the offset needed to align an object on its natural
> + *                alignment towards higher addresses.
> + * @align_drift:  object offset from an "alignment"-aligned address.
> + * @alignment:    natural object alignment. Must be non-zero, power of 2.
> + *
> + * Returns the offset that must be added to align towards higher
> + * addresses.
> + */
> +static inline size_t offset_align(size_t align_drift, size_t alignment)
> +{
> +	return (alignment - align_drift) & (alignment - 1);
> +}
> +
> +/**
> + * offset_align_floor - Calculate the offset needed to align an object
> + *                      on its natural alignment towards lower addresses.
> + * @align_drift:  object offset from an "alignment"-aligned address.
> + * @alignment:    natural object alignment. Must be non-zero, power of 2.
> + *
> + * Returns the offset that must be substracted to align towards lower addresses.
> + */
> +static inline size_t offset_align_floor(size_t align_drift, size_t alignment)
> +{
> +	return (align_drift - alignment) & (alignment - 1);
> +}

I take it that theses functions can have variables passed to it for
alignment, thus a check wont help. Although, we could add a test for the
constant case.

-- Steve

> +

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [RFC PATCH 01/20] Create generic alignment API (v8)
  2010-08-18  1:25   ` Steven Rostedt
@ 2010-08-18  2:09     ` Mathieu Desnoyers
  0 siblings, 0 replies; 5+ messages in thread
From: Mathieu Desnoyers @ 2010-08-18  2:09 UTC (permalink / raw)
  To: linux-arm-kernel

* Steven Rostedt (rostedt at goodmis.org) wrote:
> On Tue, 2010-08-17 at 19:16 -0400, Mathieu Desnoyers wrote:
> 
> > +/*
> > + * Align pointer on natural object alignment. Object size must be power of two.
> > + */
> 
> Hmm, I wonder if we should add a compiler bug here too.
> 
> extern void __bug_obj_not_power_of_two(void);
> 
> ({
> 	if ((sizeof(*obj) - 1) & sizeof(*obj))
> 		__bug_obj_not_power_of_two();
> 	PTR_ALIGN((obj), __alignof__(*(obj)));
> })

[...]

Nevermind the "Object size must be power of two." part of the comment. That was
true when I first implemented the patch, where I used sizeof(), e.g.:

#define object_align(obj)       PTR_ALIGN(obj, sizeof(*(obj)))
#define object_align_floor(obj) PTR_ALIGN_FLOOR(obj, sizeof(*(obj)))

But given that we can expect structures and various objects to be passed as
parameters, __alignof__ is more appropriate. This is what this patch now
implements:

#define object_align(obj)       PTR_ALIGN(obj, __alignof__(*(obj)))
#define object_align_floor(obj) PTR_ALIGN_FLOOR(obj, __alignof__(*(obj)))

so we let gcc determine the appropriate alignment. It will automatically be a
power of two. I'll just update the comment.

[...]

> I take it that theses functions can have variables passed to it for
> alignment, thus a check wont help. Although, we could add a test for the
> constant case.

That's right. I could add something like:

  MAYBE_BUILD_BUG_ON_NOT_POWER_OF_2(alignment);

which will add a dependency on kernel.h (for MAYBE_BUILD_BUG_ON) and log2.h (for
is_power_of_2). I'll move the #include <linux/align.h> further down in kernel.h,
keeping it outside of #ifdef __KERNEL__.

To make sure this build test works when a constant is passed to offset_align or
offset_align_floor, I'll have to turn them into macros instead of static
inlines.

The next round will have a patch that adds "MAYBE_BUILD_BUG_ON_NOT_POWER_OF_2"
to kernel.h.

Thanks,

Mathieu

-- 
Mathieu Desnoyers
Operating System Efficiency R&D Consultant
EfficiOS Inc.
http://www.efficios.com

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2010-08-18  2:09 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <20100817231619.277457797@efficios.com>
2010-08-17 23:16 ` [RFC PATCH 01/20] Create generic alignment API (v8) Mathieu Desnoyers
2010-08-18  0:00   ` Kirill A. Shutemov
2010-08-18  1:05     ` Mathieu Desnoyers
2010-08-18  1:25   ` Steven Rostedt
2010-08-18  2:09     ` Mathieu Desnoyers

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