* Incrementing module reference count
@ 2012-12-08 1:14 Aaron Williams
2012-12-08 11:44 ` Borislav Petkov
0 siblings, 1 reply; 4+ messages in thread
From: Aaron Williams @ 2012-12-08 1:14 UTC (permalink / raw)
To: linux-kernel; +Cc: Williams, Aaron
Hi,
I have a kernel module which other modules register with in order to
export access functions. So far I have everything working but I want to
prevent a module that is registered with my module from unloading since
now my module is dependent on the other module.
Is there a way I can cause the reference count of the module registering
with my module to increase? I tried calling get_device with the device
structure of the module that is registering but that does not seem to work.
For example, I have the following function:
/**
* Adds a mapping of a device node to a memory accessor
*
* @param[in] dev - device
* @param[in] macc - memory accessor
*
* @returns 0 for success or -ENOMEM
*/
int of_memory_accessor_register(struct device *dev,
struct memory_accessor *macc)
{
struct of_macc_entry *mentry;
mentry = kmalloc(sizeof(*mentry), GFP_KERNEL);
if (mentry == NULL)
return -ENOMEM;
mentry->dev = dev;
mentry->macc = macc;
mutex_lock(&lock);
get_device(dev);
list_add(&(mentry->list), &macc_list);
mutex_unlock(&lock);
return 0;
}
EXPORT_SYMBOL(of_memory_accessor_register);
Basically my module is used for things like serial EEPROMs and whatnot
so that external modules can find the accessor functions based on the
device tree. In my case I am updating the Vitesse VSC848X driver so that
it can read the SFP module when it is plugged in using the AT24 I2C
EEPROM module. I want to prevent the at24 module from unloading while
other modules in turn are using it. The at24 module does not export any
symbols.
NOTE that I plan to change the above code so that I only increment the
eeprom modules reference count when another module actually uses it and
release it only when all other modules are no longer using the accessor
functions.
I also am not subscribed to the LKML so please CC me on any responses.
-Aaron
--
Aaron Williams
Software Engineer
Cavium, Inc.
(408) 943-7198 (510) 789-8988 (cell)
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Incrementing module reference count
2012-12-08 1:14 Incrementing module reference count Aaron Williams
@ 2012-12-08 11:44 ` Borislav Petkov
2012-12-08 19:17 ` Aaron Williams
0 siblings, 1 reply; 4+ messages in thread
From: Borislav Petkov @ 2012-12-08 11:44 UTC (permalink / raw)
To: Aaron Williams; +Cc: linux-kernel, Williams, Aaron
On Fri, Dec 07, 2012 at 05:14:39PM -0800, Aaron Williams wrote:
> Hi,
>
> I have a kernel module which other modules register with in order to
> export access functions. So far I have everything working but I want
> to prevent a module that is registered with my module from unloading
> since now my module is dependent on the other module.
>
> Is there a way I can cause the reference count of the module
> registering with my module to increase? I tried calling get_device
> with the device structure of the module that is registering but that
> does not seem to work.
>
> For example, I have the following function:
>
> /**
> * Adds a mapping of a device node to a memory accessor
> *
> * @param[in] dev - device
> * @param[in] macc - memory accessor
> *
> * @returns 0 for success or -ENOMEM
> */
> int of_memory_accessor_register(struct device *dev,
> struct memory_accessor *macc)
> {
> struct of_macc_entry *mentry;
>
> mentry = kmalloc(sizeof(*mentry), GFP_KERNEL);
> if (mentry == NULL)
> return -ENOMEM;
>
> mentry->dev = dev;
> mentry->macc = macc;
>
> mutex_lock(&lock);
>
> get_device(dev);
> list_add(&(mentry->list), &macc_list);
>
> mutex_unlock(&lock);
>
> return 0;
> }
> EXPORT_SYMBOL(of_memory_accessor_register);
>
> Basically my module is used for things like serial EEPROMs and
> whatnot so that external modules can find the accessor functions
> based on the device tree. In my case I am updating the Vitesse
> VSC848X driver so that it can read the SFP module when it is plugged
> in using the AT24 I2C EEPROM module. I want to prevent the at24
> module from unloading while other modules in turn are using it. The
> at24 module does not export any symbols.
>From a quick code scan, at24 has this:
static struct i2c_driver at24_driver = {
.driver = {
.name = "at24",
.owner = THIS_MODULE,
},
.probe = at24_probe,
.remove = __devexit_p(at24_remove),
.id_table = at24_ids,
};
and then you could do:
try_module_get(at24_driver.driver->owner);
from your code. AFAICT, of course.
HTH.
--
Regards/Gruss,
Boris.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Incrementing module reference count
2012-12-08 11:44 ` Borislav Petkov
@ 2012-12-08 19:17 ` Aaron Williams
2012-12-09 11:55 ` Borislav Petkov
0 siblings, 1 reply; 4+ messages in thread
From: Aaron Williams @ 2012-12-08 19:17 UTC (permalink / raw)
To: Borislav Petkov, linux-kernel, Williams, Aaron
On 12/08/2012 03:44 AM, Borislav Petkov wrote:
> On Fri, Dec 07, 2012 at 05:14:39PM -0800, Aaron Williams wrote:
>> Hi,
>>
>> I have a kernel module which other modules register with in order to
>> export access functions. So far I have everything working but I want
>> to prevent a module that is registered with my module from unloading
>> since now my module is dependent on the other module.
>>
>> Is there a way I can cause the reference count of the module
>> registering with my module to increase? I tried calling get_device
>> with the device structure of the module that is registering but that
>> does not seem to work.
>>
>> For example, I have the following function:
>>
>> /**
>> * Adds a mapping of a device node to a memory accessor
>> *
>> * @param[in] dev - device
>> * @param[in] macc - memory accessor
>> *
>> * @returns 0 for success or -ENOMEM
>> */
>> int of_memory_accessor_register(struct device *dev,
>> struct memory_accessor *macc)
>> {
>> struct of_macc_entry *mentry;
>>
>> mentry = kmalloc(sizeof(*mentry), GFP_KERNEL);
>> if (mentry == NULL)
>> return -ENOMEM;
>>
>> mentry->dev = dev;
>> mentry->macc = macc;
>>
>> mutex_lock(&lock);
>>
>> get_device(dev);
>> list_add(&(mentry->list), &macc_list);
>>
>> mutex_unlock(&lock);
>>
>> return 0;
>> }
>> EXPORT_SYMBOL(of_memory_accessor_register);
>>
>> Basically my module is used for things like serial EEPROMs and
>> whatnot so that external modules can find the accessor functions
>> based on the device tree. In my case I am updating the Vitesse
>> VSC848X driver so that it can read the SFP module when it is plugged
>> in using the AT24 I2C EEPROM module. I want to prevent the at24
>> module from unloading while other modules in turn are using it. The
>> at24 module does not export any symbols.
> From a quick code scan, at24 has this:
>
> static struct i2c_driver at24_driver = {
> .driver = {
> .name = "at24",
> .owner = THIS_MODULE,
> },
> .probe = at24_probe,
> .remove = __devexit_p(at24_remove),
> .id_table = at24_ids,
> };
>
> and then you could do:
>
> try_module_get(at24_driver.driver->owner);
>
> from your code. AFAICT, of course.
>
> HTH.
Thank you.
try_module_get is what I needed.
I got it working. I designed my solution to be generic so it's not tied
to just the at24 driver. I added a memory accessor module which is
basically a registry. The at24 driver registers with it and then the
Vitesse PHY driver calls it to gain access to the at24 driver. Only when
the Vitesse driver gets the memory accessor the reference count for the
at24 driver is incremented.
My solution can work with any module that provides memory accessor
functions.
I still have some cleanup to do since my work was done with an older 2.6
kernel.
-Aaron
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Incrementing module reference count
2012-12-08 19:17 ` Aaron Williams
@ 2012-12-09 11:55 ` Borislav Petkov
0 siblings, 0 replies; 4+ messages in thread
From: Borislav Petkov @ 2012-12-09 11:55 UTC (permalink / raw)
To: Aaron Williams; +Cc: linux-kernel, Williams, Aaron
On Sat, Dec 08, 2012 at 11:17:31AM -0800, Aaron Williams wrote:
> I got it working. I designed my solution to be generic so it's not
> tied to just the at24 driver. I added a memory accessor module which
> is basically a registry. The at24 driver registers with it and then
> the Vitesse PHY driver calls it to gain access to the at24 driver.
> Only when the Vitesse driver gets the memory accessor the reference
> count for the at24 driver is incremented.
Hmm, I'm not sure you even need an additional layer because there's also
request_module(). And as long as your module is using at24, you won't
need to increment its refcount, AFAICT.
HTH.
--
Regards/Gruss,
Boris.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2012-12-09 11:55 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-12-08 1:14 Incrementing module reference count Aaron Williams
2012-12-08 11:44 ` Borislav Petkov
2012-12-08 19:17 ` Aaron Williams
2012-12-09 11:55 ` Borislav Petkov
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox