patches.lists.linux.dev archive mirror
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: stable@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	patches@lists.linux.dev, "Maciej W. Rozycki" <macro@orcam.me.uk>,
	Jiaxun Yang <jiaxun.yang@flygoat.com>,
	Thomas Bogendoerfer <tsbogend@alpha.franken.de>
Subject: [PATCH 6.6 10/86] MIPS: mm: Prevent a TLB shutdown on initial uniquification
Date: Thu, 27 Nov 2025 15:45:26 +0100	[thread overview]
Message-ID: <20251127144028.191081287@linuxfoundation.org> (raw)
In-Reply-To: <20251127144027.800761504@linuxfoundation.org>

6.6-stable review patch.  If anyone has any objections, please let me know.

------------------

From: Maciej W. Rozycki <macro@orcam.me.uk>

commit 9f048fa487409e364cf866c957cf0b0d782ca5a3 upstream.

Depending on the particular CPU implementation a TLB shutdown may occur
if multiple matching entries are detected upon the execution of a TLBP
or the TLBWI/TLBWR instructions.  Given that we don't know what entries
we have been handed we need to be very careful with the initial TLB
setup and avoid all these instructions.

Therefore read all the TLB entries one by one with the TLBR instruction,
bypassing the content addressing logic, and truncate any large pages in
place so as to avoid a case in the second step where an incoming entry
for a large page at a lower address overlaps with a replacement entry
chosen at another index.  Then preinitialize the TLB using addresses
outside our usual unique range and avoiding clashes with any entries
received, before making the usual call to local_flush_tlb_all().

This fixes (at least) R4x00 cores if TLBP hits multiple matching TLB
entries (SGI IP22 PROM for examples sets up all TLBs to the same virtual
address).

Signed-off-by: Maciej W. Rozycki <macro@orcam.me.uk>
Fixes: 35ad7e181541 ("MIPS: mm: tlb-r4k: Uniquify TLB entries on init")
Cc: stable@vger.kernel.org
Reviewed-by: Jiaxun Yang <jiaxun.yang@flygoat.com>
Tested-by: Jiaxun Yang <jiaxun.yang@flygoat.com> # Boston I6400, M5150 sim
Signed-off-by: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
---
 arch/mips/mm/tlb-r4k.c |  102 ++++++++++++++++++++++++++++++-------------------
 1 file changed, 64 insertions(+), 38 deletions(-)

--- a/arch/mips/mm/tlb-r4k.c
+++ b/arch/mips/mm/tlb-r4k.c
@@ -15,6 +15,7 @@
 #include <linux/mm.h>
 #include <linux/hugetlb.h>
 #include <linux/export.h>
+#include <linux/sort.h>
 
 #include <asm/cpu.h>
 #include <asm/cpu-type.h>
@@ -506,55 +507,79 @@ static int __init set_ntlb(char *str)
 
 __setup("ntlb=", set_ntlb);
 
