Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp1293107pxb; Fri, 21 Jan 2022 14:34:40 -0800 (PST) X-Google-Smtp-Source: ABdhPJyClMci2j4i3PG+fA6NfUmXC00d01nJmSjp43SMdhyxhKLtTdM5CpUyFTHeoQ1KSUBJQ1Lu X-Received: by 2002:a17:902:8a89:b0:149:a833:af2a with SMTP id p9-20020a1709028a8900b00149a833af2amr5679232plo.153.1642804480413; Fri, 21 Jan 2022 14:34:40 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1642804480; cv=none; d=google.com; s=arc-20160816; b=GMsppKN6Wo3rImIY0MnX3tToa6su86REAxruZPdETNVeyKC77RiY8kob10Y0pYtk3Q MlIkbD88m8uO4PGwcit84JUxktVTxZfLlFMwtN+6EjnSDfvrfN8rqCimc0zq3nWEiFjZ oEZZhUepcl58yD2fI2oEY/Penyz8Ypl2CEOzdsW5kATxBJKlluVgtVfPC5jWIpA1dWyc Qultdc+blyILwSujiV7hlIbooJ4EAkNuMSqYA+Fe3sEKD7n3HlY5D7rrB39qAdqEYzSX vLMrj/EmovFWaHnvKGlTdoZe1ycFaqpwjaLl5Ozkor33VPNdNJ2K/lwRxpV5Kh1l4NWA YjvQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=KZGh/vO8iNPIjm9lnAo/seS/Mz15zK1Wb6BFHiS9ldg=; b=PRqvQE1fhyBepQPAB1eAHvJ84Xv/bMEw6WiEIZjWWfEMZb5lqUU3SGsJ8vzNIIxuK1 KeVGLy0uilLJzKu46HtVRpsWTvrOu8Ocx3RKNx7vU2bVOh0hjbFq6J3aQDlugx3Xqip7 OgpZeytQBZ/6n8IlU0dlD0UGd/soqJtZ9p19/6V3q5TXBZfIH5LpxEe++xOEe8TIAF+u vF/aM5hHN/STOzFDKboceLhG30IepewPe8hsiytt6UXkqcv1H4/vBPHyt4qcbWqct3jE Q+Jedrz7facwdWXkjTFVLHI4kp59sjIoMjtDNiX7treShmUOHCm8GvgQEriuVrdHjDgW SV1Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id bf20si1073266pgb.474.2022.01.21.14.34.24; Fri, 21 Jan 2022 14:34:40 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377609AbiATTOh convert rfc822-to-8bit (ORCPT + 99 others); Thu, 20 Jan 2022 14:14:37 -0500 Received: from mx0a-00082601.pphosted.com ([67.231.145.42]:57860 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377505AbiATTNh (ORCPT ); Thu, 20 Jan 2022 14:13:37 -0500 Received: from pps.filterd (m0044012.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.1.2/8.16.1.2) with ESMTP id 20KHwWFJ013570 for ; Thu, 20 Jan 2022 11:13:37 -0800 Received: from maileast.thefacebook.com ([163.114.130.16]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 3dpyswcse1-5 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Thu, 20 Jan 2022 11:13:37 -0800 Received: from twshared3115.02.ash8.facebook.com (2620:10d:c0a8:1b::d) by mail.thefacebook.com (2620:10d:c0a8:83::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.20; Thu, 20 Jan 2022 11:13:34 -0800 Received: by devbig006.ftw2.facebook.com (Postfix, from userid 4523) id 15CA2283245F9; Thu, 20 Jan 2022 11:13:25 -0800 (PST) From: Song Liu To: , , CC: , , , , , , Song Liu Subject: [PATCH v5 bpf-next 5/7] x86/alternative: introduce text_poke_copy Date: Thu, 20 Jan 2022 11:13:03 -0800 Message-ID: <20220120191306.1801459-6-song@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220120191306.1801459-1-song@kernel.org> References: <20220120191306.1801459-1-song@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8BIT X-FB-Internal: Safe Content-Type: text/plain X-Proofpoint-GUID: OI5EHmaDoWf9j3r-wAkSAaQI5vK4eFqU X-Proofpoint-ORIG-GUID: OI5EHmaDoWf9j3r-wAkSAaQI5vK4eFqU X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.816,Hydra:6.0.425,FMLib:17.11.62.513 definitions=2022-01-20_07,2022-01-20_01,2021-12-02_01 X-Proofpoint-Spam-Details: rule=fb_outbound_notspam policy=fb_outbound score=0 bulkscore=0 mlxlogscore=375 priorityscore=1501 impostorscore=0 phishscore=0 lowpriorityscore=0 adultscore=0 malwarescore=0 suspectscore=0 spamscore=0 clxscore=1015 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2110150000 definitions=main-2201200097 X-FB-Internal: deliver Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This will be used by BPF jit compiler to dump JITed binary to a RX huge page, and thus allow multiple BPF programs sharing the a huge (2MB) page. Signed-off-by: Song Liu --- arch/x86/include/asm/text-patching.h | 1 + arch/x86/kernel/alternative.c | 32 ++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/arch/x86/include/asm/text-patching.h b/arch/x86/include/asm/text-patching.h index b7421780e4e9..4cc18ba1b75e 100644 --- a/arch/x86/include/asm/text-patching.h +++ b/arch/x86/include/asm/text-patching.h @@ -44,6 +44,7 @@ extern void text_poke_early(void *addr, const void *opcode, size_t len); extern void *text_poke(void *addr, const void *opcode, size_t len); extern void text_poke_sync(void); extern void *text_poke_kgdb(void *addr, const void *opcode, size_t len); +extern void *text_poke_copy(void *addr, const void *opcode, size_t len); extern int poke_int3_handler(struct pt_regs *regs); extern void text_poke_bp(void *addr, const void *opcode, size_t len, const void *emulate); diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c index 23fb4d51a5da..903a415c19fa 100644 --- a/arch/x86/kernel/alternative.c +++ b/arch/x86/kernel/alternative.c @@ -1102,6 +1102,38 @@ void *text_poke_kgdb(void *addr, const void *opcode, size_t len) return __text_poke(addr, opcode, len); } +/** + * text_poke_copy - Copy instructions into (an unused part of) RX memory + * @addr: address to modify + * @opcode: source of the copy + * @len: length to copy, could be more than 2x PAGE_SIZE + * + * Not safe against concurrent execution; useful for JITs to dump + * new code blocks into unused regions of RX memory. Can be used in + * conjunction with synchronize_rcu_tasks() to wait for existing + * execution to quiesce after having made sure no existing functions + * pointers are live. + */ +void *text_poke_copy(void *addr, const void *opcode, size_t len) +{ + unsigned long start = (unsigned long)addr; + size_t patched = 0; + + if (WARN_ON_ONCE(core_kernel_text(start))) + return NULL; + + while (patched < len) { + unsigned long ptr = start + patched; + size_t s; + + s = min_t(size_t, PAGE_SIZE * 2 - offset_in_page(ptr), len - patched); + + __text_poke((void *)ptr, opcode + patched, s); + patched += s; + } + return addr; +} + static void do_sync_core(void *info) { sync_core(); -- 2.30.2