From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796412916; b=ywGNjHLj73m1Bzhlog3ErCa+0oumVuvilEcyi/jRjaC8FBE5tobP5byGWL+U6K/dD9N1 KBACG95UyQf75MyHnJX88V7m5ihxna7Tb4po+TDEkzOp7HKOikq9VdHKoMuWu+gANoljI iP29OiPZ4/SAtHj8ti+VfajiUe+hdhUL4RHxxl4XCXck73ZTHcFYOtYf9t3de6ytCi1FL 2vODY92bdxNHM2lFP/Vk8bZXGMJgm2DllJDh4bWiEcF04ngmEfLPvKKuxuv1cRnni4DW/ ZuSLA70c6Odzoi1y3kzvgKXAXvnDRFr+uJWpy0ad22rtoQAl9geXd1uyBJ1PvLrzTZQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796412916; h=Received:DKIM-Signature:Mime-Version:Content-Transfer-Encoding: Content-Type:Date:Message-Id:Subject:Cc:To:From:References: In-Reply-To; bh=E5PfgxyBCrq4v7AbUuC+ez+H3iqnyEcYjKAc+I1uMKU=; b=z3ptKtMWmUNJoR5zD1MFyVahQjgIpQ/s30QQ8+j8J3q0bPF2G7MJ/OiAWepyfhckrNu9 1fFiVQJ7j3QlxERbAo2lsIBrzbZgQH/cDnG0Fmqdp2Z6xxwtcZGpdydo28fvIxu7msISc kub3k536pCgIfISQJB9qU2LOSrhMQvXvObxibfGXXarB4Cwtmsrx8Xm/7xqE1Nn2InOym nIRMdzOAR2gThwZhL+CiyLttdErXe/ojttIRl2B31nwe+iP/JjyiWDNsaPPaY9QtAG+WU gXmvONC57KTTPtpViD96q0RWMyxfxOJt1JiJyQ+jzMCHm88zIsY7IGeQEB4oWxlCuYA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=UPufYS10; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7785A606A322; Fri, 4 Dec 2026 19:35:16 +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: 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-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-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: 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= ); > + } > + } > +} > +