From patchwork Tue Nov 20 23:23:11 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 10691397 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1BDA213BF for ; Tue, 20 Nov 2018 23:22:14 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 080F92ABE0 for ; Tue, 20 Nov 2018 23:22:14 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EFF972ABED; Tue, 20 Nov 2018 23:22:13 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6EDF82ABE0 for ; Tue, 20 Nov 2018 23:22:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1E54E6B227A; Tue, 20 Nov 2018 18:22:09 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 13C4D6B227B; Tue, 20 Nov 2018 18:22:08 -0500 (EST) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C39DA6B227E; Tue, 20 Nov 2018 18:22:08 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pl1-f200.google.com (mail-pl1-f200.google.com [209.85.214.200]) by kanga.kvack.org (Postfix) with ESMTP id 68D2F6B227A for ; Tue, 20 Nov 2018 18:22:08 -0500 (EST) Received: by mail-pl1-f200.google.com with SMTP id 4so4268874plc.5 for ; Tue, 20 Nov 2018 15:22:08 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references; bh=+mSO61ChHconaFDfZPZTkzvJ9XsnfWKihWo8ZoXaF20=; b=EumjjNKwagikuc/nNH9dwNlomrfvNX73clLQ9QqD/dCEQloMx6mD7M9Xk5vE6OaEuP dFJOZ/TNbwyF5dMJKn3bXsl6abkyd/kQjxFQ6BDSa08MT0o5yvAGpSTfOQ/Z1ETxNm+K yAFtKF4M+QW1CAq9ffnfggDCHd1RKHpXMai0NWkS7LkZ1TOiRdkQ/ZUF00IjUGPFbYE1 Sm7mn79UKyAKf+D0YsntEHqUg9SRsM6Iv/sB6WKywI5LfNokyeKt93ZDMnKgmivG4hoI 1DUQartVFa3ej2ELUNR5n2Zw2joJavpjltBsbgt9hYKLxRoOvqWtgERTwJxtupE7kK/H ugVw== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.151 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Gm-Message-State: AA+aEWalQRIkBNIS8it5RGPUUapn76HJK5AzmrDbZekPID0uuA8ojaya UOXfLJVusUAbBfc/2VzdVPdDCeQ0IGgD2341QqKmdxEa2ZZV8Ie7Rm5aZPoQo8NbGGLhjij61Qn vqQlc1wxX6WaaYQLHdnMxdBfwxXELCilI2zJcx37ggCJdHEJAj1u0Ib1wVbrilO9JXw== X-Received: by 2002:a17:902:9347:: with SMTP id g7-v6mr4201528plp.148.1542756128036; Tue, 20 Nov 2018 15:22:08 -0800 (PST) X-Google-Smtp-Source: AFSGD/UYsjsQBIZ7nWYhKpbv4thMwPuubGbh2GEwgSk+qA+Q1wl3mZJwyZ2eUIeiXLyMv6mwJvSM X-Received: by 2002:a17:902:9347:: with SMTP id g7-v6mr4201473plp.148.1542756126833; Tue, 20 Nov 2018 15:22:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1542756126; cv=none; d=google.com; s=arc-20160816; b=bQQALgp7jAkvuMz+7n5yqmeNxohXocKxnkWxzFWQSz87FRnGVJUUgV8OgMqYcFAKC+ LEA00Sc+YaBb08k4vdlyOF7oleSGgcRBUpNU4mVqgfOz0xpEQR5DS10c38qSb3RZpHhB vIlHnXNji1HpGELHVn8Ius6h9yp+K+WzY5IGhoJG6vO1eW5s5HgwuluJbaa/hF/q6H7i IZOLyMlwLWp0ni0FClGaRyWKI5bixd7f7P9xXsPlmsZKBdR8rZa3s3JP1xzUii1+dZSq iLiQryFU1B6X98DqbFE3uu7jvgwLmHCzRkWLq0WdoR/auhJowwrNtLBQKKg0oOwrx2rB o5ug== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from; bh=+mSO61ChHconaFDfZPZTkzvJ9XsnfWKihWo8ZoXaF20=; b=06ffJvywqA3wGesc9xr+fhDPa87g+ZtPlEmCXRIxadGbaOUirNGmdPMjXOwQ64heRa YO1P8b45i6kvhcfzB1ujtTKozi+V1FyhyTf4OgxlvaabMWMhexIO2sJEOZJyY4SgYI0T NuLX/tmQgR8HYGCVJ5Mw3RrMm8RFVKrBttn5PWkf8RUTozCHX+iLOfo7bVw/huwVLNfb els/6qPdh+Qi0z4/UqQdZVO2IuGsULN40MqCsl4diisREBpAS5cfNnW6vS0JHH3kN4BG E/V2PBKlqB+40CKJV0RvbulWoc5nLSBvpc63olRFYY0oK80oFHdVMVXU5Sa/hpzXcxLq 56Rw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.151 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from mga17.intel.com (mga17.intel.com. [192.55.52.151]) by mx.google.com with ESMTPS id o3-v6si47169979pld.329.2018.11.20.15.22.06 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 20 Nov 2018 15:22:06 -0800 (PST) Received-SPF: pass (google.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.151 as permitted sender) client-ip=192.55.52.151; Authentication-Results: mx.google.com; spf=pass (google.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.151 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmsmga107.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 20 Nov 2018 15:22:05 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.56,258,1539673200"; d="scan'208";a="251330796" Received: from rpedgeco-desk5.jf.intel.com ([10.54.75.128]) by orsmga004.jf.intel.com with ESMTP; 20 Nov 2018 15:22:05 -0800 From: Rick Edgecombe To: jeyu@kernel.org, akpm@linux-foundation.org, willy@infradead.org, tglx@linutronix.de, mingo@redhat.com, hpa@zytor.com, x86@kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, kernel-hardening@lists.openwall.com, daniel@iogearbox.net, jannh@google.com, keescook@chromium.org Cc: kristen@linux.intel.com, dave.hansen@intel.com, arjan@linux.intel.com, Rick Edgecombe Subject: [PATCH v9 RESEND 3/4] vmalloc: Add debugfs modfraginfo Date: Tue, 20 Nov 2018 15:23:11 -0800 Message-Id: <20181120232312.30037-4-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181120232312.30037-1-rick.p.edgecombe@intel.com> References: <20181120232312.30037-1-rick.p.edgecombe@intel.com> X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP Add debugfs file "modfraginfo" for providing info on module space fragmentation. This can be used for determining if loadable module randomization is causing any problems for extreme module loading situations, like huge numbers of modules or extremely large modules. Sample output when KASLR is enabled and X86_64 is configured: Largest free space: 897912 kB Total free space: 1025424 kB Allocations in backup area: 0 Sample output when just X86_64: Largest free space: 897912 kB Total free space: 1025424 kB Signed-off-by: Rick Edgecombe Reviewed-by: Kees Cook --- mm/vmalloc.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 98 insertions(+), 2 deletions(-) diff --git a/mm/vmalloc.c b/mm/vmalloc.c index b8b34d319c85..63894cb50873 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -36,6 +37,12 @@ #include #include +#ifdef CONFIG_X86 +#include +#include +#include +#endif + #include "internal.h" struct vfree_deferred { @@ -2415,7 +2422,6 @@ void free_vm_area(struct vm_struct *area) } EXPORT_SYMBOL_GPL(free_vm_area); -#ifdef CONFIG_SMP static struct vmap_area *node_to_va(struct rb_node *n) { return rb_entry_safe(n, struct vmap_area, rb_node); @@ -2463,6 +2469,7 @@ static bool pvm_find_next_prev(unsigned long end, return true; } +#ifdef CONFIG_SMP /** * pvm_determine_end - find the highest aligned address between two vmap_areas * @pnext: in/out arg for the next vmap_area @@ -2804,7 +2811,96 @@ static int __init proc_vmalloc_init(void) proc_create_seq("vmallocinfo", 0400, NULL, &vmalloc_op); return 0; } -module_init(proc_vmalloc_init); +#elif defined(CONFIG_DEBUG_FS) +static int __init proc_vmalloc_init(void) +{ + return 0; +} +#endif + +#if defined(CONFIG_DEBUG_FS) && defined(CONFIG_RANDOMIZE_FINE_MODULE) +static inline unsigned long is_in_backup(unsigned long addr) +{ + return addr >= MODULES_VADDR + get_modules_rand_len(); +} + +static int modulefraginfo_debug_show(struct seq_file *m, void *v) +{ + unsigned long last_end = MODULES_VADDR; + unsigned long total_free = 0; + unsigned long largest_free = 0; + unsigned long backup_cnt = 0; + unsigned long gap; + struct vmap_area *prev, *cur = NULL; + + spin_lock(&vmap_area_lock); + + if (!pvm_find_next_prev(MODULES_VADDR, &cur, &prev) || !cur) + goto done; + + for (; cur->va_end <= MODULES_END; cur = list_next_entry(cur, list)) { + /* Don't count areas that are marked to be lazily freed */ + if (!(cur->flags & VM_LAZY_FREE)) { + if (kaslr_mod_randomize_each_module()) + backup_cnt += is_in_backup(cur->va_start); + gap = cur->va_start - last_end; + if (gap > largest_free) + largest_free = gap; + total_free += gap; + last_end = cur->va_end; + } + + if (list_is_last(&cur->list, &vmap_area_list)) + break; + } + +done: + gap = (MODULES_END - last_end); + if (gap > largest_free) + largest_free = gap; + total_free += gap; + spin_unlock(&vmap_area_lock); + + seq_printf(m, "\tLargest free space:\t%lu kB\n", largest_free / 1024); + seq_printf(m, "\t Total free space:\t%lu kB\n", total_free / 1024); + + if (kaslr_mod_randomize_each_module()) + seq_printf(m, "Allocations in backup area:\t%lu\n", backup_cnt); + + return 0; +} + +static int proc_module_frag_debug_open(struct inode *inode, struct file *file) +{ + return single_open(file, modulefraginfo_debug_show, NULL); +} + +static const struct file_operations debug_module_frag_operations = { + .open = proc_module_frag_debug_open, + .read = seq_read, + .llseek = seq_lseek, + .release = single_release, +}; + +static void __init debug_modfrag_init(void) +{ + debugfs_create_file("modfraginfo", 0400, NULL, NULL, + &debug_module_frag_operations); +} +#elif defined(CONFIG_DEBUG_FS) || defined(CONFIG_PROC_FS) +static void __init debug_modfrag_init(void) +{ +} #endif +#if defined(CONFIG_DEBUG_FS) || defined(CONFIG_PROC_FS) +static int __init info_vmalloc_init(void) +{ + proc_vmalloc_init(); + debug_modfrag_init(); + return 0; +} + +module_init(info_vmalloc_init); +#endif