diff --git a/drivers/mtd/ubi/build.c b/drivers/mtd/ubi/build.c
index aa60b30..865e4fa 100644
--- a/drivers/mtd/ubi/build.c
+++ b/drivers/mtd/ubi/build.c
@@ -860,9 +860,6 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num,
 	if (max_beb_per1024 < 0 || max_beb_per1024 > MAX_MTD_UBI_BEB_LIMIT)
 		return -EINVAL;
 
-	if (!max_beb_per1024)
-		max_beb_per1024 = CONFIG_MTD_UBI_BEB_LIMIT;
-
 	/*
 	 * Check if we already have the same MTD device attached.
 	 *
@@ -935,6 +932,15 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num,
 	if (err)
 		goto out_free;
 
+	if (!max_beb_per1024) {
+		if (!ubi->bad_allowed) {
+			ubi_err("mtd%d does not admit of bad blocks, "
+				"max_beb_per1024 cannot be used", mtd->index);
+			goto out_free;
+		}
+		max_beb_per1024 = CONFIG_MTD_UBI_BEB_LIMIT;
+	}
+
 	err = -ENOMEM;
 	ubi->peb_buf = vmalloc(ubi->peb_size);
 	if (!ubi->peb_buf)
