__initdata access at runtime

Jim Cromie jim.cromie at gmail.com
Tue Apr 10 01:09:43 EDT 2012


hi folks,

Ive written a pair of patches which have an issue with __initdata

[PATCH 1/3] bug.h: add BUILD_BUG_DECL, usable at file scope
[PATCH 2/3] bug.h: add test/demo module

1st one declares a BUILD_BUG_DECL(name, condition) which breaks
compile if the condition is true, much like the other *BUG* macros.
Its usable at file scope, which lets it verify that 2 arrays are the
same size:

int arr1[] = {1,2,3};
int arr2[] = {1,2,4};
BUILD_BUG_DECL(arr1_VS_arr2, ARRAY_SIZE(arr1) != ARRAY_SIZE( arr2));

heres the macro:

+#define BUILD_BUG_DECL(name, cond)				\
+	static __initdata struct {				\
+		int BUILD_BUG_DECL_ ## name[1 - 2*!!(cond)];	\
+	} BUILD_BUG_DECL_ ##name[0] __attribute__((unused))
+

which expands the above like:

static __attribute__ ((__section__(".init.data"))) struct {
       int BUILD_BUG_DECL_arr1_VS_arr1[1 - 2*!!(sizeof(arr1) != sizeof(arr2))];
} BUILD_BUG_DECL_arr1_VS_arr1[0]  __attribute__((unused));

It works as pretty much as intended, but there are a few wrinkes, as
exposed by the run-time part of the test module (patch 2).

The macro attempts to create no storage (by sizing the array as [0]),
and put it into the .init.data section of the object code.

The test module does 3 things:

1 - proves the macro works. Remove the // on commented BUILD_BUG_DECL
    statements to try for yourself.

2 - references the variable defined by the macro, during module_init.
    Printing that value works, its always 0, why ?

    I thought that the __attribute__((unused)) would have prevented or
    warned about this access.  I also tried __attribute__((nodref,
    unused)), but compiler warned about it, so I pulled that out.

3 - references the variable defined by the macro, during runtime.
    ie: $> cat /sys/module/build-asserts/cbint

    That gets a paging error.  This makes sense, as the .init.data has
    been freed after module_init is done.

Why doesnt the compiler warn or error about referencing an __initdata
var from a non-__init function ?





More information about the Kernelnewbies mailing list