From patchwork Thu Nov 17 20:23:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Song Liu X-Patchwork-Id: 13047245 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 C0398C433FE for ; Thu, 17 Nov 2022 20:23:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5CD736B007D; Thu, 17 Nov 2022 15:23:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 501C66B007E; Thu, 17 Nov 2022 15:23:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 32EF98E0001; Thu, 17 Nov 2022 15:23:45 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 115246B007E for ; Thu, 17 Nov 2022 15:23:45 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id DAC301C6360 for ; Thu, 17 Nov 2022 20:23:44 +0000 (UTC) X-FDA: 80144059968.28.B7E53F2 Received: from mx0a-00082601.pphosted.com (mx0a-00082601.pphosted.com [67.231.145.42]) by imf02.hostedemail.com (Postfix) with ESMTP id 7421D8000B for ; Thu, 17 Nov 2022 20:23:43 +0000 (UTC) Received: from pps.filterd (m0109334.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 2AHGgkjQ013222 for ; Thu, 17 Nov 2022 12:23:42 -0800 Received: from mail.thefacebook.com ([163.114.132.120]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 3kwqbnamy2-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Thu, 17 Nov 2022 12:23:42 -0800 Received: from twshared21592.39.frc1.facebook.com (2620:10d:c085:208::11) by mail.thefacebook.com (2620:10d:c085:21d::4) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Thu, 17 Nov 2022 12:23:41 -0800 Received: by devbig932.frc1.facebook.com (Postfix, from userid 4523) id B5B91FF17571; Thu, 17 Nov 2022 12:23:29 -0800 (PST) From: Song Liu To: , CC: , , , , , , , Song Liu Subject: [PATCH bpf-next v4 2/6] x86/alternative: support execmem_alloc() and execmem_free() Date: Thu, 17 Nov 2022 12:23:18 -0800 Message-ID: <20221117202322.944661-3-song@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20221117202322.944661-1-song@kernel.org> References: <20221117202322.944661-1-song@kernel.org> MIME-Version: 1.0 X-FB-Internal: Safe X-Proofpoint-GUID: g5T5u7K-goTXt6y6e_ydVtKn1VrHiG98 X-Proofpoint-ORIG-GUID: g5T5u7K-goTXt6y6e_ydVtKn1VrHiG98 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-17_06,2022-11-17_01,2022-06-22_01 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1668716623; 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=JIa+IMtDh4ENxqDxlsjUo9ezIpnQOcHr9tIng7kyPv59d/EHZZbRHhfsGMqUxpEOxj/VwL KaA0Bq2nXxKJKBHppNIyVIPDVamvnTl1WASyv4WYlHp3QyXblGzi8aWfLRv+nsH8skSpd4 2b+B8hImEmnITTRh3axLtspk9ECReyo= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=none; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=kernel.org (policy=none); spf=pass (imf02.hostedemail.com: domain of "prvs=232048c045=songliubraving@meta.com" designates 67.231.145.42 as permitted sender) smtp.mailfrom="prvs=232048c045=songliubraving@meta.com" ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1668716623; a=rsa-sha256; cv=none; b=4pXfolFOQYnVAnyo6Wl8E4INSwlW2gYlStkRs8Ke3hGbL0ilyN2Ht41eOIEHXzX5m/JXs4 xthRULU2GpqD2M+HLEDSpYbR4sJbbhQ2DBCCnOYo2hg2IBhtJFI2gRaCtv8cvRldqWC5n8 esD8j2vl+eSJo8GxBiWIonFRkyhiwS8= X-Rspam-User: X-Stat-Signature: k36b5p13suxiwgwzdzzne8ab67t38wt1 X-Rspamd-Queue-Id: 7421D8000B Authentication-Results: imf02.hostedemail.com; dkim=none; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=kernel.org (policy=none); spf=pass (imf02.hostedemail.com: domain of "prvs=232048c045=songliubraving@meta.com" designates 67.231.145.42 as permitted sender) smtp.mailfrom="prvs=232048c045=songliubraving@meta.com" X-Rspamd-Server: rspam07 X-HE-Tag: 1668716623-951011 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