Skip to content

Commit 5def489

Browse files
dedekindDavid Woodhouse
authored andcommitted
mtd: do not duplicate length and offset checks in drivers
We already verify that offset and length are within the MTD device size in the MTD API functions. Let's remove the duplicated checks in drivers. This patch only affects the following API's: 'mtd_erase()' 'mtd_point()' 'mtd_unpoint()' 'mtd_get_unmapped_area()' 'mtd_read()' 'mtd_write()' 'mtd_panic_write()' 'mtd_lock()' 'mtd_unlock()' 'mtd_is_locked()' 'mtd_block_isbad()' 'mtd_block_markbad()' This patch adds a bit of noise by removing too sparse empty lines, but this is not too bad. Signed-off-by: Artem Bityutskiy <artem.bityutskiy@linux.intel.com> Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
1 parent 8273a0c commit 5def489

23 files changed

Lines changed: 3 additions & 321 deletions

‎drivers/mtd/chips/cfi_cmdset_0001.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1324,7 +1324,7 @@ static int cfi_intelext_point(struct mtd_info *mtd, loff_t from, size_t len,
13241324
int chipnum;
13251325
int ret = 0;
13261326

1327-
if (!map->virt || (from + len > mtd->size))
1327+
if (!map->virt)
13281328
return -EINVAL;
13291329

13301330
/* Now lock the chip(s) to POINT state */

‎drivers/mtd/chips/cfi_cmdset_0020.c‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -904,12 +904,6 @@ static int cfi_staa_erase_varsize(struct mtd_info *mtd,
904904
int i, first;
905905
struct mtd_erase_region_info *regions = mtd->eraseregions;
906906

907-
if (instr->addr > mtd->size)
908-
return -EINVAL;
909-
910-
if ((instr->len + instr->addr) > mtd->size)
911-
return -EINVAL;
912-
913907
/* Check that both start and end of the requested erase are
914908
* aligned with the erasesize at the appropriate addresses.
915909
*/
@@ -1155,9 +1149,6 @@ static int cfi_staa_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
11551149
if (len & (mtd->erasesize -1))
11561150
return -EINVAL;
11571151

1158-
if ((len + ofs) > mtd->size)
1159-
return -EINVAL;
1160-
11611152
chipnum = ofs >> cfi->chipshift;
11621153
adr = ofs - (chipnum << cfi->chipshift);
11631154

‎drivers/mtd/chips/cfi_util.c‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,6 @@ int cfi_varsize_frob(struct mtd_info *mtd, varsize_frob_t frob,
173173
int i, first;
174174
struct mtd_erase_region_info *regions = mtd->eraseregions;
175175

176-
if (ofs > mtd->size)
177-
return -EINVAL;
178-
179-
if ((len + ofs) > mtd->size)
180-
return -EINVAL;
181-
182176
/* Check that both start and end of the requested erase are
183177
* aligned with the erasesize at the appropriate addresses.
184178
*/

‎drivers/mtd/devices/block2mtd.c‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,6 @@ static int block2mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
104104
int offset = from & (PAGE_SIZE-1);
105105
int cpylen;
106106

107-
if (from > mtd->size)
108-
return -EINVAL;
109-
if (from + len > mtd->size)
110-
len = mtd->size - from;
111-
112107
if (retlen)
113108
*retlen = 0;
114109

@@ -190,10 +185,6 @@ static int block2mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
190185

191186
if (!len)
192187
return 0;
193-
if (to >= mtd->size)
194-
return -ENOSPC;
195-
if (to + len > mtd->size)
196-
len = mtd->size - to;
197188

198189
mutex_lock(&dev->write_mutex);
199190
err = _block2mtd_write(dev, buf, to, len, retlen);

‎drivers/mtd/devices/doc2000.c‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -602,10 +602,6 @@ static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
602602
int i, len256 = 0, ret=0;
603603
size_t left = len;
604604

605-
/* Don't allow read past end of device */
606-
if (from >= this->totlen)
607-
return -EINVAL;
608-
609605
mutex_lock(&this->lock);
610606

611607
*retlen = 0;
@@ -748,10 +744,6 @@ static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
748744
size_t left = len;
749745
int status;
750746

751-
/* Don't allow write past end of device */
752-
if (to >= this->totlen)
753-
return -EINVAL;
754-
755747
mutex_lock(&this->lock);
756748

757749
*retlen = 0;

‎drivers/mtd/devices/doc2001.c‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -383,10 +383,6 @@ static int doc_read (struct mtd_info *mtd, loff_t from, size_t len,
383383
void __iomem *docptr = this->virtadr;
384384
struct Nand *mychip = &this->chips[from >> (this->chipshift)];
385385

386-
/* Don't allow read past end of device */
387-
if (from >= this->totlen)
388-
return -EINVAL;
389-
390386
/* Don't allow a single read to cross a 512-byte block boundary */
391387
if (from + len > ((from | 0x1ff) + 1))
392388
len = ((from | 0x1ff) + 1) - from;
@@ -494,10 +490,6 @@ static int doc_write (struct mtd_info *mtd, loff_t to, size_t len,
494490
void __iomem *docptr = this->virtadr;
495491
struct Nand *mychip = &this->chips[to >> (this->chipshift)];
496492

497-
/* Don't allow write past end of device */
498-
if (to >= this->totlen)
499-
return -EINVAL;
500-
501493
#if 0
502494
/* Don't allow a single write to cross a 512-byte block boundary */
503495
if (to + len > ( (to | 0x1ff) + 1))

‎drivers/mtd/devices/doc2001plus.c‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -581,10 +581,6 @@ static int doc_read(struct mtd_info *mtd, loff_t from, size_t len,
581581
void __iomem * docptr = this->virtadr;
582582
struct Nand *mychip = &this->chips[from >> (this->chipshift)];
583583

584-
/* Don't allow read past end of device */
585-
if (from >= this->totlen)
586-
return -EINVAL;
587-
588584
/* Don't allow a single read to cross a 512-byte block boundary */
589585
if (from + len > ((from | 0x1ff) + 1))
590586
len = ((from | 0x1ff) + 1) - from;
@@ -700,10 +696,6 @@ static int doc_write(struct mtd_info *mtd, loff_t to, size_t len,
700696
void __iomem * docptr = this->virtadr;
701697
struct Nand *mychip = &this->chips[to >> (this->chipshift)];
702698

703-
/* Don't allow write past end of device */
704-
if (to >= this->totlen)
705-
return -EINVAL;
706-
707699
/* Don't allow writes which aren't exactly one block (512 bytes) */
708700
if ((to & 0x1ff) || (len != 0x200))
709701
return -EINVAL;

‎drivers/mtd/devices/lart.c‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,6 @@ static int flash_erase (struct mtd_info *mtd,struct erase_info *instr)
367367
printk (KERN_DEBUG "%s(addr = 0x%.8x, len = %d)\n", __func__, instr->addr, instr->len);
368368
#endif
369369

370-
/* sanity checks */
371-
if (instr->addr + instr->len > mtd->size) return (-EINVAL);
372-
373370
/*
374371
* check that both start and end of the requested erase are
375372
* aligned with the erasesize at the appropriate addresses.
@@ -442,7 +439,6 @@ static int flash_read (struct mtd_info *mtd,loff_t from,size_t len,size_t *retle
442439

443440
/* sanity checks */
444441
if (!len) return (0);
445-
if (from + len > mtd->size) return (-EINVAL);
446442

447443
/* we always read len bytes */
448444
*retlen = len;
@@ -526,7 +522,6 @@ static int flash_write (struct mtd_info *mtd,loff_t to,size_t len,size_t *retlen
526522

527523
/* sanity checks */
528524
if (!len) return (0);
529-
if (to + len > mtd->size) return (-EINVAL);
530525

531526
/* first, we write a 0xFF.... padded byte until we reach a dword boundary */
532527
if (to & (BUSWIDTH - 1))

‎drivers/mtd/devices/m25p80.c‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,6 @@ static int m25p80_erase(struct mtd_info *mtd, struct erase_info *instr)
288288
__func__, (long long)instr->addr,
289289
(long long)instr->len);
290290

291-
/* sanity checks */
292-
if (instr->addr + instr->len > flash->mtd.size)
293-
return -EINVAL;
294291
div_u64_rem(instr->len, mtd->erasesize, &rem);
295292
if (rem)
296293
return -EINVAL;
@@ -353,9 +350,6 @@ static int m25p80_read(struct mtd_info *mtd, loff_t from, size_t len,
353350
if (!len)
354351
return 0;
355352

356-
if (from + len > flash->mtd.size)
357-
return -EINVAL;
358-
359353
spi_message_init(&m);
360354
memset(t, 0, (sizeof t));
361355

@@ -423,9 +417,6 @@ static int m25p80_write(struct mtd_info *mtd, loff_t to, size_t len,
423417
if (!len)
424418
return(0);
425419

426-
if (to + len > flash->mtd.size)
427-
return -EINVAL;
428-
429420
spi_message_init(&m);
430421
memset(t, 0, (sizeof t));
431422

@@ -515,9 +506,6 @@ static int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
515506
if (!len)
516507
return 0;
517508

518-
if (to + len > flash->mtd.size)
519-
return -EINVAL;
520-
521509
spi_message_init(&m);
522510
memset(t, 0, (sizeof t));
523511

‎drivers/mtd/devices/ms02-nv.c‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,8 @@ static int ms02nv_read(struct mtd_info *mtd, loff_t from,
5959
{
6060
struct ms02nv_private *mp = mtd->priv;
6161

62-
if (from + len > mtd->size)
63-
return -EINVAL;
64-
6562
memcpy(buf, mp->uaddr + from, len);
6663
*retlen = len;
67-
6864
return 0;
6965
}
7066

@@ -73,12 +69,8 @@ static int ms02nv_write(struct mtd_info *mtd, loff_t to,
7369
{
7470
struct ms02nv_private *mp = mtd->priv;
7571

76-
if (to + len > mtd->size)
77-
return -EINVAL;
78-
7972
memcpy(mp->uaddr + to, buf, len);
8073
*retlen = len;
81-
8274
return 0;
8375
}
8476

0 commit comments

Comments
 (0)