public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Chen Gang F T <chen.gang.flying.transformer@gmail.com>
To: "Eric W. Biederman" <ebiederm@xmission.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	Chen Gang <gang.chen@asianux.com>,
	Al Viro <viro@zeniv.linux.org.uk>,
	xi.wang@gmail.com, nicolas.dichtel@6wind.com,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] kernel/sysctl_binary.c: improve the usage of return value 'result'
Date: Wed, 07 Aug 2013 13:28:45 +0800	[thread overview]
Message-ID: <5201DB0D.7080200@gmail.com> (raw)
In-Reply-To: <87zjsu5uy1.fsf@xmission.com>

On 08/07/2013 06:13 AM, Eric W. Biederman wrote:
> Andrew Morton <akpm@linux-foundation.org> writes:
> 
>> On Tue, 06 Aug 2013 15:29:42 +0800 Chen Gang <gang.chen@asianux.com> wrote:
>>
>>> Improve the usage of return value 'result', so not only can make code
>>> clearer to readers, but also can improve the performance.
>>
>> It used to be pervasive kernel style do to
>>
>> 	ret = -ENOMEM;
>> 	foo = alloc(...);
>> 	if (!foo)
>> 		goto out;
>>
>> whereas nowadays people usually do the more straightforward
>>
>> 	foo = alloc(...);
>> 	if (!foo) {
>> 		ret = -ENOMEM;
>> 		goto out;
>> 	}
>>
>> The thinking was that the old style generated better code, but for the
>> life of me I can't remember why :(
> 
> Because doing the assignment outside of the if() goto .  Allows the
> compiler to emit the if() goto as a single branch.
> 
> While a smart compiler may perform the code motion across the branch,
> it is much easier for the compiler to branch to somewhere else perform
> the assignment and then branch out.
> 

For my opinion, for assembly code, the old style is clearer than the new
style. And commonly, the old style will be faster than new style.

Thanks.

> Eric
> 
> 
>> Your patch switches from old-style to new-style.  And it appears to
>> have increased the text size.  I did this, to switch three sites back
>> to old-style:
>>
>> --- a/kernel/sysctl_binary.c~kernel-sysctl_binaryc-improve-the-usage-of-return-value-result-fix
>> +++ a/kernel/sysctl_binary.c
>> @@ -941,17 +941,15 @@ static ssize_t bin_string(struct file *f
>>  		copied = result;
>>  		lastp = oldval + copied - 1;
>>  
>> -		if (get_user(ch, lastp)) {
>> -			result = -EFAULT;
>> +		result = -EFAULT;
>> +		if (get_user(ch, lastp))
>>  			goto out;
>> -		}
>>  
>>  		/* Trim off the trailing newline */
>>  		if (ch == '\n') {
>> -			if (put_user('\0', lastp)) {
>> -				result = -EFAULT;
>> +			result = -EFAULT;
>> +			if (put_user('\0', lastp))
>>  				goto out;
>> -			}
>>  			copied -= 1;
>>  		}
>>  	}
>> @@ -976,11 +974,10 @@ static ssize_t bin_intvec(struct file *f
>>  	char *buffer;
>>  	ssize_t result;
>>  
>> +	result = -ENOMEM;
>>  	buffer = kmalloc(BUFSZ, GFP_KERNEL);
>> -	if (!buffer) {
>> -		result = -ENOMEM;
>> +	if (!buffer)
>>  		goto out;
>> -	}
>>  
>>  	if (oldval && oldlen) {
>>  		unsigned __user *vec = oldval;
>> _
>>
>> and kernel/sysctl_binary.o's .text got six bytes smaller.
>>
>> Now, smaller text doesn't mean faster code.  But it probably means
>> larger cache footprint, which can mean slower code.
>>
>> IOW, it isn't obvious that this was an improvement.
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at  http://www.tux.org/lkml/
> 


-- 
Chen Gang

  reply	other threads:[~2013-08-07  5:29 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-08-06  7:29 [PATCH] kernel/sysctl_binary.c: improve the usage of return value 'result' Chen Gang
2013-08-06 21:43 ` Andrew Morton
2013-08-06 22:11   ` Joe Perches
2013-08-07  3:53     ` Chen Gang
2013-08-06 22:13   ` Eric W. Biederman
2013-08-07  5:28     ` Chen Gang F T [this message]
2013-08-07  5:11   ` Chen Gang
2013-08-06 21:46 ` Eric W. Biederman
2013-08-07  5:07   ` Chen Gang
2013-08-07  5:56     ` Li Zefan
2013-08-07  6:10       ` Joe Perches
2013-08-07  6:29         ` Chen Gang
2013-08-07  6:42         ` Li Zefan
2013-08-07  6:42         ` Li Zefan
2013-08-07  6:57           ` Chen Gang
2013-08-07  6:24       ` Chen Gang
2013-08-07  6:29         ` Andrew Morton
2013-08-07  6:34           ` Chen Gang
2013-08-07  7:02         ` Li Zefan
2013-08-07  8:03           ` Chen Gang
2013-08-07  8:44             ` Li Zefan
2013-08-07  9:13               ` Chen Gang
2013-08-07  7:45     ` Eric W. Biederman
2013-08-07 10:25       ` Chen Gang
2013-08-07 18:38         ` Eric W. Biederman
2013-08-08  3:19           ` Chen Gang

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=5201DB0D.7080200@gmail.com \
    --to=chen.gang.flying.transformer@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=ebiederm@xmission.com \
    --cc=gang.chen@asianux.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nicolas.dichtel@6wind.com \
    --cc=viro@zeniv.linux.org.uk \
    --cc=xi.wang@gmail.com \
    /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