Received: by 10.223.176.46 with SMTP id f43csp1061707wra; Wed, 24 Jan 2018 10:00:26 -0800 (PST) X-Google-Smtp-Source: AH8x2244B6ssuB/S61u6N5bIHmKlzAER5/L12DiT0gEpmzQOUP5oXAxyjJ58bvGQXndip6gc9Dkl X-Received: by 2002:a17:902:bcc5:: with SMTP id o5-v6mr9016833pls.67.1516816826280; Wed, 24 Jan 2018 10:00:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516816826; cv=none; d=google.com; s=arc-20160816; b=MrsDDHxyoy/+ihvZKCEUfXnFLc0bMTM2rd84kX54m5q0mvVdltd5SVD6+UU/IgYY6O vvRN/MAd7cOe5Vns/AcqOJbR6SHHFbmQs73B5zJyhQaOm7dMF0W/TldnRGMq2m3VANcx y1ANcV7stNB0Gutj1D19BXDTrUUzWsBBkaDGqLzh2Cb/3mXN3AKUoi6G7AVJ05bpTjd1 aC8EzXYjfR3kK1W0egU4lIGbrb82DLWXo4be8EZN+mjL+FmAqNlhg2Wjwu0OQv6yR9HV H7w73SEokpIao/NKCvmkQNQSMWD8UWHlaVtOdAzqtP9XsAxt/IO38brLQDdfocaN21y2 uorQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:arc-authentication-results; bh=x9GXMn7c/hr/kTryhwJoyV9iUM9owlaVxzC4PZTjxx8=; b=ogJ4l/lVJFbMjRbtVCyqN6rC87s8lokACT9ewqXk30k888F/v70KnTdC6uoUic7qAX fq/NI+8X7g9+n1oR2t60PdJS98aIGLJIckAVONAgsbt8gfB0PD231HEo4G+VTIk0HuXi NMJfZ91WM1ebrhf69vQvqazjGO51dNsqWGN3XydNeC3KmctxBRiFOqy/V9vp6ZgwZDBY rqBEVmBu5POlLDGn82CInnjUvGm7WrmndEsDadw+07/fm07ToFrMoXmd7GObjrF+/qaZ nDEhmk0ipbgUNZlerHdqUWX6tj4dkx6OoK42fiHHLazp1NaZqd6FtmM8K8K2U8MvGcqX +ghg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 71-v6si523160plc.603.2018.01.24.10.00.12; Wed, 24 Jan 2018 10:00:26 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S965030AbeAXR7s (ORCPT + 99 others); Wed, 24 Jan 2018 12:59:48 -0500 Received: from lhrrgout.huawei.com ([194.213.3.17]:22733 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S964826AbeAXR7q (ORCPT ); Wed, 24 Jan 2018 12:59:46 -0500 Received: from LHREML712-CAH.china.huawei.com (unknown [172.18.7.107]) by Forcepoint Email with ESMTP id 41649CE1961E6; Wed, 24 Jan 2018 17:59:43 +0000 (GMT) Received: from localhost.localdomain (10.122.225.51) by smtpsuk.huawei.com (10.201.108.35) with Microsoft SMTP Server (TLS) id 14.3.361.1; Wed, 24 Jan 2018 17:59:39 +0000 From: Igor Stoppa To: , , , , , CC: , , , , , Igor Stoppa Subject: [PATCH 6/6] Pmalloc: self-test Date: Wed, 24 Jan 2018 19:56:31 +0200 Message-ID: <20180124175631.22925-7-igor.stoppa@huawei.com> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20180124175631.22925-1-igor.stoppa@huawei.com> References: <20180124175631.22925-1-igor.stoppa@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.122.225.51] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add basic self-test functionality for pmalloc. Signed-off-by: Igor Stoppa --- mm/Kconfig | 7 ++++++ mm/Makefile | 1 + mm/pmalloc-selftest.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++ mm/pmalloc-selftest.h | 30 ++++++++++++++++++++++++ mm/pmalloc.c | 3 +++ 5 files changed, 106 insertions(+) create mode 100644 mm/pmalloc-selftest.c create mode 100644 mm/pmalloc-selftest.h diff --git a/mm/Kconfig b/mm/Kconfig index c782e8f..1de6ea6 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -760,3 +760,10 @@ 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" + 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 a6a47e1..1e76a9b 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 0000000..1c025f3 --- /dev/null +++ b/mm/pmalloc-selftest.c @@ -0,0 +1,65 @@ +/* + * pmalloc-selftest.c + * + * (C) Copyright 2018 Huawei Technologies Co. Ltd. + * Author: Igor Stoppa + * + * 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 +#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 0000000..3673d23 --- /dev/null +++ b/mm/pmalloc-selftest.h @@ -0,0 +1,30 @@ +/* + * pmalloc-selftest.h + * + * (C) Copyright 2018 Huawei Technologies Co. Ltd. + * Author: Igor Stoppa + * + * 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 __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 diff --git a/mm/pmalloc.c b/mm/pmalloc.c index a64ac49..a722d7b 100644 --- a/mm/pmalloc.c +++ b/mm/pmalloc.c @@ -25,6 +25,8 @@ #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. @@ -508,6 +510,7 @@ static int __init pmalloc_late_init(void) } } mutex_unlock(&pmalloc_mutex); + pmalloc_selftest(); return 0; } late_initcall(pmalloc_late_init); -- 2.9.3