diff mbox

x86emul: correct DECLARE_ALIGNED()

Message ID 58CA9BAB0200007800143CE4@prv-mh.provo.novell.com (mailing list archive)
State New, archived
Headers show

Commit Message

Jan Beulich March 16, 2017, 1:05 p.m. UTC
Stop creating an excessively large array on the stack, by properly
taking into account the array element size when establishing its
element count (and of course also when calculating the pointer to
be actually used to access the memory).

Signed-off-by: Jan Beulich <jbeulich@suse.com>
x86emul: correct DECLARE_ALIGNED()

Stop creating an excessively large array on the stack, by properly
taking into account the array element size when establishing its
element count (and of course also when calculating the pointer to
be actually used to access the memory).

Signed-off-by: Jan Beulich <jbeulich@suse.com>

--- a/xen/arch/x86/x86_emulate/x86_emulate.c
+++ b/xen/arch/x86/x86_emulate/x86_emulate.c
@@ -553,10 +553,10 @@ typedef union {
  * the compiler for automatic variables. Use this helper to instantiate a
  * suitably aligned variable, producing a pointer to access it.
  */
-#define DECLARE_ALIGNED(type, var)                                   \
-    long __##var[sizeof(type) + __alignof(type) - __alignof(long)];  \
-    type *const var##p =                                             \
-        (void *)((long)(__##var + __alignof(type) - __alignof(long)) \
+#define DECLARE_ALIGNED(type, var)                                        \
+    long __##var[(sizeof(type) + __alignof(type)) / __alignof(long) - 1]; \
+    type *const var##p =                                                  \
+        (void *)(((long)__##var + __alignof(type) - __alignof(__##var))   \
                  & -__alignof(type))
 
 #ifdef __GCC_ASM_FLAG_OUTPUTS__

Comments

Andrew Cooper March 16, 2017, 1:09 p.m. UTC | #1
On 16/03/17 13:05, Jan Beulich wrote:
> Stop creating an excessively large array on the stack, by properly
> taking into account the array element size when establishing its
> element count (and of course also when calculating the pointer to
> be actually used to access the memory).

What in practice does this do?  It looks like it reduces the size of the
array by 4 or 8 times?

~Andrew

>
> Signed-off-by: Jan Beulich <jbeulich@suse.com>
>
> --- a/xen/arch/x86/x86_emulate/x86_emulate.c
> +++ b/xen/arch/x86/x86_emulate/x86_emulate.c
> @@ -553,10 +553,10 @@ typedef union {
>   * the compiler for automatic variables. Use this helper to instantiate a
>   * suitably aligned variable, producing a pointer to access it.
>   */
> -#define DECLARE_ALIGNED(type, var)                                   \
> -    long __##var[sizeof(type) + __alignof(type) - __alignof(long)];  \
> -    type *const var##p =                                             \
> -        (void *)((long)(__##var + __alignof(type) - __alignof(long)) \
> +#define DECLARE_ALIGNED(type, var)                                        \
> +    long __##var[(sizeof(type) + __alignof(type)) / __alignof(long) - 1]; \
> +    type *const var##p =                                                  \
> +        (void *)(((long)__##var + __alignof(type) - __alignof(__##var))   \
>                   & -__alignof(type))
>  
>  #ifdef __GCC_ASM_FLAG_OUTPUTS__
>
>
>
Jan Beulich March 16, 2017, 1:31 p.m. UTC | #2
>>> On 16.03.17 at 14:09, <andrew.cooper3@citrix.com> wrote:
> On 16/03/17 13:05, Jan Beulich wrote:
>> Stop creating an excessively large array on the stack, by properly
>> taking into account the array element size when establishing its
>> element count (and of course also when calculating the pointer to
>> be actually used to access the memory).
> 
> What in practice does this do?  It looks like it reduces the size of the
> array by 4 or 8 times?

Correct - it was this much oversized, bloating the stack frame of the
function quite a bit.

Jan
Andrew Cooper March 16, 2017, 1:47 p.m. UTC | #3
On 16/03/17 13:31, Jan Beulich wrote:
>>>> On 16.03.17 at 14:09, <andrew.cooper3@citrix.com> wrote:
>> On 16/03/17 13:05, Jan Beulich wrote:
>>> Stop creating an excessively large array on the stack, by properly
>>> taking into account the array element size when establishing its
>>> element count (and of course also when calculating the pointer to
>>> be actually used to access the memory).
>> What in practice does this do?  It looks like it reduces the size of the
>> array by 4 or 8 times?
> Correct - it was this much oversized, bloating the stack frame of the
> function quite a bit.

Ok, although it would be helpful if this was called out more clearly in
the commit message.

Reviewed-by: Andrew Cooper <andrew.cooper3@citrix.com>
diff mbox

Patch

--- a/xen/arch/x86/x86_emulate/x86_emulate.c
+++ b/xen/arch/x86/x86_emulate/x86_emulate.c
@@ -553,10 +553,10 @@  typedef union {
  * the compiler for automatic variables. Use this helper to instantiate a
  * suitably aligned variable, producing a pointer to access it.
  */
-#define DECLARE_ALIGNED(type, var)                                   \
-    long __##var[sizeof(type) + __alignof(type) - __alignof(long)];  \
-    type *const var##p =                                             \
-        (void *)((long)(__##var + __alignof(type) - __alignof(long)) \
+#define DECLARE_ALIGNED(type, var)                                        \
+    long __##var[(sizeof(type) + __alignof(type)) / __alignof(long) - 1]; \
+    type *const var##p =                                                  \
+        (void *)(((long)__##var + __alignof(type) - __alignof(__##var))   \
                  & -__alignof(type))
 
 #ifdef __GCC_ASM_FLAG_OUTPUTS__