<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Apr 27, 2021 at 12:25 PM Dr. David Alan Gilbert <<a href="mailto:dgilbert@redhat.com">dgilbert@redhat.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">* Mahmoud Mandour (<a href="mailto:ma.mandourr@gmail.com" target="_blank">ma.mandourr@gmail.com</a>) wrote:<br>
> Replaced the calls to malloc()/calloc() and their respective<br>
> calls to free() of iovec structs with GLib's allocation and<br>
> deallocation functions.<br>
> <br>
> Also, in one instance, used g_new0() instead of a calloc() call plus<br>
> a null-checking assertion.<br>
> <br>
> iovec structs were created locally and freed as the function<br>
> ends. Hence, I used g_autofree and removed the respective calls to<br>
> free().<br>
> <br>
> In one instance, a struct fuse_ioctl_iovec pointer is returned from a<br>
> function, namely, fuse_ioctl_iovec_copy. There, I used g_steal_pointer()<br>
> in conjunction with g_autofree, this gives the ownership of the pointer<br>
> to the calling function and still auto-frees the memory when the calling<br>
> function finishes (maintaining the symantics of previous code).<br>
> <br>
> Signed-off-by: Mahmoud Mandour <<a href="mailto:ma.mandourr@gmail.com" target="_blank">ma.mandourr@gmail.com</a>><br>
> Reviewed-by: Stefan Hajnoczi <<a href="mailto:stefanha@redhat.com" target="_blank">stefanha@redhat.com</a>><br>
> ---<br>
>  tools/virtiofsd/fuse_lowlevel.c | 19 +++++++------------<br>
>  tools/virtiofsd/fuse_virtio.c   |  6 +-----<br>
>  2 files changed, 8 insertions(+), 17 deletions(-)<br>
> <br>
> diff --git a/tools/virtiofsd/fuse_lowlevel.c b/tools/virtiofsd/fuse_lowlevel.c<br>
> index 812cef6ef6..f965299ad9 100644<br>
> --- a/tools/virtiofsd/fuse_lowlevel.c<br>
> +++ b/tools/virtiofsd/fuse_lowlevel.c<br>
> @@ -217,9 +217,9 @@ static int send_reply(fuse_req_t req, int error, const void *arg,<br>
>  int fuse_reply_iov(fuse_req_t req, const struct iovec *iov, int count)<br>
>  {<br>
>      int res;<br>
> -    struct iovec *padded_iov;<br>
> +    g_autofree struct iovec *padded_iov;<br>
>  <br>
> -    padded_iov = malloc((count + 1) * sizeof(struct iovec));<br>
> +    padded_iov = g_try_new(struct iovec, count + 1);<br>
>      if (padded_iov == NULL) {<br>
>          return fuse_reply_err(req, ENOMEM);<br>
>      }<br>
> @@ -228,7 +228,6 @@ int fuse_reply_iov(fuse_req_t req, const struct iovec *iov, int count)<br>
>      count++;<br>
>  <br>
>      res = send_reply_iov(req, 0, padded_iov, count);<br>
> -    free(padded_iov);<br>
>  <br>
>      return res;<br>
>  }<br>
<br>
OK.<br>
<br>
> @@ -565,10 +564,10 @@ int fuse_reply_bmap(fuse_req_t req, uint64_t idx)<br>
>  static struct fuse_ioctl_iovec *fuse_ioctl_iovec_copy(const struct iovec *iov,<br>
>                                                        size_t count)<br>
>  {<br>
> -    struct fuse_ioctl_iovec *fiov;<br>
> +    g_autofree struct fuse_ioctl_iovec *fiov;<br>
>      size_t i;<br>
>  <br>
> -    fiov = malloc(sizeof(fiov[0]) * count);<br>
> +    fiov = g_try_new(fuse_ioctl_iovec, count);<br>
>      if (!fiov) {<br>
>          return NULL;<br>
>      }<br>
> @@ -578,7 +577,7 @@ static struct fuse_ioctl_iovec *fuse_ioctl_iovec_copy(const struct iovec *iov,<br>
>          fiov[i].len = iov[i].iov_len;<br>
>      }<br>
>  <br>
> -    return fiov;<br>
> +    return g_steal_pointer(&fiov);<br>
>  }<br>
<br>
This is OK, but doesn't gain anything - marking it as g_autofree'ing and<br>
always stealing is no benefit.<br>
<br>
>  <br>
>  int fuse_reply_ioctl_retry(fuse_req_t req, const struct iovec *in_iov,<br>
> @@ -629,9 +628,6 @@ int fuse_reply_ioctl_retry(fuse_req_t req, const struct iovec *in_iov,<br>
>  <br>
>      res = send_reply_iov(req, 0, iov, count);<br>
>  out:<br>
> -    free(in_fiov);<br>
> -    free(out_fiov);<br>
> -<br>
<br>
I don't think you can do that - I think you're relying here on the<br>
g_autofree from fuse_ioclt_iovec_copy - but my understanding is that<br>
doesn't work; g_autofree is scoped, so it's designed to free at the end<br>
of fuse_ioctl_iovec_copy, fuse_reply_ioctl_retry doesn't know that the<br>
ion_fiov were allocated that way, so it won't get autocleaned up.<br>
<br></blockquote><div><br></div><div>In GLib's documentation, it is clarified (w.r.t. g_autoptr but I think similar logic applies to g_autofree)</div><div>that g_steal_pointer() "This can be very useful when combined with g_autoptr() to prevent </div><div>the return value of a function from being automatically freed."</div><div>I think, but not 100% clear of course, that this means that the g_autoptr-annotated memory</div><div>does not get freed at the end of the current scope, and  its "scope" is migrated to the calling</div><div>function(to be honest I don't know how would they implement that but maybe this is the case).</div><div>Otherwise why bother with g_autoptr'ing memory that we don't want to free automatically and</div><div>would like to return to the calling function?</div><div><br></div><div>The first example in <a href="https://developer.gnome.org/glib/stable/glib-Memory-Allocation.html#g-steal-pointer">Memory Allocation: GLib Reference Manual (gnome.org)</a> does annotate</div><div>the memory as g_autoptr and then returns it through g_steal_pointer. With your logic, I think that</div><div>this example would be wrong(?)</div><div><br></div><div>Mr. Hajnoczi already reviewed this patch  <a href="https://lists.gnu.org/archive/html/qemu-devel/2021-03/msg08459.html">Re: [PATCH 2/8] virtiofds: Changed allocations of iovec to GLib's functi</a></div><div>in a previous version and this v2 patch series is supposed to only contain already-reviewed patches and</div><div>remove bad ones</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
>      return res;<br>
>  <br>
>  enomem:<br>
> @@ -663,11 +659,11 @@ int fuse_reply_ioctl(fuse_req_t req, int result, const void *buf, size_t size)<br>
>  int fuse_reply_ioctl_iov(fuse_req_t req, int result, const struct iovec *iov,<br>
>                           int count)<br>
>  {<br>
> -    struct iovec *padded_iov;<br>
> +    g_autofree struct iovec *padded_iov;<br>
>      struct fuse_ioctl_out arg;<br>
>      int res;<br>
>  <br>
> -    padded_iov = malloc((count + 2) * sizeof(struct iovec));<br>
> +    padded_iov = g_try_new(struct iovec, count + 2);<br>
>      if (padded_iov == NULL) {<br>
>          return fuse_reply_err(req, ENOMEM);<br>
>      }<br>
> @@ -680,7 +676,6 @@ int fuse_reply_ioctl_iov(fuse_req_t req, int result, const struct iovec *iov,<br>
>      memcpy(&padded_iov[2], iov, count * sizeof(struct iovec));<br>
>  <br>
>      res = send_reply_iov(req, 0, padded_iov, count + 2);<br>
> -    free(padded_iov);<br>
>  <br>
>      return res;<br>
>  }<br>
<br>
OK<br>
<br>
> diff --git a/tools/virtiofsd/fuse_virtio.c b/tools/virtiofsd/fuse_virtio.c<br>
> index 3e13997406..07e5d91a9f 100644<br>
> --- a/tools/virtiofsd/fuse_virtio.c<br>
> +++ b/tools/virtiofsd/fuse_virtio.c<br>
> @@ -347,8 +347,7 @@ int virtio_send_data_iov(struct fuse_session *se, struct fuse_chan *ch,<br>
>       * Build a copy of the the in_sg iov so we can skip bits in it,<br>
>       * including changing the offsets<br>
>       */<br>
> -    struct iovec *in_sg_cpy = calloc(sizeof(struct iovec), in_num);<br>
> -    assert(in_sg_cpy);<br>
> +    g_autofree struct iovec *in_sg_cpy = g_new0(struct iovec, in_num);<br>
>      memcpy(in_sg_cpy, in_sg, sizeof(struct iovec) * in_num);<br>
>      /* These get updated as we skip */<br>
>      struct iovec *in_sg_ptr = in_sg_cpy;<br>
> @@ -386,7 +385,6 @@ int virtio_send_data_iov(struct fuse_session *se, struct fuse_chan *ch,<br>
>              ret = errno;<br>
>              fuse_log(FUSE_LOG_DEBUG, "%s: preadv failed (%m) len=%zd\n",<br>
>                       __func__, len);<br>
> -            free(in_sg_cpy);<br>
>              goto err;<br>
>          }<br>
>          fuse_log(FUSE_LOG_DEBUG, "%s: preadv ret=%d len=%zd\n", __func__,<br>
> @@ -410,13 +408,11 @@ int virtio_send_data_iov(struct fuse_session *se, struct fuse_chan *ch,<br>
>          if (ret != len) {<br>
>              fuse_log(FUSE_LOG_DEBUG, "%s: ret!=len\n", __func__);<br>
>              ret = EIO;<br>
> -            free(in_sg_cpy);<br>
>              goto err;<br>
>          }<br>
>          in_sg_left -= ret;<br>
>          len -= ret;<br>
>      } while (in_sg_left);<br>
> -    free(in_sg_cpy);<br>
<br>
Yes, this is where the autofree really helps; getting rid of a few<br>
free's.<br>
<br>
Dave<br>
<br>
>      /* Need to fix out->len on EOF */<br>
>      if (len) {<br>
> -- <br>
> 2.25.1<br>
> <br>
-- <br>
Dr. David Alan Gilbert / <a href="mailto:dgilbert@redhat.com" target="_blank">dgilbert@redhat.com</a> / Manchester, UK<br>
<br></blockquote><div><br></div><div>Thanks,</div><div>Mahmoud </div></div></div></div></div></div>