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: 13057874 X-Patchwork-Delegate: bpf@iogearbox.net 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B8E3EC4332F for ; Mon, 28 Nov 2022 19:03:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231818AbiK1TDG convert rfc822-to-8bit (ORCPT ); Mon, 28 Nov 2022 14:03:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34898 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231714AbiK1TDF (ORCPT ); Mon, 28 Nov 2022 14:03:05 -0500 Received: from mx0a-00082601.pphosted.com (mx0b-00082601.pphosted.com [67.231.153.30]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7575D27FE9 for ; Mon, 28 Nov 2022 11:03:04 -0800 (PST) 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 2ASI0xNv021086 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 3m3f6ej248-1 (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:108::4) 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; 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: uL49pttm3s3WmipDmfmQyGuXroIHWJ0B X-Proofpoint-ORIG-GUID: uL49pttm3s3WmipDmfmQyGuXroIHWJ0B 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 Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net 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