Skip to contentSkip to conversation

CMA + VCMM integration

RFC Superseded Michal Nazarewicz 3 participants 20 messages Sep 6, 2010 – Sep 21, 2010

Subsystems (9)

v5 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v5 cover CMA + VCMM integration

Hello everyone,

This patchset introduces a draft of a redesign of Zach Pfeffer's VCMM. Not all of the functionality of the original VCMM has been ported into this patchset. This is mostly meant as RFC. Moreover, the code for VCMM implementation in this RFC has not been tested.

CMA has not been changed compared to the previous CMA versions so no aspects discussed on the list have been addressed yet.

The redesigned VCMM now uses notion of drivers -- a VCM context is created for each MMU on the platform and each such context is handled by a VCM driver. A context (or may contexts) for One-to-One mappings is created as well and handled with a One-to-One VCM driver.

The patchset introduces a sample (or a template if you will) VCM MMU driver as well as a VCM CMA One-to-One driver so it is shown by example how VCM drivers are written.

The VCMM framework proposed by this patchset also introduces a vcm_make_binding() call which allocates physical memory, creates virtual address reservation and binds the two together. This makes life easier for One-to-One mappings and if device drivers limit their use of VCM API to a subset of functionality the can work on systems with or without MMU with no modifications (only the VCM context would need to change).

Please refer to documentation in the second and seventh patch for more information regarding CMA and VCMM respectively.

Michal Nazarewicz (9):
lib: rbtree: rb_root_init() function added
mm: cma: Contiguous Memory Allocator added
mm: cma: Added SysFS support
mm: cma: Added command line parameters support
mm: cma: Test device and application added
ARM: cma: Added CMA to Aquila, Goni and c210 universal boards
mm: vcm: Virtual Contiguous Memory framework added
mm: vcm: Sample driver added
mm: vcm: vcm-cma: VCM CMA driver added

 Documentation/00-INDEX                             |    4 +
 .../ABI/testing/sysfs-kernel-mm-contiguous         |   53 +
 Documentation/contiguous-memory.txt                |  623 +++++++++
 Documentation/kernel-parameters.txt                |    7 +
 Documentation/virtual-contiguous-memory.txt        |  866 ++++++++++++
 arch/arm/mach-s5pv210/mach-aquila.c                |   31 +
 arch/arm/mach-s5pv210/mach-goni.c                  |   31 +
 arch/arm/mach-s5pv310/mach-universal_c210.c        |   23 +
 drivers/misc/Kconfig                               |    8 +
 drivers/misc/Makefile                              |    1 +
 drivers/misc/cma-dev.c                             |  202 +++
 include/linux/cma.h                                |  479 +++++++
 include/linux/rbtree.h                             |   11 +
 include/linux/vcm-cma.h                            |   38 +
 include/linux/vcm-drv.h                            |  299 +++++
 include/linux/vcm-sample.h                         |   30 +
 include/linux/vcm.h                                |  275 ++++
 mm/Kconfig                                         |  123 ++
 mm/Makefile                                        |    5 +
 mm/cma-best-fit.c                                  |  407 ++++++
 mm/cma.c                                           | 1377 ++++++++++++++++++++
 mm/vcm-cma.c                                       |   84 ++
 mm/vcm-sample.c                                    |  120 ++
 mm/vcm.c                                           |  932 +++++++++++++
 tools/cma/cma-test.c                               |  386 ++++++
 25 files changed, 6415 insertions(+), 0 deletions(-)
 create mode 100644 Documentation/ABI/testing/sysfs-kernel-mm-contiguous
 create mode 100644 Documentation/contiguous-memory.txt
 create mode 100644 Documentation/virtual-contiguous-memory.txt
 create mode 100644 drivers/misc/cma-dev.c
 create mode 100644 include/linux/cma.h
 create mode 100644 include/linux/vcm-cma.h
 create mode 100644 include/linux/vcm-drv.h
 create mode 100644 include/linux/vcm-sample.h
 create mode 100644 include/linux/vcm.h
 create mode 100644 mm/cma-best-fit.c
 create mode 100644 mm/cma.c
 create mode 100644 mm/vcm-cma.c
 create mode 100644 mm/vcm-sample.c
 create mode 100644 mm/vcm.c
 create mode 100644 tools/cma/cma-test.c
#1 lore raw

RFC v5 1/9 lib: rbtree: rb_root_init() function added

Added a rb_root_init() function which initialises a rb_root structure as a red-black tree with at most one element. The rationale is that using rb_root_init(root, node) is more straightforward and cleaner then first initialising and empty tree followed by an insert operation.

---
 include/linux/rbtree.h |   11 +++++++++++
 1 files changed, 11 insertions(+), 0 deletions(-)
include/linux/rbtree.h +11 -0
index 7066acb..5b6dc66 100644
@@ -130,6 +130,17 @@ static inline void rb_set_color(struct rb_node *rb, int color)
 }
 
 #define RB_ROOT	(struct rb_root) { NULL, }
+
+static inline void rb_root_init(struct rb_root *root, struct rb_node *node)
+{
+	root->rb_node = node;
+	if (node) {
+		node->rb_parent_color = RB_BLACK; /* black, no parent */
+		node->rb_left  = NULL;
+		node->rb_right = NULL;
+	}
+}
+
 #define	rb_entry(ptr, type, member) container_of(ptr, type, member)
 
 #define RB_EMPTY_ROOT(root)	((root)->rb_node == NULL)
Signature · 1 line
1.7.1
#2 lore raw

RFC v5 2/9 mm: cma: Contiguous Memory Allocator added

In reply to Michal Nazarewicz

The Contiguous Memory Allocator framework is a set of APIs for allocating physically contiguous chunks of memory.

Various chips require contiguous blocks of memory to operate. Those chips include devices such as cameras, hardware video decoders and encoders, etc.

The code is highly modular and customisable to suit the needs of various users. Set of regions reserved for CMA can be configured per-platform and it is easy to add custom allocator algorithms if one has such need.

---
 Documentation/00-INDEX              |    2 +
 Documentation/contiguous-memory.txt |  544 +++++++++++++++++++++
 include/linux/cma.h                 |  434 +++++++++++++++++
 mm/Kconfig                          |   41 ++
 mm/Makefile                         |    2 +
 mm/cma-best-fit.c                   |  407 ++++++++++++++++
 mm/cma.c                            |  911 +++++++++++++++++++++++++++++++++++
 7 files changed, 2341 insertions(+), 0 deletions(-)
 create mode 100644 Documentation/contiguous-memory.txt
 create mode 100644 include/linux/cma.h
 create mode 100644 mm/cma-best-fit.c
 create mode 100644 mm/cma.c
Documentation/00-INDEX +2 -0
index 8dfc670..f93e787 100644
@@ -94,6 +94,8 @@ connector/
 	- docs on the netlink based userspace<->kernel space communication mod.
 console/
 	- documentation on Linux console drivers.
+contiguous-memory.txt
+	- documentation on physically-contiguous memory allocation framework.
 cpu-freq/
 	- info on CPU frequency and voltage scaling.
 cpu-hotplug.txt
Documentation/contiguous-memory.txt new file +544 -0

544 lines not on this page · raw message

include/linux/cma.h new file +434 -0

434 lines not on this page · raw message

mm/Kconfig +41 -0

44 lines not on this page · raw message

mm/Makefile +2 -0
index 34b2546..d8c717f 100644
@@ -47,3 +47,5 @@ obj-$(CONFIG_MEMORY_FAILURE) += memory-failure.o
 obj-$(CONFIG_HWPOISON_INJECT) += hwpoison-inject.o
 obj-$(CONFIG_DEBUG_KMEMLEAK) += kmemleak.o
 obj-$(CONFIG_DEBUG_KMEMLEAK_TEST) += kmemleak-test.o
+obj-$(CONFIG_CMA) += cma.o
+obj-$(CONFIG_CMA_BEST_FIT) += cma-best-fit.o
mm/cma-best-fit.c new file +407 -0

407 lines not on this page · raw message

mm/cma.c new file +911 -0

911 lines not on this page · raw message

Signature · 1 line
1.7.1
#3 lore raw

RFC v5 3/9 mm: cma: Added SysFS support

In reply to Michal Nazarewicz

The SysFS development interface lets one change the map attribute at run time as well as observe what regions have been reserved.

---
 .../ABI/testing/sysfs-kernel-mm-contiguous         |   53 +++
 Documentation/contiguous-memory.txt                |    4 +
 include/linux/cma.h                                |    7 +
 mm/Kconfig                                         |   26 ++-
 mm/cma.c                                           |  345 +++++++++++++++++++-
 5 files changed, 430 insertions(+), 5 deletions(-)
 create mode 100644 Documentation/ABI/testing/sysfs-kernel-mm-contiguous
Documentation/ABI/testing/sysfs-kernel-mm-contiguous new file +53 -0
new file mode 100644
index 0000000..8df15bc
@@ -0,0 +1,53 @@
+What:		/sys/kernel/mm/contiguous/
+Date:		August 2010
+Contact:	Michal Nazarewicz <[email protected]>
+Description:
+		If CMA has been built with SysFS support,
+		/sys/kernel/mm/contiguous/ contains a file called
+		"map", a file called "allocators" and a directory
+		called "regions".
+
+		The "map" file lets one change the CMA's map attribute
+		at run-time.
+
+		The "allocators" file list all registered allocators.
+		Allocators with no name are listed as a single minus
+		sign.
+
+		The "regions" directory list all reserved regions.
+
+		For more details see
+		Documentation/contiguous-memory.txt.
+
+What:		/sys/kernel/mm/contiguous/regions/
+Date:		August 2010
+Contact:	Michal Nazarewicz <[email protected]>
+Description:
+		The /sys/kernel/mm/contiguous/regions/ directory
+		contain directories for each registered CMA region.
+		The name of the directory is the same as the start
+		address of the region.
+
+		If region is named there is also a symbolic link named
+		like the region pointing to the region's directory.
+
+		Such directory contains the following files:
+
+		* "name"  -- the name of the region or an empty file
+		* "start" -- starting address of the region (formatted
+		            with %p, ie. hex).
+		* "size"  -- size of the region (in bytes).
+		* "free"  -- free space in the region (in bytes).
+		* "users" -- number of chunks allocated in the region.
+		* "alloc" -- name of the allocator.
+
+		If allocator is not attached to the region, "alloc" is
+		either the name of desired allocator in square
+		brackets (ie. "[foo]") or an empty file if region is
+		to be attached to default allocator.  If an allocator
+		is attached to the region. "alloc" is either its name
+		or "-" if attached allocator has no name.
+
+		If there are no chunks allocated in given region
+		("users" is "0") then a name of desired allocator can
+		be written to "alloc".
Documentation/contiguous-memory.txt +4 -0
index e470c6f..15aff7a 100644
@@ -256,6 +256,10 @@
      iff it matched in previous pattern.  If the second part is
      omitted it will mach any type of memory requested by device.
 
+     If SysFS support is enabled, this attribute is accessible via
+     SysFS and can be changed at run-time by writing to
+     /sys/kernel/mm/contiguous/map.
+
      Some examples (whitespace added for better readability):
 
          cma_map = foo/quaz = r1;
include/linux/cma.h +7 -0
index f6f9cb5..d0f41f4 100644
@@ -18,6 +18,9 @@
 #include <linux/rbtree.h>
 #include <linux/list.h>
 #include <linux/init.h>
+#if defined CONFIG_CMA_SYSFS
+#  include <linux/kobject.h>
+#endif
 
 
 struct device;
@@ -204,6 +207,10 @@ struct cma_region {
 	unsigned users;
 	struct list_head list;
 
+#if defined CONFIG_CMA_SYSFS
+	struct kobject kobj;
+#endif
+
 	unsigned used:1;
 	unsigned registered:1;
 	unsigned reserved:1;
mm/Kconfig +23 -3
index 86043a3..8bed799 100644
@@ -322,16 +322,36 @@ config CMA
 	  For more information see <Documentation/contiguous-memory.txt>.
 	  If unsure, say "n".
 
-config CMA_DEBUG
-	bool "CMA debug messages (DEVELOPEMENT)"
+config CMA_DEVELOPEMENT
+	bool "Include CMA developement features"
 	depends on CMA
 	help
+	  This lets you enable some developement features of the CMA
+	  framework.  It does not add any code to the kernel.
+
+	  Those options are mostly usable during development and testing.
+	  If unsure, say "n".
+
+config CMA_DEBUG
+	bool "CMA debug messages"
+	depends on CMA_DEVELOPEMENT
+	help
 	  Turns on debug messages in CMA.  This produces KERN_DEBUG
 	  messages for every CMA call as well as various messages while
 	  processing calls such as cma_alloc().  This option does not
 	  affect warning and error messages.
 
-	  This is mostly used during development.  If unsure, say "n".
+config CMA_SYSFS
+	bool "CMA SysFS interface support"
+	depends on CMA_DEVELOPEMENT
+	help
+	  Enable support for SysFS interface.  The interface is available
+	  under /sys/kernel/mm/contiguous.  Each region and allocator is
+	  represented there.
+
+	  For more information consult
+	  <Documentation/contiguous-memory.txt> and
+	  <Documentation/ABI/testing/sysfs-kernel-mm-contiguous> files.
 
 config CMA_BEST_FIT
 	bool "CMA best-fit allocator"
mm/cma.c +343 -2

376 lines not on this page · raw message

Signature · 1 line
1.7.1
#8 lore raw

RFC v5 4/9 mm: cma: Added command line parameters support

In reply to Michal Nazarewicz

This patch adds a pair of early parameters ("cma" and "cma.map") which let one override the CMA configuration given by platform without the need to recompile the kernel.

---
 Documentation/contiguous-memory.txt |   85 ++++++++++++++++++++++--
 Documentation/kernel-parameters.txt |    7 ++
 mm/Kconfig                          |    9 +++
 mm/cma.c                            |  125 +++++++++++++++++++++++++++++++++++
 4 files changed, 221 insertions(+), 5 deletions(-)
Documentation/contiguous-memory.txt +80 -5
index 15aff7a..3d9d42c 100644
@@ -88,6 +88,20 @@
            early region and the framework will handle the rest
            including choosing the right early allocator.
 
+    4. CMA allows a run-time configuration of the memory regions it
+       will use to allocate chunks of memory from.  The set of memory
+       regions is given on command line so it can be easily changed
+       without the need for recompiling the kernel.
+
+       Each region has it's own size, alignment demand, a start
+       address (physical address where it should be placed) and an
+       allocator algorithm assigned to the region.
+
+       This means that there can be different algorithms running at
+       the same time, if different devices on the platform have
+       distinct memory usage characteristics and different algorithm
+       match those the best way.
+
 ** Use cases
 
     Let's analyse some imaginary system that uses the CMA to see how
@@ -162,7 +176,6 @@
     This solution also shows how with CMA you can assign private pools
     of memory to each device if that is required.
 
-
     Allocation mechanisms can be replaced dynamically in a similar
     manner as well. Let's say that during testing, it has been
     discovered that, for a given shared region of 40 MiB,
@@ -217,6 +230,42 @@
      it will be set to a PAGE_SIZE.  start will be aligned to
      alignment.
 
+     If command line parameter support is enabled, this attribute can
+     also be overriden by a command line "cma" parameter.  When given
+     on command line its forrmat is as follows:
+
+         regions-attr  ::= [ regions [ ';' ] ]
+         regions       ::= region [ ';' regions ]
+
+         region        ::= REG-NAME
+                             '=' size
+                           [ '@' start ]
+                           [ '/' alignment ]
+                           [ ':' ALLOC-NAME ]
+
+         size          ::= MEMSIZE   // size of the region
+         start         ::= MEMSIZE   // desired start address of
+                                     // the region
+         alignment     ::= MEMSIZE   // alignment of the start
+                                     // address of the region
+
+     REG-NAME specifies the name of the region.  All regions given at
+     via the regions attribute need to have a name.  Moreover, all
+     regions need to have a unique name.  If two regions have the same
+     name it is unspecified which will be used when requesting to
+     allocate memory from region with given name.
+
+     ALLOC-NAME specifies the name of allocator to be used with the
+     region.  If no allocator name is provided, the "default"
+     allocator will be used with the region.  The "default" allocator
+     is, of course, the first allocator that has been registered. ;)
+
+     size, start and alignment are specified in bytes with suffixes
+     that memparse() accept.  If start is given, the region will be
+     reserved on given starting address (or at close to it as
+     possible).  If alignment is specified, the region will be aligned
+     to given value.
+
 **** Map
 
      The format of the "map" attribute is as follows:
@@ -260,8 +309,33 @@
      SysFS and can be changed at run-time by writing to
      /sys/kernel/mm/contiguous/map.
 
+     If command line parameter support is enabled, this attribute can
+     also be overriden by a command line "cma.map" parameter.
+
+**** Examples
+
      Some examples (whitespace added for better readability):
 
+         cma = r1 = 64M       // 64M region
+                    @512M       // starting at address 512M
+                                // (or at least as near as possible)
+                    /1M         // make sure it's aligned to 1M
+                    :foo(bar);  // uses allocator "foo" with "bar"
+                                // as parameters for it
+               r2 = 64M       // 64M region
+                    /1M;        // make sure it's aligned to 1M
+                                // uses the first available allocator
+               r3 = 64M       // 64M region
+                    @512M       // starting at address 512M
+                    :foo;       // uses allocator "foo" with no parameters
+
+         cma_map = foo = r1;
+                       // device foo with kind==NULL uses region r1
+
+                   foo/quaz = r2;  // OR:
+                   /quaz = r2;
+                       // device foo with kind == "quaz" uses region r2
+
          cma_map = foo/quaz = r1;
                        // device foo with type == "quaz" uses region r1
 
@@ -529,10 +603,11 @@
 
         int cma_set_defaults(struct cma_region *regions, const char *map)
 
-    It needs to be called prior to reserving regions.  It let one
-    specify the list of regions defined by platform and the map
-    attribute.  The map may point to a string in __initdata.  See
-    above in this document for example usage of this function.
+    It needs to be called after early params have been parsed but
+    prior to reserving regions.  It let one specify the list of
+    regions defined by platform and the map attribute.  The map may
+    point to a string in __initdata.  See above in this document for
+    example usage of this function.
 
 ** Future work
 
Documentation/kernel-parameters.txt +7 -0
index b7eb33f..015e458 100644
@@ -43,6 +43,7 @@ parameter is applicable:
 	AVR32	AVR32 architecture is enabled.
 	AX25	Appropriate AX.25 support is enabled.
 	BLACKFIN Blackfin architecture is enabled.
+	CMA	Contiguous Memory Allocator is enabled.
 	EDD	BIOS Enhanced Disk Drive Services (EDD) is enabled
 	EFI	EFI Partitioning (GPT) is enabled
 	EIDE	EIDE/ATAPI support is enabled.
@@ -478,6 +479,12 @@ and is between 256 and 4096 characters. It is defined in the file
 			Also note the kernel might malfunction if you disable
 			some critical bits.
 
+	cma=		[CMA] List of CMA regions.
+			See Documentation/contiguous-memory.txt for details.
+
+	cma.map=	[CMA] CMA mapping
+			See Documentation/contiguous-memory.txt for details.
+
 	cmo_free_hint=	[PPC] Format: { yes | no }
 			Specify whether pages are marked as being inactive
 			when they are freed.  This is used in CMO environments
mm/Kconfig +9 -0
index 8bed799..b410910 100644
@@ -353,6 +353,15 @@ config CMA_SYSFS
 	  <Documentation/contiguous-memory.txt> and
 	  <Documentation/ABI/testing/sysfs-kernel-mm-contiguous> files.
 
+config CMA_CMDLINE
+	bool "CMA command line parameters support"
+	depends on CMA_DEVELOPEMENT
+	help
+	  Enable support for cma and cma.map command line parameters.
+	  This lets overwrite the CMA defaults defined by for the
+	  platform.  This should only be usable during development and
+	  testing.
+
 config CMA_BEST_FIT
 	bool "CMA best-fit allocator"
 	depends on CMA
mm/cma.c +125 -0

137 lines not on this page · raw message

Signature · 1 line
1.7.1
#9 lore raw

RFC v5 5/9 mm: cma: Test device and application added

In reply to Michal Nazarewicz

This patch adds a "cma" misc device which lets user space use the CMA API. This device is meant for testing. A testing application is also provided.

---
 drivers/misc/Kconfig   |    8 +
 drivers/misc/Makefile  |    1 +
 drivers/misc/cma-dev.c |  202 +++++++++++++++++++++++++
 include/linux/cma.h    |   38 +++++
 tools/cma/cma-test.c   |  386 ++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 635 insertions(+), 0 deletions(-)
 create mode 100644 drivers/misc/cma-dev.c
 create mode 100644 tools/cma/cma-test.c
drivers/misc/Kconfig +8 -0
index 2c38d4e..ac1dd45 100644
@@ -407,4 +407,12 @@ source "drivers/misc/eeprom/Kconfig"
 source "drivers/misc/cb710/Kconfig"
 source "drivers/misc/iwmc3200top/Kconfig"
 
+config CMA_DEVICE
+	tristate "CMA misc device (DEVELOPEMENT)"
+	depends on CMA_DEVELOPEMENT
+	help
+	  The CMA misc device allows allocating contiguous memory areas
+	  from user space.  This is mostly for testing of the CMA
+	  framework.
+
 endif # MISC_DEVICES
drivers/misc/Makefile +1 -0
index 21b4761..b08844a 100644
@@ -36,3 +36,4 @@ obj-y				+= cb710/
 obj-$(CONFIG_VMWARE_BALLOON)	+= vmware_balloon.o
 obj-$(CONFIG_ARM_CHARLCD)	+= arm-charlcd.o
 obj-$(CONFIG_PCH_PHUB)		+= pch_phub.o
+obj-$(CONFIG_CMA_DEVICE)	+= cma-dev.o
drivers/misc/cma-dev.c new file +202 -0

202 lines not on this page · raw message

include/linux/cma.h +38 -0
index d0f41f4..409bae4 100644
@@ -11,6 +11,44 @@
  * See Documentation/contiguous-memory.txt for details.
  */
 
+#include <linux/ioctl.h>
+#include <linux/types.h>
+
+
+#define CMA_MAGIC (('c' << 24) | ('M' << 16) | ('a' << 8) | 0x42)
+
+enum {
+	CMA_REQ_DEV_KIND,
+	CMA_REQ_FROM_REG
+};
+
+/**
+ * An information about area exportable to user space.
+ * @magic:	must always be CMA_MAGIC.
+ * @type:	type of the request.
+ * @spec:	either "dev/kind\0" or "regions\0" depending on @type.
+ *		In any case, the string must be NUL terminated.
+ *		additionally, in the latter case scanning stops at
+ *		semicolon (';').
+ * @size:	size of the chunk to allocate.
+ * @alignment:	desired alignment of the chunk (must be power of two or zero).
+ * @start:	when ioctl() finishes this stores physical address of the chunk.
+ */
+struct cma_alloc_request {
+	__u32 magic;
+	__u32 type;
+
+	/* __u64 to be compatible accross 32 and 64 bit systems. */
+	__u64 size;
+	__u64 alignment;
+	__u64 start;
+
+	char spec[32];
+};
+
+#define IOCTL_CMA_ALLOC    _IOWR('p', 0, struct cma_alloc_request)
+
+
 /***************************** Kernel level API *****************************/
 
 #ifdef __KERNEL__
tools/cma/cma-test.c new file +386 -0

386 lines not on this page · raw message

Signature · 1 line
1.7.1
#10 lore raw

RFC v5 6/9 ARM: cma: Added CMA to Aquila, Goni and c210 universal boards

In reply to Michal Nazarewicz

This commit adds CMA memory reservation code to Aquila, Goni and c210 universal boards.

---
 arch/arm/mach-s5pv210/mach-aquila.c         |   31 +++++++++++++++++++++++++++
 arch/arm/mach-s5pv210/mach-goni.c           |   31 +++++++++++++++++++++++++++
 arch/arm/mach-s5pv310/mach-universal_c210.c |   23 ++++++++++++++++++++
 3 files changed, 85 insertions(+), 0 deletions(-)
arch/arm/mach-s5pv210/mach-aquila.c +31 -0
index bf772de..ff89534 100644
@@ -19,6 +19,7 @@
 #include <linux/gpio_keys.h>
 #include <linux/input.h>
 #include <linux/gpio.h>
+#include <linux/cma.h>
 
 #include <asm/mach/arch.h>
 #include <asm/mach/map.h>
@@ -486,6 +487,35 @@ static struct platform_device *aquila_devices[] __initdata = {
 	&s5p_device_fimc2,
 };
 
