linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] xfs: use kmem_cache_free() for kmem_cache objects
@ 2021-09-29 21:23 Rustam Kovhaev
  2021-09-30  4:42 ` Dave Chinner
  0 siblings, 1 reply; 42+ messages in thread
From: Rustam Kovhaev @ 2021-09-29 21:23 UTC (permalink / raw)
  To: djwong, linux-xfs, cl, penberg, rientjes, iamjoonsoo.kim, akpm, vbabka
  Cc: linux-kernel, linux-mm, gregkh, Rustam Kovhaev

For kmalloc() allocations SLOB prepends the blocks with a 4-byte header,
and it puts the size of the allocated blocks in that header.
Blocks allocated with kmem_cache_alloc() allocations do not have that
header.

SLOB explodes when you allocate memory with kmem_cache_alloc() and then
try to free it with kfree() instead of kmem_cache_free().
SLOB will assume that there is a header when there is none, read some
garbage to size variable and corrupt the adjacent objects, which
eventually leads to hang or panic.

Let's make XFS work with SLOB by using proper free function.

Fixes: 9749fee83f38 ("xfs: enable the xfs_defer mechanism to process extents to free")
Signed-off-by: Rustam Kovhaev <rkovhaev@gmail.com>
---
 fs/xfs/xfs_extfree_item.c | 6 +++---
 mm/slob.c                 | 6 ++++--
 2 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/fs/xfs/xfs_extfree_item.c b/fs/xfs/xfs_extfree_item.c
index 3f8a0713573a..a4b8caa2c601 100644
--- a/fs/xfs/xfs_extfree_item.c
+++ b/fs/xfs/xfs_extfree_item.c
@@ -482,7 +482,7 @@ xfs_extent_free_finish_item(
 			free->xefi_startblock,
 			free->xefi_blockcount,
 			&free->xefi_oinfo, free->xefi_skip_discard);
-	kmem_free(free);
+	kmem_cache_free(xfs_bmap_free_item_zone, free);
 	return error;
 }
 
@@ -502,7 +502,7 @@ xfs_extent_free_cancel_item(
 	struct xfs_extent_free_item	*free;
 
 	free = container_of(item, struct xfs_extent_free_item, xefi_list);
-	kmem_free(free);
+	kmem_cache_free(xfs_bmap_free_item_zone, free);
 }
 
 const struct xfs_defer_op_type xfs_extent_free_defer_type = {
@@ -564,7 +564,7 @@ xfs_agfl_free_finish_item(
 	extp->ext_len = free->xefi_blockcount;
 	efdp->efd_next_extent++;
 
-	kmem_free(free);
+	kmem_cache_free(xfs_bmap_free_item_zone, free);
 	return error;
 }
 
diff --git a/mm/slob.c b/mm/slob.c
index 74d3f6e60666..d2d859ded5f8 100644
--- a/mm/slob.c
+++ b/mm/slob.c
@@ -389,7 +389,6 @@ static void slob_free(void *block, int size)
 
 	if (unlikely(ZERO_OR_NULL_PTR(block)))
 		return;
-	BUG_ON(!size);
 
 	sp = virt_to_page(block);
 	units = SLOB_UNITS(size);
@@ -556,6 +555,7 @@ void kfree(const void *block)
 	if (PageSlab(sp)) {
 		int align = max_t(size_t, ARCH_KMALLOC_MINALIGN, ARCH_SLAB_MINALIGN);
 		unsigned int *m = (unsigned int *)(block - align);
+		BUG_ON(!*m || *m > (PAGE_SIZE - align));
 		slob_free(m, *m + align);
 	} else {
 		unsigned int order = compound_order(sp);
@@ -649,8 +649,10 @@ EXPORT_SYMBOL(kmem_cache_alloc_node);
 
 static void __kmem_cache_free(void *b, int size)
 {
-	if (size < PAGE_SIZE)
+	if (size < PAGE_SIZE) {
+		BUG_ON(!size);
 		slob_free(b, size);
+	}
 	else
 		slob_free_pages(b, get_order(size));
 }
-- 
2.30.2


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

end of thread, other threads:[~2021-11-30 15:39 UTC | newest]

Thread overview: 42+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
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

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).