Re: [PATCH v2 5/5] virt: sevguest: Add TSM_REPORTS support for SNP_{GET, GET_EXT}_REPORT
From: Peter Zijlstra
Date: Mon Aug 14 2023 - 18:16:27 EST
On Mon, Aug 14, 2023 at 06:48:04PM +0200, Peter Zijlstra wrote:
> On Mon, Aug 14, 2023 at 09:25:06AM -0700, Dan Williams wrote:
> > Peter Zijlstra wrote:
> > > On Mon, Aug 14, 2023 at 12:43:38AM -0700, Dan Williams wrote:
> > > > +static u8 *sev_report_new(struct device *dev, const struct tsm_desc *desc,
> > >
> > > > + size_t *outblob_len)
> > > > +{
> > >
> > > > +
> > > > + u8 *buf __free(kvfree) = kvzalloc(size, GFP_KERNEL);
> > > > +
> > >
> > > > +
> > > > + *outblob_len = size;
> > > > + no_free_ptr(buf);
> > > > + return buf;
> > >
> > > This seems broken, no_free_ptr(x) is basically xchg(X, NULL) (except no
> > > atomics). So the above would end up being:
> > >
> > > return NULL;
> > >
> > > What you want to write is somehting like:
> > >
> > > return no_free_ptr(buf);
> > >
> > > or, a convenient shorthand:
> > >
> > > return_ptr(buf);
> > >
> >
> > Oh, I indeed did not realize that no_free_ptr() had side effects beyond
> > canceling the free when the variable goes out of scope. Will switch to
> > return_ptr().
>
> Indeed -- ideally no_free_ptr() would be combined with __must_check, but
> I'm not immediately sure how to pull that off. Let me stick that on the
> to-do list.
This seems to actually work. I'll try and write some coherent comments
tomorrow -- it's definitely too late for that now.
diff --git a/include/linux/cleanup.h b/include/linux/cleanup.h
index 53f1a7a932b0..162052cef4c7 100644
--- a/include/linux/cleanup.h
+++ b/include/linux/cleanup.h
@@ -39,8 +39,12 @@
#define __free(_name) __cleanup(__free_##_name)
+static inline __must_check void * __no_free_ptr(void **pp)
+{ void *p = *pp; *pp = NULL; return p; }
+
#define no_free_ptr(p) \
- ({ __auto_type __ptr = (p); (p) = NULL; __ptr; })
+ ((void)__builtin_types_compatible_p(void *, typeof(p)), \
+ ((typeof(p))__no_free_ptr((void **)&(p))))
#define return_ptr(p) return no_free_ptr(p)