All of lore.kernel.org
 help / color / mirror / Atom feed
From: Chen Gang <gang.chen@asianux.com>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>,
	Rusty Russell <rusty@rustcorp.com.au>, Robin Holt <holt@sgi.com>,
	athorlton@sgi.com, Al Viro <viro@zeniv.linux.org.uk>,
	linux-next@vger.kernel.org
Subject: Re: [PATCH] kernel/panic.c: reduce 1 byte usage for print tainted buffer.
Date: Tue, 08 Oct 2013 09:45:56 +0800	[thread overview]
Message-ID: <525363D4.3090706@asianux.com> (raw)
In-Reply-To: <20131007182518.e64c8596851bfb6b44279129@linux-foundation.org>

On 10/08/2013 09:25 AM, Andrew Morton wrote:
> On Tue, 08 Oct 2013 09:10:37 +0800 Chen Gang <gang.chen@asianux.com> wrote:
> 
>>> - Requires that the two literal "Tainted: " strings be kept in sync.
>>>
>>
>> Hmm... if more than 2, we should use a macro instead of, else (within
>> 2), especially they are near by, we can still let it hard coded, I feel
>> that will more straightful for readers and writers.
>>
>>
>>> - Assumes that strlen("Not tainted") <= strlen("Tainted") +
>>>   ARRAY_SIZE(tnts).  Which is true, but might not be if someone makes
>>>   changes...
>>>
>>
>> Hmm... it use snprintf() instead of sprintf(), although I feel better
>> using scnprintf() instead of.
>>
>> This string can be trucated, and scnprintf() is more suitable for this
>> kind of string. And snprintf() is for the string which can not be
>> truncated (will return the ideal length to notify the caller).
> 
> It's hardly a huge issue, but I'd do something along the lines of
> 
> --- a/kernel/panic.c~a
> +++ a/kernel/panic.c
> @@ -233,13 +233,16 @@ static const struct tnt tnts[] = {
>   */
>  const char *print_tainted(void)
>  {
> -	static char buf[ARRAY_SIZE(tnts) + sizeof("Tainted: ")];
> +	static const char tainted[] = "Tainted: ";
> +	static const char not_tainted[] = "Not tainted: ";
> +	static char buf[ARRAY_SIZE(tnts) +
> +			max(sizeof(tainted), sizeof(not_tainted))];
>  
>  	if (tainted_mask) {
>  		char *s;
>  		int i;
>  
> -		s = buf + sprintf(buf, "Tainted: ");
> +		s = buf + sprintf(buf, tainted);
>  		for (i = 0; i < ARRAY_SIZE(tnts); i++) {
>  			const struct tnt *t = &tnts[i];
>  			*s++ = test_bit(t->bit, &tainted_mask) ?
> @@ -247,7 +250,7 @@ const char *print_tainted(void)
>  		}
>  		*s = 0;
>  	} else
> -		snprintf(buf, sizeof(buf), "Not tainted");
> +		snprintf(buf, sizeof(buf), not_tainted);
>  
>  	return buf;
>  }
> 

Theoretically, your implementation is better than the original one.


> Except that doesn't compile because of our fancy max().
> 
> Maybe we have a compile-time-evaluable max which does plain old
> ((a < b) ?  b : a), not sure...  I don't think it's worth bothering
> about - leave it be.
> 
> 
> 

Excuse me, I am not quite understand your meaning :-(

Hmm... at least, if max() will be OK, I support your fixing.

 - in my memory, the old C compiler may not recognize "? :" in array.
   maybe it is not old version ansi C standard requirements? (at least,
   it is rarely used in history).

 - I guess: "max()" may be just the reason why original author don't
   implement it like you have done (maybe at that time, the C compiler
   did not support it).



Thanks.
-- 
Chen Gang

      reply	other threads:[~2013-10-08  1:46 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-10-05 15:50 [PATCH] kernel/panic.c: reduce 1 byte usage for print tainted buffer Chen Gang
2013-10-05 15:53 ` Chen Gang
2013-10-07 16:35 ` Alex Thorlton
2013-10-07 21:04   ` Chen Gang
2013-10-08  0:27 ` Andrew Morton
2013-10-08  0:32   ` Al Viro
2013-10-08  1:10   ` Chen Gang
2013-10-08  1:25     ` Andrew Morton
2013-10-08  1:45       ` Chen Gang [this message]

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=525363D4.3090706@asianux.com \
    --to=gang.chen@asianux.com \
    --cc=akpm@linux-foundation.org \
    --cc=athorlton@sgi.com \
    --cc=holt@sgi.com \
    --cc=linux-next@vger.kernel.org \
    --cc=peterz@infradead.org \
    --cc=rusty@rustcorp.com.au \
    --cc=viro@zeniv.linux.org.uk \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.