Re: [PATCH 5/6] Pmalloc: self-test
On 22/02/18 00:24, Kees Cook wrote: > On Tue, Feb 20, 2018 at 8:40 AM, Igor Stoppa wrote: [...] >> sorry for being dense ... are you proposing that I do something to >> lkdtm_rodata.c ? An example would probably help me understand. > > It would likely live in lkdtm_perms.c (or maybe lkdtm_heap.c). Namely, > use the pmalloc API and then attempt to write to a read-only variable > in the pmalloc region (to prove that the permission adjustment > actually happened). Likely a good example is > lkdtm_WRITE_RO_AFTER_INIT(). ok, thanks for the explanation, I will do it -- igor
Re: [PATCH 5/6] Pmalloc: self-test
On Tue, Feb 20, 2018 at 8:40 AM, Igor Stoppa wrote: > > On 13/02/18 01:43, Kees Cook wrote: >> On Mon, Feb 12, 2018 at 8:53 AM, Igor Stoppa wrote: > > [...] > >>> +obj-$(CONFIG_PROTECTABLE_MEMORY_SELFTEST) += pmalloc-selftest.o >> >> Nit: self-test modules are traditionally named "test_$thing.o" >> (outside of the tools/ directory). > > ok > > [...] > >> I wonder if lkdtm should grow a test too, to validate the RO-ness of >> the allocations at the right time in API usage? > > sorry for being dense ... are you proposing that I do something to > lkdtm_rodata.c ? An example would probably help me understand. It would likely live in lkdtm_perms.c (or maybe lkdtm_heap.c). Namely, use the pmalloc API and then attempt to write to a read-only variable in the pmalloc region (to prove that the permission adjustment actually happened). Likely a good example is lkdtm_WRITE_RO_AFTER_INIT(). -Kees -- Kees Cook Pixel Security
Re: [PATCH 5/6] Pmalloc: self-test
On 13/02/18 01:43, Kees Cook wrote: > On Mon, Feb 12, 2018 at 8:53 AM, Igor Stoppa wrote: [...] >> +obj-$(CONFIG_PROTECTABLE_MEMORY_SELFTEST) += pmalloc-selftest.o > > Nit: self-test modules are traditionally named "test_$thing.o" > (outside of the tools/ directory). ok [...] > I wonder if lkdtm should grow a test too, to validate the RO-ness of > the allocations at the right time in API usage? sorry for being dense ... are you proposing that I do something to lkdtm_rodata.c ? An example would probably help me understand. -- igor
Re: [PATCH 5/6] Pmalloc: self-test
Hi Igor, Thank you for the patch! Yet something to improve: [auto build test ERROR on linus/master] [also build test ERROR on v4.16-rc1 next-20180212] [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/genalloc-track-beginning-of-allocations/20180212-192839 config: arm-allnoconfig (attached as .config) compiler: arm-linux-gnueabi-gcc (Debian 7.2.0-11) 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=arm All errors (new ones prefixed by >>): mm/pmalloc.o: In function `pmalloc_prealloc': >> pmalloc.c:(.text+0x268): undefined reference to `vfree_atomic' mm/pmalloc.o: In function `pmalloc': pmalloc.c:(.text+0x2ac): undefined reference to `vfree_atomic' mm/pmalloc.o: In function `pmalloc_chunk_free': pmalloc.c:(.text+0x86): undefined reference to `vfree_atomic' --- 0-DAY kernel test infrastructureOpen Source Technology Center https://lists.01.org/pipermail/kbuild-all Intel Corporation .config.gz Description: application/gzip
Re: [PATCH 5/6] Pmalloc: self-test
On Mon, Feb 12, 2018 at 8:53 AM, Igor Stoppa wrote: > Add basic self-test functionality for pmalloc. > > Signed-off-by: Igor Stoppa > --- > mm/Kconfig| 9 > mm/Makefile | 1 + > mm/pmalloc-selftest.c | 64 > +++ > mm/pmalloc-selftest.h | 24 +++ > mm/pmalloc.c | 2 ++ > 5 files changed, 100 insertions(+) > create mode 100644 mm/pmalloc-selftest.c > create mode 100644 mm/pmalloc-selftest.h > > diff --git a/mm/Kconfig b/mm/Kconfig > index be578fbdce6d..098aefef78b1 100644 > --- a/mm/Kconfig > +++ b/mm/Kconfig > @@ -766,3 +766,12 @@ config PROTECTABLE_MEMORY > depends on ARCH_HAS_SET_MEMORY > select GENERIC_ALLOCATOR > default y > + > +config PROTECTABLE_MEMORY_SELFTEST > + bool "Run self test for pmalloc memory allocator" > + depends on ARCH_HAS_SET_MEMORY > + select PROTECTABLE_MEMORY > + default n > + help > + Tries to verify that pmalloc works correctly and that the memory > + is effectively protected. > diff --git a/mm/Makefile b/mm/Makefile > index 959fdbdac118..f7bbbfde6967 100644 > --- a/mm/Makefile > +++ b/mm/Makefile > @@ -66,6 +66,7 @@ obj-$(CONFIG_SPARSEMEM_VMEMMAP) += sparse-vmemmap.o > obj-$(CONFIG_SLOB) += slob.o > obj-$(CONFIG_MMU_NOTIFIER) += mmu_notifier.o > obj-$(CONFIG_PROTECTABLE_MEMORY) += pmalloc.o > +obj-$(CONFIG_PROTECTABLE_MEMORY_SELFTEST) += pmalloc-selftest.o Nit: self-test modules are traditionally named "test_$thing.o" (outside of the tools/ directory). > obj-$(CONFIG_KSM) += ksm.o > obj-$(CONFIG_PAGE_POISONING) += page_poison.o > obj-$(CONFIG_SLAB) += slab.o > diff --git a/mm/pmalloc-selftest.c b/mm/pmalloc-selftest.c > new file mode 100644 > index ..97ba52d17f69 > --- /dev/null > +++ b/mm/pmalloc-selftest.c > @@ -0,0 +1,64 @@ > +// SPDX-License-Identifier: GPL-2.0 > +/* > + * pmalloc-selftest.c > + * > + * (C) Copyright 2018 Huawei Technologies Co. Ltd. > + * Author: Igor Stoppa > + */ > + > +#include > +#include > + > +#include "pmalloc-selftest.h" > + > +#define SIZE_1 (PAGE_SIZE * 3) > +#define SIZE_2 1000 > + > +#define validate_alloc(expected, variable, size) \ > + pr_notice("must be " expected ": %s", \ > + is_pmalloc_object(variable, size) > 0 ? "ok" : "no") > + > +#define is_alloc_ok(variable, size)\ > + validate_alloc("ok", variable, size) > + > +#define is_alloc_no(variable, size)\ > + validate_alloc("no", variable, size) > + > +void pmalloc_selftest(void) > +{ > + struct gen_pool *pool_unprot; > + struct gen_pool *pool_prot; > + void *var_prot, *var_unprot, *var_vmall; > + > + pr_notice("pmalloc self-test"); > + pool_unprot = pmalloc_create_pool("unprotected", 0); > + pool_prot = pmalloc_create_pool("protected", 0); > + BUG_ON(!(pool_unprot && pool_prot)); > + > + var_unprot = pmalloc(pool_unprot, SIZE_1 - 1, GFP_KERNEL); > + var_prot = pmalloc(pool_prot, SIZE_1, GFP_KERNEL); > + *(int *)var_prot = 0; > + var_vmall = vmalloc(SIZE_2); > + is_alloc_ok(var_unprot, 10); > + is_alloc_ok(var_unprot, SIZE_1); > + is_alloc_ok(var_unprot, PAGE_SIZE); > + is_alloc_no(var_unprot, SIZE_1 + 1); > + is_alloc_no(var_vmall, 10); > + > + > + pfree(pool_unprot, var_unprot); > + vfree(var_vmall); > + > + pmalloc_protect_pool(pool_prot); > + > + /* > +* This will intentionally trigger a WARN because the pool being > +* destroyed is not protected, which is unusual and should happen > +* on error paths only, where probably other warnings are already > +* displayed. > +*/ > + pmalloc_destroy_pool(pool_unprot); > + > + /* This must not cause WARNings */ > + pmalloc_destroy_pool(pool_prot); > +} I wonder if lkdtm should grow a test too, to validate the RO-ness of the allocations at the right time in API usage? Otherwise, yay! Selftests! -Kees > diff --git a/mm/pmalloc-selftest.h b/mm/pmalloc-selftest.h > new file mode 100644 > index ..58a5a0cbec14 > --- /dev/null > +++ b/mm/pmalloc-selftest.h > @@ -0,0 +1,24 @@ > +/* SPDX-License-Identifier: GPL-2.0 */ > +/* > + * pmalloc-selftest.h > + * > + * (C) Copyright 2018 Huawei Technologies Co. Ltd. > + * Author: Igor Stoppa > + */ > + > + > +#ifndef __MM_PMALLOC_SELFTEST_H > +#define __MM_PMALLOC_SELFTEST_H > + > + > +#ifdef CONFIG_PROTECTABLE_MEMORY_SELFTEST > + > +void pmalloc_selftest(void); > + > +#else > + > +static inline void pmalloc_selftest(void){}; > + > +#endif > + > +#endif > diff --git a/mm/pmalloc.c b/mm/pmalloc.c > index abddba90a9f6..eb445c574b19 100644 > --- a/mm/pmalloc.c > +++ b/mm/pmalloc.c > @@ -22,6 +22,7 @@ > #include > > #include > +#include "pmalloc-selftest.h" > /* > * pmalloc_data contains the data specific to a pmalloc pool, > * in a format compatible with the design o
[PATCH 5/6] Pmalloc: self-test
Add basic self-test functionality for pmalloc. Signed-off-by: Igor Stoppa --- mm/Kconfig| 9 mm/Makefile | 1 + mm/pmalloc-selftest.c | 64 +++ mm/pmalloc-selftest.h | 24 +++ mm/pmalloc.c | 2 ++ 5 files changed, 100 insertions(+) create mode 100644 mm/pmalloc-selftest.c create mode 100644 mm/pmalloc-selftest.h diff --git a/mm/Kconfig b/mm/Kconfig index be578fbdce6d..098aefef78b1 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -766,3 +766,12 @@ config PROTECTABLE_MEMORY depends on ARCH_HAS_SET_MEMORY select GENERIC_ALLOCATOR default y + +config PROTECTABLE_MEMORY_SELFTEST + bool "Run self test for pmalloc memory allocator" + depends on ARCH_HAS_SET_MEMORY + select PROTECTABLE_MEMORY + default n + help + Tries to verify that pmalloc works correctly and that the memory + is effectively protected. diff --git a/mm/Makefile b/mm/Makefile index 959fdbdac118..f7bbbfde6967 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -66,6 +66,7 @@ obj-$(CONFIG_SPARSEMEM_VMEMMAP) += sparse-vmemmap.o obj-$(CONFIG_SLOB) += slob.o obj-$(CONFIG_MMU_NOTIFIER) += mmu_notifier.o obj-$(CONFIG_PROTECTABLE_MEMORY) += pmalloc.o +obj-$(CONFIG_PROTECTABLE_MEMORY_SELFTEST) += pmalloc-selftest.o obj-$(CONFIG_KSM) += ksm.o obj-$(CONFIG_PAGE_POISONING) += page_poison.o obj-$(CONFIG_SLAB) += slab.o diff --git a/mm/pmalloc-selftest.c b/mm/pmalloc-selftest.c new file mode 100644 index ..97ba52d17f69 --- /dev/null +++ b/mm/pmalloc-selftest.c @@ -0,0 +1,64 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * pmalloc-selftest.c + * + * (C) Copyright 2018 Huawei Technologies Co. Ltd. + * Author: Igor Stoppa + */ + +#include +#include + +#include "pmalloc-selftest.h" + +#define SIZE_1 (PAGE_SIZE * 3) +#define SIZE_2 1000 + +#define validate_alloc(expected, variable, size) \ + pr_notice("must be " expected ": %s", \ + is_pmalloc_object(variable, size) > 0 ? "ok" : "no") + +#define is_alloc_ok(variable, size)\ + validate_alloc("ok", variable, size) + +#define is_alloc_no(variable, size)\ + validate_alloc("no", variable, size) + +void pmalloc_selftest(void) +{ + struct gen_pool *pool_unprot; + struct gen_pool *pool_prot; + void *var_prot, *var_unprot, *var_vmall; + + pr_notice("pmalloc self-test"); + pool_unprot = pmalloc_create_pool("unprotected", 0); + pool_prot = pmalloc_create_pool("protected", 0); + BUG_ON(!(pool_unprot && pool_prot)); + + var_unprot = pmalloc(pool_unprot, SIZE_1 - 1, GFP_KERNEL); + var_prot = pmalloc(pool_prot, SIZE_1, GFP_KERNEL); + *(int *)var_prot = 0; + var_vmall = vmalloc(SIZE_2); + is_alloc_ok(var_unprot, 10); + is_alloc_ok(var_unprot, SIZE_1); + is_alloc_ok(var_unprot, PAGE_SIZE); + is_alloc_no(var_unprot, SIZE_1 + 1); + is_alloc_no(var_vmall, 10); + + + pfree(pool_unprot, var_unprot); + vfree(var_vmall); + + pmalloc_protect_pool(pool_prot); + + /* +* This will intentionally trigger a WARN because the pool being +* destroyed is not protected, which is unusual and should happen +* on error paths only, where probably other warnings are already +* displayed. +*/ + pmalloc_destroy_pool(pool_unprot); + + /* This must not cause WARNings */ + pmalloc_destroy_pool(pool_prot); +} diff --git a/mm/pmalloc-selftest.h b/mm/pmalloc-selftest.h new file mode 100644 index ..58a5a0cbec14 --- /dev/null +++ b/mm/pmalloc-selftest.h @@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * pmalloc-selftest.h + * + * (C) Copyright 2018 Huawei Technologies Co. Ltd. + * Author: Igor Stoppa + */ + + +#ifndef __MM_PMALLOC_SELFTEST_H +#define __MM_PMALLOC_SELFTEST_H + + +#ifdef CONFIG_PROTECTABLE_MEMORY_SELFTEST + +void pmalloc_selftest(void); + +#else + +static inline void pmalloc_selftest(void){}; + +#endif + +#endif diff --git a/mm/pmalloc.c b/mm/pmalloc.c index abddba90a9f6..eb445c574b19 100644 --- a/mm/pmalloc.c +++ b/mm/pmalloc.c @@ -22,6 +22,7 @@ #include #include +#include "pmalloc-selftest.h" /* * pmalloc_data contains the data specific to a pmalloc pool, * in a format compatible with the design of gen_alloc. @@ -494,6 +495,7 @@ static int __init pmalloc_late_init(void) } } mutex_unlock(&pmalloc_mutex); + pmalloc_selftest(); return 0; } late_initcall(pmalloc_late_init); -- 2.14.1
[PATCH 5/6] Pmalloc: self-test
Add basic self-test functionality for pmalloc. Signed-off-by: Igor Stoppa --- mm/Kconfig| 9 mm/Makefile | 1 + mm/pmalloc-selftest.c | 63 +++ mm/pmalloc-selftest.h | 24 mm/pmalloc.c | 2 ++ 5 files changed, 99 insertions(+) create mode 100644 mm/pmalloc-selftest.c create mode 100644 mm/pmalloc-selftest.h diff --git a/mm/Kconfig b/mm/Kconfig index be578fbdce6d..098aefef78b1 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -766,3 +766,12 @@ config PROTECTABLE_MEMORY depends on ARCH_HAS_SET_MEMORY select GENERIC_ALLOCATOR default y + +config PROTECTABLE_MEMORY_SELFTEST + bool "Run self test for pmalloc memory allocator" + depends on ARCH_HAS_SET_MEMORY + select PROTECTABLE_MEMORY + default n + help + Tries to verify that pmalloc works correctly and that the memory + is effectively protected. diff --git a/mm/Makefile b/mm/Makefile index 959fdbdac118..f7bbbfde6967 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -66,6 +66,7 @@ obj-$(CONFIG_SPARSEMEM_VMEMMAP) += sparse-vmemmap.o obj-$(CONFIG_SLOB) += slob.o obj-$(CONFIG_MMU_NOTIFIER) += mmu_notifier.o obj-$(CONFIG_PROTECTABLE_MEMORY) += pmalloc.o +obj-$(CONFIG_PROTECTABLE_MEMORY_SELFTEST) += pmalloc-selftest.o obj-$(CONFIG_KSM) += ksm.o obj-$(CONFIG_PAGE_POISONING) += page_poison.o obj-$(CONFIG_SLAB) += slab.o diff --git a/mm/pmalloc-selftest.c b/mm/pmalloc-selftest.c new file mode 100644 index ..05acd62d23ec --- /dev/null +++ b/mm/pmalloc-selftest.c @@ -0,0 +1,63 @@ +/* SPDX-License-Identifier: GPL-2.0 + * + * pmalloc-selftest.c + * + * (C) Copyright 2018 Huawei Technologies Co. Ltd. + * Author: Igor Stoppa + */ + +#include +#include + +#include "pmalloc-selftest.h" + +#define SIZE_1 (PAGE_SIZE * 3) +#define SIZE_2 1000 + +#define validate_alloc(expected, variable, size) \ + pr_notice("must be " expected ": %s", \ + is_pmalloc_object(variable, size) > 0 ? "ok" : "no") + +#define is_alloc_ok(variable, size)\ + validate_alloc("ok", variable, size) + +#define is_alloc_no(variable, size)\ + validate_alloc("no", variable, size) + +void pmalloc_selftest(void) +{ + struct gen_pool *pool_unprot; + struct gen_pool *pool_prot; + void *var_prot, *var_unprot, *var_vmall; + + pr_notice("pmalloc self-test"); + pool_unprot = pmalloc_create_pool("unprotected", 0); + pool_prot = pmalloc_create_pool("protected", 0); + BUG_ON(!(pool_unprot && pool_prot)); + + var_unprot = pmalloc(pool_unprot, SIZE_1 - 1, GFP_KERNEL); + var_prot = pmalloc(pool_prot, SIZE_1, GFP_KERNEL); + *(int *)var_prot = 0; + var_vmall = vmalloc(SIZE_2); + is_alloc_ok(var_unprot, 10); + is_alloc_ok(var_unprot, SIZE_1); + is_alloc_ok(var_unprot, PAGE_SIZE); + is_alloc_no(var_unprot, SIZE_1 + 1); + is_alloc_no(var_vmall, 10); + + + pfree(pool_unprot, var_unprot); + vfree(var_vmall); + + pmalloc_protect_pool(pool_prot); + + /* This will intentionally trigger a WARN because the pool being +* destroyed is not protected, which is unusual and should happen +* on error paths only, where probably other warnings are already +* displayed. +*/ + pmalloc_destroy_pool(pool_unprot); + + /* This must not cause WARNings */ + pmalloc_destroy_pool(pool_prot); +} diff --git a/mm/pmalloc-selftest.h b/mm/pmalloc-selftest.h new file mode 100644 index ..63e430de74f8 --- /dev/null +++ b/mm/pmalloc-selftest.h @@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: GPL-2.0 + * + * pmalloc-selftest.h + * + * (C) Copyright 2018 Huawei Technologies Co. Ltd. + * Author: Igor Stoppa + */ + + +#ifndef __MM_PMALLOC_SELFTEST_H +#define __MM_PMALLOC_SELFTEST_H + + +#ifdef CONFIG_PROTECTABLE_MEMORY_SELFTEST + +void pmalloc_selftest(void); + +#else + +static inline void pmalloc_selftest(void){}; + +#endif + +#endif diff --git a/mm/pmalloc.c b/mm/pmalloc.c index e94bfb407c92..c9a472730afc 100644 --- a/mm/pmalloc.c +++ b/mm/pmalloc.c @@ -22,6 +22,7 @@ #include #include +#include "pmalloc-selftest.h" /* * pmalloc_data contains the data specific to a pmalloc pool, * in a format compatible with the design of gen_alloc. @@ -492,6 +493,7 @@ static int __init pmalloc_late_init(void) } } mutex_unlock(&pmalloc_mutex); + pmalloc_selftest(); return 0; } late_initcall(pmalloc_late_init); -- 2.14.1
Re: [PATCH 5/6] Pmalloc: self-test
On 07/02/18 19:18, kbuild test robot wrote: > 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' Wrong default value for the pmalloc Kconfig option, it must default to true. Will fix it. -- igor
Re: [PATCH 5/6] Pmalloc: self-test
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 66struct pmalloc_data *data; 13e2be64 Igor Stoppa 2018-02-04 67 13e2be64 Igor Stoppa 2018-02-04 68data = container_of(attr, struct pmalloc_data, attr_avail); 13e2be64 Igor Stoppa 2018-02-04 69return 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 77struct pmalloc_data *data; 13e2be64 Igor Stoppa 2018-02-04 78 13e2be64 Igor Stoppa 2018-02-04 79data = container_of(attr, struct pmalloc_data, attr_size); 13e2be64 Igor Stoppa 2018-02-04 80return 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 87unsigned 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 93struct kobj_attribute *attr, 13e2be64 Igor Stoppa 2018-02-04 94char *buf) 13e2be64 Igor Stoppa 2018-02-04 95 { 13e2be64 Igor Stoppa 2018-02-04 96struct pmalloc_data *data; 13e2be64 Igor Stoppa 2018-02-04 97unsigned long chunks_num = 0; 13e2be64 Igor Stoppa 2018-02-04 98 13e2be64 Igor Stoppa 2018-02-04 99data = container_of(attr, struct pmalloc_data, attr_chunks); 13e2be64 Igor Stoppa 2018-02-04 @100gen_pool_for_each_chunk(data->pool, pool_chunk_number, &chunks_num); 13e2be64 Igor Stoppa 2018-02-04 101return sprintf(buf, "%lu\n", chunks_num); 13e2be64 Igor Stoppa 2018-02-04 102 } 13e2be64 Igor Stoppa 2018
[PATCH 5/6] Pmalloc: self-test
Add basic self-test functionality for pmalloc. Signed-off-by: Igor Stoppa --- 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 diff --git a/mm/Kconfig b/mm/Kconfig index 03ff7703d322..f5edfadd036a 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -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. diff --git a/mm/Makefile b/mm/Makefile index a6a47e1b6e66..1e76a9b96180 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -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 diff --git a/mm/pmalloc-selftest.c b/mm/pmalloc-selftest.c new file mode 100644 index ..9f6401ebd8d1 --- /dev/null +++ b/mm/pmalloc-selftest.c @@ -0,0 +1,61 @@ +/* SPDX-License-Identifier: GPL-2.0 + * + * pmalloc-selftest.c + * + * (C) Copyright 2018 Huawei Technologies Co. Ltd. + * Author: Igor Stoppa + */ + +#include +#include + + +#define SIZE_1 (PAGE_SIZE * 3) +#define SIZE_2 1000 + +#define validate_alloc(expected, variable, size) \ + pr_notice("must be " expected ": %s", \ + is_pmalloc_object(variable, size) > 0 ? "ok" : "no") + +#define is_alloc_ok(variable, size)\ + validate_alloc("ok", variable, size) + +#define is_alloc_no(variable, size)\ + validate_alloc("no", variable, size) + +void pmalloc_selftest(void) +{ + struct gen_pool *pool_unprot; + struct gen_pool *pool_prot; + void *var_prot, *var_unprot, *var_vmall; + + pr_notice("pmalloc self-test"); + pool_unprot = pmalloc_create_pool("unprotected", 0); + pool_prot = pmalloc_create_pool("protected", 0); + BUG_ON(!(pool_unprot && pool_prot)); + + var_unprot = pmalloc(pool_unprot, SIZE_1 - 1, GFP_KERNEL); + var_prot = pmalloc(pool_prot, SIZE_1, GFP_KERNEL); + var_vmall = vmalloc(SIZE_2); + is_alloc_ok(var_unprot, 10); + is_alloc_ok(var_unprot, SIZE_1); + is_alloc_ok(var_unprot, PAGE_SIZE); + is_alloc_no(var_unprot, SIZE_1 + 1); + is_alloc_no(var_vmall, 10); + + + pfree(pool_unprot, var_unprot); + vfree(var_vmall); + + pmalloc_protect_pool(pool_prot); + + /* This will intentionally trigger a WARN because the pool being +* destroyed is not protected, which is unusual and should happen +* on error paths only, where probably other warnings are already +* displayed. +*/ + pmalloc_destroy_pool(pool_unprot); + + /* This must not cause WARNings */ + pmalloc_destroy_pool(pool_prot); +} diff --git a/mm/pmalloc-selftest.h b/mm/pmalloc-selftest.h new file mode 100644 index ..b34ac86e14a8 --- /dev/null +++ b/mm/pmalloc-selftest.h @@ -0,0 +1,26 @@ +/* SPDX-License-Identifier: GPL-2.0 + * + * pmalloc-selftest.h + * + * (C) Copyright 2018 Huawei Technologies Co. Ltd. + * Author: Igor Stoppa + */ + + +#ifndef __PMALLOC_SELFTEST_H__ +#define __PMALLOC_SELFTEST_H__ + + +#ifdef CONFIG_PROTECTABLE_MEMORY_SELFTEST + +#include + +void pmalloc_selftest(void); + +#else + +static inline void pmalloc_selftest(void){}; + +#endif + +#endif -- 2.16.0