From patchwork Fri Jul 24 05:04:57 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jarkko Sakkinen X-Patchwork-Id: 11682033 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 0E543138C for ; Fri, 24 Jul 2020 05:06:52 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id D56E120737 for ; Fri, 24 Jul 2020 05:06:51 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D56E120737 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=linux.intel.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 207D88D0021; Fri, 24 Jul 2020 01:06:51 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 190588D0007; Fri, 24 Jul 2020 01:06:51 -0400 (EDT) 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 057A08D0021; Fri, 24 Jul 2020 01:06:50 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0071.hostedemail.com [216.40.44.71]) by kanga.kvack.org (Postfix) with ESMTP id DF44D8D0007 for ; Fri, 24 Jul 2020 01:06:50 -0400 (EDT) Received: from smtpin12.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id AC37422C09 for ; Fri, 24 Jul 2020 05:06:50 +0000 (UTC) X-FDA: 77071784580.12.root11_2a1322c26f44 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin12.hostedemail.com (Postfix) with ESMTP id 6CD55180454FA for ; Fri, 24 Jul 2020 05:05:34 +0000 (UTC) X-Spam-Summary: 1,0,0,,d41d8cd98f00b204,jarkko.sakkinen@linux.intel.com,,RULES_HIT:30012:30054:30067:30069:30090,0,RBL:192.55.52.43:@linux.intel.com:.lbl8.mailshell.net-64.95.201.95 62.18.0.100;04ygyy89u4ja798c93tsucxmdfkhjyph37hjkxuajwkfekptanjuw98b4gd186i.wdzutx3dt4dz8snifisk533wkkq41gq43ntki7fpwhbixjc5m6tezyjytwjsc4j.n-lbl8.mailshell.net-223.238.255.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fn,MSBL:0,DNSBL:none,Custom_rules:0:0:0,LFtime:25,LUA_SUMMARY:none X-HE-Tag: root11_2a1322c26f44 X-Filterd-Recvd-Size: 4695 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by imf31.hostedemail.com (Postfix) with ESMTP for ; Fri, 24 Jul 2020 05:05:33 +0000 (UTC) IronPort-SDR: 8YsTI7mv7KpFMTCIC63Dd2C8MwVoTShXVBxFC9TM/QF6C8PCBFStbhuZ7Ljfo5i1Jnczn1OAD+ hXoiQ/+gx0Tg== X-IronPort-AV: E=McAfee;i="6000,8403,9691"; a="235538567" X-IronPort-AV: E=Sophos;i="5.75,389,1589266800"; d="scan'208";a="235538567" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Jul 2020 22:05:31 -0700 IronPort-SDR: FxIvEMr0VQstUo688TaoH7q9kyNYHCyZJgcCSuv1J8SKcwm2cz5knGFhZivT5JLVpbetc4YIXy liasXZaTbPdA== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.75,389,1589266800"; d="scan'208";a="302553164" Received: from itopinsk-mobl1.ccr.corp.intel.com (HELO localhost) ([10.249.36.179]) by orsmga002.jf.intel.com with ESMTP; 23 Jul 2020 22:05:24 -0700 From: Jarkko Sakkinen To: linux-kernel@vger.kernel.org Cc: Jarkko Sakkinen , linux-mm@kvack.org, Masami Hiramatsu , Andi Kleen , Peter Zijlstra , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org (maintainer:X86 ARCHITECTURE (32-BIT AND 64-BIT)), "H. Peter Anvin" , Andy Lutomirski , "Paul E. McKenney" , Nayna Jain , Omar Sandoval , Brian Gerst , Marco Elver , Babu Moger Subject: [PATCH v5 3/6] arch/x86: Implement text_alloc() and text_free() Date: Fri, 24 Jul 2020 08:04:57 +0300 Message-Id: <20200724050501.1723315-4-jarkko.sakkinen@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200724050501.1723315-1-jarkko.sakkinen@linux.intel.com> References: <20200724050501.1723315-1-jarkko.sakkinen@linux.intel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 6CD55180454FA X-Spamd-Result: default: False [0.00 / 100.00] X-Rspamd-Server: rspam03 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: Implement text_alloc() and text_free() with with the vmalloc API. These can be used to allocate pages for trampoline code without relying on the module loader code. Cc: linux-mm@kvack.org Cc: Masami Hiramatsu Cc: Andi Kleen Suggested-by: Peter Zijlstra Signed-off-by: Jarkko Sakkinen --- arch/x86/Kconfig | 3 +++ arch/x86/kernel/Makefile | 1 + arch/x86/kernel/text_alloc.c | 41 ++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 arch/x86/kernel/text_alloc.c diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig index 0dea7fdd7a00..a4ee5d1300f6 100644 --- a/arch/x86/Kconfig +++ b/arch/x86/Kconfig @@ -2035,6 +2035,9 @@ config KEXEC_FILE config ARCH_HAS_KEXEC_PURGATORY def_bool KEXEC_FILE +config ARCH_HAS_TEXT_ALLOC + def_bool y + config KEXEC_SIG bool "Verify kernel signature during kexec_file_load() syscall" depends on KEXEC_FILE diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile index e77261db2391..fa1415424ae3 100644 --- a/arch/x86/kernel/Makefile +++ b/arch/x86/kernel/Makefile @@ -68,6 +68,7 @@ obj-y += tsc.o tsc_msr.o io_delay.o rtc.o obj-y += pci-iommu_table.o obj-y += resource.o obj-y += irqflags.o +obj-y += text_alloc.o obj-y += process.o obj-y += fpu/ diff --git a/arch/x86/kernel/text_alloc.c b/arch/x86/kernel/text_alloc.c new file mode 100644 index 000000000000..f31c2d82c258 --- /dev/null +++ b/arch/x86/kernel/text_alloc.c @@ -0,0 +1,41 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Kernel module help for x86. + * Copyright (C) 2001 Rusty Russell. + */ + +#include +#include +#include +#include +#include + +void *text_alloc(unsigned long size) +{ + void *p; + + if (PAGE_ALIGN(size) > MODULES_LEN) + return NULL; + + p = __vmalloc_node_range(size, MODULE_ALIGN, MODULES_VADDR, MODULES_END, + GFP_KERNEL, PAGE_KERNEL, 0, NUMA_NO_NODE, + __builtin_return_address(0)); + + if (p && (kasan_module_alloc(p, size) < 0)) { + vfree(p); + return NULL; + } + + return p; +} + +void text_free(void *region) +{ + /* + * This memory may be RO, and freeing RO memory in an interrupt is not + * supported by vmalloc. + */ + lockdep_assert_irqs_enabled(); + + vfree(region); +}