The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: aijazbaig1@gmail.com
Cc: brgerst@gmail.com, jengelh@medozas.de,
	netfilter-devel@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: Fwd: help needed with EXPORT_SYMBOL
Date: Wed, 25 Aug 2010 12:23:35 +0200	[thread overview]
Message-ID: <1282731815.2605.3473.camel@laptop> (raw)
In-Reply-To: <1282728478.17044.22.camel@aijazbaig1-desktop>

On Wed, 2010-08-25 at 14:57 +0530, Aijaz Baig wrote:

> I am trying to understand if its possible to add functions dynamically
> to the kernel source based on the presence of certain modules in the
> running kernel image.
> 
> I did try what brian suggested with the function pointer and yeah it
> does work. But I could not understand what peter was trying to say about
> modular users since I suppose he mentioned one module (B in this case)
> using a function pointer defined in (or by) module A. In my case, since
> it is the kernel that is gonna use the function, I need to make sure
> that the module doesn't get unloaded while we are using the function.

Right, so there's two problems there:

 - the exposed function pointer
 - unload serialization

The problem with the exposed function pointer is that two competing
modules can set the function pointer:

extern void (*ptr)(void);

static void (*old_ptr)(void);

void func(void)
{
  /* do something */
}

module_init()
{
  old_ptr = ptr;
  ptr = func;
}

module_exit()
{
  ptr = old_ptr;
  synchronize_stuff();
}

Now suppose two modules A and B both have that template, then load
module A, at that time ptr == A::func, A::old_ptr = NULL, right?

Then load B, then ptr == B:func, B::old_ptr = A::func.

then unload A, then ptr == NULL; /* hey where's B gone? */

Suppose module_exit() had read:

module_exit()
{
  if (ptr == func)
    ptr = old_ptr;
}

Then after A got unloaded, you'd have: ptr = B::func, B::old_ptr ==
A:func, and the world goes *bang* when you unload B.

If you'd have exposed the thing like:

static void (*callback)(void);

int set_foo_callback(void (*func)(void))
{
  if (callback && func)
    return -EBUSY;

  callback = func;
  synchronize_world();
  return 0;
}
EXPORT_SYMBOL_GPL(set_foo_callback);

Then there'd be no confusion, as loading B while A was already loaded
would fail since set_foo_callback() would fail with -EBUSY.


Now the synchronization issue, above represented by synchronize_world();
that will have to do something that ensures all current users of the ptr
have gone, taking a reference on the module will likely result in an
pinned module, since what will drop the ref again?

Module unload does a fairly heavy stop-machine thing, which might be
sufficient for some, but a sensible solution really depends on the
problem domain, RCU could be used in various ways.

  parent reply	other threads:[~2010-08-25 10:23 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-08-21  6:57 help needed with EXPORT_SYMBOL Aijaz Baig
2010-08-21  9:23 ` Jan Engelhardt
2010-08-23  5:14 ` Aijaz Baig
2010-08-23 11:48   ` Brian Gerst
2010-08-23 13:17     ` Peter Zijlstra
2010-08-23 13:32       ` Jan Engelhardt
2010-08-23 13:43         ` Peter Zijlstra
2010-08-23 13:44       ` Brian Gerst
2010-08-23 14:05         ` Peter Zijlstra
     [not found]           ` <AANLkTimQDV74kcM_84QySMKmdf-XxFOhqp48cQdQNN4s@mail.gmail.com>
     [not found]             ` <AANLkTinRyoHcSmmHv9tZ36X4wxVVCKGLDwGiZUTPPn+z@mail.gmail.com>
2010-08-24  4:44               ` Aijaz Baig
     [not found]                 ` <AANLkTi=cj+qCFxeT5hq0-x-XqSUCbcqQRAwmQ9h+cOxy@mail.gmail.com>
     [not found]                   ` <AANLkTim9nvJR0K6xJc2kHGCeoMzFLzSxR8Avx59jsEbf@mail.gmail.com>
2010-08-25  9:27                     ` Fwd: " Aijaz Baig
2010-08-25 10:06                       ` Jan Engelhardt
2010-08-25 10:23                       ` Peter Zijlstra [this message]
2010-08-25 15:37                       ` Randy Dunlap

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=1282731815.2605.3473.camel@laptop \
    --to=peterz@infradead.org \
    --cc=aijazbaig1@gmail.com \
    --cc=brgerst@gmail.com \
    --cc=jengelh@medozas.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netfilter-devel@vger.kernel.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