From mboxrd@z Thu Jan 1 00:00:00 1970 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A6DA3E71083 for ; Thu, 21 Sep 2023 14:51:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-ID:Date:References :In-Reply-To:Subject:To:From:Reply-To:Cc:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=tMjsohmSRIYRfgCtzHb2nUVZ01N2O5YuK5TTw8PstPI=; b=2CaQxjSXzpVekV 536ypAQR7gXgHQIOiWMheN6GIMvgo4I/f6ZUa215bsTNAA8f5A3pkrSZJKd223oHmDvBG4nhuiU7V 8xSiw01WRym9V9fyMaagP5jgAfou6eVihK028yCCtOv30zgZvNxo0IOezCY9ayd6ObJUKCgPFoANA D2vkHtmydYKvb2vqFDnkxSDqw5XgaL/PF8hnMP5tmQlLyCuuNAOM3cGVe8VVNgqWNSCLujFduFiYf Oh/c6NQ3VREtWZtaslofDbMkSsNUtjkTUYZ3z4ZKzPya5HblY2rVO2NsGiVx4Xz4SjHmttMto+LKu 3pyNp/euR0q00KdGXjyA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qjL1G-006KTu-2R; Thu, 21 Sep 2023 14:51:06 +0000 Received: from mail-wm1-x32c.google.com ([2a00:1450:4864:20::32c]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qjL1C-006KT0-00 for linux-arm-kernel@lists.infradead.org; Thu, 21 Sep 2023 14:51:04 +0000 Received: by mail-wm1-x32c.google.com with SMTP id 5b1f17b1804b1-401da71b83cso11068205e9.2 for ; Thu, 21 Sep 2023 07:51:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1695307860; x=1695912660; darn=lists.infradead.org; h=mime-version:message-id:date:references:in-reply-to:subject:to:from :from:to:cc:subject:date:message-id:reply-to; bh=oQjWGh5FJkJl0s9pvk/+A6cLo2EhA5CUtBJB0T+VZWo=; b=ddRJgqcYVbqUc2v2lsx2dFPAE2JEkc0uBVkuFSFtdLZXyXfbaBvGuZmb8WklA2KRKc CSQ7aLdhkO8d85KlY/e+wQTfAXFw8bLmprKWdQFpHOdth6lKaJ0kOwrWIg3S/RCzAr41 Jss/1Sbpa/Xtg+CHLtkpKpc/ASzhqXo1DTPuZDotkoWhqE8fO3JLPSF9Bg+eSMqFKYAD Q5oSQgEDZd0lWjLJmIier5iVbZ6NKXUXq0mDtUNRqank2T+DkLOhmVV9qDptmTHOTy9K yxWBeR1DZEwzflrTMWRESL8Up0oigT0s0K0TyLUm2WKFlfN0COfCiEgQ8PuNht4rtpGD 0KOw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695307860; x=1695912660; h=mime-version:message-id:date:references:in-reply-to:subject:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=oQjWGh5FJkJl0s9pvk/+A6cLo2EhA5CUtBJB0T+VZWo=; b=O5JQumCJDMSNMBIZjehoPSiLuKHYuVpXM6p2YQHIa1OXvEMygGisu27+zLea8Ul/0n VCF5zY76hK64ndhVYuC0EOmny4OOU+XSGREyPTyS7Sw54KkceP1yKN0j86Fm94R1Jhwa yWa6aLhrZ0jiRVWrTeT0GUZ/IQ6oUtvgUboryv7D86UhT3XyfGqqWHORUyAn6+dssXI1 Tcb/q5XQbPEN6qPE+MXD6PpFr+p4N2ZTvhLN3z8+uhHt8hVtOB5DTwgHX0h487/nPt53 G2rHbJiCFQFaUjxFO3h2yNcNCWiOYD99UWd5uyfLrj7p3UN+s/QC8040usdFcFKAM+Xj 4H9Q== X-Gm-Message-State: AOJu0Yy7HaatyB6aM83XfWIi7H88UNiUNXDc4nK9Baa0CUCvd2lNwp6w vU5PU0QSQssLt5tgG+9utEk= X-Google-Smtp-Source: AGHT+IGf19k4uMXQxHLVePErL4etNMmx5eWqBe9n5W2Jow9QcPW8JD/yKoQlaELtpNjeFxHgRKlQ9g== X-Received: by 2002:a05:600c:2186:b0:402:f54d:745 with SMTP id e6-20020a05600c218600b00402f54d0745mr5777398wme.17.1695307859542; Thu, 21 Sep 2023 07:50:59 -0700 (PDT) Received: from localhost (54-240-197-231.amazon.com. [54.240.197.231]) by smtp.gmail.com with ESMTPSA id e20-20020a05600c219400b003fe1c332810sm4988805wme.33.2023.09.21.07.50.58 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 21 Sep 2023 07:50:59 -0700 (PDT) From: Puranjay Mohan To: Xu Kuohai , ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, catalin.marinas@arm.com, mark.rutland@arm.com, bpf@vger.kernel.org, kpsingh@kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH bpf-next v5 2/3] arm64: patching: Add aarch64_insn_set() In-Reply-To: <2095a591-8f3e-318c-a390-a43a653ce6d5@huaweicloud.com> References: <20230908144320.2474-1-puranjay12@gmail.com> <20230908144320.2474-3-puranjay12@gmail.com> <2095a591-8f3e-318c-a390-a43a653ce6d5@huaweicloud.com> Date: Thu, 21 Sep 2023 14:50:55 +0000 Message-ID: MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230921_075102_041254_BCF1DB9D X-CRM114-Status: GOOD ( 21.73 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Xu Kuohai writes: > On 9/8/2023 10:43 PM, Puranjay Mohan wrote: >> The BPF JIT needs to write invalid instructions to RX regions of memory >> to invalidate removed BPF programs. This needs a function like memset() >> that can work with RX memory. >> >> Implement aarch64_insn_set() which is similar to text_poke_set() of x86. >> >> Signed-off-by: Puranjay Mohan >> --- >> arch/arm64/include/asm/patching.h | 1 + >> arch/arm64/kernel/patching.c | 40 +++++++++++++++++++++++++++++++ >> 2 files changed, 41 insertions(+) >> >> diff --git a/arch/arm64/include/asm/patching.h b/arch/arm64/include/asm/patching.h >> index f78a0409cbdb..551933338739 100644 >> --- a/arch/arm64/include/asm/patching.h >> +++ b/arch/arm64/include/asm/patching.h >> @@ -8,6 +8,7 @@ int aarch64_insn_read(void *addr, u32 *insnp); >> int aarch64_insn_write(void *addr, u32 insn); >> >> int aarch64_insn_write_literal_u64(void *addr, u64 val); >> +int aarch64_insn_set(void *dst, const u32 insn, size_t len); >> void *aarch64_insn_copy(void *dst, const void *src, size_t len); >> >> int aarch64_insn_patch_text_nosync(void *addr, u32 insn); >> diff --git a/arch/arm64/kernel/patching.c b/arch/arm64/kernel/patching.c >> index 243d6ae8d2d8..63d9e0e77806 100644 >> --- a/arch/arm64/kernel/patching.c >> +++ b/arch/arm64/kernel/patching.c >> @@ -146,6 +146,46 @@ noinstr void *aarch64_insn_copy(void *dst, const void *src, size_t len) >> return dst; >> } >> >> +/** >> + * aarch64_insn_set - memset for RX memory regions. >> + * @dst: address to modify >> + * @c: value to set > > insn Thanks for catching. >> + * @len: length of memory region. >> + * >> + * Useful for JITs to fill regions of RX memory with illegal instructions. >> + */ >> +noinstr int aarch64_insn_set(void *dst, const u32 insn, size_t len) > > const is unnecessary > Will remove in next version. >> +{ >> + unsigned long flags; >> + size_t patched = 0; >> + size_t size; >> + void *waddr; >> + void *ptr; >> + >> + /* A64 instructions must be word aligned */ >> + if ((uintptr_t)dst & 0x3) >> + return -EINVAL; >> + >> + raw_spin_lock_irqsave(&patch_lock, flags); >> + >> + while (patched < len) { >> + ptr = dst + patched; >> + size = min_t(size_t, PAGE_SIZE - offset_in_page(ptr), >> + len - patched); >> + >> + waddr = patch_map(ptr, FIX_TEXT_POKE0); >> + memset32(waddr, insn, size / 4); >> + patch_unmap(FIX_TEXT_POKE0); >> + >> + patched += size; >> + } >> + raw_spin_unlock_irqrestore(&patch_lock, flags); >> + >> + caches_clean_inval_pou((uintptr_t)dst, (uintptr_t)dst + len); >> + >> + return 0; >> +} >> + > > this function shares most of the code with aarch64_insn_copy(), how about > extracting the shared code to a separate function? I was thinking of writing it like the text_poke api of x86. Where you can provide a function as an argument to work on a memory area. Essentially, it will look like: typedef int text_poke_f(void *dst, const void *src, size_t len); static void *aarch64_insn_poke(text_poke_f func, void *addr, const void *src, size_t len) We can call this function with a wrapper of `copy_to_kernel_nofault` for copy and with a wrapper of memset32 for setting. Do you think this is a good approach? > >> int __kprobes aarch64_insn_patch_text_nosync(void *addr, u32 insn) >> { >> u32 *tp = addr; Thanks, Puranjay _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel