Skip to contentSkip to conversation

tools/virtio: fix build caused by virtio_ring changes

PATCH In review Shunsuke Mie 2 participants 6 messages Apr 10, 2023 virtio core

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools/virtio: fix build caused by virtio_ring changes

Fix the build dependency for virtio_test. virtio_ring requires container_of_const() and struce device.

---
 tools/include/linux/container_of.h | 36 ++++++++++++++++++++++++++++++
 tools/virtio/linux/compiler.h      |  3 +++
 tools/virtio/linux/kernel.h        |  5 +----
 tools/virtio/linux/module.h        |  1 +
 4 files changed, 41 insertions(+), 4 deletions(-)
 create mode 100644 tools/include/linux/container_of.h
tools/include/linux/container_of.h new file +36 -0
new file mode 100644
index 000000000000..06e293b7cfda
@@ -0,0 +1,36 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_CONTAINER_OF_H
+#define _LINUX_CONTAINER_OF_H
+
+#include <linux/build_bug.h>
+#include <linux/stddef.h>
+
+/**
+ * container_of - cast a member of a structure out to the containing structure
+ * @ptr:	the pointer to the member.
+ * @type:	the type of the container struct this is embedded in.
+ * @member:	the name of the member within the struct.
+ *
+ * WARNING: any const qualifier of @ptr is lost.
+ */
+#define container_of(ptr, type, member) ({				\
+	void *__mptr = (void *)(ptr);					\
+	static_assert(__same_type(*(ptr), ((type *)0)->member) ||	\
+		      __same_type(*(ptr), void),			\
+		      "pointer type mismatch in container_of()");	\
+	((type *)(__mptr - offsetof(type, member))); })
+
+/**
+ * container_of_const - cast a member of a structure out to the containing
+ *			structure and preserve the const-ness of the pointer
+ * @ptr:		the pointer to the member
+ * @type:		the type of the container struct this is embedded in.
+ * @member:		the name of the member within the struct.
+ */
+#define container_of_const(ptr, type, member)				\
+	_Generic(ptr,							\
+		const typeof(*(ptr)) *: ((const type *)container_of(ptr, type, member)),\
+		default: ((type *)container_of(ptr, type, member))	\
+	)
+
+#endif	/* _LINUX_CONTAINER_OF_H */
tools/virtio/linux/compiler.h +3 -0
index 2c51bccb97bb..ac27b3ea6e67 100644
@@ -8,4 +8,7 @@
 #define READ_ONCE(var) (*((volatile typeof(var) *)(&(var))))
 
 #define __aligned(x) __attribute((__aligned__(x)))
+
+#define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
+
 #endif
tools/virtio/linux/kernel.h +1 -4
index 8b877167933d..3cd20cb92649 100644
@@ -10,6 +10,7 @@
 #include <stdarg.h>
 
 #include <linux/compiler.h>
+#include <linux/container_of.h>
 #include <linux/log2.h>
 #include <linux/types.h>
 #include <linux/overflow.h>
@@ -107,10 +108,6 @@ static inline void free_page(unsigned long addr)
 	free((void *)addr);
 }
 
-#define container_of(ptr, type, member) ({			\
-	const typeof( ((type *)0)->member ) *__mptr = (ptr);	\
-	(type *)( (char *)__mptr - offsetof(type,member) );})
-
 # ifndef likely
 #  define likely(x)	(__builtin_expect(!!(x), 1))
 # endif
tools/virtio/linux/module.h +1 -0
index 9dfa96fea2b2..e2e791dbd104 100644
@@ -5,3 +5,4 @@
 	static __attribute__((unused)) const char *__MODULE_LICENSE_name = \
 		__MODULE_LICENSE_value
 
+struct device;
Signature · 6 lines
2.25.1

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization
#1 lore raw
Quoted 44 lines · Shunsuke Mie

Fix the build dependency for virtio_test. virtio_ring requires container_of_const() and struce device.

---
 tools/include/linux/container_of.h | 36 ++++++++++++++++++++++++++++++
 tools/virtio/linux/compiler.h      |  3 +++
 tools/virtio/linux/kernel.h        |  5 +----
 tools/virtio/linux/module.h        |  1 +
 4 files changed, 41 insertions(+), 4 deletions(-)
 create mode 100644 tools/include/linux/container_of.h
tools/include/linux/container_of.h new file +26 -0
new file mode 100644
index 000000000000..06e293b7cfda
@@ -0,0 +1,36 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_CONTAINER_OF_H
+#define _LINUX_CONTAINER_OF_H
+
+#include <linux/build_bug.h>
+#include <linux/stddef.h>
+
+/**
+ * container_of - cast a member of a structure out to the containing structure
+ * @ptr:	the pointer to the member.
+ * @type:	the type of the container struct this is embedded in.
+ * @member:	the name of the member within the struct.
+ *
+ * WARNING: any const qualifier of @ptr is lost.
+ */
+#define container_of(ptr, type, member) ({				\
+	void *__mptr = (void *)(ptr);					\
+	static_assert(__same_type(*(ptr), ((type *)0)->member) ||	\
+		      __same_type(*(ptr), void),			\
+		      "pointer type mismatch in container_of()");	\
+	((type *)(__mptr - offsetof(type, member))); })
+
+/**
+ * container_of_const - cast a member of a structure out to the containing
+ *			structure and preserve the const-ness of the pointer
+ * @ptr:		the pointer to the member

tools/include/linux/container_of.h line 36

Quoted 10 lines
tools/include/linux/container_of.h new file +10 -0
+ * @type:		the type of the container struct this is embedded in.
+ * @member:		the name of the member within the struct.
+ */
+#define container_of_const(ptr, type, member)				\
+	_Generic(ptr,							\
+		const typeof(*(ptr)) *: ((const type *)container_of(ptr, type, member)),\
+		default: ((type *)container_of(ptr, type, member))	\
+	)
+
+#endif	/* _LINUX_CONTAINER_OF_H */

Please just do
#include "../../../include/linux/container_of.h"
instead.

tools/virtio/linux/compiler.h line 14

Quoted 12 lines
tools/virtio/linux/compiler.h +3 -0
index 2c51bccb97bb..ac27b3ea6e67 100644
@@ -8,4 +8,7 @@
 #define READ_ONCE(var) (*((volatile typeof(var) *)(&(var))))
 
 #define __aligned(x) __attribute((__aligned__(x)))
+
+#define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
+
 #endif

#include "../../../include/linux/compiler_types.h" instead?

Quoted 28 lines
tools/virtio/linux/kernel.h +1 -4
index 8b877167933d..3cd20cb92649 100644
@@ -10,6 +10,7 @@
 #include <stdarg.h>
 
 #include <linux/compiler.h>
+#include <linux/container_of.h>
 #include <linux/log2.h>
 #include <linux/types.h>
 #include <linux/overflow.h>
@@ -107,10 +108,6 @@ static inline void free_page(unsigned long addr)
 	free((void *)addr);
 }
 
-#define container_of(ptr, type, member) ({			\
-	const typeof( ((type *)0)->member ) *__mptr = (ptr);	\
-	(type *)( (char *)__mptr - offsetof(type,member) );})
-
 # ifndef likely
 #  define likely(x)	(__builtin_expect(!!(x), 1))
 # endif
tools/virtio/linux/module.h +0 -0
index 9dfa96fea2b2..e2e791dbd104 100644

tools/virtio/linux/module.h line 8

Quoted 4 lines
tools/virtio/linux/module.h +1 -0
@@ -5,3 +5,4 @@
 	static __attribute__((unused)) const char *__MODULE_LICENSE_name = \
 		__MODULE_LICENSE_value
 
+struct device;

It's not there in include/linux/module - pls put it in the same here where it is for linux.

Quoted 2 lines
Signature · 1 line
2.25.1

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#2 lore raw
Quoted 51 lines · Michael S. Tsirkin
Quoted 50 lines · Shunsuke Mie

Fix the build dependency for virtio_test. virtio_ring requires container_of_const() and struce device.

