grub-devel.gnu.org archive mirror
 help / color / mirror / Atom feed
From: Stanislav Kholmanskikh <stanislav.kholmanskikh@oracle.com>
To: The development of GNU GRUB <grub-devel@gnu.org>
Cc: vasily.isaenko@oracle.com
Subject: Re: [PATCH V2 3/4] ofnet: free memory on module unload
Date: Mon, 12 Dec 2016 12:56:57 +0300	[thread overview]
Message-ID: <584E7469.4010003@oracle.com> (raw)
In-Reply-To: <150829c3-3493-a2b1-8891-1d49875f7451@gmail.com>



On 12/10/2016 09:18 PM, Andrei Borzenkov wrote:
> 02.12.2016 18:10, Stanislav Kholmanskikh пишет:
>> On module unload each of its network cards are unregistered,
>> but corresponding memory areas are not freed.
>>
>> This commit is to fix this situation.
>>
>> Freeing the transmit buffer goes via a special function, since
>> it is allocated via ofnet_alloc_netbuf().
>>
>> Signed-off-by: Stanislav Kholmanskikh <stanislav.kholmanskikh@oracle.com>
>> ---
>>  grub-core/net/drivers/ieee1275/ofnet.c |   61 +++++++++++++++++++++++++++++++-
>>  1 files changed, 60 insertions(+), 1 deletions(-)
>>
>> diff --git a/grub-core/net/drivers/ieee1275/ofnet.c b/grub-core/net/drivers/ieee1275/ofnet.c
>> index 25559c8..1f8ac9a 100644
>> --- a/grub-core/net/drivers/ieee1275/ofnet.c
>> +++ b/grub-core/net/drivers/ieee1275/ofnet.c
>> @@ -331,6 +331,40 @@ grub_ieee1275_alloc_mem (grub_size_t len)
>>      return (void *)args.result;
>>  }
>>  
>> +/* Free memory allocated by alloc-mem */
>> +static grub_err_t
>> +grub_ieee1275_free_mem (void *addr, grub_size_t len)
>> +{
>> +  struct free_args
>> +  {
>> +    struct grub_ieee1275_common_hdr common;
>> +    grub_ieee1275_cell_t method;
>> +    grub_ieee1275_cell_t len;
>> +    grub_ieee1275_cell_t addr;
>> +    grub_ieee1275_cell_t catch;
>> +  }
>> +  args;
>> +
>> +  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_CANNOT_INTERPRET))
>> +    {
>> +      grub_error (GRUB_ERR_UNKNOWN_COMMAND, N_("interpret is not supported"));
>> +      return grub_errno;
>> +    }
>> +
>> +  INIT_IEEE1275_COMMON (&args.common, "interpret", 3, 1);
>> +  args.addr = (grub_ieee1275_cell_t)addr;
>> +  args.len = len;
>> +  args.method = (grub_ieee1275_cell_t) "free-mem";
>> +
>> +  if (IEEE1275_CALL_ENTRY_FN(&args) == -1 || args.catch)
>> +    {
>> +      grub_error (GRUB_ERR_INVALID_COMMAND, N_("free-mem failed"));
>> +      return grub_errno;
>> +    }
>> +
>> +  return GRUB_ERR_NONE;
>> +}
>> +
>>  static void *
>>  ofnet_alloc_netbuf (grub_size_t len)
>>  {
>> @@ -340,6 +374,15 @@ ofnet_alloc_netbuf (grub_size_t len)
>>      return grub_zalloc (len);
>>  }
>>  
>> +static void
>> +ofnet_free_netbuf (void *addr, grub_size_t len)
>> +{
>> +  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_VIRT_TO_REAL_BROKEN))
>> +    grub_ieee1275_free_mem (addr, len);
>> +  else
>> +    grub_free (addr);
>> +}
>> +
>>  static int
>>  search_net_devices (struct grub_ieee1275_devalias *alias)
>>  {
>> @@ -494,9 +537,25 @@ GRUB_MOD_INIT(ofnet)
>>  GRUB_MOD_FINI(ofnet)
>>  {
>>    struct grub_net_card *card, *next;
>> +  struct grub_ofnetcard_data *ofdata;
>>  
>>    FOR_NET_CARDS_SAFE (card, next) 
>>      if (card->driver && grub_strcmp (card->driver->name, "ofnet") == 0)
>> -      grub_net_card_unregister (card);
>> +      {
>> +	grub_net_card_unregister (card);
>> +	/*
>> +	 * The fact that we are here means the card was successfully
>> +	 * initialized in the past, so all the below pointers are valid,
>> +	 * and we may free associated memory without checks.
>> +	 */
>> +	ofdata = (struct grub_ofnetcard_data *) card->data;
>> +	grub_free (ofdata->path);
>> +	grub_free (ofdata);
>> +
>> +	ofnet_free_netbuf (card->txbuf, card->txbufsize);
>> +
>> +	grub_free ((void *) card->name);
>> +	grub_free (card);
>> +      }
> 
> No, it's not safe to do. I plunged into it in efinet and reverted. We
> may have dangling references to card left, so you cannot free it (at
> least, please not at this point before release and not without prior
> audit). See
> 
> commit cc699535e57e0d0f099090e64a63037c7834f104
> Author: Andrei Borzenkov <arvidjaar@gmail.com>
> Date:   Mon May 4 09:13:53 2015 +0300
> 
>     Revert "efinet: memory leak on module removal"
> 

Thanks. This is sad.

I'll exclude these changes from V3 then.


> 
>>    grub_ieee1275_net_config = 0;
>>  }
>>
> 
> 
> _______________________________________________
> Grub-devel mailing list
> Grub-devel@gnu.org
> https://lists.gnu.org/mailman/listinfo/grub-devel
> 


  reply	other threads:[~2016-12-12  9:57 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-12-02 15:10 [V2] Implementing the receive buffer for ofnet Stanislav Kholmanskikh
2016-12-02 15:10 ` [PATCH V2 1/4] ofnet: add error check for grub_netbuff_reserve Stanislav Kholmanskikh
2016-12-05 15:49   ` Daniel Kiper
2016-12-10 18:08   ` Andrei Borzenkov
2016-12-12 10:34     ` Stanislav Kholmanskikh
2016-12-12 12:10       ` Andrei Borzenkov
2016-12-02 15:10 ` [PATCH V2 2/4] ofnet: move the allocation of the transmit buffer into a function Stanislav Kholmanskikh
2016-12-05 15:52   ` Daniel Kiper
2016-12-12  9:47     ` Stanislav Kholmanskikh
2016-12-12 10:27       ` Andrei Borzenkov
2016-12-12 10:38         ` Stanislav Kholmanskikh
2016-12-02 15:10 ` [PATCH V2 3/4] ofnet: free memory on module unload Stanislav Kholmanskikh
2016-12-05 16:02   ` Daniel Kiper
2016-12-10 18:18   ` Andrei Borzenkov
2016-12-12  9:56     ` Stanislav Kholmanskikh [this message]
2016-12-02 15:10 ` [PATCH V2 4/4] ofnet: implement the receive buffer Stanislav Kholmanskikh
2016-12-05 16:12   ` Daniel Kiper
2016-12-10 18:29   ` Andrei Borzenkov
2016-12-10 19:50     ` Mark Otto

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=584E7469.4010003@oracle.com \
    --to=stanislav.kholmanskikh@oracle.com \
    --cc=grub-devel@gnu.org \
    --cc=vasily.isaenko@oracle.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;
as well as URLs for NNTP newsgroup(s).