linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] btrfs-progs: use less memory for pretty_size_mode buffers
@ 2015-02-27 18:37 David Sterba
  2015-03-06 19:21 ` Anand Jain
  0 siblings, 1 reply; 3+ messages in thread
From: David Sterba @ 2015-02-27 18:37 UTC (permalink / raw)
  To: linux-btrfs; +Cc: David Sterba, Anand Jain, Zach Brown

Anand reports that the static buffers used for pertty size strings cause
a stack overflow on SPARC. Zach proposed to change the printf format to
wrap the number and the suffix into a macro. This would require to
change all callsites of pretty_size* and is not very convienient to
write.

This patch replaces the per-call-site static buffers with a limited
number for slots that would be used on each invokation of pretty_size
and wrap around. The number of array slots shall be 10 for now, in
current codebase there are no more than 2 calls to pretty_size in a
single argument list.

Reported-by: Anand Jain <Anand.Jain@oracle.com>
CC: Zach Brown <zab@zabbo.net>
Signed-off-by: David Sterba <dsterba@suse.cz>
---

Anand, please test on a real SPARC machine.

 utils.c           | 18 ++++++++++++++++++
 utils.h           |  8 ++------
 3 files changed, 21 insertions(+), 6 deletions(-)

--- a/utils.c
+++ b/utils.c
@@ -1366,6 +1366,24 @@ out:
 	return ret;
 }
 
+/*
+ * Note: this function uses a static per-thread buffer. Do not call this
+ * function more than 10 times within one argument list!
+ */
+const char *pretty_size_mode(u64 size, unsigned mode)
+{
+	static __thread int ps_index = 0;
+	static __thread char ps_array[10][32];
+	char *ret;
+
+	ret = ps_array[ps_index];
+	ps_index++;
+	ps_index %= 10;
+	(void)pretty_size_snprintf(size, ret, 32, mode);
+
+	return ret;
+}
+
 static const char* unit_suffix_binary[] =
 	{ "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"};
 static const char* unit_suffix_decimal[] =
diff --git a/utils.h b/utils.h
index 82ab5e82a3ff..539797c705b4 100644
--- a/utils.h
+++ b/utils.h
@@ -103,12 +103,7 @@ int btrfs_device_already_in_root(struct btrfs_root *root, int fd,
 
 int pretty_size_snprintf(u64 size, char *str, size_t str_bytes, unsigned unit_mode);
 #define pretty_size(size) 	pretty_size_mode(size, UNITS_DEFAULT)
-#define pretty_size_mode(size, mode) 					      \
-	({								      \
-		static __thread char _str[32];				      \
-		(void)pretty_size_snprintf((size), _str, sizeof(_str), (mode)); \
-		_str;							      \
-	})
+const char *pretty_size_mode(u64 size, unsigned mode);
 
 int get_mountpt(char *dev, char *mntpt, size_t size);
 int btrfs_scan_block_devices(int run_ioctl);

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

end of thread, other threads:[~2015-03-09 10:38 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-02-27 18:37 [PATCH] btrfs-progs: use less memory for pretty_size_mode buffers David Sterba
2015-03-06 19:21 ` Anand Jain
2015-03-09 10:38   ` David Sterba

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