+static void __init aquila_reserve(void)
+{
+	static struct cma_region regions[] = {
+		{
+			.name		= "fw",
+			.size		=   1 << 20,
+			{ .alignment	= 128 << 10 },
+			.start		= 0x32000000,
+		},
+		{
+			.name		= "b1",
+			.size		=  32 << 20,
+			.start		= 0x33000000,
+		},
+		{
+			.name		= "b2",
+			.size		=  16 << 20,
+			.start		= 0x44000000,
+		},
+		{ }
+	};
+
+	static const char map[] __initconst =
+		"s5p-mfc5/f=fw;s5p-mfc5/a=b1;s5p-mfc5/b=b2;*=b1,b2";
+
+	cma_set_defaults(regions, map);
+	cma_early_regions_reserve(NULL);
+}
+
 static void __init aquila_map_io(void)
 {
 	s5p_init_io(NULL, 0, S5P_VA_CHIPID);
@@ -523,4 +553,5 @@ MACHINE_START(AQUILA, "Aquila")
 	.map_io		= aquila_map_io,
 	.init_machine	= aquila_machine_init,
 	.timer		= &s3c24xx_timer,
+	.reserve	= aquila_reserve,
 MACHINE_END
arch/arm/mach-s5pv210/mach-goni.c +31 -0
index fdc5cca..10dc2b9 100644
@@ -19,6 +19,7 @@
 #include <linux/gpio_keys.h>
 #include <linux/input.h>
 #include <linux/gpio.h>
+#include <linux/cma.h>
 
 #include <asm/mach/arch.h>
 #include <asm/mach/map.h>
@@ -467,6 +468,35 @@ static struct platform_device *goni_devices[] __initdata = {
 	&s3c_device_hsmmc2,
 };
 
+static void __init goni_reserve(void)
+{
+	static struct cma_region regions[] = {
+		{
+			.name		= "fw",
+			.size		=   1 << 20,
+			{ .alignment	= 128 << 10 },
+			.start		= 0x32000000,
+		},
+		{
+			.name		= "b1",
+			.size		=  32 << 20,
+			.start		= 0x33000000,
+		},
+		{
+			.name		= "b2",
+			.size		=  16 << 20,
+			.start		= 0x44000000,
+		},
+		{ }
+	};
+
+	static const char map[] __initconst =
+		"s5p-mfc5/f=fw;s5p-mfc5/a=b1;s5p-mfc5/b=b2;*=b1,b2";
+
+	cma_set_defaults(regions, map);
+	cma_early_regions_reserve(NULL);
+}
+
 static void __init goni_map_io(void)
 {
 	s5p_init_io(NULL, 0, S5P_VA_CHIPID);
@@ -498,4 +528,5 @@ MACHINE_START(GONI, "GONI")
 	.map_io		= goni_map_io,
 	.init_machine	= goni_machine_init,
 	.timer		= &s3c24xx_timer,
+	.reserve	= goni_reserve,
 MACHINE_END
arch/arm/mach-s5pv310/mach-universal_c210.c +23 -0
index b57efae..f735c52 100644
@@ -12,6 +12,7 @@
 #include <linux/input.h>
 #include <linux/gpio_keys.h>
 #include <linux/gpio.h>
+#include <linux/cma.h>
 
 #include <asm/mach/arch.h>
 #include <asm/mach-types.h>
@@ -126,6 +127,27 @@ static void __init universal_map_io(void)
 	s3c24xx_init_uarts(universal_uartcfgs, ARRAY_SIZE(universal_uartcfgs));
 }
 
+static void __init universal_reserve(void)
+{
+	static struct cma_region regions[] = {
+		{
+			.name		= "r",
+			.size		=  64 << 20,
+		},
+		{
+			.name		= "fw",
+			.size		=   1 << 20,
+		},
+		{ }
+	};
+
+	static const char map[] __initconst =
+		"s3c-mfc5/f=fw;*=r";
+
+	cma_set_defaults(regions, map);
+	cma_early_regions_reserve(NULL);
+}
+
 static void __init universal_machine_init(void)
 {
 #ifdef CONFIG_CACHE_L2X0
@@ -143,6 +165,7 @@ MACHINE_START(UNIVERSAL_C210, "UNIVERSAL_C210")
 	.boot_params	= S5P_PA_SDRAM + 0x100,
 	.init_irq	= s5pv310_init_irq,
 	.map_io		= universal_map_io,
+	.reserve	= universal_reserve,
 	.init_machine	= universal_machine_init,
 	.timer		= &s5pv310_timer,
 MACHINE_END
Signature · 1 line
1.7.1
#11 lore raw

RFC v5 7/9 mm: vcm: Virtual Contiguous Memory framework added

In reply to Michal Nazarewicz

This commit adds the Virtual Contiguous Memory framework which provides an abstraction for virtual address space provided by various MMUs present on the platform.

The framework uses plugable MMU drivers for hardware MMUs and if drivers obeys some limitations it can be also used on platforms with no MMU.

For more information see
<Documentation/virtual-contiguous-memory.txt>.

---
 Documentation/00-INDEX                      |    2 +
 Documentation/virtual-contiguous-memory.txt |  853 ++++++++++++++++++++++++
 include/linux/vcm-drv.h                     |  299 +++++++++
 include/linux/vcm.h                         |  275 ++++++++
 mm/Kconfig                                  |   30 +
 mm/Makefile                                 |    1 +
 mm/vcm.c                                    |  932 +++++++++++++++++++++++++++
 7 files changed, 2392 insertions(+), 0 deletions(-)
 create mode 100644 Documentation/virtual-contiguous-memory.txt
 create mode 100644 include/linux/vcm-drv.h
 create mode 100644 include/linux/vcm.h
 create mode 100644 mm/vcm.c
Documentation/00-INDEX +2 -0
index f93e787..7c79ffa 100644
@@ -344,6 +344,8 @@ video-output.txt
 	- sysfs class driver interface to enable/disable a video output device.
 video4linux/
 	- directory with info regarding video/TV/radio cards and linux.
+virtual-contiguous-memory.txt
+	- documentation on virtual contiguous memory manager framework.
 vm/
 	- directory with info on the Linux vm code.
 volatile-considered-harmful.txt
Documentation/virtual-contiguous-memory.txt new file +853 -0

853 lines not on this page · raw message

include/linux/vcm-drv.h new file +299 -0

299 lines not on this page · raw message

include/linux/vcm.h new file +275 -0

275 lines not on this page · raw message

mm/Kconfig +30 -0
index b410910..0445f68 100644
@@ -371,3 +371,33 @@ config CMA_BEST_FIT
 	  the number of allocated regions and usually much smaller).  It
 	  allocates area from the smallest hole that is big enough for
 	  allocation in question.
+
+
+config VCM
+	bool "Virtual Contiguous Memory framework"
+	help
+	  This enables the Virtual Contiguous Memory framework which
+	  provides an abstraction for virtual address space provided by
+	  various MMUs present on the platform.
+
+	  The framework uses plugable MMU drivers for hardware MMUs and
+	  if drivers obeys some limitations it can be also used on
+	  platforms with no MMU.
+
+	  For more information see
+	  <Documentation/virtual-contiguous-memory.txt>.  If unsure, say
+	  "n".
+
+# Select it if you need vcm_mmu wrapper driver
+config VCM_MMU
+	select VCM_PHYS
+	select GENERIC_ALLOCATOR
+	bool
+
+# Select if you need vcm_o2o wrapper driver
+config VCM_O2O
+	bool
+
+# Select if you need vcm_phys_alloc() or vcm_phys_walk() functions
+config VCM_PHYS
+	bool
mm/Makefile +1 -0
index d8c717f..e908202 100644
@@ -49,3 +49,4 @@ obj-$(CONFIG_DEBUG_KMEMLEAK) += kmemleak.o
 obj-$(CONFIG_DEBUG_KMEMLEAK_TEST) += kmemleak-test.o
 obj-$(CONFIG_CMA) += cma.o
 obj-$(CONFIG_CMA_BEST_FIT) += cma-best-fit.o
+obj-$(CONFIG_VCM) += vcm.o
mm/vcm.c new file +932 -0

932 lines not on this page · raw message

Signature · 1 line
1.7.1
#13 lore raw

RFC v5 8/9 mm: vcm: Sample driver added

In reply to Michal Nazarewicz

This commit adds a sample Virtual Contiguous Memory framework driver. It handles no real hardware and is there only for demonstrating purposes.

---
 Documentation/virtual-contiguous-memory.txt |    3 +
 include/linux/vcm-sample.h                  |   30 +++++++
 mm/Kconfig                                  |    9 ++
 mm/Makefile                                 |    1 +
 mm/vcm-sample.c                             |  120 +++++++++++++++++++++++++++
 5 files changed, 163 insertions(+), 0 deletions(-)
 create mode 100644 include/linux/vcm-sample.h
 create mode 100644 mm/vcm-sample.c
Documentation/virtual-contiguous-memory.txt +3 -0
index 0c0e90c..6d1014c 100644
@@ -730,6 +730,9 @@ already there.
 If you want to use this wrapper, you need to select VCM_MMU Kconfig
 option.
 
+There is a sample driver provided which provides a template for real
+drivers.  It can be found in [[file:../mm/vcm-sample.c][mm/vcm-sample.c]] file.
+
 *** Context creation
 
 Similarly to normal drivers, MMU driver needs to provide a context
include/linux/vcm-sample.h new file +30 -0
new file mode 100644
index 0000000..9a79403
@@ -0,0 +1,30 @@
+/*
+ * Virtual Contiguous Memory driver driver template header
+ * Copyright (c) 2010 by Samsung Electronics.
+ * Written by Michal Nazarewicz (m.nazarewicz at samsung.com)
+ *
+ * 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 optional) any later version of the license.
+ */
+
+/*
+ * See Documentation/virtual-contiguous-memory.txt for details.
+ */
+
+#ifndef __LINUX_VCM_SAMPLE_H
+#define __LINUX_VCM_SAMPLE_H
+
+#include <linux/vcm.h>
+
+struct vcm;
+
+/**
+ * vcm_samp_create() - creates a VCM context
+ *
+ * ... Documentation goes here ...
+ */
+struct vcm *__must_check vcm_samp_create(/* ... */);
+
+#endif
mm/Kconfig +9 -0
index 0445f68..be040e7 100644
@@ -401,3 +401,12 @@ config VCM_O2O
 # Select if you need vcm_phys_alloc() or vcm_phys_walk() functions
 config VCM_PHYS
 	bool
+
+config VCM_SAMP
+	bool "VCM sample driver"
+	depends on VCM
+	select VCM_MMU
+	help
+	  This enables a sample driver for the VCM framework.  This driver
+	  does not handle any real harwdare.  It's merely an template of
+	  how for real drivers.
mm/Makefile +1 -0
index e908202..c465dfa 100644
@@ -50,3 +50,4 @@ obj-$(CONFIG_DEBUG_KMEMLEAK_TEST) += kmemleak-test.o
 obj-$(CONFIG_CMA) += cma.o
 obj-$(CONFIG_CMA_BEST_FIT) += cma-best-fit.o
 obj-$(CONFIG_VCM) += vcm.o
+obj-$(CONFIG_VCM_SAMPLE) += vcm-sample.o
mm/vcm-sample.c new file +120 -0

120 lines not on this page · raw message

Signature · 1 line
1.7.1
#16 lore raw

RFC v5 9/9 mm: vcm: vcm-cma: VCM CMA driver added

In reply to Michal Nazarewicz

This commit adds a VCM driver that instead of using real hardware MMU emulates one and uses CMA for allocating contiguous memory chunks.

---
 Documentation/virtual-contiguous-memory.txt |   12 ++++-
 include/linux/vcm-cma.h                     |   38 ++++++++++++
 mm/Kconfig                                  |   14 +++++
 mm/Makefile                                 |    1 +
 mm/vcm-cma.c                                |   84 +++++++++++++++++++++++++++
 5 files changed, 148 insertions(+), 1 deletions(-)
 create mode 100644 include/linux/vcm-cma.h
 create mode 100644 mm/vcm-cma.c
Documentation/virtual-contiguous-memory.txt +11 -1
index 6d1014c..01e2e6c 100644
@@ -496,7 +496,17 @@ able to run with One-to-One driver you should limit operations to:
 
 under some conditions, vcm_map() may also work.
 
-There are no One-to-One drivers at this time.
+*** VCM CMA
+
+VCM CMA driver is a One-to-One driver which uses CMA (see
+[[file:contiguous-memory.txt][contiguous-memory.txt]]) to allocate physically contiguous memory.  VCM
+CMA context is created by calling:
+
+	struct vcm *__must_check
+	vcm_cma_create(const char *regions, dma_addr_t alignment);
+
+Its first argument is the list of regions that CMA should try to
+allocate memory from.  The second argument is required alignment.
 
 * Writing a VCM driver
 
include/linux/vcm-cma.h new file +38 -0
new file mode 100644
index 0000000..bc06767
@@ -0,0 +1,38 @@
+/*
+ * Virtual Contiguous Memory driver for CMA header
+ * Copyright (c) 2010 by Samsung Electronics.
+ * Written by Michal Nazarewicz (m.nazarewicz at samsung.com)
+ *
+ * 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 optional) any later version of the license.
+ */
+
+/*
+ * See Documentation/virtual-contiguous-memory.txt for details.
+ */
+
+#ifndef __LINUX_VCM_CMA_H
+#define __LINUX_VCM_CMA_H
+
+#include <linux/types.h>
+
+struct vcm;
+
+/**
+ * vcm_cma_create() - creates a VCM context that fakes a hardware MMU
+ * @regions:	list of CMA regions physical allocations should be done
+ *		from.
+ * @alignment:	required alignment of allocations.
+ *
+ * This creates VCM context that can be used on platforms with no
+ * hardware MMU or for devices that aro conected to the bus directly.
+ * Because it does not represent real MMU it has some limitations:
+ * basically, vcm_alloc(), vcm_reserve() and vcm_bind() are likely to
+ * fail so vcm_make_binding() should be used instead.
+ */
+struct vcm *__must_check
+vcm_cma_create(const char *regions, dma_addr_t alignment);
+
+#endif
mm/Kconfig +14 -0
index be040e7..bf0c7f6 100644
@@ -410,3 +410,17 @@ config VCM_SAMP
 	  This enables a sample driver for the VCM framework.  This driver
 	  does not handle any real harwdare.  It's merely an template of
 	  how for real drivers.
+
+config VCM_CMA
+	bool "VCM CMA driver"
+	depends on VCM && CMA
+	select VCM_O2O
+	help
+	  This enables VCM driver that instead of using a real hardware
+	  MMU fakes one and uses a direct mapping.  It provides a subset
+	  of functionalities of a real MMU but if drivers limits their
+	  use of VCM to only supported operations they can work on
+	  both systems with and without MMU with no changes.
+
+	  For more information see
+	  <Documentation/virtual-contiguous-memory-cma.txt>.
mm/Makefile +1 -0
index c465dfa..e376eef 100644
@@ -51,3 +51,4 @@ obj-$(CONFIG_CMA) += cma.o
 obj-$(CONFIG_CMA_BEST_FIT) += cma-best-fit.o
 obj-$(CONFIG_VCM) += vcm.o
 obj-$(CONFIG_VCM_SAMPLE) += vcm-sample.o
+obj-$(CONFIG_VCM_CMA) += vcm-cma.o
mm/vcm-cma.c new file +84 -0

84 lines not on this page · raw message

Signature · 1 line
1.7.1
#4 lore raw

Re: [RFCv5 3/9] mm: cma: Added SysFS support

In reply to Michal Nazarewicz

Documentation/ABI/testing/sysfs-kernel-mm-contiguous line 11

Quoted 13 lines · Michal Nazarewicz

+++ b/Documentation/ABI/testing/sysfs-kernel-mm-contiguous

Documentation/ABI/testing/sysfs-kernel-mm-contiguous new file +11 -0
@@ -0,0 +1,53 @@
+What:		/sys/kernel/mm/contiguous/
+Date:		August 2010
+Contact:	Michal Nazarewicz <[email protected]>
+Description:
+		If CMA has been built with SysFS support,
+		/sys/kernel/mm/contiguous/ contains a file called
+		"map", a file called "allocators" and a directory
+		called "regions".
+
+		The "map" file lets one change the CMA's map attribute
+		at run-time.

How?

Documentation/ABI/testing/sysfs-kernel-mm-contiguous line 15

Quoted 3 lines
Documentation/ABI/testing/sysfs-kernel-mm-contiguous new file +3 -0
+		The "allocators" file list all registered allocators.
+		Allocators with no name are listed as a single minus
+		sign.

So this returns more than one value?

Documentation/ABI/testing/sysfs-kernel-mm-contiguous line 17

Quoted 1 line
Documentation/ABI/testing/sysfs-kernel-mm-contiguous new file +1 -0
+		The "regions" directory list all reserved regions.

Same here?

Documentation/contiguous-memory.txt line 261

Quoted 9 lines
Documentation/contiguous-memory.txt +3 -0
@@ -256,6 +256,10 @@
      iff it matched in previous pattern.  If the second part is
      omitted it will mach any type of memory requested by device.
 
+     If SysFS support is enabled, this attribute is accessible via
+     SysFS and can be changed at run-time by writing to
+     /sys/kernel/mm/contiguous/map.

it is "sysfs" no wierd "SysFS" please.

include/linux/cma.h line 23

Quoted 11 lines
include/linux/cma.h +3 -0
index f6f9cb5..d0f41f4 100644
@@ -18,6 +18,9 @@
 #include <linux/rbtree.h>
 #include <linux/list.h>
 #include <linux/init.h>
+#if defined CONFIG_CMA_SYSFS
+#  include <linux/kobject.h>
+#endif

Just always include it, no need to put a #ifdef around it, right?

include/linux/cma.h struct cma_region { · line 212

Quoted 9 lines
include/linux/cma.h +3 -0
 struct device;
@@ -204,6 +207,10 @@ struct cma_region {
 	unsigned users;
 	struct list_head list;
 
+#if defined CONFIG_CMA_SYSFS
+	struct kobject kobj;
+#endif

Same here, no need for a #ifdef, right?

Especially as it will change your lifetime rules for this object, why would you not want this to always be the case?

Quoted 85 lines
include/linux/cma.h +1 -0
+
 	unsigned used:1;
 	unsigned registered:1;
 	unsigned reserved:1;
mm/Kconfig +23 -3
index 86043a3..8bed799 100644
@@ -322,16 +322,36 @@ config CMA
 	  For more information see <Documentation/contiguous-memory.txt>.
 	  If unsure, say "n".
 
-config CMA_DEBUG
-	bool "CMA debug messages (DEVELOPEMENT)"
+config CMA_DEVELOPEMENT
+	bool "Include CMA developement features"
 	depends on CMA
 	help
+	  This lets you enable some developement features of the CMA
+	  framework.  It does not add any code to the kernel.
+
+	  Those options are mostly usable during development and testing.
+	  If unsure, say "n".
+
+config CMA_DEBUG
+	bool "CMA debug messages"
+	depends on CMA_DEVELOPEMENT
+	help
 	  Turns on debug messages in CMA.  This produces KERN_DEBUG
 	  messages for every CMA call as well as various messages while
 	  processing calls such as cma_alloc().  This option does not
 	  affect warning and error messages.
 
-	  This is mostly used during development.  If unsure, say "n".
+config CMA_SYSFS
+	bool "CMA SysFS interface support"
+	depends on CMA_DEVELOPEMENT
+	help
+	  Enable support for SysFS interface.  The interface is available
+	  under /sys/kernel/mm/contiguous.  Each region and allocator is
+	  represented there.
+
+	  For more information consult
+	  <Documentation/contiguous-memory.txt> and
+	  <Documentation/ABI/testing/sysfs-kernel-mm-contiguous> files.
 
 config CMA_BEST_FIT
 	bool "CMA best-fit allocator"
mm/cma.c +8 -2
index 06d0d5a..955f08c 100644
@@ -38,8 +38,8 @@
 
 
 /*
- * Protects cma_regions, cma_allocators, cma_map, cma_map_length, and
- * cma_chunks_by_start.
+ * Protects cma_regions, cma_allocators, cma_map, cma_map_length,
+ * cma_kobj, cma_sysfs_regions and cma_chunks_by_start.
  */
 static DEFINE_MUTEX(cma_mutex);
 
@@ -143,7 +143,11 @@ int __init __must_check cma_early_region_register(struct cma_region *reg)
 
 /************************* Regions & Allocators *************************/
 
+static void __cma_sysfs_region_add(struct cma_region *reg);
+
 static int __cma_region_attach_alloc(struct cma_region *reg);
+static void __maybe_unused __cma_region_detach_alloc(struct cma_region *reg);
+
 
 /* List of all regions.  Named regions are kept before unnamed. */
 static LIST_HEAD(cma_regions);
@@ -226,6 +230,8 @@ int __must_check cma_region_register(struct cma_region *reg)
 	else
 		list_add_tail(&reg->list, &cma_regions);
 
+	__cma_sysfs_region_add(reg);
+
 done:
 	mutex_unlock(&cma_mutex);
 

mm/cma.c subsys_initcall(cma_init); · line 495

Quoted 8 lines
mm/cma.c +5 -0
@@ -482,6 +488,329 @@ subsys_initcall(cma_init);
 
 
 
+/************************* SysFS *************************/
+
+#if defined CONFIG_CMA_SYSFS
+
+static struct kobject cma_sysfs_regions;

No static kobjects please, that means the code is broken.

Quoted 18 lines
mm/cma.c +18 -0
+static int cma_sysfs_regions_ready;
+
+
+#define CMA_ATTR_INLINE(_type, _name)					\
+	(&((struct cma_ ## _type ## _attribute){			\
+		.attr	= {						\
+			.name	= __stringify(_name),			\
+			.mode	= 0644,					\
+		},							\
+		.show	= cma_sysfs_ ## _type ## _ ## _name ## _show,	\
+		.store	= cma_sysfs_ ## _type ## _ ## _name ## _store,	\
+	}).attr)
+
+#define CMA_ATTR_RO_INLINE(_type, _name)				\
+	(&((struct cma_ ## _type ## _attribute){			\
+		.attr	= {						\
+			.name	= __stringify(_name),			\
+			.mode	= 0444,					\

mm/cma.c subsys_initcall(cma_init); · line 523

Quoted 10 lines
mm/cma.c +10 -0
+		},							\
+		.show	= cma_sysfs_ ## _type ## _ ## _name ## _show,	\
+	}).attr)
+
+
+struct cma_root_attribute {
+	struct attribute attr;
+	ssize_t (*show)(char *buf);
+	int (*store)(const char *buf);
+};

Why can't you use a 'struct device' instead of using a "raw" kobject?

Quoted 8 lines
mm/cma.c +8 -0
+
+static ssize_t cma_sysfs_root_map_show(char *page)
+{
+	ssize_t len;
+
+	len = cma_map_length;
+	if (!len) {
+		*page = 0;

mm/cma.c subsys_initcall(cma_init); · line 541

Quoted 10 lines
mm/cma.c +10 -0
+		len = 0;
+	} else {
+		if (len > (size_t)PAGE_SIZE - 1)
+			len = (size_t)PAGE_SIZE - 1;
+		memcpy(page, cma_map, len);
+		page[len++] = '\n';
+	}
+
+	return len;
+}

What are you returning here? A "raw" buffer? Why? That's not what sysfs is for.

Quoted 12 lines
mm/cma.c +12 -0
+
+static int cma_sysfs_root_map_store(const char *page)
+{
+	ssize_t len = cma_map_validate(page);
+	char *val = NULL;
+
+	if (len < 0)
+		return len;
+
+	if (len) {
+		val = kmemdup(page, len + 1, GFP_KERNEL);
+		if (!val)

mm/cma.c subsys_initcall(cma_init); · line 563

Quoted 10 lines
mm/cma.c +10 -0
+			return -ENOMEM;
+		val[len] = '\0';
+	}
+
+	kfree(cma_map);
+	cma_map = val;
+	cma_map_length = len;
+
+	return 0;
+}

What is this doing? It doesn't look like a "text" file to me, or am I confused?

Quoted 7 lines
mm/cma.c +7 -0
+
+static ssize_t cma_sysfs_root_allocators_show(char *page)
+{
+	struct cma_allocator *alloc;
+	size_t left = PAGE_SIZE;
+	char *ch = page;
+

mm/cma.c subsys_initcall(cma_init); · line 580

Quoted 10 lines
mm/cma.c +10 -0
+	cma_foreach_allocator(alloc) {
+		ssize_t l = snprintf(ch, left, "%s ", alloc->name ?: "-");
+		ch   += l;
+		left -= l;
+	}
+
+	if (ch != page)
+		ch[-1] = '\n';
+	return ch - page;
+}

You are returning more than one value per sysfs file, why?

mm/cma.c subsys_initcall(cma_init); · line 590

Quoted 9 lines
mm/cma.c +9 -0
+static ssize_t
+cma_sysfs_root_show(struct kobject *kobj, struct attribute *attr, char *buf)
+{
+	struct cma_root_attribute *rattr =
+		container_of(attr, struct cma_root_attribute, attr);
+	ssize_t ret;
+
+	mutex_lock(&cma_mutex);
+	ret = rattr->show(buf);

Are you sure you are not going to want to pass the attribute and kobject to the show/store function?

Quoted 21 lines
mm/cma.c +21 -0
+	mutex_unlock(&cma_mutex);
+
+	return ret;
+}
+
+static ssize_t
+cma_sysfs_root_store(struct kobject *kobj, struct attribute *attr,
+		       const char *buf, size_t count)
+{
+	struct cma_root_attribute *rattr =
+		container_of(attr, struct cma_root_attribute, attr);
+	int ret;
+
+	mutex_lock(&cma_mutex);
+	ret = rattr->store(buf);
+	mutex_unlock(&cma_mutex);
+
+	return ret < 0 ? ret : count;
+}
+
+static struct kobj_type cma_sysfs_root_type = {

mm/cma.c subsys_initcall(cma_init); · line 621

Quoted 10 lines
mm/cma.c +10 -0
+	.sysfs_ops	= &(const struct sysfs_ops){
+		.show	= cma_sysfs_root_show,
+		.store	= cma_sysfs_root_store,
+	},
+	.default_attrs	= (struct attribute * []) {
+		CMA_ATTR_INLINE(root, map),
+		CMA_ATTR_RO_INLINE(root, allocators),
+		NULL
+	},
+};

This type is broken by design, and as per the documentation in the kernel, I now get to publically ridicule you for doing so (as explained in the documentation.)

Please go fix it up.

If you don't know why it is broken, then please, read the documentation provided in the kernel, it's as if I write it for no one to later pay attention to. Why do I even bother...

mm/cma.c subsys_initcall(cma_init); · line 625

Quoted 4 lines
mm/cma.c +4 -0
+
+static int __init cma_sysfs_init(void)
+{
+	static struct kobject root;

Again, no static kobjects please.

mm/cma.c subsys_initcall(cma_init); · line 634

Quoted 9 lines
mm/cma.c +9 -0
+	static struct kobj_type fake_type;
+
+	struct cma_region *reg;
+	int ret;
+
+	/* Root */
+	ret = kobject_init_and_add(&root, &cma_sysfs_root_type,
+				   mm_kobj, "contiguous");
+	if (unlikely(ret < 0)) {

Why "unlikely"? Is this a fast-path or something? Hint, I don't think so...

mm/cma.c subsys_initcall(cma_init); · line 642

Quoted 8 lines
mm/cma.c +8 -0
+		pr_err("init: unable to add root kobject: %d\n", ret);
+		return ret;
+	}
+
+	/* Regions */
+	ret = kobject_init_and_add(&cma_sysfs_regions, &fake_type,
+				   &root, "regions");
+	if (unlikely(ret < 0)) {

Same thing here.

Quoted 17 lines
mm/cma.c +17 -0
+		pr_err("init: unable to add regions kobject: %d\n", ret);
+		return ret;
+	}
+
+	mutex_lock(&cma_mutex);
+	cma_sysfs_regions_ready = 1;
+	cma_foreach_region(reg)
+		__cma_sysfs_region_add(reg);
+	mutex_unlock(&cma_mutex);
+
+	return 0;
+}
+device_initcall(cma_sysfs_init);
+
+
+
+struct cma_region_attribute {

mm/cma.c subsys_initcall(cma_init); · line 669

Quoted 10 lines
mm/cma.c +10 -0
+	struct attribute attr;
+	ssize_t (*show)(struct cma_region *reg, char *buf);
+	int (*store)(struct cma_region *reg, const char *buf);
+};
+
+
+static ssize_t cma_sysfs_region_name_show(struct cma_region *reg, char *page)
+{
+	return reg->name ? snprintf(page, PAGE_SIZE, "%s\n", reg->name) : 0;
+}

Is a name field ever really going to be bigger than a page?

mm/cma.c subsys_initcall(cma_init); · line 674

Quoted 5 lines
mm/cma.c +5 -0
+
+static ssize_t cma_sysfs_region_start_show(struct cma_region *reg, char *page)
+{
+	return snprintf(page, PAGE_SIZE, "%p\n", (void *)reg->start);
+}

Same for a start field.

mm/cma.c subsys_initcall(cma_init); · line 679

Quoted 5 lines
mm/cma.c +5 -0
+
+static ssize_t cma_sysfs_region_size_show(struct cma_region *reg, char *page)
+{
+	return snprintf(page, PAGE_SIZE, "%zu\n", reg->size);
+}

Same for the size

mm/cma.c subsys_initcall(cma_init); · line 684

Quoted 5 lines
mm/cma.c +5 -0
+
+static ssize_t cma_sysfs_region_free_show(struct cma_region *reg, char *page)
+{
+	return snprintf(page, PAGE_SIZE, "%zu\n", reg->free_space);
+}

Same here.

mm/cma.c subsys_initcall(cma_init); · line 689

Quoted 5 lines
mm/cma.c +5 -0
+
+static ssize_t cma_sysfs_region_users_show(struct cma_region *reg, char *page)
+{
+	return snprintf(page, PAGE_SIZE, "%u\n", reg->users);
+}

And here

mm/cma.c subsys_initcall(cma_init); · line 700

Quoted 11 lines
mm/cma.c +11 -0
+
+static ssize_t cma_sysfs_region_alloc_show(struct cma_region *reg, char *page)
+{
+	if (reg->alloc)
+		return snprintf(page, PAGE_SIZE, "%s\n",
+				reg->alloc->name ?: "-");
+	else if (reg->alloc_name)
+		return snprintf(page, PAGE_SIZE, "[%s]\n", reg->alloc_name);
+	else
+		return 0;
+}

Are all of these files in the Documentation/ABI directory? I missed that in your above description...

Quoted 74 lines
mm/cma.c +74 -0
+
+static int
+cma_sysfs_region_alloc_store(struct cma_region *reg, const char *page)
+{
+	char *s;
+
+	if (reg->alloc && reg->users)
+		return -EBUSY;
+
+	if (!*page || *page == '\n') {
+		s = NULL;
+	} else {
+		size_t len;
+
+		for (s = (char *)page; *++s && *s != '\n'; )
+			/* nop */;
+
+		len = s - page;
+		s = kmemdup(page, len + 1, GFP_KERNEL);
+		if (!s)
+			return -ENOMEM;
+		s[len] = '\0';
+	}
+
+	if (reg->alloc)
+		__cma_region_detach_alloc(reg);
+
+	if (reg->free_alloc_name)
+		kfree(reg->alloc_name);
+
+	reg->alloc_name = s;
+	reg->free_alloc_name = !!s;
+
+	return 0;
+}
+
+
+static ssize_t
+cma_sysfs_region_show(struct kobject *kobj, struct attribute *attr,
+		      char *buf)
+{
+	struct cma_region *reg = container_of(kobj, struct cma_region, kobj);
+	struct cma_region_attribute *rattr =
+		container_of(attr, struct cma_region_attribute, attr);
+	ssize_t ret;
+
+	mutex_lock(&cma_mutex);
+	ret = rattr->show(reg, buf);
+	mutex_unlock(&cma_mutex);
+
+	return ret;
+}
+
+static int
+cma_sysfs_region_store(struct kobject *kobj, struct attribute *attr,
+		       const char *buf, size_t count)
+{
+	struct cma_region *reg = container_of(kobj, struct cma_region, kobj);
+	struct cma_region_attribute *rattr =
+		container_of(attr, struct cma_region_attribute, attr);
+	int ret;
+
+	mutex_lock(&cma_mutex);
+	ret = rattr->store(reg, buf);
+	mutex_unlock(&cma_mutex);
+
+	return ret < 0 ? ret : count;
+}
+
+static struct kobj_type cma_sysfs_region_type = {
+	.sysfs_ops	= &(const struct sysfs_ops){
+		.show	= cma_sysfs_region_show,
+		.store	= cma_sysfs_region_store,
+	},

mm/cma.c subsys_initcall(cma_init); · line 784

Quoted 10 lines
mm/cma.c +10 -0
+	.default_attrs	= (struct attribute * []) {
+		CMA_ATTR_RO_INLINE(region, name),
+		CMA_ATTR_RO_INLINE(region, start),
+		CMA_ATTR_RO_INLINE(region, size),
+		CMA_ATTR_RO_INLINE(region, free),
+		CMA_ATTR_RO_INLINE(region, users),
+		CMA_ATTR_INLINE(region, alloc),
+		NULL
+	},
+};

Again, broken by design, please fix.

Quoted 7 lines
mm/cma.c +7 -0
+
+static void __cma_sysfs_region_add(struct cma_region *reg)
+{
+	int ret;
+
+	if (!cma_sysfs_regions_ready)
+		return;

mm/cma.c subsys_initcall(cma_init); · line 801

Quoted 10 lines
mm/cma.c +10 -0
+
+	memset(&reg->kobj, 0, sizeof reg->kobj);
+
+	ret = kobject_init_and_add(&reg->kobj, &cma_sysfs_region_type,
+				   &cma_sysfs_regions,
+				   "%p", (void *)reg->start);
+
+	if (reg->name &&
+	    sysfs_create_link(&cma_sysfs_regions, &reg->kobj, reg->name) < 0)
+		/* Ignore any errors. */;

That's handy, why do you think the kernel is FORCING you to check the return value of this function? So you can then just ignore it? It's as if we add this code for absolutely no reason, sometimes I wonder why I even bother...

In short, ick, ick, ick.

Oh, and go read the documentation, it's there for a reason.

greg k-h

#17 lore raw
In reply to Michal Nazarewicz
Quoted 4 lines · Michal Nazarewicz

Hello everyone,

This patchset introduces a draft of a redesign of Zach Pfeffer's VCMM.

What is a VCMM?

What is a CMA?

Quoted 3 lines

Not all of the functionality of the original VCMM has been ported into this patchset. This is mostly meant as RFC. Moreover, the code for VCMM implementation in this RFC has not been tested.

If you haven't even tested it, why should we review it?

thanks,

greg k-h

#14 lore raw

Re: [RFCv5 8/9] mm: vcm: Sample driver added

In reply to Michal Nazarewicz

include/linux/vcm-sample.h

Quoted 2 lines · Michal Nazarewicz
include/linux/vcm-sample.h new file +0 -0

Don't put "sample" code in include/linux/ please. That's just cluttering up the place, don't you think? Especially as no one else needs the file there...

thanks,

greg k-h

#18 lore raw
In reply to Greg KH
Quoted 7 lines · Greg KH
Quoted 4 lines · Michal Nazarewicz

Hello everyone,

This patchset introduces a draft of a redesign of Zach Pfeffer's VCMM.

What is a VCMM?

Virtual Contiguous Memory Manager. The version posted by Zach can be found at: <http://article.gmane.org/gmane.linux.kernel.mm/50090>.
It is an API for managing IO MMU and IO MMU mappings.

Quoted 1 line

What is a CMA?

Contiguous Memory Manager. The v4 version can be found at
<http://marc.info/?l=linux-mm&m=128229799415817&w=2>. It is an API for allocating large, physically contiguous blocks of memory.

I haven't expected that anyone who haven't already participated in the discussion about CMA and VCMM will get interested by this patchset so I was a bit vague in the cover letter. Sorry about that.

Quoted 5 lines
Quoted 3 lines

Not all of the functionality of the original VCMM has been ported into this patchset. This is mostly meant as RFC. Moreover, the code for VCMM implementation in this RFC has not been tested.

If you haven't even tested it, why should we review it?

Ignore the code then and look just at the documentation, please. I wanted to post what I have to receive comments about the general idea and not necessarily the code itself. Code is just a mean to show how I see the implementation of the idea described in the documentation. Because of all that, I marked the patchset as a RFC rather than a PATCH.

Signature · 4 lines
Best regards,                                        _     _
| Humble Liege of Serenely Enlightened Majesty of  o' \,=./ `o
| Computer Science,  Micha? "mina86" Nazarewicz       (o o)
+----[mina86*mina86.com]---[mina86*jabber.org]----ooO--(_)--Ooo--
#15 lore raw

Re: [RFCv5 8/9] mm: vcm: Sample driver added

In reply to Greg KH
Quoted 7 lines · Greg KH

include/linux/vcm-sample.h

Quoted 2 lines · Michal Nazarewicz
include/linux/vcm-sample.h new file +0 -0

Don't put "sample" code in include/linux/ please. That's just cluttering up the place, don't you think? Especially as no one else needs the file there...

Absolutely true. My plan is to put a real driver in place of the sample driver and post it with v6. For now I just wanted to put a piece of code that will look like a driver for presentation purposes. Sorry for the confusion.

Signature · 4 lines
Best regards,                                        _     _
| Humble Liege of Serenely Enlightened Majesty of  o' \,=./ `o
| Computer Science,  Micha? "mina86" Nazarewicz       (o o)
+----[mina86*mina86.com]---[mina86*jabber.org]----ooO--(_)--Ooo--
#19 lore raw
In reply to Michał Nazarewicz
Quoted 28 lines · Micha?? Nazarewicz
Quoted 7 lines · Greg KH
Quoted 4 lines · Michal Nazarewicz

Hello everyone,

This patchset introduces a draft of a redesign of Zach Pfeffer's VCMM.

What is a VCMM?

Virtual Contiguous Memory Manager. The version posted by Zach can be found at: <http://article.gmane.org/gmane.linux.kernel.mm/50090>.
It is an API for managing IO MMU and IO MMU mappings.

Quoted 1 line

What is a CMA?

Contiguous Memory Manager. The v4 version can be found at
<http://marc.info/?l=linux-mm&m=128229799415817&w=2>. It is an API for allocating large, physically contiguous blocks of memory.

I haven't expected that anyone who haven't already participated in the discussion about CMA and VCMM will get interested by this patchset so I was a bit vague in the cover letter. Sorry about that.

Quoted 3 lines
Quoted 3 lines

Not all of the functionality of the original VCMM has been ported into this patchset. This is mostly meant as RFC. Moreover, the code for VCMM implementation in this RFC has not been tested.

Quoted 7 lines
Quoted 1 line

If you haven't even tested it, why should we review it?

Ignore the code then and look just at the documentation, please. I wanted to post what I have to receive comments about the general idea and not necessarily the code itself. Code is just a mean to show how I see the implementation of the idea described in the documentation. Because of all that, I marked the patchset as a RFC rather than a PATCH.

Oops, I looked at the code, sorry :)

greg k-h

#5 lore raw

Re: [RFCv5 3/9] mm: cma: Added SysFS support

In reply to Greg KH

Hello Greg,

Thanks for reviewing the sysfs part. Actually, I was never really sure if I shouldn't rather put this code to debugfs and you got me convinced that I should. Sysfs somehow looked more appealing from kernel's API point of view -- things seem to be more organised in sysfs than in debugfs. It seems I'll have to port it to debugfs after all

Nonetheless, a few responses to your comments:

Quoted 4 lines · Michal Nazarewicz
Quoted 3 lines
Documentation/ABI/testing/sysfs-kernel-mm-contiguous new file +3 -0
+		The "allocators" file list all registered allocators.
+		Allocators with no name are listed as a single minus
+		sign.
Quoted 1 line · Greg KH

So this returns more than one value?

Aren't thing like cpufreq governors listed in a single sysfs file? I remember there was such a file somewhere. Has that been made deprecated? I cannot seem to find any information on that.

Quoted 3 lines

Documentation/ABI/testing/sysfs-kernel-mm-contiguous line 17

Quoted 1 line
Documentation/ABI/testing/sysfs-kernel-mm-contiguous new file +1 -0
+		The "regions" directory list all reserved regions.

Same here?

regions is actually a directory with subdirectories for each region. ;)

Quoted 6 lines

mm/cma.c subsys_initcall(cma_init); · line 669

Quoted 4 lines
mm/cma.c +4 -0
+static ssize_t cma_sysfs_region_name_show(struct cma_region *reg, char *page)
+{
+	return reg->name ? snprintf(page, PAGE_SIZE, "%s\n", reg->name) : 0;
+}

Is a name field ever really going to be bigger than a page?

I prefer being on the safe side -- I have no idea what user will provide as region name so I assume as little as possible. For numeric values you are right that snprintf() is a bit paranoid, still I see no good reason why not to use it.

Signature · 4 lines
Best regards,                                        _     _
| Humble Liege of Serenely Enlightened Majesty of  o' \,=./ `o
| Computer Science,  Micha? "mina86" Nazarewicz       (o o)
+----[mina86*mina86.com]---[mina86*jabber.org]----ooO--(_)--Ooo--
#6 lore raw

Re: [RFCv5 3/9] mm: cma: Added SysFS support

Quoted 7 lines · Micha?? Nazarewicz

Hello Greg,

Thanks for reviewing the sysfs part. Actually, I was never really sure if I shouldn't rather put this code to debugfs and you got me convinced that I should. Sysfs somehow looked more appealing from kernel's API point of view -- things seem to be more organised in sysfs than in debugfs. It seems I'll have to port it to debugfs after all

Yes, debugfs looks like a much better place for this. You can do whatever you want in debugfs as long as you follow the one rule for it:
There are no rules for debugfs.

Quoted 11 lines

Nonetheless, a few responses to your comments:

Quoted 4 lines · Michal Nazarewicz
Quoted 3 lines
Documentation/ABI/testing/sysfs-kernel-mm-contiguous new file +3 -0
+		The "allocators" file list all registered allocators.
+		Allocators with no name are listed as a single minus
+		sign.
Quoted 1 line · Greg KH

So this returns more than one value?

Aren't thing like cpufreq governors listed in a single sysfs file?

Yeah, but I don't like it :)

Quoted 2 lines

I remember there was such a file somewhere. Has that been made deprecated? I cannot seem to find any information on that.

It's best if you really don't do this, but it does happen as it is the best way to show the information. If that's the case, fine.

Quoted 6 lines
Quoted 3 lines

Documentation/ABI/testing/sysfs-kernel-mm-contiguous line 17

Quoted 1 line
Documentation/ABI/testing/sysfs-kernel-mm-contiguous new file +1 -0
+		The "regions" directory list all reserved regions.

Same here?

regions is actually a directory with subdirectories for each region. ;)

Ah.

Quoted 9 lines
Quoted 6 lines

mm/cma.c subsys_initcall(cma_init); · line 669

Quoted 4 lines
mm/cma.c +4 -0
+static ssize_t cma_sysfs_region_name_show(struct cma_region *reg, char *page)
+{
+	return reg->name ? snprintf(page, PAGE_SIZE, "%s\n", reg->name) : 0;
+}

Is a name field ever really going to be bigger than a page?

I prefer being on the safe side -- I have no idea what user will provide as region name so I assume as little as possible.

By "user" do you mean userspace, or another kernel driver file? If it's a kernel driver, you can assume that they will be sane. if userspace, assume it's insane and do some checking of the name before you use it.

Quoted 2 lines

For numeric values you are right that snprintf() is a bit paranoid, still I see no good reason why not to use it.

Same goes for no good reason to use it :)

thanks,

greg k-h

#7 lore raw

Re: [RFCv5 3/9] mm: cma: Added SysFS support

Quoted 6 lines · Micha?? Nazarewicz
Quoted 5 lines

Thanks for reviewing the sysfs part. Actually, I was never really sure if I shouldn't rather put this code to debugfs and you got me convinced that I should. Sysfs somehow looked more appealing from kernel's API point of view -- things seem to be more organised in sysfs than in debugfs. It seems I'll have to port it to debugfs after all

Quoted 1 line · Greg KH

Yes, debugfs looks like a much better place for this.

I'll fix that in v6 then.

Quoted 11 lines
Quoted 9 lines
Quoted 6 lines

mm/cma.c subsys_initcall(cma_init); · line 669

Quoted 4 lines
mm/cma.c +4 -0
+static ssize_t cma_sysfs_region_name_show(struct cma_region *reg, char *page)
+{
+	return reg->name ? snprintf(page, PAGE_SIZE, "%s\n", reg->name) : 0;
+}

Is a name field ever really going to be bigger than a page?

For numeric values you are right that snprintf() is a bit paranoid, still I see no good reason why not to use it.

Same goes for no good reason to use it :)

I somehow prefer to always use "safe" versions of the string manipulation functions -- it's better to use it everywhere then to forget it in one place. Call to sprintf() is translated to vsnprintf() anyway so there's no performance gain.

