Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

ntb: reduce stack usage in idt_scan_mws

idt_scan_mws() puts a large fixed-size array on the stack and copies
it into a smaller dynamically allocated array at the end. On 32-bit
targets, the fixed size can easily exceed the warning limit for
possible stack overflow:

drivers/ntb/hw/idt/ntb_hw_idt.c:1041:27: error: stack frame size (1032) exceeds limit (1024) in 'idt_scan_mws' [-Werror,-Wframe-larger-than]

Change it to instead just always use dynamic allocation for the
array from the start. It's too big for the stack, but not actually
all that much for a permanent allocation.

Reported-by: kernel test robot <lkp@intel.com>
Closes: https://lore.kernel.org/all/202205111109.PiKTruEj-lkp@intel.com/
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Reviewed-by: Dave Jiang <dave.jiang@intel.com>
Reviewed-by: Damien Le Moal <dlemoal@kernel.org>
Signed-off-by: Jon Mason <jdmason@kudzu.us>

authored by

Arnd Bergmann and committed by
Jon Mason
aff12700 fd5625fc

+7 -11
+7 -11
drivers/ntb/hw/idt/ntb_hw_idt.c
··· 1041 1041 static struct idt_mw_cfg *idt_scan_mws(struct idt_ntb_dev *ndev, int port, 1042 1042 unsigned char *mw_cnt) 1043 1043 { 1044 - struct idt_mw_cfg mws[IDT_MAX_NR_MWS], *ret_mws; 1044 + struct idt_mw_cfg *mws; 1045 1045 const struct idt_ntb_bar *bars; 1046 1046 enum idt_mw_type mw_type; 1047 1047 unsigned char widx, bidx, en_cnt; 1048 1048 bool bar_64bit = false; 1049 1049 int aprt_size; 1050 1050 u32 data; 1051 + 1052 + mws = devm_kcalloc(&ndev->ntb.pdev->dev, IDT_MAX_NR_MWS, 1053 + sizeof(*mws), GFP_KERNEL); 1054 + if (!mws) 1055 + return ERR_PTR(-ENOMEM); 1051 1056 1052 1057 /* Retrieve the array of the BARs registers */ 1053 1058 bars = portdata_tbl[port].bars; ··· 1108 1103 } 1109 1104 } 1110 1105 1111 - /* Allocate memory for memory window descriptors */ 1112 - ret_mws = devm_kcalloc(&ndev->ntb.pdev->dev, *mw_cnt, sizeof(*ret_mws), 1113 - GFP_KERNEL); 1114 - if (!ret_mws) 1115 - return ERR_PTR(-ENOMEM); 1116 - 1117 - /* Copy the info of detected memory windows */ 1118 - memcpy(ret_mws, mws, (*mw_cnt)*sizeof(*ret_mws)); 1119 - 1120 - return ret_mws; 1106 + return mws; 1121 1107 } 1122 1108 1123 1109 /*