kernelnewbies.kernelnewbies.org archive mirror
 help / color / mirror / Atom feed
From: jim.cromie@gmail.com (Jim Cromie)
To: kernelnewbies@lists.kernelnewbies.org
Subject: __initdata access at runtime
Date: Mon,  9 Apr 2012 23:09:43 -0600	[thread overview]
Message-ID: <1334034585-16651-1-git-send-email-jim.cromie@gmail.com> (raw)

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 ?

             reply	other threads:[~2012-04-10  5:09 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-04-10  5:09 Jim Cromie [this message]
2012-04-10  5:09 ` [PATCH 1/3] bug.h: add BUILD_BUG_DECL, usable at file scope Jim Cromie
2012-04-10  5:09 ` [PATCH 2/3] bug.h: add test/demo module Jim Cromie

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1334034585-16651-1-git-send-email-jim.cromie@gmail.com \
    --to=jim.cromie@gmail.com \
    --cc=kernelnewbies@lists.kernelnewbies.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).