From patchwork Mon Nov 7 22:39: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: 13035365 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 5F895C433FE for ; Mon, 7 Nov 2022 22:41:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DFDFA6B0073; Mon, 7 Nov 2022 17:41:39 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id DAE196B0074; Mon, 7 Nov 2022 17:41:39 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C4E976B0075; Mon, 7 Nov 2022 17:41:39 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id B3AE66B0073 for ; Mon, 7 Nov 2022 17:41:39 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 7E4051403AE for ; Mon, 7 Nov 2022 22:41:39 +0000 (UTC) X-FDA: 80108119518.03.E2CC86F Received: from mx0a-00082601.pphosted.com (mx0b-00082601.pphosted.com [67.231.153.30]) by imf12.hostedemail.com (Postfix) with ESMTP id 2E8C840007 for ; Mon, 7 Nov 2022 22:41:38 +0000 (UTC) Received: from pps.filterd (m0001303.ppops.net [127.0.0.1]) by m0001303.ppops.net (8.17.1.5/8.17.1.5) with ESMTP id 2A7LKovJ024091 for ; Mon, 7 Nov 2022 14:41:38 -0800 Received: from maileast.thefacebook.com ([163.114.130.16]) by m0001303.ppops.net (PPS) with ESMTPS id 3knkgvkqhs-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Mon, 07 Nov 2022 14:41:38 -0800 Received: from twshared15216.17.frc2.facebook.com (2620:10d:c0a8:1b::d) by mail.thefacebook.com (2620:10d:c0a8:83::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Mon, 7 Nov 2022 14:41:37 -0800 Received: by devbig932.frc1.facebook.com (Postfix, from userid 4523) id A7435F6B36E1; Mon, 7 Nov 2022 14:39:27 -0800 (PST) From: Song Liu To: , CC: , , , , , , , , Song Liu Subject: [PATCH bpf-next v2 2/5] x86/alternative: support execmem_alloc() and execmem_free() Date: Mon, 7 Nov 2022 14:39:18 -0800 Message-ID: <20221107223921.3451913-3-song@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20221107223921.3451913-1-song@kernel.org> References: <20221107223921.3451913-1-song@kernel.org> MIME-Version: 1.0 X-FB-Internal: Safe X-Proofpoint-ORIG-GUID: 0eduXI6i2dVOsd6iwJs_QG7hLzpVRDFw X-Proofpoint-GUID: 0eduXI6i2dVOsd6iwJs_QG7hLzpVRDFw 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-07_11,2022-11-07_02,2022-06-22_01 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1667860899; 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=WqLm4341yzvmeOcwk3aZS4KVF3b91xF0W8DgT7/+uUxiCg00/FWsLuvisiKThHSKq0saFN 8UaGwf+D954yB70fifCpWlNC0+g2xT8yDze4IPKkCzKjC2oSmVquZMh4JuMdn1qGNNATQ7 HiSWVeiyan8ceHZ2FtaSat3Rc76f1sc= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=none; spf=pass (imf12.hostedemail.com: domain of "prvs=2310266901=songliubraving@meta.com" designates 67.231.153.30 as permitted sender) smtp.mailfrom="prvs=2310266901=songliubraving@meta.com"; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=kernel.org (policy=none) ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1667860899; a=rsa-sha256; cv=none; b=q6ea/4IjiXG73UV83j/DT8q+TYOv9Si+dg6qbQYBBoSG0TNoTUQ1SZ2vFX8TM7Wwysrhu5 xvLNazOjSf1RbpkfxUSm3IeHRh65OFl1BuQjsYSUoDd2QfYTcd8Yrvwa6SZytRNdcUOZS9 aS0dWF/7AtqcI5mLaO5MTbXB3D0DTmk= X-Rspam-User: X-Stat-Signature: wb71tnjfm66tp3yjqad5q6qaw1rjs6q1 X-Rspamd-Queue-Id: 2E8C840007 Authentication-Results: imf12.hostedemail.com; dkim=none; spf=pass (imf12.hostedemail.com: domain of "prvs=2310266901=songliubraving@meta.com" designates 67.231.153.30 as permitted sender) smtp.mailfrom="prvs=2310266901=songliubraving@meta.com"; dmarc=fail reason="SPF not aligned (relaxed), No valid DKIM" header.from=kernel.org (policy=none) X-Rspamd-Server: rspam07 X-HE-Tag: 1667860898-601619 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