From patchwork Thu Jul 20 15:49:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Puranjay Mohan X-Patchwork-Id: 13320719 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 BFF40C001DC for ; Thu, 20 Jul 2023 15:49:54 +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=akqHSIMbI3KFApecjxaMYwsfPGo/fjUlpvOsue1oxNo=; b=dlUO3y5qD2NR6U kBg1jKgiYZZNcxEukcX4JPkwHGb7UzL4Z6TP782ocQT6qglcLK6lxh9UthCyvfUXGXcyqFrIchkc7 PnEErAk9/Fkuef4k/57fiFOfVFtlU2lC+DLNupLiVpcKvsVdscQoToqJ9ifFrzfuCw+sH6QCGf/p3 jC0R5kQp4SCtRS/tW66mjYBU102QfcWXCF397wUgFWkRFexfs4nWc7m0DfQW21HWyr5O9Q6ksZ0LW 9zBOM6cDzEhpbeJ6i508Hc0sKgAe8Z1kdDFBilq/X3V5LA4dZq3LOae/hII/ESF3AWNfGNq0aN/GJ me70/8A6Z1gS2U7tGXZA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qMVuZ-00BaPv-18; Thu, 20 Jul 2023 15:49:51 +0000 Received: from mail-wm1-x332.google.com ([2a00:1450:4864:20::332]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qMVuV-00BaNp-2y for linux-riscv@lists.infradead.org; Thu, 20 Jul 2023 15:49:49 +0000 Received: by mail-wm1-x332.google.com with SMTP id 5b1f17b1804b1-3fbc63c2e84so8156595e9.3 for ; Thu, 20 Jul 2023 08:49:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1689868183; x=1690472983; 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=+TdRmj0UkAm5gPWjI6OLYQN5hjGdtS3ZVtVx8EMYM4Y=; b=F7yWBNeaU2+/3BBqgAuECm5xIe9Z49EL2iGWNoPjMiiSOa6u+saOFkPGbnxYdlUe8J Hu7XI/oX+2x/xn/pyzs/tvziKsRDPzA1cWMqNj+jvEMxUec5OXy72zcN6UNx893UAPKb fdNU/3kjaN/aa401cgeZxLYyUfa1QkkTSOL6j7JnazSTAYDjlAsR4e9Bx0ubQ6t5zU2F kIAOeLUBfuoVOJaAWI5hkY8H90OjfbjdRi9NwqDkFrxHYe6dQR3Jj607PDh4Og/o2LbE lp1q3TIbOpdzUHzO4e3DphgNIGi4F4CXjEO3oZAvRDLDNlStHvyCijkV9MVIj6e4ilv0 Q8iQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689868183; x=1690472983; 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=+TdRmj0UkAm5gPWjI6OLYQN5hjGdtS3ZVtVx8EMYM4Y=; b=LMahAh7a9f6uz3m1y8c6EEbq/+kibeahRlpfP2Tx2MeVUI8kYVtoONGoVl8oF3mvw6 zNALl86uFH1JfcvL61c9ldbKTahgN0KTPYSCwNS3hgwKBcfZ5qolUjpy6QRgDggpupRK NfOvVk0on8t5wx8Jjiqz5MPk0PHVaeLtDZ0PYWpazscOL55jZmFM7yQ4TfPYGqbFHu3T hq5avC4xcV7DMtDF7W/K5FO0Hh6TheBl9CwQj2UtbH2Twugc3/jKQYedSE+tX30G3aj8 b68Z7UEcXc/q3LbPf63UWfjpzNXXN5QEf9t5VJcBIYuSaA2kKOB13a5sq6prObK1igMA 80aA== X-Gm-Message-State: ABy/qLb6kHpcD5GXNd4PqPHLrBw8CSlt+szkJjWny34rLihA2eK6WleJ rR3zrEoXLJEr88WYFyFn/2I= X-Google-Smtp-Source: APBJJlEuXMI5sektNSXxZZSfzFEf8zfF8nebOdPeEXqLNDX8t0fvGqMk9j5hKGKR3pU29ru33nGvrw== X-Received: by 2002:a05:600c:3647:b0:3f9:8c3:6805 with SMTP id y7-20020a05600c364700b003f908c36805mr4563818wmq.7.1689868183273; Thu, 20 Jul 2023 08:49:43 -0700 (PDT) Received: from ip-172-31-22-112.eu-west-1.compute.internal (ec2-34-244-51-157.eu-west-1.compute.amazonaws.com. [34.244.51.157]) by smtp.gmail.com with ESMTPSA id u6-20020a05600c00c600b003fbdd9c72aasm1471021wmm.21.2023.07.20.08.49.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 20 Jul 2023 08:49:43 -0700 (PDT) From: Puranjay Mohan To: paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, pulehui@huawei.com, conor.dooley@microchip.com, ast@kernel.org, daniel@iogearbox.net, andrii@kernel.org, martin.lau@linux.dev, song@kernel.org, yhs@fb.com, kpsingh@kernel.org, bjorn@kernel.org, bpf@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Cc: puranjay12@gmail.com Subject: [PATCH bpf-next 1/2] riscv: Extend patch_text_nosync() for multiple pages Date: Thu, 20 Jul 2023 15:49:40 +0000 Message-Id: <20230720154941.1504-2-puranjay12@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230720154941.1504-1-puranjay12@gmail.com> References: <20230720154941.1504-1-puranjay12@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230720_084947_961925_352AC7FE X-CRM114-Status: GOOD ( 12.95 ) X-BeenThere: linux-riscv@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-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org The patch_insn_write() function currently doesn't work for multiple pages of instructions, therefore patch_text_nosync() will fail with a page fault if called with lengths spanning multiple pages. This commit extends the patch_insn_write() function to support multiple pages by copying at max 2 pages at a time in a loop. This implementation is similar to text_poke_copy() function of x86. Signed-off-by: Puranjay Mohan Reviewed-by: Björn Töpel --- arch/riscv/kernel/patch.c | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/arch/riscv/kernel/patch.c b/arch/riscv/kernel/patch.c index 575e71d6c8ae..b2dbfcfdef85 100644 --- a/arch/riscv/kernel/patch.c +++ b/arch/riscv/kernel/patch.c @@ -53,7 +53,7 @@ static void patch_unmap(int fixmap) } NOKPROBE_SYMBOL(patch_unmap); -static int patch_insn_write(void *addr, const void *insn, size_t len) +static int __patch_insn_write(void *addr, const void *insn, size_t len) { void *waddr = addr; bool across_pages = (((uintptr_t) addr & ~PAGE_MASK) + len) > PAGE_SIZE; @@ -74,7 +74,7 @@ static int patch_insn_write(void *addr, const void *insn, size_t len) lockdep_assert_held(&text_mutex); if (across_pages) - patch_map(addr + len, FIX_TEXT_POKE1); + patch_map(addr + PAGE_SIZE, FIX_TEXT_POKE1); waddr = patch_map(addr, FIX_TEXT_POKE0); @@ -87,15 +87,34 @@ static int patch_insn_write(void *addr, const void *insn, size_t len) return ret; } -NOKPROBE_SYMBOL(patch_insn_write); +NOKPROBE_SYMBOL(__patch_insn_write); #else -static int patch_insn_write(void *addr, const void *insn, size_t len) +static int __patch_insn_write(void *addr, const void *insn, size_t len) { return copy_to_kernel_nofault(addr, insn, len); } -NOKPROBE_SYMBOL(patch_insn_write); +NOKPROBE_SYMBOL(__patch_insn_write); #endif /* CONFIG_MMU */ +static int patch_insn_write(void *addr, const void *insn, size_t len) +{ + size_t patched = 0; + size_t size; + int ret = 0; + + while (patched < len && !ret) { + size = min_t(size_t, + PAGE_SIZE * 2 - offset_in_page(addr + patched), + len - patched); + ret = __patch_insn_write(addr + patched, insn + patched, size); + + patched += size; + } + + return ret; +} +NOKPROBE_SYMBOL(patch_insn_write); + int patch_text_nosync(void *addr, const void *insns, size_t len) { u32 *tp = addr;