Received: by 2002:ab2:6991:0:b0:1f2:fff1:ace7 with SMTP id v17csp150936lqo; Wed, 27 Mar 2024 09:12:13 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUI20h90PIT3c6uLU80W2WP4eBV+M4Hb957I94RltdNI2LqnGksVKXTTXBalcEOGkVIhLkj5HLObAXYmOr7DTOp/oDSiplCGBFE2FKMhQ== X-Google-Smtp-Source: AGHT+IHmfOBUd2QVA+uLrZeZKOjjxM7PyL/iqGxbe9gaogD2hssNfidrqMTfxvsvAr6X7loPJiWD X-Received: by 2002:a05:620a:13ea:b0:788:4e44:a9b7 with SMTP id h10-20020a05620a13ea00b007884e44a9b7mr2960349qkl.78.1711555932812; Wed, 27 Mar 2024 09:12:12 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711555932; cv=pass; d=google.com; s=arc-20160816; b=EnCMFXDhgI1Nf7V+LJorbOPZjVJ5mZ4++M0M4fZoWhRj7YGAFCcppUZvdqI0i+fN2L nkvrt7uX1R6sAHoJRF+77E9JxqLMA2tAe71ABAZ+7sFPj6upqcvxnIpVTZZPVOjr0g/s xzwWY8p7F0eYjgXew7ZgoU3XkWziQK9UpWAh2/vJVN/NMa53L4295Yuu0jO7KFlvMG3E Nk4UpY+mwzQ4yGa0MtCrWKbizfhDuu85JfhLztw/sP8bQarxQS1MmAHPMN3i1enDkCGn 19Vsn9McIHIWX7YWnWF1PuBWJCn+qZIFBwOGPZsOxkYd3MiV11sim63Qzxrx+bswmvoG Q5HA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=+4M9Lk9Z8nU8etdW0Vehxd3ZiXzPs65Q9tqt+MZAHIQ=; fh=UCbT3yyc+wbuZ7v2YThseEliA5LINU+1v69LmoiMWoA=; b=YhEIcBTUGiJ/PibSdfOuW2xDqg/Dhm2LkMlv4EwyijjRN4+va5TfZ6pX4ZuaIqpHXD GXvtmtC3zIB4ou5fqZRRgdrHv7lrQdlIW/wL4EpUl1Hi+pQlPGd9tyNiPmPhyTLj7tqj cNa+R3XhUhROFimVoKisYf+/z74GeKg7Tm2LKI6imW5UuKZVLePbh7Jf8FFthpfcbo+T IXTYKN7lg0Awzb84+sSCho5rp4FJah9Y/Li4gSLcQ0oDkh/XuU0Vm0aLXE1pzAhszlPY ZEENeIyouWYWpyM1r4gC7y9Ps+5WX2f3OUX6OX6tOR7VR0mU5ytooVZdnILi2KrhyWCa uIAg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=IwIiEzgn; arc=pass (i=1 spf=pass spfdomain=sifive.com dkim=pass dkdomain=sifive.com dmarc=pass fromdomain=sifive.com); spf=pass (google.com: domain of linux-kernel+bounces-121605-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-121605-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=sifive.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id xz17-20020a05620a5e1100b0078a5fc2adf0si4610776qkn.138.2024.03.27.09.12.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 09:12:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-121605-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=IwIiEzgn; arc=pass (i=1 spf=pass spfdomain=sifive.com dkim=pass dkdomain=sifive.com dmarc=pass fromdomain=sifive.com); spf=pass (google.com: domain of linux-kernel+bounces-121605-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-121605-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=sifive.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 821881C20A33 for ; Wed, 27 Mar 2024 16:12:12 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 990A6136E30; Wed, 27 Mar 2024 16:05:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=sifive.com header.i=@sifive.com header.b="IwIiEzgn" Received: from mail-pl1-f173.google.com (mail-pl1-f173.google.com [209.85.214.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 471C4131E27 for ; Wed, 27 Mar 2024 16:05:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.173 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711555529; cv=none; b=j6AfsrC3QfMi58btga7HEk6Whz8jKfsXbcJ8cRaADamzuiQpQ0IjL8e9AFy4cOVl5toeWDVKSBkEkgSKpz218+8QUOmYmrdhVlWWo6mpYVQ+2Dj9dvV2unwuAi3B2iqR5+/C19EVDgXtYzRAuBqOcsjypmkYKlrktE5KspToNBM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711555529; c=relaxed/simple; bh=r89XNv4WfgUD7lamaOlWbD2n2mjunEetZGP7bb3T1KU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=BpNHGJpTQAcP2k+pequdC3AsFtzIFhQ2X2UFPuSdOLsJAFguICXAFIs5/2jLSEPuIoZEo2s0B+pPj7jriCe6k67zL2omtAG7J360qYBGkH5IGGyX+WvCylS1NBjiFTlY4roADhKKGypz0c3fI1vwMZE2cuwPsKhH4ezl2nneDbI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=sifive.com; spf=pass smtp.mailfrom=sifive.com; dkim=pass (2048-bit key) header.d=sifive.com header.i=@sifive.com header.b=IwIiEzgn; arc=none smtp.client-ip=209.85.214.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=sifive.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=sifive.com Received: by mail-pl1-f173.google.com with SMTP id d9443c01a7336-1def59b537cso46637045ad.2 for ; Wed, 27 Mar 2024 09:05:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; t=1711555527; x=1712160327; darn=vger.kernel.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=+4M9Lk9Z8nU8etdW0Vehxd3ZiXzPs65Q9tqt+MZAHIQ=; b=IwIiEzgnc6qnAAt5G7fRO88YvBd1Un4Wsm0oxdxPSwsbNSQVrKbS3RH9vprTsVi7X1 0IaxRHqWciemuna7fhWh29cMmt2EKYaiU6crZ8C/oEcBtakWk7FyGFBOWlHTwflfX4/W 4AFJaRcvHUIzlq5VK1uANZ4jXuqJKgxPgvwpRrEZN5OxMnflEMDCZOl0emAj/KCJdrU6 024bE31naaByDUDJTc67w/qqD1tFcKz0SRqDCTpwoBwVfTyAWGcPMgTY5L0HsoHcoCbt qwqAXMIYIjzjdy2tWvDs0NxdjOVzgz4bwCh2Q+gujnZR+fBwWfo0Vccj9faRwefKmldG 0uag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711555527; x=1712160327; 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=+4M9Lk9Z8nU8etdW0Vehxd3ZiXzPs65Q9tqt+MZAHIQ=; b=XzWwzNdP1Tfjaqq7jHZ96p7c0Eez2jjQrYyimmHsIeV4Rvj+2XBrJgLfqXO+DpVOKK u9IiSjKOpu2xYKSvwGBBwycr/JGtUpH7At24w36eWGcBIiHJGk42kE7jNANsX+UWo3dw iUGcmdmLCx1EjgXnN9CWpQxOrb41/AoaUomBHhzcUXJzr8GV0b2jkaUGRGveE6AmuN19 EjsVi0smxTjhF0qwG08Q2Q3zH24y6QuBxvxiHxRS/iv4X1hlmbIvrLSklv5PWd1Y6B8c P0Ab5V9w6WkgVPJNjP+6GcBSaIEmZx+kb+AuyAaNhQ+vr804RysPU0AqrVzGGD1tvUXn Xv3Q== X-Forwarded-Encrypted: i=1; AJvYcCUrxgTvHOSOn9YYqUJoCiP+vsHvN/1+IghmnNa3oqA1gKbC/nhvAYuwarlOzsDJeQbv6RWA48xvAGwfFC0o0Ww6i+RvWjvjHVTSE4+T X-Gm-Message-State: AOJu0YywK7hXCc8v9VlBXLIpdZbUD1ulFp2ibaFRikDZKYnw6wihPp9i H+wAt7MEDaYeBYpsE8QYATw5qIdfvXfPlFzPOBk7ji99g/i47++i02mlDkRaQ4O6lW1Lr9UHbaD R X-Received: by 2002:a17:902:e541:b0:1e0:9122:abc4 with SMTP id n1-20020a170902e54100b001e09122abc4mr169570plf.21.1711555527635; Wed, 27 Mar 2024 09:05:27 -0700 (PDT) Received: from sw06.internal.sifive.com ([4.53.31.132]) by smtp.gmail.com with ESMTPSA id bf4-20020a170902b90400b001e14807f9cesm1718226plb.125.2024.03.27.09.05.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 09:05:27 -0700 (PDT) From: Samuel Holland To: Palmer Dabbelt Cc: =?UTF-8?q?Bj=C3=B6rn=20T=C3=B6pel?= , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Samuel Holland Subject: [PATCH v2 4/7] riscv: Simplify text patching loops Date: Wed, 27 Mar 2024 09:04:43 -0700 Message-ID: <20240327160520.791322-5-samuel.holland@sifive.com> X-Mailer: git-send-email 2.43.1 In-Reply-To: <20240327160520.791322-1-samuel.holland@sifive.com> References: <20240327160520.791322-1-samuel.holland@sifive.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit This reduces the number of variables and makes the code easier to parse. Signed-off-by: Samuel Holland --- Changes in v2: - Further simplify patch_insn_set()/patch_insn_write() loop conditions - Use min() instead of min_t() since both sides are unsigned long arch/riscv/kernel/patch.c | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/arch/riscv/kernel/patch.c b/arch/riscv/kernel/patch.c index 9a1bce1adf5a..0d1700d1934c 100644 --- a/arch/riscv/kernel/patch.c +++ b/arch/riscv/kernel/patch.c @@ -155,22 +155,24 @@ NOKPROBE_SYMBOL(__patch_insn_write); static int patch_insn_set(void *addr, u8 c, size_t len) { - size_t patched = 0; size_t size; - int ret = 0; + int ret; /* * __patch_insn_set() can only work on 2 pages at a time so call it in a * loop with 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_set(addr + patched, c, size); - - patched += size; + while (len) { + size = min(len, PAGE_SIZE * 2 - offset_in_page(addr)); + ret = __patch_insn_set(addr, c, size); + if (ret) + return ret; + + addr += size; + len -= size; } - return ret; + return 0; } NOKPROBE_SYMBOL(patch_insn_set); @@ -190,22 +192,25 @@ NOKPROBE_SYMBOL(patch_text_set_nosync); int patch_insn_write(void *addr, const void *insn, size_t len) { - size_t patched = 0; size_t size; - int ret = 0; + int ret; /* * 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; + while (len) { + size = min(len, PAGE_SIZE * 2 - offset_in_page(addr)); + ret = __patch_insn_write(addr, insn, size); + if (ret) + return ret; + + addr += size; + insn += size; + len -= size; } - return ret; + return 0; } NOKPROBE_SYMBOL(patch_insn_write); -- 2.43.1