From patchwork Fri Sep 8 14:43:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Puranjay Mohan X-Patchwork-Id: 13377565 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 B350BEE8010 for ; Fri, 8 Sep 2023 14:44:00 +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:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=jaCzCq0iRAwccoKVwHJIVh+o+kgw5X9in3oJbVFEUi0=; b=QW/neDFzZCF5FR nfgBTsAirGakIBC23qbm28S2PQMToQASxceBQv/5aS25YSm8vKCnV+xksbNkpEFUBg8Dhbi7zskt+ Tggqc7s9PTM6v5/WWqATZmQghb186c598IqkSgM2HFrPkmdRbgmoP1mV/sPxPlj+sjONDJonyh6Di j3j/ErwxI/vLOtsfKAEJW1BWp62sjncOMH+g/E5+933SIz4rKxSk+zffLSqTiHOl2WTkhwI6Hf6tX qpvqhdHRjwHx+RuwlZCjmVZuBUcSO09wVcKo20tqYV3aC66doOKoaNdZTedXGJ2mMi6Y2SmuMxtbz TBotOq0MLy1MM/zFPmug==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qechl-00DwNp-2m; Fri, 08 Sep 2023 14:43:29 +0000 Received: from mail-ej1-x62b.google.com ([2a00:1450:4864:20::62b]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qechi-00DwLH-0a for linux-arm-kernel@lists.infradead.org; Fri, 08 Sep 2023 14:43:28 +0000 Received: by mail-ej1-x62b.google.com with SMTP id a640c23a62f3a-99bcfe28909so261814866b.3 for ; Fri, 08 Sep 2023 07:43:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1694184202; x=1694789002; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=O1QyEsWglzsKtSFRl6GqbG8YUnWZHW9/8Dm/6UtUzbY=; b=H21GJSDklf7O4ZfjSfiIOp3BpmF1U2WT4Cm363HrxaSRAVzjdJQJslfQrm5JvH2rut 117etNLmmGry8xP8RlZ2r65HFMh1b2IYfRO2J3FBbSpGNy19fwWKnmURsnqdbvSEw60b LD3QFFd3NRnYhqS4YdRbpWIGDmrzanuI+BnjfW2mxHQjIj7FL8CERYGlkdSWwxG0SQ7o XHNLS+VpfmS6yACl5z8jxZLTDLaNasYyWbfyrmGgFjf7Al60nEQcWqLgrdDXNb+CZEkD YjnNywh5cspZJGzC4hyqVY+R/T0GC5Y+BlGwU2RxBvmdIMjMiRO5eClovyuNjc56wUOs 3rGA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694184202; x=1694789002; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=O1QyEsWglzsKtSFRl6GqbG8YUnWZHW9/8Dm/6UtUzbY=; b=drVDuwWD03qAJGPWl5ahqM8jSvD/yPFJmFOwmLGn4jyLIsgLLigHManHpiY+jgsH+4 1Mu6Vd/DY8kkZfOgqLnzzoM5oxiBu2OWo08ovgrqyuvxTdsqmY8aqlNSxnD9rmPeBlhR yFcuSF9ejMx9Rq7lfLdQcziz2MVTlas6SDgqUbGPkrmQhInrozC4muZZK8GL2TRkVo1E ldsOtu2BFW4vMpsQFHEXMj9V39N9uq9ZiI4UgVi1SniQ2zLrvkxQYamBVIYZnxch11oc IndEclskqHcPhFIh28n/qzhjHVYospNIVXcaFyPn1DxmBnG42mqNJHpKYnCqufDXwGBT umAQ== X-Gm-Message-State: AOJu0YxzCa8uggMeApHH3w/CMxTuiu+WeRhe4TyI7HEXjhyhggxHbr+/ JWpiN+m5rk4rcqENMT2ekW4= X-Google-Smtp-Source: AGHT+IEzWM9tlE4k/vQsSnrED1cgEwDPDtRDd6Z3U7kUSH/qSX4ARaU+ud4z+lvVsM+ZaPaKPvKraQ== X-Received: by 2002:a17:907:7759:b0:9a9:e393:8bcd with SMTP id kx25-20020a170907775900b009a9e3938bcdmr1942854ejc.5.1694184202216; Fri, 08 Sep 2023 07:43:22 -0700 (PDT) Received: from ip-172-31-30-46.eu-west-1.compute.internal (ec2-54-217-129-48.eu-west-1.compute.amazonaws.com. [54.217.129.48]) by smtp.gmail.com with ESMTPSA id lz5-20020a170906fb0500b0098e78ff1a87sm1099436ejb.120.2023.09.08.07.43.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Sep 2023 07:43:21 -0700 (PDT) From: Puranjay Mohan To: 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 Cc: puranjay12@gmail.com Subject: [PATCH bpf-next v5 1/3] arm64: patching: Add aarch64_insn_copy() Date: Fri, 8 Sep 2023 14:43:18 +0000 Message-Id: <20230908144320.2474-2-puranjay12@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230908144320.2474-1-puranjay12@gmail.com> References: <20230908144320.2474-1-puranjay12@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230908_074326_214790_22D2FC00 X-CRM114-Status: GOOD ( 16.34 ) 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: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.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. The bpf_prog_pack allocator that implements the above feature allocates a RX/RW buffer pair. The JITed code is written to the RW buffer and then this function will be used to copy the code from RW to RX buffer. Signed-off-by: Puranjay Mohan Acked-by: Song Liu --- arch/arm64/include/asm/patching.h | 1 + arch/arm64/kernel/patching.c | 41 +++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/arch/arm64/include/asm/patching.h b/arch/arm64/include/asm/patching.h index 68908b82b168..f78a0409cbdb 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); +void *aarch64_insn_copy(void *dst, const void *src, size_t len); int aarch64_insn_patch_text_nosync(void *addr, u32 insn); int aarch64_insn_patch_text(void *addrs[], u32 insns[], int cnt); diff --git a/arch/arm64/kernel/patching.c b/arch/arm64/kernel/patching.c index b4835f6d594b..243d6ae8d2d8 100644 --- a/arch/arm64/kernel/patching.c +++ b/arch/arm64/kernel/patching.c @@ -105,6 +105,47 @@ noinstr int aarch64_insn_write_literal_u64(void *addr, u64 val) return ret; } +/** + * aarch64_insn_copy - Copy instructions into (an unused part of) RX memory + * @dst: address to modify + * @src: source of the copy + * @len: length to copy + * + * Useful for JITs to dump new code blocks into unused regions of RX memory. + */ +noinstr void *aarch64_insn_copy(void *dst, const void *src, size_t len) +{ + unsigned long flags; + size_t patched = 0; + size_t size; + void *waddr; + void *ptr; + int ret; + + 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); + ret = copy_to_kernel_nofault(waddr, src + patched, size); + patch_unmap(FIX_TEXT_POKE0); + + if (ret < 0) { + raw_spin_unlock_irqrestore(&patch_lock, flags); + return NULL; + } + patched += size; + } + raw_spin_unlock_irqrestore(&patch_lock, flags); + + caches_clean_inval_pou((uintptr_t)dst, (uintptr_t)dst + len); + + return dst; +} + int __kprobes aarch64_insn_patch_text_nosync(void *addr, u32 insn) { u32 *tp = addr;