Received: by 2002:a25:e74b:0:0:0:0:0 with SMTP id e72csp1891386ybh; Thu, 23 Jul 2020 22:07:41 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxZWAiEoz2R1o63Hiv/A0WOJEkymzIgBK5d39qIoVAFWRdKijUR/Pj5nAzzt3Y15vOKO+Il X-Received: by 2002:a17:906:7743:: with SMTP id o3mr7362650ejn.224.1595567261057; Thu, 23 Jul 2020 22:07:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595567261; cv=none; d=google.com; s=arc-20160816; b=gejcyeorV4fyC/pAqlrP5jfv+cChvuFYoft1Skt8VHGwzTy8nmEm7LTLzJi+CpzLWg fNHm8RAy/NWTYfCNsA6sGSOh3H5TqNDly2YVwH0RSBPvCKFvHrLySf6vRgPcw1doO9No lrwATYQ5+V118Ov9zciCqtmGanWC0ttPyllrbZ78ext8sBHOaIYf2V71LAour2B5pWUP NsIfE9dNgNu7kPUK5zLF2Mp3OjylCgMALU6QrK2LUMf14LvPy3Y2ZN37egRAlxDkIDOE abQ//yn1zz74cuH+ecyR+o0AJDkAjIR3yp3Aqp8hwpNZnzXQSrfmr7qts8X9/sT2S24H 6bAA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :ironport-sdr:ironport-sdr; bh=4Y9V+W4GcIRLVnyVHxY8GGzZ1B73JcUvXIE5a+BXEdE=; b=Yi/Mbi2KPurgAGoLB6Q2NCr1d9ZhuspGPR6h7wWhUP4F69HtGq8mWkRo0SvYjEDjT5 P2jCM9m02284SmSs5pw0EbwXBIwxtjDPKb31Sqy1MM3PuGW1ptS1k+91TzLhdECBu9cv PmvDczKgXdMlwaOxFBZQQVMMMC6jds7ezE7DLn4ia0juQwIMFAoKo/jpRRKN9w5cYXsK 7SD95DDPUCgK5Hp/udq1XdsUupIQoCnDnz4sCSMxlF/112b0tKjHTsKiGLRa/K6jvpQF KUwoUJDdW2YgSbIyigcwTz/S3CML5ghZgvZzA1nI4CcVk0gd2dKQnmyKkI+uLLC5SuVr 5F/Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ni2si3487724ejb.480.2020.07.23.22.07.19; Thu, 23 Jul 2020 22:07:41 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726797AbgGXFGX (ORCPT + 99 others); Fri, 24 Jul 2020 01:06:23 -0400 Received: from mga01.intel.com ([192.55.52.88]:29352 "EHLO mga01.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725929AbgGXFGX (ORCPT ); Fri, 24 Jul 2020 01:06:23 -0400 IronPort-SDR: Lx0yCg9VmeFM6LOHPJfCd0N0Fm+SlZbEEpDGYw6S3ONYScEIKyG2MuCYMIQXVdL2WZNY6Jy4b4 mYWSmD8VE/Aw== X-IronPort-AV: E=McAfee;i="6000,8403,9691"; a="168791246" X-IronPort-AV: E=Sophos;i="5.75,389,1589266800"; d="scan'208";a="168791246" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 23 Jul 2020 22:06:23 -0700 IronPort-SDR: BpTQBF3s9QZc6iS4k6nuvNMFVTnO2nOQYYAfzwrnWN2LVf1K+TP6Hgz4b6S7DxVMszefEQ/ShD 2veaIvsrlLfg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.75,389,1589266800"; d="scan'208";a="432970161" Received: from itopinsk-mobl1.ccr.corp.intel.com (HELO localhost) ([10.249.36.179]) by orsmga004.jf.intel.com with ESMTP; 23 Jul 2020 22:06:15 -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" , Marco Elver , Babu Moger , Brian Gerst , Nayna Jain Subject: [PATCH v5 3/6] arch/x86: Implement text_alloc() and text_free() Date: Fri, 24 Jul 2020 08:05:50 +0300 Message-Id: <20200724050553.1724168-4-jarkko.sakkinen@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200724050553.1724168-1-jarkko.sakkinen@linux.intel.com> References: <20200724050553.1724168-1-jarkko.sakkinen@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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); +} -- 2.25.1