Skip to contentSkip to conversation

move offsetofend() from vfio.h to stddef.h

PATCH In review Denys Vlasenko 9 participants 16 messages Mar 9, 2015 – Mar 17, 2015

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 move offsetofend() from vfio.h to stddef.h

Suggested by Linus.

CC Linus Torvalds <[email protected]>
CC Steven Rostedt <[email protected]>
CC Ingo Molnar <[email protected]>
CC Borislav Petkov <[email protected]>
CC "H. Peter Anvin" <[email protected]>
CC Andy Lutomirski <[email protected]>
CC Oleg Nesterov <[email protected]>
CC Frederic Weisbecker <[email protected]>
CC Alexei Starovoitov <[email protected]>
CC Will Drewry <[email protected]>
CC Kees Cook <[email protected]>
---
 include/linux/stddef.h |  9 +++++++++
 include/linux/vfio.h   | 13 -------------
 2 files changed, 9 insertions(+), 13 deletions(-)
include/linux/stddef.h +9 -0
index f4aec0e..076af43 100644
@@ -19,3 +19,12 @@ enum {
 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
 #endif
 #endif
+
+/**
+ * offsetofend(TYPE, MEMBER)
+ *
+ * @TYPE: The type of the structure
+ * @MEMBER: The member within the structure to get the end offset of
+ */
+#define offsetofend(TYPE, MEMBER) \
+	(offsetof(TYPE, MEMBER)	+ sizeof(((TYPE *)0)->MEMBER))
include/linux/vfio.h +0 -13
index 2d67b89..049b2f4 100644
@@ -78,19 +78,6 @@ extern int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops);
 extern void vfio_unregister_iommu_driver(
 				const struct vfio_iommu_driver_ops *ops);
 
-/**
- * offsetofend(TYPE, MEMBER)
- *
- * @TYPE: The type of the structure
- * @MEMBER: The member within the structure to get the end offset of
- *
- * Simple helper macro for dealing with variable sized structures passed
- * from user space.  This allows us to easily determine if the provided
- * structure is sized to include various fields.
- */
-#define offsetofend(TYPE, MEMBER) \
-	(offsetof(TYPE, MEMBER)	+ sizeof(((TYPE *)0)->MEMBER))
-
 /*
  * External user API
  */
Signature · 1 line
1.8.1.4
#1 lore raw

PATCH v2 2/2 x86: make 32-bit "emergency stack" better documented

Before the patch, tss.stack field was not referenced anywhere. It was used only by setting sysenter's stack to point after last byte of tss, thus the trailing field, stack[64], was used.

But grep would not know it. You can comment it out, compile, and kernel will even run until an unlucky NMI corrupts io_bitmap[] (which is also not easily detectable).

This patch changes code so that the purpose and usage of this field is not mysterious anymore, and can be easily grepped for.

This does change generated code, for a subtle reason:
since tss_struct is ____cacheline_aligned, there happen to be 5 longs of padding at the end. Old code was using the padding too; new code will strictly use only SYSENTER_stack[].

CC Linus Torvalds <[email protected]>
CC Steven Rostedt <[email protected]>
CC Ingo Molnar <[email protected]>
CC Borislav Petkov <[email protected]>
CC "H. Peter Anvin" <[email protected]>
CC Andy Lutomirski <[email protected]>
CC Oleg Nesterov <[email protected]>
CC Frederic Weisbecker <[email protected]>
CC Alexei Starovoitov <[email protected]>
CC Will Drewry <[email protected]>
CC Kees Cook <[email protected]>

---
Changes since v1: use offsetofend()

 arch/x86/include/asm/processor.h | 4 ++--
 arch/x86/kernel/asm-offsets_32.c | 2 +-
 arch/x86/kernel/cpu/common.c     | 3 ++-
 3 files changed, 5 insertions(+), 4 deletions(-)
arch/x86/include/asm/processor.h +2 -2
index 48a61c1..9e65cf8 100644
@@ -276,9 +276,9 @@ struct tss_struct {
 	unsigned long		io_bitmap[IO_BITMAP_LONGS + 1];
 
 	/*
-	 * .. and then another 0x100 bytes for the emergency kernel stack:
+	 * and then space for temporary SYSENTER stack:
 	 */
-	unsigned long		stack[64];
+	unsigned long		SYSENTER_stack[64];
 
 } ____cacheline_aligned;
 
arch/x86/kernel/asm-offsets_32.c +1 -1
index 3b3b9d3..42a3b28 100644
@@ -68,7 +68,7 @@ void foo(void)
 
 	/* Offset from the sysenter stack to tss.sp0 */
 	DEFINE(TSS_sysenter_sp0, offsetof(struct tss_struct, x86_tss.sp0) -
-		 sizeof(struct tss_struct));
+		offsetofend(struct tss_struct, SYSENTER_stack));
 
 #if defined(CONFIG_LGUEST) || defined(CONFIG_LGUEST_GUEST) || defined(CONFIG_LGUEST_MODULE)
 	BLANK();
arch/x86/kernel/cpu/common.c +2 -1
index 7634833..4701293 100644
@@ -987,7 +987,8 @@ void enable_sep_cpu(void)
 	}
 
 	tss->x86_tss.ss1 = __KERNEL_CS;
-	tss->x86_tss.sp1 = sizeof(struct tss_struct) + (unsigned long) tss;
+	tss->x86_tss.sp1 = (unsigned long) tss
+			+ offsetofend(struct tss_struct, SYSENTER_stack);
 	wrmsr(MSR_IA32_SYSENTER_CS, __KERNEL_CS, 0);
 	wrmsr(MSR_IA32_SYSENTER_ESP, tss->x86_tss.sp1, 0);
 	wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long) ia32_sysenter_target, 0);
Signature · 1 line
1.8.1.4
Acked-by on v2 2/2
#2 lore raw

Re: [PATCH 2/2 v2] x86: make 32-bit "emergency stack" better documented

Quoted 8 lines · Denys Vlasenko

Before the patch, tss.stack field was not referenced anywhere. It was used only by setting sysenter's stack to point after last byte of tss, thus the trailing field, stack[64], was used.

But grep would not know it. You can comment it out, compile, and kernel will even run until an unlucky NMI corrupts io_bitmap[] (which is also not easily detectable).

Quoted 7 lines

This patch changes code so that the purpose and usage of this field is not mysterious anymore, and can be easily grepped for.

This does change generated code, for a subtle reason:
since tss_struct is ____cacheline_aligned, there happen to be 5 longs of padding at the end. Old code was using the padding too; new code will strictly use only SYSENTER_stack[].

Quoted 69 lines
CC Linus Torvalds <[email protected]>
CC Steven Rostedt <[email protected]>
CC Ingo Molnar <[email protected]>
CC Borislav Petkov <[email protected]>
CC "H. Peter Anvin" <[email protected]>
CC Andy Lutomirski <[email protected]>
CC Oleg Nesterov <[email protected]>
CC Frederic Weisbecker <[email protected]>
CC Alexei Starovoitov <[email protected]>
CC Will Drewry <[email protected]>
CC Kees Cook <[email protected]>

---
Changes since v1: use offsetofend()

 arch/x86/include/asm/processor.h | 4 ++--
 arch/x86/kernel/asm-offsets_32.c | 2 +-
 arch/x86/kernel/cpu/common.c     | 3 ++-
 3 files changed, 5 insertions(+), 4 deletions(-)
arch/x86/include/asm/processor.h +2 -2
index 48a61c1..9e65cf8 100644
@@ -276,9 +276,9 @@ struct tss_struct {
        unsigned long           io_bitmap[IO_BITMAP_LONGS + 1];
 
        /*
-        * .. and then another 0x100 bytes for the emergency kernel stack:
+        * and then space for temporary SYSENTER stack:
         */
-       unsigned long           stack[64];
+       unsigned long           SYSENTER_stack[64];
 
 } ____cacheline_aligned;
 
arch/x86/kernel/asm-offsets_32.c +1 -1
index 3b3b9d3..42a3b28 100644
@@ -68,7 +68,7 @@ void foo(void)
 
        /* Offset from the sysenter stack to tss.sp0 */
        DEFINE(TSS_sysenter_sp0, offsetof(struct tss_struct, x86_tss.sp0) -
-                sizeof(struct tss_struct));
+               offsetofend(struct tss_struct, SYSENTER_stack));
 
 #if defined(CONFIG_LGUEST) || defined(CONFIG_LGUEST_GUEST) || defined(CONFIG_LGUEST_MODULE)
        BLANK();
arch/x86/kernel/cpu/common.c +2 -2
index 7634833..4701293 100644
@@ -987,7 +987,8 @@ void enable_sep_cpu(void)
        }
 
        tss->x86_tss.ss1 = __KERNEL_CS;
-       tss->x86_tss.sp1 = sizeof(struct tss_struct) + (unsigned long) tss;
+       tss->x86_tss.sp1 = (unsigned long) tss
+                       + offsetofend(struct tss_struct, SYSENTER_stack);
        wrmsr(MSR_IA32_SYSENTER_CS, __KERNEL_CS, 0);
        wrmsr(MSR_IA32_SYSENTER_ESP, tss->x86_tss.sp1, 0);
        wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long) ia32_sysenter_target, 0);
--

1.8.1.4

Signature · 2 lines
Andy Lutomirski
AMA Capital Management, LLC
#3 lore raw

Re: [PATCH 2/2 v2] x86: make 32-bit "emergency stack" better documented

In reply to Denys Vlasenko

Hi!

Quoted 9 lines
 arch/x86/kernel/asm-offsets_32.c | 2 +-
 arch/x86/kernel/cpu/common.c     | 3 ++-
 3 files changed, 5 insertions(+), 4 deletions(-)
arch/x86/include/asm/processor.h +0 -0
index 48a61c1..9e65cf8 100644

arch/x86/include/asm/processor.h struct tss_struct { · line 281

Quoted 8 lines
arch/x86/include/asm/processor.h +2 -2
@@ -276,9 +276,9 @@ struct tss_struct {
 	unsigned long		io_bitmap[IO_BITMAP_LONGS + 1];
 
 	/*
-	 * .. and then another 0x100 bytes for the emergency kernel stack:
+	 * and then space for temporary SYSENTER stack:
 	 */
-	unsigned long		stack[64];
+	unsigned long		SYSENTER_stack[64];

Would it make sense to make it unsigned char ...stack[256], like the comment said?

Or is it supposed to be different size on 64bit, and comment was wrong?

Pavel

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html
#4 lore raw

Re: [PATCH 2/2 v2] x86: make 32-bit "emergency stack" better documented

Quoted 22 lines · Pavel Machek

Hi!

Quoted 9 lines
 arch/x86/kernel/asm-offsets_32.c | 2 +-
 arch/x86/kernel/cpu/common.c     | 3 ++-
 3 files changed, 5 insertions(+), 4 deletions(-)
arch/x86/include/asm/processor.h +0 -0
index 48a61c1..9e65cf8 100644

arch/x86/include/asm/processor.h struct tss_struct { · line 281

Quoted 8 lines
arch/x86/include/asm/processor.h +2 -2
@@ -276,9 +276,9 @@ struct tss_struct {
      unsigned long           io_bitmap[IO_BITMAP_LONGS + 1];
 
      /*
-      * .. and then another 0x100 bytes for the emergency kernel stack:
+      * and then space for temporary SYSENTER stack:
       */
-     unsigned long           stack[64];
+     unsigned long           SYSENTER_stack[64];

Would it make sense to make it unsigned char ...stack[256], like the comment said?

Quoted 1 line

Or is it supposed to be different size on 64bit, and comment was wrong?

It isn't used on 64-bit. MSR_IA32_SYSENTER_ESP is set to 0, and tss->sp0 is read from the percpu segment instead of relative to the stack pointer.

--
Brian Gerst

#5 lore raw

[tip:x86/asm] x86/asm/entry/32: Document the 32-bit SYSENTER " emergency stack" better

In reply to Denys Vlasenko
x86/asm/entry/32: Document the 32-bit SYSENTER "emergency stack" better
Commit-ID:  9b41236f1032623ce0470481e97604fb5c5681fa
Gitweb:     http://git.kernel.org/tip/9b41236f1032623ce0470481e97604fb5c5681fa
Author:     Denys Vlasenko <[email protected]>
AuthorDate: Mon, 9 Mar 2015 15:52:18 +0100
Committer:  Ingo Molnar <[email protected]>
CommitDate: Mon, 16 Mar 2015 11:05:37 +0100

x86/asm/entry/32: Document the 32-bit SYSENTER "emergency stack" better

Before the patch, the 'tss_struct::stack' field was not referenced anywhere.

It was used only to set SYSENTER's stack to point after the last byte of tss_struct, thus the trailing field, stack[64], was used.

But grep would not know it. You can comment it out, compile, and kernel will even run until an unlucky NMI corrupts io_bitmap[] (which is also not easily detectable).

This patch changes code so that the purpose and usage of this field is not mysterious anymore, and can be easily grepped for.

This does change generated code, for a subtle reason:
since tss_struct is ____cacheline_aligned, there happens to be 5 longs of padding at the end. Old code was using the padding too; new code will strictly use it only for SYSENTER_stack[].

Cc Alexei Starovoitov <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc H. Peter Anvin <[email protected]>
Cc Kees Cook <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Oleg Nesterov <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Will Drewry <[email protected]>
---
 arch/x86/include/asm/processor.h | 4 ++--
 arch/x86/kernel/asm-offsets_32.c | 2 +-
 arch/x86/kernel/cpu/common.c     | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)
arch/x86/include/asm/processor.h +2 -2
index d157f94..5720997 100644
@@ -291,9 +291,9 @@ struct tss_struct {
 	unsigned long		io_bitmap[IO_BITMAP_LONGS + 1];
 
 	/*
-	 * .. and then another 0x100 bytes for the emergency kernel stack:
+	 * Space for the temporary SYSENTER stack:
 	 */
-	unsigned long		stack[64];
+	unsigned long		SYSENTER_stack[64];
 
 } ____cacheline_aligned;
 
arch/x86/kernel/asm-offsets_32.c +1 -1
index 3b3b9d3..47703ae 100644
@@ -68,7 +68,7 @@ void foo(void)
 
 	/* Offset from the sysenter stack to tss.sp0 */
 	DEFINE(TSS_sysenter_sp0, offsetof(struct tss_struct, x86_tss.sp0) -
-		 sizeof(struct tss_struct));
+	       offsetofend(struct tss_struct, SYSENTER_stack));
 
 #if defined(CONFIG_LGUEST) || defined(CONFIG_LGUEST_GUEST) || defined(CONFIG_LGUEST_MODULE)
 	BLANK();
arch/x86/kernel/cpu/common.c +1 -1
index 7634833..7a3dfb1 100644
@@ -987,7 +987,7 @@ void enable_sep_cpu(void)
 	}
 
 	tss->x86_tss.ss1 = __KERNEL_CS;
-	tss->x86_tss.sp1 = sizeof(struct tss_struct) + (unsigned long) tss;
+	tss->x86_tss.sp1 = (unsigned long)tss + offsetofend(struct tss_struct, SYSENTER_stack);
 	wrmsr(MSR_IA32_SYSENTER_CS, __KERNEL_CS, 0);
 	wrmsr(MSR_IA32_SYSENTER_ESP, tss->x86_tss.sp1, 0);
 	wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long) ia32_sysenter_target, 0);
#6 lore raw

[tip:x86/asm] x86/asm/entry/32: Document the 32-bit SYSENTER " emergency stack" better

In reply to Denys Vlasenko
x86/asm/entry/32: Document the 32-bit SYSENTER "emergency stack" better
Commit-ID:  d828c71fba8922b116b4ec56c3e5bca8c822d5ae
Gitweb:     http://git.kernel.org/tip/d828c71fba8922b116b4ec56c3e5bca8c822d5ae
Author:     Denys Vlasenko <[email protected]>
AuthorDate: Mon, 9 Mar 2015 15:52:18 +0100
Committer:  Ingo Molnar <[email protected]>
CommitDate: Tue, 17 Mar 2015 09:25:29 +0100

x86/asm/entry/32: Document the 32-bit SYSENTER "emergency stack" better

Before the patch, the 'tss_struct::stack' field was not referenced anywhere.

It was used only to set SYSENTER's stack to point after the last byte of tss_struct, thus the trailing field, stack[64], was used.

But grep would not know it. You can comment it out, compile, and kernel will even run until an unlucky NMI corrupts io_bitmap[] (which is also not easily detectable).

This patch changes code so that the purpose and usage of this field is not mysterious anymore, and can be easily grepped for.

This does change generated code, for a subtle reason:
since tss_struct is ____cacheline_aligned, there happens to be 5 longs of padding at the end. Old code was using the padding too; new code will strictly use it only for SYSENTER_stack[].

Cc Alexei Starovoitov <[email protected]>
Cc Andy Lutomirski <[email protected]>
Cc Borislav Petkov <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc H. Peter Anvin <[email protected]>
Cc Kees Cook <[email protected]>
Cc Linus Torvalds <[email protected]>
Cc Oleg Nesterov <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Will Drewry <[email protected]>
---
 arch/x86/include/asm/processor.h | 4 ++--
 arch/x86/kernel/asm-offsets_32.c | 2 +-
 arch/x86/kernel/cpu/common.c     | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)
arch/x86/include/asm/processor.h +2 -2
index 6a5c0ec..5abd9a5 100644
@@ -291,9 +291,9 @@ struct tss_struct {
 	unsigned long		io_bitmap[IO_BITMAP_LONGS + 1];
 
 	/*
-	 * .. and then another 0x100 bytes for the emergency kernel stack:
+	 * Space for the temporary SYSENTER stack:
 	 */
-	unsigned long		stack[64];
+	unsigned long		SYSENTER_stack[64];
 
 } ____cacheline_aligned;
 
arch/x86/kernel/asm-offsets_32.c +1 -1
index 3b3b9d3..47703ae 100644
@@ -68,7 +68,7 @@ void foo(void)
 
 	/* Offset from the sysenter stack to tss.sp0 */
 	DEFINE(TSS_sysenter_sp0, offsetof(struct tss_struct, x86_tss.sp0) -
-		 sizeof(struct tss_struct));
+	       offsetofend(struct tss_struct, SYSENTER_stack));
 
 #if defined(CONFIG_LGUEST) || defined(CONFIG_LGUEST_GUEST) || defined(CONFIG_LGUEST_MODULE)
 	BLANK();
arch/x86/kernel/cpu/common.c +1 -1
index 7634833..7a3dfb1 100644
@@ -987,7 +987,7 @@ void enable_sep_cpu(void)
 	}
 
 	tss->x86_tss.ss1 = __KERNEL_CS;
-	tss->x86_tss.sp1 = sizeof(struct tss_struct) + (unsigned long) tss;
+	tss->x86_tss.sp1 = (unsigned long)tss + offsetofend(struct tss_struct, SYSENTER_stack);
 	wrmsr(MSR_IA32_SYSENTER_CS, __KERNEL_CS, 0);
 	wrmsr(MSR_IA32_SYSENTER_ESP, tss->x86_tss.sp1, 0);
 	wrmsr(MSR_IA32_SYSENTER_EIP, (unsigned long) ia32_sysenter_target, 0);
#7 lore raw
In reply to Denys Vlasenko
Quoted 48 lines · Denys Vlasenko

Suggested by Linus.

CC Linus Torvalds <[email protected]>
CC Steven Rostedt <[email protected]>
CC Ingo Molnar <[email protected]>
CC Borislav Petkov <[email protected]>
CC "H. Peter Anvin" <[email protected]>
CC Andy Lutomirski <[email protected]>
CC Oleg Nesterov <[email protected]>
CC Frederic Weisbecker <[email protected]>
CC Alexei Starovoitov <[email protected]>
CC Will Drewry <[email protected]>
CC Kees Cook <[email protected]>
---
 include/linux/stddef.h |  9 +++++++++
 include/linux/vfio.h   | 13 -------------
 2 files changed, 9 insertions(+), 13 deletions(-)
include/linux/stddef.h +9 -0
index f4aec0e..076af43 100644
@@ -19,3 +19,12 @@ enum {
 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
 #endif
 #endif
+
+/**
+ * offsetofend(TYPE, MEMBER)
+ *
+ * @TYPE: The type of the structure
+ * @MEMBER: The member within the structure to get the end offset of
+ */
+#define offsetofend(TYPE, MEMBER) \
+	(offsetof(TYPE, MEMBER)	+ sizeof(((TYPE *)0)->MEMBER))
include/linux/vfio.h +0 -2
index 2d67b89..049b2f4 100644
@@ -78,19 +78,6 @@ extern int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops);
 extern void vfio_unregister_iommu_driver(
 				const struct vfio_iommu_driver_ops *ops);
 
-/**
- * offsetofend(TYPE, MEMBER)

include/linux/vfio.h extern int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops); · line 92

Quoted 10 lines
include/linux/vfio.h +0 -10
- *
- * @TYPE: The type of the structure
- * @MEMBER: The member within the structure to get the end offset of
- *
- * Simple helper macro for dealing with variable sized structures passed
- * from user space.  This allows us to easily determine if the provided
- * structure is sized to include various fields.
- */
-#define offsetofend(TYPE, MEMBER) \
-	(offsetof(TYPE, MEMBER)	+ sizeof(((TYPE *)0)->MEMBER))

