public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: P Lavin <lavin.p@redpinesignals.com>
To: linux-kernel@vger.kernel.org
Subject: Re: no need to check for NULL before calling kfree() -fs/ext2/
Date: Thu, 31 Mar 2005 12:00:52 +0530	[thread overview]
Message-ID: <424B991C.5040109@redpinesignals.com> (raw)


Hi Jesper,
I'm sending this mail to mailing list coz in my company we have some
restrictions on o/g mails, Sorry for that...
Lemme ask u smthing, herez the code
     199     sndpkt = (RSI_sndpkt_t *) RSI_MALLOC(sizeof(RSI_sndpkt_t));
     200     sndpkt->buf_list = (RSI_buf_t *) RSI_MALLOC(sizeof(RSI_buf_t));
Here if malloc fails sndpkt->buf_list should be null right ?? & if i
proceed further ..

     201     sndpkt->buf_list->start_addr = buf;
     202     sndpkt->buf_list->length     = length;
Here itself this should crash right ?? But its not crashing here !!! Wt
was happening was

201 sndpkt->buf_list->start_addr = buf; was not getting initailised & wn
we try to access this variable latter
this was crashing.

Actally i'm not checking for return value from kmalloc thatz a mistake,
I'll fix this but why is it not crashing in line # 201 ??

Jesper Juhl wrote:

>On Wed, 30 Mar 2005, P Lavin wrote:
>
>  
>
>>Date: Wed, 30 Mar 2005 12:45:01 +0530
>>From: P Lavin <lavin.p@redpinesignals.com>
>>To: linux-kernel@vger.kernel.org
>>Subject: Re: no need to check for NULL before calling kfree() -fs/ext2/
>>
>>Hi,
>>In my wlan driver module, i allocated some memory using kmalloc in interrupt
>>context, this one failed but its not returning NULL , 
>>    
>>
>
>kmalloc() should always return NULL if the allocation failed.
>
>
>  
>
>>so i was proceeding
>>further everything was going wrong... & finally the kernel crahed. Can any one
>>of you tell me why this is happening ? i cannot use GFP_KERNEL because i'm
>>calling this function from interrupt context & it may block. Any other
>>    
>>
>
>If you need to allocate memory from interrupt context you should be using 
>GFP_ATOMIC (or, if possible, do the allocation earlier in a different 
>context).
>
>
>  
>
I'm using this flag only, this flag does not guarentee mem allocation,
right ??

>>solution for this ?? I'm concerned abt why kmalloc is not returning null if
>>its not a success ??
>>
>>    
>>
>I have no explanation for that, are you sure that's really what's 
>happening?
>
>
>  
>
I'm not checking this , but my explanation is given above.

>>Is it not necessary to check for NULL before calling kfree() ??
>>    
>>
>
>No, it is not nessesary to check for NULL before calling kfree() since 
>kfree() does    
>
>void kfree (const void *objp)
>{
>	... 
>        if (!objp)
>                return;
>	...
>}
>
>So, if you pass kfree() a NULL pointer it deals with it itself, you don't 
>need to check that explicitly before calling kfree() - that's redundant.
>
>
>  
>

Regs,
Lavin

             reply	other threads:[~2005-03-31  6:19 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-03-31  6:30 P Lavin [this message]
  -- strict thread matches above, loose matches on Subject: below --
2005-03-25 22:08 [PATCH] no need to check for NULL before calling kfree() - fs/ext2/ Jesper Juhl
2005-03-26  8:32 ` Arjan van de Ven
2005-03-26 23:21   ` [PATCH] no need to check for NULL before calling kfree() -fs/ext2/ linux-os
2005-03-26 23:54     ` Jesper Juhl
2005-03-27  0:05       ` Lee Revell
2005-03-27 10:55         ` Jesper Juhl
2005-03-27 14:56           ` Paul Jackson
2005-03-27 15:12             ` Jan Engelhardt
2005-03-27 17:40               ` Dave Jones
2005-03-29  2:52                 ` Lee Revell
2005-03-29  6:30                   ` Pekka Enberg
2005-03-29  7:06                     ` Jan Engelhardt
2005-03-29  7:24                       ` Pekka J Enberg
2005-03-30  2:44                         ` Paul Jackson
2005-03-30  6:13                           ` Pekka J Enberg
2005-03-30  6:16                             ` Paul Jackson
2005-03-30  7:15                             ` P Lavin
2005-03-30 14:20                               ` Jesper Juhl
2005-03-30 19:10                           ` Jesper Juhl
2005-04-09  2:21                             ` Jesper Juhl

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=424B991C.5040109@redpinesignals.com \
    --to=lavin.p@redpinesignals.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