Skip to contentSkip to conversation

powerpc: use string library

PATCH New Akinobu Mita 4 participants 14 messages Jan 27, 2012 – Feb 1, 2012

Subsystems (7)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 powerpc: use string library

- Use memchr_inv to check if the data contains all 0xFF bytes. It is faster than looping for each byte.

- Use memcmp to compare memory areas

---
 arch/powerpc/platforms/powermac/nvram.c |   42 ++++++++++++++-----------------
 1 files changed, 19 insertions(+), 23 deletions(-)
arch/powerpc/platforms/powermac/nvram.c +19 -23
index 54d2271..da18b26 100644
@@ -279,7 +279,7 @@ static u32 core99_check(u8* datas)
 
 static int sm_erase_bank(int bank)
 {
-	int stat, i;
+	int stat;
 	unsigned long timeout;
 
 	u8 __iomem *base = (u8 __iomem *)nvram_data + core99_bank*NVRAM_SIZE;
@@ -301,11 +301,10 @@ static int sm_erase_bank(int bank)
 	out_8(base, SM_FLASH_CMD_CLEAR_STATUS);
 	out_8(base, SM_FLASH_CMD_RESET);
 
-	for (i=0; i<NVRAM_SIZE; i++)
-		if (base[i] != 0xff) {
-			printk(KERN_ERR "nvram: Sharp/Micron flash erase failed !\n");
-			return -ENXIO;
-		}
+	if (memchr_inv(base, 0xff, NVRAM_SIZE)) {
+		printk(KERN_ERR "nvram: Sharp/Micron flash erase failed !\n");
+		return -ENXIO;
+	}
 	return 0;
 }
 
@@ -336,17 +335,16 @@ static int sm_write_bank(int bank, u8* datas)
 	}
 	out_8(base, SM_FLASH_CMD_CLEAR_STATUS);
 	out_8(base, SM_FLASH_CMD_RESET);
-	for (i=0; i<NVRAM_SIZE; i++)
-		if (base[i] != datas[i]) {
-			printk(KERN_ERR "nvram: Sharp/Micron flash write failed !\n");
-			return -ENXIO;
-		}
+	if (memcmp(base, datas, NVRAM_SIZE)) {
+		printk(KERN_ERR "nvram: Sharp/Micron flash write failed !\n");
+		return -ENXIO;
+	}
 	return 0;
 }
 
 static int amd_erase_bank(int bank)
 {
-	int i, stat = 0;
+	int stat = 0;
 	unsigned long timeout;
 
 	u8 __iomem *base = (u8 __iomem *)nvram_data + core99_bank*NVRAM_SIZE;
@@ -382,12 +380,11 @@ static int amd_erase_bank(int bank)
 	/* Reset */
 	out_8(base, 0xf0);
 	udelay(1);
-	
-	for (i=0; i<NVRAM_SIZE; i++)
-		if (base[i] != 0xff) {
-			printk(KERN_ERR "nvram: AMD flash erase failed !\n");
-			return -ENXIO;
-		}
+
+	if (memchr_inv(base, 0xff, NVRAM_SIZE)) {
+		printk(KERN_ERR "nvram: AMD flash erase failed !\n");
+		return -ENXIO;
+	}
 	return 0;
 }
 
@@ -429,11 +426,10 @@ static int amd_write_bank(int bank, u8* datas)
 	out_8(base, 0xf0);
 	udelay(1);
 
-	for (i=0; i<NVRAM_SIZE; i++)
-		if (base[i] != datas[i]) {
-			printk(KERN_ERR "nvram: AMD flash write failed !\n");
-			return -ENXIO;
-		}
+	if (memcmp(base, datas, NVRAM_SIZE)) {
+		printk(KERN_ERR "nvram: AMD flash write failed !\n");
+		return -ENXIO;
+	}
 	return 0;
 }
 
Signature · 1 line
1.7.4.4
#1 lore raw

PATCH 1/1 mtd/nand: use string library

- Use memchr_inv to check if the data contains all 0xFF bytes. It is faster than looping for each byte.

- Use memcmp to compare memory areas

Cc Jiandong Zheng <[email protected]>
Cc Scott Branden <[email protected]>
Cc David Woodhouse <[email protected]>
---
 drivers/mtd/nand/davinci_nand.c |    7 +++----
 drivers/mtd/nand/denali.c       |    7 +++----
 drivers/mtd/nand/diskonchip.c   |   18 +++---------------
 drivers/mtd/nand/nand_bbt.c     |   16 +++++-----------
 drivers/mtd/nand/nand_bcm_umi.c |   10 +++-------
 5 files changed, 17 insertions(+), 41 deletions(-)
drivers/mtd/nand/davinci_nand.c +3 -4
index 6e56615..423a7cc 100644
@@ -315,10 +315,9 @@ static int nand_davinci_correct_4bit(struct mtd_info *mtd,
 	unsigned long timeo;
 
 	/* All bytes 0xff?  It's an erased page; ignore its ECC. */
-	for (i = 0; i < 10; i++) {
-		if (ecc_code[i] != 0xff)
-			goto compare;
-	}
+	if (memchr_inv(ecc_code, 0xff, 10))
+		goto compare;
+
 	return 0;
 
 compare:
drivers/mtd/nand/denali.c +3 -4
index 3984d48..a87e3a4 100644
@@ -908,10 +908,9 @@ static void read_oob_data(struct mtd_info *mtd, uint8_t *buf, int page)
  */
 bool is_erased(uint8_t *buf, int len)
 {
-	int i = 0;
-	for (i = 0; i < len; i++)
-		if (buf[i] != 0xFF)
-			return false;
+	if (memchr_inv(buf, 0xFF, len))
+		return false;
+
 	return true;
 }
 #define ECC_SECTOR_SIZE 512
drivers/mtd/nand/diskonchip.c +3 -15
index df921e7..bf0602e 100644
@@ -945,12 +945,8 @@ static int doc200x_calculate_ecc(struct mtd_info *mtd, const u_char *dat, unsign
 		/* Note: this somewhat expensive test should not be triggered
 		   often.  It could be optimized away by examining the data in
 		   the writebuf routine, and remembering the result. */
-		for (i = 0; i < 512; i++) {
-			if (dat[i] == 0xff)
-				continue;
+		if (memchr_inv(dat, 0xff, 512))
 			emptymatch = 0;
-			break;
-		}
 	}
 	/* If emptymatch still =1, we do have an all-0xff data buffer.
 	   Return all-0xff ecc value instead of the computed one, so
@@ -1000,24 +996,16 @@ static int doc200x_correct_data(struct mtd_info *mtd, u_char *dat,
 		/* If emptymatch=1, the read syndrome is consistent with an
 		   all-0xff data and stored ecc block.  Check the stored ecc. */
 		if (emptymatch) {
-			for (i = 0; i < 6; i++) {
-				if (read_ecc[i] == 0xff)
-					continue;
+			if (memchr_inv(read_ecc, 0xff, 6))
 				emptymatch = 0;
-				break;
-			}
 		}
 		/* If emptymatch still =1, check the data block. */
 		if (emptymatch) {
 			/* Note: this somewhat expensive test should not be triggered
 			   often.  It could be optimized away by examining the data in
 			   the readbuf routine, and remembering the result. */
-			for (i = 0; i < 512; i++) {
-				if (dat[i] == 0xff)
-					continue;
+			if (memchr_inv(dat, 0xff, 512))
 				emptymatch = 0;
-				break;
-			}
 		}
 		/* If emptymatch still =1, this is almost certainly a freshly-
 		   erased block, in which case the ECC will not come out right.
drivers/mtd/nand/nand_bbt.c +5 -11
index 20a112f..a1b2dd5 100644
@@ -100,10 +100,8 @@ static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_desc
 
 	end = paglen + td->offs;
 	if (td->options & NAND_BBT_SCANEMPTY) {
-		for (i = 0; i < end; i++) {
-			if (p[i] != 0xff)
-				return -1;
-		}
+		if (memchr_inv(p, 0xff, end))
+			return -1;
 	}
 	p += end;
 
@@ -133,14 +131,10 @@ static int check_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_desc
  */
 static int check_short_pattern(uint8_t *buf, struct nand_bbt_descr *td)
 {
-	int i;
-	uint8_t *p = buf;
-
 	/* Compare the pattern */
-	for (i = 0; i < td->len; i++) {
-		if (p[td->offs + i] != td->pattern[i])
-			return -1;
-	}
+	if (memcmp(buf + td->offs, td->pattern, td->len))
+		return -1;
+
 	return 0;
 }
 
drivers/mtd/nand/nand_bcm_umi.c +3 -7
index 46a6bc9..ad51dad 100644
@@ -109,13 +109,9 @@ int nand_bcm_umi_bch_correct_page(uint8_t *datap, uint8_t *readEccData,
 	 * see if errors are correctible
 	 */
 	if ((regValue & REG_UMI_BCH_CTRL_STATUS_UNCORR_ERR) > 0) {
-		int i;
-
-		for (i = 0; i < numEccBytes; i++) {
-			if (readEccData[i] != 0xff) {
-				/* errors cannot be fixed, return -1 */
-				return -1;
-			}
+		if (memchr_inv(readEccData, 0xff, numEccBytes)) {
+			/* errors cannot be fixed, return -1 */
+			return -1;
 		}
 		/* If ECC is unprogrammed then we can't correct,
 		 * assume everything OK */
Signature · 1 line
1.7.4.4
#2 lore raw

Re: [PATCH] mtd/nand: use string library

Quoted 2 lines · Akinobu Mita

- Use memchr_inv to check if the data contains all 0xFF bytes. It is faster than looping for each byte.

Stupid question:

Are there any mtd devices modified that are slower at 64 bit accesses than repeated 8 bit accesses?

#3 lore raw

Re: [PATCH] mtd/nand: use string library

Quoted 8 lines · Joe Perches
Quoted 2 lines · Akinobu Mita

- Use memchr_inv to check if the data contains all 0xFF bytes. It is faster than looping for each byte.

Stupid question:

Are there any mtd devices modified that are slower at 64 bit accesses than repeated 8 bit accesses?

I believe this patch deals with kernel buffers, not any kind of direct access to the MTD, so the question (which is not stupid IMO) should be regarding CPU architectures. And my educated guess is that 64-bit access should not be any slower. I do know that 8-bit access *is* slower for some relevant architectures.

Brian

#4 lore raw

Re: [PATCH] mtd/nand: use string library

2012/1/28 Brian Norris <[email protected]>:

Quoted 7 lines · Joe Perches
Quoted 6 lines · Akinobu Mita
Quoted 2 lines

- Use memchr_inv to check if the data contains all 0xFF bytes. It is faster than looping for each byte.

Stupid question:

Quoted 8 lines
Quoted 2 lines

Are there any mtd devices modified that are slower at 64 bit accesses than repeated 8 bit accesses?

I believe this patch deals with kernel buffers, not any kind of direct access to the MTD, so the question (which is not stupid IMO) should be regarding CPU architectures. And my educated guess is that 64-bit access should not be any slower. I do know that 8-bit access *is* slower for some relevant architectures.

It could be slower when the number of bytes scanned is very small (found a unmatched character immediately, or the size of the area is very small), because memchr_inv() needs to generate a 64bit pattern to compare before starting the loop. I recalled that Eric Dumazet pointed out it could generate the 64bit pattern more efficiently.
(https://lkml.org/lkml/2011/8/8/480)

Even if that small scanning is slower, this change can be assumed cleanup patch that simplifies the code.

Acked-by
#5 lore raw

Re: [PATCH] mtd/nand: use string library

Quoted 24 lines · Akinobu Mita

2012/1/28 Brian Norris <[email protected]>:

Quoted 7 lines · Joe Perches
Quoted 6 lines · Akinobu Mita
Quoted 2 lines

- Use memchr_inv to check if the data contains all 0xFF bytes. It is faster than looping for each byte.

Stupid question:

Quoted 8 lines
Quoted 2 lines

Are there any mtd devices modified that are slower at 64 bit accesses than repeated 8 bit accesses?

I believe this patch deals with kernel buffers, not any kind of direct access to the MTD, so the question (which is not stupid IMO) should be regarding CPU architectures. And my educated guess is that 64-bit access should not be any slower. I do know that 8-bit access *is* slower for some relevant architectures.

It could be slower when the number of bytes scanned is very small (found a unmatched character immediately, or the size of the area is very small), because memchr_inv() needs to generate a 64bit pattern to compare before starting the loop.  I recalled that Eric Dumazet pointed out it could generate the 64bit pattern more efficiently.
(https://lkml.org/lkml/2011/8/8/480)

Quoted 2 lines

Even if that small scanning is slower, this change can be assumed cleanup patch that simplifies the code.

Well, I agree that it qualifies as cleanup as well, but we should at least make an attempt not to cause performance regression...

So by my understanding, the use of memchr_inv() is on buffers of minimum length of 10 in this patch, so we're likely to have decent results. And memcmp() usage looks fine to me.

So unless other concerns arise:

#6 lore raw

Re: [PATCH] mtd/nand: use string library

2012/2/1 Brian Norris <[email protected]>:

Quoted 28 lines · Akinobu Mita
Quoted 24 lines

2012/1/28 Brian Norris <[email protected]>:

Quoted 7 lines · Joe Perches
Quoted 6 lines · Akinobu Mita
Quoted 2 lines

- Use memchr_inv to check if the data contains all 0xFF bytes. It is faster than looping for each byte.

Stupid question:

Quoted 8 lines
Quoted 2 lines

Are there any mtd devices modified that are slower at 64 bit accesses than repeated 8 bit accesses?

I believe this patch deals with kernel buffers, not any kind of direct access to the MTD, so the question (which is not stupid IMO) should be regarding CPU architectures. And my educated guess is that 64-bit access should not be any slower. I do know that 8-bit access *is* slower for some relevant architectures.

It could be slower when the number of bytes scanned is very small (found a unmatched character immediately, or the size of the area is very small), because memchr_inv() needs to generate a 64bit pattern to compare before starting the loop.  I recalled that Eric Dumazet pointed out it could generate the 64bit pattern more efficiently.
(https://lkml.org/lkml/2011/8/8/480)

Quoted 2 lines

Even if that small scanning is slower, this change can be assumed cleanup patch that simplifies the code.

Quoted 6 lines

Well, I agree that it qualifies as cleanup as well, but we should at least make an attempt not to cause performance regression...

So by my understanding, the use of memchr_inv() is on buffers of minimum length of 10 in this patch, so we're likely to have decent results. And memcmp() usage looks fine to me.

Sorry, I answered without checking memchr_inv() carefully. If the size of buffer is less than 16 bytes, memchr_inv() scans for each byte as the original code did. So it is unlikely to be slower in the most cases.

But I mentioned in the previous email, there are some problems in memchr_inv(). I'll send the patch in a few days.

Quoted 3 lines

So unless other concerns arise:

Thanks.

#7 lore raw

PATCH 1/1 mtd/onenand: use string library

In reply to Akinobu Mita

- Use memchr_inv to check if the data contains all 0xFF bytes. It is faster than looping for each byte.

- Use memcmp to compare memory areas

---
 drivers/mtd/onenand/onenand_base.c |    8 ++------
 drivers/mtd/onenand/onenand_bbt.c  |   10 +++-------
 2 files changed, 5 insertions(+), 13 deletions(-)
drivers/mtd/onenand/onenand_base.c +2 -6
index a061bc1..374605f 100644
@@ -3673,7 +3673,7 @@ static void flexonenand_get_size(struct mtd_info *mtd)
 static int flexonenand_check_blocks_erased(struct mtd_info *mtd, int start, int end)
 {
 	struct onenand_chip *this = mtd->priv;
-	int i, ret;
+	int ret;
 	int block;
 	struct mtd_oob_ops ops = {
 		.mode = MTD_OPS_PLACE_OOB,
@@ -3699,11 +3699,7 @@ static int flexonenand_check_blocks_erased(struct mtd_info *mtd, int start, int
 		if (ret)
 			return ret;
 
-		for (i = 0; i < mtd->oobsize; i++)
-			if (this->oob_buf[i] != 0xff)
-				break;
-
-		if (i != mtd->oobsize) {
+		if (memchr_inv(this->oob_buf, 0xff, mtd->oobsize)) {
 			printk(KERN_WARNING "%s: Block %d not erased.\n",
 				__func__, block);
 			return 1;
drivers/mtd/onenand/onenand_bbt.c +3 -7
index 66fe3b7..8b1277b 100644
@@ -32,14 +32,10 @@
  */
 static int check_short_pattern(uint8_t *buf, int len, int paglen, struct nand_bbt_descr *td)
 {
-	int i;
-	uint8_t *p = buf;
-
 	/* Compare the pattern */
-	for (i = 0; i < td->len; i++) {
-		if (p[i] != td->pattern[i])
-			return -1;
-	}
+	if (memcmp(buf, td->pattern, td->len))
+		return -1;
+
         return 0;
 }
 
Signature · 1 line
1.7.4.4
#8 lore raw

PATCH 1/1 mISDN: use memchr_inv

In reply to Akinobu Mita

Use memchr_inv to check if the data contains all same bytes. It is faster than looping for each byte.

---
 drivers/isdn/mISDN/l1oip_core.c |   16 +++-------------
 1 files changed, 3 insertions(+), 13 deletions(-)
drivers/isdn/mISDN/l1oip_core.c +3 -13
index 22f8ec8..04f115a 100644
@@ -1112,7 +1112,7 @@ handle_bmsg(struct mISDNchannel *ch, struct sk_buff *skb)
 	struct l1oip			*hc = bch->hw;
 	int			ret = -EINVAL;
 	struct mISDNhead	*hh = mISDN_HEAD_P(skb);
-	int			l, ll, i;
+	int			l, ll;
 	unsigned char		*p;
 
 	switch (hh->prim) {
@@ -1128,13 +1128,8 @@ handle_bmsg(struct mISDNchannel *ch, struct sk_buff *skb)
 			break;
 		}
 		/* check for AIS / ulaw-silence */
-		p = skb->data;
 		l = skb->len;
-		for (i = 0; i < l; i++) {
-			if (*p++ != 0xff)
-				break;
-		}
-		if (i == l) {
+		if (!memchr_inv(skb->data, 0xff, l)) {
 			if (debug & DEBUG_L1OIP_MSG)
 				printk(KERN_DEBUG "%s: got AIS, not sending, "
 					"but counting\n", __func__);
@@ -1144,13 +1139,8 @@ handle_bmsg(struct mISDNchannel *ch, struct sk_buff *skb)
 			return 0;
 		}
 		/* check for silence */
-		p = skb->data;
 		l = skb->len;
-		for (i = 0; i < l; i++) {
-			if (*p++ != 0x2a)
-				break;
-		}
-		if (i == l) {
+		if (!memchr_inv(skb->data, 0x2a, l)) {
 			if (debug & DEBUG_L1OIP_MSG)
 				printk(KERN_DEBUG "%s: got silence, not sending"
 					", but counting\n", __func__);
Signature · 1 line
1.7.4.4
#10 lore raw

PATCH 1/1 mtd/ubi: use memchr_inv

In reply to Akinobu Mita

Use memchr_inv to check if the data contains all 0xFF bytes. It is faster than looping for each byte.
This also removes ubi_check_pattern which is no longer used.

Cc Artem Bityutskiy <[email protected]>
Cc David Woodhouse <[email protected]>
---
 drivers/mtd/ubi/io.c   |   14 +++++---------
 drivers/mtd/ubi/misc.c |   19 -------------------
 drivers/mtd/ubi/scan.c |    2 +-
 drivers/mtd/ubi/ubi.h  |    1 -
 4 files changed, 6 insertions(+), 30 deletions(-)
drivers/mtd/ubi/io.c +5 -9
index 5cde4e5..9fc27d3 100644
@@ -435,8 +435,7 @@ static int torture_peb(struct ubi_device *ubi, int pnum)
 		if (err)
 			goto out;
 
-		err = ubi_check_pattern(ubi->peb_buf1, 0xFF, ubi->peb_size);
-		if (err == 0) {
+		if (memchr_inv(ubi->peb_buf1, 0xFF, ubi->peb_size)) {
 			ubi_err("erased PEB %d, but a non-0xFF byte found",
 				pnum);
 			err = -EIO;
@@ -454,9 +453,7 @@ static int torture_peb(struct ubi_device *ubi, int pnum)
 		if (err)
 			goto out;
 
-		err = ubi_check_pattern(ubi->peb_buf1, patterns[i],
-					ubi->peb_size);
-		if (err == 0) {
+		if (memchr_inv(ubi->peb_buf1, patterns[i], ubi->peb_size)) {
 			ubi_err("pattern %x checking failed for PEB %d",
 				patterns[i], pnum);
 			err = -EIO;
@@ -783,7 +780,7 @@ int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
 		 * 0xFF. If yes, this physical eraseblock is assumed to be
 		 * empty.
 		 */
-		if (ubi_check_pattern(ec_hdr, 0xFF, UBI_EC_HDR_SIZE)) {
+		if (!memchr_inv(ec_hdr, 0xFF, UBI_EC_HDR_SIZE)) {
 			/* The physical eraseblock is supposedly empty */
 			if (verbose)
 				ubi_warn("no EC header found at PEB %d, "
@@ -1039,7 +1036,7 @@ int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
 		if (mtd_is_eccerr(read_err))
 			return UBI_IO_BAD_HDR_EBADMSG;
 
-		if (ubi_check_pattern(vid_hdr, 0xFF, UBI_VID_HDR_SIZE)) {
+		if (!memchr_inv(vid_hdr, 0xFF, UBI_VID_HDR_SIZE)) {
 			if (verbose)
 				ubi_warn("no VID header found at PEB %d, "
 					 "only 0xFF bytes", pnum);
@@ -1427,8 +1424,7 @@ int ubi_dbg_check_all_ff(struct ubi_device *ubi, int pnum, int offset, int len)
 		goto error;
 	}
 
-	err = ubi_check_pattern(buf, 0xFF, len);
-	if (err == 0) {
+	if (memchr_inv(buf, 0xFF, len)) {
 		ubi_err("flash region at PEB %d:%d, length %d does not "
 			"contain all 0xFF bytes", pnum, offset, len);
 		goto fail;
drivers/mtd/ubi/misc.c +0 -19
index f6a7d7a..0e4b9a2 100644
@@ -103,22 +103,3 @@ void ubi_calculate_reserved(struct ubi_device *ubi)
 	if (ubi->beb_rsvd_level < MIN_RESEVED_PEBS)
 		ubi->beb_rsvd_level = MIN_RESEVED_PEBS;
 }
-
-/**
- * ubi_check_pattern - check if buffer contains only a certain byte pattern.
- * @buf: buffer to check
- * @patt: the pattern to check
- * @size: buffer size in bytes
- *
- * This function returns %1 in there are only @patt bytes in @buf, and %0 if
- * something else was also found.
- */
-int ubi_check_pattern(const void *buf, uint8_t patt, int size)
-{
-	int i;
-
-	for (i = 0; i < size; i++)
-		if (((const uint8_t *)buf)[i] != patt)
-			return 0;
-	return 1;
-}
drivers/mtd/ubi/scan.c +1 -1
index 0cb17d9..54a56acc 100644
@@ -808,7 +808,7 @@ static int check_corruption(struct ubi_device *ubi, struct ubi_vid_hdr *vid_hdr,
 	if (err)
 		goto out_unlock;
 
-	if (ubi_check_pattern(ubi->peb_buf1, 0xFF, ubi->leb_size))
+	if (!memchr_inv(ubi->peb_buf1, 0xFF, ubi->leb_size))
 		goto out_unlock;
 
 	ubi_err("PEB %d contains corrupted VID header, and the data does not "
drivers/mtd/ubi/ubi.h +0 -1
index d51d75d..2ff1eb6 100644
@@ -519,7 +519,6 @@ int ubi_calc_data_len(const struct ubi_device *ubi, const void *buf,
 		      int length);
 int ubi_check_volume(struct ubi_device *ubi, int vol_id);
 void ubi_calculate_reserved(struct ubi_device *ubi);
-int ubi_check_pattern(const void *buf, uint8_t patt, int size);
 
 /* eba.c */
 int ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol,
Signature · 1 line
1.7.4.4
#11 lore raw

PATCH 1/1 mtd/inftlmount: use memchr_inv

In reply to Akinobu Mita

Use memchr_inv to check if the data contains all 0xFF bytes. It is faster than looping for each byte.
This also removes memcmpb which is no longer used.

---
 drivers/mtd/inftlmount.c |   14 ++------------
 1 files changed, 2 insertions(+), 12 deletions(-)
drivers/mtd/inftlmount.c +2 -12
index 4adc037..419b1c9 100644
@@ -320,16 +320,6 @@ static int find_boot_record(struct INFTLrecord *inftl)
 	return -1;
 }
 
-static int memcmpb(void *a, int c, int n)
-{
-	int i;
-	for (i = 0; i < n; i++) {
-		if (c != ((unsigned char *)a)[i])
-			return 1;
-	}
-	return 0;
-}
-
 /*
  * check_free_sector: check if a free sector is actually FREE,
  *	i.e. All 0xff in data and oob area.
@@ -345,14 +335,14 @@ static int check_free_sectors(struct INFTLrecord *inftl, unsigned int address,
 	for (i = 0; i < len; i += SECTORSIZE) {
 		if (mtd_read(mtd, address, SECTORSIZE, &retlen, buf))
 			return -1;
-		if (memcmpb(buf, 0xff, SECTORSIZE) != 0)
+		if (memchr_inv(buf, 0xff, SECTORSIZE))
 			return -1;
 
 		if (check_oob) {
 			if(inftl_read_oob(mtd, address, mtd->oobsize,
 					  &retlen, &buf[SECTORSIZE]) < 0)
 				return -1;
-			if (memcmpb(buf + SECTORSIZE, 0xff, mtd->oobsize) != 0)
+			if (memchr_inv(buf + SECTORSIZE, 0xff, mtd->oobsize))
 				return -1;
 		}
 		address += SECTORSIZE;
Signature · 1 line
1.7.4.4
#12 lore raw

PATCH 1/1 mtd/nftlmount: use memchr_inv

In reply to Akinobu Mita

Use memchr_inv to check if the data contains all 0xFF bytes. It is faster than looping for each byte.
This also removes memcmpb which is no longer used.

---
 drivers/mtd/nftlmount.c |   20 +++++---------------
 1 files changed, 5 insertions(+), 15 deletions(-)
drivers/mtd/nftlmount.c +5 -15
index 51b9d6a..a96bdb4 100644
@@ -255,16 +255,6 @@ The new DiskOnChip driver already scanned the bad block table.  Just query it.
 	return boot_record_count?0:-1;
 }
 
-static int memcmpb(void *a, int c, int n)
-{
-	int i;
-	for (i = 0; i < n; i++) {
-		if (c != ((unsigned char *)a)[i])
-			return 1;
-	}
-	return 0;
-}
-
 /* check_free_sector: check if a free sector is actually FREE, i.e. All 0xff in data and oob area */
 static int check_free_sectors(struct NFTLrecord *nftl, unsigned int address, int len,
 			      int check_oob)
@@ -277,14 +267,14 @@ static int check_free_sectors(struct NFTLrecord *nftl, unsigned int address, int
 	for (i = 0; i < len; i += SECTORSIZE) {
 		if (mtd_read(mtd, address, SECTORSIZE, &retlen, buf))
 			return -1;
-		if (memcmpb(buf, 0xff, SECTORSIZE) != 0)
+		if (memchr_inv(buf, 0xff, SECTORSIZE))
 			return -1;
 
 		if (check_oob) {
 			if(nftl_read_oob(mtd, address, mtd->oobsize,
 					 &retlen, &buf[SECTORSIZE]) < 0)
 				return -1;
-			if (memcmpb(buf + SECTORSIZE, 0xff, mtd->oobsize) != 0)
+			if (memchr_inv(buf + SECTORSIZE, 0xff, mtd->oobsize))
 				return -1;
 		}
 		address += SECTORSIZE;
@@ -392,7 +382,7 @@ static void check_sectors_in_chain(struct NFTLrecord *nftl, unsigned int first_b
 			case SECTOR_FREE:
 				/* verify that the sector is really free. If not, mark
 				   as ignore */
-				if (memcmpb(&bci, 0xff, 8) != 0 ||
+				if (memchr_inv(&bci, 0xff, 8) ||
 				    check_free_sectors(nftl, block * nftl->EraseSize + i * SECTORSIZE,
 						       SECTORSIZE, 0) != 0) {
 					printk("Incorrect free sector %d in block %d: "
@@ -529,10 +519,10 @@ static int check_and_mark_free_block(struct NFTLrecord *nftl, int block)
 				return -1;
 			if (i == SECTORSIZE) {
 				/* skip erase mark */
-				if (memcmpb(buf, 0xff, 8))
+				if (memchr_inv(buf, 0xff, 8))
 					return -1;
 			} else {
-				if (memcmpb(buf, 0xff, 16))
+				if (memchr_inv(buf, 0xff, 16))
 					return -1;
 			}
 		}
Signature · 1 line
1.7.4.4
#13 lore raw

PATCH 1/1 mtd/tests: use memchr_inv

In reply to Akinobu Mita

Use memchr_inv to check if the data contains all 0xFF bytes. It is faster than looping for each byte.

---
 drivers/mtd/tests/mtd_pagetest.c |   16 ++++++++--------
 1 files changed, 8 insertions(+), 8 deletions(-)
drivers/mtd/tests/mtd_pagetest.c +8 -8
index 252ddb0..08847cf 100644
@@ -401,6 +401,7 @@ static int erasetest(void)
 	size_t read, written;
 	int err = 0, i, ebnum, ok = 1;
 	loff_t addr0;
+	unsigned char *p;
 
 	printk(PRINT_PREF "erasetest\n");
 
@@ -442,14 +443,13 @@ static int erasetest(void)
 
 	printk(PRINT_PREF "verifying 1st page of block %d is all 0xff\n",
 	       ebnum);
-	for (i = 0; i < pgsize; ++i)
-		if (twopages[i] != 0xff) {
-			printk(PRINT_PREF "verifying all 0xff failed at %d\n",
-			       i);
-			errcnt += 1;
-			ok = 0;
-			break;
-		}
+	p = memchr_inv(twopages, 0xff, pgsize);
+	if (p) {
+		printk(PRINT_PREF "verifying all 0xff failed at %d\n",
+			       p - twopages);
+		errcnt += 1;
+		ok = 0;
+	}
 
 	if (ok && !err)
 		printk(PRINT_PREF "erasetest ok\n");
Signature · 1 line
1.7.4.4