From patchwork Thu Aug 31 13:12:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Puranjay Mohan X-Patchwork-Id: 13371557 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 E054AC83F01 for ; Thu, 31 Aug 2023 13:12:48 +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=hybb2qcqI/eM+/Yb6xUTcRP7FqPYaFIWSJmJE7cwFa8=; b=tNdIAuNpf5/a+A nqA3Gu0nSBI7gXU5XoXTVriBlpbUHu0x9G/Ce7PdChUh8idx+P+22Sa10dBSJk9xJWFN8nnHwF4H8 /9HLvF5OYzG5sSFrGUpRhnQA3Nw9+C7WeQAKj05fEUF6EWVRiZy6THvalVS1cD1Wiu311z248QFkm KL58DZknYEWF157mIgvOvq38TDTGfH90h9C9jGjrDunF4t64kuJnF9Ew7eLxFBjT+ly2f64t7yOYr qpIG7sFD5/FH8psWp3DYnpUMvJX3gOkvrmoCYg17vbwtq6EI3e1owWBDYKjxFT77QVIQzqZ3CiCob kz6+byPQdxanuOj5xb0g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qbhTX-00FLDG-1p; Thu, 31 Aug 2023 13:12:43 +0000 Received: from mail-wr1-x42d.google.com ([2a00:1450:4864:20::42d]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qbhTQ-00FLA3-0e for linux-riscv@lists.infradead.org; Thu, 31 Aug 2023 13:12:37 +0000 Received: by mail-wr1-x42d.google.com with SMTP id ffacd0b85a97d-31781e15a0cso601968f8f.3 for ; Thu, 31 Aug 2023 06:12:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1693487553; x=1694092353; 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=CUYKChQYfJZZ8DPO0whW/zno6UWIarXDmyfOm92F8UA=; b=gT+vFZ42RdJ6gnLLr8WT+mDJz64+f7Up7VWNrhNgfizBKAMOHBUtg2/0KEs3Nz3tKr VgTNnOdQA22sPTdGqOxkhC9aJVzgeqoZCdtH/wRJdJhHdj8RkNMWfR4iO407l5pFFfu1 /QrpMPkrX+pVqLvBLwskotFCNjAiJh36J6xQrf24vrcN4qceL68UQMA31+uzvlLxAAbl Dsq/zR0MSu3gMPvAKqbbAlG4YPAJdvvqFQkzEounuaAcK2N7/pEl8v5QUpfgSph3Y1tD LBZs7jiFAomnGmP/w3zJYnokB2OUChAY4/rIiquIgjjcDCFPs6l3jOGBXrgtSFln3k5h s0qA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693487553; x=1694092353; 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=CUYKChQYfJZZ8DPO0whW/zno6UWIarXDmyfOm92F8UA=; b=WJmHugycDzuVXxkVDP8yJ2G4DS6zEgIj5TsqyCXoLnw0+P5K21uduOzIgomj5NBnRd yUBOGzc8BhlXUbeT+KFiMnvsLTfvBnZKAeWEyCDn5Yss4pDWpDw63MbSfB0Gep2HjVMa mXYhoHcdgtothLADekMAEMYhJzJkIkExs3EmtaXH/Ssm6KSUFVsiG7qyNyY/XGwrut2b V+cmh44W2qQNWfccZDJrmHwiT+fiMPztY6c5NTXV3lg9FwUGw57FNZwiseD2TjBWHBA9 xXdDF0fd5+WJwnI5VLDctvE0TqOP3MipVxsvZLM9r50DRoRDDSrG85RUczKTRqpUDm9f VJoQ== X-Gm-Message-State: AOJu0YzXVuL6+NsZGgHssRt/xqbQohQe4EHHEFBYSCx9jZe8qS7Ysx6h Dd7UYZyU/CN5ZeJahaX9PT0= X-Google-Smtp-Source: AGHT+IEmRLcQA4Zh4qOEOVWO+OJdUOgzn5p2uNiV+/qiisIBP67acZzJXuCuSC/kJMqcbfCWT7qEVg== X-Received: by 2002:a5d:668f:0:b0:317:6a7c:6e07 with SMTP id l15-20020a5d668f000000b003176a7c6e07mr3756142wru.32.1693487553087; Thu, 31 Aug 2023 06:12:33 -0700 (PDT) Received: from ip-172-31-30-46.eu-west-1.compute.internal (ec2-54-170-241-106.eu-west-1.compute.amazonaws.com. [54.170.241.106]) by smtp.gmail.com with ESMTPSA id a28-20020a5d457c000000b00317f70240afsm2206607wrc.27.2023.08.31.06.12.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 31 Aug 2023 06:12:32 -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 v4 2/4] riscv: extend patch_text_nosync() for multiple pages Date: Thu, 31 Aug 2023 13:12:27 +0000 Message-Id: <20230831131229.497941-3-puranjay12@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230831131229.497941-1-puranjay12@gmail.com> References: <20230831131229.497941-1-puranjay12@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230831_061236_239858_748B6AB3 X-CRM114-Status: GOOD ( 15.74 ) 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: Pu Lehui Reviewed-by: Björn Töpel Tested-by: Björn Töpel --- arch/riscv/kernel/patch.c | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/arch/riscv/kernel/patch.c b/arch/riscv/kernel/patch.c index 575e71d6c8ae..2c97e246f4dc 100644 --- a/arch/riscv/kernel/patch.c +++ b/arch/riscv/kernel/patch.c @@ -53,12 +53,18 @@ 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; int ret; + /* + * Only two pages can be mapped at a time for writing. + */ + if (len + offset_in_page(addr) > 2 * PAGE_SIZE) + return -EINVAL; + /* * Before reaching here, it was expected to lock the text_mutex * already, so we don't need to give another lock here and could @@ -74,7 +80,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 +93,36 @@ 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; + + /* + * Copy the instructions to the destination address, two pages at a time + * because __patch_insn_write() can only handle len <= 2 * PAGE_SIZE. + */ + 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;