From patchwork Thu Nov 17 01:06:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Song Liu X-Patchwork-Id: 13045998 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 08689C433FE for ; Thu, 17 Nov 2022 01:09:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9C2798E0002; Wed, 16 Nov 2022 20:09:31 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 972876B0081; Wed, 16 Nov 2022 20:09:31 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 861BA8E0002; Wed, 16 Nov 2022 20:09:31 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 753236B0080 for ; Wed, 16 Nov 2022 20:09:31 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 528FAC05A6 for ; Thu, 17 Nov 2022 01:09:31 +0000 (UTC) X-FDA: 80141151342.30.3C74473 Received: from mx0a-00082601.pphosted.com (mx0a-00082601.pphosted.com [67.231.145.42]) by imf05.hostedemail.com (Postfix) with ESMTP id CA2D8100004 for ; Thu, 17 Nov 2022 01:09:30 +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 2AH0btXd012130 for ; Wed, 16 Nov 2022 17:09:29 -0800 Received: from maileast.thefacebook.com ([163.114.130.16]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 3kw0bs6448-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Wed, 16 Nov 2022 17:09:29 -0800 Received: from twshared24004.14.frc2.facebook.com (2620:10d:c0a8:1b::d) by mail.thefacebook.com (2620:10d:c0a8:82::d) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Wed, 16 Nov 2022 17:09:27 -0800 Received: by devbig932.frc1.facebook.com (Postfix, from userid 4523) id D889EFE65C30; Wed, 16 Nov 2022 17:06:30 -0800 (PST) From: Song Liu To: , CC: , , , , , , , , Song Liu Subject: [PATCH bpf-next v3 2/6] x86/alternative: support execmem_alloc() and execmem_free() Date: Wed, 16 Nov 2022 17:06:17 -0800 Message-ID: <20221117010621.1891711-3-song@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20221117010621.1891711-1-song@kernel.org> References: <20221117010621.1891711-1-song@kernel.org> MIME-Version: 1.0 X-FB-Internal: Safe X-Proofpoint-GUID: V4mTRmEB9YMtwhhrZo7fSfP3dDZkH08d X-Proofpoint-ORIG-GUID: V4mTRmEB9YMtwhhrZo7fSfP3dDZkH08d 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-16_03,2022-11-16_01,2022-06-22_01 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1668647371; a=rsa-sha256; cv=none; b=zvNCWf7Sm2VzZzpMGawwj+ORPMh8oApoa8nPbqAgF8w+mV8YGkT5Sa/neiEmIqHlYGSwok eqvg4pmgQ5taq8xFB+vMbIysXZBYMA/4m3PmizY4UEsdvb4KSwT3v9Zh0wgU0hAB7YeuYX qedsEJLZPpb4HzVfj9v7sUksYnW9YdI= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=none; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=kernel.org (policy=none); spf=pass (imf05.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-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1668647371; 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=5bwqC1kfPxVaSeYdeWZ79zNOewYw8ORryytHlLSbLn5zINZe662D0cOlbfj1OFc6H0IuY2 P2Vys2eDwxw4jV0DyiM5iOSQ11NwAUO2neX23tgmjCzgRsg52k9zRaS9vDyiepXRldMb+J 5du4fGjs5xFj5oCh3HSSFEpk+IwYBUQ= Authentication-Results: imf05.hostedemail.com; dkim=none; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=kernel.org (policy=none); spf=pass (imf05.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-Rspam-User: X-Stat-Signature: w1ocfieqhgmkxcts93pi4kwdj7o47gee X-Rspamd-Queue-Id: CA2D8100004 X-Rspamd-Server: rspam11 X-HE-Tag: 1668647370-651889 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