From patchwork Mon Nov 28 19:02:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Song Liu X-Patchwork-Id: 13057881 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 01B2EC433FE for ; Mon, 28 Nov 2022 19:03:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F01206B0072; Mon, 28 Nov 2022 14:03:11 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EB2836B0073; Mon, 28 Nov 2022 14:03:11 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DA3B06B0074; Mon, 28 Nov 2022 14:03:11 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id CB9BA6B0072 for ; Mon, 28 Nov 2022 14:03:11 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 9361B160C2E for ; Mon, 28 Nov 2022 19:03:11 +0000 (UTC) X-FDA: 80183773782.08.4D76A8C Received: from mx0a-00082601.pphosted.com (mx0b-00082601.pphosted.com [67.231.153.30]) by imf18.hostedemail.com (Postfix) with ESMTP id 6B7F51C0009 for ; Mon, 28 Nov 2022 19:03:09 +0000 (UTC) Received: from pps.filterd (m0089730.ppops.net [127.0.0.1]) by m0089730.ppops.net (8.17.1.19/8.17.1.19) with ESMTP id 2ASI15Ff021717 for ; Mon, 28 Nov 2022 11:03:03 -0800 Received: from mail.thefacebook.com ([163.114.132.120]) by m0089730.ppops.net (PPS) with ESMTPS id 3m3f6ej243-3 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Mon, 28 Nov 2022 11:03:03 -0800 Received: from twshared13940.35.frc1.facebook.com (2620:10d:c085:208::f) by mail.thefacebook.com (2620:10d:c085:11d::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Mon, 28 Nov 2022 11:03:01 -0800 Received: by devbig932.frc1.facebook.com (Postfix, from userid 4523) id E373F10980359; Mon, 28 Nov 2022 11:02:56 -0800 (PST) From: Song Liu To: , CC: , , , , , , , Song Liu Subject: [PATCH bpf-next v5 2/6] x86/alternative: support execmem_alloc() and execmem_free() Date: Mon, 28 Nov 2022 11:02:41 -0800 Message-ID: <20221128190245.2337461-3-song@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20221128190245.2337461-1-song@kernel.org> References: <20221128190245.2337461-1-song@kernel.org> MIME-Version: 1.0 X-FB-Internal: Safe X-Proofpoint-GUID: -snjheOxk425Bm53rNghZy_B6SpXJ9aV X-Proofpoint-ORIG-GUID: -snjheOxk425Bm53rNghZy_B6SpXJ9aV X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.219,Aquarius:18.0.895,Hydra:6.0.545,FMLib:17.11.122.1 definitions=2022-11-28_17,2022-11-28_02,2022-06-22_01 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1669662189; a=rsa-sha256; cv=none; b=Pt0CW/DAK9+wbMM2ISwTVLZkG6mrzlsgoJNRFumG7vAOu9Q5W5S32xK6esD5bHKgztqoC+ K6q0KnWlazuAgbK+ljbHj4XlyVY3hRtX5bR+tyTfFqXxoUUX6HTvfX+DhGcU0VqhjBpqZ9 kCgcw8B241QMYgDSrDieCvWWagmaiuk= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=none; spf=pass (imf18.hostedemail.com: domain of "prvs=3331966363=songliubraving@meta.com" designates 67.231.153.30 as permitted sender) smtp.mailfrom="prvs=3331966363=songliubraving@meta.com"; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=kernel.org (policy=none) ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1669662189; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Fupx6ZSJDAlIJoNyk3oeuHyVP4fRctEb99FjP+ZzJJ0=; b=4grLbjPVgUvSQn9Ql/hiCb6fhre5RwgzrH+YaD/SKeaBmb7QRKU6xINPbX6nq3QXLoAmd/ tKOXpoVM+fUEtjgsnDjEGgGPRDuWH5Tmhl9XOQD1wApBht5ue5rrtQFmVHZCwnjYL5BElJ /9hfw3bmR8T/7clBTVjL4AaE3C46YZM= X-Stat-Signature: ze8n9quuc49xjotztnshn1bjhwobaq6z X-Rspamd-Queue-Id: 6B7F51C0009 X-Rspam-User: Authentication-Results: imf18.hostedemail.com; dkim=none; spf=pass (imf18.hostedemail.com: domain of "prvs=3331966363=songliubraving@meta.com" designates 67.231.153.30 as permitted sender) smtp.mailfrom="prvs=3331966363=songliubraving@meta.com"; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=kernel.org (policy=none) X-Rspamd-Server: rspam05 X-HE-Tag: 1669662189-426996 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 arch_fill_execmem() and arch_invalidate_execmem() to support execmem_alloc. arch_fill_execmem() copies dynamic kernel text (such as BPF programs) to RO+X memory region allocated by execmem_alloc(). arch_invalidate_execmem() fills memory with 0xcc after it is released by execmem_free(). Signed-off-by: Song Liu --- arch/x86/kernel/alternative.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c index 5cadcea035e0..e38829f19a81 100644 --- a/arch/x86/kernel/alternative.c +++ b/arch/x86/kernel/alternative.c @@ -1270,6 +1270,18 @@ void *text_poke_copy(void *addr, const void *opcode, size_t len) return addr; } +void *arch_fill_execmem(void *dst, void *src, size_t len) +{ + if (text_poke_copy(dst, src, len) == NULL) + return ERR_PTR(-EINVAL); + return dst; +} + +int arch_invalidate_execmem(void *ptr, size_t len) +{ + return IS_ERR_OR_NULL(text_poke_set(ptr, 0xcc, len)); +} + /** * text_poke_set - memset into (an unused part of) RX memory * @addr: address to modify