<div dir="ltr"><div dir="ltr"><div dir="ltr"><br></div><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 1:33 PM Dr. David Alan Gilbert <<a href="mailto:dgilbert@redhat.com" target="_blank">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>
> On Tue, Apr 27, 2021 at 1:01 PM Dr. David Alan Gilbert <<a href="mailto:dgilbert@redhat.com" target="_blank">dgilbert@redhat.com</a>><br>
> wrote:<br>
> <br>
> > * Mahmoud Mandour (<a href="mailto:ma.mandourr@gmail.com" target="_blank">ma.mandourr@gmail.com</a>) wrote:<br>
> > > On Tue, Apr 27, 2021 at 12:25 PM Dr. David Alan Gilbert <<br>
> > <a href="mailto:dgilbert@redhat.com" target="_blank">dgilbert@redhat.com</a>><br>
> > > wrote:<br>
> > ><br>
> > > > * 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<br>
> > g_steal_pointer()<br>
> > > > > in conjunction with g_autofree, this gives the ownership of the<br>
> > pointer<br>
> > > > > to the calling function and still auto-frees the memory when the<br>
> > 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<br>
> > > > 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,<br>
> > > > const void *arg,<br>
> > > > >  int fuse_reply_iov(fuse_req_t req, const struct iovec *iov, int<br>
> > 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<br>
> > > > 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<br>
> > idx)<br>
> > > > >  static struct fuse_ioctl_iovec *fuse_ioctl_iovec_copy(const struct<br>
> > > > 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<br>
> > > > *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<br>
> > and<br>
> > > > always stealing is no benefit.<br>
> > > ><br>
> > > > ><br>
> > > > >  int fuse_reply_ioctl_retry(fuse_req_t req, const struct iovec<br>
> > *in_iov,<br>
> > > > > @@ -629,9 +628,6 @@ int fuse_reply_ioctl_retry(fuse_req_t req, const<br>
> > > > 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>
> > > ><br>
> > > In GLib's documentation, it is clarified (w.r.t. g_autoptr but I think<br>
> > > similar logic applies to g_autofree)<br>
> > > that g_steal_pointer() "This can be very useful when combined with<br>
> > > g_autoptr() to prevent<br>
> > > the return value of a function from being automatically freed."<br>
> > > I think, but not 100% clear of course, that this means that the<br>
> > > g_autoptr-annotated memory<br>
> > > does not get freed at the end of the current scope, and  its "scope" is<br>
> > > migrated to the calling<br>
> > > function(to be honest I don't know how would they implement that but<br>
> > maybe<br>
> > > this is the case).<br>
> > > Otherwise why bother with g_autoptr'ing memory that we don't want to free<br>
> > > automatically and<br>
> > > would like to return to the calling function?<br>
> > ><br>
> > > The first example in Memory Allocation: GLib Reference Manual (<a href="http://gnome.org" rel="noreferrer" target="_blank">gnome.org</a><br>
> > )<br>
> > > <<br>
> > <a href="https://developer.gnome.org/glib/stable/glib-Memory-Allocation.html#g-steal-pointer" rel="noreferrer" target="_blank">https://developer.gnome.org/glib/stable/glib-Memory-Allocation.html#g-steal-pointer</a><br>
> > ><br>
> > > does<br>
> > > annotate<br>
> > > the memory as g_autoptr and then returns it through g_steal_pointer. With<br>
> > > your logic, I think that<br>
> > > this example would be wrong(?)<br>
> ><br>
> > The example is correct but not quite the case you have;  the<br>
> > g_steal_pointer stops the g_autoptr freeing it at the end of the current<br>
> > scope; but it doesn't cause it to be free'd later - the caller can't<br>
> > tell that the function that did the allocation had a g_autofree in it;<br>
> > once you get outside of the function, the pointer is just a normal<br>
> > pointer that needs free or g_free on.<br>
> ><br>
> > I think that this is logical, yes. I think that I understand now. Can you<br>
> please instruct<br>
> me on what to do with the patch? Do you want me to resend the entire patch<br>
> series<br>
> and amend this one?<br>
<br>
Just resend this one as a '[PATCH v3 2/7]'<br>
<br>
Dave<br>
<br>
> <br>
> ><br>
> > > Mr. Hajnoczi already reviewed this patch  Re: [PATCH 2/8] virtiofds:<br>
> > > Changed allocations of iovec to GLib's functi<br>
> > > <<a href="https://lists.gnu.org/archive/html/qemu-devel/2021-03/msg08459.html" rel="noreferrer" target="_blank">https://lists.gnu.org/archive/html/qemu-devel/2021-03/msg08459.html</a>><br>
> > > in a previous version and this v2 patch series is supposed to only<br>
> > contain<br>
> > > already-reviewed patches and<br>
> > > remove bad ones<br>
> ><br>
> > But he didn't spot this particular problem.<br>
> ><br>
> > Dave<br>
> ><br>
> > ><br>
> > > > >      return res;<br>
> > > > ><br>
> > > > >  enomem:<br>
> > > > > @@ -663,11 +659,11 @@ int fuse_reply_ioctl(fuse_req_t req, int<br>
> > result,<br>
> > > > const void *buf, size_t size)<br>
> > > > >  int fuse_reply_ioctl_iov(fuse_req_t req, int result, const struct<br>
> > iovec<br>
> > > > *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<br>
> > result,<br>
> > > > 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<br>
> > > > 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,<br>
> > > > 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,<br>
> > 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,<br>
> > > > struct fuse_chan *ch,<br>
> > > > >              ret = errno;<br>
> > > > >              fuse_log(FUSE_LOG_DEBUG, "%s: preadv failed (%m)<br>
> > 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",<br>
> > > > __func__,<br>
> > > > > @@ -410,13 +408,11 @@ int virtio_send_data_iov(struct fuse_session<br>
> > *se,<br>
> > > > 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>
> > > ><br>
> > > Thanks,<br>
> > > Mahmoud<br>
> > --<br>
> > Dr. David Alan Gilbert / <a href="mailto:dgilbert@redhat.com" target="_blank">dgilbert@redhat.com</a> / Manchester, UK<br>
> ><br>
> ><br>
> Thanks,<br>
> Mahmoud<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>I sent a v3 of this patch, and I used the --in-reply-to and put the message-id of your latest </div><div>email but despite this, it was sent as a separate thread, I apologise. That's the link of the thread </div><div><a href="https://lists.gnu.org/archive/html/qemu-devel/2021-04/msg05559.html">[PATCH v3 2/7] virtiofsd: Changed allocations of iovec to GLib's functio (gnu.org)</a></div><div>and if you think that it'd better be here, I'll send it again manually as a reply to this mailing series.</div><div><br></div><div>Mahmoud</div></div></div>
</div></div>