Skip to contentSkip to conversation

NOMMU: Present backing device capabilities for MTD chardevs

PATCH In review David Howells 2 participants 7 messages Feb 12, 2009 – Feb 20, 2009 Memory technology devices (MTD)

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 NOMMU: Present backing device capabilities for MTD chardevs

Present backing device capabilities for MTD character device files to allow NOMMU mmap to do direct mapping where possible.

---

 drivers/mtd/Makefile         |    2 +
 drivers/mtd/chips/map_ram.c  |   17 +++++++++++
 drivers/mtd/chips/map_rom.c  |   17 +++++++++++
 drivers/mtd/devices/mtdram.c |   14 +++++++++
 drivers/mtd/internal.h       |   17 +++++++++++
 drivers/mtd/mtdbdi.c         |   43 +++++++++++++++++++++++++++++
 drivers/mtd/mtdchar.c        |   63 +++++++++++++++++++++++++++++++++++++++++-
 drivers/mtd/mtdcore.c        |   15 ++++++++++
 drivers/mtd/mtdpart.c        |   15 ++++++++++
 include/linux/mtd/mtd.h      |   14 +++++++++
 10 files changed, 215 insertions(+), 2 deletions(-)
 create mode 100644 drivers/mtd/internal.h
 create mode 100644 drivers/mtd/mtdbdi.c
drivers/mtd/Makefile +1 -1
index 4521b1e..82d1e4d 100644
@@ -4,7 +4,7 @@
 
 # Core functionality.
 obj-$(CONFIG_MTD)		+= mtd.o
-mtd-y				:= mtdcore.o mtdsuper.o
+mtd-y				:= mtdcore.o mtdsuper.o mtdbdi.o
 mtd-$(CONFIG_MTD_PARTITIONS)	+= mtdpart.o
 
 obj-$(CONFIG_MTD_CONCAT)	+= mtdconcat.o
