linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mm: ksize() should silently accept a NULL pointer
@ 2020-06-16 22:54 William Kucharski
  2020-06-17  0:45 ` Matthew Wilcox
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: William Kucharski @ 2020-06-16 22:54 UTC (permalink / raw)
  To: linux-mm, linux-kernel
  Cc: Christoph Lameter, Pekka Enberg, David Rientjes, Joonsoo Kim,
	Andrew Morton

Other mm routines such as kfree() and kzfree() silently do the right
thing if passed a NULL pointer, so ksize() should do the same.

Signed-off-by: William Kucharski <william.kucharski@oracle.com>
---
 mm/slab_common.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)

diff --git a/mm/slab_common.c b/mm/slab_common.c
index 9e72ba224175..2bff01ad94d8 100644
--- a/mm/slab_common.c
+++ b/mm/slab_common.c
@@ -1660,10 +1660,9 @@ static __always_inline void *__do_krealloc(const void *p, size_t new_size,
 					   gfp_t flags)
 {
 	void *ret;
-	size_t ks = 0;
+	size_t ks;
 
-	if (p)
-		ks = ksize(p);
+	ks = ksize(p);
 
 	if (ks >= new_size) {
 		p = kasan_krealloc((void *)p, new_size, flags);
@@ -1723,10 +1722,9 @@ void kzfree(const void *p)
 	size_t ks;
 	void *mem = (void *)p;
 
-	if (unlikely(ZERO_OR_NULL_PTR(mem)))
-		return;
 	ks = ksize(mem);
-	memset(mem, 0, ks);
+	if (ks)
+		memset(mem, 0, ks);
 	kfree(mem);
 }
 EXPORT_SYMBOL(kzfree);
@@ -1749,8 +1747,6 @@ size_t ksize(const void *objp)
 {
 	size_t size;
 
-	if (WARN_ON_ONCE(!objp))
-		return 0;
 	/*
 	 * We need to check that the pointed to object is valid, and only then
 	 * unpoison the shadow memory below. We use __kasan_check_read(), to
@@ -1764,7 +1760,7 @@ size_t ksize(const void *objp)
 	 * We want to perform the check before __ksize(), to avoid potentially
 	 * crashing in __ksize() due to accessing invalid metadata.
 	 */
-	if (unlikely(objp == ZERO_SIZE_PTR) || !__kasan_check_read(objp, 1))
+	if (unlikely(ZERO_OR_NULL_PTR(objp)) || !__kasan_check_read(objp, 1))
 		return 0;
 
 	size = __ksize(objp);
-- 
2.26.2



^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] mm: ksize() should silently accept a NULL pointer
  2020-06-16 22:54 [PATCH] mm: ksize() should silently accept a NULL pointer William Kucharski
@ 2020-06-17  0:45 ` Matthew Wilcox
  2020-06-17  7:09 ` David Hildenbrand
  2020-06-17 16:23 ` Christopher Lameter
  2 siblings, 0 replies; 5+ messages in thread
From: Matthew Wilcox @ 2020-06-17  0:45 UTC (permalink / raw)
  To: William Kucharski
  Cc: linux-mm, linux-kernel, Christoph Lameter, Pekka Enberg,
	David Rientjes, Joonsoo Kim, Andrew Morton

On Tue, Jun 16, 2020 at 04:54:09PM -0600, William Kucharski wrote:
> Other mm routines such as kfree() and kzfree() silently do the right
> thing if passed a NULL pointer, so ksize() should do the same.
> 
> Signed-off-by: William Kucharski <william.kucharski@oracle.com>

Nice simplification.

Reviewed-by: Matthew Wilcox (Oracle) <willy@infradead.org>


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] mm: ksize() should silently accept a NULL pointer
  2020-06-16 22:54 [PATCH] mm: ksize() should silently accept a NULL pointer William Kucharski
  2020-06-17  0:45 ` Matthew Wilcox
@ 2020-06-17  7:09 ` David Hildenbrand
  2020-06-17 16:23 ` Christopher Lameter
  2 siblings, 0 replies; 5+ messages in thread
From: David Hildenbrand @ 2020-06-17  7:09 UTC (permalink / raw)
  To: William Kucharski, linux-mm, linux-kernel
  Cc: Christoph Lameter, Pekka Enberg, David Rientjes, Joonsoo Kim,
	Andrew Morton

On 17.06.20 00:54, William Kucharski wrote:
> Other mm routines such as kfree() and kzfree() silently do the right
> thing if passed a NULL pointer, so ksize() should do the same.
> 
> Signed-off-by: William Kucharski <william.kucharski@oracle.com>
> ---
>  mm/slab_common.c | 14 +++++---------
>  1 file changed, 5 insertions(+), 9 deletions(-)
> 
> diff --git a/mm/slab_common.c b/mm/slab_common.c
> index 9e72ba224175..2bff01ad94d8 100644
> --- a/mm/slab_common.c
> +++ b/mm/slab_common.c
> @@ -1660,10 +1660,9 @@ static __always_inline void *__do_krealloc(const void *p, size_t new_size,
>  					   gfp_t flags)
>  {
>  	void *ret;
> -	size_t ks = 0;
> +	size_t ks;
>  
> -	if (p)
> -		ks = ksize(p);
> +	ks = ksize(p);
>  
>  	if (ks >= new_size) {
>  		p = kasan_krealloc((void *)p, new_size, flags);
> @@ -1723,10 +1722,9 @@ void kzfree(const void *p)
>  	size_t ks;
>  	void *mem = (void *)p;
>  
> -	if (unlikely(ZERO_OR_NULL_PTR(mem)))
> -		return;
>  	ks = ksize(mem);
> -	memset(mem, 0, ks);
> +	if (ks)
> +		memset(mem, 0, ks);
>  	kfree(mem);
>  }
>  EXPORT_SYMBOL(kzfree);
> @@ -1749,8 +1747,6 @@ size_t ksize(const void *objp)
>  {
>  	size_t size;
>  
> -	if (WARN_ON_ONCE(!objp))
> -		return 0;
>  	/*
>  	 * We need to check that the pointed to object is valid, and only then
>  	 * unpoison the shadow memory below. We use __kasan_check_read(), to
> @@ -1764,7 +1760,7 @@ size_t ksize(const void *objp)
>  	 * We want to perform the check before __ksize(), to avoid potentially
>  	 * crashing in __ksize() due to accessing invalid metadata.
>  	 */
> -	if (unlikely(objp == ZERO_SIZE_PTR) || !__kasan_check_read(objp, 1))
> +	if (unlikely(ZERO_OR_NULL_PTR(objp)) || !__kasan_check_read(objp, 1))
>  		return 0;
>  
>  	size = __ksize(objp);
> 

Reviewed-by: David Hildenbrand <david@redhat.com>

-- 
Thanks,

David / dhildenb



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] mm: ksize() should silently accept a NULL pointer
  2020-06-16 22:54 [PATCH] mm: ksize() should silently accept a NULL pointer William Kucharski
  2020-06-17  0:45 ` Matthew Wilcox
  2020-06-17  7:09 ` David Hildenbrand
@ 2020-06-17 16:23 ` Christopher Lameter
  2020-06-17 16:26   ` Matthew Wilcox
  2 siblings, 1 reply; 5+ messages in thread
From: Christopher Lameter @ 2020-06-17 16:23 UTC (permalink / raw)
  To: William Kucharski
  Cc: linux-mm, linux-kernel, Pekka Enberg, David Rientjes,
	Joonsoo Kim, Andrew Morton

On Tue, 16 Jun 2020, William Kucharski wrote:

> Other mm routines such as kfree() and kzfree() silently do the right
> thing if passed a NULL pointer, so ksize() should do the same.

Ok so the size of an no object pointer is zero? Ignoring the freeing
of a nonexisting object makes sense. But determining it size?


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] mm: ksize() should silently accept a NULL pointer
  2020-06-17 16:23 ` Christopher Lameter
@ 2020-06-17 16:26   ` Matthew Wilcox
  0 siblings, 0 replies; 5+ messages in thread
From: Matthew Wilcox @ 2020-06-17 16:26 UTC (permalink / raw)
  To: Christopher Lameter
  Cc: William Kucharski, linux-mm, linux-kernel, Pekka Enberg,
	David Rientjes, Joonsoo Kim, Andrew Morton

On Wed, Jun 17, 2020 at 04:23:04PM +0000, Christopher Lameter wrote:
> On Tue, 16 Jun 2020, William Kucharski wrote:
> 
> > Other mm routines such as kfree() and kzfree() silently do the right
> > thing if passed a NULL pointer, so ksize() should do the same.
> 
> Ok so the size of an no object pointer is zero? Ignoring the freeing
> of a nonexisting object makes sense. But determining it size?

ksize() is misnamed.  It's not the size of the object, it's the number
of bytes allocated for that object.

A NULL pointer represents a freed object, or one that was never
allocated in the first place.  Clearly that's 0 bytes.  What other
answer would make sense?



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2020-06-17 16:26 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-16 22:54 [PATCH] mm: ksize() should silently accept a NULL pointer William Kucharski
2020-06-17  0:45 ` Matthew Wilcox
2020-06-17  7:09 ` David Hildenbrand
2020-06-17 16:23 ` Christopher Lameter
2020-06-17 16:26   ` Matthew Wilcox

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).