Signature · 4 lines
Best regards,                                        _     _
| Humble Liege of Serenely Enlightened Majesty of  o' \,=./ `o
| Computer Science,  Micha? "mina86" Nazarewicz       (o o)
+----[mina86*mina86.com]---[mina86*jabber.org]----ooO--(_)--Ooo--
#12 lore raw

Re: [RFCv5 7/9] mm: vcm: Virtual Contiguous Memory framework added

In reply to Michal Nazarewicz

Documentation/virtual-contiguous-memory.txt line 19

Quoted 13 lines
Documentation/virtual-contiguous-memory.txt new file +13 -0
+* The Virtual Contiguous Memory Manager
+
+The VCMM was built to solve the system-wide memory mapping issues that
+occur when many bus-masters have IOMMUs.
+
+An IOMMU maps device addresses to physical addresses.  It also
+insulates the system from spurious or malicious device bus
+transactions and allows fine-grained mapping attribute control.  The
+Linux kernel core does not contain a generic API to handle IOMMU
+mapped memory; device driver writers must implement device specific
+code to interoperate with the Linux kernel core.  As the number of
+IOMMUs increases, coordinating the many address spaces mapped by all
+discrete IOMMUs becomes difficult without in-kernel support.

Looking at the set of calls and the examples it struck me as similar to the agp.h API (drivers/char/agp/). It has allocate, bind, de-allocate. Naturally it has no bus device mapping, but the DRM code that utilizes the AGP API bridge has that: drivers/gpu/drm/ati_pcigart.c (DRM API).

