kernelnewbies.kernelnewbies.org archive mirror
 help / color / mirror / Atom feed
From: bernd@petrovitsch.priv.at (Bernd Petrovitsch)
To: kernelnewbies@lists.kernelnewbies.org
Subject: Why exported const value modified by another driver not updated in original driver
Date: Tue, 04 Sep 2012 14:06:31 +0200	[thread overview]
Message-ID: <1346760392.5081.19.camel@thorin> (raw)
In-Reply-To: <CAJKgH8Ah-aq=LfOWs2SBZnndzugNGxJbqYS11YVQLXJy6FZxkw@mail.gmail.com>

Hi!

On Die, 2012-09-04 at 15:00 +0530, Manavendra Nath Manav wrote:
[....]
> I have declared a static const int variable in one driver and exported
> that variable symbol.

Exporting a "static" variable makes conceptually no sense whatsoever -
either you want it exported (so you should remove the "static") or you
want it not exported - which is precisely the definition of "static".

> retains the original value. When both virtual and physical addresses
> of the variable as seen by both drivers are same, how is this even
> possible. Is it a kernel bug?

It's a bug in your drivers.
The C compiler sees an initialized "static const" variable. So the C
compiler knows that it is not accessible from other compilation units
(because it is "static") and the variable isn't changing within the
current compilation unit (because the variable as such is "const" and
their is no non-const pointer to it or similar).
Since this is the only compilation unit, it knows that no one will/can
change it's value.
So the C compiler happily replaces all references to the variable with
it's value - just as if you would have #define'd that value.

Obviously, later on it is exported and the variable actually exists and
you can change it in the other file. But since the value has been
replaced literally by "123" at compile (more precise: optimizer time),
you don't see a change in the printk()s.

> Interestingly, when i change the declaration of variable to "volatile"
> in driver.c as "static const volatile int" then the driver.ko prints
> modified value "987" during rmmod and the original value "123" is
> lost. What is the difference between this and previous declaration?

"volatile" tells the C compiler that a variable may change it's value
without explicit code.
A typical use case are hardware registers which are changed by the
underlying hardware.
In user space, variables may be modified by signal handlers (which can
occur at any time in a program - unless specifcally blocked).

So the optimizer must not accesses to "volatile" variables because they
may change their value at any time.

> Please forgive if you find this question silly?

That all is actually pure (K&R-)C and has nothing to do with the kernel.


> static const int value = 123;
[...]
> EXPORT_SYMBOL(value);

I wonder if we can modify EXPORT_SYMBOL() so that it compile-time-fails
for "static" variables.
And if we actually want that.

	Bernd
-- 
Bernd Petrovitsch                  Email : bernd at petrovitsch.priv.at
                     LUGA : http://www.luga.at

      parent reply	other threads:[~2012-09-04 12:06 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-09-04  9:30 Why exported const value modified by another driver not updated in original driver Manavendra Nath Manav
2012-09-04 10:28 ` Manavendra Nath Manav
     [not found]   ` <20120904103905.GA21953@jak-linux.org>
2012-09-04 10:59     ` Manavendra Nath Manav
     [not found]   ` <20120904122544.GC19396@mwanda>
2012-09-05  8:27     ` Manavendra Nath Manav
2012-09-04 10:46 ` Alan Cox
2012-09-04 12:06 ` Bernd Petrovitsch [this message]

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=1346760392.5081.19.camel@thorin \
    --to=bernd@petrovitsch.priv.at \
    --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).