---
  tools/include/linux/container_of.h | 36 ++++++++++++++++++++++++++++++
  tools/virtio/linux/compiler.h      |  3 +++
  tools/virtio/linux/kernel.h        |  5 +----
  tools/virtio/linux/module.h        |  1 +
  4 files changed, 41 insertions(+), 4 deletions(-)

create mode 100644 tools/include/linux/container_of.h

tools/include/linux/container_of.h new file +32 -0
new file mode 100644
index 000000000000..06e293b7cfda
@@ -0,0 +1,36 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_CONTAINER_OF_H
+#define _LINUX_CONTAINER_OF_H
+
+#include <linux/build_bug.h>
+#include <linux/stddef.h>
+
+/**
+ * container_of - cast a member of a structure out to the containing structure
+ * @ptr:	the pointer to the member.
+ * @type:	the type of the container struct this is embedded in.
+ * @member:	the name of the member within the struct.
+ *
+ * WARNING: any const qualifier of @ptr is lost.
+ */
+#define container_of(ptr, type, member) ({				\
+	void *__mptr = (void *)(ptr);					\
+	static_assert(__same_type(*(ptr), ((type *)0)->member) ||	\
+		      __same_type(*(ptr), void),			\
+		      "pointer type mismatch in container_of()");	\
+	((type *)(__mptr - offsetof(type, member))); })
+
+/**
+ * container_of_const - cast a member of a structure out to the containing
+ *			structure and preserve the const-ness of the pointer
+ * @ptr:		the pointer to the member
+ * @type:		the type of the container struct this is embedded in.
+ * @member:		the name of the member within the struct.
+ */
+#define container_of_const(ptr, type, member)				\
+	_Generic(ptr,							\
+		const typeof(*(ptr)) *: ((const type *)container_of(ptr, type, member)),\
Quoted 8 lines

tools/include/linux/container_of.h line 36

Quoted 4 lines
tools/include/linux/container_of.h new file +4 -0
+		default: ((type *)container_of(ptr, type, member))	\
+	)
+
+#endif	/* _LINUX_CONTAINER_OF_H */

Please just do
#include "../../../include/linux/container_of.h"
instead.

Okey, I'll do that.

Quoted 9 lines
Quoted 9 lines
tools/virtio/linux/compiler.h +0 -0
index 2c51bccb97bb..ac27b3ea6e67 100644
@@ -8,4 +8,7 @@
  #define READ_ONCE(var) (*((volatile typeof(var) *)(&(var))))
 
Quoted 6 lines

tools/virtio/linux/compiler.h line 14

Quoted 5 lines
tools/virtio/linux/compiler.h +3 -0
  #define __aligned(x) __attribute((__aligned__(x)))
+
+#define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
+
  #endif

#include "../../../include/linux/compiler_types.h" instead?

I'll try it.

Quoted 31 lines
Quoted 31 lines
tools/virtio/linux/kernel.h +1 -4
index 8b877167933d..3cd20cb92649 100644
@@ -10,6 +10,7 @@
  #include <stdarg.h>
 
  #include <linux/compiler.h>
+#include <linux/container_of.h>
  #include <linux/log2.h>
  #include <linux/types.h>
  #include <linux/overflow.h>
@@ -107,10 +108,6 @@ static inline void free_page(unsigned long addr)
  	free((void *)addr);
  }
 
-#define container_of(ptr, type, member) ({			\
-	const typeof( ((type *)0)->member ) *__mptr = (ptr);	\
-	(type *)( (char *)__mptr - offsetof(type,member) );})
-
  # ifndef likely
  #  define likely(x)	(__builtin_expect(!!(x), 1))
  # endif
tools/virtio/linux/module.h +0 -0
index 9dfa96fea2b2..e2e791dbd104 100644
@@ -5,3 +5,4 @@
  	static __attribute__((unused)) const char *__MODULE_LICENSE_name = \
  		__MODULE_LICENSE_value
 
Quoted 4 lines

tools/virtio/linux/module.h line 8

Quoted 1 line
tools/virtio/linux/module.h +1 -0
+struct device;

