Skip to contentSkip to conversation

mtd: cfi_cmdset_0001: Factor out do_write_buffer_locked() to reduce stack frame

PATCH Superseded Andy Shevchenko 4 participants 12 messages Feb 4 – Apr 8 Memory technology devices (MTD)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 mtd: cfi_cmdset_0001: Factor out do_write_buffer_locked() to reduce stack frame

Compiler is not happy about used stack frame:

drivers/mtd/chips/cfi_cmdset_0001.c: In function 'do_write_buffer':
drivers/mtd/chips/cfi_cmdset_0001.c:1887:1: error: the frame size of 1296 bytes is larger than 1280 bytes [-Werror=frame-larger-than=]

Fix this by factoring out do_write_buffer_locked().

---
v2: kept DIS/ENABLE_VPP paired

 drivers/mtd/chips/cfi_cmdset_0001.c | 82 +++++++++++++++++------------
 1 file changed, 48 insertions(+), 34 deletions(-)
drivers/mtd/chips/cfi_cmdset_0001.c +48 -34
index c10693ba265b..c495bee04053 100644
@@ -1720,42 +1720,24 @@ static int cfi_intelext_write_words (struct mtd_info *mtd, loff_t to , size_t le
 }
 
 
-static int __xipram do_write_buffer(struct map_info *map, struct flchip *chip,
-				    unsigned long adr, const struct kvec **pvec,
-				    unsigned long *pvec_seek, int len)
+static int __xipram do_write_buffer_locked(struct map_info *map, struct flchip *chip,
+					   unsigned long cmd_adr, unsigned long adr,
+					   const struct kvec **pvec,
+					   unsigned long *pvec_seek, int len)
 {
 	struct cfi_private *cfi = map->fldrv_priv;
 	map_word status, write_cmd, datum;
-	unsigned long cmd_adr;
-	int ret, wbufsize, word_gap, words;
+	int ret, word_gap, words;
 	const struct kvec *vec;
 	unsigned long vec_seek;
 	unsigned long initial_adr;
 	int initial_len = len;
 
-	wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
-	adr += chip->start;
 	initial_adr = adr;
-	cmd_adr = adr & ~(wbufsize-1);
-
-	/* Sharp LH28F640BF chips need the first address for the
-	 * Page Buffer Program command. See Table 5 of
-	 * LH28F320BF, LH28F640BF, LH28F128BF Series (Appendix FUM00701) */
-	if (is_LH28F640BF(cfi))
-		cmd_adr = adr;
 
 	/* Let's determine this according to the interleave only once */
 	Mwrite_cmd = (cfi->cfiq->P_ID != P_ID_INTEL_PERFORMANCE) ? CMD(0xe8) : CMD(0xe9);
 
-	mutex_lock(&chip->mutex);
-	ret = get_chip(map, chip, cmd_adr, FL_WRITING);
-	if (ret) {
-		mutex_unlock(&chip->mutex);
-		return ret;
-	}
-
-	XIP_INVAL_CACHED_RANGE(map, initial_adr, initial_len);
-	ENABLE_VPP(map);
 	xip_disable(map, chip, cmd_adr);
 
 	/* §4.8 of the 28FxxxJ3A datasheet says "Any time SR.4 and/or SR.5 is set
@@ -1789,7 +1771,7 @@ static int __xipram do_write_buffer(struct map_info *map, struct flchip *chip,
 		xip_enable(map, chip, cmd_adr);
 		printk(KERN_ERR "%s: Chip not ready for buffer write. Xstatus = %lx, status = %lx\n",
 				map->name, Xstatus.x[0], status.x[0]);
-		goto out;
+		return ret;
 	}
 
 	/* Figure out the number of words to write */
@@ -1853,7 +1835,7 @@ static int __xipram do_write_buffer(struct map_info *map, struct flchip *chip,
 		chip->state = FL_STATUS;
 		xip_enable(map, chip, cmd_adr);
 		printk(KERN_ERR "%s: buffer write error (status timeout)\n", map->name);
-		goto out;
+		return ret;
 	}
 
 	/* check for errors */
@@ -1866,21 +1848,53 @@ static int __xipram do_write_buffer(struct map_info *map, struct flchip *chip,
 		map_write(map, CMD(0x70), cmd_adr);
 		xip_enable(map, chip, cmd_adr);
 
-		if (chipstatus & 0x02) {
-			ret = -EROFS;
-		} else if (chipstatus & 0x08) {
+		if (chipstatus & 0x02)
+			return -EROFS;
+
+		if (chipstatus & 0x08) {
 			printk(KERN_ERR "%s: buffer write error (bad VPP)\n", map->name);
-			ret = -EIO;
-		} else {
-			printk(KERN_ERR "%s: buffer write error (status 0x%lx)\n", map->name, chipstatus);
-			ret = -EINVAL;
+			return  -EIO;
 		}
 
-		goto out;
+		printk(KERN_ERR "%s: buffer write error (status 0x%lx)\n", map->name, chipstatus);
+		return -EINVAL;
 	}
 
 	xip_enable(map, chip, cmd_adr);
- out:	DISABLE_VPP(map);
+	return 0;
+}
+
+static int __xipram do_write_buffer(struct map_info *map, struct flchip *chip,
+				    unsigned long adr, const struct kvec **pvec,
+				    unsigned long *pvec_seek, int len)
+{
+	struct cfi_private *cfi = map->fldrv_priv;
+	unsigned long cmd_adr;
+	int ret, wbufsize;
+
+	wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
+	adr += chip->start;
+	cmd_adr = adr & ~(wbufsize - 1);
+
+	/* Sharp LH28F640BF chips need the first address for the
+	 * Page Buffer Program command. See Table 5 of
+	 * LH28F320BF, LH28F640BF, LH28F128BF Series (Appendix FUM00701) */
+	if (is_LH28F640BF(cfi))
+		cmd_adr = adr;
+
+	mutex_lock(&chip->mutex);
+	ret = get_chip(map, chip, cmd_adr, FL_WRITING);
+	if (ret) {
+		mutex_unlock(&chip->mutex);
+		return ret;
+	}
+
+	XIP_INVAL_CACHED_RANGE(map, adr, len);
+	ENABLE_VPP(map);
+
+	ret = do_write_buffer_locked(map, chip, cmd_adr, adr, pvec, pvec_seek, len);
+
+	DISABLE_VPP(map);
 	put_chip(map, chip, cmd_adr);
 	mutex_unlock(&chip->mutex);
 	return ret;
Signature · 1 line
2.50.1
#1 lore raw
Quoted 6 lines · Andy Shevchenko

Compiler is not happy about used stack frame:

drivers/mtd/chips/cfi_cmdset_0001.c: In function 'do_write_buffer':
drivers/mtd/chips/cfi_cmdset_0001.c:1887:1: error: the frame size of 1296 bytes is larger than 1280 bytes [-Werror=frame-larger-than=]

Fix this by factoring out do_write_buffer_locked().

My gosh, I already sent a v2 earlier and there were some replies, I need to check...

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#2 lore raw

Hi Andy,

Quoted 10 lines · Andy Shevchenko
Quoted 6 lines · Andy Shevchenko

Compiler is not happy about used stack frame:

drivers/mtd/chips/cfi_cmdset_0001.c: In function 'do_write_buffer':
drivers/mtd/chips/cfi_cmdset_0001.c:1887:1: error: the frame size of 1296 bytes is larger than 1280 bytes [-Werror=frame-larger-than=]

Fix this by factoring out do_write_buffer_locked().

My gosh, I already sent a v2 earlier and there were some replies, I need to check...

I'm not sure I get what you mean here? I got no reply (in my inbox) to your v2. I am letting a bit of time for Vignesh to take a look, but I know he's busy so I might take it nevertheless by the end of the week as we are approaching the merge window.

Cheers,
Miquèl

#3 lore raw
Quoted 9 lines · Miquel Raynal
Quoted 8 lines · Andy Shevchenko
Quoted 6 lines · Andy Shevchenko

Compiler is not happy about used stack frame:

drivers/mtd/chips/cfi_cmdset_0001.c: In function 'do_write_buffer':
drivers/mtd/chips/cfi_cmdset_0001.c:1887:1: error: the frame size of 1296 bytes is larger than 1280 bytes [-Werror=frame-larger-than=]

Fix this by factoring out do_write_buffer_locked().

Quoted 7 lines
Quoted 2 lines

My gosh, I already sent a v2 earlier and there were some replies, I need to check...

I'm not sure I get what you mean here? I got no reply (in my inbox) to your v2. I am letting a bit of time for Vignesh to take a look, but I know he's busy so I might take it nevertheless by the end of the week as we are approaching the merge window.

I got some strange LKP reports, I need to check them. Please, discard this email thread as we have it there

https://lore.kernel.org/r/[email protected]

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#4 lore raw
Quoted 18 lines · Andy Shevchenko
Quoted 9 lines · Miquel Raynal
Quoted 8 lines · Andy Shevchenko
Quoted 6 lines · Andy Shevchenko

Compiler is not happy about used stack frame:

drivers/mtd/chips/cfi_cmdset_0001.c: In function 'do_write_buffer':
drivers/mtd/chips/cfi_cmdset_0001.c:1887:1: error: the frame size of 1296 bytes is larger than 1280 bytes [-Werror=frame-larger-than=]

Fix this by factoring out do_write_buffer_locked().

Quoted 7 lines
Quoted 2 lines

My gosh, I already sent a v2 earlier and there were some replies, I need to check...

I'm not sure I get what you mean here? I got no reply (in my inbox) to your v2. I am letting a bit of time for Vignesh to take a look, but I know he's busy so I might take it nevertheless by the end of the week as we are approaching the merge window.

Quoted 4 lines

I got some strange LKP reports, I need to check them. Please, discard this email thread as we have it there

https://lore.kernel.org/r/[email protected]

Ah, this did not reach my inbox, marked SPAM... I'll discard this patch for this cycle, feel free to resend once the report has been figured out.

Thanks,
Miquèl

#5 lore raw
Quoted 20 lines · Miquel Raynal
Quoted 18 lines · Andy Shevchenko
Quoted 16 lines · Miquel Raynal
Quoted 10 lines · Andy Shevchenko
Quoted 6 lines · Andy Shevchenko

Compiler is not happy about used stack frame:

drivers/mtd/chips/cfi_cmdset_0001.c: In function 'do_write_buffer':
drivers/mtd/chips/cfi_cmdset_0001.c:1887:1: error: the frame size of 1296 bytes is larger than 1280 bytes [-Werror=frame-larger-than=]

Fix this by factoring out do_write_buffer_locked().

My gosh, I already sent a v2 earlier and there were some replies, I need to check...

I'm not sure I get what you mean here? I got no reply (in my inbox) to your v2. I am letting a bit of time for Vignesh to take a look, but I know he's busy so I might take it nevertheless by the end of the week as we are approaching the merge window.

Quoted 9 lines
Quoted 4 lines

I got some strange LKP reports, I need to check them. Please, discard this email thread as we have it there

https://lore.kernel.org/r/[email protected]

Ah, this did not reach my inbox, marked SPAM... I'll discard this patch for this cycle, feel free to resend once the report has been figured out.

Yeah, things become tricky with CONFIG_MTD_XIP enabled. With XIP_INVAL_CACHED_RANGE() moved to caller, initial_adr becomes unused when CONFIG_MTD_XIP=y

One solution maybe to move calls to XIP_INVAL_CACHED_RANGE(), ENABLE_VPP(map) and xip_disable() and their complementary functions to do_write_buffer_locked() as these calls need to repeated for every writes (if and when we find need to reuse do_write_buffer_locked())

Alternately move INVAL_CACHE_AND_WAIT() and everything after that to caller

Signature · 3 lines
Regards
Vignesh
https://ti.com/opensource
#6 lore raw
Quoted 33 lines · Vignesh Raghavendra
Quoted 19 lines · Miquel Raynal
Quoted 18 lines · Andy Shevchenko
Quoted 16 lines · Miquel Raynal
Quoted 10 lines · Andy Shevchenko
Quoted 6 lines · Andy Shevchenko

Compiler is not happy about used stack frame:

drivers/mtd/chips/cfi_cmdset_0001.c: In function 'do_write_buffer':
drivers/mtd/chips/cfi_cmdset_0001.c:1887:1: error: the frame size of 1296 bytes is larger than 1280 bytes [-Werror=frame-larger-than=]

Fix this by factoring out do_write_buffer_locked().

My gosh, I already sent a v2 earlier and there were some replies, I need to check...

I'm not sure I get what you mean here? I got no reply (in my inbox) to your v2. I am letting a bit of time for Vignesh to take a look, but I know he's busy so I might take it nevertheless by the end of the week as we are approaching the merge window.

Quoted 8 lines
Quoted 4 lines

I got some strange LKP reports, I need to check them. Please, discard this email thread as we have it there

https://lore.kernel.org/r/[email protected]

Ah, this did not reach my inbox, marked SPAM... I'll discard this patch for this cycle, feel free to resend once the report has been figured out.

Yeah, things become tricky with CONFIG_MTD_XIP enabled. With XIP_INVAL_CACHED_RANGE() moved to caller, initial_adr becomes unused when CONFIG_MTD_XIP=y

Quoted 6 lines

One solution maybe to move calls to XIP_INVAL_CACHED_RANGE(), ENABLE_VPP(map) and xip_disable() and their complementary functions to do_write_buffer_locked() as these calls need to repeated for every writes (if and when we find need to reuse do_write_buffer_locked())

Alternately move INVAL_CACHE_AND_WAIT() and everything after that to caller

Thanks for the analysis! Can you copy this as the reply to the LKP report? I don't want to have the report and analysis in different threads, I may forgot about this one (and actually I closed this thread for myself).

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#7 lore raw
In reply to Andy Shevchenko
Quoted 6 lines · Andy Shevchenko

Compiler is not happy about used stack frame:

drivers/mtd/chips/cfi_cmdset_0001.c: In function 'do_write_buffer':
drivers/mtd/chips/cfi_cmdset_0001.c:1887:1: error: the frame size of 1296 bytes is larger than 1280 bytes [-Werror=frame-larger-than=]

Fix this by factoring out do_write_buffer_locked().

FWIW, the issue only occurs with CONFIG_KASAN_STACK=y, so an alternative to refactoring the function is to disable KASAN for this particular file:

drivers/mtd/chips/Makefile +5 -0
index 1f4e84f1cd88..7110eb97bd25 100644
@@ -14,3 +14,8 @@ obj-$(CONFIG_MTD_JEDECPROBE)	+= jedec_probe.o
 obj-$(CONFIG_MTD_RAM)		+= map_ram.o
 obj-$(CONFIG_MTD_ROM)		+= map_rom.o
 obj-$(CONFIG_MTD_ABSENT)	+= map_absent.o
+
+# Avoid exceeding stack frame in do_write_buffer()
+ifeq ($(ARCH),arm)
+KASAN_SANITIZE_cfi_cmdset_0001.o := n
+endif
#8 lore raw
Quoted 10 lines · Lukas Wunner
Quoted 6 lines · Andy Shevchenko

Compiler is not happy about used stack frame:

drivers/mtd/chips/cfi_cmdset_0001.c: In function 'do_write_buffer':
drivers/mtd/chips/cfi_cmdset_0001.c:1887:1: error: the frame size of 1296 bytes is larger than 1280 bytes [-Werror=frame-larger-than=]

Fix this by factoring out do_write_buffer_locked().

FWIW, the issue only occurs with CONFIG_KASAN_STACK=y, so an alternative to refactoring the function is to disable KASAN for this particular file:

I would like to avoid such big hammers.

Somehow I lost the track of this patch, I need to find it locally and check the status, I will come up with updated one once I found the latest version.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#9 lore raw
Quoted 9 lines · Andy Shevchenko
Quoted 8 lines · Lukas Wunner
Quoted 6 lines · Andy Shevchenko

Compiler is not happy about used stack frame:

drivers/mtd/chips/cfi_cmdset_0001.c: In function 'do_write_buffer':
drivers/mtd/chips/cfi_cmdset_0001.c:1887:1: error: the frame size of 1296 bytes is larger than 1280 bytes [-Werror=frame-larger-than=]

Fix this by factoring out do_write_buffer_locked().

Quoted 7 lines
Quoted 2 lines

FWIW, the issue only occurs with CONFIG_KASAN_STACK=y, so an alternative to refactoring the function is to disable KASAN for this particular file:

I would like to avoid such big hammers.

Somehow I lost the track of this patch, I need to find it locally and check the status, I will come up with updated one once I found the latest version.

I just wanted to highlight that it's a KASAN-specific issue that doesn't show up unless CONFIG_KASAN_STACK=y. I think it's worth mentioning that in the commit message.

I'm not saying disabling KASAN is the right approach here, but it's one *possible* approach. Ideally the number of variables on the stack should be reduced. Perhaps alocating them on the heap would be more appropriate than refactoring the code, I don't know.

Thanks,

Lukas

#10 lore raw
Quoted 13 lines · Lukas Wunner
Quoted 12 lines · Andy Shevchenko
Quoted 10 lines · Lukas Wunner
Quoted 6 lines · Andy Shevchenko

Compiler is not happy about used stack frame:

drivers/mtd/chips/cfi_cmdset_0001.c: In function 'do_write_buffer':
drivers/mtd/chips/cfi_cmdset_0001.c:1887:1: error: the frame size of 1296 bytes is larger than 1280 bytes [-Werror=frame-larger-than=]

Fix this by factoring out do_write_buffer_locked().

FWIW, the issue only occurs with CONFIG_KASAN_STACK=y, so an alternative to refactoring the function is to disable KASAN for this particular file:

Quoted 8 lines
Quoted 4 lines

I would like to avoid such big hammers.

Somehow I lost the track of this patch, I need to find it locally and check the status, I will come up with updated one once I found the latest version.

I just wanted to highlight that it's a KASAN-specific issue that doesn't show up unless CONFIG_KASAN_STACK=y. I think it's worth mentioning that in the commit message.

Sure, makes sense.

Quoted 4 lines

I'm not saying disabling KASAN is the right approach here, but it's one *possible* approach. Ideally the number of variables on the stack should be reduced. Perhaps alocating them on the heap would be more appropriate than refactoring the code, I don't know.

Thanks, Lukas, for reviewing and proposing alternatives!

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#11 lore raw
Quoted 16 lines · Andy Shevchenko
Quoted 15 lines · Lukas Wunner
Quoted 14 lines · Andy Shevchenko
Quoted 10 lines · Lukas Wunner
Quoted 6 lines · Andy Shevchenko

Compiler is not happy about used stack frame:

drivers/mtd/chips/cfi_cmdset_0001.c: In function 'do_write_buffer':
drivers/mtd/chips/cfi_cmdset_0001.c:1887:1: error: the frame size of 1296 bytes is larger than 1280 bytes [-Werror=frame-larger-than=]

Fix this by factoring out do_write_buffer_locked().

FWIW, the issue only occurs with CONFIG_KASAN_STACK=y, so an alternative to refactoring the function is to disable KASAN for this particular file:

I would like to avoid such big hammers.

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Somehow I lost the track of this patch, I need to find it locally and check the status, I will come up with updated one once I found the latest version.

I just wanted to highlight that it's a KASAN-specific issue that doesn't show up unless CONFIG_KASAN_STACK=y. I think it's worth mentioning that in the commit message.

Sure, makes sense.

Sorry, I forgot this in v3. If you still think it's crucial to have, I will wait for MTD maintainers and update later on, but please, comment on v3, so I won't forget it again (as it was two email threads for v2 and I missed one to check carefully for the requests).

Quoted 6 lines
Quoted 4 lines

I'm not saying disabling KASAN is the right approach here, but it's one *possible* approach. Ideally the number of variables on the stack should be reduced. Perhaps alocating them on the heap would be more appropriate than refactoring the code, I don't know.

Thanks, Lukas, for reviewing and proposing alternatives!

FWIW, v3 just has been sent: [email protected]

Signature · 2 lines
With Best Regards,
Andy Shevchenko