From: Vlastimil Babka <vbabka@suse.cz>
To: Rustam Kovhaev <rkovhaev@gmail.com>,
cl@linux.com, penberg@kernel.org, rientjes@google.com,
iamjoonsoo.kim@lge.com, akpm@linux-foundation.org,
corbet@lwn.net
Cc: djwong@kernel.org, david@fromorbit.com,
linux-kernel@vger.kernel.org, linux-mm@kvack.org,
linux-doc@vger.kernel.org, gregkh@linuxfoundation.org,
viro@zeniv.linux.org.uk, dvyukov@google.com
Subject: Re: [PATCH v2] slob: add size header to all allocations
Date: Mon, 25 Oct 2021 11:36:53 +0200 [thread overview]
Message-ID: <be7ee3a6-9b3c-b436-f042-82bd3c416acc@suse.cz> (raw)
In-Reply-To: <20211023064114.708532-1-rkovhaev@gmail.com>
On 10/23/21 08:41, Rustam Kovhaev wrote:
> Let's prepend both kmalloc() and kmem_cache_alloc() allocations with the
> size header.
> It simplifies the slab API and guarantees that both kmem_cache_alloc()
> and kmalloc() memory could be freed by kfree().
>
> meminfo right after the system boot, without the patch:
> Slab: 35456 kB
>
> the same, with the patch:
> Slab: 36160 kB
>
> Link: https://lore.kernel.org/lkml/20210929212347.1139666-1-rkovhaev@gmail.com
> Signed-off-by: Rustam Kovhaev <rkovhaev@gmail.com>
Seems overal correct to me, thanks! I'll just suggest some improvements:
> ---
> v2:
> - Allocate compound pages in slob_alloc_node()
> - Use slob_free_pages() in kfree()
> - Update documentation
>
> Documentation/core-api/memory-allocation.rst | 4 +-
> mm/slob.c | 114 +++++++++----------
> 2 files changed, 55 insertions(+), 63 deletions(-)
>
> diff --git a/Documentation/core-api/memory-allocation.rst b/Documentation/core-api/memory-allocation.rst
> index 5954ddf6ee13..fea0ed11a7c5 100644
> --- a/Documentation/core-api/memory-allocation.rst
> +++ b/Documentation/core-api/memory-allocation.rst
> @@ -172,5 +172,5 @@ wrappers can allocate memory from that cache.
>
> When the allocated memory is no longer needed it must be freed. You can
> use kvfree() for the memory allocated with `kmalloc`, `vmalloc` and
> -`kvmalloc`. The slab caches should be freed with kmem_cache_free(). And
> -don't forget to destroy the cache with kmem_cache_destroy().
> +`kvmalloc`. The slab caches can be freed with kmem_cache_free() or kvfree().
> +And don't forget to destroy the cache with kmem_cache_destroy().
I would phrase it like this (improves also weird wording "The slab caches
should be freed with..." prior to your patch, etc.):
When the allocated memory is no longer needed it must be freed. Objects
allocated by `kmalloc` can be freed by `kfree` or `kvfree`.
Objects allocated by `kmem_cache_alloc` can be freed with `kmem_cache_free`
or also by `kfree` or `kvfree`.
Memory allocated by `vmalloc` can be freed with `vfree` or `kvfree`.
Memory allocated by `kvmalloc` can be freed with `kvfree`.
Caches created by `kmem_cache_create` should be freed with with
`kmem_cache_destroy`.
> -static void slob_free_pages(void *b, int order)
> +static void slob_free_pages(struct page *sp, int order)
> {
> - struct page *sp = virt_to_page(b);
> -
> - if (current->reclaim_state)
> - current->reclaim_state->reclaimed_slab += 1 << order;
> + if (PageSlab(sp)) {
> + __ClearPageSlab(sp);
> + page_mapcount_reset(sp);
> + if (current->reclaim_state)
> + current->reclaim_state->reclaimed_slab += 1 << order;
> + }
>
> mod_node_page_state(page_pgdat(sp), NR_SLAB_UNRECLAIMABLE_B,
> -(PAGE_SIZE << order));
> @@ -247,9 +244,7 @@ static void *slob_page_alloc(struct page *sp, size_t size, int align,
> /*
> * 'aligned' will hold the address of the slob block so that the
> * address 'aligned'+'align_offset' is aligned according to the
> - * 'align' parameter. This is for kmalloc() which prepends the
> - * allocated block with its size, so that the block itself is
> - * aligned when needed.
> + * 'align' parameter.
> */
> if (align) {
> aligned = (slob_t *)
> @@ -373,25 +368,28 @@ static void *slob_alloc(size_t size, gfp_t gfp, int align, int node,
> }
> if (unlikely(gfp & __GFP_ZERO))
> memset(b, 0, size);
> + /* Write size in the header */
> + *(unsigned int *)b = size - align_offset;
> + b = (void *)b + align_offset;
> return b;
I would just "return (void *)b + align_offset;" here, no need to update 'b'.
> }
>
> /*
> * slob_free: entry point into the slob allocator.
> */
> -static void slob_free(void *block, int size)
> +static void slob_free(void *block)
> {
> struct page *sp;
> - slob_t *prev, *next, *b = (slob_t *)block;
> + int align_offset = max_t(size_t, ARCH_KMALLOC_MINALIGN, ARCH_SLAB_MINALIGN);
This patch adds a number of these in several functions, it was just
__do_kmalloc_node(). It's compile-time constant so I would just #define it
somewhere at the top of slob.c, e.g. something like:
#if ARCH_KMALLOC_MINALIGN < ARCH_SLAB_MINALIGN
#define SLOB_HDR_SIZE ARCH_SLAB_MINALIGN
#else
#define SLOB_HDR_SIZE ARCH_KMALLOC_MINALIGN
#endif
> + void *hdr = block - align_offset;
> + unsigned int size = *(unsigned int *)hdr + align_offset;
> + slob_t *prev, *next, *b = hdr;
IMHO this is too subtle to put in the declaration. I would move these
assignments below the declarations.
That way you can also ditch 'hdr' and just do a 'block -= SLOB_HDR_SIZE;';
> slobidx_t units;
> unsigned long flags;
> struct list_head *slob_list;
>
> - if (unlikely(ZERO_OR_NULL_PTR(block)))
> - return;
> - BUG_ON(!size);
> -
> - sp = virt_to_page(block);
> + BUG_ON(!size || size >= PAGE_SIZE);
> + sp = virt_to_page(hdr);
> units = SLOB_UNITS(size);
>
> spin_lock_irqsave(&slob_lock, flags);
next prev parent reply other threads:[~2021-10-25 9:36 UTC|newest]
Thread overview: 42+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-09-29 21:23 [PATCH] xfs: use kmem_cache_free() for kmem_cache objects Rustam Kovhaev
2021-09-30 4:42 ` Dave Chinner
2021-09-30 8:13 ` Vlastimil Babka
2021-09-30 18:48 ` Rustam Kovhaev
2021-09-30 21:10 ` Vlastimil Babka
2021-10-01 0:32 ` Rustam Kovhaev
2021-10-04 1:07 ` David Rientjes
2021-10-12 20:43 ` Darrick J. Wong
2021-10-12 20:43 ` Darrick J. Wong
2021-10-12 21:32 ` Vlastimil Babka
2021-10-12 23:22 ` Darrick J. Wong
2021-10-13 7:38 ` Vlastimil Babka
2021-10-13 16:56 ` Rustam Kovhaev
2021-10-15 0:57 ` Darrick J. Wong
2021-10-18 3:38 ` [PATCH] slob: add size header to all allocations Rustam Kovhaev
2021-10-18 9:22 ` Vlastimil Babka
2021-10-19 1:22 ` Rustam Kovhaev
2021-10-20 11:46 ` Hyeonggon Yoo
2021-10-21 17:36 ` Vlastimil Babka
2021-10-23 6:41 ` [PATCH v2] " Rustam Kovhaev
2021-10-25 9:36 ` Vlastimil Babka [this message]
2021-10-25 21:49 ` Rustam Kovhaev
2021-10-29 3:05 ` [PATCH v3] " Rustam Kovhaev
2021-11-16 11:26 ` Vlastimil Babka
2021-11-16 23:19 ` Rustam Kovhaev
2021-11-22 1:30 ` [PATCH v4] " Rustam Kovhaev
2021-11-22 9:22 ` Christoph Lameter
2021-11-22 9:40 ` Vlastimil Babka
2021-11-22 10:36 ` Christoph Lameter
2021-11-22 10:45 ` Vlastimil Babka
2021-11-22 11:40 ` Christoph Lameter
2021-11-22 11:49 ` Vlastimil Babka
2021-11-23 10:18 ` David Laight
2021-11-30 7:00 ` Rustam Kovhaev
2021-11-30 9:23 ` David Laight
2021-11-30 9:41 ` Christoph Lameter
2021-11-30 14:55 ` Vlastimil Babka
2021-11-30 15:21 ` David Laight
2021-11-30 15:39 ` Vlastimil Babka
2021-11-30 15:26 ` Christoph Lameter
2021-10-24 10:43 ` [PATCH] " Hyeonggon Yoo
2021-10-25 8:19 ` Vlastimil Babka
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=be7ee3a6-9b3c-b436-f042-82bd3c416acc@suse.cz \
--to=vbabka@suse.cz \
--cc=akpm@linux-foundation.org \
--cc=cl@linux.com \
--cc=corbet@lwn.net \
--cc=david@fromorbit.com \
--cc=djwong@kernel.org \
--cc=dvyukov@google.com \
--cc=gregkh@linuxfoundation.org \
--cc=iamjoonsoo.kim@lge.com \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=penberg@kernel.org \
--cc=rientjes@google.com \
--cc=rkovhaev@gmail.com \
--cc=viro@zeniv.linux.org.uk \
/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