-/* Initialise all TLB entries with unique values */
+
+/* Comparison function for EntryHi VPN fields.  */
+static int r4k_vpn_cmp(const void *a, const void *b)
+{
+	long v = *(unsigned long *)a - *(unsigned long *)b;
+	int s = sizeof(long) > sizeof(int) ? sizeof(long) * 8 - 1: 0;
+	return s ? (v != 0) | v >> s : v;
+}
+
+/*
+ * Initialise all TLB entries with unique values that do not clash with
+ * what we have been handed over and what we'll be using ourselves.
+ */
 static void r4k_tlb_uniquify(void)
 {
-	int entry = num_wired_entries();
+	unsigned long tlb_vpns[1 << MIPS_CONF1_TLBS_SIZE];
+	int tlbsize = current_cpu_data.tlbsize;
+	int start = num_wired_entries();
+	unsigned long vpn_mask;
+	int cnt, ent, idx, i;
+
+	vpn_mask = GENMASK(cpu_vmbits - 1, 13);
+	vpn_mask |= IS_ENABLED(CONFIG_64BIT) ? 3ULL << 62 : 1 << 31;
 
 	htw_stop();
-	write_c0_entrylo0(0);
-	write_c0_entrylo1(0);
 
-	while (entry < current_cpu_data.tlbsize) {
-		unsigned long asid_mask = cpu_asid_mask(&current_cpu_data);
-		unsigned long asid = 0;
-		int idx;
+	for (i = start, cnt = 0; i < tlbsize; i++, cnt++) {
+		unsigned long vpn;
 
-		/* Skip wired MMID to make ginvt_mmid work */
-		if (cpu_has_mmid)
-			asid = MMID_KERNEL_WIRED + 1;
+		write_c0_index(i);
+		mtc0_tlbr_hazard();
+		tlb_read();
+		tlb_read_hazard();
+		vpn = read_c0_entryhi();
+		vpn &= vpn_mask & PAGE_MASK;
+		tlb_vpns[cnt] = vpn;
 
-		/* Check for match before using UNIQUE_ENTRYHI */
-		do {
-			if (cpu_has_mmid) {
-				write_c0_memorymapid(asid);
-				write_c0_entryhi(UNIQUE_ENTRYHI(entry));
-			} else {
-				write_c0_entryhi(UNIQUE_ENTRYHI(entry) | asid);
-			}
-			mtc0_tlbw_hazard();
-			tlb_probe();
-			tlb_probe_hazard();
-			idx = read_c0_index();
-			/* No match or match is on current entry */
-			if (idx < 0 || idx == entry)
-				break;
-			/*
-			 * If we hit a match, we need to try again with
-			 * a different ASID.
-			 */
-			asid++;
-		} while (asid < asid_mask);
-
-		if (idx >= 0 && idx != entry)
-			panic("Unable to uniquify TLB entry %d", idx);
-
-		write_c0_index(entry);
+		/* Prevent any large pages from overlapping regular ones.  */
+		write_c0_pagemask(read_c0_pagemask() & PM_DEFAULT_MASK);
 		mtc0_tlbw_hazard();
 		tlb_write_indexed();
-		entry++;
+		tlbw_use_hazard();
 	}
 
+	sort(tlb_vpns, cnt, sizeof(tlb_vpns[0]), r4k_vpn_cmp, NULL);
+
+	write_c0_pagemask(PM_DEFAULT_MASK);
+	write_c0_entrylo0(0);
+	write_c0_entrylo1(0);
+
+	idx = 0;
+	ent = tlbsize;
+	for (i = start; i < tlbsize; i++)
+		while (1) {
+			unsigned long entryhi, vpn;
+
+			entryhi = UNIQUE_ENTRYHI(ent);
+			vpn = entryhi & vpn_mask & PAGE_MASK;
+
+			if (idx >= cnt || vpn < tlb_vpns[idx]) {
+				write_c0_entryhi(entryhi);
+				write_c0_index(i);
+				mtc0_tlbw_hazard();
+				tlb_write_indexed();
+				ent++;
+				break;
+			} else if (vpn == tlb_vpns[idx]) {
+				ent++;
+			} else {
+				idx++;
+			}
+		}
+
 	tlbw_use_hazard();
 	htw_start();
 	flush_micro_tlb();
@@ -600,6 +625,7 @@ static void r4k_tlb_configure(void)
 
 	/* From this point on the ARC firmware is dead.	 */
 	r4k_tlb_uniquify();
+	local_flush_tlb_all();
 
 	/* Did I tell you that ARC SUCKS?  */
 }



  parent reply	other threads:[~2025-11-27 14:47 UTC|newest]

Thread overview: 95+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-27 14:45 [PATCH 6.6 00/86] 6.6.118-rc1 review Greg Kroah-Hartman
2025-11-27 14:45 ` [PATCH 6.6 01/86] timers: Fix NULL function pointer race in timer_shutdown_sync() Greg Kroah-Hartman
2025-11-27 14:45 ` [PATCH 6.6 02/86] HID: quirks: work around VID/PID conflict for 0x4c4a/0x4155 Greg Kroah-Hartman
2025-11-27 14:45 ` [PATCH 6.6 03/86] mtd: rawnand: cadence: fix DMA device NULL pointer dereference Greg Kroah-Hartman
2025-11-27 14:45 ` [PATCH 6.6 04/86] mtdchar: fix integer overflow in read/write ioctls Greg Kroah-Hartman
2025-11-27 14:45 ` [PATCH 6.6 05/86] shmem: fix tmpfs reconfiguration (remount) when noswap is set Greg Kroah-Hartman
2025-11-27 14:45 ` [PATCH 6.6 06/86] exfat: check return value of sb_min_blocksize in exfat_read_boot_sector Greg Kroah-Hartman
2025-11-27 14:45 ` [PATCH 6.6 07/86] mptcp: Disallow MPTCP subflows from sockmap Greg Kroah-Hartman
2025-11-27 14:45 ` [PATCH 6.6 08/86] mptcp: Fix proto fallback detection with BPF Greg Kroah-Hartman
2025-11-27 14:45 ` [PATCH 6.6 09/86] ata: libata-scsi: Fix system suspend for a security locked drive Greg Kroah-Hartman
2025-11-27 14:45 ` Greg Kroah-Hartman [this message]
2025-11-28  6:09   ` [PATCH 6.6 10/86] MIPS: mm: Prevent a TLB shutdown on initial uniquification Maciej W. Rozycki
2025-11-27 14:45 ` [PATCH 6.6 11/86] smb: client: introduce close_cached_dir_locked() Greg Kroah-Hartman
2025-11-27 14:45 ` [PATCH 6.6 12/86] ata: libata-scsi: Add missing scsi_device_put() in ata_scsi_dev_rescan() Greg Kroah-Hartman
2025-11-27 14:45 ` [PATCH 6.6 13/86] be2net: pass wrb_params in case of OS2BMC Greg Kroah-Hartman
2025-11-27 14:45 ` [PATCH 6.6 14/86] net: dsa: microchip: lan937x: Fix RGMII delay tuning Greg Kroah-Hartman
2025-11-27 14:45 ` [PATCH 6.6 15/86] Revert "drm/tegra: dsi: Clear enable register if powered by bootloader" Greg Kroah-Hartman
2025-11-27 14:45 ` [PATCH 6.6 16/86] Input: cros_ec_keyb - fix an invalid memory access Greg Kroah-Hartman
2025-11-27 14:45 ` [PATCH 6.6 17/86] Input: goodix - add support for ACPI ID GDIX1003 Greg Kroah-Hartman
2025-11-27 14:45 ` [PATCH 6.6 18/86] Input: imx_sc_key - fix memory corruption on unload Greg Kroah-Hartman
2025-11-27 14:45 ` [PATCH 6.6 19/86] Input: pegasus-notetaker - fix potential out-of-bounds access Greg Kroah-Hartman
2025-11-27 14:45 ` [PATCH 6.6 20/86] nouveau/firmware: Add missing kfree() of nvkm_falcon_fw::boot Greg Kroah-Hartman
2025-11-27 14:45 ` [PATCH 6.6 21/86] nvme: nvme-fc: move tagset removal to nvme_fc_delete_ctrl() Greg Kroah-Hartman
2025-11-27 14:45 ` [PATCH 6.6 22/86] nvme: nvme-fc: Ensure ->ioerr_work is cancelled in nvme_fc_delete_ctrl() Greg Kroah-Hartman
2025-11-27 14:45 ` [PATCH 6.6 23/86] scsi: sg: Do not sleep in atomic context Greg Kroah-Hartman
2025-11-27 14:45 ` [PATCH 6.6 24/86] scsi: target: tcm_loop: Fix segfault in tcm_loop_tpg_address_show() Greg Kroah-Hartman
2025-11-27 14:45 ` [PATCH 6.6 25/86] MIPS: Malta: Fix !EVA SOC-it PCI MMIO Greg Kroah-Hartman
2025-11-27 14:45 ` [PATCH 6.6 26/86] dt-bindings: pinctrl: toshiba,visconti: Fix number of items in groups Greg Kroah-Hartman
2025-11-27 14:45 ` [PATCH 6.6 27/86] LoongArch: Dont panic if no valid cache info for PCI Greg Kroah-Hartman
2025-11-27 14:45 ` [PATCH 6.6 28/86] mptcp: fix race condition in mptcp_schedule_work() Greg Kroah-Hartman
2025-11-27 14:45 ` [PATCH 6.6 29/86] mptcp: fix ack generation for fallback msk Greg Kroah-Hartman
2025-11-27 14:45 ` [PATCH 6.6 30/86] mptcp: fix premature close in case of fallback Greg Kroah-Hartman
2025-11-27 14:45 ` [PATCH 6.6 31/86] mptcp: avoid unneeded subflow-level drops Greg Kroah-Hartman
2025-11-27 14:45 ` [PATCH 6.6 32/86] mptcp: decouple mptcp fastclose from tcp close Greg Kroah-Hartman
2025-11-27 14:45 ` [PATCH 6.6 33/86] mptcp: do not fallback when OoO is present Greg Kroah-Hartman
2025-11-27 14:45 ` [PATCH 6.6 34/86] drm/tegra: dc: Fix reference leak in tegra_dc_couple() Greg Kroah-Hartman
2025-11-27 14:45 ` [PATCH 6.6 35/86] drm/amdgpu: Skip emit de meta data on gfx11 with rs64 enabled Greg Kroah-Hartman
2025-11-27 14:45 ` [PATCH 6.6 36/86] drm/amd/display: Increase DPCD read retries Greg Kroah-Hartman
2025-11-27 14:45 ` [PATCH 6.6 37/86] drm/amd/display: Move sleep into each retry for retrieve_link_cap() Greg Kroah-Hartman
2025-11-27 14:45 ` [PATCH 6.6 38/86] xfrm: Determine inner GSO type from packet inner protocol Greg Kroah-Hartman
2025-11-27 14:45 ` [PATCH 6.6 39/86] xfrm: Prevent locally generated packets from direct output in tunnel mode Greg Kroah-Hartman
2025-11-27 14:45 ` [PATCH 6.6 40/86] pinctrl: cirrus: Fix fwnode leak in cs42l43_pin_probe() Greg Kroah-Hartman
2025-11-27 14:45 ` [PATCH 6.6 41/86] mlxsw: spectrum: Fix memory leak in mlxsw_sp_flower_stats() Greg Kroah-Hartman
2025-11-27 14:45 ` [PATCH 6.6 42/86] drm/tegra: Add call to put_pid() Greg Kroah-Hartman
2025-11-27 14:45 ` [PATCH 6.6 43/86] net: dsa: hellcreek: fix missing error handling in LED registration Greg Kroah-Hartman
2025-11-27 14:46 ` [PATCH 6.6 44/86] net: mlxsw: linecards: fix missing error check in mlxsw_linecard_devlink_info_get() Greg Kroah-Hartman
2025-11-27 14:46 ` [PATCH 6.6 45/86] net: openvswitch: remove never-working support for setting nsh fields Greg Kroah-Hartman
2025-11-27 14:46 ` [PATCH 6.6 46/86] nvme-multipath: fix lockdep WARN due to partition scan work Greg Kroah-Hartman
2025-11-27 14:46 ` [PATCH 6.6 47/86] s390/ctcm: Fix double-kfree Greg Kroah-Hartman
2025-11-27 14:46 ` [PATCH 6.6 48/86] platform/x86/intel/speed_select_if: Convert PCIBIOS_* return codes to errnos Greg Kroah-Hartman
2025-11-27 14:46 ` [PATCH 6.6 49/86] kernel.h: Move ARRAY_SIZE() to a separate header Greg Kroah-Hartman
2025-11-27 14:46 ` [PATCH 6.6 50/86] net: qlogic/qede: fix potential out-of-bounds read in qede_tpa_cont() and qede_tpa_end() Greg Kroah-Hartman
2025-11-27 14:46 ` [PATCH 6.6 51/86] pinctrl: s32cc: fix uninitialized memory in s32_pinctrl_desc Greg Kroah-Hartman
2025-11-27 14:46 ` [PATCH 6.6 52/86] pinctrl: s32cc: initialize gpio_pin_config::list after kmalloc() Greg Kroah-Hartman
2025-11-27 14:46 ` [PATCH 6.6 53/86] devlink: rate: Unset parent pointer in devl_rate_nodes_destroy Greg Kroah-Hartman
2025-11-27 14:46 ` [PATCH 6.6 54/86] net/mlx5: Clean up only new IRQ glue on request_irq() failure Greg Kroah-Hartman
2025-11-27 14:46 ` [PATCH 6.6 55/86] LoongArch: Use UAPI types in ptrace UAPI header Greg Kroah-Hartman
2025-11-27 14:46 ` [PATCH 6.6 56/86] cifs: fix memory leak in smb3_fs_context_parse_param error path Greg Kroah-Hartman
2025-11-27 14:46 ` [PATCH 6.6 57/86] vsock: Ignore signal/timeout on connect() if already established Greg Kroah-Hartman
2025-11-27 14:46 ` [PATCH 6.6 58/86] bcma: dont register devices disabled in OF Greg Kroah-Hartman
2025-11-27 14:46 ` [PATCH 6.6 59/86] cifs: fix typo in enable_gcm_256 module parameter Greg Kroah-Hartman
2025-11-27 14:46 ` [PATCH 6.6 60/86] scsi: core: Fix a regression triggered by scsi_host_busy() Greg Kroah-Hartman
2025-11-27 14:46 ` [PATCH 6.6 61/86] x86/microcode/AMD: Limit Entrysign signature checking to known generations Greg Kroah-Hartman
2025-11-27 14:46 ` [PATCH 6.6 62/86] selftests: net: use BASH for bareudp testing Greg Kroah-Hartman
2025-11-27 14:46 ` [PATCH 6.6 63/86] net: tls: Cancel RX async resync request on rcd_delta overflow Greg Kroah-Hartman
2025-11-27 14:46 ` [PATCH 6.6 64/86] kconfig/mconf: Initialize the default locale at startup Greg Kroah-Hartman
2025-11-27 14:46 ` [PATCH 6.6 65/86] kconfig/nconf: " Greg Kroah-Hartman
2025-11-27 14:46 ` [PATCH 6.6 66/86] f2fs: compress: change the first parameter of page_array_{alloc,free} to sbi Greg Kroah-Hartman
2025-11-27 14:46 ` [PATCH 6.6 67/86] s390/mm: Fix __ptep_rdp() inline assembly Greg Kroah-Hartman
2025-11-27 14:46 ` [PATCH 6.6 68/86] f2fs: compress: fix UAF of f2fs_inode_info in f2fs_free_dic Greg Kroah-Hartman
2025-11-27 14:46 ` [PATCH 6.6 69/86] ALSA: usb-audio: fix uac2 clock source at terminal parser Greg Kroah-Hartman
2025-11-27 14:46 ` [PATCH 6.6 70/86] net: ethernet: ti: netcp: Standardize knav_dma_open_channel to return NULL on error Greg Kroah-Hartman
2025-11-27 14:46 ` [PATCH 6.6 71/86] tracing/tools: Fix incorrcet short option in usage text for --threads Greg Kroah-Hartman
2025-11-27 14:46 ` [PATCH 6.6 72/86] smb: client: fix incomplete backport in cfids_invalidation_worker() Greg Kroah-Hartman
2025-11-27 14:46 ` [PATCH 6.6 73/86] KVM: arm64: Check the untrusted offset in FF-A memory share Greg Kroah-Hartman
2025-11-27 14:46 ` [PATCH 6.6 74/86] uio_hv_generic: Set event for all channels on the device Greg Kroah-Hartman
2025-11-27 14:46 ` [PATCH 6.6 75/86] maple_tree: fix tracepoint string pointers Greg Kroah-Hartman
2025-11-27 14:46 ` [PATCH 6.6 76/86] wifi: cfg80211: Add missing lock in cfg80211_check_and_end_cac() Greg Kroah-Hartman
2025-11-27 14:46 ` [PATCH 6.6 77/86] crash: fix crashkernel resource shrink Greg Kroah-Hartman
2025-11-27 14:46 ` [PATCH 6.6 78/86] ftrace: Fix BPF fexit with livepatch Greg Kroah-Hartman
2025-11-27 14:46 ` [PATCH 6.6 79/86] pmdomain: arm: scmi: Fix genpd leak on provider registration failure Greg Kroah-Hartman
2025-11-27 14:46 ` [PATCH 6.6 80/86] pmdomain: imx-gpc: Convert to platform remove callback returning void Greg Kroah-Hartman
2025-11-27 14:46 ` [PATCH 6.6 81/86] pmdomain: imx: Fix reference count leak in imx_gpc_remove Greg Kroah-Hartman
2025-11-27 14:46 ` [PATCH 6.6 82/86] selftests: mptcp: join: endpoints: longer transfer Greg Kroah-Hartman
2025-11-27 14:46 ` [PATCH 6.6 83/86] HID: amd_sfh: Stop sensor before starting Greg Kroah-Hartman
2025-11-27 14:46 ` [PATCH 6.6 84/86] mm/mempool: replace kmap_atomic() with kmap_local_page() Greg Kroah-Hartman
2025-11-27 14:46 ` [PATCH 6.6 85/86] mm/mempool: fix poisoning order>0 pages with HIGHMEM Greg Kroah-Hartman
2025-11-27 14:46 ` [PATCH 6.6 86/86] mptcp: fix a race in mptcp_pm_del_add_timer() Greg Kroah-Hartman
2025-11-27 17:10 ` [PATCH 6.6 00/86] 6.6.118-rc1 review Peter Schneider
2025-11-27 17:29 ` Brett A C Sheffield
2025-11-28  8:43 ` Ron Economos
2025-11-28 10:16 ` Naresh Kamboju
2025-11-29  1:02 ` Mark Brown
2025-11-29 17:43 ` Florian Fainelli
2025-12-01 18:18 ` Jon Hunter

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251127144028.191081287@linuxfoundation.org \
    --to=gregkh@linuxfoundation.org \
    --cc=jiaxun.yang@flygoat.com \
    --cc=macro@orcam.me.uk \
    --cc=patches@lists.linux.dev \
    --cc=stable@vger.kernel.org \
    --cc=tsbogend@alpha.franken.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).