So I like it, and because it is not particularly trivial when to use this primitive it was explained nicely in a description in the vfio.h version.

But you lost that nice description during the code move!!

Please don't.

Thanks,

Ingo

#8 lore raw
Quoted 17 lines · Ingo Molnar

include/linux/vfio.h extern int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops); · line 92

Quoted 12 lines
include/linux/vfio.h +0 -12
-/**
- * offsetofend(TYPE, MEMBER)
- *
- * @TYPE: The type of the structure
- * @MEMBER: The member within the structure to get the end offset of
- *
- * Simple helper macro for dealing with variable sized structures passed
- * from user space.  This allows us to easily determine if the provided
- * structure is sized to include various fields.
- */
-#define offsetofend(TYPE, MEMBER) \
-     (offsetof(TYPE, MEMBER) + sizeof(((TYPE *)0)->MEMBER))

So I like it, and because it is not particularly trivial when to use this primitive it was explained nicely in a description in the vfio.h version.

Quoted 1 line

But you lost that nice description during the code move!!

That description was clearly specific to how that macro is used in drivers/vfio/*.c, along the lines of

                minsz = offsetofend(struct vfio_eeh_pe_op, op);
                if (copy_from_user(&op, (void __user *)arg, minsz))
                        return -EFAULT;
                if (op.argsz < minsz || op.flags)
                        return -EINVAL;

But the macro is generic, it has many other uses besides this one.

Nevertheless, I can resend a version where comment survives if you want...

#9 lore raw
Quoted 18 lines · Denys Vlasenko
Quoted 17 lines · Ingo Molnar

include/linux/vfio.h extern int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops); · line 92

Quoted 12 lines
include/linux/vfio.h +0 -12
-/**
- * offsetofend(TYPE, MEMBER)
- *
- * @TYPE: The type of the structure
- * @MEMBER: The member within the structure to get the end offset of
- *
- * Simple helper macro for dealing with variable sized structures passed
- * from user space.  This allows us to easily determine if the provided
- * structure is sized to include various fields.
- */
-#define offsetofend(TYPE, MEMBER) \
-     (offsetof(TYPE, MEMBER) + sizeof(((TYPE *)0)->MEMBER))

So I like it, and because it is not particularly trivial when to use this primitive it was explained nicely in a description in the vfio.h version.

Quoted 6 lines
Quoted 1 line

But you lost that nice description during the code move!!

That description was clearly specific to how that macro is used in drivers/vfio/*.c, along the lines of

                minsz = offsetofend(struct vfio_eeh_pe_op, op);

Hm, but here 'minsz' == sizeof(struct vfio_eeh_pe_op), so the vfio usage does not seem to be justified.

Quoted 6 lines
                if (copy_from_user(&op, (void __user *)arg, minsz))
                        return -EFAULT;
                if (op.argsz < minsz || op.flags)
                        return -EINVAL;

But the macro is generic, it has many other uses besides this one.

So I might be missing something, but what generic uses does it have, beyond structures that have some rare size related weirdness, such as alignment attributes? In 99% of the cases:

sizeof(struct) == offsetofend(struct, last_member)

right?

Quoted 2 lines

Nevertheless, I can resend a version where comment survives if you want...

So maybe extend it to a description that you think describes its uses correctly? People will keep wondering about when to use this.

Thanks,

Ingo

#10 lore raw
Quoted 38 lines · Ingo Molnar
Quoted 18 lines · Denys Vlasenko
Quoted 17 lines · Ingo Molnar

include/linux/vfio.h extern int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops); · line 92

Quoted 12 lines
include/linux/vfio.h +0 -12
-/**
- * offsetofend(TYPE, MEMBER)
- *
- * @TYPE: The type of the structure
- * @MEMBER: The member within the structure to get the end offset of
- *
- * Simple helper macro for dealing with variable sized structures passed
- * from user space.  This allows us to easily determine if the provided
- * structure is sized to include various fields.
- */
-#define offsetofend(TYPE, MEMBER) \
-     (offsetof(TYPE, MEMBER) + sizeof(((TYPE *)0)->MEMBER))

So I like it, and because it is not particularly trivial when to use this primitive it was explained nicely in a description in the vfio.h version.

Quoted 6 lines
Quoted 1 line

But you lost that nice description during the code move!!

That description was clearly specific to how that macro is used in drivers/vfio/*.c, along the lines of

                minsz = offsetofend(struct vfio_eeh_pe_op, op);

Hm, but here 'minsz' == sizeof(struct vfio_eeh_pe_op), so the vfio usage does not seem to be justified.

Quoted 6 lines
                if (copy_from_user(&op, (void __user *)arg, minsz))
                        return -EFAULT;
                if (op.argsz < minsz || op.flags)
                        return -EINVAL;

But the macro is generic, it has many other uses besides this one.

Quoted 7 lines

So I might be missing something, but what generic uses does it have, beyond structures that have some rare size related weirdness, such as alignment attributes? In 99% of the cases:

   sizeof(struct) == offsetofend(struct, last_member)

right?

struct foo {
    u64 a;
    char b;
};

sizeof(struct foo) will be 16, but offsetofend(struct foo, b) will be 9 on most platforms, right?

--Andy

#11 lore raw
Quoted 8 lines · Andy Lutomirski
Quoted 7 lines

So I might be missing something, but what generic uses does it have, beyond structures that have some rare size related weirdness, such as alignment attributes? In 99% of the cases:

   sizeof(struct) == offsetofend(struct, last_member)

right?

Quoted 7 lines
struct foo {
    u64 a;
    char b;
};

sizeof(struct foo) will be 16, but offsetofend(struct foo, b) will be 9 on most platforms, right?

I knew I missed something obvious :-)

Let me attempt to get it right:

When the next byte after the last member of a structure is not aligned to the largest alignment requirement of any structure member, then the structure grows (is padded) and offsetofend() < sizeof().

'packed' or 'aligned' attributes will modify the largest alignment requirement value so they are a common but not only mechanism for this to be the case.

Thanks,

Ingo

#12 lore raw
In reply to Ingo Molnar
Quoted 37 lines · Ingo Molnar
Quoted 18 lines · Denys Vlasenko
Quoted 17 lines · Ingo Molnar

include/linux/vfio.h extern int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops); · line 92

Quoted 12 lines
include/linux/vfio.h +0 -12
-/**
- * offsetofend(TYPE, MEMBER)
- *
- * @TYPE: The type of the structure
- * @MEMBER: The member within the structure to get the end offset of
- *
- * Simple helper macro for dealing with variable sized structures passed
- * from user space.  This allows us to easily determine if the provided
- * structure is sized to include various fields.
- */
-#define offsetofend(TYPE, MEMBER) \
-     (offsetof(TYPE, MEMBER) + sizeof(((TYPE *)0)->MEMBER))

So I like it, and because it is not particularly trivial when to use this primitive it was explained nicely in a description in the vfio.h version.

Quoted 6 lines
Quoted 1 line

But you lost that nice description during the code move!!

That description was clearly specific to how that macro is used in drivers/vfio/*.c, along the lines of

                minsz = offsetofend(struct vfio_eeh_pe_op, op);

Hm, but here 'minsz' == sizeof(struct vfio_eeh_pe_op), so the vfio usage does not seem to be justified.

Quoted 6 lines
                if (copy_from_user(&op, (void __user *)arg, minsz))
                        return -EFAULT;
                if (op.argsz < minsz || op.flags)
                        return -EINVAL;

But the macro is generic, it has many other uses besides this one.

Quoted 7 lines

So I might be missing something, but what generic uses does it have, beyond structures that have some rare size related weirdness, such as alignment attributes? In 99% of the cases:

   sizeof(struct) == offsetofend(struct, last_member)

right?

The idea in the vfio code is to allow the structure to evolve over time while maintaining compatibility. We effectively create a header in the structure with the base functionality and flags and structure size can tell us what optional fields are present. A better examples is drivers/vfio/pci/vfio_pci.c:vfio_pci_ioctl(), particularly VFIO_DEVICE_SET_IRQS where we read what's being provided in the "header" and then read beyond the minimum structure size if directed. Thanks,

Alex

Quoted 14 lines
Quoted 2 lines

Nevertheless, I can resend a version where comment survives if you want...

So maybe extend it to a description that you think describes its uses correctly? People will keep wondering about when to use this.

Thanks,

Ingo
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

#14 lore raw

[tip:x86/asm] include/stddef.h: Move offsetofend() from vfio.h to a generic kernel header

In reply to Denys Vlasenko
include/stddef.h: Move offsetofend() from vfio.h to a generic kernel header
Commit-ID:  23ec13ebc0426fc5f3ecc623c432d57f919e1b28
Gitweb:     http://git.kernel.org/tip/23ec13ebc0426fc5f3ecc623c432d57f919e1b28
Author:     Denys Vlasenko <[email protected]>
AuthorDate: Mon, 9 Mar 2015 15:52:17 +0100
Committer:  Ingo Molnar <[email protected]>
CommitDate: Mon, 16 Mar 2015 11:05:37 +0100

include/stddef.h: Move offsetofend() from vfio.h to a generic kernel header

Suggested by Andy.

---
 include/linux/stddef.h |  9 +++++++++
 include/linux/vfio.h   | 13 -------------
 2 files changed, 9 insertions(+), 13 deletions(-)
include/linux/stddef.h +9 -0
index f4aec0e..076af43 100644
@@ -19,3 +19,12 @@ enum {
 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
 #endif
 #endif
+
+/**
+ * offsetofend(TYPE, MEMBER)
+ *
+ * @TYPE: The type of the structure
+ * @MEMBER: The member within the structure to get the end offset of
+ */
+#define offsetofend(TYPE, MEMBER) \
+	(offsetof(TYPE, MEMBER)	+ sizeof(((TYPE *)0)->MEMBER))
include/linux/vfio.h +0 -13
index 2d67b89..049b2f4 100644
@@ -78,19 +78,6 @@ extern int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops);
 extern void vfio_unregister_iommu_driver(
 				const struct vfio_iommu_driver_ops *ops);
 
