Skip to contentSkip to conversation

FATAL: drivers/gpu/drm/udl/udl: sizeof(struct usb_device_id)=24 is not a modulo of the size of section __mod_usb_device_table=44.

-next Superseded Geert Uytterhoeven 6 participants 17 messages Jun 15, 2012 – Jun 26, 2012

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

commit 81df2d594340dcb6d1a02191976be88a1ca8120c ("USB: allow match on bInterfaceNumber") added a byte to the interior of struct usb_device_id, enabling implicit padding:

include/linux/mod_devicetable.h +3 -0
@@ -115,6 +118,9 @@ struct usb_device_id {
        __u8            bInterfaceSubClass;
        __u8            bInterfaceProtocol;
 
+       /* Used for vendor-specific interface matches */
+       __u8            bInterfaceNumber;
+
        /* not matched against */
        kernel_ulong_t  driver_info;
 };

On m68k, this causes failures like:

| FATAL: drivers/gpu/drm/udl/udl: sizeof(struct usb_device_id)=24 is not a modulo of the size of section __mod_usb_device_table=44. | Fix definition of struct usb_device_id in mod_devicetable.h

M68k is special in that it uses 2 for the alignment of 32-bit entities, hence sizeof(struct usb_device_id) = 22.

However, when cross-compiling on amd64, sizeof(struct usb_device_id) = 24 in scripts/mod/file2alias.c.

m68k/allmodconfig at http://kisskb.ellerman.id.au/kisskb/buildresult/6518563/

Gr{oetje,eeting}s,

                        Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds

#1 lore raw
Quoted 23 lines · Geert Uytterhoeven

commit 81df2d594340dcb6d1a02191976be88a1ca8120c ("USB: allow match on bInterfaceNumber") added a byte to the interior of struct usb_device_id, enabling implicit padding:

include/linux/mod_devicetable.h +3 -0
@@ -115,6 +118,9 @@ struct usb_device_id {
        __u8            bInterfaceSubClass;
        __u8            bInterfaceProtocol;
 
+       /* Used for vendor-specific interface matches */
+       __u8            bInterfaceNumber;
+
        /* not matched against */
        kernel_ulong_t  driver_info;
 };

On m68k, this causes failures like:

| FATAL: drivers/gpu/drm/udl/udl: sizeof(struct usb_device_id)=24 is not a modulo of the size of section __mod_usb_device_table=44. | Fix definition of struct usb_device_id in mod_devicetable.h

Quoted 5 lines

M68k is special in that it uses 2 for the alignment of 32-bit entities, hence sizeof(struct usb_device_id) = 22.

However, when cross-compiling on amd64, sizeof(struct usb_device_id) = 24 in scripts/mod/file2alias.c.

I don't really understand, what is the problem here? The fact that we added one byte to the structure? And now something breaks? How?

What can we do to resolve it?

confused,

greg k-h

#2 lore raw

Hi Greg,

Quoted 27 lines · Greg Kroah-Hartman
Quoted 26 lines · Geert Uytterhoeven

commit 81df2d594340dcb6d1a02191976be88a1ca8120c ("USB: allow match on bInterfaceNumber") added a byte to the interior of struct usb_device_id, enabling implicit padding:

include/linux/mod_devicetable.h +3 -0
@@ -115,6 +118,9 @@ struct usb_device_id {
         __u8            bInterfaceSubClass;
         __u8            bInterfaceProtocol;
 
+       /* Used for vendor-specific interface matches */
+       __u8            bInterfaceNumber;
+
         /* not matched against */
         kernel_ulong_t  driver_info;
  };

On m68k, this causes failures like:

| FATAL: drivers/gpu/drm/udl/udl: sizeof(struct usb_device_id)=24 is not a modulo of the size of section __mod_usb_device_table=44. | Fix definition of struct usb_device_id in mod_devicetable.h

M68k is special in that it uses 2 for the alignment of 32-bit entities, hence sizeof(struct usb_device_id) = 22.

Quoted 7 lines
Quoted 2 lines

However, when cross-compiling on amd64, sizeof(struct usb_device_id) = 24 in scripts/mod/file2alias.c.

I don't really understand, what is the problem here?  The fact that we added one byte to the structure?  And now something breaks?  How?

What can we do to resolve it?

As "kernel_ulong_t  driver_info" is no longer naturally aligned, the compiler will
add implicit padding. But the padding depends on the architecture.

It can be fixed by adding explicit padding. Probably it should be padded by 7 bytes (not 3), as kernel_ulong_t may require 8-byte alignment on some 64-bit platforms. Or by an explicit alignment attribute.

