From: Matt Mackall <mpm@selenic.com>
To: Steven Liu <lingjiujianke@gmail.com>
Cc: linux-kernel@vger.kernel.org
Subject: Re: [PATCH] random: Check kmalloc return value and give the failed message
Date: Tue, 18 May 2010 00:38:43 -0500 [thread overview]
Message-ID: <1274161123.11603.507.camel@calx> (raw)
In-Reply-To: <AANLkTikYvtfMgpXW5mGHmMDSoj9O0QKOMzspoDQHURxF@mail.gmail.com>
On Tue, 2010-05-18 at 13:23 +0800, Steven Liu wrote:
> Hi, Matt Mackall,
>
> Check kmalloc return value and give the failed message
>
> Signed-off-by: Liu Qi <lingjiujianke@gmail.com>
> ---
> drivers/char/random.c | 5 ++++-
> 1 files changed, 4 insertions(+), 1 deletions(-)
>
> diff --git a/drivers/char/random.c b/drivers/char/random.c
> index 2fd3d39..d1bedeb 100644
> --- a/drivers/char/random.c
> +++ b/drivers/char/random.c
> @@ -952,8 +952,11 @@ static void init_std_data(struct entropy_store *r)
> mix_pool_bytes(r, &now, sizeof(now));
> mix_pool_bytes(r, utsname(), sizeof(*(utsname())));
> /* Enable continuous test in fips mode */
> - if (fips_enabled)
> + if (fips_enabled){
> r->last_data = kmalloc(EXTRACT_SIZE, GFP_KERNEL);
> + if(r->last_data == NULL)
> + printk(KERN_ERR "kmalloc memory failed in %s \n", __func__);
> + }
This is the wrong fix. I'm not sure how this kmalloc got in; I'm sure I
objected to it the first time around. The right answer is: use a static
buffer because a) it can't fail so no error-checking is needed and b)
EXTRACT_SIZE is only 10 bytes! That means that allocating it dynamically
as above takes more code space than you'd be saving by not allocating
it, even without the printk. Here's a better patch:
diff -r a12387bbcc93 drivers/char/random.c
--- a/drivers/char/random.c Thu Apr 08 10:02:02 2010 -0700
+++ b/drivers/char/random.c Tue May 18 00:38:04 2010 -0500
@@ -414,7 +414,7 @@
unsigned add_ptr;
int entropy_count;
int input_rotate;
- __u8 *last_data;
+ __u8 last_data[EXTRACT_SIZE];
};
static __u32 input_pool_data[INPUT_POOL_WORDS];
@@ -862,7 +862,7 @@
while (nbytes) {
extract_buf(r, tmp);
- if (r->last_data) {
+ if (fips_enabled) {
spin_lock_irqsave(&r->lock, flags);
if (!memcmp(tmp, r->last_data, EXTRACT_SIZE))
panic("Hardware RNG duplicated output!\n");
@@ -951,9 +951,6 @@
now = ktime_get_real();
mix_pool_bytes(r, &now, sizeof(now));
mix_pool_bytes(r, utsname(), sizeof(*(utsname())));
- /* Enable continuous test in fips mode */
- if (fips_enabled)
- r->last_data = kmalloc(EXTRACT_SIZE, GFP_KERNEL);
}
static int rand_initialize(void)
>
--
Mathematics is the supreme nostalgia of our time.
prev parent reply other threads:[~2010-05-18 5:38 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-05-18 5:23 [PATCH] random: Check kmalloc return value and give the failed message Steven Liu
2010-05-18 5:38 ` Matt Mackall [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=1274161123.11603.507.camel@calx \
--to=mpm@selenic.com \
--cc=lingjiujianke@gmail.com \
--cc=linux-kernel@vger.kernel.org \
/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