Contiguous Memory Allocator
Subsystems (14)
v8 cover Contiguous Memory Allocator
Hello everyone,
This is yet another version of CMA this time stripped from a lot of code and with working migration implementation.
The Contiguous Memory Allocator (CMA) makes it possible for device drivers to allocate big contiguous chunks of memory after the system has booted.
For more information see 7th patch in the set.
This version fixes some things Kamezawa suggested plus it separates code that uses MIGRATE_CMA from the rest of the code. This I hope will help to grasp the overall idea of CMA.
The current version is just an allocator that handles allocation of contiguous memory blocks. The difference between this patchset and Kamezawa's alloc_contig_pages() are:
1. alloc_contig_pages() requires MAX_ORDER alignment of allocations which may be unsuitable for embeded systems where a few MiBs are required.
Lack of the requirement on the alignment means that several threads might try to access the same pageblock/page. To prevent this from happening CMA uses a mutex so that only one cm_alloc()/cm_free() function may run at one point.
2. CMA may use its own migratetype (MIGRATE_CMA) which behaves similarly to ZONE_MOVABLE but can be put in arbitrary places.
This is required for us since we need to define two disjoint memory ranges inside system RAM. (ie. in two memory banks (do not confuse with nodes)).
3. alloc_contig_pages() scans memory in search for range that could be migrated. CMA on the other hand maintains its own allocator to decide where to allocate memory for device drivers and then tries to migrate pages from that part if needed. This is not strictly required but I somehow feel it might be faster.
Links to previous versions of the patchset:
v7: <http://article.gmane.org/gmane.linux.kernel.mm/55626>
v6: <http://article.gmane.org/gmane.linux.kernel.mm/55626>
v5: (intentionally left out as CMA v5 was identical to CMA v4)
v4: <http://article.gmane.org/gmane.linux.kernel.mm/52010>
v3: <http://article.gmane.org/gmane.linux.kernel.mm/51573>
v2: <http://article.gmane.org/gmane.linux.kernel.mm/50986>
v1: <http://article.gmane.org/gmane.linux.kernel.mm/50669>
Changelog:
v8: 1. The alloc_contig_range() function has now been separated from
CMA and put in page_allocator.c. This function tries to migrate all LRU pages in specified range and then allocate the range using alloc_contig_freed_pages().
2. Support for MIGRATE_CMA has been separated from the CMA code. I have not tested if CMA works with ZONE_MOVABLE but I see no reasons why it shouldn't.
3. I have added a @private argument when creating CMA contexts so that one can reserve memory and not share it with the rest of the system. This way, CMA acts only as allocation algorithm.
v7: 1. A lot of functionality that handled driver->allocator_context
mapping has been removed from the patchset. This is not to say that this code is not needed, it's just not worth posting everything in one patchset.
Currently, CMA is "just" an allocator. It uses it's own migratetype (MIGRATE_CMA) for defining ranges of pageblokcs which behave just like ZONE_MOVABLE but dispite the latter can be put in arbitrary places.
2. The migration code that was introduced in the previous version actually started working.
v6: 1. Most importantly, v6 introduces support for memory migration.
The implementation is not yet complete though.
Migration support means that when CMA is not using memory reserved for it, page allocator can allocate pages from it. When CMA wants to use the memory, the pages have to be moved and/or evicted as to make room for CMA.
To make it possible it must be guaranteed that only movable and reclaimable pages are allocated in CMA controlled regions. This is done by introducing a MIGRATE_CMA migrate type that guarantees exactly that.
Some of the migration code is "borrowed" from Kamezawa Hiroyuki's alloc_contig_pages() implementation. The main difference is that thanks to MIGRATE_CMA migrate type CMA assumes that memory controlled by CMA are is always movable or reclaimable so that it makes allocation decisions regardless of the whether some pages are actually allocated and migrates them if needed.
The most interesting patches from the patchset that implement the functionality are:
09/13: mm: alloc_contig_free_pages() added 10/13: mm: MIGRATE_CMA migration type added 11/13: mm: MIGRATE_CMA isolation functions added 12/13: mm: cma: Migration support added [wip]
Currently, kernel panics in some situations which I am trying to investigate.
2. cma_pin() and cma_unpin() functions has been added (after a conversation with Johan Mossberg). The idea is that whenever hardware does not use the memory (no transaction is on) the chunk can be moved around. This would allow defragmentation to be implemented if desired. No defragmentation algorithm is provided at this time.
3. Sysfs support has been replaced with debugfs. I always felt unsure about the sysfs interface and when Greg KH pointed it out I finally got to rewrite it to debugfs.
v5: (intentionally left out as CMA v5 was identical to CMA v4)
v4: 1. The "asterisk" flag has been removed in favour of requiring
that platform will provide a "*=<regions>" rule in the map attribute.
2. The terminology has been changed slightly renaming "kind" to "type" of memory. In the previous revisions, the documentation indicated that device drivers define memory kinds and now,
v3: 1. The command line parameters have been removed (and moved to
a separate patch, the fourth one). As a consequence, the cma_set_defaults() function has been changed -- it no longer accepts a string with list of regions but an array of regions.
2. The "asterisk" attribute has been removed. Now, each region has an "asterisk" flag which lets one specify whether this region should by considered "asterisk" region.
3. SysFS support has been moved to a separate patch (the third one in the series) and now also includes list of regions.
v2: 1. The "cma_map" command line have been removed. In exchange,
a SysFS entry has been created under kernel/mm/contiguous.
The intended way of specifying the attributes is
a cma_set_defaults() function called by platform initialisation code. "regions" attribute (the string specified by "cma" command line parameter) can be overwritten with command line parameter; the other attributes can be changed during run-time using the SysFS entries.
2. The behaviour of the "map" attribute has been modified slightly. Currently, if no rule matches given device it is assigned regions specified by the "asterisk" attribute. It is by default built from the region names given in "regions" attribute.
3. Devices can register private regions as well as regions that can be shared but are not reserved using standard CMA mechanisms. A private region has no name and can be accessed only by devices that have the pointer to it.
4. The way allocators are registered has changed. Currently, a cma_allocator_register() function is used for that purpose. Moreover, allocators are attached to regions the first time memory is registered from the region or when allocator is registered which means that allocators can be dynamic modules that are loaded after the kernel booted (of course, it won't be possible to allocate a chunk of memory from a region if allocator is not loaded).
5. Index of new functions:
+static inline dma_addr_t __must_check +cma_alloc_from(const char *regions, size_t size,
+ dma_addr_t alignment)
+static inline int
+cma_info_about(struct cma_info *info, const const char *regions)
+int __must_check cma_region_register(struct cma_region *reg);
+dma_addr_t __must_check +cma_alloc_from_region(struct cma_region *reg,
+ size_t size, dma_addr_t alignment);
+static inline dma_addr_t __must_check
+cma_alloc_from(const char *regions,
+ size_t size, dma_addr_t alignment);
+int cma_allocator_register(struct cma_allocator *alloc);
Patches in this patchset:
mm: migrate.c: fix compilation error
I had some strange compilation error; this patch fixed them.
lib: bitmap: Added alignment offset for bitmap_find_next_zero_area()
lib: genalloc: Generic allocator improvements
Some improvements to genalloc API (most importantly possibility to allocate memory with alignment requirement).
mm: move some functions from memory_hotplug.c to page_isolation.c
mm: alloc_contig_freed_pages() added
Code "stolen" from Kamezawa. The first patch just moves code around and the second provide function for "allocates" already freed memory.
mm: alloc_contig_range() added
This is what Kamezawa asked: a function that tries to migrate all pages from given range and then use alloc_contig_freed_pages() (defined by the previous commit) to allocate those pages.
mm: cma: Contiguous Memory Allocator added
The CMA code but with no MIGRATE_CMA support yet. This assues that one uses a ZONE_MOVABLE. I must admit I have not test that setup yet but I don't see any reasons why it should not work.
mm: MIGRATE_CMA migration type added
mm: MIGRATE_CMA isolation functions added
mm: MIGRATE_CMA support added to CMA
Introduction of the new migratetype and support for it in CMA. MIGRATE_CMA works similar to ZONE_MOVABLE expect almost any memory range can be marked as one.
mm: cma: Test device and application added
Test device and application. Not really for merging; just for testing really.
ARM: cma: Added CMA to Aquila, Goni and c210 universal boards
A stub integration with some ARM machines. Mostly to get the cma testing device working. Again, not for merging, just an example.
arch/arm/mach-s5pv210/Kconfig | 2 + arch/arm/mach-s5pv210/mach-aquila.c | 2 + arch/arm/mach-s5pv210/mach-goni.c | 2 + arch/arm/mach-s5pv310/Kconfig | 1 + arch/arm/mach-s5pv310/mach-universal_c210.c | 2 + arch/arm/plat-s5p/Makefile | 2 + arch/arm/plat-s5p/cma-stub.c | 49 +++ arch/arm/plat-s5p/include/plat/cma-stub.h | 21 ++ drivers/misc/Kconfig | 28 ++ drivers/misc/Makefile | 1 + drivers/misc/cma-dev.c | 238 ++++++++++++++ include/linux/bitmap.h | 24 ++- include/linux/cma.h | 290 +++++++++++++++++ include/linux/genalloc.h | 46 ++-- include/linux/mmzone.h | 43 ++- include/linux/page-isolation.h | 50 +++- lib/bitmap.c | 22 +- lib/genalloc.c | 182 ++++++----- mm/Kconfig | 36 ++ mm/Makefile | 1 + mm/cma.c | 455 ++++++++++++++++++++++++++ mm/compaction.c | 10 + mm/internal.h | 3 + mm/memory_hotplug.c | 108 ------- mm/migrate.c | 2 + mm/page_alloc.c | 286 +++++++++++++++-- mm/page_isolation.c | 126 +++++++- tools/cma/cma-test.c | 457 +++++++++++++++++++++++++++ 28 files changed, 2219 insertions(+), 270 deletions(-) create mode 100644 arch/arm/plat-s5p/cma-stub.c create mode 100644 arch/arm/plat-s5p/include/plat/cma-stub.h create mode 100644 drivers/misc/cma-dev.c create mode 100644 include/linux/cma.h create mode 100644 mm/cma.c create mode 100644 tools/cma/cma-test.c
Signature · 1 line
1.7.2.3
PATCH v8 1/12 mm: migrate.c: fix compilation error
GCC complained about update_mmu_cache() not being defined in migrate.c. Including <asm/tlbflush.h> seems to solve the problem.
--- mm/migrate.c | 2 ++ 1 files changed, 2 insertions(+), 0 deletions(-)
mm/migrate.c +2 -0
@@ -35,6 +35,8 @@#include <linux/hugetlb.h> #include <linux/gfp.h> +#include <asm/tlbflush.h> +#include "internal.h" #define lru_to_page (_head) (list_entry((_head)->prev, struct page, lru))
Signature · 1 line
1.7.2.3
PATCH v8 2/12 lib: bitmap: Added alignment offset for bitmap_find_next_zero_area()
In reply to Michal NazarewiczFrom: Michal Nazarewicz <[email protected]>
This commit adds a bitmap_find_next_zero_area_off() function which works like bitmap_find_next_zero_area() function expect it allows an offset to be specified when alignment is checked. This lets caller request a bit such that its number plus the offset is aligned according to the mask.
--- include/linux/bitmap.h | 24 +++++++++++++++++++----- lib/bitmap.c | 22 ++++++++++++---------- 2 files changed, 31 insertions(+), 15 deletions(-)
include/linux/bitmap.h +19 -5
@@ -45,6 +45,7 @@* bitmap_set(dst, pos, nbits) Set specified bit area * bitmap_clear(dst, pos, nbits) Clear specified bit area * bitmap_find_next_zero_area(buf, len, pos, n, mask) Find bit free area +* bitmap_find_next_zero_area_off(buf, len, pos, n, mask) as above * bitmap_shift_right(dst, src, n, nbits) *dst = *src >> n * bitmap_shift_left(dst, src, n, nbits) *dst = *src << n * bitmap_remap(dst, src, old, new, nbits) *dst = map(old, new)(src)
@@ -113,11 +114,24 @@ extern int __bitmap_weight(const unsigned long *bitmap, int bits);extern void bitmap_set (unsigned long *map,int i,int len);extern void bitmap_clear (unsigned long *map,int start,int nr); -extern unsigned long bitmap_find_next_zero_area (unsigned long *map, -unsigned long size, -unsigned long start, -unsigned int nr, -unsigned long align_mask); + +extern unsigned long bitmap_find_next_zero_area_off (unsigned long *map, +unsigned long size, +unsigned long start, +unsigned int nr, +unsigned long align_mask, +unsigned long align_offset); + +static inline unsigned long +bitmap_find_next_zero_area (unsigned long *map, +unsigned long size, +unsigned long start, +unsigned int nr, +unsigned long align_mask) +{ +return bitmap_find_next_zero_area_off (map, size, start, nr, + align_mask,0 ); +}extern int bitmap_scnprintf (char *buf,unsigned int len,const unsigned long *src,int nbits);
lib/bitmap.c +12 -10
@@ -315,30 +315,32 @@ void bitmap_clear(unsigned long *map, int start, int nr) }EXPORT_SYMBOL (bitmap_clear); -/* +/** * bitmap_find_next_zero_area - find a contiguous aligned zero area * @map: The address to base the search on * @size: The bitmap size in bits * @start: The bitnumber to start searching at * @nr: The number of zeroed bits we're looking for * @align_mask: Alignment mask for zero area +* @align_offset: Alignment offset for zero area. * * The @align_mask should be one less than a power of 2; the effect is that -* the bit offset of all zero areas this function finds is multiples of that -* power of 2. A @align_mask of 0 means no alignment is required. +* the bit offset of all zero areas this function finds plus @align_offset +* is multiple of that power of 2. */ -unsigned long bitmap_find_next_zero_area (unsigned long *map, -unsigned long size, -unsigned long start, -unsigned int nr, -unsigned long align_mask) +unsigned long bitmap_find_next_zero_area_off (unsigned long *map, +unsigned long size, +unsigned long start, +unsigned int nr, +unsigned long align_mask, +unsigned long align_offset) {unsigned long index, end, i; again: index =find_next_zero_bit (map, size, start);/* Align allocation */ - index =__ALIGN_MASK (index, align_mask); + index =__ALIGN_MASK (index + align_offset, align_mask) - align_offset; end = index + nr;if (end > size)
@@ -350,7 +352,7 @@ again: }return index; } -EXPORT_SYMBOL (bitmap_find_next_zero_area); +EXPORT_SYMBOL (bitmap_find_next_zero_area_off);/* * Bitmap printing & parsing functions: first version by Bill Irwin,
Signature · 1 line
1.7.2.3
PATCH v8 3/12 lib: genalloc: Generic allocator improvements
In reply to Michal NazarewiczThis commit adds a gen_pool_alloc_aligned() function to the generic allocator API. It allows specifying alignment for the allocated block. This feature uses
the bitmap_find_next_zero_area_off() function.
It also fixes possible issue with bitmap's last element being not fully allocated (ie. space allocated for chunk->bits is not a multiple of sizeof(long)).
It also makes some other smaller changes:
- moves structure definitions out of the header file,
- adds __must_check to functions returning value,
- makes gen_pool_add() return -ENOMEM rater than -1 on error,
- changes list_for_each to list_for_each_entry, and
- makes use of bitmap_clear().
--- include/linux/genalloc.h | 46 ++++++------ lib/genalloc.c | 182 ++++++++++++++++++++++++++------------------- 2 files changed, 129 insertions(+), 99 deletions(-)
include/linux/genalloc.h +24 -22
@@ -8,29 +8,31 @@* Version 2. See the file COPYING for more details. */ +struct gen_pool ; -/* -* General purpose special memory pool descriptor. -*/ -struct gen_pool { -rwlock_t lock; -struct list_head chunks;/* list of chunks in this pool */ -int min_alloc_order;/* minimum allocation order */ -}; +struct gen_pool *__must_check gen_pool_create (unsigned order,int nid); -/* -* General purpose special memory pool chunk descriptor. +int __must_check gen_pool_add (struct gen_pool *pool,unsigned long addr, +size_t size,int nid); + +void gen_pool_destroy (struct gen_pool *pool); + +unsigned long __must_check +gen_pool_alloc_aligned (struct gen_pool *pool,size_t size, +unsigned alignment_order); + +/** +* gen_pool_alloc() - allocate special memory from the pool +* @pool: Pool to allocate from. +* @size: Number of bytes to allocate from the pool. +* +* Allocate the requested number of bytes from the specified pool. +* Uses a first-fit algorithm. */ -struct gen_pool_chunk { -spinlock_t lock; -struct list_head next_chunk;/* next chunk in pool */ -unsigned long start_addr;/* starting address of memory chunk */ -unsigned long end_addr;/* ending address of memory chunk */ -unsigned long bits[0 ];/* bitmap for allocating memory chunk */ -}; +static inline unsigned long __must_check +gen_pool_alloc (struct gen_pool *pool,size_t size) +{ +return gen_pool_alloc_aligned (pool, size,0 ); +} -extern struct gen_pool *gen_pool_create (int ,int ); -extern int gen_pool_add (struct gen_pool *,unsigned long ,size_t ,int ); -extern void gen_pool_destroy (struct gen_pool *); -extern unsigned long gen_pool_alloc (struct gen_pool *,size_t ); -extern void gen_pool_free (struct gen_pool *,unsigned long ,size_t ); +void gen_pool_free (struct gen_pool *pool,unsigned long addr,size_t size);
lib/genalloc.c +105 -77
273 lines not on this page · raw message
Signature · 1 line
1.7.2.3
PATCH v8 4/12 mm: move some functions from memory_hotplug.c to page_isolation.c
In reply to Michal NazarewiczFrom: KAMEZAWA Hiroyuki <[email protected]>
Memory hotplug is a logic for making pages unused in the specified range of pfn. So, some of core logics can be used for other purpose as allocating a very large contigous memory block.
This patch moves some functions from mm/memory_hotplug.c to mm/page_isolation.c. This helps adding a function for large-alloc in page_isolation.c with memory-unplug technique.
--- include/linux/page-isolation.h | 7 +++ mm/memory_hotplug.c | 108 -------------------------------------- mm/page_isolation.c | 111 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 118 insertions(+), 108 deletions(-)
include/linux/page-isolation.h +7 -0
@@ -33,5 +33,12 @@ test_pages_isolated(unsigned long start_pfn, unsigned long end_pfn);extern int set_migratetype_isolate (struct page *page);extern void unset_migratetype_isolate (struct page *page); +/* +* For migration. +*/ + +int test_pages_in_a_zone (unsigned long start_pfn,unsigned long end_pfn); +unsigned long scan_lru_pages (unsigned long start,unsigned long end); +int do_migrate_range (unsigned long start_pfn,unsigned long end_pfn);#endif
mm/memory_hotplug.c +0 -108
114 lines not on this page · raw message
mm/page_isolation.c +111 -0
120 lines not on this page · raw message
Signature · 1 line
1.7.2.3
PATCH v8 5/12 mm: alloc_contig_freed_pages() added
In reply to Michal NazarewiczFrom: KAMEZAWA Hiroyuki <[email protected]>
This commit introduces alloc_contig_freed_pages() function which allocates (ie. removes from buddy system) free pages in range. Caller has to guarantee that all pages in range are in buddy system.
Along with this function, a free_contig_pages() function is provided which frees all (or a subset of) pages allocated with alloc_contig_free_pages().
Michal Nazarewicz has modified the function to make it easier to allocate not MAX_ORDER_NR_PAGES aligned pages by making it return pfn of one-past-the-last allocated page.
--- include/linux/page-isolation.h | 3 ++ mm/page_alloc.c | 44 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 0 deletions(-)
include/linux/page-isolation.h +3 -0
@@ -32,6 +32,9 @@ test_pages_isolated(unsigned long start_pfn, unsigned long end_pfn);*/ extern int set_migratetype_isolate (struct page *page);extern void unset_migratetype_isolate (struct page *page); +extern unsigned long alloc_contig_freed_pages (unsigned long start, +unsigned long end,gfp_t flag); +extern void free_contig_pages (struct page *page,int nr_pages);/* * For migration.
mm/page_alloc.c +44 -0
50 lines not on this page · raw message
Signature · 1 line
1.7.2.3
PATCH v8 6/12 mm: alloc_contig_range() added
In reply to Michal NazarewiczThis commit adds the alloc_contig_range() function which tries to allecate given range of pages. It tries to migrate all already allocated pages that fall in the range thus freeing them. Once all pages in the range are freed they are removed from the buddy system thus allocated for the caller to use.
--- include/linux/page-isolation.h | 2 + mm/page_alloc.c | 144 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 146 insertions(+), 0 deletions(-)
include/linux/page-isolation.h +2 -0
@@ -34,6 +34,8 @@ extern int set_migratetype_isolate(struct page *page);extern void unset_migratetype_isolate (struct page *page);extern unsigned long alloc_contig_freed_pages (unsigned long start,unsigned long end,gfp_t flag); +extern int alloc_contig_range (unsigned long start,unsigned long end, +gfp_t flags);extern void free_contig_pages (struct page *page,int nr_pages);/*
mm/page_alloc.c +144 -0
150 lines not on this page · raw message
Signature · 1 line
1.7.2.3
PATCH v8 7/12 mm: cma: Contiguous Memory Allocator added
In reply to Michal NazarewiczThe Contiguous Memory Allocator is a set of functions that lets one initialise a region of memory which then can be used to perform allocations of contiguous memory chunks from.
CMA allows for creation of private and non-private contexts. The former is reserved for CMA and no other kernel subsystem can use it. The latter allows for movable pages to be allocated within CMA's managed memory so that it can be used for page cache when CMA devices do not use it.
--- include/linux/cma.h | 219 ++++++++++++++++++++++++++++++++++ mm/Kconfig | 22 ++++ mm/Makefile | 1 + mm/cma.c | 328 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 570 insertions(+), 0 deletions(-) create mode 100644 include/linux/cma.h create mode 100644 mm/cma.c
include/linux/cma.h new file +219 -0
@@ -0,0 +1,219 @@ +#ifndef __LINUX_CMA_H +#define __LINUX_CMA_H + +/* +* Contiguous Memory Allocator +* Copyright (c) 2010 by Samsung Electronics. +* Written by Michal Nazarewicz (m.nazarewicz at samsung.com) +*/ + +/* +* Contiguous Memory Allocator +* +* The Contiguous Memory Allocator (CMA) makes it possible for +* device drivers to allocate big contiguous chunks of memory after +* the system has booted. +* +* It requires some machine- and/or platform-specific initialisation +* code which prepares memory ranges to be used with CMA and later, +* device drivers can allocate memory from those ranges. +* +* Why is it needed? +* +* Various devices on embedded systems have no scatter-getter and/or +* IO map support and require contiguous blocks of memory to +* operate. They include devices such as cameras, hardware video +* coders, etc. +* +* Such devices often require big memory buffers (a full HD frame +* is, for instance, more then 2 mega pixels large, i.e. more than 6 +* MB of memory), which makes mechanisms such as kmalloc() or +* alloc_page() ineffective. +* +* At the same time, a solution where a big memory region is +* reserved for a device is suboptimal since often more memory is +* reserved then strictly required and, moreover, the memory is +* inaccessible to page system even if device drivers don't use it. +* +* CMA tries to solve this issue by operating on memory regions +* where only movable pages can be allocated from. This way, kernel +* can use the memory for pagecache and when device driver requests +* it, allocated pages can be migrated. +* +* Driver usage +* +* For device driver to use CMA it needs to have a pointer to a CMA +* context represented by a struct cma (which is an opaque data +* type). +* +* Once such pointer is obtained, device driver may allocate +* contiguous memory chunk using the following function: +* +* cm_alloc() +* +* This function returns a pointer to struct cm (another opaque data +* type) which represent a contiguous memory chunk. This pointer +* may be used with the following functions: +* +* cm_free() -- frees allocated contiguous memory +* cm_pin() -- pins memory +* cm_unpin() -- unpins memory +* cm_vmap() -- maps memory in kernel space +* cm_vunmap() -- unmaps memory from kernel space +* +* See the respective functions for more information. +* +* Platform/machine integration +* +* For device drivers to be able to use CMA platform or machine +* initialisation code must create a CMA context and pass it to +* device drivers. The latter may be done by a global variable or +* a platform/machine specific function. For the former CMA +* provides the following functions: +* +* cma_reserve() +* cma_create() +* +* The cma_reserve() function must be called when memblock is still +* operational and reserving memory with it is still possible. On +* ARM platform the "reserve" machine callback is a perfect place to +* call it. +* +* The last function creates a CMA context on a range of previously +* initialised memory addresses. Because it uses kmalloc() it needs +* to be called after SLAB is initialised. +*/ + +/***************************** Kernel level API *****************************/ + +#if defined __KERNEL__ && definedCONFIG_CMA + +/* CMA context */ +struct cma ; +/* Contiguous Memory chunk */ +struct cm ; + +/** +* cma_reserve() - reserves memory. +* @start: start address of the memory range in bytes hint; if unsure +* pass zero. +* @size: size of the memory to reserve in bytes. +* @alignment: desired alignment in bytes (must be power of two or zero). +* +* It will use memblock to allocate memory. @start and @size will be +* aligned to PAGE_SIZE. +* +* Returns reserved's area physical address or value that yields true +* when checked with IS_ERR_VALUE(). +*/ +unsigned long cma_reserve (unsigned long start,unsigned long size, +unsigned long alignment); + +/** +* cma_create() - creates a CMA context. +* @start: start address of the context in bytes. +* @size: size of the context in bytes. +* @min_alignment: minimal desired alignment or zero. +* @private: whether to create private context. +* +* The range must be page aligned. Different contexts cannot overlap. +* +* Unless @private is true the memory range must lay in ZONE_MOVABLE. +* If @private is true no underlaying memory checking is done and +* during allocation no pages migration will be performed - it is +* assumed that the memory is reserved and only CMA manages it. +* +* @start and @size must be page and @min_alignment alignment. +* @min_alignment specifies the minimal alignment that user will be +* able to request through cm_alloc() function. In most cases one +* will probably pass zero as @min_alignment but if the CMA context +* will be used only for, say, 1 MiB blocks passing 1 << 20 as +* @min_alignment may increase performance and reduce memory usage +* slightly. +* +* Because this function uses kmalloc() it must be called after SLAB +* is initialised. This in particular means that it cannot be called +* just after cma_reserve() since the former needs to be run way +* earlier. +* +* Returns pointer to CMA context or a pointer-error on error. +*/ +struct cma *cma_create (unsigned long start,unsigned long size, +unsigned long min_alignment,_Bool private); + +/** +* cma_destroy() - destroys CMA context. +* @cma: context to destroy. +*/ +void cma_destroy (struct cma *cma); + +/** +* cm_alloc() - allocates contiguous memory. +* @cma: CMA context to use. +* @size: desired chunk size in bytes (must be non-zero). +* @alignent: desired minimal alignment in bytes (must be power of two +* or zero). +* +* Returns pointer to structure representing contiguous memory or +* a pointer-error on error. +*/ +struct cm *cm_alloc (struct cma *cma,unsigned long size, +unsigned long alignment); + +/** +* cm_free() - frees contiguous memory. +* @cm: contiguous memory to free. +* +* The contiguous memory must be not be pinned (see cma_pin()) and +* must not be mapped to kernel space (cma_vmap()). +*/ +void cm_free (struct cm *cm); + +/** +* cm_pin() - pins contiguous memory. +* @cm: contiguous memory to pin. +* +* Pinning is required to obtain contiguous memory's physical address. +* While memory is pinned the memory will remain valid it may change +* if memory is unpinned and then pinned again. This facility is +* provided so that memory defragmentation can be implemented inside +* CMA. +* +* Each call to cm_pin() must be accompanied by call to cm_unpin() and +* the calls may be nested. +* +* Returns chunk's physical address or a value that yields true when +* tested with IS_ERR_VALUE(). +*/ +unsigned long cm_pin (struct cm *cm); + +/** +* cm_unpin() - unpins contiguous memory. +* @cm: contiguous memory to unpin. +* +* See cm_pin(). +*/ +void cm_unpin (struct cm *cm); + +/** +* cm_vmap() - maps memory to kernel space (or returns existing mapping). +* @cm: contiguous memory to map. +* +* Each call to cm_vmap() must be accompanied with call to cm_vunmap() +* and the calls may be nested. +* +* Returns kernel virtual address or a pointer-error. +*/ +void *cm_vmap (struct cm *cm); + +/** +* cm_vunmap() - unmpas memory from kernel space. +* @cm: contiguous memory to unmap. +* +* See cm_vmap(). +*/ +void cm_vunmap (struct cm *cm); + +#endif + +#endif
mm/Kconfig +22 -0
@@ -331,3 +331,25 @@ config CLEANCACHE in a negligible performance hit. If unsure, say Y to enable cleancache + +config CMA + bool "Contiguous Memory Allocator" + select MIGRATION + select GENERIC_ALLOCATOR + help + This enables the Contiguous Memory Allocator which allows drivers + to allocate big physically-contiguous blocks of memory for use with + hardware components that do not support I/O map nor scatter-gather. + + For more information see <include/linux/cma.h>. If unsure, say "n". + +config CMA_DEBUG + bool "CMA debug messages (DEVELOPEMENT)" + depends on CMA + 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".
mm/Makefile +1 -0
@@ -43,3 +43,4 @@ obj-$(CONFIG_HWPOISON_INJECT) += hwpoison-inject.o obj-$(CONFIG_DEBUG_KMEMLEAK) += kmemleak.o obj-$(CONFIG_DEBUG_KMEMLEAK_TEST) += kmemleak-test.o obj-$(CONFIG_CLEANCACHE) += cleancache.o +obj-$(CONFIG_CMA) += cma.o
mm/cma.c new file +328 -0
328 lines not on this page · raw message
Signature · 1 line
1.7.2.3
Re: [PATCHv8 07/12] mm: cma: Contiguous Memory Allocator added
Hi Michal,
Quoted 14 lines · Michal Nazarewicz
The Contiguous Memory Allocator is a set of functions that lets one initialise a region of memory which then can be used to perform allocations of contiguous memory chunks from.
CMA allows for creation of private and non-private contexts. The former is reserved for CMA and no other kernel subsystem can use it. The latter allows for movable pages to be allocated within CMA's managed memory so that it can be used for page cache when CMA devices do not use it.
Signed-off-by Michal Nazarewicz <[email protected]>Signed-off-by Kyungmin Park <[email protected]>---
<snip>
Quoted 29 lines
mm/cma.c new file +29
-0+/************************* Initialise CMA *************************/ + +unsigned long cma_reserve (unsigned long start,unsigned long size, +unsigned long alignment) +{ +pr_debug ("%s(%p+%p/%p)\n" , __func__, (void *)start, (void *)size, + (void *)alignment); + +/* Sanity checks */ +if (!size || (alignment & (alignment -1 ))) +return (unsigned long )-EINVAL ; + +/* Sanitise input arguments */ + start =PAGE_ALIGN (start); + size =PAGE_ALIGN (size); +if (alignment <PAGE_SIZE ) + alignment =PAGE_SIZE ; + +/* Reserve memory */ +if (start) { +if (memblock_is_region_reserved (start, size) || +memblock_reserve (start, size) <0 ) +return (unsigned long )-EBUSY ; + }else { +/* +* Use __memblock_alloc_base() since +* memblock_alloc_base() panic()s. +*/ +u64 addr =__memblock_alloc_base (size, alignment,0 );
mm/cma.c line 96
Quoted 10 lines
mm/cma.c new file +10
-0+if (!addr) { +return (unsigned long )-ENOMEM ; + }else if (addr + size > ~(unsigned long )0 ) { +memblock_free (addr, size); +return (unsigned long )-EOVERFLOW ; + }else { + start = addr; + } + } +
Reserving the areas of memory belonging to CMA using memblock_reserve, would preclude that range from the zones, due to which it would not be available for buddy allocations right ?
Quoted 4 lines
mm/cma.c new file +4
-0+return start; +} + +
Signature · 5 lines
Regards, Ankita Garg (ankita at in.ibm.com) Linux Technology Center IBM India Systems & Technology Labs, Bangalore, India
Re: [PATCHv8 07/12] mm: cma: Contiguous Memory Allocator added
Quoted 38 lines · Michal Nazarewicz
Quoted 37 lines
mm/cma.c new file +37
-0+unsigned long cma_reserve (unsigned long start,unsigned long size, +unsigned long alignment) +{ +pr_debug ("%s(%p+%p/%p)\n" , __func__, (void *)start, (void *)size, + (void *)alignment); + +/* Sanity checks */ +if (!size || (alignment & (alignment -1 ))) +return (unsigned long )-EINVAL ; + +/* Sanitise input arguments */ + start =PAGE_ALIGN (start); + size =PAGE_ALIGN (size); +if (alignment <PAGE_SIZE ) + alignment =PAGE_SIZE ; + +/* Reserve memory */ +if (start) { +if (memblock_is_region_reserved (start, size) || +memblock_reserve (start, size) <0 ) +return (unsigned long )-EBUSY ; + }else { +/* +* Use __memblock_alloc_base() since +* memblock_alloc_base() panic()s. +*/ +u64 addr =__memblock_alloc_base (size, alignment,0 ); +if (!addr) { +return (unsigned long )-ENOMEM ; + }else if (addr + size > ~(unsigned long )0 ) { +memblock_free (addr, size); +return (unsigned long )-EOVERFLOW ; + }else { + start = addr; + } + } +
Quoted 3 lines · Ankita Garg
Reserving the areas of memory belonging to CMA using
memblock_reserve, would preclude that range from the zones, due to which it would not be available for buddy allocations right ?
Correct. CMA however, injects allocated pageblocks to buddy so they end up in buddy with migratetype set to MIGRATE_CMA.
Quoted 2 lines
Quoted 2 lines
(quoted diff) +2
-0+ return start; +}
Signature · 4 lines
Best regards, _ _ .o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o ..o | Computer Science, Michal "mina86" Nazarewicz (o o) ooo +-<email/jid: [email protected]>--------ooO--(_)--Ooo--
PATCH v8 8/12 mm: MIGRATE_CMA migration type added
In reply to Michal NazarewiczThe MIGRATE_CMA migration type has two main characteristics:
(i) only movable pages can be allocated from MIGRATE_CMA pageblocks and (ii) page allocator will never change migration type of MIGRATE_CMA pageblocks.
This guarantees that page in a MIGRATE_CMA page block can always be migrated somewhere else (unless there's no memory left in the system).
It is designed to be used with Contiguous Memory Allocator (CMA) for allocating big chunks (eg. 10MiB) of physically contiguous memory. Once driver requests contiguous memory, CMA will migrate pages from MIGRATE_CMA pageblocks.
To minimise number of migrations, MIGRATE_CMA migration type is the last type tried when page allocator falls back to other migration types then requested.
--- include/linux/mmzone.h | 43 ++++++++++++++++++++---- mm/Kconfig | 14 ++++++++ mm/compaction.c | 10 +++++ mm/internal.h | 3 ++ mm/page_alloc.c | 87 +++++++++++++++++++++++++++++++++++++---------- 5 files changed, 131 insertions(+), 26 deletions(-)
include/linux/mmzone.h +36 -7
@@ -35,13 +35,37 @@*/ #define PAGE_ALLOC_COSTLY_ORDER 3 -#define MIGRATE_UNMOVABLE 0 -#define MIGRATE_RECLAIMABLE 1 -#define MIGRATE_MOVABLE 2 -#define MIGRATE_PCPTYPES 3/* the number of types on the pcp lists */ -#define MIGRATE_RESERVE 3 -#define MIGRATE_ISOLATE 4/* can't allocate from here */ -#define MIGRATE_TYPES 5 +enum { +MIGRATE_UNMOVABLE , +MIGRATE_RECLAIMABLE , +MIGRATE_MOVABLE , +MIGRATE_PCPTYPES ,/* the number of types on the pcp lists */ +MIGRATE_RESERVE =MIGRATE_PCPTYPES , +#ifdef CONFIG_MIGRATE_CMA +/* +* MIGRATE_CMA migration type is designed to mimic the way +* ZONE_MOVABLE works. Only movable pages can be allocated +* from MIGRATE_CMA pageblocks and page allocator never +* implicitly change migration type of MIGRATE_CMA pageblock. +* +* The way to use it is to change migratetype of a range of +* pageblocks to MIGRATE_CMA which can be done by +* __free_pageblock_cma() function. What is important though +* is that a range of pageblocks must be aligned to +* MAX_ORDER_NR_PAGES should biggest page be bigger then +* a single pageblock. +*/ +MIGRATE_CMA , +#endif +MIGRATE_ISOLATE ,/* can't allocate from here */ +MIGRATE_TYPES +}; + +#ifdef CONFIG_MIGRATE_CMA +# define is_migrate_cma (migratetype) unlikely((migratetype) == MIGRATE_CMA) +#else +# define is_migrate_cma (migratetype) false +#endif #define for_each_migratetype_order (order, type) \ for (order = 0; order < MAX_ORDER; order++) \
@@ -54,6 +78,11 @@ static inline int get_pageblock_migratetype(struct page *page)return get_pageblock_flags_group (page, PB_migrate, PB_migrate_end); } +static inline bool is_pageblock_cma (struct page *page) +{ +return is_migrate_cma (get_pageblock_migratetype (page)); +} +struct free_area {struct list_head free_list[MIGRATE_TYPES ];unsigned long nr_free;
mm/Kconfig +14 -0
@@ -343,6 +343,20 @@ config CMA For more information see <include/linux/cma.h>. If unsure, say "n". +config MIGRATE_CMA + bool "Use MIGRATE_CMA migratetype" + depends on CMA + default y + help + This enables the use the MIGRATE_CMA migrate type in the CMA. + MIGRATE_CMA lets CMA work on almost arbitrary memory range and + not only inside ZONE_MOVABLE. + + This option can also be selected by code that uses MIGRATE_CMA + even if CMA is not present. + + If unsure, say "y". + config CMA_DEBUG bool "CMA debug messages (DEVELOPEMENT)" depends on CMA
mm/compaction.c +10 -0
@@ -113,6 +113,16 @@ static bool suitable_migration_target(struct page *page)if (migratetype ==MIGRATE_ISOLATE || migratetype ==MIGRATE_RESERVE )return false; +/* Keep MIGRATE_CMA alone as well. */ +/* +* XXX Revisit. We currently cannot let compaction touch CMA +* pages since compaction insists on changing their migration +* type to MIGRATE_MOVABLE (see split_free_page() called from +* isolate_freepages_block() above). +*/ +if (is_migrate_cma (migratetype)) +return false; +/* If the page is a large free page, then allow migration */ if (PageBuddy (page) &&page_order (page) >= pageblock_order)return true;
mm/internal.h +3 -0
@@ -49,6 +49,9 @@ extern void putback_lru_page(struct page *page);* in mm/page_alloc.c */ extern void __free_pages_bootmem (struct page *page,unsigned int order); +#ifdef CONFIG_MIGRATE_CMA +extern void __free_pageblock_cma (struct page *page); +#endif extern void prep_compound_page (struct page *page,unsigned long order);#ifdef CONFIG_MEMORY_FAILURE extern bool is_free_buddy_page (struct page *page);
mm/page_alloc.c +68 -19
156 lines not on this page · raw message
Signature · 1 line
1.7.2.3
PATCH v8 9/12 mm: MIGRATE_CMA isolation functions added
In reply to Michal NazarewiczThis commit changes various functions that change pages and pageblocks migrate type between MIGRATE_ISOLATE and MIGRATE_MOVABLE in such a way as to allow to work with MIGRATE_CMA migrate type.
--- include/linux/page-isolation.h | 40 +++++++++++++++++++++++++++------------- mm/page_alloc.c | 19 ++++++++++++------- mm/page_isolation.c | 15 ++++++++------- 3 files changed, 47 insertions(+), 27 deletions(-)
include/linux/page-isolation.h +27 -13
@@ -3,39 +3,53 @@/* * Changes migrate type in [start_pfn, end_pfn) to be MIGRATE_ISOLATE. -* If specified range includes migrate types other than MOVABLE, +* If specified range includes migrate types other than MOVABLE or CMA, * this will fail with -EBUSY. * * For isolating all pages in the range finally, the caller have to * free all pages in the range. test_page_isolated() can be used for * test it. */ -extern int -start_isolate_page_range (unsigned long start_pfn,unsigned long end_pfn); +int __start_isolate_page_range (unsigned long start_pfn,unsigned long end_pfn, +unsigned migratetype); + +static inline int +start_isolate_page_range (unsigned long start_pfn,unsigned long end_pfn) +{ +return __start_isolate_page_range (start_pfn, end_pfn,MIGRATE_MOVABLE ); +} + +int __undo_isolate_page_range (unsigned long start_pfn,unsigned long end_pfn, +unsigned migratetype);/* * Changes MIGRATE_ISOLATE to MIGRATE_MOVABLE. * target range is [start_pfn, end_pfn) */ -extern int -undo_isolate_page_range (unsigned long start_pfn,unsigned long end_pfn); +static inline int +undo_isolate_page_range (unsigned long start_pfn,unsigned long end_pfn) +{ +return __undo_isolate_page_range (start_pfn, end_pfn,MIGRATE_MOVABLE ); +}/* -* test all pages in [start_pfn, end_pfn)are isolated or not. +* Test all pages in [start_pfn, end_pfn) are isolated or not. */ -extern int -test_pages_isolated (unsigned long start_pfn,unsigned long end_pfn); +int test_pages_isolated (unsigned long start_pfn,unsigned long end_pfn);/* -* Internal funcs.Changes pageblock's migrate type. -* Please use make_pagetype_isolated()/make_pagetype_movable(). +* Internal functions. Changes pageblock's migrate type. */ -extern int set_migratetype_isolate (struct page *page); -extern void unset_migratetype_isolate (struct page *page); +int set_migratetype_isolate (struct page *page); +void __unset_migratetype_isolate (struct page *page,unsigned migratetype); +static inline void unset_migratetype_isolate (struct page *page) +{ +__unset_migratetype_isolate (page,MIGRATE_MOVABLE ); +}extern unsigned long alloc_contig_freed_pages (unsigned long start,unsigned long end,gfp_t flag);extern int alloc_contig_range (unsigned long start,unsigned long end, -gfp_t flags); +gfp_t flags,unsigned migratetype);extern void free_contig_pages (struct page *page,int nr_pages);/*
mm/page_alloc.c +12 -7
@@ -5460,7 +5460,7 @@ out:return ret; } -void unset_migratetype_isolate (struct page *page) +void __unset_migratetype_isolate (struct page *page,unsigned migratetype) {struct zone *zone;unsigned long flags;
@@ -5468,8 +5468,8 @@ void unset_migratetype_isolate(struct page *page)spin_lock_irqsave (&zone->lock, flags);if (get_pageblock_migratetype (page) !=MIGRATE_ISOLATE ) goto out; -set_pageblock_migratetype (page,MIGRATE_MOVABLE ); -move_freepages_block (zone, page,MIGRATE_MOVABLE ); +set_pageblock_migratetype (page, migratetype); +move_freepages_block (zone, page, migratetype); out:spin_unlock_irqrestore (&zone->lock, flags); }
@@ -5574,6 +5574,10 @@ static int __alloc_contig_migrate_range(unsigned long start, unsigned long end)* @start: start PFN to allocate * @end: one-past-the-last PFN to allocate * @flags: flags passed to alloc_contig_freed_pages(). +* @migratetype: migratetype of the underlaying pageblocks (either +* #MIGRATE_MOVABLE or #MIGRATE_CMA). All pageblocks +* in range must have the same migratetype and it must +* be either of the two. * * The PFN range does not have to be pageblock or MAX_ORDER_NR_PAGES * aligned, hovewer it's callers responsibility to guarantee that we
@@ -5585,7 +5589,7 @@ static int __alloc_contig_migrate_range(unsigned long start, unsigned long end)* need to be freed with free_contig_pages(). */ int alloc_contig_range (unsigned long start,unsigned long end, -gfp_t flags) +gfp_t flags,unsigned migratetype) {unsigned long _start, _end;int ret;
@@ -5613,8 +5617,8 @@ int alloc_contig_range(unsigned long start, unsigned long end,* them. */ - ret =start_isolate_page_range (pfn_to_maxpage (start), -pfn_to_maxpage_up (end)); + ret =__start_isolate_page_range (pfn_to_maxpage (start), +pfn_to_maxpage_up (end), migratetype);if (ret) goto done;
@@ -5652,7 +5656,8 @@ int alloc_contig_range(unsigned long start, unsigned long end, ret =0 ; done: -undo_isolate_page_range (pfn_to_maxpage (start),pfn_to_maxpage_up (end)); +__undo_isolate_page_range (pfn_to_maxpage (start),pfn_to_maxpage_up (end), + migratetype);return ret; }
mm/page_isolation.c +8 -7
@@ -23,10 +23,11 @@ __first_valid_page(unsigned long pfn, unsigned long nr_pages) }/* -* start_isolate_page_range() -- make page-allocation-type of range of pages +* __start_isolate_page_range() -- make page-allocation-type of range of pages * to be MIGRATE_ISOLATE. * @start_pfn: The lower PFN of the range to be isolated. * @end_pfn: The upper PFN of the range to be isolated. +* @migratetype: migrate type to set in error recovery. * * Making page-allocation-type to be MIGRATE_ISOLATE means free pages in * the range will never be allocated. Any free pages and pages freed in the
@@ -35,8 +36,8 @@ __first_valid_page(unsigned long pfn, unsigned long nr_pages)* start_pfn/end_pfn must be aligned to pageblock_order. * Returns 0 on success and -EBUSY if any part of range cannot be isolated. */ -int -start_isolate_page_range (unsigned long start_pfn,unsigned long end_pfn) +int __start_isolate_page_range (unsigned long start_pfn,unsigned long end_pfn, +unsigned migratetype) {unsigned long pfn;unsigned long undo_pfn;
@@ -59,7 +60,7 @@ undo:for (pfn = start_pfn; pfn < undo_pfn; pfn += pageblock_nr_pages) -unset_migratetype_isolate (pfn_to_page (pfn)); +__unset_migratetype_isolate (pfn_to_page (pfn), migratetype);return -EBUSY ; }
@@ -67,8 +68,8 @@ undo:/* * Make isolated pages available again. */ -int -undo_isolate_page_range (unsigned long start_pfn,unsigned long end_pfn) +int __undo_isolate_page_range (unsigned long start_pfn,unsigned long end_pfn, +unsigned migratetype) {unsigned long pfn;struct page *page;
@@ -80,7 +81,7 @@ undo_isolate_page_range(unsigned long start_pfn, unsigned long end_pfn) page =__first_valid_page (pfn, pageblock_nr_pages);if (!page ||get_pageblock_migratetype (page) !=MIGRATE_ISOLATE )continue ; -unset_migratetype_isolate (page); +__unset_migratetype_isolate (page, migratetype); }return 0 ; }
Signature · 1 line
1.7.2.3
PATCH v8 10/12 mm: MIGRATE_CMA support added to CMA
In reply to Michal NazarewiczThis commit adds MIGRATE_CMA migratetype support to the CMA. The advantage is that an (almost) arbitrary memory range can be marked as MIGRATE_CMA which may not be the case with ZONE_MOVABLE.
--- include/linux/cma.h | 58 ++++++++++++++++--- mm/cma.c | 161 +++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 194 insertions(+), 25 deletions(-)
include/linux/cma.h +50 -8
@@ -71,9 +71,14 @@* a platform/machine specific function. For the former CMA * provides the following functions: * +* cma_init_migratetype() * cma_reserve() * cma_create() * +* The first one initialises a portion of reserved memory so that it +* can be used with CMA. The second first tries to reserve memory +* (using memblock) and then initialise it. +* * The cma_reserve() function must be called when memblock is still * operational and reserving memory with it is still possible. On * ARM platform the "reserve" machine callback is a perfect place to
@@ -93,21 +98,56 @@ struct cma;/* Contiguous Memory chunk */ struct cm ; +#ifdef CONFIG_MIGRATE_CMA + +/** +* cma_init_migratetype() - initialises range of physical memory to be used +* with CMA context. +* @start: start address of the memory range in bytes. +* @size: size of the memory range in bytes. +* +* The range must be MAX_ORDER_NR_PAGES aligned and it must have been +* already reserved (eg. with memblock). +* +* The actual initialisation is deferred until subsys initcalls are +* evaluated (unless this has already happened). +* +* Returns zero on success or negative error. +*/ +int cma_init_migratetype (unsigned long start,unsigned long end); + +#else + +static inline int cma_init_migratetype (unsigned long start,unsigned long end) +{ + (void )start; (void )end; +return -EOPNOTSUPP ; +} + +#endif +/** * cma_reserve() - reserves memory. * @start: start address of the memory range in bytes hint; if unsure * pass zero. * @size: size of the memory to reserve in bytes. * @alignment: desired alignment in bytes (must be power of two or zero). +* @init_migratetype: whether to initialise pageblocks. +* +* It will use memblock to allocate memory. If @init_migratetype is +* true, the function will also call cma_init_migratetype() on +* reserved region so that a non-private CMA context can be created on +* given range. * -* It will use memblock to allocate memory. @start and @size will be -* aligned to PAGE_SIZE. +* @start and @size will be aligned to PAGE_SIZE if @init_migratetype +* is false or to (MAX_ORDER_NR_PAGES << PAGE_SHIFT) if +* @init_migratetype is true. * * Returns reserved's area physical address or value that yields true * when checked with IS_ERR_VALUE(). */ unsigned long cma_reserve (unsigned long start,unsigned long size, -unsigned long alignment); +unsigned long alignment,_Bool init_migratetype);/** * cma_create() - creates a CMA context.
@@ -118,12 +158,14 @@ unsigned long cma_reserve(unsigned long start, unsigned long size,* * The range must be page aligned. Different contexts cannot overlap. * -* Unless @private is true the memory range must lay in ZONE_MOVABLE. -* If @private is true no underlaying memory checking is done and -* during allocation no pages migration will be performed - it is -* assumed that the memory is reserved and only CMA manages it. +* Unless @private is true the memory range must either lay in +* ZONE_MOVABLE or must have been initialised with +* cma_init_migratetype() function. If @private is true no +* underlaying memory checking is done and during allocation no pages +* migration will be performed - it is assumed that the memory is +* reserved and only CMA manages it. * -* @start and @size must be page and @min_alignment alignment. +* @start and @size must be page and @min_alignment aligned. * @min_alignment specifies the minimal alignment that user will be * able to request through cm_alloc() function. In most cases one * will probably pass zero as @min_alignment but if the CMA context
mm/cma.c +144 -17
244 lines not on this page · raw message
Signature · 1 line
1.7.2.3
PATCH v8 11/12 mm: cma: Test device and application added
In reply to Michal NazarewiczThis 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 | 28 +++ drivers/misc/Makefile | 1 + drivers/misc/cma-dev.c | 238 +++++++++++++++++++++++++ include/linux/cma.h | 29 +++ tools/cma/cma-test.c | 457 ++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 753 insertions(+), 0 deletions(-) create mode 100644 drivers/misc/cma-dev.c create mode 100644 tools/cma/cma-test.c
drivers/misc/Kconfig +28 -0
@@ -458,4 +458,32 @@ source "drivers/misc/cb710/Kconfig" source "drivers/misc/iwmc3200top/Kconfig" source "drivers/misc/ti-st/Kconfig" +# Selet this for platforms/machines that implemented code making +# the CMA test device usable. +config CMA_DEVICE_POSSIBLE + bool + +config CMA_DEVICE + tristate "CMA test device (DEVELOPEMENT)" + depends on CMA && CMA_DEVICE_POSSIBLE + help + The CMA misc device allows allocating contiguous memory areas + from user space. This is for testing of the CMA framework. + + If unsure, say "n" + +# Selet this for platforms/machines that implemented code making +# the CMA test device usable. +config CMA_DEVICE_POSSIBLE + bool + +config CMA_DEVICE + tristate "CMA test device (DEVELOPEMENT)" + depends on CMA && CMA_DEVICE_POSSIBLE + help + The CMA misc device allows allocating contiguous memory areas + from user space. This is for testing of the CMA framework. + + If unsure, say "n" + endif # MISC_DEVICES
drivers/misc/Makefile +1 -0
@@ -42,3 +42,4 @@ obj-$(CONFIG_ARM_CHARLCD) += arm-charlcd.o obj-$(CONFIG_PCH_PHUB) += pch_phub.o obj-y += ti-st/ obj-$(CONFIG_AB8500_PWM) += ab8500-pwm.o +obj-$(CONFIG_CMA_DEVICE) += cma-dev.o
drivers/misc/cma-dev.c new file +238 -0
238 lines not on this page · raw message
include/linux/cma.h +29 -0
@@ -89,6 +89,35 @@* to be called after SLAB is initialised. */ +#include <linux/ioctl.h> +#include <linux/types.h> + + +#define CMA_MAGIC (('c' << 24) | ('M' << 16) | ('a' << 8) | 0x42) + +/** +* An information about area exportable to user space. +* @magic: must always be CMA_MAGIC. +* @_pad: padding (ignored). +* @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 _pad; + +/* __u64 to be compatible accross 32 and 64 bit systems. */ +__u64 size; +__u64 alignment; +__u64 start; +}; + +#define IOCTL_CMA_ALLOC _IOWR('p', 0, struct cma_alloc_request) +#define IOCTL_CMA_PATTERN _IO('p', 1) +#define IOCTL_CMA_DUMP _IO('p', 2) + +/***************************** Kernel level API *****************************/ #if defined __KERNEL__ && definedCONFIG_CMA
tools/cma/cma-test.c new file +457 -0
457 lines not on this page · raw message
Signature · 1 line
1.7.2.3
PATCH v8 12/12 ARM: cma: Added CMA to Aquila, Goni and c210 universal boards
In reply to Michal NazarewiczThis commit adds CMA memory reservation code to Aquila, Goni and c210 universal boards.
--- arch/arm/mach-s5pv210/Kconfig | 2 + arch/arm/mach-s5pv210/mach-aquila.c | 2 + arch/arm/mach-s5pv210/mach-goni.c | 2 + arch/arm/mach-s5pv310/Kconfig | 1 + arch/arm/mach-s5pv310/mach-universal_c210.c | 2 + arch/arm/plat-s5p/Makefile | 2 + arch/arm/plat-s5p/cma-stub.c | 49 +++++++++++++++++++++++++++ arch/arm/plat-s5p/include/plat/cma-stub.h | 21 +++++++++++ 8 files changed, 81 insertions(+), 0 deletions(-) create mode 100644 arch/arm/plat-s5p/cma-stub.c create mode 100644 arch/arm/plat-s5p/include/plat/cma-stub.h
arch/arm/mach-s5pv210/Kconfig +2 -0
@@ -68,6 +68,7 @@ config MACH_AQUILA select S5P_DEV_ONENAND select S5PV210_SETUP_FB_24BPP select S5PV210_SETUP_SDHCI + select CMA_DEVICE_POSSIBLE help Machine support for the Samsung Aquila target based on S5PC110 SoC
@@ -92,6 +93,7 @@ config MACH_GONI select S5PV210_SETUP_I2C2 select S5PV210_SETUP_KEYPAD select S5PV210_SETUP_SDHCI + select CMA_DEVICE_POSSIBLE help Machine support for Samsung GONI board S5PC110(MCP) is one of package option of S5PV210
arch/arm/mach-s5pv210/mach-aquila.c +2 -0
@@ -39,6 +39,7 @@#include <plat/fb.h> #include <plat/fimc-core.h> #include <plat/sdhci.h> +#include <plat/cma-stub.h> /* Following are default values for UCON, ULCON and UFCON UART registers */ #define AQUILA_UCON_DEFAULT (S3C2410_UCON_TXILEVEL | \
@@ -690,4 +691,5 @@ MACHINE_START(AQUILA, "Aquila") .map_io = aquila_map_io, .init_machine = aquila_machine_init, .timer = &s3c24xx_timer, + .reserve = cma_mach_reserve,MACHINE_END
arch/arm/mach-s5pv210/mach-goni.c +2 -0
@@ -45,6 +45,7 @@#include <plat/keypad.h> #include <plat/sdhci.h> #include <plat/clock.h> +#include <plat/cma-stub.h> /* Following are default values for UCON, ULCON and UFCON UART registers */ #define GONI_UCON_DEFAULT (S3C2410_UCON_TXILEVEL | \
@@ -865,4 +866,5 @@ MACHINE_START(GONI, "GONI") .map_io = goni_map_io, .init_machine = goni_machine_init, .timer = &s3c24xx_timer, + .reserve = cma_mach_reserve,MACHINE_END
arch/arm/mach-s5pv310/Kconfig +1 -0
@@ -85,6 +85,7 @@ config MACH_UNIVERSAL_C210 select S5P_DEV_ONENAND select S3C_DEV_I2C1 select S5PV310_SETUP_I2C1 + select CMA_DEVICE_POSSIBLE help Machine support for Samsung Mobile Universal S5PC210 Reference Board. S5PC210(MCP) is one of package option of S5PV310
arch/arm/mach-s5pv310/mach-universal_c210.c +2 -0
@@ -21,6 +21,7 @@#include <plat/s5pv310.h> #include <plat/cpu.h> #include <plat/devs.h> +#include <plat/cma-stub.h> #include <mach/map.h>
@@ -152,6 +153,7 @@ MACHINE_START(UNIVERSAL_C210, "UNIVERSAL_C210") .boot_params = S5P_PA_SDRAM + 0x100, .init_irq = s5pv310_init_irq, .map_io = universal_map_io, + .reserve = cma_mach_reserve, .init_machine = universal_machine_init, .timer = &s5pv310_timer,MACHINE_END
arch/arm/plat-s5p/Makefile +2 -0
@@ -28,3 +28,5 @@ obj-$(CONFIG_S5P_DEV_FIMC0) += dev-fimc0.o obj-$(CONFIG_S5P_DEV_FIMC1) += dev-fimc1.o obj-$(CONFIG_S5P_DEV_FIMC2) += dev-fimc2.o obj-$(CONFIG_S5P_DEV_ONENAND) += dev-onenand.o + +obj-$(CONFIG_CMA) += cma-stub.o
arch/arm/plat-s5p/cma-stub.c new file +49 -0
49 lines not on this page · raw message
arch/arm/plat-s5p/include/plat/cma-stub.h new file +21 -0
@@ -0,0 +1,21 @@ +/* +* This file is just a quick and dirty hack to get CMA testing device +* working. The cma_mach_reserve() should be called as mach's reserve +* callback. CMA testing device will use cma_ctx for allocations. +*/ + +struct cma ; + +#ifdef CONFIG_CMA + +extern struct cma *cma_ctx; + +void cma_mach_reserve (void ); + +#else + +#define cma_ctx ((struct cma *)NULL) + +#define cma_mach_reserve NULL + +#endif
Signature · 1 line
1.7.2.3
Hi Andrew,
any comments? what's the next step to merge it for 2.6.38 kernel. we want to use this feature at mainline kernel.
Any idea and comments are welcome.
Thank you,
Kyungmin Park
Quoted 298 lines · Michal Nazarewicz
Hello everyone,
This is yet another version of CMA this time stripped from a lot of code and with working migration implementation.
? The Contiguous Memory Allocator (CMA) makes it possible for ? device drivers to allocate big contiguous chunks of memory after ? the system has booted.
For more information see 7th patch in the set.
This version fixes some things Kamezawa suggested plus it separates code that uses
MIGRATE_CMAfrom the rest of the code. ?This I hope will help to grasp the overall idea of CMA.The current version is just an allocator that handles allocation of contiguous memory blocks. ?The difference between this patchset and Kamezawa's
alloc_contig_pages()are:1.
alloc_contig_pages()requiresMAX_ORDERalignment of allocations ? which may be unsuitable for embeded systems where a few MiBs are ? required.? Lack of the requirement on the alignment means that several threads ? might try to access the same pageblock/page. ?To prevent this from ? happening CMA uses a mutex so that only one cm_alloc()/cm_free() ? function may run at one point.
2. CMA may use its own migratetype (
MIGRATE_CMA) which behaves ? similarly toZONE_MOVABLEbut can be put in arbitrary places.? This is required for us since we need to define two disjoint memory ? ranges inside system RAM. ?(ie. in two memory banks (do not confuse ? with nodes)).
3.
alloc_contig_pages()scans memory in search for range that could be ? migrated. ?CMA on the other hand maintains its own allocator to ? decide where to allocate memory for device drivers and then tries ? to migrate pages from that part if needed. ?This is not strictly ? required but I somehow feel it might be faster.Links to previous versions of the patchset:
v7: <http://article.gmane.org/gmane.linux.kernel.mm/55626>
v6: <http://article.gmane.org/gmane.linux.kernel.mm/55626>
v5: (intentionally left out as CMA v5 was identical to CMA v4)
v4: <http://article.gmane.org/gmane.linux.kernel.mm/52010>
v3: <http://article.gmane.org/gmane.linux.kernel.mm/51573>
v2: <http://article.gmane.org/gmane.linux.kernel.mm/50986>
v1: <http://article.gmane.org/gmane.linux.kernel.mm/50669>Changelog:
v8: 1. The
alloc_contig_range()function has now been separated from ? ? ? CMA and put inpage_allocator.c. ?This function tries to ? ? ? migrate all LRU pages in specified range and then allocate the ? ? ? range usingalloc_contig_freed_pages().? ?2. Support for
MIGRATE_CMAhas been separated from the CMA code. ? ? ? I have not tested if CMA works withZONE_MOVABLEbut I see no ? ? ? reasons why it shouldn't.? ?3. I have added a @private argument when creating CMA contexts so ? ? ? that one can reserve memory and not share it with the rest of ? ? ? the system. ?This way, CMA acts only as allocation algorithm.
v7: 1. A lot of functionality that handled
driver->allocator_context? ? ? mapping has been removed from the patchset. ?This is not to say ? ? ? that this code is not needed, it's just not worth posting ? ? ? everything in one patchset.? ? ? Currently, CMA is "just" an allocator. ?It uses it's own ? ? ? migratetype (
MIGRATE_CMA) for defining ranges of pageblokcs ? ? ? which behave just likeZONE_MOVABLEbut dispite the latter can ? ? ? be put in arbitrary places.? ?2. The migration code that was introduced in the previous version ? ? ? actually started working.
v6: 1. Most importantly, v6 introduces support for memory migration. ? ? ? The implementation is not yet complete though.
? ? ? Migration support means that when CMA is not using memory ? ? ? reserved for it, page allocator can allocate pages from it. ? ? ? When CMA wants to use the memory, the pages have to be moved ? ? ? and/or evicted as to make room for CMA.
? ? ? To make it possible it must be guaranteed that only movable and ? ? ? reclaimable pages are allocated in CMA controlled regions. ? ? ? This is done by introducing a
MIGRATE_CMAmigrate type that ? ? ? guarantees exactly that.? ? ? Some of the migration code is "borrowed" from Kamezawa ? ? ? Hiroyuki's
alloc_contig_pages()implementation. ?The main ? ? ? difference is that thanks toMIGRATE_CMAmigrate type CMA ? ? ? assumes that memory controlled by CMA are is always movable or ? ? ? reclaimable so that it makes allocation decisions regardless of ? ? ? the whether some pages are actually allocated and migrates them ? ? ? if needed.? ? ? The most interesting patches from the patchset that implement ? ? ? the functionality are:
? ? ? ? 09/13: mm:
alloc_contig_free_pages()added ? ? ? ? 10/13: mm:MIGRATE_CMAmigration type added ? ? ? ? 11/13: mm:MIGRATE_CMAisolation functions added ? ? ? ? 12/13: mm: cma: Migration support added [wip]? ? ? Currently, kernel panics in some situations which I am trying ? ? ? to investigate.
? ?2.
cma_pin()andcma_unpin()functions has been added (after ? ? ? a conversation with Johan Mossberg). ?The idea is that whenever ? ? ? hardware does not use the memory (no transaction is on) the ? ? ? chunk can be moved around. ?This would allow defragmentation to ? ? ? be implemented if desired. ?No defragmentation algorithm is ? ? ? provided at this time.? ?3. Sysfs support has been replaced with debugfs. ?I always felt ? ? ? unsure about the sysfs interface and when Greg KH pointed it ? ? ? out I finally got to rewrite it to debugfs.
v5: (intentionally left out as CMA v5 was identical to CMA v4)
v4: 1. The "asterisk" flag has been removed in favour of requiring ? ? ? that platform will provide a "*=<regions>" rule in the map ? ? ? attribute.
? ?2. The terminology has been changed slightly renaming "kind" to ? ? ? "type" of memory. ?In the previous revisions, the documentation ? ? ? indicated that device drivers define memory kinds and now,
v3: 1. The command line parameters have been removed (and moved to ? ? ? a separate patch, the fourth one). ?As a consequence, the ? ? ?
cma_set_defaults()function has been changed -- it no longer ? ? ? accepts a string with list of regions but an array of regions.? ?2. The "asterisk" attribute has been removed. ?Now, each region ? ? ? has an "asterisk" flag which lets one specify whether this ? ? ? region should by considered "asterisk" region.
? ?3. SysFS support has been moved to a separate patch (the third one ? ? ? in the series) and now also includes list of regions.
v2: 1. The "
cma_map" command line have been removed. ?In exchange, ? ? ? a SysFS entry has been created underkernel/mm/contiguous.? ? ? The intended way of specifying the attributes is
? ? ? acma_set_defaults()function called by platform initialisation ? ? ? code. ?"regions" attribute (the string specified by "cma" ? ? ? command line parameter) can be overwritten with command line ? ? ? parameter; the other attributes can be changed during run-time ? ? ? using the SysFS entries.? ?2. The behaviour of the "map" attribute has been modified ? ? ? slightly. ?Currently, if no rule matches given device it is ? ? ? assigned regions specified by the "asterisk" attribute. ?It is ? ? ? by default built from the region names given in "regions" ? ? ? attribute.
? ?3. Devices can register private regions as well as regions that ? ? ? can be shared but are not reserved using standard CMA ? ? ? mechanisms. ?A private region has no name and can be accessed ? ? ? only by devices that have the pointer to it.
? ?4. The way allocators are registered has changed. ?Currently, ? ? ? a
cma_allocator_register()function is used for that purpose. ? ? ? Moreover, allocators are attached to regions the first time ? ? ? memory is registered from the region or when allocator is ? ? ? registered which means that allocators can be dynamic modules ? ? ? that are loaded after the kernel booted (of course, it won't be ? ? ? possible to allocate a chunk of memory from a region if ? ? ? allocator is not loaded).? ?5. Index of new functions:
? ?+static inline
dma_addr_t__must_check
? ?+cma_alloc_from(const char *regions,size_tsize, ? ?+ ? ? ? ? ? ? ?dma_addr_talignment)? ?+static inline int
? ?+cma_info_about(structcma_info*info, const const char *regions)? ?+int
__must_checkcma_region_register(struct cma_region *reg);? ?+dma_addr_t
__must_check
? ?+cma_alloc_from_region(structcma_region*reg,
? ?+ ? ? ? ? ? ? ? ? ? ? ?size_t size,dma_addr_talignment);? ?+static inline
dma_addr_t__must_check
? ?+cma_alloc_from(const char *regions,
? ?+ ? ? ? ? ? ? ?size_tsize,dma_addr_talignment);? ?+int
cma_allocator_register(struct cma_allocator *alloc);Patches in this patchset:
?mm:
migrate.c: fix compilation error? ?I had some strange compilation error; this patch fixed them.
?lib: bitmap: Added alignment offset for
bitmap_find_next_zero_area()?lib: genalloc: Generic allocator improvements? ?Some improvements to genalloc API (most importantly possibility to ? ?allocate memory with alignment requirement).
?mm: move some functions from
memory_hotplug.ctopage_isolation.c?mm:alloc_contig_freed_pages()added? ?Code "stolen" from Kamezawa. ?The first patch just moves code ? ?around and the second provide function for "allocates" already ? ?freed memory.
?mm:
alloc_contig_range()added? ?This is what Kamezawa asked: a function that tries to migrate all ? ?pages from given range and then use
alloc_contig_freed_pages()? ?(defined by the previous commit) to allocate those pages.?mm: cma: Contiguous Memory Allocator added
? ?The CMA code but with no
MIGRATE_CMAsupport yet. ?This assues ? ?that one uses aZONE_MOVABLE. ?I must admit I have not test that ? ?setup yet but I don't see any reasons why it should not work.?mm:
MIGRATE_CMAmigration type added
?mm:MIGRATE_CMAisolation functions added
?mm:MIGRATE_CMAsupport added to CMA? ?Introduction of the new migratetype and support for it in CMA. ? ?MIGRATE_CMA works similar to
ZONE_MOVABLEexpect almost any ? ?memory range can be marked as one.?mm: cma: Test device and application added
? ?Test device and application. ?Not really for merging; just for ? ?testing really.
?ARM: cma: Added CMA to Aquila, Goni and c210 universal boards
? ?A stub integration with some ARM machines. ?Mostly to get the cma ? ?testing device working. ?Again, not for merging, just an example.
?arch/arm/mach-s5pv210/Kconfig ? ? ? ? ? ? ? | ? ?2 + ?arch/arm/mach-s5pv210/mach-aquila.c ? ? ? ? | ? ?2 + ?arch/arm/mach-s5pv210/mach-goni.c ? ? ? ? ? | ? ?2 + ?arch/arm/mach-s5pv310/Kconfig ? ? ? ? ? ? ? | ? ?1 + ?arch/arm/mach-s5pv310/mach-universal_c210.c | ? ?2 + ?arch/arm/plat-s5p/Makefile ? ? ? ? ? ? ? ? ?| ? ?2 + ?arch/arm/plat-s5p/cma-stub.c ? ? ? ? ? ? ? ?| ? 49 +++ ?arch/arm/plat-s5p/include/plat/cma-stub.h ? | ? 21 ++ ?drivers/misc/Kconfig ? ? ? ? ? ? ? ? ? ? ? ?| ? 28 ++ ?drivers/misc/Makefile ? ? ? ? ? ? ? ? ? ? ? | ? ?1 + ?drivers/misc/cma-dev.c ? ? ? ? ? ? ? ? ? ? ?| ?238 ++++++++++++++ ?include/linux/bitmap.h ? ? ? ? ? ? ? ? ? ? ?| ? 24 ++- ?include/linux/cma.h ? ? ? ? ? ? ? ? ? ? ? ? | ?290 +++++++++++++++++ ?include/linux/genalloc.h ? ? ? ? ? ? ? ? ? ?| ? 46 ++-- ?include/linux/mmzone.h ? ? ? ? ? ? ? ? ? ? ?| ? 43 ++- ?include/linux/page-isolation.h ? ? ? ? ? ? ?| ? 50 +++- ?lib/bitmap.c ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?| ? 22 +- ?lib/genalloc.c ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?| ?182 ++++++----- ?mm/Kconfig ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?| ? 36 ++ ?mm/Makefile ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | ? ?1 + ?mm/cma.c ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?| ?455 ++++++++++++++++++++++++++ ?mm/compaction.c ? ? ? ? ? ? ? ? ? ? ? ? ? ? | ? 10 + ?mm/internal.h ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? | ? ?3 + ?mm/memory_hotplug.c ? ? ? ? ? ? ? ? ? ? ? ? | ?108 ------- ?mm/migrate.c ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?| ? ?2 + ?mm/page_alloc.c ? ? ? ? ? ? ? ? ? ? ? ? ? ? | ?286 +++++++++++++++-- ?mm/page_isolation.c ? ? ? ? ? ? ? ? ? ? ? ? | ?126 +++++++- ?tools/cma/cma-test.c ? ? ? ? ? ? ? ? ? ? ? ?| ?457 +++++++++++++++++++++++++++?28 files changed, 2219 insertions(+), 270 deletions(-) ?create mode 100644
arch/arm/plat-s5p/cma-stub.c?create mode 100644arch/arm/plat-s5p/include/plat/cma-stub.h?create mode 100644drivers/misc/cma-dev.c
?create mode 100644include/linux/cma.h
?create mode 100644mm/cma.c
?create mode 100644tools/cma/cma-test.c--
1.7.2.3--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to majordomo at kvack.org. ?For more info on Linux MM,
see: http://www.linux-mm.org/ .
Fight unfair telecom policy in Canada: sign http://dissolvethecrtc.ca/
Don't email: <a href=mailto:"[email protected]"> email at kvack.org </a>
Quoted 4 lines · Kyungmin Park
Hi Andrew,
any comments? what's the next step to merge it for 2.6.38 kernel. we want to use this feature at mainline kernel.
Has anyone addressed my issue with it that this is wide-open for abuse by allocating large chunks of memory, and then remapping them in some way with different attributes, thereby violating the ARM architecture specification?
In other words, do we _actually_ have a use for this which doesn't involve doing something like allocating 32MB of memory from it, remapping it so that it's DMA coherent, and then performing DMA on the resulting buffer?
Hello,
Quoted 10 lines · Russell King - ARM Linux
Quoted 4 lines · Kyungmin Park
Hi Andrew,
any comments? what's the next step to merge it for 2.6.38 kernel. we want to use this feature at mainline kernel.
Has anyone addressed my issue with it that this is wide-open for abuse by allocating large chunks of memory, and then remapping them in some way with different attributes, thereby violating the ARM architecture specification?
Actually this contiguous memory allocator is a better replacement for alloc_pages() which is used by dma_alloc_coherent(). It is a generic framework that is not tied only to ARM architecture.
Quoted 4 lines
In other words, do we _actually_ have a use for this which doesn't involve doing something like allocating 32MB of memory from it, remapping it so that it's DMA coherent, and then performing DMA on the resulting buffer?
This is an arm specific problem, also related to dma_alloc_coherent() allocator. To be 100% conformant with ARM specification we would probably need to unmap all pages used by the dma_coherent allocator from the LOW MEM area. This is doable, but completely not related to the CMA and this patch series.
Best regards
--
Marek Szyprowski
Samsung Poland R&D Center
Quoted 3 lines · Marek Szyprowski
Actually this contiguous memory allocator is a better replacement for
alloc_pages()which is used bydma_alloc_coherent(). It is a generic framework that is not tied only to ARM architecture.
... which is open to abuse. What I'm trying to find out is - if it can't be used for DMA, what is it to be used for?
Or are we inventing an everything-but-ARM framework?
Quoted 10 lines
Quoted 4 lines
In other words, do we _actually_ have a use for this which doesn't involve doing something like allocating 32MB of memory from it, remapping it so that it's DMA coherent, and then performing DMA on the resulting buffer?
This is an arm specific problem, also related to
dma_alloc_coherent()allocator. To be 100% conformant with ARM specification we would probably need to unmap all pages used by thedma_coherentallocator from the LOW MEM area. This is doable, but completely not related to the CMA and this patch series.
You've already been told why we can't unmap pages from the kernel direct mapping.
Okay, so I'm just going to assume that CMA has _no_ _business_ being used on ARM, and is not something that should interest anyone in the ARM community.
Hello,
Quoted 9 lines · Russell King - ARM Linux
Quoted 3 lines · Marek Szyprowski
Actually this contiguous memory allocator is a better replacement for
alloc_pages()which is used bydma_alloc_coherent(). It is a generic framework that is not tied only to ARM architecture.... which is open to abuse. What I'm trying to find out is - if it can't be used for DMA, what is it to be used for?
Or are we inventing an everything-but-ARM framework?
We are trying to get something that really works and SOLVES some of the problems with real devices that require contiguous memory for DMA.
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
In other words, do we _actually_ have a use for this which doesn't involve doing something like allocating 32MB of memory from it, remapping it so that it's DMA coherent, and then performing DMA on the resulting buffer?
Quoted 8 lines
Quoted 5 lines
This is an arm specific problem, also related to
dma_alloc_coherent()allocator. To be 100% conformant with ARM specification we would probably need to unmap all pages used by thedma_coherentallocator from the LOW MEM area. This is doable, but completely not related to the CMA and this patch series.You've already been told why we can't unmap pages from the kernel direct mapping.
It requires some amount of work but I see no reason why we shouldn't be able to unmap that pages to stay 100% conformant with ARM spec.
Please notice that there are also use cases where the memory will not be accessed by the CPU at all (like DMA transfers between multimedia devices and the system memory).
Quoted 3 lines
Okay, so I'm just going to assume that CMA has _no_ _business_ being used on ARM, and is not something that should interest anyone in the ARM community.
Go ahead! Remeber to remove dma_coherent because it also breaks the spec. :) Oh, I forgot. We can also remove all device drivers that might use DMA. :)
Merry Christmas and Happy New Year for everyone! :)
Best regards
--
Marek Szyprowski
Samsung Poland R&D Center
Quoted 9 lines · Marek Szyprowski
Hello,
Quoted 5 lines · Russell King - ARM Linux
Quoted 3 lines · Marek Szyprowski
Actually this contiguous memory allocator is a better replacement for
alloc_pages()which is used bydma_alloc_coherent(). It is a generic framework that is not tied only to ARM architecture.
Quoted 7 lines
Quoted 4 lines
... which is open to abuse. What I'm trying to find out is - if it can't be used for DMA, what is it to be used for?
Or are we inventing an everything-but-ARM framework?
We are trying to get something that really works and SOLVES some of the problems with real devices that require contiguous memory for DMA.
So, here you've confirmed that it's for DMA.
Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
Quoted 4 lines
In other words, do we _actually_ have a use for this which doesn't involve doing something like allocating 32MB of memory from it, remapping it so that it's DMA coherent, and then performing DMA on the resulting buffer?
This is an arm specific problem, also related to
dma_alloc_coherent()allocator. To be 100% conformant with ARM specification we would probably need to unmap all pages used by thedma_coherentallocator from the LOW MEM area. This is doable, but completely not related to the CMA and this patch series.
Quoted 5 lines
Quoted 2 lines
You've already been told why we can't unmap pages from the kernel direct mapping.
It requires some amount of work but I see no reason why we shouldn't be able to unmap that pages to stay 100% conformant with ARM spec.
I have considered - and tried - to do that with the dma_alloc_coherent() spec, but it is NOT POSSIBLE to do so - too many factors stand in the way of making it work, such as the need bring the system to a complete halt to modify all the L1 page tables and broadcast the TLB operations to invalidate the old mappings. None of that can be done from all the contexts under which dma_alloc_coherent() is called from.
Quoted 3 lines
Please notice that there are also use cases where the memory will not be accessed by the CPU at all (like DMA transfers between multimedia devices and the system memory).
Rubbish - if you think that, then you have very little understanding of modern CPUs. Modern CPUs speculatively access _any_ memory which is visible to them, and as the ARM architecture progresses, the speculative prefetching will become more aggressive. So if you have memory mapped in the kernel direct map, then you _have_ to assume that the CPU will fire off accesses to that memory at any time, loading it into its cache.
Quoted 6 lines
Quoted 3 lines
Okay, so I'm just going to assume that CMA has _no_ _business_ being used on ARM, and is not something that should interest anyone in the ARM community.
Go ahead! Remeber to remove
dma_coherentbecause it also breaks the spec. :) Oh, I forgot. We can also remove all device drivers that might use DMA. :)
The only solution I've come up for dma_alloc_coherent() is to reserve the entire coherent DMA region at boot time, taking it out of the kernel's view of available memory and thereby preventing it from ever being mapped or the kernel using that memory for any other purpose. That's about the best we can realistically do for ARM to conform to the spec.
Every time I've brought this issue up with you, you've brushed it aside. So if you feel that the right thing to do is to ignore such issues, you won't be surprised if I keep opposing your efforts to get this into mainline.
If you're serious about making this work, then provide some proper code which shows how to use this for DMA on ARM systems without violating the architecture specification. Until you do, I see no hope that CMA will ever be suitable for use on ARM.
Hello,
I'm sorry for the delay. Just got back from my holidays and getting thought the mails.
Quoted 13 lines · Russell King - ARM Linux
Quoted 12 lines · Marek Szyprowski
Hello,
Quoted 8 lines · Russell King - ARM Linux
Quoted 3 lines · Marek Szyprowski
Actually this contiguous memory allocator is a better replacement for
alloc_pages()which is used bydma_alloc_coherent(). It is a generic framework that is not tied only to ARM architecture.... which is open to abuse. What I'm trying to find out is - if it can't be used for DMA, what is it to be used for?
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
Or are we inventing an everything-but-ARM framework?
We are trying to get something that really works and SOLVES some of the problems with real devices that require contiguous memory for DMA.
So, here you've confirmed that it's for DMA.
Right, otherwise it wouldn't really make much sense. Note that our proposal already works for non-arm platforms.
Quoted 17 lines
Quoted 11 lines
Quoted 11 lines
Quoted 10 lines
Quoted 4 lines
In other words, do we _actually_ have a use for this which doesn't involve doing something like allocating 32MB of memory from it, remapping it so that it's DMA coherent, and then performing DMA on the resulting buffer?
This is an arm specific problem, also related to
dma_alloc_coherent()allocator. To be 100% conformant with ARM specification we would probably need to unmap all pages used by thedma_coherentallocator from the LOW MEM area. This is doable, but completely not related to the CMA and this patch series.Quoted 5 lines
Quoted 2 lines
You've already been told why we can't unmap pages from the kernel direct mapping.
It requires some amount of work but I see no reason why we shouldn't be able to unmap that pages to stay 100% conformant with ARM spec.
Quoted 6 lines
I have considered - and tried - to do that with the
dma_alloc_coherent()spec, but it is NOT POSSIBLE to do so - too many factors stand in the way of making it work, such as the need bring the system to a complete halt to modify all the L1 page tables and broadcast the TLB operations to invalidate the old mappings. None of that can be done from all the contexts under whichdma_alloc_coherent()is called from.
I understand that this requires a lot of work, but I still think this might be possible to get it working with some additional constraints.
I understand that modifying L1 page tables is definitely not a proper way of handling this. It simply costs too much. But what if we consider that the DMA memory can be only allocated from a specific range of the system memory? Assuming that this range of memory is known during the boot time, it CAN be mapped with two-level of tables in MMU. First level mapping will stay the same all the time for all processes, but it would be possible to unmap the pages required for DMA from the second level mapping what will be visible from all the processes at once.
Is there any reason why such solution won't work?
Best regards
--
Marek Szyprowski
Samsung Poland R&D Center
Quoted 8 lines · Marek Szyprowski
I understand that modifying L1 page tables is definitely not a proper way of handling this. It simply costs too much. But what if we consider that the DMA memory can be only allocated from a specific range of the system memory? Assuming that this range of memory is known during the boot time, it CAN be mapped with two-level of tables in MMU. First level mapping will stay the same all the time for all processes, but it would be possible to unmap the pages required for DMA from the second level mapping what will be visible from all the processes at once.
How much memory are we talking about? What is the typical figure?
Quoted 1 line
Is there any reason why such solution won't work?
It could work indeed.
One similar solution that is already in place is to use highmem for that reclaimable DMA memory. It is easy to ensure affected highmem pages are not mapped in kernel space. And you can decide at boot time how many highmem pages you want even if the system has less that 1GB of RAM.
Nicolas
Hello,
Quoted 11 lines · Nicolas Pitre
Quoted 8 lines · Marek Szyprowski
I understand that modifying L1 page tables is definitely not a proper way of handling this. It simply costs too much. But what if we consider that the DMA memory can be only allocated from a specific range of the system memory? Assuming that this range of memory is known during the boot time, it CAN be mapped with two-level of tables in MMU. First level mapping will stay the same all the time for all processes, but it would be possible to unmap the pages required for DMA from the second level mapping what will be visible from all the processes at once.
Quoted 1 line
How much memory are we talking about? What is the typical figure?
One typical scenario we would like to support is full-hd decoding. One frame is about 4MB (1920x1080x2 ~= 4MB). Depending on the codec, it may require up to 15 buffers what gives about 60MB. This simple calculation does not include memory for the framebuffer, temporary buffers for the hardware codec and buffers for the stream.
Quoted 8 lines
Quoted 1 line
Is there any reason why such solution won't work?
It could work indeed.
One similar solution that is already in place is to use highmem for that reclaimable DMA memory. It is easy to ensure affected highmem pages are not mapped in kernel space. And you can decide at boot time how many highmem pages you want even if the system has less that 1GB of RAM.
Hmmm, right, this might also help solving the problem.
Best regards
--
Marek Szyprowski
Samsung Poland R&D Center
Dear Mr. King,
AFAIK the CMA is the fourth attempt since 2008 taken to solve the multimedia memory allocation issue on some embedded devices. Most notably on ARM, that happens to be present in the SoCs we care about along the IOMMU-incapable multimedia IPs.
I understand that you have your guidelines taken from the ARM specification, but this approach is not helping us. The mainline kernel is server- and desktop-centric for various reasons I am not going to dwell into. We're trying hard to solve the physical memory fragmentation issue for some time now, only to hear "this is not acceptable, go somewhere else". So we did - the CMA is targeted towards mm, NOT the ARM. While I do not exactly know how you see your role in ARM kernel development, we have shown a few times that this issue is important for us, and we'd like to solve it. So if you could give a glimpse of what is acceptable, given the existing circumstances, we could possibly help developing that solution. Namely:
1. ARM-compatible SoC
2. Multimedia IP blocks requiring large amounts of contiguous memory
2. No IOMMU or SG in said blocks
4. Unused memory reserved for said multimedia drivers should be used by the kernel
5. Multimedia allocation scenarios must always be working (under some constraints of course), within sane time limit
6. The solution shall have minimal delta to upstream linux (none?)
While the obvious CMA uses are the ones you'd mostly like to avoid, we haven't tried to post anything like that along.
This way no obvious spec abuse is made, and we minimize the delta to the upstream - it's even better than current state, when you have dma coherent memory doing exactly what you claim is forbidden (unpredictable results could possibly happen).
As the feedback from the first CMA patches confirm, the issue we're trying to solve here is real. Yet no real solution exists to my knowledge. I understand the ARM holding my try to just wait till all the relevant chips do have an IOMMU, but here and now there is a SOC we are going to use. No IMOMMU, no SG. So would you please help us - or if for some reason you can't, just not make our work any harder?
BTW why is the lowmem unmap not feasible? Is it the section entries in the page tables scattered throughout the system? I was unable to find the answer so far.
Best regards
Signature · 2 lines
Tomasz Fujak Samsung Electronics Poland R&D
Quoted 30 lines · Russell King - ARM Linux
Quoted 3 lines · Marek Szyprowski
Actually this contiguous memory allocator is a better replacement for
alloc_pages()which is used bydma_alloc_coherent(). It is a generic framework that is not tied only to ARM architecture.... which is open to abuse. What I'm trying to find out is - if it can't be used for DMA, what is it to be used for?
Or are we inventing an everything-but-ARM framework?
Quoted 9 lines
Quoted 4 lines
In other words, do we _actually_ have a use for this which doesn't involve doing something like allocating 32MB of memory from it, remapping it so that it's DMA coherent, and then performing DMA on the resulting buffer?
This is an arm specific problem, also related to
dma_alloc_coherent()allocator. To be 100% conformant with ARM specification we would probably need to unmap all pages used by thedma_coherentallocator from the LOW MEM area. This is doable, but completely not related to the CMA and this patch series.You've already been told why we can't unmap pages from the kernel direct mapping.
Okay, so I'm just going to assume that CMA has _no_ _business_ being used on ARM, and is not something that should interest anyone in the ARM community.
_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
Quoted 9 lines · Tomasz Fujak
Dear Mr. King,
AFAIK the CMA is the fourth attempt since 2008 taken to solve the multimedia memory allocation issue on some embedded devices. Most notably on ARM, that happens to be present in the SoCs we care about along the IOMMU-incapable multimedia IPs.
I understand that you have your guidelines taken from the ARM specification, but this approach is not helping us.
I'm sorry you feel like that, but I'm living in reality. If we didn't have these architecture restrictions then we wouldn't have this problem in the first place.
What I'm trying to do here is to ensure that we remain _legal_ to the architecture specification - which for this issue means that we avoid corrupting people's data.
Maybe you like having a system which randomly corrupts people's data? I most certainly don't. But that's the way CMA is heading at the moment on ARM.
It is not up to me to solve these problems - that's for the proposer of the new API to do so. So, please, don't try to lump this problem on my shoulders. It's not my problem to sort out.
Quoted 8 lines · Russell King - ARM Linux
Quoted 7 lines · Tomasz Fujak
Dear Mr. King,
AFAIK the CMA is the fourth attempt since 2008 taken to solve the multimedia memory allocation issue on some embedded devices. Most notably on ARM, that happens to be present in the SoCs we care about along the IOMMU-incapable multimedia IPs.
Quoted 5 lines
Quoted 2 lines
I understand that you have your guidelines taken from the ARM specification, but this approach is not helping us.
I'm sorry you feel like that, but I'm living in reality. If we didn't have these architecture restrictions then we wouldn't have this problem in the first place.
Do we really have them, or just the documents say they exist?
Quoted 3 lines
What I'm trying to do here is to ensure that we remain _legal_ to the architecture specification - which for this issue means that we avoid corrupting people's data.
As legal as the mentioned dma_coherent?
Quoted 3 lines
Maybe you like having a system which randomly corrupts people's data? I most certainly don't. But that's the way CMA is heading at the moment on ARM.
Has this been experienced? I had some ARM-compatible boards on my desk (xscale, v6 and v7) and none of them crashed due to this behavior. And we *do* have multiple memory mappings, with different attributes.
Quoted 3 lines
It is not up to me to solve these problems - that's for the proposer of the new API to do so. So, please, don't try to lump this problem on my shoulders. It's not my problem to sort out.
Just great. Nothing short of spectacular - this way the IA32 is going to take the embedded market piece by piece once the big two advance their foundry processes.
Despite having the translator, so much burden in the legacy ISA and the fact that most of the embedded engineers from the high end are accustomed to the ARM.
In other words, should we take your response as yet another NAK? Or would you try harder and at least point us to some direction that would not doom the effort from the very beginning.
I understand that the role of an oracle is so much easier, but the time is running and devising subsequent solutions is not the use of engineers' time.
Best regards
---
Tomasz Fujak
Quoted 4 lines
To unsubscribe from this list: send the line "unsubscribe linux-media" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 9 lines · Tomasz Fujak
Quoted 8 lines · Russell King - ARM Linux
Quoted 7 lines · Tomasz Fujak
Dear Mr. King,
AFAIK the CMA is the fourth attempt since 2008 taken to solve the multimedia memory allocation issue on some embedded devices. Most notably on ARM, that happens to be present in the SoCs we care about along the IOMMU-incapable multimedia IPs.
Quoted 6 lines
Quoted 5 lines
Quoted 2 lines
I understand that you have your guidelines taken from the ARM specification, but this approach is not helping us.
I'm sorry you feel like that, but I'm living in reality. If we didn't have these architecture restrictions then we wouldn't have this problem in the first place.
Do we really have them, or just the documents say they exist?
Yes. We have seen CPUs which lockup or crash as a result of mismatched attributes in the page tables.
Quoted 4 lines
Quoted 3 lines
What I'm trying to do here is to ensure that we remain _legal_ to the architecture specification - which for this issue means that we avoid corrupting people's data.
As legal as the mentioned
dma_coherent?
See my other comment in an earlier email. See the patch which prevents ioremap() being used on system memory. There is active movement at the present time to sorting these violations out and find solutions for them.
The last thing we need is a new API which introduces new violations.
Quoted 6 lines
Quoted 3 lines
Maybe you like having a system which randomly corrupts people's data? I most certainly don't. But that's the way CMA is heading at the moment on ARM.
Has this been experienced? I had some ARM-compatible boards on my desk (xscale, v6 and v7) and none of them crashed due to this behavior. And we *do* have multiple memory mappings, with different attributes.
Xscale doesn't suffer from the problem. V6 doesn't aggressively speculate. V7 speculates more aggressively, and corruption has been seen there.
Quoted 6 lines
Quoted 3 lines
It is not up to me to solve these problems - that's for the proposer of the new API to do so. So, please, don't try to lump this problem on my shoulders. It's not my problem to sort out.
Just great. Nothing short of spectacular - this way the IA32 is going to take the embedded market piece by piece once the big two advance their foundry processes.
Look, I've been pointing out this problem ever since the very _first_ CMA patches were posted to the list, yet the CMA proponents have decided to brush those problems aside each and every time I've raised them.
So, you should be asking _why_ the CMA proponents are choosing to ignore this issue completely, rather than working to resolve it.
If it's resolved, then the problem goes away.
Quoted 3 lines
In other words, should we take your response as yet another NAK? Or would you try harder and at least point us to some direction that would not doom the effort from the very beginning.
What the fsck do you think I've been doing? This is NOT THE FIRST time I've raised this issue. I gave up raising it after the first couple of attempts because I wasn't being listened to.
You say about _me_ not being very helpful. How about the CMA proponents start taking the issue I've raised seriously, and try to work out how to solve it? And how about blaming them for the months of wasted time on this issue _because_ _they_ have chosen to ignore it?
Quoted 5 lines · Russell King - ARM Linux
Quoted 3 lines · Tomasz Fujak
In other words, should we take your response as yet another NAK? Or would you try harder and at least point us to some direction that would not doom the effort from the very beginning.
Quoted 8 lines
What the fsck do you think I've been doing? ?This is NOT THE FIRST time I've raised this issue. ?I gave up raising it after the first couple of attempts because I wasn't being listened to.
You say about _me_ not being very helpful. ?How about the CMA proponents start taking the issue I've raised seriously, and try to work out how to solve it? ?And how about blaming them for the months of wasted time on this issue _because_ _they_ have chosen to ignore it?
I've also raised the issue for ARM. However, I don't see what is the big problem.
A generic solution (that I think I already proposed) would be to reserve a chunk of memory for the CMA that can be removed from the normally mapped kernel memory through memblock at boot time. The size of this memory region would be configurable through kconfig. Then, the CMA would have a "dma" flag or something, and take chunks out of it until there's no more, and then return errors. That would work for ARM.
Cheers.
Signature · 1 line
Felipe Contreras
Quoted 15 lines
Quoted 4 lines · Tomasz Fujak
Quoted 3 lines
In other words, should we take your response as yet another NAK? Or would you try harder and at least point us to some direction that would not doom the effort from the very beginning.
Quoted 8 lines · Russell King - ARM Linux
What the fsck do you think I've been doing? ?This is NOT THE FIRST time I've raised this issue. ?I gave up raising it after the first couple of attempts because I wasn't being listened to.
You say about _me_ not being very helpful. ?How about the CMA proponents start taking the issue I've raised seriously, and try to work out how to solve it? ?And how about blaming them for the months of wasted time on this issue _because_ _they_ have chosen to ignore it?
Quoted 8 lines · Felipe Contreras
I've also raised the issue for ARM. However, I don't see what is the big problem.
A generic solution (that I think I already proposed) would be to reserve a chunk of memory for the CMA that can be removed from the normally mapped kernel memory through memblock at boot time. The size of this memory region would be configurable through kconfig. Then, the CMA would have a "dma" flag or something,
Having exactly that usage in mind, in v8 I've added notion of private CMA contexts which can be used for DMA coherent RAM as well as memory mapped devices.
Quoted 2 lines
and take chunks out of it until there's no more, and then return errors. That would work for ARM.
Signature · 4 lines
Best regards, _ _ .o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o ..o | Computer Science, Michal "mina86" Nazarewicz (o o) ooo +--<mina86-tlen.pl>--<jid:mina86-jabber.org>--ooO--(_)--Ooo--
Quoted 9 lines · Felipe Contreras
On Thu, Dec 23, 2010 at 4:16 PM, Russell King - ARM Linux
A generic solution (that I think I already proposed) would be to reserve a chunk of memory for the CMA that can be removed from the normally mapped kernel memory through memblock at boot time. The size of this memory region would be configurable through kconfig. Then, the CMA would have a "dma" flag or something, and take chunks out of it until there's no more, and then return errors. That would work for ARM.
That sounds an awful lot like the Android kernel's pmem implementation.
Solving this problem is important for us as well, but, I'm not sure I see a better solution that something like Felipe suggests.
The disadvantage, of course, being that the memory isn't available for the system when the user isn't doing the multi-media.
David
Signature · 2 lines
Sent by an employee of the Qualcomm Innovation Center, Inc. The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum.
v8 cover Contiguous Memory Allocator
Quoted 9 lines · David Brown
Quoted 7 lines · Felipe Contreras
A generic solution (that I think I already proposed) would be to reserve a chunk of memory for the CMA that can be removed from the normally mapped kernel memory through memblock at boot time. The size of this memory region would be configurable through kconfig. Then, the CMA would have a "dma" flag or something, and take chunks out of it until there's no more, and then return errors. That would work for ARM.
Quoted 5 lines
That sounds an awful lot like the Android kernel's pmem implementation.
Solving this problem is important for us as well, but, I'm not sure I see a better solution that something like Felipe suggests.
Alas, until the multiple mapping problem is solved, we're stuck. Right now, as brought to us by Russell, dma-able memory must be uncached. Can't see reasonable use of such a memory in system-wide manner yet, at least on A8.
A9 has PIPT caches in the data hierarchy, so some workaround could be not to use such memory for program text. But could it really?
Any ideas?
Quoted 3 lines
The disadvantage, of course, being that the memory isn't available for the system when the user isn't doing the multi-media.
The CMA is intended to do this: multimedia memory doubles as movable zone, but again - according to RMK the problem is real (we just haven't run into it yet though). And it's no fun when the device goes to a crawl due to swapping while a third of the physical memory lies unused.
Quoted 2 lines
David
Quoted 9 lines · Russell King - ARM Linux
Has anyone addressed my issue with it that this is wide-open for abuse by allocating large chunks of memory, and then remapping them in some way with different attributes, thereby violating the ARM architecture specification?
In other words, do we _actually_ have a use for this which doesn't involve doing something like allocating 32MB of memory from it, remapping it so that it's DMA coherent, and then performing DMA on the resulting buffer?
Huge pages.
Also, don't treat it as coherent memory and just flush/clear/invalidate cache before and after each DMA transaction. I never understood what's wrong with that approach.
Signature · 4 lines
Best regards, _ _ .o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o ..o | Computer Science, Michal "mina86" Nazarewicz (o o) ooo +--<mina86-tlen.pl>--<jid:mina86-jabber.org>--ooO--(_)--Ooo--
Quoted 11 lines · Michal Nazarewicz
Quoted 9 lines · Russell King - ARM Linux
Has anyone addressed my issue with it that this is wide-open for abuse by allocating large chunks of memory, and then remapping them in some way with different attributes, thereby violating the ARM architecture specification?
In other words, do we _actually_ have a use for this which doesn't involve doing something like allocating 32MB of memory from it, remapping it so that it's DMA coherent, and then performing DMA on the resulting buffer?
Quoted 5 lines
Huge pages.
Also, don't treat it as coherent memory and just flush/clear/invalidate cache before and after each DMA transaction. I never understood what's wrong with that approach.
If you've ever used an ARM system with a VIVT cache, you'll know what's wrong with this approach.
ARM systems with VIVT caches have extremely poor task switching performance because they flush the entire data cache at every task switch
- to the extent that it makes system performance drop dramatically when they become loaded.
Doing that for every DMA operation will kill the advantage we've gained from having VIPT caches and ASIDs stone dead.
Quoted 19 lines · Russell King - ARM Linux
Quoted 10 lines · Michal Nazarewicz
Quoted 9 lines · Russell King - ARM Linux
Has anyone addressed my issue with it that this is wide-open for abuse by allocating large chunks of memory, and then remapping them in some way with different attributes, thereby violating the ARM architecture specification?
In other words, do we _actually_ have a use for this which doesn't involve doing something like allocating 32MB of memory from it, remapping it so that it's DMA coherent, and then performing DMA on the resulting buffer?
Quoted 5 lines
Huge pages.
Also, don't treat it as coherent memory and just flush/clear/invalidate cache before and after each DMA transaction. I never understood what's wrong with that approach.
If you've ever used an ARM system with a VIVT cache, you'll know what's wrong with this approach.
Quoted 7 lines
ARM systems with VIVT caches have extremely poor task switching performance because they flush the entire data cache at every task switch
- to the extent that it makes system performance drop dramatically when they become loaded.Doing that for every DMA operation will kill the advantage we've gained from having VIPT caches and ASIDs stone dead.
This statement effectively means: don't map dma-able memory to the CPU unless it's uncached. Have I missed anything?
Quoted 5 lines
--
To unsubscribe from this list: send the line "unsubscribe linux-media" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 25 lines · Tomasz Fujak
Quoted 24 lines · Russell King - ARM Linux
Quoted 10 lines · Michal Nazarewicz
Quoted 9 lines · Russell King - ARM Linux
Has anyone addressed my issue with it that this is wide-open for abuse by allocating large chunks of memory, and then remapping them in some way with different attributes, thereby violating the ARM architecture specification?
In other words, do we _actually_ have a use for this which doesn't involve doing something like allocating 32MB of memory from it, remapping it so that it's DMA coherent, and then performing DMA on the resulting buffer?
Quoted 5 lines
Huge pages.
Also, don't treat it as coherent memory and just flush/clear/invalidate cache before and after each DMA transaction. I never understood what's wrong with that approach.
If you've ever used an ARM system with a VIVT cache, you'll know what's wrong with this approach.
ARM systems with VIVT caches have extremely poor task switching performance because they flush the entire data cache at every task switch
- to the extent that it makes system performance drop dramatically when they become loaded.
Quoted 4 lines
Quoted 2 lines
Doing that for every DMA operation will kill the advantage we've gained from having VIPT caches and ASIDs stone dead.
This statement effectively means: don't map dma-able memory to the CPU unless it's uncached. Have I missed anything?
I'll give you another solution to the problem - lobby ARM Ltd to have this restriction lifted from the architecture specification, which will probably result in the speculative prefetching also having to be removed.
That would be my preferred solution if I had the power to do so, but I have to live with what ARM Ltd (and their partners such as yourselves) decide should end up in the architecture specification.
Quoted 26 lines · Russell King - ARM Linux
Quoted 25 lines · Tomasz Fujak
Quoted 24 lines · Russell King - ARM Linux
Quoted 10 lines · Michal Nazarewicz
Quoted 9 lines · Russell King - ARM Linux
Has anyone addressed my issue with it that this is wide-open for abuse by allocating large chunks of memory, and then remapping them in some way with different attributes, thereby violating the ARM architecture specification?
In other words, do we _actually_ have a use for this which doesn't involve doing something like allocating 32MB of memory from it, remapping it so that it's DMA coherent, and then performing DMA on the resulting buffer?
Quoted 5 lines
Huge pages.
Also, don't treat it as coherent memory and just flush/clear/invalidate cache before and after each DMA transaction. I never understood what's wrong with that approach.
If you've ever used an ARM system with a VIVT cache, you'll know what's wrong with this approach.
ARM systems with VIVT caches have extremely poor task switching performance because they flush the entire data cache at every task switch
- to the extent that it makes system performance drop dramatically when they become loaded.
Quoted 9 lines
Quoted 4 lines
Quoted 2 lines
Doing that for every DMA operation will kill the advantage we've gained from having VIPT caches and ASIDs stone dead.
This statement effectively means: don't map dma-able memory to the CPU unless it's uncached. Have I missed anything?
I'll give you another solution to the problem - lobby ARM Ltd to have this restriction lifted from the architecture specification, which will probably result in the speculative prefetching also having to be removed.
Isn't disabling Speculative Accesses forwarding to the AXI bus the solution to our woes?
At least on the A8, which happens to be paired with non-IOMMU capable IPs on our SoCs.
On A9 the bit is gone (or has it moved?), but we have IOMMU here so the CMA isn't needed.
http://infocenter.arm.com/
Cortex-A8 Technical Reference Manual Revision: r3p2
3.2.26. c1, Auxiliary Control Register
CP15, c1, c0, bit 4: Enables speculative accesses on AXI
Quoted 8 lines
That would be my preferred solution if I had the power to do so, but I have to live with what ARM Ltd (and their partners such as yourselves) decide should end up in the architecture specification.
--
To unsubscribe from this list: send the line "unsubscribe linux-media" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 4 lines · Russell King - ARM Linux
I'll give you another solution to the problem - lobby ARM Ltd to have this restriction lifted from the architecture specification, which will probably result in the speculative prefetching also having to be removed.
I don't know if there was lobbying involved, but the way some barriers on x86 turned out to be unnecessary, on both Intel and AMD, after years of specs which abstractly implied they might be necessary.... I guess someone realised the relaxed specs weren't providing a benefit at the hardware level.
Perhaps it is possible to draw this to ARM's attention as a rather serious performance-damaging thing, so they might tighten the rules in favour of common sense, at least for the majority of devices?
Off the top of my head (and I really don't know much about the internals of an ARM), hardware that avoided speculation where there was no entry already in the TLB for the address... that would be workable, as page/range TLB flushes would be enough to protect pages from access.
With regard to specific chips (i.e. current ones, while lobbying to tighten the rules for future ones). Is there a control register on the chips which are known to have this annoying issue, to turn off the problematic cache behaviour (permanently while Linux runs), or some set of memory attributes that produces that effect? (Obviously there
is: Turn off all caching, but is there a weaker and equally effective one on current hardware with the problem?)
It might be there simply _isn't_ any solution that satisfies the generality of ARM spec, while satisfying the engineering requirements of particular media player chips on which the CMA+DMA may be perfectly safe. That would be unpleasant but hardly the first time some feature was not usable on some chips and essential on some others.
Take, for example, those (now old) ARMs that mishandle SWP so it can't be used. We still use SWP in kernels, and indeed userspace, which will break if run on those particular chips, but that's ok - it's an understood limitation.
Russell, I think the repeated attempts to propose the same thing, which you keep rejecting (rightly), isn't because they're not listening, but because you haven't got a better solution - other than scrap the hardware :-) Their code might actually be 100% reliable with the chips they use in those products, and it might be the _only_ solution which works on those, thus solving a real problem.
What's the right thing to do in that case? Maintain a fork out of tree, or some Kconfig animal that says you can't select this ARM subarch and that memory facility in the same kernel because they are technically incompatible - but at least everyone can see the code, and know which chip families to avoid for certain applications?
Here is a hint of an idea for a way forward:
- An API that everyone can use (in drivers etc.), that behaves the same for everyone, except that:
- On some chips (ARMv7...) some functions requires a large up-front memory reservation at boot time, (but that's ok because you probably have gobs of RAM with it anyway).
- On other chips (<= ARMv6?) it is safe to reduce the up-front reservation to less, maybe zero. (Better for smaller memories).
- Maybe it even makes sense for drivers using the API to request, @boot time, "_if_ you need early reservation, then _this_ is how much I will need maximum". The values can potentially dynamic anti-fragmantation allocators too. (I've done a bit of research on this - a sort of "semi-reservation" where you don't keep it free up front, but you limit how its used and grouped in a precise way, to make sure other uses are sufficiently reclaimable to satisfy the call when it comes.)
- Didn't reserve enough in advance for the architectural constraints - get NULL back. That's what allocators always do. That's what /proc/cmdline's options have a long history helping with - finding the setting which works on your kit. People are already used to a bit of fiddly tuning (and random crashes ;-) with these media application sorts of things.
Presumably the problem will ease off with IOMMUs and/or sensible SG (and/or sensible architectural constraints) becoming ubiquitous eventually.
-- Jamie
Quoted 4 lines · Russell King
Has anyone addressed my issue with it that this is wide-open for abuse by allocating large chunks of memory, and then remapping them in some way with different attributes, thereby violating the ARM architecture specification?
I seem to have missed the previous discussion about this issue. Where in the specification (preferably ARMv7) can I find information about this? Is the problem that it is simply forbidden to map an address multiple times with different cache setting and if this is done the hardware might start failing? Or is the problem that having an address mapped cached means that speculative pre-fetch can read it into the cache at any time, possibly causing problems if an un-cached mapping exists? In my opinion option number two can be handled and I've made an attempt at doing that in hwmem (posted on linux-mm a while ago), look in cache_handler.c. Hwmem currently does not use cma but the next version probably will.
/Johan Mossberg
Quoted 5 lines
Quoted 4 lines · Russell King
Has anyone addressed my issue with it that this is wide-open for abuse by allocating large chunks of memory, and then remapping them in some way with different attributes, thereby violating the ARM architecture specification?
2011/1/4 Johan MOSSBERG <[email protected]>:
Quoted 7 lines
Where in the specification (preferably ARMv7) can I find information about this? Is the problem that it is simply forbidden to map an address multiple times with different cache setting and if this is done the hardware might start failing? Or is the problem that having an address mapped cached means that speculative pre-fetch can read it into the cache at any time, possibly causing problems if an un-cached mapping exists?
IIRC both apply.
Quoted 9 lines · Johan MOSSBERG
Quoted 4 lines · Russell King
Has anyone addressed my issue with it that this is wide-open for abuse by allocating large chunks of memory, and then remapping them in some way with different attributes, thereby violating the ARM architecture specification?
I seem to have missed the previous discussion about this issue. Where in the specification (preferably ARMv7) can I find information about this?
Here's the extracts from the architecture reference manual:
* If the same memory locations are marked as having different cacheability attributes, for example by the use of aliases in a virtual to physical address mapping, behavior is UNPREDICTABLE.
A3.5.7 Memory access restrictions
Behavior is UNPREDICTABLE if the same memory location:
* is marked as Shareable Normal and Non-shareable Normal
* is marked as having different memory types (Normal, Device, or Strongly-ordered)
* is marked as having different cacheability attributes
* is marked as being Shareable Device and Non-shareable Device memory.
Such memory marking contradictions can occur, for example, by the use of aliases in a virtual to physical address mapping.
Glossary:
UNPREDICTABLE
Means the behavior cannot be relied upon. UNPREDICTABLE behavior must not represent security holes. UNPREDICTABLE behavior must not halt or hang the processor, or any parts of the system. UNPREDICTABLE behavior must not be documented or promoted as having a defined effect.
Quoted 10 lines
Is the problem that it is simply
forbidden to map an address multiple times with different cache setting and if this is done the hardware might start failing? Or is the problem that having an address mapped cached means that speculative pre-fetch can read it into the cache at any time, possibly causing problems if an un-cached mapping exists? In my opinion option number two can be handled and I've made an attempt at doing that in hwmem (posted on linux-mm a while ago), look incache_handler.c. Hwmem currently does not use cma but the next version probably will.
Given the extract from the architecture reference manual, do you want to run a system where you can't predict what the behaviour will be if you have two mappings present, one which is cacheable and one which is non-cacheable, and you're relying on the non-cacheable mapping to never return data from the cache?
What if during your testing, it appears to work correctly, but out in the field, someone's loaded a different application to your setup resulting in different memory access patterns, causing cache lines to appear in the non-cacheable mapping, and then the CPU hits them on subsequent accesses corrupting data...
You can't say that will never happen if you're relying on this unpredictable behaviour.
Quoted 83 lines
-----Original Message-----
From: linux-arm-kernel-bounces at lists.infradead.org [mailto:linux- arm-kernel-bounces at lists.infradead.org] On Behalf Of Russell King - ARM Linux
Sent: Tuesday, January 04, 2011 10:49 PM
To: Johan MOSSBERG
Cc: Daniel Walker; Kyungmin Park; Mel Gorman; KAMEZAWA Hiroyuki; Michal Nazarewicz; linux-kernel at vger.kernel.org; Michal Nazarewicz; linux-mm at kvack.org; Ankita Garg; Andrew Morton; Marek Szyprowski; linux-arm-kernel at lists.infradead.org; linux-media at vger.kernel.org
Subject: Re: [PATCHv8 00/12] Contiguous Memory AllocatorQuoted 4 lines · Johan MOSSBERG
Quoted 3 lines · Russell King
Has anyone addressed my issue with it that this is wide-open for abuse by allocating large chunks of memory, and then remapping them in some way with different attributes, thereby violating
the
Quoted 5 lines
Quoted 1 line
ARM architecture specification?
I seem to have missed the previous discussion about this issue. Where in the specification (preferably ARMv7) can I find information about this?
Here's the extracts from the architecture reference manual:
* If the same memory locations are marked as having different cacheability attributes, for example by the use of aliases in a virtual to physical address mapping, behavior is UNPREDICTABLE.
A3.5.7 Memory access restrictions
Behavior is UNPREDICTABLE if the same memory location:
* is marked as Shareable Normal and Non-shareable Normal
* is marked as having different memory types (Normal, Device, or Strongly-ordered)
* is marked as having different cacheability attributes
* is marked as being Shareable Device and Non-shareable Device memory.Such memory marking contradictions can occur, for example, by the use of
aliases in a virtual to physical address mapping.Glossary:
UNPREDICTABLE
Means the behavior cannot be relied upon. UNPREDICTABLE behavior must not
represent security holes. UNPREDICTABLE behavior must not halt or hang
the processor, or any parts of the system. UNPREDICTABLE behavior must not
be documented or promoted as having a defined effect.Quoted 10 lines
Is the problem that it is simply
forbidden to map an address multiple times with different cache setting and if this is done the hardware might start failing? Or is the problem that having an address mapped cached means that speculative pre-fetch can read it into the cache at any time, possibly causing problems if an un-cached mapping exists? In my opinion option number two can be handled and I've made an attempt at doing that in hwmem (posted on linux-mm a while ago), look incache_handler.c. Hwmem currently does not use cma but the next version probably will.Given the extract from the architecture reference manual, do you want
to run a system where you can't predict what the behaviour will be if
you have two mappings present, one which is cacheable and one which is
non-cacheable, and you're relying on the non-cacheable mapping to never
return data from the cache?What if during your testing, it appears to work correctly, but out in
the field, someone's loaded a different application to your setup resulting in different memory access patterns, causing cache lines to
appear in the non-cacheable mapping, and then the CPU hits them on subsequent accesses corrupting data...
Quoted 3 lines
You can't say that will never happen if you're relying on this unpredictable behaviour.
Just to add to Russell's point, we did land up in un-traceable CPU deadlocks while running the kernel which was violating some of the rules set by ARM ARM.
The usecase use to work ~98% of the time.
Regards,
Santosh