All of lore.kernel.org
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: Stefano Garzarella <sgarzare@redhat.com>, qemu-devel@nongnu.org
Cc: Peter Maydell <peter.maydell@linaro.org>,
	Eduardo Habkost <ehabkost@redhat.com>,
	Sergio Lopez <slp@redhat.com>,
	"Michael S. Tsirkin" <mst@redhat.com>,
	Julio Montes <julio.montes@intel.com>,
	"Dr . David Alan Gilbert" <dgilbert@redhat.com>,
	Richard Henderson <rth@twiddle.net>
Subject: Re: [Qemu-devel] [PATCH v2 1/2] elf-ops.h: Map into memory the ELF to load
Date: Tue, 23 Jul 2019 16:33:44 +0200	[thread overview]
Message-ID: <e35bc4db-238e-6a31-3725-57c5825cb0a0@redhat.com> (raw)
In-Reply-To: <20190723140445.12748-2-sgarzare@redhat.com>

On 23/07/19 16:04, Stefano Garzarella wrote:
> +                    /* Increments the reference count to avoid the unmap */
> +                    g_mapped_file_ref(gmf);
>                      /* rom_add_elf_program() seize the ownership of 'data' */
>                      rom_add_elf_program(label, data, file_size, mem_size,
>                                          addr, as);

I'm a bit worried about rom_reset g_free'ing rom->data, which goes
against the comment on top of rom_free:

/* rom->data must be heap-allocated (do not use with
   rom_add_elf_program()) */


Since this is the only call to rom_add_elf_program, what about adding a
GMappedFile* field to struct Rom and passing it here instead of
data+file_size?

Then the g_mapped_file_ref can be in rom_add_elf_program, and you can
have a nice

static void rom_free_data(Rom *rom)
{
    if (rom->mapped_file) {
        g_mapped_file_unref(rom->mapped_file);
        rom->mapped_file = NULL;
    } else {
        g_free(rom->data);
    }
    rom->data = NULL;
}

that is called from both rom_free and rom_reset.

Thanks,

Paolo

> @@ -531,7 +540,6 @@ static int glue(load_elf, SZ)(const char *name, int fd,
>                      address_space_write(as ? as : &address_space_memory,
>                                          addr, MEMTXATTRS_UNSPECIFIED,
>                                          data, file_size);
> -                    g_free(data);
>                  }
>              }
>  
> @@ -547,16 +555,15 @@ static int glue(load_elf, SZ)(const char *name, int fd,
>              struct elf_note *nhdr = NULL;
>  
>              file_size = ph->p_filesz; /* Size of the range of ELF notes */
> -            data = g_malloc0(file_size);
> -            if (ph->p_filesz > 0) {
> -                if (lseek(fd, ph->p_offset, SEEK_SET) < 0) {
> -                    goto fail;
> -                }
> -                if (read(fd, data, file_size) != file_size) {
> -                    goto fail;
> -                }
> +            data_offset = ph->p_offset; /* Offset where the notes are located */
> +
> +            if (g_mapped_file_get_length(gmf) < file_size + data_offset) {
> +                goto fail;
>              }
>  
> +            data = (uint8_t *)g_mapped_file_get_contents(gmf);
> +            data += data_offset;
> +
>              /*
>               * Search the ELF notes to find one with a type matching the
>               * value passed in via 'translate_opaque'
> @@ -570,7 +577,6 @@ static int glue(load_elf, SZ)(const char *name, int fd,
>                      sizeof(struct elf_note) == sizeof(struct elf64_note);
>                  elf_note_fn((void *)nhdr, (void *)&ph->p_align, is64);
>              }
> -            g_free(data);
>              data = NULL;
>          }
>      }
> @@ -582,7 +588,7 @@ static int glue(load_elf, SZ)(const char *name, int fd,
>          *highaddr = (uint64_t)(elf_sword)high;
>      return total_size;
>   fail:
> -    g_free(data);
> +    g_mapped_file_unref(gmf);
>      g_free(phdr);
>      return ret;
>  }
> 



  reply	other threads:[~2019-07-23 14:34 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-07-23 14:04 [Qemu-devel] [PATCH v2 0/2] pc: mmap kernel (ELF image) and initrd Stefano Garzarella
2019-07-23 14:04 ` [Qemu-devel] [PATCH v2 1/2] elf-ops.h: Map into memory the ELF to load Stefano Garzarella
2019-07-23 14:33   ` Paolo Bonzini [this message]
2019-07-23 14:57     ` Stefano Garzarella
2019-07-23 15:00       ` Paolo Bonzini
2019-07-23 14:04 ` [Qemu-devel] [PATCH v2 2/2] hw/i386/pc: Map into memory the initrd Stefano Garzarella
2019-07-23 14:30   ` Paolo Bonzini
2019-07-23 14:47     ` Stefano Garzarella
2019-07-23 17:37 ` [Qemu-devel] [PATCH v2 0/2] pc: mmap kernel (ELF image) and initrd Montes, Julio
2019-07-24  7:36   ` Stefano Garzarella
2019-07-24 13:03     ` Montes, Julio
2019-07-24 13:25       ` Stefano Garzarella

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=e35bc4db-238e-6a31-3725-57c5825cb0a0@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=dgilbert@redhat.com \
    --cc=ehabkost@redhat.com \
    --cc=julio.montes@intel.com \
    --cc=mst@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    --cc=rth@twiddle.net \
    --cc=sgarzare@redhat.com \
    --cc=slp@redhat.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 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.