See also
* commit 8175fe2dda1c93a9c596921c8ed4a0b4baccdefe ("HID: fix
hid_device_id for cross compiling")
* commit 7492d4a416d68ab4bd254b36ffcc4e0138daa8ff ("sdio: fix module
device table definition for m68k")
* commit 9e2d3cd34a159948dc753a14573e16bffc04dba8 ("[PATCH]
mod_devicetable.h fixes")

Still, there's a bug in file2alias (which is compiled by the host compiler), in that
it may use different padding than the target platform when cross-compiling.

Gr{oetje,eeting}s,

                        Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#3 lore raw
Quoted 37 lines · Geert Uytterhoeven

Hi Greg,

Quoted 33 lines · Greg Kroah-Hartman
Quoted 23 lines · Geert Uytterhoeven

commit 81df2d594340dcb6d1a02191976be88a1ca8120c ("USB: allow match on bInterfaceNumber") added a byte to the interior of struct usb_device_id, enabling implicit padding:

include/linux/mod_devicetable.h +3 -0
@@ -115,6 +118,9 @@ struct usb_device_id {
         __u8            bInterfaceSubClass;
         __u8            bInterfaceProtocol;
 
+       /* Used for vendor-specific interface matches */
+       __u8            bInterfaceNumber;
+
         /* not matched against */
         kernel_ulong_t  driver_info;
  };

On m68k, this causes failures like:

| FATAL: drivers/gpu/drm/udl/udl: sizeof(struct usb_device_id)=24 is not a modulo of the size of section __mod_usb_device_table=44. | Fix definition of struct usb_device_id in mod_devicetable.h

Quoted 5 lines

M68k is special in that it uses 2 for the alignment of 32-bit entities, hence sizeof(struct usb_device_id) = 22.

However, when cross-compiling on amd64, sizeof(struct usb_device_id) = 24 in scripts/mod/file2alias.c.

I don't really understand, what is the problem here?  The fact that we added one byte to the structure?  And now something breaks?  How?

Quoted 5 lines
Quoted 1 line

What can we do to resolve it?

As "kernel_ulong_t  driver_info" is no longer naturally aligned, the compiler will
add implicit padding. But the padding depends on the architecture.

Ah, so we were "lucky" before, nice.

Quoted 11 lines

It can be fixed by adding explicit padding. Probably it should be padded by 7 bytes (not 3), as kernel_ulong_t may require 8-byte alignment on some 64-bit platforms. Or by an explicit alignment attribute.

See also
* commit 8175fe2dda1c93a9c596921c8ed4a0b4baccdefe ("HID: fix
hid_device_id for cross compiling")
* commit 7492d4a416d68ab4bd254b36ffcc4e0138daa8ff ("sdio: fix module
device table definition for m68k")
* commit 9e2d3cd34a159948dc753a14573e16bffc04dba8 ("[PATCH]
mod_devicetable.h fixes")

So would the patch below fix this? It should force alignment of the driver_data field, which is all you want here, right?

Quoted 3 lines

Still, there's a bug in file2alias (which is compiled by the host compiler), in that
it may use different padding than the target platform when cross-compiling.

That's not good, but outside of this specific issue, right? Have we just been fortunate it hasn't really hit us yet?

thanks,

greg k-h

include/linux/mod_devicetable.h +2 -1
index 7771d45..6955045 100644
@@ -122,7 +122,8 @@ struct usb_device_id {
 	__u8		bInterfaceNumber;
 
 	/* not matched against */
-	kernel_ulong_t	driver_info;
+	kernel_ulong_t	driver_info
+		__attribute__((aligned(sizeof(kernel_ulong_t))));
 };
 
 /* Some useful macros to use to create struct usb_device_id */

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

#4 lore raw
Quoted 7 lines · Greg Kroah-Hartman
Quoted 3 lines · Geert Uytterhoeven

As "kernel_ulong_t  driver_info" is no longer naturally aligned, the compiler will
add implicit padding. But the padding depends on the architecture.

Ah, so we were "lucky" before, nice.

I don't really believe in luck :-) I think someone has been really smart here. Maybe too smart...

Quoted 31 lines
Quoted 11 lines

It can be fixed by adding explicit padding. Probably it should be padded by 7 bytes (not 3), as kernel_ulong_t may require 8-byte alignment on some 64-bit platforms. Or by an explicit alignment attribute.

See also
* commit 8175fe2dda1c93a9c596921c8ed4a0b4baccdefe ("HID: fix
hid_device_id for cross compiling")
* commit 7492d4a416d68ab4bd254b36ffcc4e0138daa8ff ("sdio: fix module
device table definition for m68k")
* commit 9e2d3cd34a159948dc753a14573e16bffc04dba8 ("[PATCH]
mod_devicetable.h fixes")

So would the patch below fix this? It should force alignment of the driver_data field, which is all you want here, right?

Quoted 3 lines

Still, there's a bug in file2alias (which is compiled by the host compiler), in that
it may use different padding than the target platform when cross-compiling.

That's not good, but outside of this specific issue, right? Have we just been fortunate it hasn't really hit us yet?

thanks,

greg k-h

include/linux/mod_devicetable.h +0 -0
index 7771d45..6955045 100644

include/linux/mod_devicetable.h struct usb_device_id { · line 127

Quoted 7 lines
include/linux/mod_devicetable.h +2 -1
@@ -122,7 +122,8 @@ struct usb_device_id {
 	__u8		bInterfaceNumber;
 
 	/* not matched against */
-	kernel_ulong_t	driver_info;
+	kernel_ulong_t	driver_info
+		__attribute__((aligned(sizeof(kernel_ulong_t))));
 };

This feels a lot like papering over the real problem. It will solve this instance, but the list of such previous "paper work" that Geert provided should be enough evidence that this will happen again the next time someone modifies a device id struct for some subsystem.

And adding forced aligment here feels wrong since there is no good reason why the (target) compiler shouldn't know the proper alignment for this structure, is there? OK, "feels wrong" is not a good argument. But it would be better to solve this problem once and for all.

AFAIK (which admittedly is not much wrt cross building) there is no way we can make the host built file2alias know the proper aligment for the structure in the target built modules. That's the background for this fix:

commit 4ce6efed48d736e3384c39ff87bda723e1f8e041

Author: Sam Ravnborg <[email protected]>
Date:   Sun Mar 23 21:38:54 2008 +0100

kbuild: soften modpost checks when doing cross builds

The module alias support in the kernel have a consistency check where it is checked that the size of a structure in the kernel and on the build host are the same.
For cross builds this check does not make sense so detect when we do cross builds and silently skip the check in these situations.
This fixes a build bug for a wireless driver when cross building for arm.

What I'm now wondering is why didn't this kick in? I believe we should find and fix that instead of playing around with the in-kernel structure alignments. If that's possible.

BTW, thanks for finding and reporting this at such an early stage, Geert.

Bjørn

#5 lore raw
Quoted 10 lines · Bjørn Mork

AFAIK (which admittedly is not much wrt cross building) there is no way we can make the host built file2alias know the proper aligment for the structure in the target built modules. That's the background for this fix:

commit 4ce6efed48d736e3384c39ff87bda723e1f8e041

Author: Sam Ravnborg <[email protected]>
Date:   Sun Mar 23 21:38:54 2008 +0100

kbuild: soften modpost checks when doing cross builds

This is not a fix in any sense of the word. modpost can only work properly if its view of the device_id structures matches *exactly* that of the target.

Andreas.

Signature · 7 lines
Andreas Schwab, [email protected]
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."
--
To unsubscribe from this list: send the line "unsubscribe linux-kbuild" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#6 lore raw
Quoted 11 lines · Andreas Schwab
Quoted 9 lines · Bjørn Mork

AFAIK (which admittedly is not much wrt cross building) there is no way we can make the host built file2alias know the proper aligment for the structure in the target built modules. That's the background for this fix:

commit 4ce6efed48d736e3384c39ff87bda723e1f8e041

Author: Sam Ravnborg <[email protected]>
Date:   Sun Mar 23 21:38:54 2008 +0100
Quoted 5 lines
Quoted 1 line

kbuild: soften modpost checks when doing cross builds

This is not a fix in any sense of the word. modpost can only work properly if its view of the device_id structures matches *exactly* that of the target.

You are absolutely correct, of course. I am starting to believe Greg is correct. Cross building works mostly by pure luck :-)

I found that the reason the change I referred to above didn't kick in was because CONFIG_DEBUG_SECTION_MISMATCH was set, causing modpost to be called from scripts/Makefile.build without the "-c" option:

# Do section mismatch analysis for each module/built-in.o ifdef CONFIG_DEBUG_SECTION_MISMATCH
cmd_secanalysis = ; scripts/mod/modpost $@
endif

But as you point out: The whole "soften modpost check" concept is bogus. It just cannot work. And I just verified it doing a m68k allmodconfig build without CONFIG_DEBUG_SECTION_MISMATCH. modpost will bail out unless host and target aligment matches:

scripts/mod/modpost -m -a -o /usr/local/src/build-tmp/linux/Module.symvers -S -c -s
drivers/media/video/gspca/gspca_sunplus: struct usb_device_id is 24 bytes. The last of 55 is:
0x04 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
FATAL: drivers/media/video/gspca/gspca_sunplus: struct usb_device_id is not terminated with a NULL entry! make[1]: *** [__modpost] Error 1
make: *** [modules] Error 2

So the prosed fix from Greg is certainly the correct one, until someone rewrites modpost to be completely cross build safe.

Bjørn
--
To unsubscribe from this list: send the line "unsubscribe linux-kbuild" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#7 lore raw
Quoted 2 lines · Bjørn Mork

So the prosed fix from Greg is certainly the correct one, until someone rewrites modpost to be completely cross build safe.

I started working on that. Then I saw do_input and barfed.

Andreas.

Signature · 3 lines
Andreas Schwab, [email protected]
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."
#8 lore raw

PATCH 1/1 mod/file2alias: make modalias generation safe for cross compiling

In reply to Bjørn Mork

Use the target compiler to compute the offsets of the fields of the device_id structures, so that it won't be broken by different alignments between the host and target ABIs.

This also fixes missing endian corrections for some modaliases.

---
This has been tested by cross-compiling a allmodconfig kernel for m68k and comparing the generated modaliases with the ones generated by a natively compiled modpost.

 include/linux/mod_devicetable.h   |  59 +---
 scripts/Makefile.modpost          |   3 +-
 scripts/mod/.gitignore            |   1 +
 scripts/mod/Makefile              |  35 ++
 scripts/mod/devicetable-offsets.c | 171 ++++++++++
 scripts/mod/file2alias.c          | 649 +++++++++++++++++++++-----------------
 scripts/mod/modpost.c             |   5 +-
 7 files changed, 571 insertions(+), 352 deletions(-)
 create mode 100644 scripts/mod/devicetable-offsets.c
include/linux/mod_devicetable.h +9 -50
index 501da4c..81c372b 100644
@@ -33,8 +33,7 @@ struct ieee1394_device_id {
 	__u32 model_id;
 	__u32 specifier_id;
 	__u32 version;
-	kernel_ulong_t driver_data
-		__attribute__((aligned(sizeof(kernel_ulong_t))));
+	kernel_ulong_t driver_data;
 };
 
 
@@ -135,11 +134,9 @@ struct usb_device_id {
 
 struct hid_device_id {
 	__u16 bus;
-	__u16 pad1;
 	__u32 vendor;
 	__u32 product;
-	kernel_ulong_t driver_data
-		__attribute__((aligned(sizeof(kernel_ulong_t))));
+	kernel_ulong_t driver_data;
 };
 
 /* s390 CCW devices */
@@ -163,8 +160,6 @@ struct ccw_device_id {
 struct ap_device_id {
 	__u16 match_flags;	/* which fields to match against */
 	__u8 dev_type;		/* device type */
-	__u8 pad1;
-	__u32 pad2;
 	kernel_ulong_t driver_info;
 };
 
@@ -174,13 +169,10 @@ struct ap_device_id {
 struct css_device_id {
 	__u8 match_flags;
 	__u8 type; /* subchannel type */
-	__u16 pad2;
-	__u32 pad3;
 	kernel_ulong_t driver_data;
 };
 
-#define ACPI_ID_LEN	16 /* only 9 bytes needed here, 16 bytes are used */
-			   /* to workaround crosscompile issues */
+#define ACPI_ID_LEN	9
 
 struct acpi_device_id {
 	__u8 id[ACPI_ID_LEN];
@@ -221,11 +213,7 @@ struct of_device_id
 	char	name[32];
 	char	type[32];
 	char	compatible[128];
-#ifdef __KERNEL__
 	void	*data;
-#else
-	kernel_ulong_t data;
-#endif
 };
 
 /* VIO */
@@ -250,24 +238,14 @@ struct pcmcia_device_id {
 	/* for pseudo multi-function devices */
 	__u8  		device_no;
 
-	__u32 		prod_id_hash[4]
-		__attribute__((aligned(sizeof(__u32))));
+	__u32 		prod_id_hash[4];
 
 	/* not matched against in kernelspace*/
-#ifdef __KERNEL__
 	const char *	prod_id[4];
-#else
-	kernel_ulong_t	prod_id[4]
-		__attribute__((aligned(sizeof(kernel_ulong_t))));
-#endif
 
 	/* not matched against */
 	kernel_ulong_t	driver_info;
-#ifdef __KERNEL__
 	char *		cisfile;
-#else
-	kernel_ulong_t	cisfile;
-#endif
 };
 
 #define PCMCIA_DEV_ID_MATCH_MANF_ID	0x0001
@@ -363,8 +341,7 @@ struct sdio_device_id {
 	__u8	class;			/* Standard interface or SDIO_ANY_ID */
 	__u16	vendor;			/* Vendor or SDIO_ANY_ID */
 	__u16	device;			/* Device ID or SDIO_ANY_ID */
-	kernel_ulong_t driver_data	/* Data private to the driver */
-		__attribute__((aligned(sizeof(kernel_ulong_t))));
+	kernel_ulong_t driver_data;	/* Data private to the driver */
 };
 
 /* SSB core, see drivers/ssb/ */
@@ -410,8 +387,7 @@ struct virtio_device_id {
  */
 struct hv_vmbus_device_id {
 	__u8 guid[16];
-	kernel_ulong_t driver_data	/* Data private to the driver */
-			__attribute__((aligned(sizeof(kernel_ulong_t))));
+	kernel_ulong_t driver_data;	/* Data private to the driver */
 };
 
 /* rpmsg */
@@ -430,8 +406,7 @@ struct rpmsg_device_id {
 
 struct i2c_device_id {
 	char name[I2C_NAME_SIZE];
-	kernel_ulong_t driver_data	/* Data private to the driver */
-			__attribute__((aligned(sizeof(kernel_ulong_t))));
+	kernel_ulong_t driver_data;	/* Data private to the driver */
 };
 
 /* spi */
@@ -441,8 +416,7 @@ struct i2c_device_id {
 
 struct spi_device_id {
 	char name[SPI_NAME_SIZE];
-	kernel_ulong_t driver_data	/* Data private to the driver */
-			__attribute__((aligned(sizeof(kernel_ulong_t))));
+	kernel_ulong_t driver_data;	/* Data private to the driver */
 };
 
 /* dmi */
@@ -474,15 +448,6 @@ struct dmi_strmatch {
 	char substr[79];
 };
 
-#ifndef __KERNEL__
-struct dmi_system_id {
-	kernel_ulong_t callback;
-	kernel_ulong_t ident;
-	struct dmi_strmatch matches[4];
-	kernel_ulong_t driver_data
-			__attribute__((aligned(sizeof(kernel_ulong_t))));
-};
-#else
 struct dmi_system_id {
 	int (*callback)(const struct dmi_system_id *);
 	const char *ident;
@@ -496,7 +461,6 @@ struct dmi_system_id {
  *	error: storage size of '__mod_dmi_device_table' isn't known
  */
 #define dmi_device_id dmi_system_id
-#endif
 
 #define DMI_MATCH(a, b)	{ a, b }
 
@@ -505,8 +469,7 @@ struct dmi_system_id {
 
 struct platform_device_id {
 	char name[PLATFORM_NAME_SIZE];
-	kernel_ulong_t driver_data
-			__attribute__((aligned(sizeof(kernel_ulong_t))));
+	kernel_ulong_t driver_data;
 };
 
 #define MDIO_MODULE_PREFIX	"mdio:"
@@ -562,11 +525,7 @@ struct isapnp_device_id {
 struct amba_id {
 	unsigned int		id;
 	unsigned int		mask;
-#ifndef __KERNEL__
-	kernel_ulong_t		data;
-#else
 	void			*data;
-#endif
 };
 
 /*
scripts/Makefile.modpost +1 -2
index 08dce14..ff22206 100644
@@ -80,8 +80,7 @@ modpost = scripts/mod/modpost                    \
  $(if $(KBUILD_EXTRA_SYMBOLS), $(patsubst %, -e %,$(KBUILD_EXTRA_SYMBOLS))) \
  $(if $(KBUILD_EXTMOD),-o $(modulesymfile))      \
  $(if $(CONFIG_DEBUG_SECTION_MISMATCH),,-S)      \
- $(if $(KBUILD_EXTMOD)$(KBUILD_MODPOST_WARN),-w) \
- $(if $(cross_build),-c)
+ $(if $(KBUILD_EXTMOD)$(KBUILD_MODPOST_WARN),-w)
 
 quiet_cmd_modpost = MODPOST $(words $(filter-out vmlinux FORCE, $^)) modules
       cmd_modpost = $(modpost) -s
scripts/mod/.gitignore +1 -0
index e9b7abe..33bae0d 100644
@@ -1,4 +1,5 @@
 elfconfig.h
 mk_elfconfig
 modpost
+devicetable-offsets.h
 
scripts/mod/Makefile +35 -0
index ff954f8..9415b56 100644
@@ -3,9 +3,44 @@ always		:= $(hostprogs-y) empty.o
 
 modpost-objs	:= modpost.o file2alias.o sumversion.o
 
+devicetable-offsets-file := devicetable-offsets.h
+
+define sed-y
+	"/^->/{s:->#\(.*\):/* \1 */:; \
+	s:^->\([^ ]*\) [\$$#]*\([-0-9]*\) \(.*\):#define \1 \2 /* \3 */:; \
+	s:^->\([^ ]*\) [\$$#]*\([^ ]*\) \(.*\):#define \1 \2 /* \3 */:; \
+	s:->::; p;}"
+endef
+
+quiet_cmd_offsets = GEN     $@
+define cmd_offsets
+	(set -e; \
+	 echo "#ifndef __DEVICEVTABLE_OFFSETS_H__"; \
+	 echo "#define __DEVICEVTABLE_OFFSETS_H__"; \
+	 echo "/*"; \
+	 echo " * DO NOT MODIFY."; \
+	 echo " *"; \
+	 echo " * This file was generated by Kbuild"; \
+	 echo " *"; \
+	 echo " */"; \
+	 echo ""; \
+	 sed -ne $(sed-y) $<; \
+	 echo ""; \
+	 echo "#endif" ) > $@
+endef
+
+# We use internal kbuild rules to avoid the "is up to date" message from make
+scripts/mod/devicetable-offsets.s: scripts/mod/devicetable-offsets.c FORCE
+	$(Q)mkdir -p $(dir $@)
+	$(call if_changed_dep,cc_s_c)
+
+$(obj)/$(devicetable-offsets-file): scripts/mod/devicetable-offsets.s
+	$(call cmd,offsets)
+
 # dependencies on generated files need to be listed explicitly
 
 $(obj)/modpost.o $(obj)/file2alias.o $(obj)/sumversion.o: $(obj)/elfconfig.h
+$(obj)/file2alias.o: $(obj)/$(devicetable-offsets-file)
 
 quiet_cmd_elfconfig = MKELF   $@
       cmd_elfconfig = $(obj)/mk_elfconfig < $< > $@
scripts/mod/devicetable-offsets.c new file +171 -0

171 lines not on this page · raw message

scripts/mod/file2alias.c +353 -296

1,107 lines not on this page · raw message

scripts/mod/modpost.c +1 -4
index c4e7d15..8e7b9fb 100644
@@ -2113,7 +2113,7 @@ int main(int argc, char **argv)
 	struct ext_sym_list *extsym_iter;
 	struct ext_sym_list *extsym_start = NULL;
 
-	while ((opt = getopt(argc, argv, "i:I:e:cmsSo:awM:K:")) != -1) {
+	while ((opt = getopt(argc, argv, "i:I:e:msSo:awM:K:")) != -1) {
 		switch (opt) {
 		case 'i':
 			kernel_read = optarg;
@@ -2122,9 +2122,6 @@ int main(int argc, char **argv)
 			module_read = optarg;
 			external_module = 1;
 			break;
-		case 'c':
-			cross_build = 1;
-			break;
 		case 'e':
 			external_module = 1;
 			extsym_iter =
Signature · 9 lines
1.7.11.1


Andreas.

-- 
Andreas Schwab, [email protected]
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."
#9 lore raw

Re: [PATCH] mod/file2alias: make modalias generation safe for cross compiling

Hi Andreas,

Quoted 3 lines · Andreas Schwab

Use the target compiler to compute the offsets of the fields of the device_id structures, so that it won't be broken by different alignments between the host and target ABIs.

Thanks, very nice!

include/linux/mod_devicetable.h struct ieee1394_device_id { · line 36

Quoted 9 lines
include/linux/mod_devicetable.h +1 -2
@@ -33,8 +33,7 @@ struct ieee1394_device_id {
 	__u32 model_id;
 	__u32 specifier_id;
 	__u32 version;
-	kernel_ulong_t driver_data
-		__attribute__((aligned(sizeof(kernel_ulong_t))));
+	kernel_ulong_t driver_data;

After this, we can also get rid of kernel_ulong_t, but that's a more intruisive patch all over the place.

Quoted 14 lines
scripts/mod/devicetable-offsets.c new file +11 -0
@@ -0,0 +1,171 @@
+#include <linux/kbuild.h>
+#include <linux/mod_devicetable.h>
+
+#define DEVID(devid) DEFINE(SIZE_##devid, sizeof(struct devid))
+#define DEVID_FIELD(devid, field) \
+	DEFINE(OFF_##devid##_##field, offsetof(struct devid, field))
+
+int main(void)
+{
+	DEVID(usb_device_id);
+	DEVID_FIELD(usb_device_id, match_flags);

scripts/mod/devicetable-offsets.c line 21

Quoted 10 lines
scripts/mod/devicetable-offsets.c new file +10 -0
+	DEVID_FIELD(usb_device_id, idVendor);
+	DEVID_FIELD(usb_device_id, idProduct);
+	DEVID_FIELD(usb_device_id, bcdDevice_lo);
+	DEVID_FIELD(usb_device_id, bcdDevice_hi);
+	DEVID_FIELD(usb_device_id, bDeviceClass);
+	DEVID_FIELD(usb_device_id, bDeviceSubClass);
+	DEVID_FIELD(usb_device_id, bDeviceProtocol);
+	DEVID_FIELD(usb_device_id, bInterfaceClass);
+	DEVID_FIELD(usb_device_id, bInterfaceSubClass);
+	DEVID_FIELD(usb_device_id, bInterfaceProtocol);

These are all duplicates of the structures in <linux/mod_devicetable.h>. Can't you get rid of the duplication by putting e.g. the following in <linux/mod_devicetable.h>:

    DEVID_START(usb_device_id);
    DEVID_FIELD(usb_device_id, __u16, match_flags);
    DEVID_FIELD(usb_device_id, __u16, idVendor);
    DEVID_FIELD(usb_device_id, __u16, idProduct);
    DEVID_FIELD(usb_device_id, __u16, bcdDevice_lo);
    DEVID_FIELD(usb_device_id, __u16, bcdDevice_hi);
    DEVID_FIELD(usb_device_id, __u8, bDeviceClass);
    DEVID_FIELD(usb_device_id, __u8, bDeviceSubClass);
    DEVID_FIELD(usb_device_id, __u8, bDeviceProtocol);
    DEVID_FIELD(usb_device_id, __u8, bInterfaceClass);
    DEVID_FIELD(usb_device_id, __u8, bInterfaceSubClass);
    DEVID_FIELD(usb_device_id, __u8, bInterfaceProtocol);
    DEVID_END();

and redefining the DEVID_*() macros depending on
1. __KERNEL__, to generate the C struct definitions for kernel builds,
2. first inclusion by devicetable-offsets.c, also to generate C struct definitions,
3. second inclusion by devicetable-offsets.c, to generate the sizes and offset definitions for modalias?

Quoted 11 lines
scripts/mod/file2alias.c +0 -1
@@ -116,7 +129,6 @@ static inline void add_wildcard(char *str)
 		strcat(str + len, "*");
 }
 
-unsigned int cross_build = 0;
 /**
  * Check that sizeof(device_id type) are consistent with size of section
  * in .o file. If in-consistent then userspace and kernel does not agree

scripts/mod/file2alias.c static void device_id_check(const char *modname, const char *device_id, · line 148

Quoted 8 lines
scripts/mod/file2alias.c +0 -2
@@ -131,8 +143,6 @@ static void device_id_check(const char *modname, const char *device_id,
 	int i;
 
 	if (size % id_size || size < id_size) {
-		if (cross_build != 0)
-			return;
 		fatal("%s: sizeof(struct %s_device_id)=%lu is not a modulo "
 		      "of the size of section __mod_%s_device_table=%lu.\n"
 		      "Fix definition of struct %s_device_id "

Funny, I didn't know there was a check for cross-compiling, supposedly to ignore the warnings.
And it didn't work, as the problem was noticed originally on the linux-next build service (which uses cross-compiling), and also showed up for my own cross-builds.

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds
--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#10 lore raw

Re: [PATCH] mod/file2alias: make modalias generation safe for cross compiling

Quoted 46 lines · Geert Uytterhoeven

Hi Andreas,

Quoted 14 lines · Andreas Schwab
scripts/mod/devicetable-offsets.c new file +11 -0
@@ -0,0 +1,171 @@
+#include <linux/kbuild.h>
+#include <linux/mod_devicetable.h>
+
+#define DEVID(devid) DEFINE(SIZE_##devid, sizeof(struct devid))
+#define DEVID_FIELD(devid, field) \
+	DEFINE(OFF_##devid##_##field, offsetof(struct devid, field))
+
+int main(void)
+{
+	DEVID(usb_device_id);
+	DEVID_FIELD(usb_device_id, match_flags);

scripts/mod/devicetable-offsets.c line 21

Quoted 10 lines
scripts/mod/devicetable-offsets.c new file +10 -0
+	DEVID_FIELD(usb_device_id, idVendor);
+	DEVID_FIELD(usb_device_id, idProduct);
+	DEVID_FIELD(usb_device_id, bcdDevice_lo);
+	DEVID_FIELD(usb_device_id, bcdDevice_hi);
+	DEVID_FIELD(usb_device_id, bDeviceClass);
+	DEVID_FIELD(usb_device_id, bDeviceSubClass);
+	DEVID_FIELD(usb_device_id, bDeviceProtocol);
+	DEVID_FIELD(usb_device_id, bInterfaceClass);
+	DEVID_FIELD(usb_device_id, bInterfaceSubClass);
+	DEVID_FIELD(usb_device_id, bInterfaceProtocol);

These are all duplicates of the structures in <linux/mod_devicetable.h>. Can't you get rid of the duplication by putting e.g. the following in <linux/mod_devicetable.h>:

    DEVID_START(usb_device_id);
    DEVID_FIELD(usb_device_id, __u16, match_flags);
    DEVID_FIELD(usb_device_id, __u16, idVendor);
    DEVID_FIELD(usb_device_id, __u16, idProduct);
    DEVID_FIELD(usb_device_id, __u16, bcdDevice_lo);
    DEVID_FIELD(usb_device_id, __u16, bcdDevice_hi);
    DEVID_FIELD(usb_device_id, __u8, bDeviceClass);
    DEVID_FIELD(usb_device_id, __u8, bDeviceSubClass);
    DEVID_FIELD(usb_device_id, __u8, bDeviceProtocol);
    DEVID_FIELD(usb_device_id, __u8, bInterfaceClass);
    DEVID_FIELD(usb_device_id, __u8, bInterfaceSubClass);
    DEVID_FIELD(usb_device_id, __u8, bInterfaceProtocol);
    DEVID_END();
Quoted 6 lines

and redefining the DEVID_*() macros depending on
1. __KERNEL__, to generate the C struct definitions for kernel builds,
2. first inclusion by devicetable-offsets.c, also to generate C struct definitions,
3. second inclusion by devicetable-offsets.c, to generate the sizes and offset definitions for modalias?

That requires some reorganisation of the struct pnp_card_device_id which has a somewhat complicated structure (which also accounts for the ugliest part of the patch).

Andreas.

Signature · 7 lines
Andreas Schwab, [email protected]
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."
--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#11 lore raw

Re: [PATCH] mod/file2alias: make modalias generation safe for cross compiling

In reply to Andreas Schwab

Hi Andreas.

Quoted 3 lines · Andreas Schwab

Use the target compiler to compute the offsets of the fields of the device_id structures, so that it won't be broken by different alignments between the host and target ABIs.

Looks good.

scripts/Makefile.modpost modpost = scripts/mod/modpost \ · line 83

Quoted 11 lines
scripts/Makefile.modpost +1 -2
index 08dce14..ff22206 100644
@@ -80,8 +80,7 @@ modpost = scripts/mod/modpost                    \
  $(if $(KBUILD_EXTRA_SYMBOLS), $(patsubst %, -e %,$(KBUILD_EXTRA_SYMBOLS))) \
  $(if $(KBUILD_EXTMOD),-o $(modulesymfile))      \
  $(if $(CONFIG_DEBUG_SECTION_MISMATCH),,-S)      \
- $(if $(KBUILD_EXTMOD)$(KBUILD_MODPOST_WARN),-w) \
- $(if $(cross_build),-c)
+ $(if $(KBUILD_EXTMOD)$(KBUILD_MODPOST_WARN),-w)

With this change we can kill cross_build, and thus we can drop KBUILD_BUILDHOST too (from top-level Makefile).

Sam

#12 lore raw

PATCH v2 1/1 mod/file2alias: make modalias generation safe for cross compiling

Use the target compiler to compute the offsets of the fields of the device_id structures, so that it won't be broken by different alignments between the host and target ABIs.

This also fixes missing endian corrections for some modaliases.

---
v2:
- Remove cross-build and KBUILD_BUILDHOST.
- Rebase on v3.5-rc4

 Makefile                          |   1 -
 include/linux/mod_devicetable.h   |  58 +---
 scripts/Makefile.modpost          |   7 +-
 scripts/mod/.gitignore            |   1 +
 scripts/mod/Makefile              |  35 ++
 scripts/mod/devicetable-offsets.c | 172 ++++++++++
 scripts/mod/file2alias.c          | 653 +++++++++++++++++++++-----------------
 scripts/mod/modpost.c             |   5 +-
 8 files changed, 574 insertions(+), 358 deletions(-)
 create mode 100644 scripts/mod/devicetable-offsets.c
Makefile +0 -1
index 3fdfde2..f345298 100644
@@ -191,7 +191,6 @@ SUBARCH := $(shell uname -m | sed -e s/i.86/i386/ -e s/sun4u/sparc64/ \
 # "make" in the configured kernel build directory always uses that.
 # Default value for CROSS_COMPILE is not to prefix executables
 # Note: Some architectures assign CROSS_COMPILE in their arch/*/Makefile
-export KBUILD_BUILDHOST := $(SUBARCH)
 ARCH		?= $(SUBARCH)
 CROSS_COMPILE	?= $(CONFIG_CROSS_COMPILE:"%"=%)
 
include/linux/mod_devicetable.h +9 -49

154 lines not on this page · raw message

scripts/Makefile.modpost +1 -6
index 08dce14..f26f63d 100644
@@ -66,10 +66,6 @@ modules   := $(patsubst %.o,%.ko, $(wildcard $(__modules:.ko=.o)))
 # Stop after building .o files if NOFINAL is set. Makes compile tests quicker
 _modpost: $(if $(KBUILD_MODPOST_NOFINAL), $(modules:.ko:.o),$(modules))
 
-ifneq ($(KBUILD_BUILDHOST),$(ARCH))
-        cross_build := 1
-endif
-
 # Step 2), invoke modpost
 #  Includes step 3,4
 modpost = scripts/mod/modpost                    \
@@ -80,8 +76,7 @@ modpost = scripts/mod/modpost                    \
  $(if $(KBUILD_EXTRA_SYMBOLS), $(patsubst %, -e %,$(KBUILD_EXTRA_SYMBOLS))) \
  $(if $(KBUILD_EXTMOD),-o $(modulesymfile))      \
  $(if $(CONFIG_DEBUG_SECTION_MISMATCH),,-S)      \
- $(if $(KBUILD_EXTMOD)$(KBUILD_MODPOST_WARN),-w) \
- $(if $(cross_build),-c)
+ $(if $(KBUILD_EXTMOD)$(KBUILD_MODPOST_WARN),-w)
 
 quiet_cmd_modpost = MODPOST $(words $(filter-out vmlinux FORCE, $^)) modules
       cmd_modpost = $(modpost) -s
scripts/mod/.gitignore +1 -0
index e9b7abe..33bae0d 100644
@@ -1,4 +1,5 @@
 elfconfig.h
 mk_elfconfig
 modpost
+devicetable-offsets.h
 
scripts/mod/Makefile +35 -0
index ff954f8..9415b56 100644
@@ -3,9 +3,44 @@ always		:= $(hostprogs-y) empty.o
 
 modpost-objs	:= modpost.o file2alias.o sumversion.o
 
+devicetable-offsets-file := devicetable-offsets.h
+
+define sed-y
+	"/^->/{s:->#\(.*\):/* \1 */:; \
+	s:^->\([^ ]*\) [\$$#]*\([-0-9]*\) \(.*\):#define \1 \2 /* \3 */:; \
+	s:^->\([^ ]*\) [\$$#]*\([^ ]*\) \(.*\):#define \1 \2 /* \3 */:; \
+	s:->::; p;}"
+endef
+
+quiet_cmd_offsets = GEN     $@
+define cmd_offsets
+	(set -e; \
+	 echo "#ifndef __DEVICEVTABLE_OFFSETS_H__"; \
+	 echo "#define __DEVICEVTABLE_OFFSETS_H__"; \
+	 echo "/*"; \
+	 echo " * DO NOT MODIFY."; \
+	 echo " *"; \
+	 echo " * This file was generated by Kbuild"; \
+	 echo " *"; \
+	 echo " */"; \
+	 echo ""; \
+	 sed -ne $(sed-y) $<; \
+	 echo ""; \
+	 echo "#endif" ) > $@
+endef
+
+# We use internal kbuild rules to avoid the "is up to date" message from make
+scripts/mod/devicetable-offsets.s: scripts/mod/devicetable-offsets.c FORCE
+	$(Q)mkdir -p $(dir $@)
+	$(call if_changed_dep,cc_s_c)
+
+$(obj)/$(devicetable-offsets-file): scripts/mod/devicetable-offsets.s
+	$(call cmd,offsets)
+
 # dependencies on generated files need to be listed explicitly
 
 $(obj)/modpost.o $(obj)/file2alias.o $(obj)/sumversion.o: $(obj)/elfconfig.h
+$(obj)/file2alias.o: $(obj)/$(devicetable-offsets-file)
 
 quiet_cmd_elfconfig = MKELF   $@
       cmd_elfconfig = $(obj)/mk_elfconfig < $< > $@
scripts/mod/devicetable-offsets.c new file +172 -0

172 lines not on this page · raw message

scripts/mod/file2alias.c +355 -298

1,112 lines not on this page · raw message

scripts/mod/modpost.c +1 -4
index 0f84bb3..21d4b89 100644
@@ -2116,7 +2116,7 @@ int main(int argc, char **argv)
 	struct ext_sym_list *extsym_iter;
 	struct ext_sym_list *extsym_start = NULL;
 
-	while ((opt = getopt(argc, argv, "i:I:e:cmsSo:awM:K:")) != -1) {
+	while ((opt = getopt(argc, argv, "i:I:e:msSo:awM:K:")) != -1) {
 		switch (opt) {
 		case 'i':
 			kernel_read = optarg;
@@ -2125,9 +2125,6 @@ int main(int argc, char **argv)
 			module_read = optarg;
 			external_module = 1;
 			break;
-		case 'c':
-			cross_build = 1;
-			break;
 		case 'e':
 			external_module = 1;
 			extsym_iter =
Signature · 7 lines
1.7.11.1


-- 
Andreas Schwab, [email protected]
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."
#13 lore raw
In reply to Bjørn Mork
Quoted 5 lines · Bjørn Mork

AFAIK (which admittedly is not much wrt cross building) there is no way we can make the host built file2alias know the proper aligment for the structure in the target built modules. That's the background for this fix:

autoconf has a lot of nice macros (using the cross-compiler) to solve that sort of problems.

Philippe

#14 lore raw
In reply to Bjørn Mork
Quoted 11 lines · Bjørn Mork
Quoted 7 lines · Greg Kroah-Hartman
Quoted 3 lines · Geert Uytterhoeven

As "kernel_ulong_t  driver_info" is no longer naturally aligned, the compiler will
add implicit padding. But the padding depends on the architecture.

Ah, so we were "lucky" before, nice.

I don't really believe in luck :-) I think someone has been really smart here. Maybe too smart...

No, I think the previous structure was just "lucky" in that it just happened to be the right alignment. I say this as I think I was the one who created that structure years ago. Or maybe not, this was back in the 2.3 kernel days, I can't remember what patches I wrote last week...

Quoted 33 lines
Quoted 33 lines
Quoted 11 lines

It can be fixed by adding explicit padding. Probably it should be padded by 7 bytes (not 3), as kernel_ulong_t may require 8-byte alignment on some 64-bit platforms. Or by an explicit alignment attribute.

See also
* commit 8175fe2dda1c93a9c596921c8ed4a0b4baccdefe ("HID: fix
hid_device_id for cross compiling")
* commit 7492d4a416d68ab4bd254b36ffcc4e0138daa8ff ("sdio: fix module
device table definition for m68k")
* commit 9e2d3cd34a159948dc753a14573e16bffc04dba8 ("[PATCH]
mod_devicetable.h fixes")

So would the patch below fix this? It should force alignment of the driver_data field, which is all you want here, right?

Quoted 3 lines

Still, there's a bug in file2alias (which is compiled by the host compiler), in that
it may use different padding than the target platform when cross-compiling.

That's not good, but outside of this specific issue, right? Have we just been fortunate it hasn't really hit us yet?

thanks,

greg k-h

include/linux/mod_devicetable.h +0 -0
index 7771d45..6955045 100644
@@ -122,7 +122,8 @@ struct usb_device_id {
 	__u8		bInterfaceNumber;
 
Quoted 11 lines

include/linux/mod_devicetable.h struct usb_device_id { · line 127

Quoted 5 lines
include/linux/mod_devicetable.h +2 -1
 	/* not matched against */
-	kernel_ulong_t	driver_info;
+	kernel_ulong_t	driver_info
+		__attribute__((aligned(sizeof(kernel_ulong_t))));
 };

This feels a lot like papering over the real problem. It will solve this instance, but the list of such previous "paper work" that Geert provided should be enough evidence that this will happen again the next time someone modifies a device id struct for some subsystem.

Hopefully not, if you add another field here, the alignment force will keep things lined up properly, from what I can tell. Is that not true?

Quoted 4 lines

And adding forced aligment here feels wrong since there is no good reason why the (target) compiler shouldn't know the proper alignment for this structure, is there? OK, "feels wrong" is not a good argument. But it would be better to solve this problem once and for all.

C doesn't require the structure to be aligned. Actually the spec says it doesn't guarantee anything about this, we just "know" that gcc is going to be semi-sane and try to do the best it can. Hopefully clang is also semi-sane as well.

So because of that, we have to give it some guidance, hence the patch.

thanks,

greg k-h

#15 lore raw
Quoted 63 lines · Greg Kroah-Hartman
Quoted 11 lines · Bjørn Mork
Quoted 7 lines · Greg Kroah-Hartman
Quoted 3 lines · Geert Uytterhoeven

As "kernel_ulong_t  driver_info" is no longer naturally aligned, the compiler will
add implicit padding. But the padding depends on the architecture.

Ah, so we were "lucky" before, nice.

I don't really believe in luck :-)  I think someone has been really smart here.  Maybe too smart...

No, I think the previous structure was just "lucky" in that it just happened to be the right alignment.  I say this as I think I was the one who created that structure years ago.  Or maybe not, this was back in the 2.3 kernel days, I can't remember what patches I wrote last week...

Quoted 33 lines
Quoted 33 lines
Quoted 11 lines

It can be fixed by adding explicit padding. Probably it should be padded by 7 bytes (not 3), as kernel_ulong_t may require 8-byte alignment on some 64-bit platforms. Or by an explicit alignment attribute.

See also
  * commit 8175fe2dda1c93a9c596921c8ed4a0b4baccdefe ("HID: fix hid_device_id for cross compiling")
  * commit 7492d4a416d68ab4bd254b36ffcc4e0138daa8ff ("sdio: fix module device table definition for m68k")
  * commit 9e2d3cd34a159948dc753a14573e16bffc04dba8 ("[PATCH] mod_devicetable.h fixes")

So would the patch below fix this?  It should force alignment of the driver_data field, which is all you want here, right?

Quoted 3 lines

Still, there's a bug in file2alias (which is compiled by the host compiler), in that
it may use different padding than the target platform when cross-compiling.

That's not good, but outside of this specific issue, right?  Have we just been fortunate it hasn't really hit us yet?

thanks,

greg k-h

include/linux/mod_devicetable.h +0 -0
index 7771d45..6955045 100644
@@ -122,7 +122,8 @@ struct usb_device_id {
     __u8            bInterfaceNumber;
 
Quoted 11 lines

include/linux/mod_devicetable.h struct usb_device_id { · line 127

Quoted 5 lines
include/linux/mod_devicetable.h +2 -1
     /* not matched against */
-   kernel_ulong_t  driver_info;
+   kernel_ulong_t  driver_info
+           __attribute__((aligned(sizeof(kernel_ulong_t))));
  };

This feels a lot like papering over the real problem.  It will solve this instance, but the list of such previous "paper work" that Geert provided should be enough evidence that this will happen again the next time someone modifies a device id struct for some subsystem.

Quoted 2 lines

Hopefully not, if you add another field here, the alignment force will keep things lined up properly, from what I can tell.  Is that not true?

... for struct usb_device_id.
But not for all other existing and future device ID types.

We have kernel_ulong_t to tell the host compiler what the target's unsigned long type (actually only its size, not alignment) is.

scripts/mod/file2alias.c handles this with:

#if KERNEL_ELFCLASS == ELFCLASS32
typedef Elf32_Addr kernel_ulong_t;
#define BITS_PER_LONG 32
#else
typedef Elf64_Addr kernel_ulong_t;
#define BITS_PER_LONG 64
#endif

To fix the misalignment issue, can't we add "__attribute__((aligned(2)))" to the typedef when cross-compiling for m68k?

Still, that only solves the problem for "kernel_ulong_t". Not for all other 4 or 8 bytes types (in practice just "u32") that are used in include/linux/mod_devicetable.h. So we would also need "kernel_u32". And "kernel_u16" for consistency.

Gr{oetje,eeting}s,

                        Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds
--
To unsubscribe from this list: send the line "unsubscribe linux-kbuild" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

author Acked-by
#16 lore raw
In reply to Greg Kroah-Hartman

Hi Greg,

Quoted 56 lines · Greg Kroah-Hartman
Quoted 35 lines · Geert Uytterhoeven
Quoted 33 lines · Greg Kroah-Hartman
Quoted 23 lines · Geert Uytterhoeven

commit 81df2d594340dcb6d1a02191976be88a1ca8120c ("USB: allow match on bInterfaceNumber") added a byte to the interior of struct usb_device_id, enabling implicit padding:

include/linux/mod_devicetable.h +3 -0
@@ -115,6 +118,9 @@ struct usb_device_id {
         __u8            bInterfaceSubClass;
         __u8            bInterfaceProtocol;
 
+       /* Used for vendor-specific interface matches */
+       __u8            bInterfaceNumber;
+
         /* not matched against */
         kernel_ulong_t  driver_info;
  };

On m68k, this causes failures like:

| FATAL: drivers/gpu/drm/udl/udl: sizeof(struct usb_device_id)=24 is not a modulo of the size of section __mod_usb_device_table=44. | Fix definition of struct usb_device_id in mod_devicetable.h

Quoted 5 lines

M68k is special in that it uses 2 for the alignment of 32-bit entities, hence sizeof(struct usb_device_id) = 22.

However, when cross-compiling on amd64, sizeof(struct usb_device_id) = 24 in scripts/mod/file2alias.c.

I don't really understand, what is the problem here?  The fact that we added one byte to the structure?  And now something breaks?  How?

Quoted 5 lines
Quoted 1 line

What can we do to resolve it?

As "kernel_ulong_t  driver_info" is no longer naturally aligned, the compiler will
add implicit padding. But the padding depends on the architecture.

Ah, so we were "lucky" before, nice.

Quoted 11 lines

It can be fixed by adding explicit padding. Probably it should be padded by 7 bytes (not 3), as kernel_ulong_t may require 8-byte alignment on some 64-bit platforms. Or by an explicit alignment attribute.

See also
  * commit 8175fe2dda1c93a9c596921c8ed4a0b4baccdefe ("HID: fix hid_device_id for cross compiling")
  * commit 7492d4a416d68ab4bd254b36ffcc4e0138daa8ff ("sdio: fix module device table definition for m68k")
  * commit 9e2d3cd34a159948dc753a14573e16bffc04dba8 ("[PATCH] mod_devicetable.h fixes")

Quoted 2 lines

So would the patch below fix this?  It should force alignment of the driver_data field, which is all you want here, right?

Yes, it's fixes it, so

Quoted 16 lines
Quoted 3 lines

Still, there's a bug in file2alias (which is compiled by the host compiler), in that
it may use different padding than the target platform when cross-compiling.

That's not good, but outside of this specific issue, right?  Have we just been fortunate it hasn't really hit us yet?

thanks,

greg k-h

include/linux/mod_devicetable.h +0 -0
index 7771d45..6955045 100644

include/linux/mod_devicetable.h struct usb_device_id { · line 129

Quoted 9 lines
include/linux/mod_devicetable.h +2 -1
@@ -122,7 +122,8 @@ struct usb_device_id {
        __u8            bInterfaceNumber;
 
        /* not matched against */
-       kernel_ulong_t  driver_info;
+       kernel_ulong_t  driver_info
+               __attribute__((aligned(sizeof(kernel_ulong_t))));
  };
 
  /* Some useful macros to use to create struct usb_device_id */

Gr{oetje,eeting}s,

                        Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds