From patchwork Fri Feb 27 18:37:24 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Sterba X-Patchwork-Id: 5902651 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 33AC9BF440 for ; Fri, 27 Feb 2015 18:37:38 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 4BC1A20148 for ; Fri, 27 Feb 2015 18:37:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 18BAE20142 for ; Fri, 27 Feb 2015 18:37:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753405AbbB0Sh1 (ORCPT ); Fri, 27 Feb 2015 13:37:27 -0500 Received: from cantor2.suse.de ([195.135.220.15]:47085 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752158AbbB0Sh0 (ORCPT ); Fri, 27 Feb 2015 13:37:26 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 9C28DABD0; Fri, 27 Feb 2015 18:37:25 +0000 (UTC) Received: by ds.suse.cz (Postfix, from userid 10065) id 46901DAA1B; Fri, 27 Feb 2015 19:37:25 +0100 (CET) From: David Sterba To: linux-btrfs@vger.kernel.org Cc: David Sterba , Anand Jain , Zach Brown Subject: [PATCH] btrfs-progs: use less memory for pretty_size_mode buffers Date: Fri, 27 Feb 2015 19:37:24 +0100 Message-Id: <1425062244-14807-1-git-send-email-dsterba@suse.cz> X-Mailer: git-send-email 2.1.3 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 CC: Zach Brown Signed-off-by: David Sterba --- Anand, please test on a real SPARC machine. utils.c | 18 ++++++++++++++++++ utils.h | 8 ++------ 3 files changed, 21 insertions(+), 6 deletions(-) -- To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html --- 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);