It's not there in include/linux/module - pls put it in the same here where it is for linux.

I understood. I'll add some files that has the definition.

Quoted 2 lines
Quoted 2 lines
Signature · 1 line
2.25.1

Best regards,

Shunsuke.

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#3 lore raw
Quoted 114 lines · Shunsuke Mie
Quoted 55 lines · Michael S. Tsirkin
Quoted 54 lines · Shunsuke Mie

Fix the build dependency for virtio_test. virtio_ring requires container_of_const() and struce device.

---
  tools/include/linux/container_of.h | 36
++++++++++++++++++++++++++++++
  tools/virtio/linux/compiler.h      |  3 +++
  tools/virtio/linux/kernel.h        |  5 +----
  tools/virtio/linux/module.h        |  1 +

  4 files changed, 41 insertions(+), 4 deletions(-)

  create mode 100644 tools/include/linux/container_of.h

tools/include/linux/container_of.h new file +9 -0
new file mode 100644
index 000000000000..06e293b7cfda
@@ -0,0 +1,36 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_CONTAINER_OF_H
+#define _LINUX_CONTAINER_OF_H
+
+#include <linux/build_bug.h>
+#include <linux/stddef.h>
+
+/**
+ * container_of - cast a member of a structure out to the 

containing structure
+ * @ptr:    the pointer to the member.
+ * @type:    the type of the container struct this is embedded in.
+ * @member:    the name of the member within the struct.
+ *
+ * WARNING: any const qualifier of @ptr is lost.
+ */

tools/include/linux/container_of.h new file +9 -0
+#define container_of(ptr, type, member) ({                \
+    void *__mptr = (void *)(ptr);                    \
+    static_assert(__same_type(*(ptr), ((type *)0)->member) ||    \
+              __same_type(*(ptr), void),            \
+              "pointer type mismatch in container_of()"); \
+    ((type *)(__mptr - offsetof(type, member))); })
+
+/**
+ * container_of_const - cast a member of a structure out to the 

containing
+ *            structure and preserve the const-ness of the pointer
+ * @ptr:        the pointer to the member
+ * @type:        the type of the container struct this is embedded in.
+ * @member:        the name of the member within the struct.
+ */
+#define container_of_const(ptr, type, member) \
+    _Generic(ptr,                            \
+        const typeof(*(ptr)) *: ((const type *)container_of(ptr, type, member)),\

Quoted 8 lines

tools/include/linux/container_of.h

Quoted 4 lines

+        default: ((type *)container_of(ptr, type, member)) \
+    )

tools/include/linux/container_of.h new file +2 -0
+
+#endif    /* _LINUX_CONTAINER_OF_H */

Please just do
#include "../../../include/linux/container_of.h"
instead.

Okey, I'll do that.

Quoted 15 lines
Quoted 7 lines
tools/virtio/linux/compiler.h +0 -0
index 2c51bccb97bb..ac27b3ea6e67 100644

tools/virtio/linux/compiler.h line 14

Quoted 7 lines
tools/virtio/linux/compiler.h +3 -0
@@ -8,4 +8,7 @@
   #define READ_ONCE(var) (*((volatile typeof(var) *)(&(var))))
     #define __aligned(x) __attribute((__aligned__(x)))
+
+#define __same_type(a, b) __builtin_types_compatible_p(typeof(a), 
 typeof(b))
+
   #endif

#include "../../../include/linux/compiler_types.h" instead?

I'll try it.

Quoted 24 lines
Quoted 24 lines
tools/virtio/linux/kernel.h +1 -3
index 8b877167933d..3cd20cb92649 100644
@@ -10,6 +10,7 @@
   #include <stdarg.h>
     #include <linux/compiler.h>
+#include <linux/container_of.h>
   #include <linux/log2.h>
   #include <linux/types.h>
   #include <linux/overflow.h>
@@ -107,10 +108,6 @@ static inline void free_page(unsigned long addr)
       free((void *)addr);
   }
   -#define container_of(ptr, type, member) ({            \
-    const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
-    (type *)( (char *)__mptr - offsetof(type,member) );})
-
   # ifndef likely
   #  define likely(x)    (__builtin_expect(!!(x), 1))
   # endif
tools/virtio/linux/module.h +0 -1
index 9dfa96fea2b2..e2e791dbd104 100644
--- a/tools/virtio/linux/module.h
Quoted 8 lines
Quoted 5 lines

+++ b/tools/virtio/linux/module.h

tools/virtio/linux/module.h +0 -0
@@ -5,3 +5,4 @@
       static __attribute__((unused)) const char *__MODULE_LICENSE_name = \

          __MODULE_LICENSE_value
  +struct device;

It's not there in include/linux/module - pls put it in the same here where it is for linux.

Quoted 1 line

I understood. I'll add some files that has the definition.

In vringh.c, 'struct device' is used in virtio_ring.h. Upon investigating the preprocessed file, it was

discovered that 'struct device' is defined in bitmap.h, which is included from architecture-specific headers.

On x86, the nesting includes

'linux/module.h' -> ... -> 'arch/x86/include/asm/msr.h' -> 'arch/x86/include/asm/cpumask.h' -> 'linux/cpumask.h' -> 'linux/bitmap.h'.

Could you advise on the appropriate way to include this file?

Quoted 7 lines
Quoted 2 lines
Quoted 2 lines
Signature · 1 line
2.25.1

Best regards,

Shunsuke.

#4 lore raw
Quoted 127 lines · Shunsuke Mie
Quoted 117 lines · Shunsuke Mie
Quoted 56 lines · Michael S. Tsirkin
Quoted 55 lines · Shunsuke Mie

Fix the build dependency for virtio_test. virtio_ring requires container_of_const() and struce device.

---
  tools/include/linux/container_of.h | 36
++++++++++++++++++++++++++++++
  tools/virtio/linux/compiler.h      |  3 +++
  tools/virtio/linux/kernel.h        |  5 +----
  tools/virtio/linux/module.h        |  1 +

  4 files changed, 41 insertions(+), 4 deletions(-)

  create mode 100644 tools/include/linux/container_of.h

tools/include/linux/container_of.h new file +9 -0
new file mode 100644
index 000000000000..06e293b7cfda
@@ -0,0 +1,36 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_CONTAINER_OF_H
+#define _LINUX_CONTAINER_OF_H
+
+#include <linux/build_bug.h>
+#include <linux/stddef.h>
+
+/**
+ * container_of - cast a member of a structure out to the

containing structure
+ * @ptr:    the pointer to the member.
+ * @type:    the type of the container struct this is embedded in.
+ * @member:    the name of the member within the struct.
+ *
+ * WARNING: any const qualifier of @ptr is lost.
+ */

tools/include/linux/container_of.h new file +9 -0
+#define container_of(ptr, type, member) ({                \
+    void *__mptr = (void *)(ptr);                    \
+    static_assert(__same_type(*(ptr), ((type *)0)->member) ||    \
+              __same_type(*(ptr), void),            \
+              "pointer type mismatch in container_of()"); \
+    ((type *)(__mptr - offsetof(type, member))); })
+
+/**
+ * container_of_const - cast a member of a structure out to the

containing
+ *            structure and preserve the const-ness of the pointer
+ * @ptr:        the pointer to the member
+ * @type:        the type of the container struct this is embedded in.
+ * @member:        the name of the member within the struct.
+ */
+#define container_of_const(ptr, type, member) \
+    _Generic(ptr,                            \
+        const typeof(*(ptr)) *: ((const type *)container_of(ptr, type, member)),\

Quoted 8 lines

tools/include/linux/container_of.h

Quoted 4 lines

+        default: ((type *)container_of(ptr, type, member)) \
+    )

tools/include/linux/container_of.h new file +2 -0
+
+#endif    /* _LINUX_CONTAINER_OF_H */

Please just do
#include "../../../include/linux/container_of.h"
instead.

Okey, I'll do that.

Quoted 16 lines
Quoted 8 lines
tools/virtio/linux/compiler.h +0 -0
index 2c51bccb97bb..ac27b3ea6e67 100644

tools/virtio/linux/compiler.h line 14

Quoted 7 lines
tools/virtio/linux/compiler.h +3 -0
@@ -8,4 +8,7 @@
   #define READ_ONCE(var) (*((volatile typeof(var) *)(&(var))))
     #define __aligned(x) __attribute((__aligned__(x)))
+
+#define __same_type(a, b)
 __builtin_types_compatible_p(typeof(a), typeof(b))
+
   #endif

#include "../../../include/linux/compiler_types.h" instead?

I'll try it.

Quoted 25 lines
Quoted 25 lines
tools/virtio/linux/kernel.h +1 -3
index 8b877167933d..3cd20cb92649 100644
@@ -10,6 +10,7 @@
   #include <stdarg.h>
     #include <linux/compiler.h>
+#include <linux/container_of.h>
   #include <linux/log2.h>
   #include <linux/types.h>
   #include <linux/overflow.h>
@@ -107,10 +108,6 @@ static inline void free_page(unsigned long addr)
       free((void *)addr);
   }
   -#define container_of(ptr, type, member) ({            \
-    const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
-    (type *)( (char *)__mptr - offsetof(type,member) );})
-
   # ifndef likely
   #  define likely(x)    (__builtin_expect(!!(x), 1))
   # endif
tools/virtio/linux/module.h +0 -0
index 9dfa96fea2b2..e2e791dbd104 100644
Quoted 8 lines
Quoted 5 lines
tools/virtio/linux/module.h +0 -0
@@ -5,3 +5,4 @@

      static __attribute__((unused)) const char
*__MODULE_LICENSE_name = \
          __MODULE_LICENSE_value
  +struct device;

It's not there in include/linux/module - pls put it in the same here where it is for linux.

Quoted 1 line

I understood. I'll add some files that has the definition.

In vringh.c, 'struct device' is used in virtio_ring.h. Upon investigating the preprocessed file, it was

discovered that 'struct device' is defined in bitmap.h, which is included from architecture-specific headers.

Quoted 6 lines

On x86, the nesting includes

'linux/module.h' -> ... -> 'arch/x86/include/asm/msr.h' -> 'arch/x86/include/asm/cpumask.h' -> 'linux/cpumask.h' -> 'linux/bitmap.h'.

Could you advise on the appropriate way to include this file?

Let's just add struct device; in virtio_ring.h, make it self-sufficient.

Quoted 7 lines
Quoted 7 lines
Quoted 2 lines
Quoted 2 lines
Signature · 1 line
2.25.1

Best regards,

Shunsuke.

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#5 lore raw
Quoted 122 lines · Michael S. Tsirkin
Quoted 121 lines · Shunsuke Mie
Quoted 113 lines · Shunsuke Mie
Quoted 55 lines · Michael S. Tsirkin
Quoted 54 lines · Shunsuke Mie

Fix the build dependency for virtio_test. virtio_ring requires container_of_const() and struce device.

---
   tools/include/linux/container_of.h | 36
++++++++++++++++++++++++++++++
   tools/virtio/linux/compiler.h      |  3 +++
   tools/virtio/linux/kernel.h        |  5 +----
   tools/virtio/linux/module.h        |  1 +
   4 files changed, 41 insertions(+), 4 deletions(-)

  create mode 100644 tools/include/linux/container_of.h

tools/include/linux/container_of.h new file +9 -0
new file mode 100644
index 000000000000..06e293b7cfda
@@ -0,0 +1,36 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_CONTAINER_OF_H
+#define _LINUX_CONTAINER_OF_H
+
+#include <linux/build_bug.h>
+#include <linux/stddef.h>
+
+/**
+ * container_of - cast a member of a structure out to the

containing structure
+ * @ptr:    the pointer to the member.
+ * @type:    the type of the container struct this is embedded in.
+ * @member:    the name of the member within the struct.
+ *
+ * WARNING: any const qualifier of @ptr is lost.
+ */

tools/include/linux/container_of.h new file +9 -0
+#define container_of(ptr, type, member) ({                \
+    void *__mptr = (void *)(ptr);                    \
+    static_assert(__same_type(*(ptr), ((type *)0)->member) ||    \
+              __same_type(*(ptr), void),            \
+              "pointer type mismatch in container_of()"); \
+    ((type *)(__mptr - offsetof(type, member))); })
+
+/**
+ * container_of_const - cast a member of a structure out to the

containing
+ *            structure and preserve the const-ness of the pointer
+ * @ptr:        the pointer to the member
+ * @type:        the type of the container struct this is embedded in.
+ * @member:        the name of the member within the struct.
+ */
+#define container_of_const(ptr, type, member) \
+    _Generic(ptr,                            \
+        const typeof(*(ptr)) *: ((const type

Quoted 8 lines

tools/include/linux/container_of.h

Quoted 5 lines

*)container_of(ptr, type, member)),\
+        default: ((type *)container_of(ptr, type, member)) \
+    )

tools/include/linux/container_of.h new file +2 -0
+
+#endif    /* _LINUX_CONTAINER_OF_H */

Please just do
#include "../../../include/linux/container_of.h"
instead.

Okey, I'll do that.

Quoted 15 lines
Quoted 7 lines
tools/virtio/linux/compiler.h +0 -0
index 2c51bccb97bb..ac27b3ea6e67 100644

tools/virtio/linux/compiler.h line 14

Quoted 7 lines
tools/virtio/linux/compiler.h +3 -0
@@ -8,4 +8,7 @@
   #define READ_ONCE(var) (*((volatile typeof(var) *)(&(var))))
     #define __aligned(x) __attribute((__aligned__(x)))
+
+#define __same_type(a, b)
 __builtin_types_compatible_p(typeof(a), typeof(b))
+
   #endif

#include "../../../include/linux/compiler_types.h" instead?

I'll try it.

Quoted 24 lines
Quoted 24 lines
tools/virtio/linux/kernel.h +1 -3
index 8b877167933d..3cd20cb92649 100644
@@ -10,6 +10,7 @@
   #include <stdarg.h>
     #include <linux/compiler.h>
+#include <linux/container_of.h>
   #include <linux/log2.h>
   #include <linux/types.h>
   #include <linux/overflow.h>
@@ -107,10 +108,6 @@ static inline void free_page(unsigned long addr)
       free((void *)addr);
   }
   -#define container_of(ptr, type, member) ({            \
-    const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
-    (type *)( (char *)__mptr - offsetof(type,member) );})
-
   # ifndef likely
   #  define likely(x)    (__builtin_expect(!!(x), 1))
   # endif
tools/virtio/linux/module.h +0 -1
index 9dfa96fea2b2..e2e791dbd104 100644
--- a/tools/virtio/linux/module.h
Quoted 8 lines

tools/virtio/linux/module.h

Quoted 6 lines

+++ b/tools/virtio/linux/module.h

tools/virtio/linux/module.h +0 -0
@@ -5,3 +5,4 @@
       static __attribute__((unused)) const char
 *__MODULE_LICENSE_name = \
           __MODULE_LICENSE_value
   +struct device;

It's not there in include/linux/module - pls put it in the same here where it is for linux.

Quoted 1 line

I understood. I'll add some files that has the definition.

In vringh.c, 'struct device' is used in virtio_ring.h. Upon investigating the preprocessed file, it was

discovered that 'struct device' is defined in bitmap.h, which is included from architecture-specific headers.

Quoted 9 lines
Quoted 6 lines

On x86, the nesting includes

'linux/module.h' -> ... -> 'arch/x86/include/asm/msr.h' -> 'arch/x86/include/asm/cpumask.h' -> 'linux/cpumask.h' -> 'linux/bitmap.h'.

Could you advise on the appropriate way to include this file?

Let's just add struct device; in virtio_ring.h, make it self-sufficient.

I agree. Thanks a lot.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 2 lines
Quoted 2 lines
Signature · 1 line
2.25.1

Best regards,

Shunsuke.

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization