From nobody Sat Feb 7 15:40:19 2026 Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) (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 B059A2FB99A for ; Sat, 20 Dec 2025 09:08:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766221737; cv=none; b=GutOARSaU4pTF0ccM8pXq8P2TksFNQenjRPbbTCaA1P443/8WtWu9xAu72pxQ8FymTdr2MgLAgrIkfCqf1kFQaC9l0MpiDc9JMvHMKHVzLHlr5MnLljxG8zKNThHTMCNWbaWwRewAkjeLnrxiQmMpnj924lX7q7F3B71f2fVyRM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766221737; c=relaxed/simple; bh=5sxeVmM1HSneHjU/I1Fk7AXM2eqK42xU7iyfuc1gJG0=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=eq5c9vbOMZ5EylzYhsCOYqT/Q5p89eVgz48/jWij873kkFmkYU2woMC7aTA92F1HpHngwS9PVT3+VtwAhPFhGGcSlyq14AI+86PAwvEk3xp6ffA6pWRDQHsXFYE/oTtKFfhSxuaKMYKyU0dNtEKWv4eab8p5hbEQqcA35RLYigI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b=eC9KWOhd; arc=none smtp.client-ip=209.85.210.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.b="eC9KWOhd" Received: by mail-pf1-f172.google.com with SMTP id d2e1a72fcca58-7aa2170adf9so2043819b3a.0 for ; Sat, 20 Dec 2025 01:08:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1766221734; x=1766826534; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=9FCav/YO9x/c2l0kK2rjBlOI4AUzqgUWfXFrOx8/XtI=; b=eC9KWOhdLyWOS0Ys1nNa9Kyp8TyTk0kyFyVBqUCoWPIvm0FfTziECYZRwl/HHGNGjV 0KjGUfAnD5kTrO9nitcpOsKXfcvW0qY+v0yKjofwCsYcBsH+omBhcseX3rds4QSHLoo1 pjEmelA/ZPX2gwS2J6mR8Xn2RnaWgebwigAXanAcJDSTwm47Shqp8CroGmdGXXAYHdoC mSPbuR+KJYggmuenmX7BNGfdn4bXAaz/hyJD0HaiXwSgoM7cIGVfsfozZR0XfVIyHsyw MmkWKfGZ4OFjYAzGslo9zgb8Jc2SIKixiXZX/44hghYzSqgbr9+v176q3rfNsdSRfkmF 7DpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1766221734; x=1766826534; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=9FCav/YO9x/c2l0kK2rjBlOI4AUzqgUWfXFrOx8/XtI=; b=VTAir75+950Hj/CDTjbVLUWXgRV/PkjzYo6G8Qsit6JkMAyBqMY80JQIWT5TKeLYhV uLVR0zhGZmZpp8oDgux+6LTk5W28MeJgro3b/jqivoWr1eBUES6Td6+hAXTm0aVznYsQ klNL88aaYtu0CWGJZ7q3NrnhfHgPsPuTbxX1aQXLViZl15IpVKfZtgcgKeCz7cP8vkW9 7wZIvWPqvE5TKzSW+aYulIGJdz/F+Kxa83MLjGff2k2p0WS2wKYEUpqqWca8S58NsAxG u1sytJ5gbPSKEmacP7V1kmvphLVKnc5P2Yb2wmiMbfh9XwABohw+setPIdUYDq0pvq0c W7jw== X-Forwarded-Encrypted: i=1; AJvYcCUvjRc8JhZMLGoWBfp5wIAFC3g5FfPYDtSw9fmigko8py1bf+HNDY7gA2D+0eLmL4UUvhzG9S4jy1c5CsI=@vger.kernel.org X-Gm-Message-State: AOJu0YxFMj+4Up+A/aemuUriL6Oha/R31gLpQi8CTYbmG6rfzUYZMc6z 9e0G0PclENG5Jd6ASVMVYuUwsnZMS9y0LZG4Pn1Xlo3mLhWH5TpQbzxm2gsXUFup4Is= X-Gm-Gg: AY/fxX5qIeJNTBZ4fZMU5Guo0Sm3BMQiCeowufpi1LpgQyok10lEL/+i+IpPqr0RO0t Cs5p4wAowT4Fq05sqfI4tZ4doQQR35zlfZgQQ6M4sYKbKOMpI2Hs0TpS9zjKwVPacBXcfM25IJk cHdkQhW6IRsjDT1IzAnRTQw3nA5W2gRrVEzrpFiCNQLrNnlZ7eEQaszmHDSQLSlMqpRdnryphE9 olgXiQgrBrjNpT3WaT3UTJB46rrnGc4CxpwQwHE6okOamOJxI2IJ2xDNa/k5eedz0GnlhA2Gt0e K2gQnuAv+ZKjr0AIUAEqi/PkKIYhgh6JmICPq/gmDR5Pw1BqrLtLUgocnFlgxXISYjQzHOWjDcu RYFLGHf5IzFkiLLzUKEdb4Lb3YsrfV3lJEUCB2d/z9BRCKVGRVSnyyLqXE2VD0xrzW7uVMsepJC laIc+BnDn7AyJttgQ6U8AVQDrVf2xC+tuErjIhcRt5UfoxoZN8hvLY X-Google-Smtp-Source: AGHT+IF2jkxbF7mAn+s4RGO9FXrxMFZTWrnAnqrw89U2+BW6yKGvETNKZF8EMf2cRZHfSphIJaGSBg== X-Received: by 2002:a05:6a00:330a:b0:7e8:43f5:bd46 with SMTP id d2e1a72fcca58-7ff67257bb3mr4181960b3a.50.1766221733916; Sat, 20 Dec 2025 01:08:53 -0800 (PST) Received: from J9GPGXL7NT.bytedance.net ([139.177.225.247]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7ff7e48cd07sm4623647b3a.46.2025.12.20.01.08.49 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Sat, 20 Dec 2025 01:08:53 -0800 (PST) From: Xu Lu To: anup@brainfault.org, atish.patra@linux.dev, pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, tglx@linutronix.de Cc: kvm@vger.kernel.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Xu Lu Subject: [PATCH v2] irqchip/riscv-imsic: Adjust vs irq files num according to MMIO resources Date: Sat, 20 Dec 2025 17:08:44 +0800 Message-ID: <20251220090844.46441-1-luxu.kernel@bytedance.com> X-Mailer: git-send-email 2.50.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" During initialization, kernel maps the MMIO resources of IMSIC, which is parsed from ACPI or DTS and may not strictly contains all guest interrupt files. Page fault happens when KVM wrongly allocates an unmapped guest interrupt file and writes it. Thus, during initialization, we calculate the number of available guest interrupt files according to MMIO resources and constrain the number of guest interrupt files that can be allocated by KVM. Signed-off-by: Xu Lu --- arch/riscv/kvm/aia.c | 2 +- drivers/irqchip/irq-riscv-imsic-state.c | 7 ++++++- include/linux/irqchip/riscv-imsic.h | 3 +++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/arch/riscv/kvm/aia.c b/arch/riscv/kvm/aia.c index dad3181856600..cac3c2b51d724 100644 --- a/arch/riscv/kvm/aia.c +++ b/arch/riscv/kvm/aia.c @@ -630,7 +630,7 @@ int kvm_riscv_aia_init(void) */ if (gc) kvm_riscv_aia_nr_hgei =3D min((ulong)kvm_riscv_aia_nr_hgei, - BIT(gc->guest_index_bits) - 1); + gc->nr_guest_files); else kvm_riscv_aia_nr_hgei =3D 0; =20 diff --git a/drivers/irqchip/irq-riscv-imsic-state.c b/drivers/irqchip/irq-= riscv-imsic-state.c index dc95ad856d80a..1e982ce024a47 100644 --- a/drivers/irqchip/irq-riscv-imsic-state.c +++ b/drivers/irqchip/irq-riscv-imsic-state.c @@ -794,7 +794,7 @@ static int __init imsic_parse_fwnode(struct fwnode_hand= le *fwnode, =20 int __init imsic_setup_state(struct fwnode_handle *fwnode, void *opaque) { - u32 i, j, index, nr_parent_irqs, nr_mmios, nr_handlers =3D 0; + u32 i, j, index, nr_parent_irqs, nr_mmios, nr_guest_files, nr_handlers = =3D 0; struct imsic_global_config *global; struct imsic_local_config *local; void __iomem **mmios_va =3D NULL; @@ -888,6 +888,7 @@ int __init imsic_setup_state(struct fwnode_handle *fwno= de, void *opaque) } =20 /* Configure handlers for target CPUs */ + global->nr_guest_files =3D BIT(global->guest_index_bits) - 1; for (i =3D 0; i < nr_parent_irqs; i++) { rc =3D imsic_get_parent_hartid(fwnode, i, &hartid); if (rc) { @@ -928,6 +929,10 @@ int __init imsic_setup_state(struct fwnode_handle *fwn= ode, void *opaque) local->msi_pa =3D mmios[index].start + reloff; local->msi_va =3D mmios_va[index] + reloff; =20 + nr_guest_files =3D (resource_size(&mmios[index]) - reloff) / IMSIC_MMIO_= PAGE_SZ - 1; + global->nr_guest_files =3D global->nr_guest_files > nr_guest_files ? nr_= guest_files : + global->nr_guest_files; + nr_handlers++; } =20 diff --git a/include/linux/irqchip/riscv-imsic.h b/include/linux/irqchip/ri= scv-imsic.h index 7494952c55187..43aed52385008 100644 --- a/include/linux/irqchip/riscv-imsic.h +++ b/include/linux/irqchip/riscv-imsic.h @@ -69,6 +69,9 @@ struct imsic_global_config { /* Number of guest interrupt identities */ u32 nr_guest_ids; =20 + /* Number of guest interrupt files per core */ + u32 nr_guest_files; + /* Per-CPU IMSIC addresses */ struct imsic_local_config __percpu *local; }; --=20 2.20.1