From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (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 81DF0606A322 for ; Fri, 4 Dec 2026 19:35:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=UPufYS10 Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id F0D1910E1B5; Fri, 4 Dec 2026 19:35:23 +0000 (UTC) Received: from tor.source.kernel.org (tor.source.kernel.org [172.105.4.254]) by gabe.freedesktop.org (Postfix) with ESMTPS id D08DF10E7CF for ; Fri, 4 Dec 2026 19:35:17 +0000 (UTC) Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by tor.source.kernel.org (Postfix) with ESMTP id 9F7D760EF9; Fri, 4 Dec 2026 19:35:16 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CE9981F0093B; Fri, 4 Dec 2026 19:35:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796412916; bh=E5PfgxyBCrq4v7AbUuC+ez+H3iqnyEcYjKAc+I1uMKU=; h=Date:Subject:Cc:To:From:References:In-Reply-To; b=UPufYS10Tq0X5R5y4Fc60bjjF0ZCytg5QIMUb5qw4HARtHWMQkeaV9lg7BVmgBs8H /zeIds0zdCwIYvdAQnM/UYebRn2t7N6avqrEjWXFJXUXZpzYaI52qx8/O4PaAj/XlK KSlTK9+KkxiavOOYjvMHfIK7CFWrsBFHcaCEmcZuDHm2aK/ElWS3Eklkl0AE+2waxV okMD98U2mgf/zW3ipUYIBP2SBRPEbn6VxhOsuiNLdnpRAt2E+VPpDTGYYPRtnAa57H +NXHbh3YeTB7+f6GCeOTyxXuMMgh1qXnScGY7UZanadCibONrhgxOh1oQpMMFm6cWm EoVkpFif4w2ag== X-Archived-By: Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Fri, 04 Dec 2026 20:35:06 +0100 Message-Id: Subject: Re: [PATCH v6 13/34] gpu: nova-core: mm: add BarMapping Cc: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , To: "Alok Kumar" From: "Danilo Krummrich" References: In-Reply-To: X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "dri-devel" X-Spam-Status: No, score=-6.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 On Wed Dec 2, 2026 at 9:28 AM CET, Alok Kumar wrote: > +/// A BAR mapping that borrows its backing VRAM allocation. > +#[must_use] > +pub(crate) struct BarMapping<'pool, 'map, 'gpu> { This can be reduced to only two lifetimes, without Mutex> makei= ng 'gpu invariant (which goes away on latest drm-rust-next) it can even be uni= fied to a single lifetime. However, a single lifetime may be inconvinient for the region() accessor, a= s it would only expose a common lifetime. So I think it makes more sense to keep= two lifetimes. I think 'res or 'env for the shared lifetime of BarUserAccess and GpuMm, an= d 'reg for the region lifetime is reasonable. A generic lifetime ('a, 'b) sho= uld be fine too. > + /// Mapping of `region`. > + access: BarUserAccess<'map, 'gpu>, > + mm: &'map Mutex>, > + /// The VRAM region that is mapped. > + region: VramRegion<'pool>, > + unmap_error: Option, Ugh, I don't like that, some more on this below. > +} > + > +#[expect(dead_code)] > +impl<'pool, 'map, 'gpu> BarMapping<'pool, 'map, 'gpu> { > + /// Maps `region`, which must start and end on a page boundary. > + pub(crate) fn new( > + bar_user: &'map BarUser<'gpu>, > + mm: &'map Mutex>, > + region: VramRegion<'pool>, > + writable: bool, > + ) -> Result { > + const U64_PAGE_SIZE: u64 =3D cv!(PAGE_SIZE); > + > + let range =3D region.range(); > + if !range.start.is_multiple_of(U64_PAGE_SIZE) || !range.end.is_m= ultiple_of(U64_PAGE_SIZE) { > + return Err(EINVAL); > + } > + > + let num_pages =3D usize::try_from(region.len() / U64_PAGE_SIZE).= map_err(|_| EOVERFLOW)?; > + let mut pfns =3D KVec::with_capacity(num_pages, GFP_KERNEL)?; > + for page in 0..num_pages { > + let address =3D u64::from_safe_cast(page) > + .checked_mul(U64_PAGE_SIZE) > + .and_then(|byte_offset| byte_offset.checked_add(range.st= art)) > + .ok_or(EOVERFLOW)?; > + pfns.push(Pfn::from(VramAddress::from_raw(address)), GFP_KER= NEL)?; > + } > + > + let access =3D bar_user.map(&mut mm.lock(), &pfns, writable)?; > + > + Ok(Self { > + access, > + mm, > + region, > + unmap_error: None, > + }) > + } > + > + pub(crate) fn region(&self) -> &VramRegion<'pool> { > + &self.region > + } > + > + fn access(&self) -> Result<&BarUserAccess<'map, 'gpu>> { > + if self.access.mapped.is_none() { > + return Err(ENODEV); > + } > + if let Some(error) =3D self.unmap_error { > + return Err(error); > + } > + Ok(&self.access) > + } > + > + /// Returns a typed MMIO view of a `T` located at the start of the m= apped region. > + /// > + /// Returns an error if the mapping is not live anymore, if a `T` do= es not fit within the > + /// region, or if the region is not properly aligned for `T`. > + pub(crate) fn view(&self) -> Result> { > + // The mapping covers exactly the region, so this also checks th= at the `T` fits within it. > + self.access()?.view(0) > + } > + > + /// Unmap while retaining the mapping and its backing storage on fai= lure. > + /// > + /// The first error is retained; later calls do not retry the operat= ion. > + pub(crate) fn unmap(&mut self) -> Result { > + if let Some(error) =3D self.unmap_error { > + return Err(error); > + } So, this checks whether unmap() is called although it has been unmapped already... > + let result =3D self.access.unmap(&mut self.mm.lock()); > + if let Err(error) =3D result { > + self.unmap_error =3D Some(error); > + } ...and here the internal option is set, which also the below destructor nee= ds to distinguish between the two states 'mapped' and 'unmapped'. This is much better reflected by having separate types. > + result > + } > +} > + > +impl Drop for BarMapping<'_, '_, '_> { > + /// Drop unmaps the region and may sleep. Call [`Self::unmap`] to ob= serve errors; > + /// on failure the owner must retain this object until device teardo= wn. > + fn drop(&mut self) { > + if self.unmap_error.is_some() { > + return; > + } > + if let Err(error) =3D self.unmap() { > + kernel::pr_err!("failed to unmap BAR1 region: {:?}\n", error= ); > + } > + } > +} > +