From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=165.173.182.11 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796428452; b=up8PYl+WfHEIvxcmRPAaKqUNDnMyPC2IscJqzBSY6yMZ8iCvhjlYfd1HjS05d5l+RLfD R5GDTLvUCiJVbDu5cBAABKpoKl1fSjrLqN1iRZ7NbJDnbDAsj5jPi4r/+gMNGFxj03Gam Ltvxxv6VGVV8VYkPCpQ9ChcBVNneH6JXen7eIqu2u7R2FCEKHPzX8qOcEiE/SaVuYWNOy wXxqIPd0fRkl9RDEppKJsKB+zqe8lY/wCRxgbrJJTHsdQmpmr8GNoPCyVLxc98p2s/8WU UIqcg9ZElGRrTYgvpRhq3FgNRDbK2/0vI38YwytM4/nr5S1xj0sWEm3KboO/guPjxiw== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796428452; h=DMARC-Filter:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Content-Transfer-Encoding:Message-Id:References: In-Reply-To:To:Cc:X-Mailer:X-Developer-Signature:X-Developer-Key; bh=3DHwBC9P18d4QwpVr4HCD5bpFJNCnWLBrOgeW3aIZl8=; b=J/ZQpcVcCVvDxiGjYPEnSEg+lvqJhGdon6Crlanvf7MmZjQ41WBJDU2J8YSJFpdppCMb B486//W05r7Z3AwIwqwx8o36pkDvi8WCJfKRkOEH3wQptFIhdb6yf3gn2i5JemcYdOqT/ SLwHEv+xhm6VfxnykxS+qZzCrtUg8ylchcY4OwZcSwWqToxMuIH7ybbelxbjV4JzT8HRO cZRnk1kOxcIqVyFUE4m+eDP9FvVrbK9+Hbdv1WBg9lFXPFR3Dllr+C6G7FE9F8XzKDXoN HMzmk5dzq0LmDDlQNTv3ChufX2Xi6507sn250weZKCdbfNxvk25pko7X7IdNcvwL9gw== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass header.from=collabora.com; spf=pass smtp.mailfrom=collabora.com; dkim=pass header.d=collabora.com header.i=@collabora.com header.a=rsa-sha256 header.s=zohomail header.b=KsJlxQV+; arc=pass header.oldest-pass=0 smtp.remote-ip=165.173.182.11 Received: from sender5-op-o11.zoho.com (sender5-op-o11.zoho.com [165.173.182.11]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6653B606A322; Fri, 4 Dec 2026 23:54:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 6653B606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=collabora.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=collabora.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=collabora.com header.i=@collabora.com header.a=rsa-sha256 header.s=zohomail header.b=KsJlxQV+ ARC-Seal: i=1; a=rsa-sha256; t=1796428429; cv=none; d=zohomail.com; s=zohoarc; b=WExyer8lQutwzv7jjcdDABt3R6qpP+4fC5jXNvkeuuEyyJQqcWxIIfpg6wlgykOHrcLeq+yAES6OXnu2F1lBoubMbf6UP7VW4tw0xLMl8OMITfr8yXxJhIJPcFN6Wp88k9ALULvGiN3lbocX6zV4639k/KWc1FUNSawuDk5Ey9I= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1796428429; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:MIME-Version:Message-ID:Subject:Subject:To:To:Message-Id:Reply-To; bh=3DHwBC9P18d4QwpVr4HCD5bpFJNCnWLBrOgeW3aIZl8=; b=XyN1X6nfsIKCu4qpP0rcrp3Ktk5UOnYgeLF2VbO/u0D3W8e8RZlRYb07yG069tNiPrw0DscSgqK2uTobA3yvPKTs25u9FfLcY1yUPGHAPpoMq50pcOM9X6wKlsxzTYqq8dpeX3noQj4gkWJ/SXE9t/Wwc/5LJ86po4ljV9RmE20= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=collabora.com; spf=pass smtp.mailfrom=deborah.brouwer@collabora.com; dmarc=pass header.from= DKIM-Signature: a=rsa-sha256; b=KsJlxQV+f7cG098mqm3J+M2+SAg1OBQ/WDmi4RzE34ydlhfWb8R9peWzm6cExL7GTPk9Me7DnR8yLzt0/YoYq6Olj3Cq1L3ZnhjvcqB3XeiZ5zySkBWK+c3S+SprpD5to55E3Ut76OChMqvF+9ivdJniasyeigbKBS63nVDdftk=; c=relaxed/relaxed; s=zohomail; d=collabora.com; v=1; bh=3DHwBC9P18d4QwpVr4HCD5bpFJNCnWLBrOgeW3aIZl8=; h=Content-Description:Content-ID:Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Owner:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Resent-Sender:Resent-Cc:Resent-Date:Resent-To:Reply-To:Resent-Message-ID:Resent-From:Sender:Subject:To:Date:From:Message-Id:Reply-To:Subject:To:Cc; Received: by smtp.zohomail.com with SMTPS id 1796428427388261.07665388390126; Fri, 4 Dec 2026 15:53:47 -0800 (PST) From: Deborah Brouwer Date: Fri, 04 Dec 2026 16:53:43 -0700 Subject: [PATCH 2/2] drm/tyr: unmap regions out-of-sync with gpuvm X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261204-b4-mark_failed-v1-2-39a5dca6d594@collabora.com> References: <20261204-b4-mark_failed-v1-0-39a5dca6d594@collabora.com> In-Reply-To: <20261204-b4-mark_failed-v1-0-39a5dca6d594@collabora.com> To: Danilo Krummrich , Alice Ryhl , Daniel Almeida , David Airlie , Simona Vetter Cc: dri-devel@lists.freedesktop.org, rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, Deborah Brouwer , sunke@kylinos.cn, alvin.sun@linux.dev, Sami Tolvanen X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=10112; i=deborah.brouwer@collabora.com; h=from:subject:message-id; bh=8Za8LHR5t3ttHQrXaPWvj6u7sg20Vsegqv7h/rSV/nQ=; b=owGbwMvMwCVWuULzOU9c7WvG02pJDNnCQR0zmPZyvY3Ivz3t7W2PwGfeap+tJ3XGZq2ZJ7v7x 6e4rdHdHaUsDGJcDLJiiixn7Y16xKveG+nO/98MM4eVCWQIAxenAEzEbjHDH77b7tyVF1urFf8q yfwpW+nHcO3xYc8jziV/P0w8fa91ijrDf29ByQVf9uw5zePy893e0B8bH3HxpKTIcd8s9Z/WE7U kiBsA X-Developer-Key: i=deborah.brouwer@collabora.com; a=openpgp; fpr=CD3F328C177AEF322D9FFF8379A829E70C5E7DEB If a mapping fails either during sm_step_map() or on sm_step_remap(), an error is returned without updating gpuvm so that a page table may be left partially mapped and out of sync with gpuvm bookkeeping. Keep track of the range that may be out of sync and make the unmapping functions aware of these potentially inconsistent regions so that they can unmap the remaining mapped pages without failing on holes in the page table. Marking the VM unusable and disabling its hardware address space following a failed page-table update will be addressed in a follow-up patch when the vm_bind ioctl is enabled. Co-developed-by: Sami Tolvanen Signed-off-by: Sami Tolvanen Signed-off-by: Deborah Brouwer --- drivers/gpu/drm/tyr/vm.rs | 132 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 121 insertions(+), 11 deletions(-) diff --git a/drivers/gpu/drm/tyr/vm.rs b/drivers/gpu/drm/tyr/vm.rs index e80f2cdd0fb5..98a8802c2c4b 100644 --- a/drivers/gpu/drm/tyr/vm.rs +++ b/drivers/gpu/drm/tyr/vm.rs @@ -72,6 +72,25 @@ regs::gpu_control::MMU_FEATURES, }; +/// With a 4KiB granule, 2MiB is the smallest block mapping size +/// and corresponds to a 2 MiB huge page. +/// +/// ARM64 LPAE stage 1 translation (4 KiB granule): +/// +/// L0 (512 GiB per entry) +/// | +/// L1 (1 GiB per entry) ---- 1 GiB block +/// | +/// L2 (2 MiB per entry) ---- 2 MiB block +/// | +/// L3 (4 KiB per entry) ---- 4 KiB page +/// +/// See [AArch64 Memory Management Guide - Translation granule]. +/// +/// [AArch64 Memory Management Guide - Translation granule]: +/// https://support.arm.com/documentation/101811/0106/Translation-granule +const PT_MIN_BLOCK_SIZE: usize = usize::SZ_2M; + impl_flags!( /// Flags controlling virtual memory mapping behavior. /// @@ -328,9 +347,64 @@ fn drop(&mut self) { /// Implements [`DriverGpuVm`] to provide VM operation callbacks (map, unmap, remap) /// and associated types for buffer objects, virtual addresses, and contexts. pub(crate) struct GpuVmData<'drm> { + /// Range that failed page-table updates may have left out of sync with + /// the VA tree. Outside it, the page table matches the VA tree. It is + /// empty until the first failure. + pt_out_of_sync: Range, _phantom: PhantomData<&'drm ()>, } +impl<'drm> GpuVmData<'drm> { + /// Records the region potentially left out of sync with gpuvm + /// after a failed page-table update. + fn mark_failed(&mut self, _context: &PtUpdateContext<'_, '_>, region: &Range) { + // Out of an abundance of caution, expand the affected region + // in case it was mapped as a block (i.e. a huge page). + let block = Alignment::new::<{ PT_MIN_BLOCK_SIZE }>(); + let end = region.end.align_up(block).unwrap_or(u64::MAX); + let region = region.start.align_down(block)..end; + + let span = &mut self.pt_out_of_sync; + *span = if span.is_empty() { + region + } else { + span.start.min(region.start)..span.end.max(region.end) + }; + // TODO when vm bind is enabled: + // 1) Turn the VM into a state where it can't be used. + // 2) disable the hardware address space so that gpu can't access failed mapping + } + + /// Unmaps `region` from the page table. + /// + /// A failed update can leave holes in the range it covered, so only the + /// part of `region` inside `pt_out_of_sync` is unmapped page by page. + fn unmap(&mut self, context: &PtUpdateContext<'_, '_>, region: Range) -> Result { + // Determine if any part of the region may be out of sync with gpuvm. + let span = &self.pt_out_of_sync; + let to_probe = region.start.max(span.start)..region.end.min(span.end); + + if to_probe.is_empty() { + return self.unmap_in_sync(context, region); + } + + self.unmap_in_sync(context, region.start..to_probe.start) + .and_then(|()| pt_unmap_mapped(context.dev, context.pt, to_probe.clone())) + .and_then(|()| self.unmap_in_sync(context, to_probe.end..region.end)) + .inspect_err(|_| self.mark_failed(context, ®ion)) + } + + /// Unmaps `region`, which lies outside `pt_out_of_sync` and so is fully mapped. + fn unmap_in_sync(&mut self, context: &PtUpdateContext<'_, '_>, region: Range) -> Result { + if pt_unmap(context.dev, context.pt, region.clone()).is_ok() { + return Ok(()); + } + + self.mark_failed(context, ®ion); + pt_unmap_mapped(context.dev, context.pt, region) + } +} + /// Per-mapping private data stored on each `GpuVa`. /// /// A remap splits an existing mapping and may have to rebuild page-table @@ -392,6 +466,7 @@ pub(crate) fn new( range.clone(), reserve_range, GpuVmData::<'drm> { + pt_out_of_sync: 0..0, _phantom: PhantomData::<&()>, }, ) @@ -708,6 +783,7 @@ fn sm_step_map<'op>( start_iova..(start_iova + total_mapped), ); } + self.mark_failed(context, &(start_iova..start_iova + op.length())); return Err(e); } }; @@ -720,7 +796,9 @@ fn sm_step_map<'op>( let total_mapped = iova - start_iova; if total_mapped > 0 { - let _ = pt_unmap(context.dev, context.pt, start_iova..iova); + if pt_unmap(context.dev, context.pt, start_iova..iova).is_err() { + self.mark_failed(context, &(start_iova..iova)); + } } dev_err!( @@ -747,7 +825,7 @@ fn sm_step_unmap<'op>( let length = op.va().length(); let region = start_iova..(start_iova + length); - pt_unmap(context.dev, context.pt, region.clone()).inspect_err(|e| { + self.unmap(context, region.clone()).inspect_err(|e| { dev_err!( context.dev, "Failed to unmap region {:#x}..{:#x}: {:?}", @@ -824,7 +902,7 @@ fn sm_step_remap<'op>( if region_end > region_start { let region = region_start..region_end; context.extend_lock(region.clone())?; - pt_unmap(context.dev, context.pt, region.clone()).inspect_err(|e| { + self.unmap(context, region.clone()).inspect_err(|e| { dev_err!( context.dev, "Failed to unmap remap region {:#x}..{:#x}: {:?}", @@ -843,7 +921,8 @@ fn sm_step_remap<'op>( head_paddr, unmap_start - aligned_start, prot, - )?; + ) + .inspect_err(|_| self.mark_failed(context, &(region_start..region_end)))?; } if let Some(tail_paddr) = tail_paddr { pt_map( @@ -853,7 +932,8 @@ fn sm_step_remap<'op>( tail_paddr, aligned_end - unmap_end, prot, - )?; + ) + .inspect_err(|_| self.mark_failed(context, &(region_start..region_end)))?; } let (op_remapped, remap_ret) = @@ -949,8 +1029,8 @@ fn get_pgsize(addr: u64, size: u64) -> (usize, usize) { /// virtual address `iova`, using the protection flags specified in `prot`. It /// automatically selects optimal page sizes to minimize page table overhead. /// -/// If the mapping fails partway through, all successfully mapped pages are -/// unmapped before returning an error. +/// If the mapping fails partway through, an attempt is made to unmap +/// all successfully mapped pages before returning an error. /// /// Returns the number of bytes successfully mapped. fn pt_map( @@ -1055,10 +1135,10 @@ fn pt_unmap(dev: &Device, pt: &IoPageTable<'_, ARM64LPAES1>, range: Range) // SAFETY: // No other io-pgtable operation can currently access this range because Tyr holds - // the gpuvm_unique mutex for the entire sm_unmap() operation. - // We know that this page table has one or more consecutive mappings - // starting at `iova` with the total size of `pgcount * pgsize` because - // gpuvm callbacks provide exactly the range that was previously mapped. + // the gpuvm_unique mutex for all vm update operations. + // Every range passed here is fully mapped, so this page table has one + // or more consecutive mappings starting at `iova` with the total size + // of `pgcount * pgsize`. let unmapped = unsafe { pt.unmap_pages(iova_usize, pgsize, pgcount) }; if unmapped == 0 { @@ -1073,3 +1153,33 @@ fn pt_unmap(dev: &Device, pt: &IoPageTable<'_, ARM64LPAES1>, range: Range) Ok(()) } + +/// Unmaps the pages of `range` that are still mapped, skipping any holes. +fn pt_unmap_mapped(dev: &Device, pt: &IoPageTable<'_, ARM64LPAES1>, range: Range) -> Result { + let mut run_start = None; + let mut iova = range.start; + + while iova < range.end { + let iova_usize = usize::try_from(iova).map_err(|_| EOVERFLOW)?; + + // SAFETY: The VM update holds the `gpuvm_unique` mutex, so no other + // io-pgtable operation runs on this page table. + let mapped = unsafe { pt.iova_to_phys(iova_usize) }.is_some(); + + match (mapped, run_start) { + (true, None) => run_start = Some(iova), + (false, Some(start)) => { + pt_unmap(dev, pt, start..iova)?; + run_start = None; + } + _ => {} + } + + iova += u64::SZ_4K; + } + + match run_start { + Some(start) => pt_unmap(dev, pt, start..range.end), + None => Ok(()), + } +} -- 2.56.0