Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp304110pxb; Mon, 7 Feb 2022 11:44:16 -0800 (PST) X-Google-Smtp-Source: ABdhPJx0kDpLOMn+VroEuyTeCAqTEwjPaZJWE35BdNuFIa2ILgbErRlfmYHg/MioJNJzSKn3G+2Z X-Received: by 2002:a17:903:32cb:: with SMTP id i11mr1138071plr.37.1644263056631; Mon, 07 Feb 2022 11:44:16 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644263056; cv=none; d=google.com; s=arc-20160816; b=AG4LNwvZKk0gubmU+W9oP/Jw4c8Px1fWy+ZCbgaXQ3DM7MhG28q+TLsccaMsVRjVzt g5hXg+aBCS2CDgM232dnEEkNmMolLud2iYa51KMj9OcMqbgOnbuYTbzOSa5Aglji/DIM BSDPaIzhzrgzxE0lsqI/BeXQfLmveaaPvoxi8FQ8cI5apFl/mtHG8AmZmAIIIwTDj06S WADGKlqjqNKunzy+j09CSRm3Wvpwd5fcjVxDyBHZffRrJ/eSddH2c6RRora3ZrOu9PTU WRFEITVFlxo5QKX8S6KpZlvMwrdKjFgkJ0F7+2mehbmGuBp4GL9DJ/qVk7AdWATO9mVg z5iA== 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=rEMcc9GQsmaicDr3fwHlJmgI6HaxUUbccDuTZWlEktQ=; b=kmTtz1iZ1F7oCEHrt3ITDbcYXuNHq7voWV4B7nkMnY553ZanwA77Svr5JrigIUw7k+ BN7106KsCM4Ssa64aB1lXGzB/I/UnJlhAUbpQj+j1TD99veWKzElHKjjz3U9aHNErTF9 pXos4Pi06NAeWIVCiwZDKOaA+z4TY+XZGHo9WUh1PdnFupG8rkCrQgPB0oo/3oWBGor3 T+gqvT4n1OFKPwK83RDnkTdZoX3Q6BLJo5Zv0wTTyLFSENAxwK2ES2XgdXjh3cSpdBF+ lBrPJkhfyk3mJ9QMiFZcVyo9V5NDqqCNBLWh+34fPQ3CmO0MQkKullUCCByz0LFLwJRy e4lg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p20si184700pfo.106.2022.02.07.11.44.04; Mon, 07 Feb 2022 11:44:16 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S231514AbiBDTBd convert rfc822-to-8bit (ORCPT + 99 others); Fri, 4 Feb 2022 14:01:33 -0500 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:6804 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S231397AbiBDTBU (ORCPT ); Fri, 4 Feb 2022 14:01:20 -0500 Received: from pps.filterd (m0089730.ppops.net [127.0.0.1]) by m0089730.ppops.net (8.16.1.2/8.16.1.2) with ESMTP id 214IissQ008485 for ; Fri, 4 Feb 2022 11:01:20 -0800 Received: from maileast.thefacebook.com ([163.114.130.16]) by m0089730.ppops.net (PPS) with ESMTPS id 3e0utmmh6y-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Fri, 04 Feb 2022 11:01:20 -0800 Received: from twshared7634.08.ash8.facebook.com (2620:10d:c0a8:1b::d) by mail.thefacebook.com (2620:10d:c0a8:82::f) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.21; Fri, 4 Feb 2022 11:01:19 -0800 Received: by devbig006.ftw2.facebook.com (Postfix, from userid 4523) id ADC4F296C577D; Fri, 4 Feb 2022 10:58:04 -0800 (PST) From: Song Liu To: , , CC: , , , , , , , Song Liu Subject: [PATCH v9 bpf-next 5/9] x86/alternative: introduce text_poke_copy Date: Fri, 4 Feb 2022 10:57:38 -0800 Message-ID: <20220204185742.271030-6-song@kernel.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220204185742.271030-1-song@kernel.org> References: <20220204185742.271030-1-song@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8BIT X-FB-Internal: Safe Content-Type: text/plain X-Proofpoint-ORIG-GUID: IHBGZ5hG9UAVfMxJFIp3Xhb1O3ok7I3Z X-Proofpoint-GUID: IHBGZ5hG9UAVfMxJFIp3Xhb1O3ok7I3Z 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-02-04_07,2022-02-03_01,2021-12-02_01 X-Proofpoint-Spam-Details: rule=fb_outbound_notspam policy=fb_outbound score=0 suspectscore=0 priorityscore=1501 impostorscore=0 malwarescore=0 bulkscore=0 clxscore=1015 mlxlogscore=385 phishscore=0 mlxscore=0 lowpriorityscore=0 adultscore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2201110000 definitions=main-2202040104 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. Acked-by: Peter Zijlstra (Intel) Signed-off-by: Song Liu --- arch/x86/include/asm/text-patching.h | 1 + arch/x86/kernel/alternative.c | 34 ++++++++++++++++++++++++++++ 2 files changed, 35 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 5007c3ffe96f..018b61febf0e 100644 --- a/arch/x86/kernel/alternative.c +++ b/arch/x86/kernel/alternative.c @@ -1102,6 +1102,40 @@ 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; + + mutex_lock(&text_mutex); + 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; + } + mutex_unlock(&text_mutex); + return addr; +} + static void do_sync_core(void *info) { sync_core(); -- 2.30.2