public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] bcachefs: Avoid a potential memory over-allocation in bch2_printbuf_make_room()
@ 2023-09-16  8:45 Christophe JAILLET
  2023-09-19 13:18 ` Brian Foster
  0 siblings, 1 reply; 5+ messages in thread
From: Christophe JAILLET @ 2023-09-16  8:45 UTC (permalink / raw)
  To: Kent Overstreet, Brian Foster
  Cc: linux-kernel, kernel-janitors, Christophe JAILLET, linux-bcachefs

kmalloc() and co. don't always allocate a power of 2 number of bytes.
There are some special handling for 64<n<=96 and 128<n<=192 cases.

So trust kmalloc() algorithm instead of forcing a power of 2 allocation.
This can saves a few bytes of memory and still make use of all the
memory allocated.

On the other side, it may require an additional realloc() in some cases.

Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
---
 fs/bcachefs/printbuf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fs/bcachefs/printbuf.c b/fs/bcachefs/printbuf.c
index 77bee9060bfe..34527407e950 100644
--- a/fs/bcachefs/printbuf.c
+++ b/fs/bcachefs/printbuf.c
@@ -28,7 +28,7 @@ int bch2_printbuf_make_room(struct printbuf *out, unsigned extra)
 	if (out->pos + extra < out->size)
 		return 0;
 
-	new_size = roundup_pow_of_two(out->size + extra);
+	new_size = kmalloc_size_roundup(out->size + extra);
 
 	/*
 	 * Note: output buffer must be freeable with kfree(), it's not required
-- 
2.34.1


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

end of thread, other threads:[~2023-09-19 19:21 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-09-16  8:45 [PATCH] bcachefs: Avoid a potential memory over-allocation in bch2_printbuf_make_room() Christophe JAILLET
2023-09-19 13:18 ` Brian Foster
2023-09-19 18:34   ` Christophe JAILLET
2023-09-19 18:46     ` Brian Foster
2023-09-19 19:21     ` Kent Overstreet

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox