LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 06-05-2014, 08:05 PM   #1
naftilos
LQ Newbie
 
Registered: Jun 2014
Posts: 3

Rep: Reputation: Disabled
Declaring and using a global variable in Kernel


Hi all! I need to add a new function somewhere in the kernel (in mm/page_alloc.c but I guess it doesn't really matter). Then from the userspace (through a module) I want to enable or disable some parts of the code. My idea was to use a global variable. I have this:

linux/my_file.h:
Code:
extern int do_extra_enabled;
mm/my_file.c:
Code:
int do_extra_enabled;
mm/page_alloc.c
Code:
#include <linux/my_file.h>

void do_some_work() {
	printk(KERN_WARNING "normal work\n");
	if (do_extra_enabled)
		printk(KERN_WARNING "extra work!\n");
}
my_module:
Code:
#include <linux/my_file.h>

static int __init module_init(void)
{
	do_extra_enabled = 1;
}

static void __exit module_exit(void)
{
	do_extra_enabled = 0;
}
1. When I tried to compile the kernel, I got "page_alloc.c: undefined reference to `do_extra_enabled'".

2. Is there a better way to do what I want to achieve?

Last edited by naftilos; 06-05-2014 at 08:07 PM.
 
Old 06-08-2014, 10:43 AM   #2
dijetlo
Senior Member
 
Registered: Jan 2009
Location: RHELtopia....
Distribution: Solaris 11.2/Slackware/RHEL/
Posts: 1,491
Blog Entries: 2

Rep: Reputation: Disabled
Quote:
in mm/page_alloc.c but I guess it doesn't really matter
That would be an incorrect guess.
If you start changing the kernel header files and libraries, you will end up re-writing a significant part of the kernel to accommodate the changes. You also will not end up with anything of value unless your ultimate goal is to replace Linus Torvalds and his team as the maintainers of the Linux kernel.
Quote:
Is there a better way to do what I want to achieve?
Most people write modules and have them load and unload against the standard Linux kernel. If this is your first attempt, I'd strongly encourage you to move your new static function into a module and try to compile it there. Then, when an application needs that functionality, it can dynamically load it in the kernel and then unload it when it's finished.

Last edited by dijetlo; 06-08-2014 at 10:46 AM.
 
Old 06-10-2014, 06:32 AM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940
You should construct a self-contained kernel module which initializes its own data structure pointers when it is loaded, and tears them all down correctly when it is unloaded ... with all of the necessary protocols to ensure that this takes place correctly on an incredibly-busy multi-CPU system.

Your kernel module should contain whatever APIs are necessary to access the functionality of whatever-it-is you are doing, from user-space. Nothing else in the kernel should know or care.

Treat the main-line kernel source files as sacrosanct, reserved only for the gods. Let this Eleventh Commandment be carved upon your door ... or upon your brow ... "Thou Shalt Not Touch."

(In all seriousness, "you could up a lot of things." Go find the existing kernel module that is most similar to what you are already doing, and hack it into something else very carefully base your own efforts upon its precedents. It's a time-honored tradition to do that.)

Last edited by sundialsvcs; 06-10-2014 at 06:33 AM.
 
Old 06-15-2014, 07:43 PM   #4
naftilos
LQ Newbie
 
Registered: Jun 2014
Posts: 3

Original Poster
Rep: Reputation: Disabled
For what is worth, I have found the answer I was looking for. I just had to export the global variable:

mm/my_file.c:
Code:
#include <linux/my_file.h>
#include <linux/module.h>

int do_extra_enabled;
EXPORT_SYMBOL(do_extra_enabled);
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Kernel global variable ghost_gr Programming 1 04-27-2011 11:42 AM
How to declare a Global Variable in Kernel rheosiva Linux - Kernel 1 04-17-2009 12:55 AM
add global variable in linux kernel sourcecode sid18 Linux - Kernel 3 01-18-2008 08:43 PM
defining global variable in fedora kernel tuncausluer Linux - Software 0 12-31-2004 01:53 PM
Defining Global variable in Fedora Kernel tuncausluer Fedora - Installation 0 12-31-2004 01:38 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 12:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration