LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-31-2010, 02:30 PM   #1
Dufresne
LQ Newbie
 
Registered: Jul 2008
Posts: 23

Rep: Reputation: 16
Kernel Modules, And goto


In programming courses , we always to be taught to never to use goto statement in code.In many article about programming also indicate so.
I'm new to kernel module programming , but i see many goto statements in kernel module samples.
Why there are so much goto statements in kernel modules?
 
Old 05-31-2010, 03:06 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Dufresne View Post
In programming courses , we always to be taught to never to use goto statement in code.
...
Why ?
 
1 members found this post helpful.
Old 05-31-2010, 03:25 PM   #3
posixculprit
Member
 
Registered: May 2010
Posts: 136

Rep: Reputation: 42
I'm calling so as to congratulate Sergei Steshenko on an exceptionally useful post which also took this thread out of the "zero replies threads" list. Keep up the good work!
 
0 members found this post helpful.
Old 05-31-2010, 03:37 PM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by posixculprit View Post
I'm calling so as to congratulate Sergei Steshenko on an exceptionally useful post which also took this thread out of the "zero replies threads" list. Keep up the good work!
If the OP was taught not use goto's without being explained why, when and how, he was wrongly taught.
 
Old 05-31-2010, 03:42 PM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Here is just one of many relevant hits on Google:
http://en.wikipedia.org/wiki/Goto

The main point is that "goto" is never necessary, and---in some standards---would not be allowed:
Quote:
The 1960s and 1970s saw computer scientists move away from GOTO statements in favor of the "structured programming" paradigm. Some programming style coding standards prohibit use of GOTO statements, particularly in view of the aforementioned structured program theorem. While it's a definite proof that GOTOs can be eliminated completely from the code, it doesn't prove in any way that the resulting programs would be easier to write and maintain than those using GOTO statements or that they would execute efficiently in every context.
My rule would be that the programmer can follow any standard they like, as long as the code meets the requirements of the customer.

Note that there are requirements that kernel developers are required to follow.

(("Why?" was an OK question.....))
 
Old 05-31-2010, 03:45 PM   #6
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Quote:
If the OP was taught not use goto's without being explained why, when and how, he was wrongly taught.
Absolutely correct.

Here are two (arguably) valid uses of "goto":
Quote:
http://www.lysator.liu.se/c/bwk-tutor.html

One use of goto's with some legitimacy is in a program which contains a long loop, where a while(1) would be too extended. Then you might write
Code:
          mainloop:
               ...
               goto mainloop;
Another use is to implement a break out of more than one level of for or while. goto's can only branch to labels within the same function.
Quote:
http://www.cprogramming.com/tutorial/goto.html

if we want to handle all our errors in one place, we'll need to add in a variable to track the return value of our function so we can return it:

Code:
int big_function()
{
    int ret_val = [success];
    /* do some work */
    if([error])
    {
        ret_val = [error];
        goto end;
    }
    /* do some more work */
    if([error])
    {
        ret_val = [error];
        goto end;
    }
    /* do some more work */
    if([error])
    {
        ret_val = [error];
        goto end;
    }
    /* do some more work */
    if([error])
    {
        ret_val = [error];
        goto end;
    }
end:
    /* clean up*/
    return ret_val;
}
The benefit here is that your code following end has access to everything it will need to perform cleanup, and you've managed to reduce the number of change points considerably. Another benefit is that you've gone from having multiple exit points for your function to just one; there's no chance you'll accidentally return from the function without cleaning up.

Moreover, since goto is only being used to jump to a single point, it's not as though you're creating a mass of spaghetti code jumping back and forth in an attempt to simulate function calls. Rather, goto actually helps write more structured code.

There is one thing to be aware of: while your cleanup code should be able to free all of the memory you use, there may be times when you actually want to free that memory yourself and possibly reallocate it later. In these cases, if you do call free on a ptr and then have an if([error]) between that call to free and the subsequent call to malloc, you should definitely set the pointer to point to NULL! This will prevent your jumping to the cleanup code and then calling free on that pointer a second time, which can result in a security hole (the "double free" problem).

Goto should always be used sparingly, and as a last resort -- but there is a time and a place for it. The question should be not "do you have to use it" but "is it the best choice" to use it.
 
Old 05-31-2010, 04:18 PM   #7
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,193

Rep: Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307Reputation: 5307
I think this is a pretty good explanation:
http://games.slashdot.org/comments.p...3&cid=29198751

Yes, it's the second reason quoted above. But it specifically discusses kernel code. And one of the follow-ups has the block rewritten without goto so that you can compare them.

Last edited by dugan; 05-31-2010 at 04:26 PM.
 
Old 05-31-2010, 04:26 PM   #8
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
In simple English (I think I'm reiterating what's already been said) using goto for error handling, especially to get out from deep inner loop/scope, is quite good, clear and recommended.

In more general terms, goto to a label below the goto statement most likely is OK.
 
Old 05-31-2010, 05:44 PM   #9
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Post

Quote:
Originally Posted by Dufresne View Post
Why there are so much goto statements in kernel modules?
So that kernel driver writers can write functions that look like this:
Code:
int driver_work(void *data)
{
	struct driver_data *driver = data;
	int result = -1;

	if (!driver)
		goto out;

	driver->data = kzalloc(sizeof(*driver->data), GFP_KERNEL);
	if (!driver->data)
		goto out;

	result = driver_function1(driver->data);
	if (-1 == result)
		goto error1;

	driver->info = kzalloc(sizeof(*driver->info), GFP_KERNEL);
	if (!driver->info)
		goto error1;

	result = driver_function2(driver->info);
	if (-1 == result)
		goto error2;

	result = 0;
	goto out;

error2:
	kfree(driver->info);
	driver->info = NULL;

error1:
	kfree(driver->data);
	driver->data = NULL;

out:
	return result;
}
Read the kernel CodingStyle document for more information.
 
Old 05-31-2010, 06:15 PM   #10
Dufresne
LQ Newbie
 
Registered: Jul 2008
Posts: 23

Original Poster
Rep: Reputation: 16
Thanks for all replies.I got the explanation.

Last edited by Dufresne; 05-31-2010 at 06:16 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Modules in /lib/modules kaplan71 Linux - General 1 04-02-2007 11:00 AM
Kernel Modules and modules.conf init Linux - General 0 02-20-2004 06:51 PM
new kernel (2.4.22 up from 2.4.20-6) - missing modules - use old modules? Simon Bridge Linux - Software 1 02-04-2004 05:52 AM
Goto command? batfoot Linux - General 4 08-26-2003 07:17 PM
Kernel modules: why are some kernel modules zipped? hampel Slackware 3 06-30-2003 06:33 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:50 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