linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Kees Cook <keescook@chromium.org>
To: Vlastimil Babka <vbabka@suse.cz>
Cc: Christoph Lameter <cl@linux.com>,
	David Rientjes <rientjes@google.com>,
	Pekka Enberg <penberg@kernel.org>,
	Joonsoo Kim <iamjoonsoo.kim@lge.com>,
	Hyeonggon Yoo <42.hyeyoo@gmail.com>,
	Roman Gushchin <roman.gushchin@linux.dev>,
	linux-mm@kvack.org, patches@lists.linux.dev,
	linux-kernel@vger.kernel.org,
	Matteo Rizzo <matteorizzo@google.com>,
	Jann Horn <jannh@google.com>,
	Andrey Konovalov <andreyknvl@google.com>,
	Marco Elver <elver@google.com>,
	Alexander Potapenko <glider@google.com>,
	kasan-dev@googlegroups.com, linux-hardening@vger.kernel.org
Subject: Re: [PATCH 2/2] mm/slub: remove freelist_dereference()
Date: Tue, 11 Jul 2023 09:21:36 -0700	[thread overview]
Message-ID: <202307110917.DEED145F0@keescook> (raw)
In-Reply-To: <20230711134623.12695-4-vbabka@suse.cz>

On Tue, Jul 11, 2023 at 03:46:25PM +0200, Vlastimil Babka wrote:
> freelist_dereference() is a one-liner only used from get_freepointer().
> Remove it and make get_freepointer() call freelist_ptr_decode()
> directly to make the code easier to follow.
> 
> Signed-off-by: Vlastimil Babka <vbabka@suse.cz>
> ---
>  mm/slub.c | 16 ++++++----------
>  1 file changed, 6 insertions(+), 10 deletions(-)
> 
> diff --git a/mm/slub.c b/mm/slub.c
> index 07edad305512..c4556a5dab4b 100644
> --- a/mm/slub.c
> +++ b/mm/slub.c
> @@ -397,18 +397,14 @@ static inline void *freelist_ptr_decode(const struct kmem_cache *s,
>  	return decoded;
>  }
>  
> -/* Returns the freelist pointer recorded at location ptr_addr. */
> -static inline void *freelist_dereference(const struct kmem_cache *s,
> -					 void *ptr_addr)
> -{
> -	return freelist_ptr_decode(s, *(freeptr_t *)(ptr_addr),
> -			    (unsigned long)ptr_addr);
> -}
> -
>  static inline void *get_freepointer(struct kmem_cache *s, void *object)
>  {
> -	object = kasan_reset_tag(object);
> -	return freelist_dereference(s, (freeptr_t *)(object + s->offset));
> +	unsigned long ptr_addr;
> +	freeptr_t p;
> +
> +	ptr_addr = ((unsigned long)kasan_reset_tag(object)) + s->offset;
> +	p = *(freeptr_t *)(ptr_addr);
> +	return freelist_ptr_decode(s, p, ptr_addr);
>  }
>  
>  #ifndef CONFIG_SLUB_TINY
> -- 
> 2.41.0
> 

I like reducing the complexity here, but I find dropping the "object"
reassignment makes this a bit harder to read. What about:

	object = kasan_reset_tag(object);
	unsigned long ptr_addr = (unsigned long)object + s->offset;
	freeptr_t p = *(freeptr_t *)(ptr_addr);

	return freelist_ptr_decode(s, p, ptr_addr);

?

They're the same result, so either way:

Acked-by: Kees Cook <keescook@chromium.org>

-- 
Kees Cook


  reply	other threads:[~2023-07-11 16:21 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-11 13:46 [PATCH 1/2] mm/slub: remove redundant kasan_reset_tag() from freelist_ptr calculations Vlastimil Babka
2023-07-11 13:46 ` [PATCH 2/2] mm/slub: remove freelist_dereference() Vlastimil Babka
2023-07-11 16:21   ` Kees Cook [this message]
2023-07-13  7:44     ` Vlastimil Babka
2023-07-14  8:01       ` Vlastimil Babka
2023-07-11 16:14 ` [PATCH 1/2] mm/slub: remove redundant kasan_reset_tag() from freelist_ptr calculations Kees Cook
2023-07-12 16:23 ` Andrey Konovalov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=202307110917.DEED145F0@keescook \
    --to=keescook@chromium.org \
    --cc=42.hyeyoo@gmail.com \
    --cc=andreyknvl@google.com \
    --cc=cl@linux.com \
    --cc=elver@google.com \
    --cc=glider@google.com \
    --cc=iamjoonsoo.kim@lge.com \
    --cc=jannh@google.com \
    --cc=kasan-dev@googlegroups.com \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=matteorizzo@google.com \
    --cc=patches@lists.linux.dev \
    --cc=penberg@kernel.org \
    --cc=rientjes@google.com \
    --cc=roman.gushchin@linux.dev \
    --cc=vbabka@suse.cz \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).