drivers/mtd/chips/map_ram.c +17 -0
index 072dd8a..6bdc50c 100644
@@ -21,6 +21,8 @@ static int mapram_write (struct mtd_info *, loff_t, size_t, size_t *, const u_ch
 static int mapram_erase (struct mtd_info *, struct erase_info *);
 static void mapram_nop (struct mtd_info *);
 static struct mtd_info *map_ram_probe(struct map_info *map);
+static unsigned long mapram_unmapped_area(struct mtd_info *, unsigned long,
+					  unsigned long, unsigned long);
 
 
 static struct mtd_chip_driver mapram_chipdrv = {
@@ -64,6 +66,7 @@ static struct mtd_info *map_ram_probe(struct map_info *map)
 	mtd->type = MTD_RAM;
 	mtd->size = map->size;
 	mtd->erase = mapram_erase;
+	mtd->get_unmapped_area = mapram_unmapped_area;
 	mtd->read = mapram_read;
 	mtd->write = mapram_write;
 	mtd->sync = mapram_nop;
@@ -79,6 +82,20 @@ static struct mtd_info *map_ram_probe(struct map_info *map)
 }
 
 
+/*
+ * Allow NOMMU mmap() to directly map the device (if not NULL)
+ * - return the address to which the offset maps
+ * - return -ENOSYS to indicate refusal to do the mapping
+ */
+static unsigned long mapram_unmapped_area(struct mtd_info *mtd,
+					  unsigned long len,
+					  unsigned long offset,
+					  unsigned long flags)
+{
+	struct map_info *map = mtd->priv;
+	return (unsigned long) map->virt + offset;
+}
+
 static int mapram_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
 {
 	struct map_info *map = mtd->priv;
drivers/mtd/chips/map_rom.c +17 -0
index 821d0ed..53ce799 100644
@@ -19,6 +19,8 @@ static int maprom_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
 static int maprom_write (struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
 static void maprom_nop (struct mtd_info *);
 static struct mtd_info *map_rom_probe(struct map_info *map);
+static unsigned long maprom_unmapped_area(struct mtd_info *, unsigned long,
+					  unsigned long, unsigned long);
 
 static struct mtd_chip_driver maprom_chipdrv = {
 	.probe	= map_rom_probe,
@@ -39,6 +41,7 @@ static struct mtd_info *map_rom_probe(struct map_info *map)
 	mtd->name = map->name;
 	mtd->type = MTD_ROM;
 	mtd->size = map->size;
+	mtd->get_unmapped_area = maprom_unmapped_area;
 	mtd->read = maprom_read;
 	mtd->write = maprom_write;
 	mtd->sync = maprom_nop;
@@ -51,6 +54,20 @@ static struct mtd_info *map_rom_probe(struct map_info *map)
 }
 
 
+/*
+ * Allow NOMMU mmap() to directly map the device (if not NULL)
+ * - return the address to which the offset maps
+ * - return -ENOSYS to indicate refusal to do the mapping
+ */
+static unsigned long maprom_unmapped_area(struct mtd_info *mtd,
+					  unsigned long len,
+					  unsigned long offset,
+					  unsigned long flags)
+{
+	struct map_info *map = mtd->priv;
+	return (unsigned long) map->virt + offset;
+}
+
 static int maprom_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
 {
 	struct map_info *map = mtd->priv;
drivers/mtd/devices/mtdram.c +14 -0
index 3aaca88..fce5ff7 100644
@@ -65,6 +65,19 @@ static void ram_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
 {
 }
 
+/*
+ * Allow NOMMU mmap() to directly map the device (if not NULL)
+ * - return the address to which the offset maps
+ * - return -ENOSYS to indicate refusal to do the mapping
+ */
+static unsigned long ram_get_unmapped_area(struct mtd_info *mtd,
+					   unsigned long len,
+					   unsigned long offset,
+					   unsigned long flags)
+{
+	return (unsigned long) mtd->priv + offset;
+}
+
 static int ram_read(struct mtd_info *mtd, loff_t from, size_t len,
 		size_t *retlen, u_char *buf)
 {
@@ -116,6 +129,7 @@ int mtdram_init_device(struct mtd_info *mtd, void *mapped_address,
 	mtd->erase = ram_erase;
 	mtd->point = ram_point;
 	mtd->unpoint = ram_unpoint;
+	mtd->get_unmapped_area = ram_get_unmapped_area;
 	mtd->read = ram_read;
 	mtd->write = ram_write;
 
drivers/mtd/internal.h new file +17 -0
new file mode 100644
index 0000000..b366c68
@@ -0,0 +1,17 @@
+/* Internal MTD definitions
+ *
+ * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
+ * Written by David Howells ([email protected])
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ */
+
+/*
+ * mtdbdi.c
+ */
+extern struct backing_dev_info mtd_bdi_unmappable;
+extern struct backing_dev_info mtd_bdi_ro_mappable;
+extern struct backing_dev_info mtd_bdi_rw_mappable;
drivers/mtd/mtdbdi.c new file +43 -0

43 lines not on this page · raw message

drivers/mtd/mtdchar.c +62 -1

92 lines not on this page · raw message

drivers/mtd/mtdcore.c +15 -0
index 76fe0a1..65a7f37 100644
@@ -19,6 +19,7 @@
 #include <linux/proc_fs.h>
 
 #include <linux/mtd/mtd.h>
+#include "internal.h"
 
 #include "mtdcore.h"
 
@@ -46,6 +47,20 @@ int add_mtd_device(struct mtd_info *mtd)
 {
 	int i;
 
+	if (!mtd->backing_dev_info) {
+		switch (mtd->type) {
+		case MTD_RAM:
+			mtd->backing_dev_info = &mtd_bdi_rw_mappable;
+			break;
+		case MTD_ROM:
+			mtd->backing_dev_info = &mtd_bdi_ro_mappable;
+			break;
+		default:
+			mtd->backing_dev_info = &mtd_bdi_unmappable;
+			break;
+		}
+	}
+
 	BUG_ON(mtd->writesize == 0);
 	mutex_lock(&mtd_table_mutex);
 
drivers/mtd/mtdpart.c +15 -0
index 144e6b6..06d5b9d 100644
@@ -84,6 +84,18 @@ static void part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
 	part->master->unpoint(part->master, from + part->offset, len);
 }
 
+static unsigned long part_get_unmapped_area(struct mtd_info *mtd,
+					    unsigned long len,
+					    unsigned long offset,
+					    unsigned long flags)
+{
+	struct mtd_part *part = PART(mtd);
+
+	offset += part->offset;
+	return part->master->get_unmapped_area(part->master, len, offset,
+					       flags);
+}
+
 static int part_read_oob(struct mtd_info *mtd, loff_t from,
 		struct mtd_oob_ops *ops)
 {
@@ -342,6 +354,7 @@ static struct mtd_part *add_one_partition(struct mtd_info *master,
 
 	slave->mtd.name = part->name;
 	slave->mtd.owner = master->owner;
+	slave->mtd.backing_dev_info = master->backing_dev_info;
 
 	slave->mtd.read = part_read;
 	slave->mtd.write = part_write;
@@ -354,6 +367,8 @@ static struct mtd_part *add_one_partition(struct mtd_info *master,
 		slave->mtd.unpoint = part_unpoint;
 	}
 
+	if (master->get_unmapped_area)
+		slave->mtd.get_unmapped_area = part_get_unmapped_area;
 	if (master->read_oob)
 		slave->mtd.read_oob = part_read_oob;
 	if (master->write_oob)
include/linux/mtd/mtd.h +14 -0
index 3aa5d77..0c079fd 100644
@@ -162,6 +162,20 @@ struct mtd_info {
 	/* We probably shouldn't allow XIP if the unpoint isn't a NULL */
 	void (*unpoint) (struct mtd_info *mtd, loff_t from, size_t len);
 
+	/* Allow NOMMU mmap() to directly map the device (if not NULL)
+	 * - return the address to which the offset maps
+	 * - return -ENOSYS to indicate refusal to do the mapping
+	 */
+	unsigned long (*get_unmapped_area) (struct mtd_info *mtd,
+					    unsigned long len,
+					    unsigned long offset,
+					    unsigned long flags);
+
+	/* Backing device capabilities for this device
+	 * - provides mmap capabilities
+	 */
+	struct backing_dev_info *backing_dev_info;
+
 
 	int (*read) (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf);
 	int (*write) (struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf);
#1 lore raw

PATCH 2/4 NOMMU: Add support for direct mapping through mtdconcat if possible

Add support for direct mapping through mtdconcat, if possible, by attaching the samebacking_dev_info structure to the master.

It has some restrictions:

(1) It won't permit direct mapping of concatenated devices that have differing BDIs.

(2) It doesn't support maps that span the 'gap' between devices, although it possibly could if the devices spanned across return compatible (ie. contiguous) addresses from their get_unmapped_area() ops.

---

 drivers/mtd/mtdconcat.c |   47 +++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 47 insertions(+), 0 deletions(-)
drivers/mtd/mtdconcat.c +47 -0
index 3dbb1b3..792b547 100644
@@ -13,6 +13,7 @@
 #include <linux/slab.h>
 #include <linux/sched.h>
 #include <linux/types.h>
+#include <linux/backing-dev.h>
 
 #include <linux/mtd/mtd.h>
 #include <linux/mtd/concat.h>
@@ -684,6 +685,40 @@ static int concat_block_markbad(struct mtd_info *mtd, loff_t ofs)
 }
 
 /*
+ * try to support NOMMU mmaps on concatenated devices
+ * - we don't support subdev spanning as we can't guarantee it'll work
+ */
+static unsigned long concat_get_unmapped_area(struct mtd_info *mtd,
+					      unsigned long len,
+					      unsigned long offset,
+					      unsigned long flags)
+{
+	struct mtd_concat *concat = CONCAT(mtd);
+	int i;
+
+	for (i = 0; i < concat->num_subdev; i++) {
+		struct mtd_info *subdev = concat->subdev[i];
+
+		if (offset >= subdev->size) {
+			offset -= subdev->size;
+			continue;
+		}
+
+		/* we've found the subdev over which the mapping will reside */
+		if (offset + len > subdev->size)
+			return (unsigned long) -EINVAL;
+
+		if (subdev->get_unmapped_area)
+			return subdev->get_unmapped_area(subdev, len, offset,
+							 flags);
+
+		break;
+	}
+
+	return (unsigned long) -ENOSYS;
+}
+
+/*
  * This function constructs a virtual MTD device by concatenating
  * num_devs MTD devices. A pointer to the new device object is
  * stored to *new_dev upon success. This function does _not_
@@ -740,6 +775,8 @@ struct mtd_info *mtd_concat_create(struct mtd_info *subdev[],	/* subdevices to c
 
 	concat->mtd.ecc_stats.badblocks = subdev[0]->ecc_stats.badblocks;
 
+	concat->mtd.backing_dev_info = subdev[0]->backing_dev_info;
+
 	concat->subdev[0] = subdev[0];
 
 	for (i = 1; i < num_devs; i++) {
@@ -766,6 +803,15 @@ struct mtd_info *mtd_concat_create(struct mtd_info *subdev[],	/* subdevices to c
 				concat->mtd.flags |=
 				    subdev[i]->flags & MTD_WRITEABLE;
 		}
+
+		/* only permit direct mapping if the BDIs are all the same
+		 * - copy-mapping is still permitted
+		 */
+		if (concat->mtd.backing_dev_info !=
+		    subdev[i]->backing_dev_info)
+			concat->mtd.backing_dev_info =
+				&default_backing_dev_info;
+
 		concat->mtd.size += subdev[i]->size;
 		concat->mtd.ecc_stats.badblocks +=
 			subdev[i]->ecc_stats.badblocks;
@@ -796,6 +842,7 @@ struct mtd_info *mtd_concat_create(struct mtd_info *subdev[],	/* subdevices to c
 	concat->mtd.unlock = concat_unlock;
 	concat->mtd.suspend = concat_suspend;
 	concat->mtd.resume = concat_resume;
+	concat->mtd.get_unmapped_area = concat_get_unmapped_area;
 
 	/*
 	 * Combine the erase block size info of the subdevices:
#2 lore raw

PATCH 3/4 NOMMU: Make it possible for RomFS to use MTD devices directly

In reply to David Howells

Change RomFS so that it can use MTD devices directly - without the intercession of the block layer - as well as using block devices.

This permits RomFS:

(1) to use the MTD direct mapping facility available under NOMMU conditions if

     the underlying device is directly accessible by the CPU (including XIP);

(2) and thus to be used when the block layer is disabled.

RomFS can be configured with support just for MTD devices, just for Block devices or for both. If RomFS is configured for both, then it will treat mtdblock device files as MTD backing stores, not block layer backing stores.

I tested this using a CONFIG_MMU=n CONFIG_BLOCK=n kernel running on my FRV board with a RomFS image installed on the mtdram test device. I see my test program being run XIP:

	# cat /proc/maps
	...
	c0c000b0-c0c01f8c r-xs 00000000 1f:00 144        /mnt/doshm
	...

GDB on the kernel can be used to show that these addresses are within the set-aside RAM space.

---

 fs/romfs/Kconfig      |   24 ++
 fs/romfs/Makefile     |    9 +
 fs/romfs/inode.c      |  665 -------------------------------------------------
 fs/romfs/internal.h   |   47 +++
 fs/romfs/mmap-nommu.c |   75 ++++++
 fs/romfs/storage.c    |  261 +++++++++++++++++++
 fs/romfs/super.c      |  648 ++++++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 1062 insertions(+), 667 deletions(-)
 delete mode 100644 fs/romfs/inode.c
 create mode 100644 fs/romfs/internal.h
 create mode 100644 fs/romfs/mmap-nommu.c
 create mode 100644 fs/romfs/storage.c
 create mode 100644 fs/romfs/super.c
fs/romfs/Kconfig +24 -0
index 1a17020..802c742 100644
@@ -14,3 +14,27 @@ config ROMFS_FS
 
 	  If you don't know whether you need it, then you don't need it:
 	  answer N.
+
+config ROMFS_ON_BLOCK
+	bool "Block device-backed ROM file system support" if (ROMFS_ON_MTD && EMBEDDED)
+	depends on ROMFS_FS && BLOCK
+	help
+	  This permits ROMFS to use block devices buffered through the page
+	  cache as the medium from which to retrieve data.  It does not allow
+	  direct mapping of the medium.
+
+	  If unsure, answer Y.
+
+config ROMFS_ON_MTD
+	bool "MTD-backed ROM file system support"
+	depends on ROMFS_FS
+	depends on MTD=y || (ROMFS_FS=m && MTD)
+	help
+	  This permits ROMFS to use MTD based devices directly, without the
+	  intercession of the block layer (which may have been disabled).  It
+	  also allows direct mapping of MTD devices through romfs files under
+	  NOMMU conditions if the underlying device is directly addressable by
+	  the CPU.
+
+	  If unsure, answer Y.
+
fs/romfs/Makefile +7 -2
index c95b21c..420beb7 100644
@@ -1,7 +1,12 @@
 #
-# Makefile for the linux romfs filesystem routines.
+# Makefile for the linux RomFS filesystem routines.
 #
 
 obj-$(CONFIG_ROMFS_FS) += romfs.o
 
-romfs-objs := inode.o
+romfs-y := storage.o super.o
+
+ifneq ($(CONFIG_MMU),y)
+romfs-$(CONFIG_ROMFS_ON_MTD) += mmap-nommu.o
+endif
+
fs/romfs/inode.c deleted +0 -665

665 lines not on this page · raw message

fs/romfs/internal.h new file +47 -0

47 lines not on this page · raw message

fs/romfs/mmap-nommu.c new file +75 -0

75 lines not on this page · raw message

fs/romfs/storage.c new file +261 -0

261 lines not on this page · raw message

fs/romfs/super.c new file +648 -0

648 lines not on this page · raw message

#3 lore raw

Re: [PATCH 3/4] NOMMU: Make it possible for RomFS to use MTD devices directly

Quoted 5 lines · On Thu, 12 Feb 2009 10:40:10 +0000 David Howells <[email protected]> wrote:

Change RomFS so that it can use MTD devices directly - without the intercession of the block layer - as well as using block devices.

This permits RomFS:

Quoted 8 lines

(1) to use the MTD direct mapping facility available under NOMMU conditions if

     the underlying device is directly accessible by the CPU (including XIP);

(2) and thus to be used when the block layer is disabled.

RomFS can be configured with support just for MTD devices, just for Block devices or for both. If RomFS is configured for both, then it will treat mtdblock device files as MTD backing stores, not block layer backing stores.

My x86_64 usualconfig just exploded with

fs/romfs/storage.c:18:2: error: #error no ROMFS backing store interface configured

http://userweb.kernel.org/~akpm/config-akpm2.txt

#4 lore raw

Re: [PATCH 3/4] NOMMU: Make it possible for RomFS to use MTD devices directly

Quoted 3 lines · Andrew Morton

My x86_64 usualconfig just exploded with

fs/romfs/storage.c:18:2: error: #error no ROMFS backing store interface configured

To be able to use romfs you have to set one or both of the two backing store options with this patch. Commenting out the #error isn't good enough. This needs some Kconfig magic, but I'm not sure what.

David

#5 lore raw

Re: [PATCH 3/4] NOMMU: Make it possible for RomFS to use MTD devices directly

Quoted 7 lines · David Howells
Quoted 3 lines

My x86_64 usualconfig just exploded with

fs/romfs/storage.c:18:2: error: #error no ROMFS backing store interface configured

To be able to use romfs you have to set one or both of the two backing store options with this patch. Commenting out the #error isn't good enough. This needs some Kconfig magic, but I'm not sure what.

Perhaps a choice item? The attached patch seems to work quite nicely. I wouldn't really want to do this if there were more than two options.

If you like it, I can fold it into the main patch.

David
---
From: David Howells <[email protected]>
Subject: [PATCH] NOMMU: Fix the RomFS Kconfig to ensure at least one backing store is selected

Fix the configuration of the RomFS to make sure that at least one backing store method is always selected. This is done by rendering it down to a choice item that selects between Block, MTD and both.

This also works correctly in the case that CONFIG_MTD=m: MTD cannot be selected as a backing store unless CONFIG_ROMFS_FS is also 'm'.

---

 fs/romfs/Kconfig |   34 ++++++++++++++++++++++++++++------
 1 files changed, 28 insertions(+), 6 deletions(-)
fs/romfs/Kconfig +28 -6
index 802c742..ce2d6bc 100644
@@ -1,6 +1,6 @@
 config ROMFS_FS
 	tristate "ROM file system support"
-	depends on BLOCK
+	depends on BLOCK || MTD
 	---help---
 	  This is a very small read-only file system mainly intended for
 	  initial ram disks of installation disks, but it could be used for
@@ -15,9 +15,19 @@ config ROMFS_FS
 	  If you don't know whether you need it, then you don't need it:
 	  answer N.
 
-config ROMFS_ON_BLOCK
-	bool "Block device-backed ROM file system support" if (ROMFS_ON_MTD && EMBEDDED)
-	depends on ROMFS_FS && BLOCK
+#
+# Select the backing stores to be supported
+#
+choice
+	prompt "RomFS backing stores"
+	depends on ROMFS_FS
+	default ROMFS_BACKED_BY_BLOCK
+	help
+	  Select the backing stores to be supported.
+
+config ROMFS_BACKED_BY_BLOCK
+	bool "Block device-backed ROM file system support"
+	depends on BLOCK
 	help
 	  This permits ROMFS to use block devices buffered through the page
 	  cache as the medium from which to retrieve data.  It does not allow
@@ -25,9 +35,8 @@ config ROMFS_ON_BLOCK
 
 	  If unsure, answer Y.
 
-config ROMFS_ON_MTD
+config ROMFS_BACKED_BY_MTD
 	bool "MTD-backed ROM file system support"
-	depends on ROMFS_FS
 	depends on MTD=y || (ROMFS_FS=m && MTD)
 	help
 	  This permits ROMFS to use MTD based devices directly, without the
@@ -38,3 +47,16 @@ config ROMFS_ON_MTD
 
 	  If unsure, answer Y.
 
+config ROMFS_BACKED_BY_BOTH
+	bool "Both the above"
+	depends on BLOCK && (MTD=y || (ROMFS_FS=m && MTD))
+endchoice
+
+
+config ROMFS_ON_BLOCK
+	bool
+	default y if ROMFS_BACKED_BY_BLOCK || ROMFS_BACKED_BY_BOTH
+
+config ROMFS_ON_MTD
+	bool
+	default y if ROMFS_BACKED_BY_MTD || ROMFS_BACKED_BY_BOTH
#6 lore raw

PATCH 4/4 MTD: Fix a bad dependency in the Blackfin code

In reply to David Howells

From: Bernd Schmidt <[email protected]>

Fix a bad dependency in the Blackfin code on a RomFS config symbol that doesn't exist upstream.

---

 arch/blackfin/kernel/process.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
arch/blackfin/kernel/process.c +1 -1
index 33e2e89..7f7ce07 100644
@@ -337,7 +337,7 @@ int _access_ok(unsigned long addr, unsigned long size)
 	if (addr >= memory_mtd_end && (addr + size) <= physical_mem_end)
 		return 1;
 
-#ifdef CONFIG_ROMFS_MTD_FS
+#ifdef CONFIG_ROMFS_ON_MTD
 	/* For XIP, allow user space to use pointers within the ROMFS.  */
 	if (addr >= memory_mtd_start && (addr + size) <= memory_mtd_end)
 		return 1;