Skip to content

Commit 2023f1f

Browse files
committed
mtd: rawnand: allocate model parameter dynamically
Thanks to the migration of all drivers to use nand_scan() and the related nand_controller_ops, we can now allocate data during the detection phase. Let's do it first for the NAND model parameter which is allocated in nand_detect(). Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com> Reviewed-by: Boris Brezillon <boris.brezillon@bootlin.com>
1 parent 98732da commit 2023f1f

2 files changed

Lines changed: 42 additions & 12 deletions

File tree

‎drivers/mtd/nand/raw/nand_base.c‎

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5225,8 +5225,11 @@ static int nand_flash_detect_onfi(struct nand_chip *chip)
52255225

52265226
sanitize_string(p->manufacturer, sizeof(p->manufacturer));
52275227
sanitize_string(p->model, sizeof(p->model));
5228-
strncpy(chip->parameters.model, p->model,
5229-
sizeof(chip->parameters.model) - 1);
5228+
chip->parameters.model = kstrdup(p->model, GFP_KERNEL);
5229+
if (!chip->parameters.model) {
5230+
ret = -ENOMEM;
5231+
goto free_onfi_param_page;
5232+
}
52305233

52315234
mtd->writesize = le32_to_cpu(p->byte_per_page);
52325235

@@ -5356,8 +5359,11 @@ static int nand_flash_detect_jedec(struct nand_chip *chip)
53565359

53575360
sanitize_string(p->manufacturer, sizeof(p->manufacturer));
53585361
sanitize_string(p->model, sizeof(p->model));
5359-
strncpy(chip->parameters.model, p->model,
5360-
sizeof(chip->parameters.model) - 1);
5362+
chip->parameters.model = kstrdup(p->model, GFP_KERNEL);
5363+
if (!chip->parameters.model) {
5364+
ret = -ENOMEM;
5365+
goto free_jedec_param_page;
5366+
}
53615367

53625368
mtd->writesize = le32_to_cpu(p->byte_per_page);
53635369

@@ -5546,8 +5552,9 @@ static bool find_full_id_nand(struct nand_chip *chip,
55465552
chip->onfi_timing_mode_default =
55475553
type->onfi_timing_mode_default;
55485554

5549-
strncpy(chip->parameters.model, type->name,
5550-
sizeof(chip->parameters.model) - 1);
5555+
chip->parameters.model = kstrdup(type->name, GFP_KERNEL);
5556+
if (!chip->parameters.model)
5557+
return false;
55515558

55525559
return true;
55535560
}
@@ -5706,8 +5713,9 @@ static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
57065713
if (!type->name)
57075714
return -ENODEV;
57085715

5709-
strncpy(chip->parameters.model, type->name,
5710-
sizeof(chip->parameters.model) - 1);
5716+
chip->parameters.model = kstrdup(type->name, GFP_KERNEL);
5717+
if (!chip->parameters.model)
5718+
return -ENOMEM;
57115719

57125720
chip->chipsize = (uint64_t)type->chipsize << 20;
57135721

@@ -5737,7 +5745,9 @@ static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
57375745
mtd->name);
57385746
pr_warn("bus width %d instead of %d bits\n", busw ? 16 : 8,
57395747
(chip->options & NAND_BUSWIDTH_16) ? 16 : 8);
5740-
return -EINVAL;
5748+
ret = -EINVAL;
5749+
5750+
goto free_detect_allocation;
57415751
}
57425752

57435753
nand_decode_bbm_options(chip);
@@ -5774,6 +5784,11 @@ static int nand_detect(struct nand_chip *chip, struct nand_flash_dev *type)
57745784
(int)(chip->chipsize >> 20), nand_is_slc(chip) ? "SLC" : "MLC",
57755785
mtd->erasesize >> 10, mtd->writesize, mtd->oobsize);
57765786
return 0;
5787+
5788+
free_detect_allocation:
5789+
kfree(chip->parameters.model);
5790+
5791+
return ret;
57775792
}
57785793

57795794
static const char * const nand_ecc_modes[] = {
@@ -6013,6 +6028,11 @@ static int nand_scan_ident(struct mtd_info *mtd, int maxchips,
60136028
return 0;
60146029
}
60156030

6031+
static void nand_scan_ident_cleanup(struct nand_chip *chip)
6032+
{
6033+
kfree(chip->parameters.model);
6034+
}
6035+
60166036
static int nand_set_ecc_soft_ops(struct mtd_info *mtd)
60176037
{
60186038
struct nand_chip *chip = mtd_to_nand(mtd);
@@ -6760,11 +6780,18 @@ int nand_scan_with_ids(struct mtd_info *mtd, int maxchips,
67606780

67616781
ret = nand_attach(chip);
67626782
if (ret)
6763-
return ret;
6783+
goto cleanup_ident;
67646784

67656785
ret = nand_scan_tail(mtd);
67666786
if (ret)
6767-
nand_detach(chip);
6787+
goto detach_chip;
6788+
6789+
return 0;
6790+
6791+
detach_chip:
6792+
nand_detach(chip);
6793+
cleanup_ident:
6794+
nand_scan_ident_cleanup(chip);
67686795

67696796
return ret;
67706797
}
@@ -6796,6 +6823,9 @@ void nand_cleanup(struct nand_chip *chip)
67966823

67976824
/* Free controller specific allocations after chip identification */
67986825
nand_detach(chip);
6826+
6827+
/* Free identification phase allocations */
6828+
nand_scan_ident_cleanup(chip);
67996829
}
68006830

68016831
EXPORT_SYMBOL_GPL(nand_cleanup);

‎include/linux/mtd/rawnand.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ struct onfi_params {
476476
*/
477477
struct nand_parameters {
478478
/* Generic parameters */
479-
char model[100];
479+
const char *model;
480480
bool supports_set_get_features;
481481
DECLARE_BITMAP(set_feature_list, ONFI_FEATURE_NUMBER);
482482
DECLARE_BITMAP(get_feature_list, ONFI_FEATURE_NUMBER);

0 commit comments

Comments
 (0)