mm: security: ro protection for dynamic data
Subsystems (8)
v19 cover mm: security: ro protection for dynamic data
This patch-set introduces the possibility of protecting memory that has been allocated dynamically.
The memory is managed in pools: when a memory pool is turned into R/O, all the memory that is part of it, will become R/O.
A R/O pool can be destroyed, to recover its memory, but it cannot be turned back into R/W mode.
This is intentional. This feature is meant for data that doesn't need further modifications after initialization.
However the data might need to be released, for example as part of module unloading.
To do this, the memory must first be freed, then the pool can be destroyed.
An example is provided, in the form of self-testing.
Changes since v18:
[http://www.openwall.com/lists/kernel-hardening/2018/02/28/21]
* Code refactoring in pmalloc & genalloc:
- simplify the logic for handling pools before and after sysf init
- reduced section holding mutex on pmalloc list, when adding a pool
- reduced the steps in finding length of an existing allocation
- split various functions into smaller ones
* clarified in the comments the need for pfree()
* Various fixes to the documentation:
- remove kerneldoc duplicates
- added cross-reference lables
- miscellaneous typos
* improved error notifications: use WARNs with specific messages
* added missing tests for possible error conditions
Discussion topics that are unclear if they are closed and would need comment from those who initiated them, if my answers are accepted or not:
* @Kees Cook proposed to have first self testing for genalloc, to validate the following patch, adding tracing of allocations My answer was that such tests would also need patching, therefore they could not certify that the functionality is corect both before and after the genalloc bitmap modification.
* @Kees Cook proposed to turn the self testing into modules. My answer was that the functionality is intentionally tested very early in the boot phase, to prevent unexplainable errors, should the feature really fail.
* @Matthew Wilcox proposed to use a different mechanism for th genalloc
bitmap: 2 bitmaps, one for occupation and one for start. And possibly use an rbtree for the starts.
My answer was that this solution is less optimized, because it scatters the data of one allocation across multiple words/pages, plus is not a transaction anymore. And the particular distribution of sizes of allocation is likely to eat up much more memory than the bitmap.
Igor Stoppa (8):
genalloc: track beginning of allocations Add label to genalloc.rst for cross reference
genalloc: selftest
struct page: add field for vm_struct Protectable Memory
Pmalloc selftest
lkdtm: crash on overwriting protected pmalloc var Documentation for Pmalloc
Documentation/core-api/genalloc.rst | 2 + Documentation/core-api/index.rst | 1 + Documentation/core-api/pmalloc.rst | 111 ++++++ drivers/misc/lkdtm.h | 1 + drivers/misc/lkdtm_core.c | 3 + drivers/misc/lkdtm_perms.c | 28 ++ include/linux/genalloc.h | 116 +++--- include/linux/mm_types.h | 1 + include/linux/pmalloc.h | 163 ++++++++ include/linux/test_genalloc.h | 26 ++ include/linux/test_pmalloc.h | 24 ++ include/linux/vmalloc.h | 1 + init/main.c | 4 + lib/Kconfig | 15 + lib/Makefile | 1 + lib/genalloc.c | 765 ++++++++++++++++++++++++++---------- lib/test_genalloc.c | 410 +++++++++++++++++++ mm/Kconfig | 17 + mm/Makefile | 2 + mm/pmalloc.c | 643 ++++++++++++++++++++++++++++++ mm/test_pmalloc.c | 238 +++++++++++ mm/usercopy.c | 33 ++ mm/vmalloc.c | 2 + 23 files changed, 2352 insertions(+), 255 deletions(-) create mode 100644 Documentation/core-api/pmalloc.rst create mode 100644 include/linux/pmalloc.h create mode 100644 include/linux/test_genalloc.h create mode 100644 include/linux/test_pmalloc.h create mode 100644 lib/test_genalloc.c create mode 100644 mm/pmalloc.c create mode 100644 mm/test_pmalloc.c
Signature · 1 line
2.14.1
PATCH v19 1/8 genalloc: track beginning of allocations
The genalloc library is only capable of tracking if a certain unit of allocation is in use or not.
It is not capable of discerning where the memory associated to an allocation request begins and where it ends.
The reason is that units of allocations are tracked by using a bitmap, where each bit represents that the unit is either allocated (1) or available (0).
The user of the API must keep track of how much space was requested, if it ever needs to be freed.
This can cause errors being undetected.
Examples:
* Only a subset of the memory provided to an allocation request is freed
* The memory from a subsequent allocation is freed
* The memory being freed doesn't start at the beginning of an allocation.
The bitmap is used because it allows to perform lockless read/write access, where this is supported by hw through cmpxchg. Similarly, it is possible to scan the bitmap for a sufficiently long sequence of zeros, to identify zones available for allocation.
This patch doubles the space reserved in the bitmap for each allocation, to track their beginning.
For details, see the documentation inside lib/genalloc.c
The primary effect of this patch is that code using the gen_alloc library does not need anymore to keep track of the size of the allocations it makes.
Prior to this patch, it was necessary to keep track of the size of the allocation, so that it would be possible, later on, to know how much space should be freed.
Now, users of the api can choose to etiher still specify explicitly the size, or let the library determine it, by giving a value of 0.
However, even when the value is specified, the library still uses its on understanding of the space associated with a certain allocation, to confirm that they are consistent.
This verification also confirms that the patch works correctly.
Eventually, the extra parameter (and the corresponding verification) could be dropped, in favor of a simplified API.
--- include/linux/genalloc.h | 112 +++---- lib/genalloc.c | 742 ++++++++++++++++++++++++++++++++++------------- 2 files changed, 599 insertions(+), 255 deletions(-)
include/linux/genalloc.h +60 -52
@@ -32,7 +32,7 @@#include <linux/types.h> #include <linux/spinlock_types.h> -#include <linux/atomic.h> +#include <linux/slab.h> struct device ;struct device_node ;
@@ -76,7 +76,7 @@ struct gen_pool_chunk {phys_addr_t phys_addr;/* physical starting address of memory chunk */ unsigned long start_addr;/* start address of memory chunk */ unsigned long end_addr;/* end address of memory chunk (inclusive) */ -unsigned long bits[0 ];/* bitmap for allocating memory chunk */ +unsigned long entries[0 ];/* bitmap for allocating memory chunk */ };/*
@@ -93,74 +93,82 @@ struct genpool_data_fixed {unsigned long offset;/* The offset of the specific region */ }; -extern struct gen_pool *gen_pool_create (int ,int ); -extern phys_addr_t gen_pool_virt_to_phys (struct gen_pool *pool,unsigned long ); -extern int gen_pool_add_virt (struct gen_pool *,unsigned long ,phys_addr_t , -size_t ,int ); -/** -* gen_pool_add - add a new chunk of special memory to the pool -* @pool: pool to add new memory chunk to -* @addr: starting address of memory chunk to add to pool -* @size: size in bytes of the memory chunk to add to pool -* @nid: node id of the node the chunk structure and bitmap should be -* allocated on, or -1 -* -* Add a new chunk of special memory to the specified pool. -* -* Returns 0 on success or a -ve errno on failure. -*/ +struct gen_pool *gen_pool_create (int min_alloc_order,int nid); + +int gen_pool_add_virt (struct gen_pool *pool,unsigned long virt, +phys_addr_t phys,size_t size,int nid); + +static inline int gen_pool_add (struct gen_pool *pool,unsigned long addr,size_t size,int nid) {return gen_pool_add_virt (pool, addr,-1 , size, nid); } -extern void gen_pool_destroy (struct gen_pool *); -extern unsigned long gen_pool_alloc (struct gen_pool *,size_t ); -extern unsigned long gen_pool_alloc_algo (struct gen_pool *,size_t , -genpool_algo_t algo,void *data); -extern void *gen_pool_dma_alloc (struct gen_pool *pool,size_t size, -dma_addr_t *dma); -extern void gen_pool_free (struct gen_pool *,unsigned long ,size_t ); -extern void gen_pool_for_each_chunk (struct gen_pool *, -void (*)(struct gen_pool *,struct gen_pool_chunk *,void *),void *); -extern size_t gen_pool_avail (struct gen_pool *); -extern size_t gen_pool_size (struct gen_pool *); -extern void gen_pool_set_algo (struct gen_pool *pool,genpool_algo_t algo, -void *data); +phys_addr_t gen_pool_virt_to_phys (struct gen_pool *pool,unsigned long addr); -extern unsigned long gen_pool_first_fit (unsigned long *map,unsigned long size, -unsigned long start,unsigned int nr,void *data, -struct gen_pool *pool); +void gen_pool_destroy (struct gen_pool *pool); -extern unsigned long gen_pool_fixed_alloc (unsigned long *map, -unsigned long size,unsigned long start,unsigned int nr, -void *data,struct gen_pool *pool); +unsigned long gen_pool_alloc (struct gen_pool *pool,size_t size); -extern unsigned long gen_pool_first_fit_align (unsigned long *map, -unsigned long size,unsigned long start,unsigned int nr, -void *data,struct gen_pool *pool); +unsigned long gen_pool_alloc_algo (struct gen_pool *pool,size_t size, +genpool_algo_t algo,void *data); +void *gen_pool_dma_alloc (struct gen_pool *pool,size_t size,dma_addr_t *dma); -extern unsigned long gen_pool_first_fit_order_align (unsigned long *map, -unsigned long size,unsigned long start,unsigned int nr, -void *data,struct gen_pool *pool); -extern unsigned long gen_pool_best_fit (unsigned long *map,unsigned long size, -unsigned long start,unsigned int nr,void *data, -struct gen_pool *pool); +void gen_pool_free (struct gen_pool *pool,unsigned long addr,size_t size); -extern struct gen_pool *devm_gen_pool_create (struct device *dev, -int min_alloc_order,int nid,const char *name); -extern struct gen_pool *gen_pool_get (struct device *dev,const char *name); +void gen_pool_for_each_chunk (struct gen_pool *pool, +void (*func)(struct gen_pool *pool, +struct gen_pool_chunk *chunk, +void *data), +void *data);bool addr_in_gen_pool (struct gen_pool *pool,unsigned long start, -size_t size); +size_t size); + +size_t gen_pool_avail (struct gen_pool *pool); + +size_t gen_pool_size (struct gen_pool *pool); + +void gen_pool_set_algo (struct gen_pool *pool,genpool_algo_t algo,void *data); + +unsigned long gen_pool_first_fit (unsigned long *map,unsigned long size, +unsigned long start,unsigned int nr, +void *data,struct gen_pool *pool); + + +unsigned long gen_pool_first_fit_align (unsigned long *map, +unsigned long size, +unsigned long start, +unsigned int nr,void *data, +struct gen_pool *pool); + +unsigned long gen_pool_fixed_alloc (unsigned long *map,unsigned long size, +unsigned long start,unsigned int nr, +void *data,struct gen_pool *pool); + + +unsigned long gen_pool_first_fit_order_align (unsigned long *map, +unsigned long size, +unsigned long start, +unsigned int nr,void *data, +struct gen_pool *pool); + +unsigned long gen_pool_best_fit (unsigned long *map,unsigned long size, +unsigned long start,unsigned int nr, +void *data,struct gen_pool *pool); + +struct gen_pool *gen_pool_get (struct device *dev,const char *name); + +struct gen_pool *devm_gen_pool_create (struct device *dev,int min_alloc_order, +int nid,const char *name);#ifdef CONFIG_OF -extern struct gen_pool *of_gen_pool_get (struct device_node *np, -const char *propname,int index); +struct gen_pool *of_gen_pool_get (struct device_node *np, +const char *propname,int index);#else static inline struct gen_pool *of_gen_pool_get (struct device_node *np,const char *propname,int index)
lib/genalloc.c +539 -203
1,090 lines not on this page · raw message
Signature · 1 line
2.14.1
PATCH v19 2/8 Add label to genalloc.rst for cross reference
In reply to Igor StoppaPut a label at the beginning of the genalloc.rst, to allow other documents to cross-reference it.
--- Documentation/core-api/genalloc.rst | 2 ++ 1 file changed, 2 insertions(+)
Documentation/core-api/genalloc.rst +2 -0
@@ -1,3 +1,5 @@ +.. _genalloc: + The genalloc/genpool subsystem ==============================
Signature · 1 line
2.14.1
PATCH v19 3/8 genalloc: selftest
In reply to Igor StoppaIntroduce a set of macros for writing concise test cases for genalloc.
The test cases are meant to provide regression testing, when working on new functionality for genalloc.
Primarily they are meant to confirm that the various allocation strategy will continue to work as expected.
The execution of the self testing is controlled through a Kconfig option.
The testing takes place in the very early stages of main.c, to ensure that failures in genalloc are caught before they can cause unexplained erratic behavior in any of genalloc users.
Therefore, it would not be advisable to implement it as module.
--- include/linux/test_genalloc.h | 26 +++ init/main.c | 2 + lib/Kconfig | 15 ++ lib/Makefile | 1 + lib/test_genalloc.c | 410 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 454 insertions(+) create mode 100644 include/linux/test_genalloc.h create mode 100644 lib/test_genalloc.c
include/linux/test_genalloc.h new file +26 -0
@@ -0,0 +1,26 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* test_genalloc.h +* +* (C) Copyright 2017 Huawei Technologies Co. Ltd. +* Author: Igor Stoppa <[email protected]> +*/ + + +#ifndef __LINUX_TEST_GENALLOC_H +#define __LINUX_TEST_GENALLOC_H + + +#ifdef CONFIG_TEST_GENERIC_ALLOCATOR + +#include <linux/genalloc.h> + +void test_genalloc (void ); + +#else + +static inline void test_genalloc (void ){}; + +#endif + +#endif
init/main.c +2 -0
@@ -90,6 +90,7 @@#include <linux/cache.h> #include <linux/rodata_test.h> #include <linux/jump_label.h> +#include <linux/test_genalloc.h> #include <asm/io.h> #include <asm/bugs.h>
@@ -661,6 +662,7 @@ asmlinkage __visible void __init start_kernel(void)*/ mem_encrypt_init (); +test_genalloc ();#ifdef CONFIG_BLK_DEV_INITRD if (initrd_start && !initrd_below_start_ok &&page_to_pfn (virt_to_page ((void *)initrd_start)) < min_low_pfn) {
lib/Kconfig +15 -0
@@ -287,6 +287,21 @@ config DECOMPRESS_LZ4 config GENERIC_ALLOCATOR bool +config TEST_GENERIC_ALLOCATOR + bool "genalloc tester" + default n + select GENERIC_ALLOCATOR + help + Enable automated testing of the generic allocator. + The testing is primarily for the tracking of allocated space. + +config TEST_GENERIC_ALLOCATOR_VERBOSE + bool "make the genalloc tester more verbose" + default n + select TEST_GENERIC_ALLOCATOR + help + More information will be displayed during the self-testing. + # # reed solomon support is select'ed if needed #
lib/Makefile +1 -0
@@ -108,6 +108,7 @@ obj-$(CONFIG_LIBCRC32C) += libcrc32c.o obj-$(CONFIG_CRC8) += crc8.o obj-$(CONFIG_XXHASH) += xxhash.o obj-$(CONFIG_GENERIC_ALLOCATOR) += genalloc.o +obj-$(CONFIG_TEST_GENERIC_ALLOCATOR) += test_genalloc.o obj-$(CONFIG_842_COMPRESS) += 842/ obj-$(CONFIG_842_DECOMPRESS) += 842/
lib/test_genalloc.c new file +410 -0
410 lines not on this page · raw message
Signature · 1 line
2.14.1
PATCH v19 4/8 struct page: add field for vm_struct
In reply to Igor StoppaWhen a page is used for virtual memory, it is often necessary to obtain a handler to the corresponding vm_struct, which refers to the virtually continuous area generated when invoking vmalloc.
The struct page has a "mapping" field, which can be re-used, to store a pointer to the parent area.
This will avoid more expensive searches, later on.
--- include/linux/mm_types.h | 1 + mm/vmalloc.c | 2 ++ 2 files changed, 3 insertions(+)
include/linux/mm_types.h +1 -0
@@ -84,6 +84,7 @@ struct page {void *s_mem;/* slab first object */ atomic_t compound_mapcount;/* first tail page */ /* page_deferred_list().next -- second tail page */ +struct vm_struct *area; };/* Second double word */
mm/vmalloc.c +2 -0
@@ -1536,6 +1536,7 @@ static void __vunmap(const void *addr, int deallocate_pages)struct page *page = area->pages[i];BUG_ON (!page); + page->area =NULL ;__free_pages (page,0 ); }
@@ -1705,6 +1706,7 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, area->nr_pages = i; goto fail; } + page->area = area; area->pages[i] = page;if (gfpflags_allow_blocking (gfp_mask|highmem_mask))cond_resched ();
Signature · 1 line
2.14.1
PATCH v19 5/8 Protectable Memory
In reply to Igor StoppaThe MMU available in many systems running Linux can often provide R/O protection to the memory pages it handles.
However, the MMU-based protection works efficiently only when said pages contain exclusively data that will not need further modifications.
Statically allocated variables can be segregated into a dedicated section, but this does not sit very well with dynamically allocated ones.
Dynamic allocation does not provide, currently, any means for grouping variables in memory pages that would contain exclusively data suitable for conversion to read only access mode.
The allocator here provided (pmalloc - protectable memory allocator) introduces the concept of pools of protectable memory.
A module can request a pool and then refer any allocation request to the pool handler it has received.
Once all the chunks of memory associated to a specific pool are initialized, the pool can be protected.
After this point, the pool can only be destroyed (it is up to the module to avoid any further references to the memory from the pool, after the destruction is invoked).
The latter case is mainly meant for releasing memory, when a module is unloaded.
A module can have as many pools as needed, for example to support the protection of data that is initialized in sufficiently distinct phases.
Since pmalloc memory is obtained from vmalloc, an attacker that has gained access to the physical mapping, still has to identify where the target of the attack is actually located.
At the same time, being also based on genalloc, pmalloc does not generate as much trashing of the TLB as it would be caused by using directly only vmalloc.
--- include/linux/genalloc.h | 4 + include/linux/pmalloc.h | 163 ++++++++++++ include/linux/vmalloc.h | 1 + lib/genalloc.c | 23 ++ mm/Kconfig | 7 + mm/Makefile | 1 + mm/pmalloc.c | 643 +++++++++++++++++++++++++++++++++++++++++++++++ mm/usercopy.c | 33 +++ 8 files changed, 875 insertions(+) create mode 100644 include/linux/pmalloc.h create mode 100644 mm/pmalloc.c
include/linux/genalloc.h +4 -0
@@ -120,6 +120,10 @@ void *gen_pool_dma_alloc(struct gen_pool *pool, size_t size, dma_addr_t *dma);void gen_pool_free (struct gen_pool *pool,unsigned long addr,size_t size); +void gen_pool_flush_chunk (struct gen_pool *pool, +struct gen_pool_chunk *chunk); + + void gen_pool_for_each_chunk(struct gen_pool *pool, void (*func)(struct gen_pool *pool, struct gen_pool_chunk *chunk,
include/linux/pmalloc.h new file +163 -0
163 lines not on this page · raw message
include/linux/vmalloc.h +1 -0
@@ -20,6 +20,7 @@ struct notifier_block; /* in notifier.h */#define VM_UNINITIALIZED 0x00000020/* vm_struct is not fully initialized */ #define VM_NO_GUARD 0x00000040/* don't add guard page */ #define VM_KASAN 0x00000080/* has allocated kasan shadow memory */ +#define VM_PMALLOC 0x00000100/* pmalloc area - see docs */ /* bits [20..32] reserved for arch specific ioremap internals */ /*
lib/genalloc.c +23 -0
@@ -661,6 +661,29 @@ void gen_pool_free(struct gen_pool *pool, unsigned long addr, size_t size)EXPORT_SYMBOL (gen_pool_free); +/** +* gen_pool_flush_chunk() - drops all the allocations from a specific chunk +* @pool: the generic memory pool +* @chunk: The chunk to wipe clear. +* +* This is meant to be called only while destroying a pool. It's up to the +* caller to avoid races, but really, at this point the pool should have +* already been retired and it should have become unavailable for any other +* sort of operation. +*/ +void gen_pool_flush_chunk (struct gen_pool *pool, +struct gen_pool_chunk *chunk) +{ +size_t size; + + size = chunk->end_addr +1 - chunk->start_addr; +memset (chunk->entries,0 , +DIV_ROUND_UP (size >> pool->min_alloc_order *BITS_PER_ENTRY , +BITS_PER_BYTE )); +atomic_long_set (&chunk->avail, size); +} + +/** * gen_pool_for_each_chunk() - call func for every chunk of generic memory pool * @pool: the generic memory pool
mm/Kconfig +7 -0
@@ -760,3 +760,10 @@ config GUP_BENCHMARK performance of get_user_pages_fast(). See tools/testing/selftests/vm/gup_benchmark.c + +config PROTECTABLE_MEMORY + bool + depends on MMU + depends on ARCH_HAS_SET_MEMORY + select GENERIC_ALLOCATOR + default y
mm/Makefile +1 -0
@@ -65,6 +65,7 @@ obj-$(CONFIG_SPARSEMEM) += sparse.o obj-$(CONFIG_SPARSEMEM_VMEMMAP) += sparse-vmemmap.o obj-$(CONFIG_SLOB) += slob.o obj-$(CONFIG_MMU_NOTIFIER) += mmu_notifier.o +obj-$(CONFIG_PROTECTABLE_MEMORY) += pmalloc.o obj-$(CONFIG_KSM) += ksm.o obj-$(CONFIG_PAGE_POISONING) += page_poison.o obj-$(CONFIG_SLAB) += slab.o
mm/pmalloc.c new file +643 -0
643 lines not on this page · raw message
mm/usercopy.c +33 -0
@@ -240,6 +240,36 @@ static inline void check_heap_object(const void *ptr, unsigned long n, } } +#ifdef CONFIG_PROTECTABLE_MEMORY + +int is_pmalloc_object (const void *ptr,const unsigned long n); + +static void check_pmalloc_object (const void *ptr,unsigned long n, +bool to_user) +{ +int retv; + + retv =is_pmalloc_object (ptr, n); +if (unlikely (retv)) { +if (unlikely (!to_user)) +usercopy_abort ("pmalloc" , +"trying to write to pmalloc object" , + to_user, (const unsigned long )ptr, n); +if (retv <0 ) +usercopy_abort ("pmalloc" , +"invalid pmalloc object" , + to_user, (const unsigned long )ptr, n); + } +} + +#else + +static void check_pmalloc_object (const void *ptr,unsigned long n, +bool to_user) +{ +} +#endif +/* * Validates that the given object is: * - not bogus address
@@ -277,5 +307,8 @@ void __check_object_size(const void *ptr, unsigned long n, bool to_user)/* Check for object in kernel to avoid text exposure. */ check_kernel_text_object ((const unsigned long )ptr, n, to_user); + +/* Check if object is from a pmalloc chunk. */ +check_pmalloc_object (ptr, n, to_user); }EXPORT_SYMBOL (__check_object_size);
Signature · 1 line
2.14.1
PATCH v19 6/8 Pmalloc selftest
In reply to Igor StoppaAdd basic self-test functionality for pmalloc.
The testing is introduced as early as possible, right after the main dependency, genalloc, has passed successfully, so that it can help diagnosing failures in pmalloc users.
--- include/linux/test_pmalloc.h | 24 +++++ init/main.c | 2 + mm/Kconfig | 10 ++ mm/Makefile | 1 + mm/test_pmalloc.c | 238 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 275 insertions(+) create mode 100644 include/linux/test_pmalloc.h create mode 100644 mm/test_pmalloc.c
include/linux/test_pmalloc.h new file +24 -0
@@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* test_pmalloc.h +* +* (C) Copyright 2018 Huawei Technologies Co. Ltd. +* Author: Igor Stoppa <[email protected]> +*/ + + +#ifndef __LINUX_TEST_PMALLOC_H +#define __LINUX_TEST_PMALLOC_H + + +#ifdef CONFIG_TEST_PROTECTABLE_MEMORY + +void test_pmalloc (void ); + +#else + +static inline void test_pmalloc (void ){}; + +#endif + +#endif
init/main.c +2 -0
@@ -91,6 +91,7 @@#include <linux/rodata_test.h> #include <linux/jump_label.h> #include <linux/test_genalloc.h> +#include <linux/test_pmalloc.h> #include <asm/io.h> #include <asm/bugs.h>
@@ -663,6 +664,7 @@ asmlinkage __visible void __init start_kernel(void)mem_encrypt_init ();test_genalloc (); +test_pmalloc ();#ifdef CONFIG_BLK_DEV_INITRD if (initrd_start && !initrd_below_start_ok &&page_to_pfn (virt_to_page ((void *)initrd_start)) < min_low_pfn) {
mm/Kconfig +10 -0
@@ -767,3 +767,13 @@ config PROTECTABLE_MEMORY depends on ARCH_HAS_SET_MEMORY select GENERIC_ALLOCATOR default y + +config TEST_PROTECTABLE_MEMORY + bool "Run self test for pmalloc memory allocator" + depends on MMU + depends on ARCH_HAS_SET_MEMORY + select PROTECTABLE_MEMORY + default n + help + Tries to verify that pmalloc works correctly and that the memory + is effectively protected.
mm/Makefile +1 -0
@@ -66,6 +66,7 @@ obj-$(CONFIG_SPARSEMEM_VMEMMAP) += sparse-vmemmap.o obj-$(CONFIG_SLOB) += slob.o obj-$(CONFIG_MMU_NOTIFIER) += mmu_notifier.o obj-$(CONFIG_PROTECTABLE_MEMORY) += pmalloc.o +obj-$(CONFIG_TEST_PROTECTABLE_MEMORY) += test_pmalloc.o obj-$(CONFIG_KSM) += ksm.o obj-$(CONFIG_PAGE_POISONING) += page_poison.o obj-$(CONFIG_SLAB) += slab.o
mm/test_pmalloc.c new file +238 -0
238 lines not on this page · raw message
Signature · 1 line
2.14.1
PATCH v19 7/8 lkdtm: crash on overwriting protected pmalloc var
In reply to Igor StoppaVerify that pmalloc read-only protection is in place: trying to overwrite a protected variable will crash the kernel.
--- drivers/misc/lkdtm.h | 1 + drivers/misc/lkdtm_core.c | 3 +++ drivers/misc/lkdtm_perms.c | 28 ++++++++++++++++++++++++++++ 3 files changed, 32 insertions(+)
drivers/misc/lkdtm.h +1 -0
@@ -38,6 +38,7 @@ void lkdtm_READ_BUDDY_AFTER_FREE(void);void __init lkdtm_perms_init (void );void lkdtm_WRITE_RO (void );void lkdtm_WRITE_RO_AFTER_INIT (void ); +void lkdtm_WRITE_RO_PMALLOC (void );void lkdtm_WRITE_KERN (void );void lkdtm_EXEC_DATA (void );void lkdtm_EXEC_STACK (void );
drivers/misc/lkdtm_core.c +3 -0
@@ -155,6 +155,9 @@ static const struct crashtype crashtypes[] = {CRASHTYPE (ACCESS_USERSPACE ),CRASHTYPE (WRITE_RO ),CRASHTYPE (WRITE_RO_AFTER_INIT ), +#ifdef CONFIG_PROTECTABLE_MEMORY +CRASHTYPE (WRITE_RO_PMALLOC ), +#endifCRASHTYPE (WRITE_KERN ),CRASHTYPE (REFCOUNT_INC_OVERFLOW ), CRASHTYPE(REFCOUNT_ADD_OVERFLOW),
drivers/misc/lkdtm_perms.c +28 -0
@@ -9,6 +9,7 @@#include <linux/vmalloc.h> #include <linux/mman.h> #include <linux/uaccess.h> +#include <linux/pmalloc.h> #include <asm/cacheflush.h> /* Whether or not to fill the target memory area with do_nothing(). */
@@ -104,6 +105,33 @@ void lkdtm_WRITE_RO_AFTER_INIT(void) *ptr ^=0xabcd1234 ; } +#ifdef CONFIG_PROTECTABLE_MEMORY +void lkdtm_WRITE_RO_PMALLOC (void ) +{ +struct gen_pool *pool; +int *i; + + pool =pmalloc_create_pool ("pool" ,0 ); +if (unlikely (!pool)) { +pr_info ("Failed preparing pool for pmalloc test." ); +return ; + } + + i = (int *)pmalloc (pool,sizeof (int ),GFP_KERNEL ); +if (unlikely (!i)) { +pr_info ("Failed allocating memory for pmalloc test." ); +pmalloc_destroy_pool (pool); +return ; + } + + *i =INT_MAX ; +pmalloc_protect_pool (pool); + +pr_info ("attempting bad pmalloc write at %p\n" , i); + *i =0 ; +} +#endif +void lkdtm_WRITE_KERN (void ) {size_t size;
Signature · 1 line
2.14.1
PATCH v19 8/8 Documentation for Pmalloc
In reply to Igor StoppaDetailed documentation about the protectable memory allocator.
--- Documentation/core-api/index.rst | 1 + Documentation/core-api/pmalloc.rst | 111 +++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 Documentation/core-api/pmalloc.rst
Documentation/core-api/index.rst +1 -0
@@ -25,6 +25,7 @@ Core utilities genalloc errseq printk-formats + pmalloc Interfaces for kernel debugging ===============================
Documentation/core-api/pmalloc.rst new file +111 -0
111 lines not on this page · raw message
Signature · 1 line
2.14.1
Re: [PATCH 4/8] struct page: add field for vm_struct
In reply to Igor StoppaQuoted 10 lines · Igor Stoppa
When a page is used for virtual memory, it is often necessary to obtain a handler to the corresponding
vm_struct, which refers to the virtually continuous area generated when invoking vmalloc.The struct page has a "mapping" field, which can be re-used, to store a pointer to the parent area.
This will avoid more expensive searches, later on.
Signed-off-by Igor Stoppa <[email protected]>
Regardless of the fate of the rest of this patchset, this makes sense and we should have this.
On 13/03/18 23:45, Igor Stoppa wrote:
[...]
Some more thoughts about the open topics:
Quoted 8 lines
Discussion topics that are unclear if they are closed and would need comment from those who initiated them, if my answers are accepted or not:
* @Kees Cook proposed to have first self testing for genalloc, to validate the following patch, adding tracing of allocations My answer was that such tests would also need patching, therefore they could not certify that the functionality is corect both before and after the genalloc bitmap modification.
This is the only one where I still couldn't find a solution. If Matthew Wilcox's proposal about implementing the the genalloc bitmap would work, then this too could be done, but I think this alternate bitmap proposed has problems. More on it below.
Quoted 4 lines
* @Kees Cook proposed to turn the self testing into modules. My answer was that the functionality is intentionally tested very early in the boot phase, to prevent unexplainable errors, should the feature really fail.
This could be workable, if it's acceptable that the early testing is performed only when the module is compiled in.
I do not expect the module-based testing to bring much value, but it doesn't do harm. Is this acceptable?
Quoted 7 lines
* @Matthew Wilcox proposed to use a different mechanism for the genalloc
bitmap: 2 bitmaps, one for occupation and one for start. And possibly use an rbtree for the starts.
My answer was that this solution is less optimized, because it scatters the data of one allocation across multiple words/pages, plus is not a transaction anymore. And the particular distribution of sizes of allocation is likely to eat up much more memory than the bitmap.
I think I can describe a scenario where the split bitmaps would not work (based on my understanding of the proposal), but I would appreciate a review. Here it is:
* One allocation (let's call it allocation A) is already present in both bitmaps:
- its units of allocation are marked in the "space" bitmap
- its starting bit is marked in the "starts" bitmap
* Another allocation (let's call it allocation B) is undergoing:
- some of its units of allocation (starting from the beginning) are marked in the "space" bitmap
- the starting bit is *not* yet marked in the "starts" bitmap
* B occupies the space immediately after A
* While B is being written, A is freed
* Having to determine the length of A, the "space" bitmap will be searched, then the "starts" bitmap
The space initially allocated for B will be wrongly accounted for A, because there is no empty gap in-between and the beginning of B is not yet marked.
The implementation which interleaves "space" and "start" does not suffer from this sort of races, because the alteration of the interleaved bitmaps is atomic.
However, at the very least, some more explanation is needed in the documentation/code, because this scenario is not exactly obvious.
Does this justification for the use of interleaved bitmaps (iow the current implementation) make sense?
--
igor
Quoted 9 lines · Igor Stoppa
Quoted 4 lines
* @Kees Cook proposed to turn the self testing into modules. My answer was that the functionality is intentionally tested very early in the boot phase, to prevent unexplainable errors, should the feature really fail.
This could be workable, if it's acceptable that the early testing is performed only when the module is compiled in.
I do not expect the module-based testing to bring much value, but it doesn't do harm. Is this acceptable?
Something I've been doing recently is building tests in both userspace and kernel space. Here's an example:
http://git.infradead.org/users/willy/linux-dax.git/commitdiff/717f2aa1d4040f65966bb9dab64035962576b0f9
Essentially, tools/ contains a reasonably good set of functions which emulate kernel functions. So you write your test suite as a kernel module and then build it in userspace as well.
Quoted 11 lines
Quoted 7 lines
* @Matthew Wilcox proposed to use a different mechanism for the genalloc
bitmap: 2 bitmaps, one for occupation and one for start. And possibly use an rbtree for the starts.
My answer was that this solution is less optimized, because it scatters the data of one allocation across multiple words/pages, plus is not a transaction anymore. And the particular distribution of sizes of allocation is likely to eat up much more memory than the bitmap.I think I can describe a scenario where the split bitmaps would not work (based on my understanding of the proposal), but I would appreciate a review. Here it is:
You misread my proposal. I did not suggest storing the 'start', but the 'end'.
Quoted 18 lines
* One allocation (let's call it allocation A) is already present in both bitmaps:
- its units of allocation are marked in the "space" bitmap
- its starting bit is marked in the "starts" bitmap* Another allocation (let's call it allocation B) is undergoing:
- some of its units of allocation (starting from the beginning) are marked in the "space" bitmap
- the starting bit is *not* yet marked in the "starts" bitmap* B occupies the space immediately after A
* While B is being written, A is freed
* Having to determine the length of A, the "space" bitmap will be searched, then the "starts" bitmap
Quoted 7 lines
The space initially allocated for B will be wrongly accounted for A, because there is no empty gap in-between and the beginning of B is not yet marked.
The implementation which interleaves "space" and "start" does not suffer from this sort of races, because the alteration of the interleaved bitmaps is atomic.
This would be a bug in the allocator implementation. Obviously it has to maintain the integrity of its own data structures.
Quoted 2 lines
Does this justification for the use of interleaved bitmaps (iow the current implementation) make sense?
I think you're making a mistake by basing the pmalloc allocator on genalloc. The page_frag allocator seems like a much better place to start than genalloc. It has a significantly lower overhead and is much more suited to the kind of probably-identical-lifespan that the pmalloc API is going to persuade its users to have.
Re: [PATCH 5/8] Protectable Memory
In reply to Igor Stoppainclude/linux/pmalloc.h line 92
Quoted 5 lines · Igor Stoppa
include/linux/pmalloc.h new file +5
-0+static inline void *pmalloc_array (struct gen_pool *pool,size_t n, +size_t size,gfp_t flags) +{ +if (unlikely (!(pool && n && size))) +return NULL ;
Why not use the same formula as kvmalloc_array here? You've failed to protect against integer overflow, which is the whole point of pmalloc_array.
if (size != 0 && n > SIZE_MAX / size) return NULL;
include/linux/pmalloc.h line 136
Quoted 7 lines
include/linux/pmalloc.h new file +7
-0+static inline char *pstrdup (struct gen_pool *pool,const char *s,gfp_t gfp) +{ +size_t len; +char *buf; + +if (unlikely (pool ==NULL || s ==NULL )) +return NULL ;
No, delete these checks. They'll mask real bugs.
include/linux/pmalloc.h line 158
Quoted 4 lines
include/linux/pmalloc.h new file +4
-0+static inline void pfree (struct gen_pool *pool,const void *addr) +{ +gen_pool_free (pool, (unsigned long )addr,0 ); +}
It's poor form to use a different subsystem's type here. It ties you to genpool, so if somebody wants to replace it, you have to go through all the users and change them. If you use your own type, it's a much easier task.
struct pmalloc_pool {
struct gen_pool g;
}then:
static inline void pfree(struct pmalloc_pool *pool, const void *addr)
{
gen_pool_free(&pool->g, (unsigned long)addr, 0);
}Looking further down, you could (should) move the contents of pmalloc_data into pmalloc_pool; that's one fewer object to keep track of.
mm/pmalloc.c line 41
Quoted 10 lines
mm/pmalloc.c new file +10
-0+struct pmalloc_data { +struct gen_pool *pool;/* Link back to the associated pool. */ +bool protected;/* Status of the pool: RO or RW. */ +struct kobj_attribute attr_protected;/* Sysfs attribute. */ +struct kobj_attribute attr_avail;/* Sysfs attribute. */ +struct kobj_attribute attr_size;/* Sysfs attribute. */ +struct kobj_attribute attr_chunks;/* Sysfs attribute. */ +struct kobject *pool_kobject; +struct list_head node;/* list of pools */ +};
sysfs attributes aren't free, you know. I appreciate you want something to help debug / analyse, but having one file for the whole subsystem or at least one per pool would be a better idea.
Re: [PATCH 6/8] Pmalloc selftest
In reply to Igor StoppaQuoted 1 line · Igor Stoppa
Add basic self-test functionality for pmalloc.
Here're some additional tests for your test-suite:
for (i = 1; i; i *= 2) pzalloc(pool, i - 1, GFP_KERNEL);
Quoted 1 line · Matthew Wilcox
On Wed, Mar 14, 2018 at 01:21:54PM +0200, Igor Stoppa wrote:
[...]
Quoted 2 lines
You misread my proposal. I did not suggest storing the 'start', but the 'end'.
Ok, but doesn't that only change the race scenario?
Attempting to free one allocation, while it is in progress, so that all the "space" bits are written, but the "end bit" is not yet written. That will eat up also the following, complete allocation, if there is no locking in place.
[...]
Quoted 6 lines
Quoted 3 lines
The implementation which interleaves "space" and "start" does not suffer from this sort of races, because the alteration of the interleaved bitmaps is atomic.
This would be a bug in the allocator implementation. Obviously it has to maintain the integrity of its own data structures.
But I cannot imagine how to do it, with the split bitmaps, without a lock :-/
And genalloc is supposed to be lockless.
Quoted 5 lines
Quoted 2 lines
Does this justification for the use of interleaved bitmaps (iow the current implementation) make sense?
I think you're making a mistake by basing the pmalloc allocator on genalloc.
It was recommended to me because it was a close match to the allocator that I was writing from scratch and, when I looked at it, I could only agree that it was very close.
But I have no particular reason for preferring it, if something better is available. It was just never brought up before.
At least not that I noticed.
Quoted 4 lines
The
page_fragallocator seems like a much better place to
start than genalloc. It has a significantly lower overhead and is much more suited to the kind of probably-identical-lifespan that the pmalloc API is going to persuade its users to have.
Could you please provide me a pointer?
I did a quick search on 4.16-rc5 and found the definition of page_frag and sk_page_frag(). Is this what you are referring to?
--
igor
Re: [PATCH 5/8] Protectable Memory
In reply to Matthew WilcoxQuoted 10 lines · Matthew Wilcox
include/linux/pmalloc.h line 92
Quoted 5 lines · Igor Stoppa
include/linux/pmalloc.h new file +5
-0+static inline void *pmalloc_array (struct gen_pool *pool,size_t n, +size_t size,gfp_t flags) +{ +if (unlikely (!(pool && n && size))) +return NULL ;Why not use the same formula as
kvmalloc_arrayhere? You've failed to protect against integer overflow, which is the whole point ofpmalloc_array.
Quoted 2 lines
if (size != 0 && n > SIZE_MAX / size) return NULL;
oops :-(
Quoted 9 lines
include/linux/pmalloc.h line 136
Quoted 7 lines
include/linux/pmalloc.h new file +7
-0+static inline char *pstrdup (struct gen_pool *pool,const char *s,gfp_t gfp) +{ +size_t len; +char *buf; + +if (unlikely (pool ==NULL || s ==NULL )) +return NULL ;No, delete these checks. They'll mask real bugs.
I thought I got rid of all of them, but some have escaped me
Quoted 9 lines
include/linux/pmalloc.h line 158
Quoted 4 lines
include/linux/pmalloc.h new file +4
-0+static inline void pfree (struct gen_pool *pool,const void *addr) +{ +gen_pool_free (pool, (unsigned long )addr,0 ); +}It's poor form to use a different subsystem's type here. It ties you to genpool, so if somebody wants to replace it, you have to go through all the users and change them. If you use your own type, it's a much easier task.
I thought about it, but typedef came to my mind and knowing it's usually frowned upon, I restrained myself.
Quoted 3 lines
struct pmalloc_pool {
struct gen_pool g;
}I didn't think this could be acceptable either. But if it is, then ok.
Quoted 10 lines
then:
static inline void
pfree(struct pmalloc_pool *pool, const void *addr){ gen_pool_free(&pool->g, (unsigned long)addr, 0); }Looking further down, you could (should) move the contents of
pmalloc_dataintopmalloc_pool; that's one fewer object to keep track of.
Quoted 14 lines
mm/pmalloc.c line 41
Quoted 10 lines
mm/pmalloc.c new file +10
-0+struct pmalloc_data { +struct gen_pool *pool;/* Link back to the associated pool. */ +bool protected;/* Status of the pool: RO or RW. */ +struct kobj_attribute attr_protected;/* Sysfs attribute. */ +struct kobj_attribute attr_avail;/* Sysfs attribute. */ +struct kobj_attribute attr_size;/* Sysfs attribute. */ +struct kobj_attribute attr_chunks;/* Sysfs attribute. */ +struct kobject *pool_kobject; +struct list_head node;/* list of pools */ +};sysfs attributes aren't free, you know. I appreciate you want something to help debug / analyse, but having one file for the whole subsystem or at least one per pool would be a better idea.
Which means that it should not be normal sysfs, but rather debugfs, if I understand correctly, since in sysfs 1 value -> 1 file.
--
igor
Quoted 8 lines · Igor Stoppa
Quoted 4 lines
The
page_fragallocator seems like a much better place to
start than genalloc. It has a significantly lower overhead and is much more suited to the kind of probably-identical-lifespan that the pmalloc API is going to persuade its users to have.Could you please provide me a pointer?
I did a quick search on 4.16-rc5 and found the definition ofpage_fragandsk_page_frag(). Is this what you are referring to?
It's a blink-and-you'll-miss-it allocator buried deep in mm/page_alloc.c:
void *page_frag_alloc(struct page_frag_cache *nc,
unsigned int fragsz, gfp_t gfp_mask)
void page_frag_free(void *addr)
I don't necessarily think you should use it as-is, but the principle it uses seems like a better match to me than the rather complex genalloc. Just allocate some pages and track the offset within those pages that is the current allocation point. It's less than 100 lines of code!
Quoted 1 line · Matthew Wilcox
I don't necessarily think you should use it as-is,
I think I simply cannot use it as-is, because it seems to use linear memory, while I need virtual. This reason alone would require a rewrite of several parts.
Quoted 2 lines
but the principle it uses
seems like a better match to me than the rather complex genalloc.
It uses meta data in a different way than genalloc.
There is probably a tipping point where one implementation becomes more space-efficient than the other.
Probably page_frag does well with relatively large allocations, while genalloc seems to be better for small (few allocation units) allocations.
Also, in case of high variance in the size of the allocations, genalloc requires the allocation unit to be small enough to fit the smallest request (otherwise one must accept some slack), while page_frag doesn't care if the allocation is small or large.
page_frag otoh, seems to not support the reuse of space that was freed, since there is only
But could you please explain to what you are referring to, when you say that page_frag has "significantly lower overhead" ?
Is it because it doesn't try to reclaim space that was freed, until the whole page is empty?
I see different trade-offs, but I am probably either missing or underestimating the main reason why you think this is better.
And probably I am missing the capability of judging what is acceptable in certain cases.
Ex: if the pfree is called only on error paths, is it ok to not claim back the memory released, if it's less than one page?
To be clear: I do not want to hold to genalloc just because I have already implemented it. I can at least sketch a version with page_frag, but I would like to understand why its trade-offs are better :-)
Quoted 6 lines
Just allocate some pages and track the offset within those pages that
is the current allocation point.
It's less than 100 lines of code!
Strictly speaking it is true, but it all relies on other functions, which must be rewritten, because they use linear address, while this must work with virtual (vmalloc) addresses.
Also, I see that the code relies a lot on order of allocation. I think we had similar discussion wrt compound pages.
It seems to me wasteful, if I have a request of, say, 5 pages, and I end up allocating 8.
I do not recall anyone giving a justification like:
"yeah, it uses extra pages, but it's preferable, for reasons X, Y and Z, so it's a good trade-off"
Could it be that it's normal RAM is considered less precious than the special memory genalloc is written for, so normal RAM is not really proactively reused, while special memory is treated as a more valuable resource that should not be wasted?
--
igor
Quoted 7 lines · Igor Stoppa
Quoted 2 lines · Matthew Wilcox
but the principle it uses
seems like a better match to me than the rather complex genalloc.It uses meta data in a different way than genalloc.
There is probably a tipping point where one implementation becomes more space-efficient than the other.
Certainly there are always tradeoffs in writing a memory allocator.
Quoted 2 lines
Probably
page_fragdoes well with relatively large allocations, while genalloc seems to be better for small (few allocation units) allocations.
I don't understand why you would think that. If you allocate 4096 1-byte elements, page_frag will just use up a page. Doing the same thing with genalloc requires allocating two bits per byte (1kB of bitmap), plus other overheads.
Quoted 4 lines
Also, in case of high variance in the size of the allocations, genalloc requires the allocation unit to be small enough to fit the smallest request (otherwise one must accept some slack), while
page_fragdoesn't care if the allocation is small or large.
Right; internal versus external fragmentation. The bane of memory allocators ;-)
Quoted 2 lines
page_fragotoh, seems to not support the reuse of space that was freed, since there is only
To a certain extent it does. If you free everything on a page, and that page is still in the page_frag_cache, it will get reused.
Quoted 2 lines
But could you please explain to what you are referring to, when you say that
page_fraghas "significantly lower overhead" ?
Less CPU time taken per allocation, less metadata stored per object.
Quoted 2 lines
Ex: if the pfree is called only on error paths, is it ok to not claim back the memory released, if it's less than one page?
Yes, I think that's a great example.
Quoted 6 lines
To be clear: I do not want to hold to genalloc just because I have already implemented it. I can at least sketch a version with
page_frag, but I would like to understand why its trade-offs are better :-)Quoted 1 line
Just allocate some pages and track the offset within those pages that
Quoted 8 lines
Quoted 4 lines
is the current allocation point.
It's less than 100 lines of code!
Strictly speaking it is true, but it all relies on other functions, which must be rewritten, because they use linear address, while this must work with virtual (vmalloc) addresses.
No, that's basically the whole thing. I think an implementation of pmalloc which used a page_frag-style allocator would be larger than 100 lines, but I don't think it would have to be significantly larger than that.
Quoted 5 lines
Also, I see that the code relies a lot on order of allocation. I think we had similar discussion wrt compound pages.
It seems to me wasteful, if I have a request of, say, 5 pages, and I end up allocating 8.
Yes, but the other three pages are available for use by the pmalloc pool. Now, at pmalloc_protect() time, you might well want to release the unused pages by calling make_alloc_exact() and hand those three pages back to the page allocator.
Quoted 3 lines
I do not recall anyone giving a justification like:
"yeah, it uses extra pages, but it's preferable, for reasons X, Y and Z, so it's a good trade-off"
Sometimes it is, sometimes it isn't.
Quoted 4 lines
Could it be that it's normal RAM is considered less precious than the special memory genalloc is written for, so normal RAM is not really proactively reused, while special memory is treated as a more valuable resource that should not be wasted?
We're certainly at the point where normal RAM is pretty cheap. A 16GB DIMM is $200, so that's $12.50 per gigabyte. We have more of a problem with fragmentation than we do with squeezing every last byte out of the system.
Of course, Linux still runs on tiny systems, and we don't want to unnecessarily bloat the kernel. And cachelines are also a precious resource; the fewer we touch, the faster the system runs. The bitmap in genalloc can easily occupy several cachelines; the page_frag allocator touches a single cacheline for most allocations.
Re: [PATCH 5/8] Protectable Memory
In reply to Igor StoppaQuoted 15 lines
Quoted 15 lines
mm/pmalloc.c line 41
Quoted 12 lines
mm/pmalloc.c new file +10
-0+struct pmalloc_data { +struct gen_pool *pool;/* Link back to the associated pool. */ +bool protected;/* Status of the pool: RO or RW. */ +struct kobj_attribute attr_protected;/* Sysfs attribute. */ +struct kobj_attribute attr_avail;/* Sysfs attribute. */ +struct kobj_attribute attr_size;/* Sysfs attribute. */ +struct kobj_attribute attr_chunks;/* Sysfs attribute. */ +struct kobject *pool_kobject; +struct list_head node;/* list of pools */ +};sysfs attributes aren't free, you know. I appreciate you want something to help debug / analyse, but having one file for the whole subsystem or at least one per pool would be a better idea.
Quoted 2 lines
Which means that it should not be normal sysfs, but rather debugfs, if I understand correctly, since in sysfs 1 value -> 1 file.
Yes, that is a good idea, to use debugfs so you still have a means to debug/analyze but can be also turned off for normal system execution. Sorry I didn't think about that earlier to save a revision, that's one of my favorite things I like to use for diagnosis.
Jay
Re: [PATCH 4/8] struct page: add field for vm_struct
In reply to Matthew WilcoxQuoted 5 lines · Matthew Wilcox
Quoted 4 lines · Igor Stoppa
When a page is used for virtual memory, it is often necessary to obtain a handler to the corresponding
vm_struct, which refers to the virtually continuous area generated when invoking vmalloc.
Quoted 7 lines
Quoted 6 lines
The struct page has a "mapping" field, which can be re-used, to store a pointer to the parent area.
This will avoid more expensive searches, later on.
Signed-off-by Igor Stoppa <[email protected]>Reviewed-by Matthew Wilcox <[email protected]>
Igor, do you mind sticking these tags on the files that have spent some time reviewing a revision of your patchset (like the Reviewed-by: tags I provided last revision?)
Thanks,
Jay
Re: [PATCH 4/8] struct page: add field for vm_struct
Quoted 1 line · J Freyensee
On 3/13/18 3:00 PM, Matthew Wilcox wrote:
[...]
Quoted 6 lines
Quoted 2 lines
Quoted 1 line
Signed-off-by Igor Stoppa <[email protected]>Reviewed-by Matthew Wilcox <[email protected]>Igor, do you mind sticking these tags on the files that have spent some time reviewing a revision of your patchset (like the Reviewed-by: tags I provided last revision?)
Apologies, that was not intentional, I forgot it.
I will do it, although most of the files will now change so much that I am not sure what will survive, beside this patch, in the form that you reviewed.
I suppose the Review-by tag drops, if the patch changes.
--
igor
Quoted 1 line · Matthew Wilcox
On Wed, Mar 14, 2018 at 06:11:22PM +0200, Igor Stoppa wrote:
[...]
Quoted 7 lines
Quoted 2 lines
Probably
page_fragdoes well with relatively large allocations, while genalloc seems to be better for small (few allocation units) allocations.I don't understand why you would think that. If you allocate 4096 1-byte elements,
page_fragwill just use up a page. Doing the same thing with genalloc requires allocating two bits per byte (1kB of bitmap), plus other overheads.
I had misunderstood the amount of page_frag structures needed.
--
igor
Re: [PATCH 4/8] struct page: add field for vm_struct
In reply to Igor StoppaQuoted 9 lines · Igor Stoppa
Quoted 1 line · J Freyensee
On 3/13/18 3:00 PM, Matthew Wilcox wrote:
[...]
Quoted 5 lines
Quoted 2 lines
Quoted 1 line
Signed-off-by Igor Stoppa <[email protected]>Reviewed-by Matthew Wilcox <[email protected]>Igor, do you mind sticking these tags on the files that have spent some time reviewing a revision of your patchset (like the Reviewed-by: tags I provided last revision?)
Quoted 6 lines
Apologies, that was not intentional, I forgot it.
I will do it, although most of the files will now change so much that I am not sure what will survive, beside this patch, in the form that you reviewed.I suppose the Review-by tag drops, if the patch changes.
That's true, if so much of the patch changes it basically looks like something different, the Reviewed-by: would drop.
Jay
Quoted 3 lines
--
igor
Quoted 4 lines · Matthew Wilcox
I think an implementation of
pmalloc which used a page_frag-style allocator would be larger than 100 lines, but I don't think it would have to be significantly larger than that.
I have some doubt about what is the best way to implement it using vmalloced memory.
1. Since I can allocate an arbitrary number of pages, I think allocating a rounded up amount of memory, so that it's multiple of PAGE_SIZE should be enough.
But maybe I could do better than that:
a) support pre-allocation of x pages
b) define, as pool parameter, the minimum number of pages to allocate every time there is a refill
c) both a and b
----
2. the flavor of page_frag from page_alloc relies on page->_refcount, however neither vmap_area, nor vm_struct seem to have anything like that. (My reasoning is that I should do the accounting not on page level, but based on the virtual area that I get when I allocate new memory) What would be the best way to do refcounting for the area?
a) use the the page->_refcount from the first page that belongs to the area
b) add the _refcount to either vm_struct or vmap_area (I am not really sure of why these two structures exist as separate entities, rather than a single one - cache optimization?)
----
3. I will have to add a list of chunks (in genalloc lingo, or areas, if we refer to the new implementation), because I will still need to iterate over all the memory that belongs to a pool, for either write protecting it or for destroying the pool. I have two options:
a) handle the chunks within the pmalloc pool
b) create an intermediate type of pool (vfrag_pool?) and then include it in the pmalloc pool structure.
I'd lean toward option a, but I thought I might as well ask for advice before I implement the less desirable option (whatever it might be).
--
thanks, igor
Re: [PATCH 6/8] Pmalloc selftest
In reply to Matthew WilcoxQuoted 8 lines · Matthew Wilcox
Quoted 1 line · Igor Stoppa
Add basic self-test functionality for pmalloc.
Here're some additional tests for your test-suite:
for (i = 1; i; i *= 2) pzalloc(pool, i - 1, GFP_KERNEL);
Ok, I have almost finished the rewrite.
I still have to address this comment.
When I run the test, eventually the system runs out of memory, it keeps getting allocation errors from vmalloc, until i finally overflows and becomes 0.
Am I supposed to do something about it?
If pmalloc receives a request that the vmalloc backend cannot satisfy, I would prefer that vmalloc itself produces the warning and pmalloc returns NULL.
This doesn't look like a test case that one can leave always enabled in a build, but maybe I'm missing the point.
--
igor