[v2,4/5] mm/slab: Add __free() support for kvfree

Message ID 169199901230.1782217.9803098171993981037.stgit@dwillia2-xfh.jf.intel.com
State New
Headers
Series tsm: Attestation Report ABI |

Commit Message

Dan Williams Aug. 14, 2023, 7:43 a.m. UTC
  Allow for the declaration of variables that trigger kvfree() when they
go out of scope.

Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Dan Williams <dan.j.williams@intel.com>
---
 include/linux/slab.h |    2 ++
 1 file changed, 2 insertions(+)
  

Comments

Greg KH Aug. 14, 2023, 3:31 p.m. UTC | #1
On Mon, Aug 14, 2023 at 12:43:32AM -0700, Dan Williams wrote:
> Allow for the declaration of variables that trigger kvfree() when they
> go out of scope.
> 
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Cc: Peter Zijlstra <peterz@infradead.org>
> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> Signed-off-by: Dan Williams <dan.j.williams@intel.com>
> ---
>  include/linux/slab.h |    2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/include/linux/slab.h b/include/linux/slab.h
> index 848c7c82ad5a..241025367943 100644
> --- a/include/linux/slab.h
> +++ b/include/linux/slab.h
> @@ -746,6 +746,8 @@ static inline __alloc_size(1, 2) void *kvcalloc(size_t n, size_t size, gfp_t fla
>  extern void *kvrealloc(const void *p, size_t oldsize, size_t newsize, gfp_t flags)
>  		      __realloc_size(3);
>  extern void kvfree(const void *addr);
> +DEFINE_FREE(kvfree, void *, if (_T) kvfree(_T))

No need to check _T before calling this, right (as was also pointed out
earlier).

thanks,

greg k-h
  
Lukas Wunner Jan. 4, 2024, 6:57 a.m. UTC | #2
On Mon, Aug 14, 2023 at 06:17:31PM +0200, Peter Zijlstra wrote:
> On Mon, Aug 14, 2023 at 05:31:27PM +0200, Greg Kroah-Hartman wrote:
> > On Mon, Aug 14, 2023 at 12:43:32AM -0700, Dan Williams wrote:
> > > Allow for the declaration of variables that trigger kvfree() when they
> > > go out of scope.
> > > 
> > > Cc: Andrew Morton <akpm@linux-foundation.org>
> > > Cc: Peter Zijlstra <peterz@infradead.org>
> > > Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> > > Signed-off-by: Dan Williams <dan.j.williams@intel.com>
> > > ---
> > >  include/linux/slab.h |    2 ++
> > >  1 file changed, 2 insertions(+)
> > > 
> > > diff --git a/include/linux/slab.h b/include/linux/slab.h
> > > index 848c7c82ad5a..241025367943 100644
> > > --- a/include/linux/slab.h
> > > +++ b/include/linux/slab.h
> > > @@ -746,6 +746,8 @@ static inline __alloc_size(1, 2) void *kvcalloc(size_t n, size_t size, gfp_t fla
> > >  extern void *kvrealloc(const void *p, size_t oldsize, size_t newsize, gfp_t flags)
> > >  		      __realloc_size(3);
> > >  extern void kvfree(const void *addr);
> > > +DEFINE_FREE(kvfree, void *, if (_T) kvfree(_T))
> > 
> > No need to check _T before calling this, right (as was also pointed out
> > earlier).
> 
> Well, that does mean you get an unconditional call to kvfree() in the
> success case. Linus argued against this.
> 
> This way the compiler sees:
> 
> 	buf = NULL;
> 	if (buf)
> 		kvfree(buf);
> 
> and goes: 'let me clean that up for you'. And all is well.

Have you actually verified that assumption in the generated Assembler code?

The kernel is compiled with -fno-delete-null-pointer-checks since commit
a3ca86aea507 ("Add '-fno-delete-null-pointer-checks' to gcc CFLAGS").

So NULL pointer checks are *not* optimized away even if the compiler
knows that a pointer is NULL.

Background story:
https://lwn.net/Articles/342330/

Thanks,

Lukas
  
Dan Williams Jan. 4, 2024, 6:29 p.m. UTC | #3
Lukas Wunner wrote:
> On Mon, Aug 14, 2023 at 06:17:31PM +0200, Peter Zijlstra wrote:
> > On Mon, Aug 14, 2023 at 05:31:27PM +0200, Greg Kroah-Hartman wrote:
> > > On Mon, Aug 14, 2023 at 12:43:32AM -0700, Dan Williams wrote:
> > > > Allow for the declaration of variables that trigger kvfree() when they
> > > > go out of scope.
> > > > 
> > > > Cc: Andrew Morton <akpm@linux-foundation.org>
> > > > Cc: Peter Zijlstra <peterz@infradead.org>
> > > > Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> > > > Signed-off-by: Dan Williams <dan.j.williams@intel.com>
> > > > ---
> > > >  include/linux/slab.h |    2 ++
> > > >  1 file changed, 2 insertions(+)
> > > > 
> > > > diff --git a/include/linux/slab.h b/include/linux/slab.h
> > > > index 848c7c82ad5a..241025367943 100644
> > > > --- a/include/linux/slab.h
> > > > +++ b/include/linux/slab.h
> > > > @@ -746,6 +746,8 @@ static inline __alloc_size(1, 2) void *kvcalloc(size_t n, size_t size, gfp_t fla
> > > >  extern void *kvrealloc(const void *p, size_t oldsize, size_t newsize, gfp_t flags)
> > > >  		      __realloc_size(3);
> > > >  extern void kvfree(const void *addr);
> > > > +DEFINE_FREE(kvfree, void *, if (_T) kvfree(_T))
> > > 
> > > No need to check _T before calling this, right (as was also pointed out
> > > earlier).
> > 
> > Well, that does mean you get an unconditional call to kvfree() in the
> > success case. Linus argued against this.
> > 
> > This way the compiler sees:
> > 
> > 	buf = NULL;
> > 	if (buf)
> > 		kvfree(buf);
> > 
> > and goes: 'let me clean that up for you'. And all is well.
> 
> Have you actually verified that assumption in the generated Assembler code?
> 
> The kernel is compiled with -fno-delete-null-pointer-checks since commit
> a3ca86aea507 ("Add '-fno-delete-null-pointer-checks' to gcc CFLAGS").
> 
> So NULL pointer checks are *not* optimized away even if the compiler
> knows that a pointer is NULL.

Interesting, I am not sure how -fno-delete-null-pointer-checks plays
into this, but I can confirm that Peter's expectations are being met in
a routine with:

DEFINE_FREE(pci_dev_put, struct pci_dev *, if (_T) pci_dev_put(_T))

...without that conditional the assembly is:

   0xffffffff819ad129 <+41>:	call   0xffffffff81800840 <pci_get_domain_bus_and_slot>
   0xffffffff819ad12e <+46>:	mov    %rax,%r12
   0xffffffff819ad131 <+49>:	test   %rax,%rax
   0xffffffff819ad134 <+52>:	je     0xffffffff819ad154 <cxl_cper_event_call+84>
   0xffffffff819ad136 <+54>:	mov    %rax,%rdi
   0xffffffff819ad139 <+57>:	call   0xffffffff817f5f10 <pci_dev_lock>
   0xffffffff819ad13e <+62>:	cmpq   $0xffffffff82c681c0,0x80(%r12)
   0xffffffff819ad14a <+74>:	je     0xffffffff819ad160 <cxl_cper_event_call+96>
   0xffffffff819ad14c <+76>:	mov    %r12,%rdi
   0xffffffff819ad14f <+79>:	call   0xffffffff817f5fa0 <pci_dev_unlock>
   0xffffffff819ad154 <+84>:	pop    %rbx
   0xffffffff819ad155 <+85>:	mov    %r12,%rdi
   0xffffffff819ad158 <+88>:	pop    %rbp
   0xffffffff819ad159 <+89>:	pop    %r12
   0xffffffff819ad15b <+91>:	jmp    0xffffffff817fe1e0 <pci_dev_put>

...i.e. the check for NULL at 0xffffffff819ad134 jumps to do an
unnecessary pci_dev_put(). With the conditional in the macro the
sequence is:

   0xffffffff819ad129 <+41>:	call   0xffffffff81800840 <pci_get_domain_bus_and_slot>
   0xffffffff819ad12e <+46>:	test   %rax,%rax
   0xffffffff819ad131 <+49>:	je     0xffffffff819ad18c <cxl_cper_event_call+140>
   0xffffffff819ad133 <+51>:	mov    %rax,%r12
   0xffffffff819ad136 <+54>:	mov    %rax,%rdi
   0xffffffff819ad139 <+57>:	call   0xffffffff817f5f10 <pci_dev_lock>
   0xffffffff819ad13e <+62>:	cmpq   $0xffffffff82c681c0,0x80(%r12)
   0xffffffff819ad14a <+74>:	je     0xffffffff819ad160 <cxl_cper_event_call+96>
   0xffffffff819ad14c <+76>:	mov    %r12,%rdi
   0xffffffff819ad14f <+79>:	call   0xffffffff817f5fa0 <pci_dev_unlock>
   ...
   0xffffffff819ad18c <+140>:	pop    %rbx
   0xffffffff819ad18d <+141>:	pop    %rbp
   0xffffffff819ad18e <+142>:	pop    %r12
   0xffffffff819ad190 <+144>:	jmp    0xffffffff81efc6a0 <__x86_return_thunk>

...i.e. optimize away the pci_dev_put() and return directly when @pdev
is already known to be NULL. So empirically
-fno-delete-null-pointer-checks still allows for redundant NULL checks
to be optimized.
  

Patch

diff --git a/include/linux/slab.h b/include/linux/slab.h
index 848c7c82ad5a..241025367943 100644
--- a/include/linux/slab.h
+++ b/include/linux/slab.h
@@ -746,6 +746,8 @@  static inline __alloc_size(1, 2) void *kvcalloc(size_t n, size_t size, gfp_t fla
 extern void *kvrealloc(const void *p, size_t oldsize, size_t newsize, gfp_t flags)
 		      __realloc_size(3);
 extern void kvfree(const void *addr);
+DEFINE_FREE(kvfree, void *, if (_T) kvfree(_T))
+
 extern void kvfree_sensitive(const void *addr, size_t len);
 
 unsigned int kmem_cache_size(struct kmem_cache *s);