From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C20513DAC13; Tue, 1 Sep 2026 16:09:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788278956; cv=none; b=bjdYshfKH19UdemjL3pBlF2i7QxckoBlgKkLTdnI4oj/XqdqnDQmqOqqrdPOTaPhzpZEqLjEH8TzPp6RTK9t3QZP8809v9qQBMSYqP5iS1wU9SOtziBGMrhsgwIMexfEFGLC0S/se/K9jodQQkygrt0HySGDR0WstrbS+luS5Xk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788278956; c=relaxed/simple; bh=vVRTAJM2qAJtjzVNw7AfiDMOZQ6QUxUlTdr6404pWUc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=fzB/O9JkLXAUkto7GWWnxRQqE/uTIsf+L1W2ICS316+oroRikGb15ZTyxMiMaYVvExFlH4se36FPcviXCrUYzW38+uF0PpAXhVctjeLT0WwIji4TYjA7hJp9dBfnBoDkswyLGYHjIvOLbk9cQuH/scseomIYqLzplxcc/mRRCwk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=RtNnEY7h; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="RtNnEY7h" Received: by smtp.kernel.org (Postfix) with ESMTPS id 7C349C32782; Tue, 1 Sep 2026 16:09:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1788278956; bh=vVRTAJM2qAJtjzVNw7AfiDMOZQ6QUxUlTdr6404pWUc=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=RtNnEY7hoph2NGV04fm7KqnQgfxO8XyXwmiTGUGtI3nVJfe16N40RwZkDYLT+ksZO 2YY+rS0wl+a+/TsgI1fmeUZgiWHI+idrSNb+jMfzJSc+f7p5rqYHt+gtS1nGBEt+IQ dRR3YQt/Hkm0k0lq+vc9whkbty2GyDmdKXMhfr+Hcreqfr/NlHeuETdjUGbqFSo39e SNoFvcFY8ssqH8I5/kD0XGsv8IgmE43VcBeYNH6Mw5uGP91kgbiGcsWCMkvivJ5157 W5XF6Y7c/8MUnW/r6hyHIp1Bo/d4MA3WA5xjWPleDxXUVo51o6H1b21u9574RPh0Oq 8D6lZfwiTb+eA== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 63CD2C624D7; Tue, 1 Sep 2026 16:09:16 +0000 (UTC) From: Ke Sun via B4 Relay Date: Wed, 02 Sep 2026 00:09:03 +0800 Subject: [PATCH 4/9] drm/tyr: add per-file VM pool Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20260902-tyr-ioctls-v1-4-e0fdbf8bd108@kylinos.cn> References: <20260902-tyr-ioctls-v1-0-e0fdbf8bd108@kylinos.cn> In-Reply-To: <20260902-tyr-ioctls-v1-0-e0fdbf8bd108@kylinos.cn> To: rust-for-linux@vger.kernel.org Cc: Miguel Ojeda , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , Danilo Krummrich , Daniel Almeida , Tamir Duberstein , Alexandre Courbot , =?utf-8?q?Onur_=C3=96zkan?= , Lorenzo Stoakes , "Liam R. Howlett" , Lyude Paul , David Airlie , Simona Vetter , linux-kernel@vger.kernel.org, linux-mm@kvack.org, dri-devel@lists.freedesktop.org, Ke Sun , Alvin Sun X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1788278954; l=3857; i=sunke@kylinos.cn; s=20260317; h=from:subject:message-id; bh=GYNt5fASDjvzxxgWZLDWPIxhdkvjI20iaVSTTPTfRwI=; b=zNDRrCK4SuOMnUEDGOcfEfklYsW34X7DcrbeHuulWK+S9BJ2TEp/jQ1FknCrKMDLdreWsyCD/ nGswWYrR5lrDce2bdKmuXNKhOd67JYC7lkzIJftaHcELHGgPGNrZ0Jc X-Developer-Key: i=sunke@kylinos.cn; a=ed25519; pk=CHcwQp8GSoj25V/L1ZWNSQjWp9eSIb0s9LKr0Nm3WuE= X-Endpoint-Received: by B4 Relay for sunke@kylinos.cn/20260317 with auth_id=950 X-Original-From: Ke Sun Reply-To: sunke@kylinos.cn From: Alvin Sun Add a per-file VM pool using the IdPool for ID allocation and an XArray for VM storage. Signed-off-by: Alvin Sun --- drivers/gpu/drm/tyr/pool.rs | 102 ++++++++++++++++++++++++++++++++++++++++++++ drivers/gpu/drm/tyr/tyr.rs | 1 + 2 files changed, 103 insertions(+) diff --git a/drivers/gpu/drm/tyr/pool.rs b/drivers/gpu/drm/tyr/pool.rs new file mode 100644 index 0000000000000..1745116930c3e --- /dev/null +++ b/drivers/gpu/drm/tyr/pool.rs @@ -0,0 +1,102 @@ +// SPDX-License-Identifier: GPL-2.0 or MIT + +//! Per-drm-file VM id pool. + +use kernel::{ + id_pool::IdPool, + new_mutex, + prelude::*, + sync::{ + Arc, + ArcBorrow, + Mutex, // + }, + xarray::{ + AllocKind, + XArray, // + }, // +}; + +use crate::vm::Vm; + +/// Maximum number of VMs per open file. Matches panthor's +/// `PANTHOR_MAX_VMS_PER_FILE`. +pub(crate) const PANTHOR_MAX_VMS_PER_FILE: u32 = 32; + +/// Per-open-file pool of VMs. +#[pin_data] +pub(crate) struct VmPool<'drm> { + #[pin] + ids: Mutex, + #[pin] + vms: XArray>>, +} + +impl<'drm> VmPool<'drm> { + /// Creates a new [`VmPool`] with capacity for [`PANTHOR_MAX_VMS_PER_FILE`] VMs. + pub(crate) fn new() -> Result> { + let ids = IdPool::with_capacity(PANTHOR_MAX_VMS_PER_FILE as usize, GFP_KERNEL)?; + Ok(pin_init!(Self { + ids <- new_mutex!(ids), + vms <- XArray::new(AllocKind::Alloc), + })) + } + + /// Inserts a VM into the pool, returning the allocated ID. + pub(crate) fn add(&self, vm: ArcBorrow<'_, Vm<'drm>>) -> Result { + let id = { + let mut ids = self.ids.lock(); + ids.find_unused_id(1).ok_or(ENOSPC)?.acquire() + }; + + let vm: Arc> = vm.into(); + let mut vms = self.vms.lock(); + match vms.store(id, vm, GFP_KERNEL) { + Ok(previous) => { + // Drop the previous entry (expected `None`). + drop(previous); + Ok(id as u32) + } + Err(err) => { + // Drop the XArray spinlock before acquiring the `ids` mutex. + drop(vms); + // Release the stored entry and the pooled id. + drop(err.value); + let mut ids = self.ids.lock(); + ids.release_id(id); + Err(err.error) + } + } + } + + /// Removes the VM with the given ID. + pub(crate) fn remove(&self, id: u32) -> Result>> { + let mut vms = self.vms.lock(); + match vms.remove(id as usize) { + Some(vm) => { + drop(vms); + let mut ids = self.ids.lock(); + ids.release_id(id as usize); + Ok(vm) + } + None => Err(EINVAL), + } + } + + /// Gets the VM with the given ID. + pub(crate) fn get(&self, id: u32) -> Option>> { + let vms = self.vms.lock(); + let borrow = vms.get(id as usize)?; + Some(Arc::from(borrow)) + } + + /// Removes and returns the first VM in the pool. + pub(crate) fn pop_first(&self) -> Option>> { + for id in 0..PANTHOR_MAX_VMS_PER_FILE { + if let Ok(vm) = self.remove(id) { + return Some(vm); + } + } + None + } +} diff --git a/drivers/gpu/drm/tyr/tyr.rs b/drivers/gpu/drm/tyr/tyr.rs index e7ec450bdc9c0..8c0c18c1a970b 100644 --- a/drivers/gpu/drm/tyr/tyr.rs +++ b/drivers/gpu/drm/tyr/tyr.rs @@ -13,6 +13,7 @@ mod gem; mod gpu; mod mmu; +mod pool; mod regs; mod slot; mod vm; -- 2.43.0