-/**
- * offsetofend(TYPE, MEMBER)
- *
- * @TYPE: The type of the structure
- * @MEMBER: The member within the structure to get the end offset of
- *
- * Simple helper macro for dealing with variable sized structures passed
- * from user space.  This allows us to easily determine if the provided
- * structure is sized to include various fields.
- */
-#define offsetofend(TYPE, MEMBER) \
-	(offsetof(TYPE, MEMBER)	+ sizeof(((TYPE *)0)->MEMBER))
-
 /*
  * External user API
  */
#15 lore raw

[tip:x86/asm] include/stddef.h: Move offsetofend() from vfio.h to a generic kernel header

In reply to Denys Vlasenko
include/stddef.h: Move offsetofend() from vfio.h to a generic kernel header
Commit-ID:  3876488444e71238e287459c39d7692b6f718c3e
Gitweb:     http://git.kernel.org/tip/3876488444e71238e287459c39d7692b6f718c3e
Author:     Denys Vlasenko <[email protected]>
AuthorDate: Mon, 9 Mar 2015 15:52:17 +0100
Committer:  Ingo Molnar <[email protected]>
CommitDate: Tue, 17 Mar 2015 09:25:28 +0100

include/stddef.h: Move offsetofend() from vfio.h to a generic kernel header

Suggested by Andy.

---
 include/linux/stddef.h |  9 +++++++++
 include/linux/vfio.h   | 13 -------------
 2 files changed, 9 insertions(+), 13 deletions(-)
include/linux/stddef.h +9 -0
index f4aec0e..076af43 100644
@@ -19,3 +19,12 @@ enum {
 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
 #endif
 #endif
+
+/**
+ * offsetofend(TYPE, MEMBER)
+ *
+ * @TYPE: The type of the structure
+ * @MEMBER: The member within the structure to get the end offset of
+ */
+#define offsetofend(TYPE, MEMBER) \
+	(offsetof(TYPE, MEMBER)	+ sizeof(((TYPE *)0)->MEMBER))
include/linux/vfio.h +0 -13
index 2d67b89..049b2f4 100644
@@ -78,19 +78,6 @@ extern int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops);
 extern void vfio_unregister_iommu_driver(
 				const struct vfio_iommu_driver_ops *ops);
 
-/**
- * offsetofend(TYPE, MEMBER)
- *
- * @TYPE: The type of the structure
- * @MEMBER: The member within the structure to get the end offset of
- *
- * Simple helper macro for dealing with variable sized structures passed
- * from user space.  This allows us to easily determine if the provided
- * structure is sized to include various fields.
- */
-#define offsetofend(TYPE, MEMBER) \
-	(offsetof(TYPE, MEMBER)	+ sizeof(((TYPE *)0)->MEMBER))
-
 /*
  * External user API
  */