Then there are the radeon and nouveau drivers that program the GPU GART bypassing the AGP API but still utilize the DMA API.

The nice ASCII art you included in your writeup looks to cover those use cases.

What I am ineptly trying to say is that is that we have a bunch of APIs that do this, and in case where they are inadequate (or just look to be a one-off solution) we have functions that are similar in API view but differ in implementation (check out how the Nouveau programs its VMM compared to how the Radeon does it).

Your API offers a way to unify all of that, but it looks to be an API on top of the other ones. You would still have to implement different mechanisms for the utilizing this say on the radeon driver:
AGP API, or the home-grown GPU GART programming, and then the DMA API wrapped around them all. Oh, and the DMA API sits on top of the IOMMU API.

I am not sure how this would solve the proliferation of different APIs
- it sounds like it just adds another piece where you still have to shoe-in the other APIs in.

But I do understand the problem you are facing. You want to switch to different IOMMUs for different drivers using only one API. Folks have been asking about whether it makes sense to include your algorithms in expanding the memory allocator to have huge chunks of memory reserved for specific drivers. But that does not solve the IOMMU problem.

So my question is, would it perhaps make sense to concentrate on the DMA API? Could we expand it so that for specific devices it sets the DMA API to use a different IOMMU? If you look at the Calgary IOMMU - that is a perfect example of your problem - it is only used if the specific devices which fall within its control - all other DMA operations are utilized by the SWIOTLB (the default IOMMU).

Would it possible to do something similar to that so when CMA is activated it scans the region list, finds which devices can share the same memory region and sets the struct device DMA API to point to a CMA IOMMU which is happy to utilize the memory allocator reserved chunks of memory (that would be the code lifted from your CMA and stuck in the memory allocate) and allowing different drivers (those on the whitelist) to share the same region?

This has the extra benefit that it would inclusive allow all drivers that utilize the DMA API to use this without any extra VCMA/CMA API calls?

P.S.
I am quite X86 specific here - I don't know much about ARM (is there some desktop box I can buy with it?), so I am probably missing some details.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>