Module Interdependencies - linux-kernel

I have 2 modules mod_1.ko & mod_2.ko with fun_1() & fun_2() defined in them respectively. I exported the functions and want to use fun_1 & fun_2 in mod_2.ko & mod_1.ko. How do I proceed..?

If you're using it explicitly (you have call of fun_1 from mod_2.ko and fun_2 from mod_1.ko ) then kernel won't let you load your modules. This is happened because it reads symbol table and look for kernel existing modules - the one you can see in /proc/kallsyms. So mod_1 has fun_2 reference and need mod_2 to be loaded. And mod_2 has fun_1 reference and need mod_1 to be loaded. There you have dependency lock)
I can think of 2 solutions for your problem:
Take out fun_1 and fun_2 into separate module that you'll load first.
Don't make explicit call to function. Do this implicitly with help of find_symbol. You declare function pointers, then you resolve that pointer in runtime with call to find_symbol and then you call your functions via function pointers.

Related

Can we dynamically create a function in go?

I'm trying to create a service where a user needs to operate over a data and can manipulate it in numerous ways, so I'm not aware of the manipulations at the time of compiling of my program. One way to achieve this is to give the user a function with data as param. Which landed me in the following direction.
Dynamically create a function
Dynamically linking a function after compiling it separately.
I'm open to suggestions. If you have other ways to achieve the end goal.
If you don't like this as an answer I can move to the comment section, but it's rather long that's why I put here in the answer section.
Dynamically Dispatched Method: The only way to have dynamically dispatched methods is through an interface. Methods on a struct or any other concrete type are always resolved statically.
Closure: Go supports anonymous functions, which can form closures. Anonymous functions are useful when you want to define a function inline without having to name it.
Dyncamically call method on Interface:
Please let me know if that helps you to understand the concept in golang.

Understanding the lib keyword

Is lib a special keyword for Frogatto Formula Language (FFL)? That seems to be the way of invoking class methods. For example:
where frog = lib.citadel.create_creature('Giant Frog')
Also, I am interested to know where can I find a list of all the available lib.**** library objects and how to list of all their available functions.
I wouldn't call it a keyword as such, more a symbol that appears in the standard namespace. As such it is functionally fairly close to a keyword.
When you create a class by adding a file e.g. data/classes/blah.cfg then a singleton instance of this class will be available using lib.blah. This is a convenient way of effectively creating your own namespace of functions -- create a class, add functions to it, then your functions can be accessed using lib.classname.functionname()

Avoid function duplication of dump function by psysh

I am developer of imi-conrun and have a problem: We use the psySh package we would like not to drop and defines the global scope function "dump" and have to initialize the Contao core which defines the global scope function "dump" as well without checking if the function is registered and then crashes.
Is there any possibility to only remove the dump function from psySh without making a fork?
I think there is no real solution for this.
In the end it turn's out I do not need PsySH - so I removed it - problem solved.
Might make the suggestion to Contao to not blindly defined the dump function without a function_exists() call
On the other hand I could ensure that Contao is loaded first, then PsySH and thus would not define the dump() function again which would mean to run the composer autoload before the Contao init.
TL;DR: global name space functions are bad.

Why use clone() of ReLU in torch's inception net?

I'm reading https://github.com/Element-Research/dpnn/blob/master/Inception.lua
You can see tons of clone()'s in this source. like
mlp:add(self.transfer:clone())
self.transfer is nothing more than nn.ReLU().
Then,
Why does this code call activation functions using clone()? Does this only concern memory issues?
I thought that the clone shares parameters. Is this right? If it's right, it means all activations of this inception module share parameters. It looks like nonsense. Do I misunderstand Inception-Net?
If you don't clone the module self.transfer then all modules transfer in your net mlp will have the same state variables output and gradInput.
Look for example at this toy code
require 'nn'
module = nn.ReLU()
net = nn.Sequential():add(nn.Linear(2,2)):add(module):add(nn.Linear(2,1)):add(module)
input = torch.Tensor(2,2):random()
net:forward(input)
print(net:get(2).output)
print(net:get(4).output)
Both print statements will return the same value. Modifying one of the module outputs will modify the other one. Since we do not want this behavior we have to clone the module. (However in your case, cloning a simple nn.ReLU() is not that useful.)
The documentation says
If arguments are provided to the clone(...) function it also calls share(...) with those arguments on the cloned module after creating it, hence making a deep copy of this module with some shared parameters.
Therefore if you don't provide any arguments the parameters won't be shared.

Instantiate inline class method

I write a library, this library includes a function void f() this function is a one line function and when I compile the library to shared object with -O3 gcc optimization flag it is inlined. I call it in a critical place in the code (must be as fast as possible) and I don't want to call it not inlined (hits performance substantially). The problem is that this function is part of the API that my library exposes so when library users link with my library and call this function they get undefined reference linkage error. Is there a way for my code to use the function inlined but still instantiate it in the object file so library users will be able to link and use it? (When I say "instantiate it in the object file" I mean I'd like to see it when I run objdump -t on the shared object). Just to make it clear, I'm not interested in a solution to wrap it with a function
void F() __attribute__((noinline)) { f(); }
Because I have many functions like that and I don't want to keep a copy for every function due to the enormous amount of overhead. I'm looking for a way to tell the compiler to use it inline when the definition is available to it, but still instantiate the function in the object file, so library users can link to with it too.
Check out this How can I tell gcc not to inline a function?
I found this solution the most appropriate. I'd like to note the main thing is that the code inside the library is still inlined so there is no performance penalty but users can still use the API as all functions have instantiation
Also another possible solution in the compilation level is to use -fkeep-inline-functions gcc switch which also instantiates inline functions and and uses them inlined where possible (unlike -fno-inline switch). The main problem with this switch is that if your code is heavily templated compilation time is much longer and the binary product becomes much bigger

Resources