mtd: nand: Error out if cmd_ctrl() is missing

Linux-MTD Mailing List linux-mtd at lists.infradead.org
Sat Oct 8 21:59:02 PDT 2016


Gitweb:     http://git.infradead.org/?p=mtd-2.6.git;a=commit;h=76fe334f71dd20f7eadd4c624441ec7d1fb92f33
Commit:     76fe334f71dd20f7eadd4c624441ec7d1fb92f33
Parent:     75c19e58abaed01ae20ad5802f6a6aa5a255117d
Author:     Andrey Smirnov <andrew.smirnov at gmail.com>
AuthorDate: Thu Jul 21 14:59:20 2016 -0700
Committer:  Boris Brezillon <boris.brezillon at free-electrons.com>
CommitDate: Fri Sep 23 09:35:16 2016 +0200

    mtd: nand: Error out if cmd_ctrl() is missing
    
    If no user specified chip->select_chip() function is provided, code in
    nand_base.c will automatically set this hook to nand_select_chip(),
    which in turn depends on chip->cmd_ctrl() hook being valid. Not
    providing both of those functions in NAND controller driver (for example
    by mistake) will result in a bit cryptic segfault. Same is true for
    chip->cmdfunc().
    
    To avoid the above scenario add a check in nand_scan_dent and error out
    if cmd_ctrl() is not provided.
    
    Suggested-by: Boris Brezillon <boris.brezillon at free-electrons.com>
    Suggested-by: Brian Norris <computersforpeace at gmail.com>
    Signed-off-by: Andrey Smirnov <andrew.smirnov at gmail.com>
    Signed-off-by: Boris Brezillon <boris.brezillon at free-electrons.com>
---
 drivers/mtd/nand/nand_base.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/drivers/mtd/nand/nand_base.c b/drivers/mtd/nand/nand_base.c
index 53ea796..7f742c9 100644
--- a/drivers/mtd/nand/nand_base.c
+++ b/drivers/mtd/nand/nand_base.c
@@ -4140,6 +4140,15 @@ int nand_scan_ident(struct mtd_info *mtd, int maxchips,
 	if (!mtd->name && mtd->dev.parent)
 		mtd->name = dev_name(mtd->dev.parent);
 
+	if ((!chip->cmdfunc || !chip->select_chip) && !chip->cmd_ctrl) {
+		/*
+		 * Default functions assigned for chip_select() and
+		 * cmdfunc() both expect cmd_ctrl() to be populated,
+		 * so we need to check that that's the case
+		 */
+		pr_err("chip.cmd_ctrl() callback is not provided");
+		return -EINVAL;
+	}
 	/* Set the default functions */
 	nand_set_defaults(chip, chip->options & NAND_BUSWIDTH_16);
 



More information about the linux-mtd-cvs mailing list