mm: security: ro protection for dynamic data
Subsystems (7)
v14 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 v13:
[http://www.openwall.com/lists/kernel-hardening/2018/02/03/3]
- fixed warnings from "make htmldocs"
- added documentation to core-api index
Igor Stoppa (6):
genalloc: track beginning of allocations
genalloc: selftest
struct page: add field for vm_struct
Protectable Memory
Pmalloc: self-test
Documentation for Pmalloc
Documentation/core-api/index.rst | 1 + Documentation/core-api/pmalloc.rst | 114 ++++++++ include/linux/genalloc-selftest.h | 30 +++ include/linux/genalloc.h | 7 +- include/linux/mm_types.h | 1 + include/linux/pmalloc.h | 213 +++++++++++++++ include/linux/vmalloc.h | 1 + init/main.c | 2 + lib/Kconfig | 15 ++ lib/Makefile | 1 + lib/genalloc-selftest.c | 402 +++++++++++++++++++++++++++++ lib/genalloc.c | 444 ++++++++++++++++++++++---------- mm/Kconfig | 9 + mm/Makefile | 2 + mm/pmalloc-selftest.c | 61 +++++ mm/pmalloc-selftest.h | 26 ++ mm/pmalloc.c | 514 +++++++++++++++++++++++++++++++++++++ mm/usercopy.c | 25 +- mm/vmalloc.c | 18 +- 19 files changed, 1745 insertions(+), 141 deletions(-) create mode 100644 Documentation/core-api/pmalloc.rst create mode 100644 include/linux/genalloc-selftest.h create mode 100644 include/linux/pmalloc.h create mode 100644 lib/genalloc-selftest.c create mode 100644 mm/pmalloc-selftest.c create mode 100644 mm/pmalloc-selftest.h create mode 100644 mm/pmalloc.c
Signature · 1 line
2.16.0
PATCH v14 1/6 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.
Ex:
* 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. By using 2 bits per allocation, it is possible to encode also the information of where the allocation starts:
(msb to the left, lsb to the right, in the following "dictionary")
11: first allocation unit in the allocation
10: any subsequent allocation unit (if any) in the allocation 00: available allocation unit
01: invalid
Ex, with the same notation as above - MSb.......LSb:
...000010111100000010101011 <-- Read in this direction.
\__|\__|\|\____|\______|
| | | | \___ 4 used allocation units
| | | \___________ 3 empty allocation units
| | \_________________ 1 used allocation unit
| \___________________ 2 used allocation units
\_______________________ 2 empty allocation unitsBecause of the encoding, the previous lockless operations are still possible. The only caveat is to change the parameter of the zero-finding function which establishes the alignment at which to perform the test for first zero.
The original value of the parameter is 0, meaning that an allocation can start at any point in the bitmap, while the new value is 1, meaning that allocations can start only at even places (bit 0, bit 2, etc.) The number of zeroes to look for, must therefore be doubled.
When it's time to free the memory associated to an allocation request, it's a matter of checking if the corresponding allocation unit is really the beginning of an allocation (both bits are set to 1). Looking for the ending can also be performed locklessly. It's sufficient to identify the first mapped allocation unit that is represented either as free (00) or busy (11).
Even if the allocation status should change in the meanwhile, it doesn't matter, since it can only transition between free (00) and first-allocated (11).
The parameter indicating to the *_free() function the size of the space that should be freed is not currently removed, to facilitate the transition, but it is verified, whenever it is not zero.
If it is set to zero, then the free function will autonomously decide the size to be free, by scanning the bitmap.
About the implementation: the patch introduces the concept of "bitmap entry", which has a 1:1 mapping with allocation units, while the code being patched has a 1:1 mapping between allocation units and bits.
This means that, now, the bitmap can be extended (by following powers of 2), to track also other properties of the allocations, if ever needed.
--- include/linux/genalloc.h | 4 +- lib/genalloc.c | 417 ++++++++++++++++++++++++++++++++--------------- 2 files changed, 289 insertions(+), 132 deletions(-)
include/linux/genalloc.h +2 -2
@@ -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 */ };/*
lib/genalloc.c +287 -130
603 lines not on this page · raw message
Signature · 1 line
2.16.0
Randy Dunlap
reviewerRe: [PATCH 1/6] genalloc: track beginning of allocations
Quoted 72 lines · Igor Stoppa
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.
Ex:
* 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. By using 2 bits per allocation, it is possible to encode also the information of where the allocation starts:
(msb to the left, lsb to the right, in the following "dictionary")11: first allocation unit in the allocation
10: any subsequent allocation unit (if any) in the allocation 00: available allocation unit
01: invalidEx, with the same notation as above - MSb.......LSb:
...000010111100000010101011 <-- Read in this direction.
\__|\__|\|\____|\______| | | | | \___ 4 used allocation units | | | \___________ 3 empty allocation units | | \_________________ 1 used allocation unit | \___________________ 2 used allocation units \_______________________ 2 empty allocation unitsBecause of the encoding, the previous lockless operations are still possible. The only caveat is to change the parameter of the zero-finding function which establishes the alignment at which to perform the test for first zero.
The original value of the parameter is 0, meaning that an allocation can start at any point in the bitmap, while the new value is 1, meaning that allocations can start only at even places (bit 0, bit 2, etc.) The number of zeroes to look for, must therefore be doubled.When it's time to free the memory associated to an allocation request, it's a matter of checking if the corresponding allocation unit is really the beginning of an allocation (both bits are set to 1). Looking for the ending can also be performed locklessly. It's sufficient to identify the first mapped allocation unit that is represented either as free (00) or busy (11).
Even if the allocation status should change in the meanwhile, it doesn't matter, since it can only transition between free (00) and first-allocated (11).The parameter indicating to the
*_free()function the size of the space that should be freed is not currently removed, to facilitate the transition, but it is verified, whenever it is not zero.
If it is set to zero, then the free function will autonomously decide the size to be free, by scanning the bitmap.
Quoted 6 lines
About the implementation: the patch introduces the concept of "bitmap entry", which has a 1:1 mapping with allocation units, while the code being patched has a 1:1 mapping between allocation units and bits.
This means that, now, the bitmap can be extended (by following powers of 2), to track also other properties of the allocations, if ever needed.
It would be good for a lot of this to be in a source file or the pmalloc.rst documentation file instead of living only in the git repository.
Quoted 41 lines
Signed-off-by Igor Stoppa <[email protected]>--- include/linux/genalloc.h | 4 +- lib/genalloc.c | 417 ++++++++++++++++++++++++++++++++--------------- 2 files changed, 289 insertions(+), 132 deletions(-)lib/genalloc.c +25
-0@@ -36,114 +36,221 @@#include <linux/genalloc.h> #include <linux/of_device.h> +#define ENTRY_ORDER 1UL +#define ENTRY_MASK ((1UL << ((ENTRY_ORDER) + 1UL)) - 1UL) +#define ENTRY_HEAD ENTRY_MASK +#define ENTRY_UNUSED 0UL +#define BITS_PER_ENTRY (1U << ENTRY_ORDER) +#define BITS_DIV_ENTRIES (x) ((x) >> ENTRY_ORDER) +#define ENTRIES_TO_BITS (x) ((x) << ENTRY_ORDER) +#define BITS_DIV_LONGS (x) ((x) / BITS_PER_LONG) +#define ENTRIES_DIV_LONGS (x) (BITS_DIV_LONGS(ENTRIES_TO_BITS(x))) + +#define ENTRIES_PER_LONG BITS_DIV_ENTRIES(BITS_PER_LONG) + +/* Binary pattern of 1010...1010 that spans one unsigned long. */ +#define MASK (~0UL / 3 * 2) + +/** +* get_bitmap_entry - extracts the specified entry from the bitmap +* @map: pointer to a bitmap +* @entry_index: the index of the desired entry in the bitmap +* +* Returns the requested bitmap. +*/ +static inline unsigned long get_bitmap_entry (unsigned long *map, +int entry_index) +{
lib/genalloc.c line 73
Quoted 10 lines
lib/genalloc.c +10
-0+return (map[ENTRIES_DIV_LONGS (entry_index)] >> +ENTRIES_TO_BITS (entry_index %ENTRIES_PER_LONG )) & +ENTRY_MASK ; +} + + +/** +* mem_to_units - convert references to memory into orders of allocation +* @size: amount in bytes +* @order: pow of 2 represented by each entry in the bitmap
power
lib/genalloc.c line 84
Quoted 11 lines
lib/genalloc.c +11
-0+* +* Returns the number of units representing the size. +*/ +static inline unsigned long mem_to_units (unsigned long size, +unsigned long order) +{ +return (size + (1UL << order) -1 ) >> order; +} + +/** +* chunk_size - dimension of a chunk of memory
can this be more explicit about which dimension?
Quoted 37 lines
lib/genalloc.c +22
-4+* @chunk: pointer to the struct describing the chunk +* +* Returns the size of the chunk. +*/ static inline size_t chunk_size (const struct gen_pool_chunk *chunk) {return chunk->end_addr - chunk->start_addr +1 ; } -static int set_bits_ll (unsigned long *addr,unsigned long mask_to_set) + +/** +* set_bits_ll - according to the mask, sets the bits specified by +* value, at the address specified. +* @addr: where to write +* @mask: filter to apply for the bits to alter +* @value: actual configuration of bits to store +* +* Returns 0 upon success, -EBUSY otherwise +*/ +static int set_bits_ll (unsigned long *addr, +unsigned long mask,unsigned long value) { -unsigned long val, nval; +unsigned long nval; +unsigned long present; +unsigned long target; nval = *addr; do { - val = nval; - if (val & mask_to_set) + present = nval; + if (present & mask) return -EBUSY; + target = present | value; cpu_relax();
lib/genalloc.c line 124
Quoted 10 lines
lib/genalloc.c +4
-3- }while ((nval =cmpxchg (addr, val, val | mask_to_set)) != val); - + }while ((nval =cmpxchg (addr, present, target)) != target);return 0 ; } -static int clear_bits_ll (unsigned long *addr,unsigned long mask_to_clear) + +/** +* cleart_bits_ll - according to the mask, clears the bits specified by
clear_bits_ll
Quoted 85 lines
lib/genalloc.c +39
-32+* value, at the address specified. +* @addr: where to write +* @mask: filter to apply for the bits to alter +* @value: actual configuration of bits to clear +* +* Returns 0 upon success, -EBUSY otherwise +*/ +static int clear_bits_ll (unsigned long *addr, +unsigned long mask,unsigned long value) { -unsigned long val, nval; +unsigned long nval; +unsigned long present; +unsigned long target; nval = *addr; + present = nval; +if (unlikely ((present & mask) ^ value)) +return -EBUSY ;do { - val = nval; -if ((val & mask_to_clear) != mask_to_clear) + present = nval; +if (unlikely ((present & mask) ^ value))return -EBUSY ; + target = present & ~mask;cpu_relax (); - }while ((nval =cmpxchg (addr, val, val & ~mask_to_clear)) != val); - + }while ((nval =cmpxchg (addr, present, target)) != target);return 0 ; } -/* -* bitmap_set_ll - set the specified number of bits at the specified position + +/** +* get_boundary - verify that an allocation effectively +* starts at the given address, then measure its length. * @map: pointer to a bitmap -* @start: a bit position in @map -* @nr: number of bits to set +* @start_entry: the index of the first entry in the bitmap +* @nentries: number of entries to alter * -* Set @nr bits start from @start in @map lock-lessly. Several user> - * can set/clear the same bitmap simultaneously without lock. If two -* users set the same bit, one user will return remain bits, otherwise -* return 0. +* Returns the length of an allocation, otherwise -EINVAL if the +* parameters do not refer to a correct allocation. */ -static int bitmap_set_ll (unsigned long *map,int start,int nr) +static int get_boundary (unsigned long *map,int start_entry,int nentries) { -unsigned long *p = map +BIT_WORD (start); -const int size = start + nr; -int bits_to_set =BITS_PER_LONG - (start %BITS_PER_LONG ); -unsigned long mask_to_set =BITMAP_FIRST_WORD_MASK (start); - -while (nr - bits_to_set >=0 ) { -if (set_bits_ll (p, mask_to_set)) -return nr; - nr -= bits_to_set; - bits_to_set =BITS_PER_LONG ; - mask_to_set = ~0UL ; - p++; - } -if (nr) { - mask_to_set &=BITMAP_LAST_WORD_MASK (size); -if (set_bits_ll (p, mask_to_set)) -return nr; - } +int i; +unsigned long bitmap_entry; -return 0 ; + +if (unlikely (get_bitmap_entry (map, start_entry) !=ENTRY_HEAD )) +return -EINVAL ; +for (i = start_entry +1 ; i < nentries; i++) { + bitmap_entry =get_bitmap_entry (map, i); +if (bitmap_entry ==ENTRY_HEAD || + bitmap_entry ==ENTRY_UNUSED ) +return i; + }
lib/genalloc.c line 186
Quoted 10 lines
lib/genalloc.c +6
-1+return nentries - start_entry; } + +#define SET_BITS 1 +#define CLEAR_BITS 0 +/* -* bitmap_clear_ll - clear the specified number of bits at the specified position +* alter_bitmap_ll - set or clear the entries associated to an allocation
with an allocation
lib/genalloc.c line 187
Quoted 1 line
lib/genalloc.c +1
-0+* @alteration: selection if the bits selected should be set or cleared
indicates if
Quoted 42 lines
lib/genalloc.c +14
-24* @map: pointer to a bitmap -* @start: a bit position in @map -* @nr: number of bits to set +* @start: the index of the first entry in the bitmap +* @nentries: number of entries to alter * -* Clear @nr bits start from @start in @map lock-lessly. Several users -* can set/clear the same bitmap simultaneously without lock. If two -* users clear the same bit, one user will return remain bits, -* otherwise return 0. +* The modification happens lock-lessly. +* Several users can write to the same map simultaneously, without lock. +* If two users alter the same bit, one user will return remaining +* entries, otherwise return 0. */ -static int bitmap_clear_ll (unsigned long *map,int start,int nr) +static int alter_bitmap_ll (bool alteration,unsigned long *map, +int start_entry,int nentries) { -unsigned long *p = map +BIT_WORD (start); -const int size = start + nr; -int bits_to_clear =BITS_PER_LONG - (start %BITS_PER_LONG ); -unsigned long mask_to_clear =BITMAP_FIRST_WORD_MASK (start); - -while (nr - bits_to_clear >=0 ) { -if (clear_bits_ll (p, mask_to_clear)) -return nr; - nr -= bits_to_clear; - bits_to_clear =BITS_PER_LONG ; - mask_to_clear = ~0UL ; - p++; - } -if (nr) { - mask_to_clear &=BITMAP_LAST_WORD_MASK (size); -if (clear_bits_ll (p, mask_to_clear)) -return nr; +unsigned long start_bit; +unsigned long end_bit; +unsigned long mask; +unsigned long value; +int nbits; +int bits_to_write;
lib/genalloc.c line 215
Quoted 10 lines
lib/genalloc.c +10
-0+int index; +int (*action)(unsigned long *addr, +unsigned long mask,unsigned long value); + + action = (alteration ==SET_BITS ) ? set_bits_ll : clear_bits_ll; + +/* Prepare for writing the initial part of the allocation, from +* starting entry, to the end of the UL bitmap element which +* contains it. It might be larger than the actual allocation. +*/
Use kernel multi-line comment style.
lib/genalloc.c line 223
Quoted 8 lines
lib/genalloc.c +8
-0+ start_bit =ENTRIES_TO_BITS (start_entry); + end_bit =ENTRIES_TO_BITS (start_entry + nentries); + nbits =ENTRIES_TO_BITS (nentries); + bits_to_write =BITS_PER_LONG - start_bit %BITS_PER_LONG ; + mask =BITMAP_FIRST_WORD_MASK (start_bit); +/* Mark the beginning of the allocation. */ + value =MASK | (1UL << (start_bit %BITS_PER_LONG )); + index =BITS_DIV_LONGS (start_bit);
index = BITS_DIV_LONGS
(only 1 space after '=')
lib/genalloc.c line 229
Quoted 6 lines
lib/genalloc.c +6
-0+ +/* Writes entries to the bitmap, as long as the reminder is +* positive or zero. +* Might be skipped if the entries to write do not reach the end +* of a bitmap UL unit. +*/
Fix comment style, please.
Quoted 294 lines
lib/genalloc.c +103
-51+while (nbits >= bits_to_write) { +if (action (map + index, mask, value & mask)) +return BITS_DIV_ENTRIES (nbits); + nbits -= bits_to_write; + bits_to_write =BITS_PER_LONG ; + mask = ~0UL ; + value =MASK ; + index++; } +/* Takes care of the ending part of the entries to mark. */ +if (nbits >0 ) { + mask ^=BITMAP_FIRST_WORD_MASK ((end_bit) %BITS_PER_LONG ); + bits_to_write = nbits; +if (action (map + index, mask, value & mask)) +return BITS_DIV_ENTRIES (nbits); + }return 0 ; } +/** * gen_pool_create - create a new special memory pool -* @min_alloc_order: log base 2 of number of bytes each bitmap bit represents +* @min_alloc_order: log base 2 of number of bytes each bitmap entry represents * @nid: node id of the node the pool structure should be allocated on, or -1 * * Create a new special memory pool that can be used to manage special purpose @@ -183,10 +290,12 @@ int gen_pool_add_virt(struct gen_pool *pool, unsigned long virt, phys_addr_t phy size_t size, int nid) {struct gen_pool_chunk *chunk; -int nbits = size >> pool->min_alloc_order; -int nbytes =sizeof (struct gen_pool_chunk ) + -BITS_TO_LONGS (nbits) *sizeof (long ); +int nentries; +int nbytes; + nentries = size >> pool->min_alloc_order; + nbytes =sizeof (struct gen_pool_chunk ) + +ENTRIES_DIV_LONGS (nentries) *sizeof (long ); chunk =kzalloc_node (nbytes,GFP_KERNEL , nid);if (unlikely (chunk ==NULL ))return -ENOMEM ;@@ -248,7 +357,7 @@ void gen_pool_destroy(struct gen_pool *pool)list_del (&chunk->next_chunk); end_bit =chunk_size (chunk) >> order; - bit =find_next_bit (chunk->bits, end_bit,0 ); + bit =find_next_bit (chunk->entries, end_bit,0 );BUG_ON (bit < end_bit);kfree (chunk);@@ -292,7 +401,7 @@ unsigned long gen_pool_alloc_algo(struct gen_pool *pool, size_t size,struct gen_pool_chunk *chunk;unsigned long addr =0 ;int order = pool->min_alloc_order; -int nbits, start_bit, end_bit, remain; +int nentries, start_entry, end_entry, remain;#ifndef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG BUG_ON (in_nmi ());@@ -301,29 +410,32 @@ unsigned long gen_pool_alloc_algo(struct gen_pool *pool, size_t size,if (size ==0 )return 0 ; - nbits = (size + (1UL << order) -1 ) >> order; + nentries =mem_to_units (size, order);rcu_read_lock ();list_for_each_entry_rcu (chunk, &pool->chunks, next_chunk) {if (size >atomic_long_read (&chunk->avail))continue ; - start_bit =0 ; - end_bit =chunk_size (chunk) >> order; + start_entry =0 ; + end_entry =chunk_size (chunk) >> order; retry: - start_bit =algo (chunk->bits, end_bit, start_bit, - nbits, data, pool); -if (start_bit >= end_bit) + start_entry =algo (chunk->entries, end_entry, start_entry, + nentries, data, pool); +if (start_entry >= end_entry)continue ; - remain =bitmap_set_ll (chunk->bits, start_bit, nbits); + remain =alter_bitmap_ll (SET_BITS , chunk->entries, + start_entry, nentries);if (remain) { - remain =bitmap_clear_ll (chunk->bits, start_bit, - nbits - remain); -BUG_ON (remain); + remain =alter_bitmap_ll (CLEAR_BITS , + chunk->entries, + start_entry, + nentries - remain); goto retry; } - addr = chunk->start_addr + ((unsigned long )start_bit << order); - size = nbits << order; + addr = chunk->start_addr + + ((unsigned long )start_entry << order); + size = nentries << order;atomic_long_sub (size, &chunk->avail);break ; }@@ -365,7 +477,7 @@ EXPORT_SYMBOL(gen_pool_dma_alloc);* gen_pool_free - free allocated special memory back to the pool * @pool: pool to free to * @addr: starting address of memory to free back to pool -* @size: size in bytes of memory to free +* @size: size in bytes of memory to free or 0, for auto-detection * * Free previously allocated special memory back to the specified * pool. Can not be used in NMI handler on architectures without @@ -375,22 +487,29 @@ void gen_pool_free(struct gen_pool *pool, unsigned long addr, size_t size) {struct gen_pool_chunk *chunk;int order = pool->min_alloc_order; -int start_bit, nbits, remain; +int start_entry, remaining_entries, nentries, remain; +int boundary;#ifndef CONFIG_ARCH_HAVE_NMI_SAFE_CMPXCHG BUG_ON (in_nmi ());#endif - nbits = (size + (1UL << order) -1 ) >> order;rcu_read_lock ();list_for_each_entry_rcu (chunk, &pool->chunks, next_chunk) {if (addr >= chunk->start_addr && addr <= chunk->end_addr) {BUG_ON (addr + size -1 > chunk->end_addr); - start_bit = (addr - chunk->start_addr) >> order; - remain =bitmap_clear_ll (chunk->bits, start_bit, nbits); + start_entry = (addr - chunk->start_addr) >> order; + remaining_entries = (chunk->end_addr - addr) >> order; + boundary =get_boundary (chunk->entries, start_entry, + remaining_entries); +BUG_ON (boundary <0 ); + nentries = boundary - start_entry; +BUG_ON (size && + (nentries !=mem_to_units (size, order))); + remain =alter_bitmap_ll (CLEAR_BITS , chunk->entries, + start_entry, nentries);BUG_ON (remain); - size = nbits << order; -atomic_long_add (size, &chunk->avail); +atomic_long_add (nentries << order, &chunk->avail);rcu_read_unlock ();return ; }@@ -517,9 +636,9 @@ EXPORT_SYMBOL(gen_pool_set_algo);* gen_pool_first_fit - find the first available region * of memory matching the size requirement (no alignment constraint) * @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 +* @size: The number of allocation units in the bitmap +* @start: The allocation unit to start searching at +* @nr: The number of allocation units we're looking for * @data: additional data - unused * @pool: pool to find the fit region memory from */ @@ -527,7 +646,15 @@ unsigned long gen_pool_first_fit(unsigned long *map, unsigned long size, unsigned long start, unsigned int nr, void *data, struct gen_pool *pool) { - return bitmap_find_next_zero_area(map, size, start, nr, 0); + unsigned long align_mask; +unsigned long bit_index; + + align_mask =roundup_pow_of_two (BITS_PER_ENTRY ) -1 ; + bit_index =bitmap_find_next_zero_area (map,ENTRIES_TO_BITS (size), +ENTRIES_TO_BITS (start), +ENTRIES_TO_BITS (nr), + align_mask); +return BITS_DIV_ENTRIES (bit_index); }EXPORT_SYMBOL (gen_pool_first_fit);@@ -535,9 +662,9 @@ EXPORT_SYMBOL(gen_pool_first_fit);* gen_pool_first_fit_align - find the first available region * of memory matching the size requirement (alignment constraint) * @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 +* @size: The number of allocation units in the bitmap +* @start: The allocation unit to start searching at +* @nr: The number of allocation units we're looking for * @data: data for alignment * @pool: pool to get order from */ @@ -547,21 +674,28 @@ unsigned long gen_pool_first_fit_align(unsigned long *map, unsigned long size, {struct genpool_data_align *alignment;unsigned long align_mask; +unsigned long bit_index;int order; alignment = data; order = pool->min_alloc_order; - align_mask = ((alignment->align + (1UL << order) -1 ) >> order) -1 ; -return bitmap_find_next_zero_area (map, size, start, nr, align_mask); + align_mask =roundup_pow_of_two ( +ENTRIES_TO_BITS (mem_to_units (alignment->align, + order))) -1 ; + bit_index =bitmap_find_next_zero_area (map,ENTRIES_TO_BITS (size), +ENTRIES_TO_BITS (start), +ENTRIES_TO_BITS (nr), + align_mask); +return BITS_DIV_ENTRIES (bit_index); }EXPORT_SYMBOL (gen_pool_first_fit_align);/** * gen_pool_fixed_alloc - reserve a specific region * @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 +* @size: The number of allocation units in the bitmap +* @start: The allocation unit to start searching at +* @nr: The number of allocation units we're looking for * @data: data for alignment * @pool: pool to get order from */ @@ -571,20 +705,23 @@ unsigned long gen_pool_fixed_alloc(unsigned long *map, unsigned long size, {struct genpool_data_fixed *fixed_data;int order; -unsigned long offset_bit; -unsigned long start_bit; +unsigned long offset; +unsigned long align_mask; +unsigned long bit_index; fixed_data = data; order = pool->min_alloc_order; - offset_bit = fixed_data->offset >> order;if (WARN_ON (fixed_data->offset & ((1UL << order) -1 )))return size; + offset = fixed_data->offset >> order; + align_mask =roundup_pow_of_two (BITS_PER_ENTRY ) -1 ; + bit_index =bitmap_find_next_zero_area (map,ENTRIES_TO_BITS (size), +ENTRIES_TO_BITS (start + offset), +ENTRIES_TO_BITS (nr), align_mask); +if (bit_index !=ENTRIES_TO_BITS (offset)) +return size; - start_bit =bitmap_find_next_zero_area (map, size, - start + offset_bit, nr,0 ); -if (start_bit != offset_bit) - start_bit = size; -return start_bit; +return BITS_DIV_ENTRIES (bit_index); }EXPORT_SYMBOL (gen_pool_fixed_alloc);@@ -593,9 +730,9 @@ EXPORT_SYMBOL(gen_pool_fixed_alloc);* of memory matching the size requirement. The region will be aligned * to the order of the size specified. * @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 +* @size: The number of allocation units in the bitmap +* @start: The allocation unit to start searching at +* @nr: The number of allocation units we're looking for * @data: additional data - unused * @pool: pool to find the fit region memory from */ @@ -603,9 +740,15 @@ 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 align_mask = roundup_pow_of_two(nr) - 1; - -return bitmap_find_next_zero_area (map, size, start, nr, align_mask); + unsigned long align_mask; +unsigned long bit_index; + + align_mask =roundup_pow_of_two (ENTRIES_TO_BITS (nr)) -1 ; + bit_index =bitmap_find_next_zero_area (map,ENTRIES_TO_BITS (size), +ENTRIES_TO_BITS (start), +ENTRIES_TO_BITS (nr), + align_mask); +return BITS_DIV_ENTRIES (bit_index); }EXPORT_SYMBOL (gen_pool_first_fit_order_align);
lib/genalloc.c EXPORT_SYMBOL(gen_pool_first_fit_order_align); · line 757
Quoted 2 lines
lib/genalloc.c +0
-0@@ -613,9 +756,9 @@ EXPORT_SYMBOL(gen_pool_first_fit_order_align);* gen_pool_best_fit - find the best fitting region of memory * macthing the size requirement (no alignment constraint)
* matching
(if you don't mind fixing that also)
Quoted 10 lines
lib/genalloc.c +3
-3* @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 +* @size: The number of allocation units in the bitmap +* @start: The allocation unit to start searching at +* @nr: The number of allocation units we're looking for * @data: additional data - unused * @pool: pool to find the fit region memory from *
Signature · 1 line
~Randy
Re: [PATCH 1/6] genalloc: track beginning of allocations
Quoted 4 lines · Randy Dunlap
Quoted 2 lines
+/**
+ *cleart_bits_ll- according to the mask, clears the bits specified by
clear_bits_ll
'make W=1' should catch this ... yes?
(hint: building with 'make C=1 W=1' finds all kinds of interesting issues in your code. W=12 or W=123 finds too many false positives for my tastes)
Re: [PATCH 1/6] genalloc: track beginning of allocations
Quoted 10 lines · Matthew Wilcox
Quoted 4 lines · Randy Dunlap
Quoted 2 lines
+/**
+ *cleart_bits_ll- according to the mask, clears the bits specified by
clear_bits_ll'make W=1' should catch this ... yes?
(hint: building with 'make C=1 W=1' finds all kinds of interesting issues in your code. W=12 or W=123 finds too many false positives for my tastes)
ok, thank you, I will start using it
--
igor
Re: [PATCH 1/6] genalloc: track beginning of allocations
In reply to Randy DunlapQuoted 1 line · Randy Dunlap
On 02/04/2018 08:47 AM, Igor Stoppa wrote:
[...]
Quoted 2 lines
It would be good for a lot of this to be in a source file or the
pmalloc.rstdocumentation file instead of living only in the git repository.
This is actually about genalloc. The genalloc documentation is high level and mostly about the API, while this talks about the guts of the library. The part modified by the patch. This text doesn't seem to belong to the generic genalloc documentation.
I will move it to the .c file, but isn't it too much text in a source file?
[...]
Quoted 3 lines
Quoted 1 line
+ * @order: pow of 2 represented by each entry in the bitmap
power
ok
[...]
Quoted 3 lines
Quoted 1 line
+ *
chunk_size- dimension of a chunk of memorycan this be more explicit about which dimension?
I'll put "size in bytes of a chunk of memory"
[...]
Quoted 3 lines
Quoted 1 line
+ *
cleart_bits_ll- according to the mask, clears the bits specified by
clear_bits_ll
yes :-(
[...]
Quoted 4 lines
Quoted 2 lines
- *
bitmap_clear_ll- clear the specified number of bits at the specified position
+ *alter_bitmap_ll- set or clear the entries associated to an allocationwith an allocation
ok
Quoted 3 lines
Quoted 1 line
+ * @alteration: selection if the bits selected should be set or cleared
indicates if
ok
[...]
Quoted 6 lines
lib/genalloc.c line 215
Quoted 4 lines
lib/genalloc.c +4
-0+/* Prepare for writing the initial part of the allocation, from +* starting entry, to the end of the UL bitmap element which +* contains it. It might be larger than the actual allocation. +*/ Use kernel multi-line comment style.
ok, also for further occurrences
[...]
Quoted 4 lines
lib/genalloc.c line 223
Quoted 1 line
lib/genalloc.c +1
-0+ index =BITS_DIV_LONGS (start_bit);index =
BITS_DIV_LONGS
(only 1 space after '=')
oops, yes
--
thank you, igor
Randy Dunlap
reviewerRe: [PATCH 1/6] genalloc: track beginning of allocations
Quoted 7 lines · Igor Stoppa
Quoted 1 line · Randy Dunlap
On 02/04/2018 08:47 AM, Igor Stoppa wrote:
[...]
Quoted 8 lines
Quoted 2 lines
It would be good for a lot of this to be in a source file or the
pmalloc.rstdocumentation file instead of living only in the git repository.This is actually about genalloc. The genalloc documentation is high level and mostly about the API, while this talks about the guts of the library. The part modified by the patch. This text doesn't seem to belong to the generic genalloc documentation.
I will move it to the .c file, but isn't it too much text in a source file?
No, that will be fine.
thanks,
Signature · 1 line
~Randy
PATCH v14 2/6 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.
--- include/linux/genalloc-selftest.h | 30 +++ init/main.c | 2 + lib/Kconfig | 15 ++ lib/Makefile | 1 + lib/genalloc-selftest.c | 402 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 450 insertions(+) create mode 100644 include/linux/genalloc-selftest.h create mode 100644 lib/genalloc-selftest.c
include/linux/genalloc-selftest.h new file +30 -0
@@ -0,0 +1,30 @@ +/* +* genalloc-selftest.h +* +* (C) Copyright 2017 Huawei Technologies Co. Ltd. +* Author: Igor Stoppa <[email protected]> +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; version 2 +* of the License. +*/ + + +#ifndef __GENALLOC_SELFTEST_H__ +#define __GENALLOC_SELFTEST_H__ + + +#ifdef CONFIG_GENERIC_ALLOCATOR_SELFTEST + +#include <linux/genalloc.h> + +void genalloc_selftest (void ); + +#else + +static inline void genalloc_selftest (void ){}; + +#endif + +#endif
init/main.c +2 -0
@@ -89,6 +89,7 @@#include <linux/io.h> #include <linux/cache.h> #include <linux/rodata_test.h> +#include <linux/genalloc-selftest.h> #include <asm/io.h> #include <asm/bugs.h>
@@ -660,6 +661,7 @@ asmlinkage __visible void __init start_kernel(void)*/ mem_encrypt_init (); +genalloc_selftest ();#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 GENERIC_ALLOCATOR_SELFTEST + 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 GENERIC_ALLOCATOR_SELFTEST_VERBOSE + bool "make the genalloc tester more verbose" + default n + select GENERIC_ALLOCATOR_SELFTEST + 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_GENERIC_ALLOCATOR_SELFTEST) += genalloc-selftest.o obj-$(CONFIG_842_COMPRESS) += 842/ obj-$(CONFIG_842_DECOMPRESS) += 842/
lib/genalloc-selftest.c new file +402 -0
402 lines not on this page · raw message
Signature · 1 line
2.16.0
Randy Dunlap
reviewerRe: [PATCH 2/6] genalloc: selftest
Quoted 32 lines · Igor Stoppa
Introduce 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
Kconfigoption.Signed-off-by Igor Stoppa <[email protected]>--- include/linux/genalloc-selftest.h | 30 +++ init/main.c | 2 + lib/Kconfig | 15 ++ lib/Makefile | 1 + lib/genalloc-selftest.c | 402 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 450 insertions(+) create mode 100644 include/linux/genalloc-selftest.h create mode 100644 lib/genalloc-selftest.cinclude/linux/genalloc-selftest.h new file +5
-0@@ -0,0 +1,30 @@ +/* +* genalloc-selftest.h +* +* (C) Copyright 2017 Huawei Technologies Co. Ltd. +* Author: Igor Stoppa <[email protected]>
include/linux/genalloc-selftest.h line 15
Quoted 10 lines
include/linux/genalloc-selftest.h new file +10
-0+* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; version 2 +* of the License. +*/ + + +#ifndef __GENALLOC_SELFTEST_H__ +#define __GENALLOC_SELFTEST_H__
Please use _LINUX_GENALLOC_SELFTEST_H_
Quoted 119 lines
include/linux/genalloc-selftest.h new file +15
-0+ + +#ifdef CONFIG_GENERIC_ALLOCATOR_SELFTEST + +#include <linux/genalloc.h> + +void genalloc_selftest (void ); + +#else + +static inline void genalloc_selftest (void ){}; + +#endif + +#endif lib/genalloc-selftest.c new file +96
-0@@ -0,0 +1,402 @@ +/* +* genalloc-selftest.c +* +* (C) Copyright 2017 Huawei Technologies Co. Ltd. +* Author: Igor Stoppa <[email protected]> +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; version 2 +* of the License. +*/ + +#include <linux/module.h> +#include <linux/printk.h> +#include <linux/init.h> +#include <linux/vmalloc.h> +#include <asm/set_memory.h> +#include <linux/string.h> +#include <linux/debugfs.h> +#include <linux/atomic.h> +#include <linux/genalloc.h> + + + +/* Keep the bitmap small, while including case of cross-ulong mapping. +* For simplicity, the test cases use only 1 chunk of memory. +*/ +#define BITMAP_SIZE_C 16 +#define ALLOC_ORDER 0 + +#define ULONG_SIZE (sizeof(unsigned long)) +#define BITMAP_SIZE_UL (BITMAP_SIZE_C / ULONG_SIZE) +#define MIN_ALLOC_SIZE (1 << ALLOC_ORDER) +#define ENTRIES (BITMAP_SIZE_C * 8) +#define CHUNK_SIZE (MIN_ALLOC_SIZE * ENTRIES) + +#ifndef CONFIG_GENERIC_ALLOCATOR_SELFTEST_VERBOSE + +static inline void print_first_chunk_bitmap (struct gen_pool *pool) {} + +#else + +static void print_first_chunk_bitmap (struct gen_pool *pool) +{ +struct gen_pool_chunk *chunk; +char bitmap[BITMAP_SIZE_C *2 +1 ]; +unsigned long i; +char *bm = bitmap; +char *entry; + +if (unlikely (pool ==NULL || pool->chunks.next ==NULL )) +return ; + + chunk = container_of(pool->chunks.next, struct gen_pool_chunk, + next_chunk); + entry = (void *)chunk->entries; +for (i =1 ; i <=BITMAP_SIZE_C ; i++) + bm +=snprintf (bm,3 ,"%02hhx" , entry[BITMAP_SIZE_C - i]); + *bm ='\0' ; +pr_notice ("chunk: %p bitmap: 0x%s\n" , chunk, bitmap); + +} + +#endif + +enum test_commands { +CMD_ALLOCATOR , +CMD_ALLOCATE , +CMD_FLUSH , +CMD_FREE , +CMD_NUMBER , +CMD_END =CMD_NUMBER , +}; + +struct null_struct { +void *null; +}; + +struct test_allocator { +genpool_algo_t algo; +union { +struct genpool_data_align align; +struct genpool_data_fixed offset; +struct null_struct null; + } data; +}; + +struct test_action { +unsigned int location; +char pattern[BITMAP_SIZE_C ]; +unsigned int size; +}; + + +struct test_command { +enum test_commands command;
lib/genalloc-selftest.c line 106
Quoted 10 lines
lib/genalloc-selftest.c new file +10
-0+union { +struct test_allocator allocator; +struct test_action action; + }; +}; + + +/* To pass an array literal as parameter to a macro, it must go through +* this one, first. +*/
Please use kernel multi-line comment style.
Quoted 69 lines
lib/genalloc-selftest.c new file +69
-0+#define ARR (...) __VA_ARGS__ + +#define SET_DATA (parameter, value) \ + .parameter = { \ + .parameter = value, \ + } \ + +#define SET_ALLOCATOR (alloc, parameter, value) \ +{ \ + .command = CMD_ALLOCATOR, \ + .allocator = { \ + .algo = (alloc), \ + .data = { \ + SET_DATA(parameter, value), \ + }, \ + } \ +} + +#define ACTION_MEM (act, mem_size, mem_loc, match) \ +{ \ + .command = act, \ + .action = { \ + .size = (mem_size), \ + .location = (mem_loc), \ + .pattern = match, \ + }, \ +} + +#define ALLOCATE_MEM (mem_size, mem_loc, match) \ + ACTION_MEM(CMD_ALLOCATE, mem_size, mem_loc, ARR(match)) + +#define FREE_MEM (mem_size, mem_loc, match) \ + ACTION_MEM(CMD_FREE, mem_size, mem_loc, ARR(match)) + +#define FLUSH_MEM () \ +{ \ + .command = CMD_FLUSH, \ +} + +#define END () \ +{ \ + .command = CMD_END, \ +} + +static inline int compare_bitmaps (const struct gen_pool *pool, +const char *reference) +{ +struct gen_pool_chunk *chunk; +char *bitmap; +unsigned int i; + + chunk = container_of(pool->chunks.next, struct gen_pool_chunk, + next_chunk); + bitmap = (char *)chunk->entries; + +for (i =0 ; i <BITMAP_SIZE_C ; i++) +if (bitmap[i] != reference[i]) +return -1 ; +return 0 ; +} + +static void callback_set_allocator (struct gen_pool *pool, +const struct test_command *cmd, +unsigned long *locations) +{ +gen_pool_set_algo (pool, cmd->allocator.algo, + (void *)&cmd->allocator.data); +} +
lib/genalloc-selftest.c line 185
Quoted 10 lines
lib/genalloc-selftest.c new file +10
-0+static void callback_allocate (struct gen_pool *pool, +const struct test_command *cmd, +unsigned long *locations) +{ +const struct test_action *action = &cmd->action; + + locations[action->location] =gen_pool_alloc (pool, action->size); +BUG_ON (!locations[action->location]); +print_first_chunk_bitmap (pool); +BUG_ON (compare_bitmaps (pool, action->pattern));
BUG_ON() seems harsh to me, but some of the other self-tests also do that.
Quoted 2 lines
lib/genalloc-selftest.c new file +2
-0+} +
[snip]
lib/genalloc-selftest.c line 241
Quoted 8 lines
lib/genalloc-selftest.c new file +8
-0+ +/* To make the test work for both 32bit and 64bit ulong sizes, +* allocate (8 / 2 * 4 - 1) = 15 bytes bytes, then 16, then 2. +* The first allocation prepares for the crossing of the 32bit ulong +* threshold. The following crosses the 32bit threshold and prepares for +* crossing the 64bit thresholds. The last is large enough (2 bytes) to +* cross the 64bit threshold. +* Then free the allocations in the order: 2nd, 1st, 3rd.
Fix multi-line comment style.
Quoted 13 lines
+ */
lib/genalloc-selftest.c new file +12
-0+const struct test_command test_ulong_span[] = { + SET_ALLOCATOR(gen_pool_first_fit, null, NULL), + ALLOCATE_MEM(15, 0, ARR({0xab, 0xaa, 0xaa, 0x2a})), + ALLOCATE_MEM(16, 1, ARR({0xab, 0xaa, 0xaa, 0xea, + 0xaa, 0xaa, 0xaa, 0x2a})), + ALLOCATE_MEM(2, 2, ARR({0xab, 0xaa, 0xaa, 0xea, + 0xaa, 0xaa, 0xaa, 0xea, + 0x02})), + FREE_MEM(0, 1, ARR({0xab, 0xaa, 0xaa, 0x2a, + 0x00, 0x00, 0x00, 0xc0, + 0x02})), + FREE_MEM(0, 0, ARR({0x00, 0x00, 0x00, 0x00,
lib/genalloc-selftest.c line 264
Quoted 10 lines
lib/genalloc-selftest.c new file +10
-0+ 0x00, 0x00, 0x00, 0xc0, + 0x02})), + FREE_MEM(0, 2, ARR({0x00})), + END(), +}; + +/* Create progressively smaller allocations A B C D E. +* then free B and D. +* Then create new allocation that would fit in both of the gaps left by +* B and D. Verify that it uses the gap from B.
Ditto.
Quoted 2 lines
+ */
+const structtest_commandtest_first_fit_gaps[] = {
Signature · 1 line
~Randy
Re: [PATCH 2/6] genalloc: selftest
Quoted 4 lines · Randy Dunlap
include/linux/genalloc-selftest.h line 15
Quoted 2 lines
include/linux/genalloc-selftest.h new file +2
-0+#ifndef __GENALLOC_SELFTEST_H__ +#define __GENALLOC_SELFTEST_H__Please use _LINUX_GENALLOC_SELFTEST_H_
willy@bobo:~/kernel/linux$ git grep define.*_H__$ include/linux/*.h |wc -l 98 willy@bobo:~/kernel/linux$ git grep define.*_H_$ include/linux/*.h |wc -l 110 willy@bobo:~/kernel/linux$ git grep define.*_H$ include/linux/*.h |wc -l 885
No trailing underscore is 8x as common as one trailing underscore.
Randy Dunlap
reviewerRe: [PATCH 2/6] genalloc: selftest
Quoted 6 lines · Matthew Wilcox
Quoted 4 lines · Randy Dunlap
include/linux/genalloc-selftest.h line 15
Quoted 2 lines
include/linux/genalloc-selftest.h new file +2
-0+#ifndef __GENALLOC_SELFTEST_H__ +#define __GENALLOC_SELFTEST_H__Please use _LINUX_GENALLOC_SELFTEST_H_
Quoted 8 lines
willy@bobo:~/kernel/linux$ git grep define.*_H__$ include/linux/*.h |wc -l 98 willy@bobo:~/kernel/linux$ git grep define.*_H_$ include/linux/*.h |wc -l 110 willy@bobo:~/kernel/linux$ git grep define.*_H$ include/linux/*.h |wc -l 885No trailing underscore is 8x as common as one trailing underscore.
OK, ack.
thanks,
Signature · 1 line
~Randy
Re: [PATCH 2/6] genalloc: selftest
Quoted 14 lines · Randy Dunlap
Quoted 13 lines · Matthew Wilcox
Quoted 4 lines · Randy Dunlap
include/linux/genalloc-selftest.h line 15
Quoted 2 lines
include/linux/genalloc-selftest.h new file +2
-0+#ifndef __GENALLOC_SELFTEST_H__ +#define __GENALLOC_SELFTEST_H__Please use _LINUX_GENALLOC_SELFTEST_H_
willy@bobo:~/kernel/linux$ git grep define.*_H__$ include/linux/*.h |wc -l 98 willy@bobo:~/kernel/linux$ git grep define.*_H_$ include/linux/*.h |wc -l 110 willy@bobo:~/kernel/linux$ git grep define.*_H$ include/linux/*.h |wc -l 885
Quoted 3 lines
Quoted 1 line
No trailing underscore is 8x as common as one trailing underscore.
OK, ack.
ok, I'll move to _LINUX_xxx_yyy_H format
--
thanks, igor
Re: [PATCH 2/6] genalloc: selftest
In reply to Randy DunlapQuoted 1 line · Randy Dunlap
On 02/04/2018 08:47 AM, Igor Stoppa wrote:
[...]
Quoted 1 line
Please use kernel multi-line comment style.
ok for all of them
[...]
Quoted 5 lines
lib/genalloc-selftest.c line 185
Quoted 3 lines
lib/genalloc-selftest.c new file +3
-0+BUG_ON (!locations[action->location]); +print_first_chunk_bitmap (pool); +BUG_ON (compare_bitmaps (pool, action->pattern));
BUG_ON()seems harsh to me, but some of the other self-tests also do that.
I would expect that the test never fails, if one is not modifying anything related to genalloc.
But if an error slips in during development of genalloc or anything related (like the functions used to scan the bitmaps), I think it's better to pull the handbrake immediately, because failure in tracking correctly the memory allocation is likely to cause corruption and every sort of mysterious weird errors.
--
igor
kbuild test robot
build botRe: [PATCH 2/6] genalloc: selftest
In reply to Igor StoppaThank you for the patch! Yet something to improve:
Hi Igor,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on kees/for-next/pstore]
[also build test ERROR on v4.15 next-20180207]
[cannot apply to linus/master mmotm/master]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
url: https://github.com/0day-ci/linux/commits/Igor-Stoppa/mm-security-ro-protection-for-dynamic-data/20180207-171252 base: https://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git for-next/pstore config: xtensa-allyesconfig (attached as .config) compiler: xtensa-linux-gcc (GCC) 7.2.0
reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
make.cross ARCH=xtensa
All errors (new ones prefixed by >>):
>> lib/genalloc-selftest.c:17:10: fatal error: asm/set_memory.h: No such file or directory
#include <asm/set_memory.h>
^~~~~~~~~~~~~~~~~~compilation terminated.
vim +17 lib/genalloc-selftest.c
> 17 #include <asm/set_memory.h>
18 #include <linux/string.h>
19 #include <linux/debugfs.h>
20 #include <linux/atomic.h>
21 #include <linux/genalloc.h>
22
23
24 ---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
Re: [PATCH 2/6] genalloc: selftest
On 07/02/18 22:25, kbuild test robot wrote:
[...]
Quoted 2 lines
Quoted 1 line
Quoted 1 line
lib/genalloc-selftest.c:17:10: fatal error:asm/set_memory.h: No such file or directory#include <asm/set_memory.h>
This header is unnecessary and will be removed.
--
igor
PATCH v14 3/6 struct page: add field for vm_struct
In reply to Igor StoppaWhen a page is used for virtual memory, it is often necessary to obtian 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.
As example, the function find_vm_area is reimplemented, to take advantage of the newly introduced field.
--- include/linux/mm_types.h | 1 + mm/vmalloc.c | 18 +++++++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-)
include/linux/mm_types.h +1 -0
@@ -56,6 +56,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 +13 -5
@@ -1466,13 +1466,16 @@ struct vm_struct *get_vm_area_caller(unsigned long size, unsigned long flags,*/ struct vm_struct *find_vm_area (const void *addr) { -struct vmap_area *va; +struct page *page; - va =find_vmap_area ((unsigned long )addr); -if (va && va->flags &VM_VM_AREA ) -return va->vm; +if (unlikely (!is_vmalloc_addr (addr))) +return NULL ; -return NULL ; + page =vmalloc_to_page (addr); +if (unlikely (!page)) +return NULL ; + +return page->area; }/**
@@ -1536,6 +1539,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 ); }
@@ -1744,6 +1748,7 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align, const void *caller) {struct vm_struct *area; +unsigned int page_counter;void *addr;unsigned long real_size = size;
@@ -1769,6 +1774,9 @@ void *__vmalloc_node_range(unsigned long size, unsigned long align,kmemleak_vmalloc (area, size, gfp_mask); +for (page_counter =0 ; page_counter < area->nr_pages; page_counter++) + area->pages[page_counter]->area = area; +return addr; fail:
Signature · 1 line
2.16.0
PATCH v14 4/6 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.
--- include/linux/genalloc.h | 3 + include/linux/pmalloc.h | 213 ++++++++++++++++++++ include/linux/vmalloc.h | 1 + lib/genalloc.c | 27 +++ mm/Makefile | 1 + mm/pmalloc.c | 514 +++++++++++++++++++++++++++++++++++++++++++++++ mm/usercopy.c | 25 ++- 7 files changed, 780 insertions(+), 4 deletions(-) create mode 100644 include/linux/pmalloc.h create mode 100644 mm/pmalloc.c
include/linux/genalloc.h +3 -0
@@ -121,6 +121,9 @@ extern unsigned long gen_pool_alloc_algo(struct gen_pool *, size_t,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_flush_chunk (struct gen_pool *pool, +struct gen_pool_chunk *chunk);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 *);
include/linux/pmalloc.h new file +213 -0
213 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 +27 -0
@@ -519,6 +519,33 @@ 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 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; + +if (unlikely (!(pool && chunk))) +return ; + + 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/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_ARCH_HAS_SET_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 +514 -0
514 lines not on this page · raw message
mm/usercopy.c +21 -4
@@ -15,6 +15,7 @@#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt#include <linux/mm.h> +#include <linux/pmalloc.h> #include <linux/slab.h> #include <linux/sched.h> #include <linux/sched/task.h>
@@ -222,6 +223,7 @@ static inline const char *check_heap_object(const void *ptr, unsigned long n,void __check_object_size (const void *ptr,unsigned long n,bool to_user) {const char *err; +int retv;/* Skip all tests if size is zero. */ if (!n)
@@ -229,12 +231,12 @@ void __check_object_size(const void *ptr, unsigned long n, bool to_user)/* Check for invalid addresses. */ err =check_bogus_address (ptr, n); -if (err) +if (unlikely (err)) goto report;/* Check for bad heap object. */ err =check_heap_object (ptr, n, to_user); -if (err) +if (unlikely (err)) goto report;/* Check for bad stack object. */
@@ -257,8 +259,23 @@ void __check_object_size(const void *ptr, unsigned long n, bool to_user)/* Check for object in kernel to avoid text exposure. */ err =check_kernel_text_object (ptr, n); -if (!err) -return ; +if (unlikely (err)) + goto report; + +/* Check if object is from a pmalloc chunk. +*/ + retv =is_pmalloc_object (ptr, n); +if (unlikely (retv)) { +if (unlikely (!to_user)) { + err ="<trying to write to pmalloc object>" ; + goto report; + } +if (retv <0 ) { + err ="<invalid pmalloc object>" ; + goto report; + } + } +return ; report:report_usercopy (ptr, n, to_user, err);
Signature · 1 line
2.16.0
Randy Dunlap
reviewerRe: [PATCH 4/6] Protectable Memory
Quoted 66 lines · Igor Stoppa
The 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.
Signed-off-by Igor Stoppa <[email protected]>--- include/linux/genalloc.h | 3 + include/linux/pmalloc.h | 213 ++++++++++++++++++++ include/linux/vmalloc.h | 1 + lib/genalloc.c | 27 +++ mm/Makefile | 1 + mm/pmalloc.c | 514 +++++++++++++++++++++++++++++++++++++++++++++++ mm/usercopy.c | 25 ++- 7 files changed, 780 insertions(+), 4 deletions(-) create mode 100644 include/linux/pmalloc.h create mode 100644 mm/pmalloc.cinclude/linux/genalloc.h +3
-0@@ -121,6 +121,9 @@ extern unsigned long gen_pool_alloc_algo(struct gen_pool *, size_t,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_flush_chunk (struct gen_pool *pool, +struct gen_pool_chunk *chunk);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 *);include/linux/pmalloc.h new file +0
-0
include/linux/pmalloc.h line 10
Quoted 10 lines
include/linux/pmalloc.h new file +10
-0@@ -0,0 +1,213 @@ +/* SPDX-License-Identifier: GPL-2.0 +* +* pmalloc.h: Header for Protectable Memory Allocator +* +* (C) Copyright 2017 Huawei Technologies Co. Ltd. +* Author: Igor Stoppa <[email protected]> +*/ + +#ifndef _PMALLOC_H +#define _PMALLOC_H
use _LINUX_PMALLOC_H_
Quoted 23 lines
include/linux/pmalloc.h new file +23
-0+ + +#include <linux/genalloc.h> +#include <linux/string.h> + +#define PMALLOC_DEFAULT_ALLOC_ORDER (-1) + +/* +* Library for dynamic allocation of pools of memory that can be, +* after initialization, marked as read-only. +* +* This is intended to complement __read_only_after_init, for those cases +* where either it is not possible to know the initialization value before +* init is completed, or the amount of data is variable and can be +* determined only at run-time. +* +* ***WARNING*** +* The user of the API is expected to synchronize: +* 1) allocation, +* 2) writes to the allocated memory, +* 3) write protection of the pool, +* 4) freeing of the allocated memory, and +* 5) destruction of the pool.
include/linux/pmalloc.h line 43
Quoted 10 lines
include/linux/pmalloc.h new file +10
-0+* +* For a non-threaded scenario, this type of locking is not even required. +* +* Even if the library were to provide support for locking, point 2) +* would still depend on the user taking the lock. +*/ + + +/** +* pmalloc_create_pool - create a new protectable memory pool -
Drop trailing " -".
include/linux/pmalloc.h line 44
Quoted 1 line
include/linux/pmalloc.h new file +1
-0+* @name: the name of the pool, must be unique
Is that enforced? Will return NULL if @name is duplicated?
Quoted 7 lines
include/linux/pmalloc.h new file +7
-0+* @min_alloc_order: log2 of the minimum allocation size obtainable +* from the pool +* +* Creates a new (empty) memory pool for allocation of protectable +* memory. Memory will be allocated upon request (through pmalloc). +* +* Returns a pointer to the new pool upon success, otherwise a NULL.
include/linux/pmalloc.h line 61
Quoted 10 lines
include/linux/pmalloc.h new file +10
-0+*/ +struct gen_pool *pmalloc_create_pool (const char *name, +int min_alloc_order); + + +int is_pmalloc_object (const void *ptr,const unsigned long n); + +/** +* pmalloc_prealloc - tries to allocate a memory chunk of the requested size +* @pool: handler to the pool to be used for memory allocation
handle (I think)
include/linux/pmalloc.h line 66
Quoted 5 lines
include/linux/pmalloc.h new file +5
-0+* @size: amount of memory (in bytes) requested +* +* Prepares a chunk of the requested size. +* This is intended to both minimize latency in later memory requests and +* avoid sleping during allocation.
sleeping
include/linux/pmalloc.h line 67
Quoted 1 line
include/linux/pmalloc.h new file +1
-0+* Memory allocated with prealloc is stored in one single chunk, as
with pmalloc_prealloc()
include/linux/pmalloc.h line 68
Quoted 1 line
include/linux/pmalloc.h new file +1
-0+* opposite to what is allocated on-demand when pmalloc runs out of free
opposed to
include/linux/pmalloc.h line 71
Quoted 3 lines
include/linux/pmalloc.h new file +3
-0+* space already existing in the pool and has to invoke vmalloc. +* +* Returns true if the vmalloc call was successful, false otherwise.
Where is the allocated memory (pointer)? I.e., how does the caller know where that memory is?
Oh, that memory isn't yet available to the caller until it calls pmalloc(), right?
include/linux/pmalloc.h line 77
Quoted 6 lines
include/linux/pmalloc.h new file +6
-0+*/ +bool pmalloc_prealloc (struct gen_pool *pool,size_t size); + +/** +* pmalloc - allocate protectable memory from a pool +* @pool: handler to the pool to be used for memory allocation
handle (?)
include/linux/pmalloc.h line 84
Quoted 7 lines
include/linux/pmalloc.h new file +7
-0+* @size: amount of memory (in bytes) requested +* @gfp: flags for page allocation +* +* Allocates memory from an unprotected pool. If the pool doesn't have +* enough memory, and the request did not include GFP_ATOMIC, an attempt +* is made to add a new chunk of memory to the pool +* (a multiple of PAGE_SIZE), in order to fit the new request.
fill
What if @size is > PAGE_SIZE?
include/linux/pmalloc.h line 88
Quoted 4 lines
include/linux/pmalloc.h new file +4
-0+* Otherwise, NULL is returned. +* +* Returns the pointer to the memory requested upon success, +* NULL otherwise (either no memory available or pool already read-only).
It would be good to use the
* Return:
kernel-doc notation for return values.
include/linux/pmalloc.h line 95
Quoted 7 lines
include/linux/pmalloc.h new file +7
-0+*/ +void *pmalloc (struct gen_pool *pool,size_t size,gfp_t gfp); + + +/** +* pzalloc - zero-initialized version of pmalloc +* @pool: handler to the pool to be used for memory allocation
handle (?)
Quoted 7 lines
include/linux/pmalloc.h new file +7
-0+* @size: amount of memory (in bytes) requested +* @gfp: flags for page allocation +* +* Executes pmalloc, initializing the memory requested to 0, +* before returning the pointer to it. +* +* Returns the pointer to the zeroed memory requested, upon success,
include/linux/pmalloc.h line 112
Quoted 10 lines
include/linux/pmalloc.h new file +10
-0+* NULL otherwise (either no memory available or pool already read-only). +*/ +static inline void *pzalloc (struct gen_pool *pool,size_t size,gfp_t gfp) +{ +return pmalloc (pool, size, gfp | __GFP_ZERO); +} + +/** +* pmalloc_array - allocates an array according to the parameters +* @pool: handler to the pool to be used for memory allocation
handle
Quoted 9 lines
include/linux/pmalloc.h new file +9
-0+* @n: number of elements in the array +* @size: amount of memory (in bytes) requested for each element +* @flags: flags for page allocation +* +* Executes pmalloc, if it has a chance to succeed. +* +* Returns either NULL or the pmalloc result. +*/ +static inline void *pmalloc_array(struct gen_pool *pool, size_t n,
include/linux/pmalloc.h line 131
Quoted 10 lines
include/linux/pmalloc.h new file +10
-0+ size_t size, gfp_t flags) +{ +if (unlikely (!(pool && n && size))) +return NULL ; +return pmalloc (pool, n * size, flags); +} + +/** +* pcalloc - allocates a 0-initialized array according to the parameters +* @pool: handler to the pool to be used for memory allocation
handle
Quoted 7 lines
include/linux/pmalloc.h new file +7
-0+* @n: number of elements in the array +* @size: amount of memory (in bytes) requested +* @flags: flags for page allocation +* +* Executes pmalloc_array, if it has a chance to succeed. +* +* Returns either NULL or the pmalloc result.
include/linux/pmalloc.h line 148
Quoted 10 lines
include/linux/pmalloc.h new file +10
-0+*/ +static inline void *pcalloc (struct gen_pool *pool,size_t n, +size_t size,gfp_t flags) +{ +return pmalloc_array (pool, n, size, flags | __GFP_ZERO); +} + +/** +* pstrdup - duplicate a string, using pmalloc as allocator +* @pool: handler to the pool to be used for memory allocation
handle
Quoted 27 lines
include/linux/pmalloc.h new file +27
-0+* @s: string to duplicate +* @gfp: flags for page allocation +* +* Generates a copy of the given string, allocating sufficient memory +* from the given pmalloc pool. +* +* Returns a pointer to the replica, NULL in case of recoverable error. +*/ +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 ; + + len =strlen (s) +1 ; + buf =pmalloc (pool, len, gfp); +if (likely (buf)) +strncpy (buf, s, len); +return buf; +} + +/** +* pmalloc_protect_pool - turn a read/write pool read-only +* @pool: the pool to protect +*
include/linux/pmalloc.h line 185
Quoted 10 lines
include/linux/pmalloc.h new file +10
-0+* Write-protects all the memory chunks assigned to the pool. +* This prevents any further allocation. +* +* Returns 0 upon success, -EINVAL in abnormal cases. +*/ +int pmalloc_protect_pool (struct gen_pool *pool); + +/** +* pfree - mark as unused memory that was previously in use +* @pool: handler to the pool to be used for memory allocation
handle
include/linux/pmalloc.h line 191
Quoted 6 lines
include/linux/pmalloc.h new file +6
-0+* @addr: the beginning of the memory area to be freed +* +* The behavior of pfree is different, depending on the state of the +* protection. +* If the pool is not yet protected, the memory is marked as unused and +* will be availabel for further allocations.
available
Quoted 46 lines
include/linux/pmalloc.h new file +22
-0+* If the pool is already protected, the memory is marked as unused, but +* it will still be impossible to perform further allocation, because of +* the existing protection. +* The freed memory, in this case, will be truly released only when the +* pool is destroyed. +*/ +static inline void pfree (struct gen_pool *pool,const void *addr) +{ +gen_pool_free (pool, (unsigned long )addr,0 ); +} + +/** +* pmalloc_destroy_pool - destroys a pool and all the associated memory +* @pool: the pool to destroy +* +* All the memory that was allocated through pmalloc in the pool will be freed. +* +* Returns 0 upon success, -EINVAL in abnormal cases. +*/ +int pmalloc_destroy_pool (struct gen_pool *pool); + +#endif mm/pmalloc.c new file +16
-0@@ -0,0 +1,514 @@ +/* SPDX-License-Identifier: GPL-2.0 +* +* pmalloc.c: Protectable Memory Allocator +* +* (C) Copyright 2017 Huawei Technologies Co. Ltd. +* Author: Igor Stoppa <[email protected]> +*/ + +#include <linux/printk.h> +#include <linux/init.h> +#include <linux/mm.h> +#include <linux/vmalloc.h> +#include <linux/genalloc.h> +#include <linux/kernel.h> +#include <linux/log2.h> +#include <linux/slab.h>
mm/pmalloc.c line 26
Quoted 10 lines
mm/pmalloc.c new file +10
-0+#include <linux/device.h> +#include <linux/atomic.h> +#include <linux/rculist.h> +#include <linux/set_memory.h> +#include <asm/cacheflush.h> +#include <asm/page.h> + +#include "pmalloc-selftest.h" + +/**
/** means that the following comments are kernel-doc notation, but these comments are not, so just use /* there, please.
Quoted 21 lines
mm/pmalloc.c new file +21
-0+* pmalloc_data contains the data specific to a pmalloc pool, +* in a format compatible with the design of gen_alloc. +* Some of the fields are used for exposing the corresponding parameter +* to userspace, through sysfs. +*/ +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 */ +}; + +static LIST_HEAD (pmalloc_final_list); +static LIST_HEAD (pmalloc_tmp_list); +static struct list_head *pmalloc_list = &pmalloc_tmp_list; +static DEFINE_MUTEX (pmalloc_mutex); +static struct kobject *pmalloc_kobject;
[snip]
Quoted 1 line
+/**
Just use /* since this is not kernel-doc notation.
Quoted 15 lines
+ * Exposes the pool and its attributes through sysfs.
+ */mm/pmalloc.c new file +13
-0+static struct kobject *pmalloc_connect (struct pmalloc_data *data) +{ +const struct attribute *attrs[] = { + &data->attr_protected.attr, + &data->attr_avail.attr, + &data->attr_size.attr, + &data->attr_chunks.attr, +NULL + }; +struct kobject *kobj; + + kobj =kobject_create_and_add (data->pool->name, pmalloc_kobject); +if (unlikely (!kobj))
mm/pmalloc.c line 129
Quoted 10 lines
mm/pmalloc.c new file +10
-0+return NULL ; + +if (unlikely (sysfs_create_files (kobj, attrs) <0 )) { +kobject_put (kobj); + kobj =NULL ; + } +return kobj; +} + +/**
Ditto.
Quoted 8 lines
+ * Removes the pool and its attributes from sysfs.
+ */mm/pmalloc.c new file +6
-0+static void pmalloc_disconnect (struct pmalloc_data *data, +struct kobject *kobj) +{ +const struct attribute *attrs[] = { + &data->attr_protected.attr, + &data->attr_avail.attr,
mm/pmalloc.c line 147
Quoted 10 lines
mm/pmalloc.c new file +10
-0+ &data->attr_size.attr, + &data->attr_chunks.attr, + NULL + }; + +sysfs_remove_files (kobj, attrs); +kobject_put (kobj); +} + +/**
Same.
Quoted 10 lines
+ * Declares an attribute of the pool.
+ */mm/pmalloc.c new file +8
-0+ +#define pmalloc_attr_init (data, attr_name) \ +do { \ + sysfs_attr_init(&data->attr_##attr_name.attr); \ + data->attr_##attr_name.attr.name = #attr_name; \ + data->attr_##attr_name.attr.mode = VERIFY_OCTAL_PERMISSIONS(0400); \ + data->attr_##attr_name.show = pmalloc_pool_show_##attr_name; \ +} while (0)
[snip]
mm/pmalloc.c line 286
Quoted 13 lines
mm/pmalloc.c new file +13
-0+int is_pmalloc_object (const void *ptr,const unsigned long n) +{ +struct vm_struct *area; +struct page *page; +unsigned long area_start; +unsigned long area_end; +unsigned long object_start; +unsigned long object_end; + + +/* is_pmalloc_object gets called pretty late, so chances are high +* that the object is indeed of vmalloc type +*/
Multi-line comment style is
/* * comment1 * comment..N */
Quoted 103 lines
mm/pmalloc.c new file +103
-0+if (unlikely (!is_vmalloc_addr (ptr))) +return NOT_PMALLOC_OBJECT ; + + page =vmalloc_to_page (ptr); +if (unlikely (!page)) +return NOT_PMALLOC_OBJECT ; + + area = page->area; + +if (likely (!(area->flags &VM_PMALLOC ))) +return NOT_PMALLOC_OBJECT ; + + area_start = (unsigned long )area->addr; + area_end = area_start + area->nr_pages *PAGE_SIZE -1 ; + object_start = (unsigned long )ptr; + object_end = object_start + n -1 ; + +if (likely ((area_start <= object_start) && + (object_end <= area_end))) +return VALID_PMALLOC_OBJECT ; +else +return INVALID_PMALLOC_OBJECT ; +} + + +bool pmalloc_prealloc (struct gen_pool *pool,size_t size) +{ +void *chunk; +size_t chunk_size; +bool add_error; +unsigned int order; + +if (check_alloc_params (pool, size)) +return false; + + order = (unsigned int )pool->min_alloc_order; + +/* Expand pool */ + chunk_size =roundup (size,PAGE_SIZE ); + chunk =vmalloc (chunk_size); +if (unlikely (chunk ==NULL )) +return false; + +/* Locking is already done inside gen_pool_add */ + add_error =gen_pool_add (pool, (unsigned long )chunk, chunk_size, +NUMA_NO_NODE ); +if (unlikely (add_error !=0 )) + goto abort; + +return true; +abort: +vfree_atomic (chunk); +return false; + +} + +void *pmalloc (struct gen_pool *pool,size_t size,gfp_t gfp) +{ +void *chunk; +size_t chunk_size; +bool add_error; +unsigned long retval; +unsigned int order; + +if (check_alloc_params (pool, size)) +return NULL ; + + order = (unsigned int )pool->min_alloc_order; + +retry_alloc_from_pool: + retval =gen_pool_alloc (pool, size); +if (retval) + goto return_allocation; + +if (unlikely ((gfp & __GFP_ATOMIC))) { +if (unlikely ((gfp & __GFP_NOFAIL))) + goto retry_alloc_from_pool; +else +return NULL ; + } + +/* Expand pool */ + chunk_size =roundup (size,PAGE_SIZE ); + chunk =vmalloc (chunk_size); +if (unlikely (!chunk)) { +if (unlikely ((gfp & __GFP_NOFAIL))) + goto retry_alloc_from_pool; +else +return NULL ; + } +if (unlikely (!tag_chunk (chunk))) + goto free; + +/* Locking is already done inside gen_pool_add */ + add_error =gen_pool_add (pool, (unsigned long )chunk, chunk_size, +NUMA_NO_NODE ); +if (unlikely (add_error)) + goto abort; + + retval =gen_pool_alloc (pool, size); +if (retval) { +return_allocation: + *(size_t *)retval = size;
mm/pmalloc.c line 399
Quoted 10 lines
mm/pmalloc.c new file +10
-0+if (gfp & __GFP_ZERO) +memset ((void *)retval,0 , size); +return (void *)retval; + } +/* Here there is no test for __GFP_NO_FAIL because, in case of +* concurrent allocation, one thread might add a chunk to the +* pool and this memory could be allocated by another thread, +* before the first thread gets a chance to use it. +* As long as vmalloc succeeds, it's ok to retry. +*/
Fix multi-line comment style.
Quoted 7 lines
mm/pmalloc.c new file +7
-0+ goto retry_alloc_from_pool; +abort: +untag_chunk (chunk); +free: +vfree_atomic (chunk); +return NULL ; +}
[snip]
Quoted 1 line
+/**
Just use /*
Quoted 61 lines
+ * When the sysfs is ready to receive registrations, connect all the
+ * pools previously created. Also enable further pools to be connected
+ * right away.
+ */mm/pmalloc.c new file +20
-0+static int __init pmalloc_late_init (void ) +{ +struct pmalloc_data *data, *n; + + pmalloc_kobject =kobject_create_and_add ("pmalloc" , kernel_kobj); + +mutex_lock (&pmalloc_mutex); + pmalloc_list = &pmalloc_final_list; + +if (likely (pmalloc_kobject !=NULL )) { +list_for_each_entry_safe (data, n, &pmalloc_tmp_list, node) { +list_move (&data->node, &pmalloc_final_list); +pmalloc_connect (data); + } + } +mutex_unlock (&pmalloc_mutex); +pmalloc_selftest (); +return 0 ; +} +late_initcall (pmalloc_late_init);mm/usercopy.c +4
-2@@ -15,6 +15,7 @@#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt#include <linux/mm.h> +#include <linux/pmalloc.h> #include <linux/slab.h> #include <linux/sched.h> #include <linux/sched/task.h> @@ -222,6 +223,7 @@ static inline const char *check_heap_object(const void *ptr, unsigned long n,void __check_object_size (const void *ptr,unsigned long n,bool to_user) {const char *err; +int retv;/* Skip all tests if size is zero. */ if (!n)@@ -229,12 +231,12 @@ void __check_object_size(const void *ptr, unsigned long n, bool to_user)/* Check for invalid addresses. */ err =check_bogus_address (ptr, n); -if (err) +if (unlikely (err)) goto report;/* Check for bad heap object. */ err =check_heap_object (ptr, n, to_user); -if (err) +if (unlikely (err)) goto report;/* Check for bad stack object. */
mm/usercopy.c void __check_object_size(const void *ptr, unsigned long n, bool to_user) · line 266
Quoted 10 lines
mm/usercopy.c +5
-2@@ -257,8 +259,23 @@ void __check_object_size(const void *ptr, unsigned long n, bool to_user)/* Check for object in kernel to avoid text exposure. */ err =check_kernel_text_object (ptr, n); -if (!err) -return ; +if (unlikely (err)) + goto report; + +/* Check if object is from a pmalloc chunk. +*/
Use kernel multi-line comment style.
Quoted 16 lines
mm/usercopy.c +12
-0+ retv =is_pmalloc_object (ptr, n); +if (unlikely (retv)) { +if (unlikely (!to_user)) { + err ="<trying to write to pmalloc object>" ; + goto report; + } +if (retv <0 ) { + err ="<invalid pmalloc object>" ; + goto report; + } + } +return ; report:report_usercopy (ptr, n, to_user, err);
Signature · 1 line
~Randy
Re: [PATCH 4/6] Protectable Memory
Quoted 1 line · Randy Dunlap
On 02/04/2018 08:47 AM, Igor Stoppa wrote:
[...]
Quoted 3 lines
Quoted 1 line
+ *
pmalloc_create_pool- create a new protectable memory pool -Drop trailing " -".
yes
Quoted 3 lines
Quoted 1 line
+ * @name: the name of the pool, must be unique
Is that enforced? Will return NULL if @name is duplicated?
ok, I'll state it more clearly that it's enforced
[...]
Quoted 3 lines
Quoted 1 line
+ * @pool: handler to the pool to be used for memory allocation
handle (I think)
yes, also for all the other ones
[...]
Quoted 3 lines
Quoted 1 line
+ * avoid sleping during allocation.
sleeping
yes
[...]
Quoted 3 lines
Quoted 1 line
+ * opposite to what is allocated on-demand when pmalloc runs out of free
opposed to
yes
Quoted 7 lines
Quoted 3 lines
+ * space already existing in the pool and has to invoke vmalloc.
+ *
+ * Returns true if the vmalloc call was successful, false otherwise.Where is the allocated memory (pointer)? I.e., how does the caller know where that memory is?
Oh, that memory isn't yet available to the caller until it callspmalloc(), right?
yes, it's a way to:
- preemptively beef up the pool, before entering atomic context (unlikely that it will be needed, but possible), so that there is no need to allocate extra pages (assuming one can estimate the max memory that will be requested)
- avoid fragmentation caused by allocating smaller groups of pages
I'll add explanation for this.
[...]
Quoted 10 lines
Quoted 7 lines
+ * @size: amount of memory (in bytes) requested
+ * @gfp: flags for page allocation
+ *
+ * Allocates memory from an unprotected pool. If the pool doesn't have
+ * enough memory, and the request did not includeGFP_ATOMIC, an attempt
+ * is made to add a new chunk of memory to the pool
+ * (a multiple ofPAGE_SIZE), in order to fit the new request.fill
What if @size is >PAGE_SIZE?
Nothing special, it gets rounded up to the nearest multiple of PAGE_SIZE. vmalloc doesn't have only drawbacks ;-)
[...]
Quoted 6 lines
Quoted 2 lines
+ * Returns the pointer to the memory requested upon success,
+ * NULL otherwise (either no memory available or pool already read-only).It would be good to use the
* Return:
kernel-doc notation for return values.
yes, good point, I'm fixing it everywhere in the patchset
[...]
Quoted 3 lines
Quoted 1 line
+ * will be availabel for further allocations.
available
yes
[...]
Quoted 4 lines
Quoted 1 line
+/**
/** means that the following comments are kernel-doc notation, but these comments are not, so just use /* there, please.
yes, also to the others
[...]
Quoted 9 lines
mm/pmalloc.c line 286
Quoted 3 lines
mm/pmalloc.c new file +3
-0+/* is_pmalloc_object gets called pretty late, so chances are high +* that the object is indeed of vmalloc type +*/ Multi-line comment style is
/* * comment1 * comment..N */
yes, also to the others
--
igor
kbuild test robot
build botRe: [PATCH 4/6] Protectable Memory
In reply to Igor StoppaThank you for the patch! Yet something to improve:
Hi Igor,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on kees/for-next/pstore]
[also build test ERROR on v4.15]
[cannot apply to linus/master mmotm/master next-20180206]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
url: https://github.com/0day-ci/linux/commits/Igor-Stoppa/mm-security-ro-protection-for-dynamic-data/20180207-171252 base: https://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git for-next/pstore config: i386-tinyconfig (attached as .config) compiler: gcc-7 (Debian 7.3.0-1) 7.3.0 reproduce: # save the attached .config to linux build tree make ARCH=i386
Note: the linux-review/Igor-Stoppa/mm-security-ro-protection-for-dynamic-data/20180207-171252 HEAD 99d0cb7905216da7595ef08a781a9be16a8ce687 builds fine.
It only hurts bisectibility.
All errors (new ones prefixed by >>):
>> mm/pmalloc.c:24:10: fatal error: pmalloc-selftest.h: No such file or directory
#include "pmalloc-selftest.h"
^~~~~~~~~~~~~~~~~~~~compilation terminated.
vim +24 mm/pmalloc.c
23
> 24 #include "pmalloc-selftest.h"
25 ---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
kbuild test robot
build botRe: [PATCH 4/6] Protectable Memory
In reply to Igor StoppaThank you for the patch! Yet something to improve:
Hi Igor,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on kees/for-next/pstore]
[also build test ERROR on v4.15]
[cannot apply to linus/master mmotm/master next-20180207]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
url: https://github.com/0day-ci/linux/commits/Igor-Stoppa/mm-security-ro-protection-for-dynamic-data/20180207-171252 base: https://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git for-next/pstore config: um-allmodconfig (attached as .config) compiler: gcc-7 (Debian 7.3.0-1) 7.3.0 reproduce: # save the attached .config to linux build tree make ARCH=um
All errors (new ones prefixed by >>):
arch/um/drivers/vde.o: In function `vde_open_real':
(.text+0x951): warning: Using 'getgrnam' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking (.text+0x79c): warning: Using 'getpwuid' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking (.text+0xab5): warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking arch/um/drivers/pcap.o: In function `pcap_nametoaddr':
(.text+0xdee5): warning: Using 'gethostbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking arch/um/drivers/pcap.o: In function `pcap_nametonetaddr':
(.text+0xdf85): warning: Using 'getnetbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking arch/um/drivers/pcap.o: In function `pcap_nametoproto':
(.text+0xe1a5): warning: Using 'getprotobyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking arch/um/drivers/pcap.o: In function `pcap_nametoport':
(.text+0xdfd7): warning: Using 'getservbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking mm/usercopy.o: In function `__check_object_size':
>> (.text+0x3aa): undefined reference to `is_pmalloc_object' >> collect2: error: ld returned 1 exit status
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
PATCH v14 5/6 Pmalloc: self-test
In reply to Igor StoppaAdd basic self-test functionality for pmalloc.
--- mm/Kconfig | 9 ++++++++ mm/Makefile | 1 + mm/pmalloc-selftest.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++ mm/pmalloc-selftest.h | 26 ++++++++++++++++++++++ 4 files changed, 97 insertions(+) create mode 100644 mm/pmalloc-selftest.c create mode 100644 mm/pmalloc-selftest.h
mm/Kconfig +9 -0
@@ -765,3 +765,12 @@ config GUP_BENCHMARK performance of get_user_pages_fast(). See tools/testing/selftests/vm/gup_benchmark.c + +config PROTECTABLE_MEMORY_SELFTEST + bool "Run self test for pmalloc memory allocator" + depends on ARCH_HAS_SET_MEMORY + select GENERIC_ALLOCATOR + 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_ARCH_HAS_SET_MEMORY) += pmalloc.o +obj-$(CONFIG_PROTECTABLE_MEMORY_SELFTEST) += pmalloc-selftest.o obj-$(CONFIG_KSM) += ksm.o obj-$(CONFIG_PAGE_POISONING) += page_poison.o obj-$(CONFIG_SLAB) += slab.o
mm/pmalloc-selftest.c new file +61 -0
61 lines not on this page · raw message
mm/pmalloc-selftest.h new file +26 -0
@@ -0,0 +1,26 @@ +/* SPDX-License-Identifier: GPL-2.0 +* +* pmalloc-selftest.h +* +* (C) Copyright 2018 Huawei Technologies Co. Ltd. +* Author: Igor Stoppa <[email protected]> +*/ + + +#ifndef __PMALLOC_SELFTEST_H__ +#define __PMALLOC_SELFTEST_H__ + + +#ifdef CONFIG_PROTECTABLE_MEMORY_SELFTEST + +#include <linux/pmalloc.h> + +void pmalloc_selftest (void ); + +#else + +static inline void pmalloc_selftest (void ){}; + +#endif + +#endif
Signature · 1 line
2.16.0
PATCH v14 6/6 Documentation for Pmalloc
Detailed documentation about the protectable memory allocator.
--- Documentation/core-api/index.rst | 1 + Documentation/core-api/pmalloc.rst | 114 +++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 Documentation/core-api/pmalloc.rst
Documentation/core-api/index.rst +1 -0
@@ -21,6 +21,7 @@ Core utilities flexible-arrays librs genalloc + pmalloc Interfaces for kernel debugging ===============================
Documentation/core-api/pmalloc.rst new file +114 -0
114 lines not on this page · raw message
Signature · 1 line
2.16.0
Randy Dunlap
reviewerRe: [PATCH 6/6] Documentation for Pmalloc
Hi,
Quoted 27 lines · Igor Stoppa
Detailed documentation about the protectable memory allocator.
Signed-off-by Igor Stoppa <[email protected]>--- Documentation/core-api/index.rst | 1 + Documentation/core-api/pmalloc.rst | 114 +++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 Documentation/core-api/pmalloc.rstDocumentation/core-api/index.rst +1
-0@@ -21,6 +21,7 @@ Core utilities flexible-arrays librs genalloc + pmalloc Interfaces for kernel debugging ===============================Documentation/core-api/pmalloc.rst new file +0
-0
Documentation/core-api/pmalloc.rst line 10
Quoted 10 lines
Documentation/core-api/pmalloc.rst new file +10
-0@@ -0,0 +1,114 @@ +SPDX-License-Identifier: GPL-2.0 + +Protectable memory allocator +============================ + +Purpose +------- + +The pmalloc library is meant to provide R/O status to data that, for some +reason, could neither be declared as constant, nor it could take advantage
nor could it
Documentation/core-api/pmalloc.rst line 14
Quoted 4 lines
Documentation/core-api/pmalloc.rst new file +4
-0+of the qualifier __ro_after_init, but is write-once and read-only in spirit. +It protects data from both accidental and malicious overwrites. + +Ex: A policy that is loaded from userspace.
Either
Example:
or
E.g.:
(meaning For example)
Documentation/core-api/pmalloc.rst line 25
Quoted 11 lines
Documentation/core-api/pmalloc.rst new file +11
-0+ + +Concept +------- + +pmalloc builds on top of genalloc, using the same concept of memory pools. + +The value added by pmalloc is that now the memory contained in a pool can +become R/O, for the rest of the life of the pool. + +Different kernel idrivers and threads can use different pools, for finer
drivers
Documentation/core-api/pmalloc.rst line 38
Quoted 13 lines
Documentation/core-api/pmalloc.rst new file +13
-0+control of what becomes R/O and when. And for improved lockless concurrency. + + +Caveats +------- + +- Memory freed while a pool is not yet protected will be reused. + +- Once a pool is protected, it's not possible to allocate any more memory + from it. + +- Memory "freed" from a protected pool indicates that such memory is not + in use anymore by the requestor, however it will not become avaiable for
requester; however, available
Documentation/core-api/pmalloc.rst line 44
Quoted 6 lines
Documentation/core-api/pmalloc.rst new file +6
-0+ further use, until the pool is destroyed. + +- Before destroying a pool, all the memory allocated from it must be + released. + +- pmalloc does not provide locking support wrt allocating vs protecting
Write out "wrt" -> with respect to.
Documentation/core-api/pmalloc.rst line 45
Quoted 1 line
Documentation/core-api/pmalloc.rst new file +1
-0+ an individual pool, for performance reason. It is recommended to not
reasons. not to
Documentation/core-api/pmalloc.rst line 52
Quoted 7 lines
Documentation/core-api/pmalloc.rst new file +7
-0+ share the same pool between unrelated functions. Should sharing be a + necessity, the user of the shared pool is expected to implement locking + for that pool. + +- pmalloc uses genalloc to optimize the use of the space it allocates + through vmalloc. Some more TLB entries will be used, however less than + in the case of using directly vmalloc. The exact number depends on size
of using vmalloc directly. on the size
Quoted 32 lines
Documentation/core-api/pmalloc.rst new file +32
-0+ of each allocation request and possible slack. + +- Considering that not much data is supposed to be dynamically allocated + and then marked as read-only, it shouldn't be an issue that the address + range for pmalloc is limited, on 32-bit systems. + +- Regarding SMP systems, the allocations are expected to happen mostly + during an initial transient, after which there should be no more need to + perform cross-processor synchronizations of page tables. + +- To facilitate the conversion of existing code to pmalloc pools, several + helper functions are provided, mirroring their kmalloc counterparts. + + +Use +--- + +The typical sequence, when using pmalloc, is: + +1. create a pool + +.. kernel-doc:: include/linux/pmalloc.h + :functions: pmalloc_create_pool + +2. [optional] pre-allocate some memory in the pool + +.. kernel-doc:: include/linux/pmalloc.h + :functions: pmalloc_prealloc + +3. issue one or more allocation requests to the pool with locking as needed + +.. kernel-doc:: include/linux/pmalloc.h
Documentation/core-api/pmalloc.rst line 94
Quoted 10 lines
Documentation/core-api/pmalloc.rst new file +10
-0+ :functions: pmalloc + +.. kernel-doc:: include/linux/pmalloc.h + :functions: pzalloc + +4. initialize the memory obtained with desired values + +5. [optional] iterate over points 3 & 4 as needed + +6. write protect the pool
write-protect
Documentation/core-api/pmalloc.rst line 99
Quoted 5 lines
Documentation/core-api/pmalloc.rst new file +5
-0+ +.. kernel-doc:: include/linux/pmalloc.h + :functions: pmalloc_protect_pool + +7. use in read-only mode the handlers obtained through the allocations
handles ??
Quoted 16 lines
Documentation/core-api/pmalloc.rst new file +15
-0+ +8. [optional] release all the memory allocated + +.. kernel-doc:: include/linux/pmalloc.h + :functions: pfree + +9. [optional, but depends on point 8] destroy the pool + +.. kernel-doc:: include/linux/pmalloc.h + :functions: pmalloc_destroy_pool + +API +--- + +.. kernel-doc:: include/linux/pmalloc.h
Signature · 1 line
~Randy
Re: [PATCH 6/6] Documentation for Pmalloc
On 04/02/18 23:37, Randy Dunlap wrote:
[...]
Quoted 3 lines
Quoted 1 line
+reason, could neither be declared as constant, nor it could take advantage
nor could it
ok
[...]
Quoted 7 lines
Quoted 1 line
+Ex: A policy that is loaded from userspace.
Either
Example:
or
E.g.:
(meaning For example)
ok
[...]
Quoted 3 lines
Quoted 1 line
+Different kernel idrivers and threads can use different pools, for finer
drivers
:-( ok
[...]
Quoted 3 lines
Quoted 1 line
+ in use anymore by the requestor, however it will not become avaiable for
requester; however, available
ok
[...]
Quoted 3 lines
Quoted 1 line
+- pmalloc does not provide locking support wrt allocating vs protecting
Write out "wrt" -> with respect to.
ok
Quoted 3 lines
Quoted 1 line
+ an individual pool, for performance reason. It is recommended to not
reasons. not to
ok & ok
[...]
Quoted 3 lines
Quoted 1 line
+ in the case of using directly vmalloc. The exact number depends on size
of using vmalloc directly. on the size
ok & ok
[...]
Quoted 3 lines
Quoted 1 line
+6. write protect the pool
write-protect
ok
[...]
Quoted 3 lines
Quoted 1 line
+7. use in read-only mode the handlers obtained through the allocations
handles ??
yes
---
thanks, igor
kbuild test robot
build botRe: [PATCH 5/6] Pmalloc: self-test
In reply to Igor StoppaThank you for the patch! Yet something to improve:
Hi Igor,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on kees/for-next/pstore]
[also build test ERROR on v4.15]
[cannot apply to linus/master mmotm/master next-20180207]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
url: https://github.com/0day-ci/linux/commits/Igor-Stoppa/mm-security-ro-protection-for-dynamic-data/20180207-171252 base: https://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git for-next/pstore config: i386-randconfig-s1-201805+bisect_validate (attached as .config) compiler: gcc-6 (Debian 6.4.0-9) 6.4.0 20171026 reproduce: # save the attached .config to linux build tree make ARCH=i386
All errors (new ones prefixed by >>):
mm/pmalloc.o: In function `pmalloc_pool_show_chunks':
>> mm/pmalloc.c:100: undefined reference to `gen_pool_for_each_chunk' mm/pmalloc.o: In function `pmalloc_pool_show_size': >> mm/pmalloc.c:81: undefined reference to `gen_pool_size' mm/pmalloc.o: In function `pmalloc_pool_show_avail': >> mm/pmalloc.c:70: undefined reference to `gen_pool_avail' mm/pmalloc.o: In function `pmalloc_chunk_free': >> mm/pmalloc.c:459: undefined reference to `gen_pool_flush_chunk' mm/pmalloc.o: In function `pmalloc_create_pool': >> mm/pmalloc.c:173: undefined reference to `gen_pool_create' >> mm/pmalloc.c:210: undefined reference to `gen_pool_destroy' mm/pmalloc.o: In function `gen_pool_add': >> include/linux/genalloc.h:115: undefined reference to `gen_pool_add_virt' mm/pmalloc.o: In function `pmalloc': >> mm/pmalloc.c:357: undefined reference to `gen_pool_alloc' mm/pmalloc.o: In function `gen_pool_add': >> include/linux/genalloc.h:115: undefined reference to `gen_pool_add_virt'
mm/pmalloc.o: In function `pmalloc':
mm/pmalloc.c:386: undefined reference to gen_pool_alloc' mm/pmalloc.o: In function pmalloc_destroy_pool':
mm/pmalloc.c:484: undefined reference to gen_pool_for_each_chunk' mm/pmalloc.c:485: undefined reference to gen_pool_destroy'
vim +100 mm/pmalloc.c
13e2be64 Igor Stoppa 2018-02-04 61
13e2be64 Igor Stoppa 2018-02-04 62 static ssize_t pmalloc_pool_show_avail(struct kobject *dev,
13e2be64 Igor Stoppa 2018-02-04 63 struct kobj_attribute *attr,
13e2be64 Igor Stoppa 2018-02-04 64 char *buf)
13e2be64 Igor Stoppa 2018-02-04 65 {
13e2be64 Igor Stoppa 2018-02-04 66 struct pmalloc_data *data;
13e2be64 Igor Stoppa 2018-02-04 67
13e2be64 Igor Stoppa 2018-02-04 68 data = container_of(attr, struct pmalloc_data, attr_avail);
13e2be64 Igor Stoppa 2018-02-04 69 return sprintf(buf, "%lu\n",
13e2be64 Igor Stoppa 2018-02-04 @70 (unsigned long)gen_pool_avail(data->pool));
13e2be64 Igor Stoppa 2018-02-04 71 }
13e2be64 Igor Stoppa 2018-02-04 72
13e2be64 Igor Stoppa 2018-02-04 73 static ssize_t pmalloc_pool_show_size(struct kobject *dev,
13e2be64 Igor Stoppa 2018-02-04 74 struct kobj_attribute *attr,
13e2be64 Igor Stoppa 2018-02-04 75 char *buf)
13e2be64 Igor Stoppa 2018-02-04 76 {
13e2be64 Igor Stoppa 2018-02-04 77 struct pmalloc_data *data;
13e2be64 Igor Stoppa 2018-02-04 78
13e2be64 Igor Stoppa 2018-02-04 79 data = container_of(attr, struct pmalloc_data, attr_size);
13e2be64 Igor Stoppa 2018-02-04 80 return sprintf(buf, "%lu\n",
13e2be64 Igor Stoppa 2018-02-04 @81 (unsigned long)gen_pool_size(data->pool));
13e2be64 Igor Stoppa 2018-02-04 82 }
13e2be64 Igor Stoppa 2018-02-04 83
13e2be64 Igor Stoppa 2018-02-04 84 static void pool_chunk_number(struct gen_pool *pool,
13e2be64 Igor Stoppa 2018-02-04 85 struct gen_pool_chunk *chunk, void *data)
13e2be64 Igor Stoppa 2018-02-04 86 {
13e2be64 Igor Stoppa 2018-02-04 87 unsigned long *counter = data;
13e2be64 Igor Stoppa 2018-02-04 88
13e2be64 Igor Stoppa 2018-02-04 89 (*counter)++;
13e2be64 Igor Stoppa 2018-02-04 90 }
13e2be64 Igor Stoppa 2018-02-04 91
13e2be64 Igor Stoppa 2018-02-04 92 static ssize_t pmalloc_pool_show_chunks(struct kobject *dev,
13e2be64 Igor Stoppa 2018-02-04 93 struct kobj_attribute *attr,
13e2be64 Igor Stoppa 2018-02-04 94 char *buf)
13e2be64 Igor Stoppa 2018-02-04 95 {
13e2be64 Igor Stoppa 2018-02-04 96 struct pmalloc_data *data;
13e2be64 Igor Stoppa 2018-02-04 97 unsigned long chunks_num = 0;
13e2be64 Igor Stoppa 2018-02-04 98
13e2be64 Igor Stoppa 2018-02-04 99 data = container_of(attr, struct pmalloc_data, attr_chunks);
13e2be64 Igor Stoppa 2018-02-04 @100 gen_pool_for_each_chunk(data->pool, pool_chunk_number, &chunks_num);
13e2be64 Igor Stoppa 2018-02-04 101 return sprintf(buf, "%lu\n", chunks_num);
13e2be64 Igor Stoppa 2018-02-04 102 }
13e2be64 Igor Stoppa 2018-02-04 103
13e2be64 Igor Stoppa 2018-02-04 104 /**
13e2be64 Igor Stoppa 2018-02-04 105 * Exposes the pool and its attributes through sysfs.
13e2be64 Igor Stoppa 2018-02-04 106 */
13e2be64 Igor Stoppa 2018-02-04 107 static struct kobject *pmalloc_connect(struct pmalloc_data *data)
13e2be64 Igor Stoppa 2018-02-04 108 {
13e2be64 Igor Stoppa 2018-02-04 109 const struct attribute *attrs[] = {
13e2be64 Igor Stoppa 2018-02-04 110 &data->attr_protected.attr,
13e2be64 Igor Stoppa 2018-02-04 111 &data->attr_avail.attr,
13e2be64 Igor Stoppa 2018-02-04 112 &data->attr_size.attr,
13e2be64 Igor Stoppa 2018-02-04 113 &data->attr_chunks.attr,
13e2be64 Igor Stoppa 2018-02-04 114 NULL
13e2be64 Igor Stoppa 2018-02-04 115 };
13e2be64 Igor Stoppa 2018-02-04 116 struct kobject *kobj;
13e2be64 Igor Stoppa 2018-02-04 117
13e2be64 Igor Stoppa 2018-02-04 118 kobj = kobject_create_and_add(data->pool->name, pmalloc_kobject);
13e2be64 Igor Stoppa 2018-02-04 119 if (unlikely(!kobj))
13e2be64 Igor Stoppa 2018-02-04 120 return NULL;
13e2be64 Igor Stoppa 2018-02-04 121
13e2be64 Igor Stoppa 2018-02-04 122 if (unlikely(sysfs_create_files(kobj, attrs) < 0)) {
13e2be64 Igor Stoppa 2018-02-04 123 kobject_put(kobj);
13e2be64 Igor Stoppa 2018-02-04 124 kobj = NULL;
13e2be64 Igor Stoppa 2018-02-04 125 }
13e2be64 Igor Stoppa 2018-02-04 126 return kobj;
13e2be64 Igor Stoppa 2018-02-04 127 }
13e2be64 Igor Stoppa 2018-02-04 128
13e2be64 Igor Stoppa 2018-02-04 129 /**
13e2be64 Igor Stoppa 2018-02-04 130 * Removes the pool and its attributes from sysfs.
13e2be64 Igor Stoppa 2018-02-04 131 */
13e2be64 Igor Stoppa 2018-02-04 132 static void pmalloc_disconnect(struct pmalloc_data *data,
13e2be64 Igor Stoppa 2018-02-04 133 struct kobject *kobj)
13e2be64 Igor Stoppa 2018-02-04 134 {
13e2be64 Igor Stoppa 2018-02-04 135 const struct attribute *attrs[] = {
13e2be64 Igor Stoppa 2018-02-04 136 &data->attr_protected.attr,
13e2be64 Igor Stoppa 2018-02-04 137 &data->attr_avail.attr,
13e2be64 Igor Stoppa 2018-02-04 138 &data->attr_size.attr,
13e2be64 Igor Stoppa 2018-02-04 139 &data->attr_chunks.attr,
13e2be64 Igor Stoppa 2018-02-04 140 NULL
13e2be64 Igor Stoppa 2018-02-04 141 };
13e2be64 Igor Stoppa 2018-02-04 142
13e2be64 Igor Stoppa 2018-02-04 143 sysfs_remove_files(kobj, attrs);
13e2be64 Igor Stoppa 2018-02-04 144 kobject_put(kobj);
13e2be64 Igor Stoppa 2018-02-04 145 }
13e2be64 Igor Stoppa 2018-02-04 146
13e2be64 Igor Stoppa 2018-02-04 147 /**
13e2be64 Igor Stoppa 2018-02-04 148 * Declares an attribute of the pool.
13e2be64 Igor Stoppa 2018-02-04 149 */
13e2be64 Igor Stoppa 2018-02-04 150
13e2be64 Igor Stoppa 2018-02-04 151 #define pmalloc_attr_init(data, attr_name) \
13e2be64 Igor Stoppa 2018-02-04 152 do { \
13e2be64 Igor Stoppa 2018-02-04 153 sysfs_attr_init(&data->attr_##attr_name.attr); \
13e2be64 Igor Stoppa 2018-02-04 154 data->attr_##attr_name.attr.name = #attr_name; \
13e2be64 Igor Stoppa 2018-02-04 155 data->attr_##attr_name.attr.mode = VERIFY_OCTAL_PERMISSIONS(0400); \
13e2be64 Igor Stoppa 2018-02-04 156 data->attr_##attr_name.show = pmalloc_pool_show_##attr_name; \
13e2be64 Igor Stoppa 2018-02-04 157 } while (0)
13e2be64 Igor Stoppa 2018-02-04 158
13e2be64 Igor Stoppa 2018-02-04 159 struct gen_pool *pmalloc_create_pool(const char *name, int min_alloc_order)
13e2be64 Igor Stoppa 2018-02-04 160 {
13e2be64 Igor Stoppa 2018-02-04 161 struct gen_pool *pool;
13e2be64 Igor Stoppa 2018-02-04 162 const char *pool_name;
13e2be64 Igor Stoppa 2018-02-04 163 struct pmalloc_data *data;
13e2be64 Igor Stoppa 2018-02-04 164
13e2be64 Igor Stoppa 2018-02-04 165 if (!name) {
13e2be64 Igor Stoppa 2018-02-04 166 WARN_ON(1);
13e2be64 Igor Stoppa 2018-02-04 167 return NULL;
13e2be64 Igor Stoppa 2018-02-04 168 }
13e2be64 Igor Stoppa 2018-02-04 169
13e2be64 Igor Stoppa 2018-02-04 170 if (min_alloc_order < 0)
13e2be64 Igor Stoppa 2018-02-04 171 min_alloc_order = ilog2(sizeof(unsigned long));
13e2be64 Igor Stoppa 2018-02-04 172
13e2be64 Igor Stoppa 2018-02-04 @173 pool = gen_pool_create(min_alloc_order, NUMA_NO_NODE);
13e2be64 Igor Stoppa 2018-02-04 174 if (unlikely(!pool))
13e2be64 Igor Stoppa 2018-02-04 175 return NULL;
13e2be64 Igor Stoppa 2018-02-04 176
13e2be64 Igor Stoppa 2018-02-04 177 mutex_lock(&pmalloc_mutex);
13e2be64 Igor Stoppa 2018-02-04 178 list_for_each_entry(data, pmalloc_list, node)
13e2be64 Igor Stoppa 2018-02-04 179 if (!strcmp(name, data->pool->name))
13e2be64 Igor Stoppa 2018-02-04 180 goto same_name_err;
13e2be64 Igor Stoppa 2018-02-04 181
13e2be64 Igor Stoppa 2018-02-04 182 pool_name = kstrdup(name, GFP_KERNEL);
13e2be64 Igor Stoppa 2018-02-04 183 if (unlikely(!pool_name))
13e2be64 Igor Stoppa 2018-02-04 184 goto name_alloc_err;
13e2be64 Igor Stoppa 2018-02-04 185
13e2be64 Igor Stoppa 2018-02-04 186 data = kzalloc(sizeof(struct pmalloc_data), GFP_KERNEL);
13e2be64 Igor Stoppa 2018-02-04 187 if (unlikely(!data))
13e2be64 Igor Stoppa 2018-02-04 188 goto data_alloc_err;
13e2be64 Igor Stoppa 2018-02-04 189
13e2be64 Igor Stoppa 2018-02-04 190 data->protected = false;
13e2be64 Igor Stoppa 2018-02-04 191 data->pool = pool;
13e2be64 Igor Stoppa 2018-02-04 192 pmalloc_attr_init(data, protected);
13e2be64 Igor Stoppa 2018-02-04 193 pmalloc_attr_init(data, avail);
13e2be64 Igor Stoppa 2018-02-04 194 pmalloc_attr_init(data, size);
13e2be64 Igor Stoppa 2018-02-04 195 pmalloc_attr_init(data, chunks);
13e2be64 Igor Stoppa 2018-02-04 196 pool->data = data;
13e2be64 Igor Stoppa 2018-02-04 197 pool->name = pool_name;
13e2be64 Igor Stoppa 2018-02-04 198
13e2be64 Igor Stoppa 2018-02-04 199 list_add(&data->node, pmalloc_list);
13e2be64 Igor Stoppa 2018-02-04 200 if (pmalloc_list == &pmalloc_final_list)
13e2be64 Igor Stoppa 2018-02-04 201 data->pool_kobject = pmalloc_connect(data);
13e2be64 Igor Stoppa 2018-02-04 202 mutex_unlock(&pmalloc_mutex);
13e2be64 Igor Stoppa 2018-02-04 203 return pool;
13e2be64 Igor Stoppa 2018-02-04 204
13e2be64 Igor Stoppa 2018-02-04 205 data_alloc_err:
13e2be64 Igor Stoppa 2018-02-04 206 kfree(pool_name);
13e2be64 Igor Stoppa 2018-02-04 207 name_alloc_err:
13e2be64 Igor Stoppa 2018-02-04 208 same_name_err:
13e2be64 Igor Stoppa 2018-02-04 209 mutex_unlock(&pmalloc_mutex);
13e2be64 Igor Stoppa 2018-02-04 @210 gen_pool_destroy(pool);
13e2be64 Igor Stoppa 2018-02-04 211 return NULL;
13e2be64 Igor Stoppa 2018-02-04 212 }13e2be64 Igor Stoppa 2018-02-04 213
:::::: The code at line 100 was first introduced by commit :::::: 13e2be64b34fce3c12159af55855eef7a8b4a54f Protectable Memory
:::::: TO: Igor Stoppa <[email protected]>
:::::: CC: 0day robot <[email protected]>
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
Re: [PATCH 5/6] Pmalloc: self-test
Quoted 33 lines · kbuild test robot
Hi Igor,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on kees/for-next/pstore]
[also build test ERROR on v4.15]
[cannot apply to linus/master mmotm/master next-20180207]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]url: https://github.com/0day-ci/linux/commits/Igor-Stoppa/mm-security-ro-protection-for-dynamic-data/20180207-171252 base: https://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git for-next/pstore config: i386-randconfig-s1-201805+bisect_validate (attached as .config) compiler: gcc-6 (Debian 6.4.0-9) 6.4.0 20171026 reproduce: # save the attached .config to linux build tree make ARCH=i386All errors (new ones prefixed by >>):
mm/pmalloc.o: In function `pmalloc_pool_show_chunks':Quoted 1 line
Quoted 1 line
mm/pmalloc.c:100: undefined reference to `gen_pool_for_each_chunk'
mm/pmalloc.o: In function `pmalloc_pool_show_size':Quoted 1 line
Quoted 1 line
mm/pmalloc.c:81: undefined reference to `gen_pool_size'
mm/pmalloc.o: In function `pmalloc_pool_show_avail':Quoted 1 line
Quoted 1 line
mm/pmalloc.c:70: undefined reference to `gen_pool_avail'
mm/pmalloc.o: In function `pmalloc_chunk_free':Quoted 1 line
Quoted 1 line
mm/pmalloc.c:459: undefined reference to `gen_pool_flush_chunk'
mm/pmalloc.o: In function `pmalloc_create_pool':Quoted 2 lines
Quoted 2 lines
mm/pmalloc.c:173: undefined reference togen_pool_create' mm/pmalloc.c:210: undefined reference togen_pool_destroy'
mm/pmalloc.o: In function `gen_pool_add':Quoted 1 line
Quoted 1 line
include/linux/genalloc.h:115: undefined reference to `gen_pool_add_virt'
mm/pmalloc.o: In function `pmalloc':
Quoted 8 lines
Quoted 1 line
Quoted 1 line
mm/pmalloc.c:357: undefined reference to `gen_pool_alloc'
mm/pmalloc.o: In function `gen_pool_add':Quoted 1 line
Quoted 1 line
include/linux/genalloc.h:115: undefined reference to `gen_pool_add_virt'
mm/pmalloc.o: In function `pmalloc':
mm/pmalloc.c:386: undefined reference togen_pool_alloc' mm/pmalloc.o: In functionpmalloc_destroy_pool':
mm/pmalloc.c:484: undefined reference togen_pool_for_each_chunk' mm/pmalloc.c:485: undefined reference togen_pool_destroy'
Wrong default value for the pmalloc Kconfig option, it must default to true. Will fix it.
--
igor