From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 2871547F2FA for ; Tue, 7 Jul 2026 21:51:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783461111; cv=none; b=LjcFVDlc3P3nSwcT3rB087FKGXtTkGkRhEfpgNdwuhe+T9vvyg2br1Q1sUP6obscx8yhCRwMQNE7teeqdHR5zh3VXBoTbuCPf87kHX9meza0qUUWtKFPsZEM4XdRBuyMSQYgVeDwOSqW14GCI/my3UvvWf8PRlcPM8NwU4TsORk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783461111; c=relaxed/simple; bh=jl+YAP8tpQr4+e/VlJSTpnAqWxdzEyvzpYLsNlFWKbg=; h=Message-ID:Subject:From:To:Cc:Date:In-Reply-To:References: Content-Type:MIME-Version; b=qaB25Zg5w9k6JrVnyeIR9RLXo+Ov03n1VcpGG61m6ggUML4UiAzj5+U+zYY8Gsqbqg+xAm2dVUGtam9UN7eGYRb1GPp57/IZGftpkMJdI/PIEDBzMV6OSmEunLtv3AksnLdpqqlwxAP6wTgQhW7aVgLl3v9sz/FuJg2HbDdIyBg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=EWDpNcOG; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b=rf/fqmiC; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="EWDpNcOG"; dkim=pass (2048-bit key) header.d=redhat.com header.i=@redhat.com header.b="rf/fqmiC" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1783461108; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=jl+YAP8tpQr4+e/VlJSTpnAqWxdzEyvzpYLsNlFWKbg=; b=EWDpNcOGwd8Spa1jzI3HMv4AUygR5kQzsZAzCJ0qyXkm1zJq+vtn5PqgTRugA1Bo/J87xH eajf6B/fV06yWjbeIOO1VVIvpfi4YcIGe8Qy1A9Mo1MwRH5bUIyBu9zRt8iedWm8mgpqQc hsdmozJRd5HPXDFbpoJ9q0FWOwn1e+c= Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-516-idZ_FWDnNaOADS4panv1Pg-1; Tue, 07 Jul 2026 17:51:47 -0400 X-MC-Unique: idZ_FWDnNaOADS4panv1Pg-1 X-Mimecast-MFC-AGG-ID: idZ_FWDnNaOADS4panv1Pg_1783461107 Received: by mail-qv1-f70.google.com with SMTP id 6a1803df08f44-8fdace20927so747206d6.2 for ; Tue, 07 Jul 2026 14:51:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=google; t=1783461107; x=1784065907; darn=vger.kernel.org; h=mime-version:user-agent:content-transfer-encoding:content-type :references:in-reply-to:date:cc:to:from:subject:message-id:from:to :cc:subject:date:message-id:reply-to:content-type; bh=jl+YAP8tpQr4+e/VlJSTpnAqWxdzEyvzpYLsNlFWKbg=; b=rf/fqmiCo49rwtqyBAIVFGB9ZiM1Mvi0eDJHd80HyrW9leO0o9hiiholX2puqxB0W7 wQ4kukT6+TZse0JwMHRdmXpKtYmA2JjO+2lJwpicALsZGj2R17dlTp+u11XdT/uAXafm SVGDYGafyLaSsUu0AFI3B0vzWXgsYSwyLBHR7yJJygLr35IeTCHDtvYHh0aU9UUKA+Ls gwScVEdJNY6tuNH+S2F+EINvnTEJmic6X8QI28QZQvMAYOlKJlmOSBHWGuQMjzZNHtaF lNIlT82ztfzVVysLyo6KXKMnGZQYRXgJ7Cw8CdyogFb/nOiLyJdTu+mWF+CX4ArdFWos eq9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783461107; x=1784065907; h=mime-version:user-agent:content-transfer-encoding:content-type :references:in-reply-to:date:cc:to:from:subject:message-id:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=jl+YAP8tpQr4+e/VlJSTpnAqWxdzEyvzpYLsNlFWKbg=; b=Xryn2QbOJ+s11HiKOrsOMOUhByNmjSYfaAt4wnUC22Cqydu9TytTnhCe0y+aNn32lk 9rEm1b4zSznSIUf+qYIv6quUIXz5K2brmzx5+INT0jq5BP3j7SiYK10bLVYKlUA5HYCL E9l8tHo0rluOgjq+EPvY/+D2Fk0mv/qwa9KTIix3N3vu/eZ5wQAqNfjAVRgVor5yVVYE orC36qGvMwlmZZjmb03qwIpmflkJv12icW+n3O154YewrJ0IVLDFsKHQK9gEylb7e7Rr jrW/xYyCpflZKeB8D2yQnXwaFfVm/20xCn664pfG36mN5vlkPzarOC09T7Q1qW/XLD17 5ejg== X-Forwarded-Encrypted: i=1; AHgh+RpBDLVdbRS/xrnsbcEym1r70R8B0M479xjuXqBUc0hMtaTR0FJPoCOY38OgQIekBmXCfqlcFTeE/fM7y7s=@vger.kernel.org X-Gm-Message-State: AOJu0YxdoXsUZj3irruYKpAxInAI+s2bEU9RRVqZu+W2bqHmhpXzb3RP GU7tfuduDWE576qQvZH/ASO2jU1N50Xy4YJ6kkntQpDY2RWWKhOv3Rbiy6gtQ2d/rqPRgvezjmb 7MmwKPQHUhIIUpOTg1FY/8N+6FxpR4FX+DHKM24RqdFz/HTyMLrfhaF1v9DMuLocCLg== X-Gm-Gg: AfdE7cleufmetBck7EG43E1GGTPcM/1L2v7v+rK4x4MZ7PDMffIKXmiBTxusIpqOruG yZN444AQa04SLbVxHVwIIdjgH9sE5BeE/S3zzMhbQOo4vjIVQL4UwM0H3Tuwr4oa0kJwAQW4vrv 3b6dsEUtarqwFf6QypYYXEHRu1WH0Hv757A4dQJSQeeJd5l+DyB/tWRY5mQ1xqBURJiHAtyjbEG l0FdwRXbXzbfuD4zs9unSCLWSiepMZPPnBay4xyjLiqGsu2tQLoZDhJu7GdV1Kb0tQMihJESIe7 gTptxBlpMR/FjktllxVjWfZnrKjJ+Znu5CJ2u4Oe0DpZbqIJzlaPaepqwBCOcbRP+FdVtY8liJD MPXtc6MY= X-Received: by 2002:a05:6214:21c3:b0:8f3:6c2:222 with SMTP id 6a1803df08f44-8fcb2e7e9f6mr97091186d6.14.1783461107112; Tue, 07 Jul 2026 14:51:47 -0700 (PDT) X-Received: by 2002:a05:6214:21c3:b0:8f3:6c2:222 with SMTP id 6a1803df08f44-8fcb2e7e9f6mr97090676d6.14.1783461106543; Tue, 07 Jul 2026 14:51:46 -0700 (PDT) Received: from [192.168.8.4] ([100.0.180.93]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-8f4724b9ff8sm167215646d6.40.2026.07.07.14.51.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 07 Jul 2026 14:51:45 -0700 (PDT) Message-ID: <742ce0da3608a8cf467db9b7a6f0ada8b7d61a93.camel@redhat.com> Subject: Re: [RFC PATCH v2 06/18] rust: drm: kms: add a Framebuffer::vmap() guard From: lyude@redhat.com To: Mike Lothian , rust-for-linux@vger.kernel.org Cc: dri-devel@lists.freedesktop.org, David Airlie , Simona Vetter , Danilo Krummrich , Miguel Ojeda , Boqun Feng , Gary Guo , =?ISO-8859-1?Q?Bj=F6rn?= Roy Baron , Benno Lossin , Andreas Hindborg , Alice Ryhl , Trevor Gross , linux-kernel@vger.kernel.org Date: Tue, 07 Jul 2026 17:51:45 -0400 In-Reply-To: <20260703030123.2814-7-mike@fireburn.co.uk> References: <20260617150232.2210-1-mike@fireburn.co.uk> <20260703030123.2814-1-mike@fireburn.co.uk> <20260703030123.2814-7-mike@fireburn.co.uk> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.58.3 (3.58.3-1.fc43) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 We have VMap at home already, please (with your eyes) search the rest of the kernel for it. The work for this was pushed to drm-rust-next recently. On Fri, 2026-07-03 at 04:00 +0100, Mike Lothian wrote: > Framebuffer::vmap() maps a framebuffer's plane-0 backing pages into > the > kernel address space via drm_gem_fb_vmap()/vunmap(), returning an > RAII > FramebufferVmap guard that unmaps on drop. Only single-CPU-visible- > plane > framebuffers are supported (packed formats backed by GEM-shmem/CMA; > multi-plane YUV or an unmapped imported dma-buf return EINVAL). This > is > the driver-side FbVmap guard the v1 series carried, moved into the > layer > proper so every KMS driver gets it instead of hand-rolling the same > map/use/unmap dance. >=20 > Signed-off-by: Mike Lothian > Assisted-by: Claude:claude-sonnet-5 [Claude-Code] > --- > =C2=A0rust/kernel/drm/kms/framebuffer.rs | 67 > +++++++++++++++++++++++++++++- > =C2=A01 file changed, 66 insertions(+), 1 deletion(-) >=20 > diff --git a/rust/kernel/drm/kms/framebuffer.rs > b/rust/kernel/drm/kms/framebuffer.rs > index 54d0391388a9..1ec6779ba7de 100644 > --- a/rust/kernel/drm/kms/framebuffer.rs > +++ b/rust/kernel/drm/kms/framebuffer.rs > @@ -5,7 +5,12 @@ > =C2=A0//! C header: > [`include/drm/drm_framebuffer.h`](srctree/include/drm/drm_framebuffer > .h) > =C2=A0 > =C2=A0use super::{KmsDriver, ModeObject, Sealed}; > -use crate::{drm::device::Device, types::*}; > +use crate::{ > +=C2=A0=C2=A0=C2=A0 drm::device::Device, > +=C2=A0=C2=A0=C2=A0 error::{code::EINVAL, to_result}, > +=C2=A0=C2=A0=C2=A0 prelude::*, > +=C2=A0=C2=A0=C2=A0 types::*, > +}; > =C2=A0use bindings; > =C2=A0use core::{marker::*, ptr}; > =C2=A0 > @@ -67,4 +72,64 @@ pub(super) unsafe fn from_raw<'a>(ptr: *const > bindings::drm_framebuffer) -> &'a > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 // SAFETY: Our data layo= ut is identical to drm_framebuffer > =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 unsafe { &*ptr.cast() } > =C2=A0=C2=A0=C2=A0=C2=A0 } > + > +=C2=A0=C2=A0=C2=A0 /// Return the raw `bindings::drm_framebuffer` for th= is > framebuffer. > +=C2=A0=C2=A0=C2=A0 #[inline] > +=C2=A0=C2=A0=C2=A0 pub(crate) fn as_raw(&self) -> *mut bindings::drm_fra= mebuffer { > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 self.0.get() > +=C2=A0=C2=A0=C2=A0 } > + > +=C2=A0=C2=A0=C2=A0 /// Map this framebuffer's plane-0 backing pages into= the kernel > address space for CPU > +=C2=A0=C2=A0=C2=A0 /// access, for the duration of the returned guard. > +=C2=A0=C2=A0=C2=A0 /// > +=C2=A0=C2=A0=C2=A0 /// Only framebuffers with a single, CPU-visible plan= e are > supported (i.e. packed formats > +=C2=A0=C2=A0=C2=A0 /// backed by GEM-shmem/CMA memory, not multi-plane Y= UV or an > IMPORTED dma-buf without a > +=C2=A0=C2=A0=C2=A0 /// CPU mapping); other cases return `EINVAL`. > +=C2=A0=C2=A0=C2=A0 pub fn vmap(&self) -> Result> = { > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 // SAFETY: `iosys_map` is POD= (a pointer union plus a bool); > all-zero is a valid > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 // "not mapped" value that `d= rm_gem_fb_vmap` overwrites for > present planes. > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 let mut map: [bindings::iosys= _map; 4] =3D unsafe { > core::mem::zeroed() }; > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 let mut data_map: [bindings::= iosys_map; 4] =3D unsafe { > core::mem::zeroed() }; > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 // SAFETY: `self.as_raw()` is= a valid, GEM-backed > framebuffer for the lifetime of `self`. > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 to_result(unsafe { > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 bindi= ngs::drm_gem_fb_vmap(self.as_raw(), > map.as_mut_ptr(), data_map.as_mut_ptr()) > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 })?; > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 // SAFETY: `map[0]` was just = filled in by `drm_gem_fb_vmap` > above. > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 let vaddr =3D unsafe { map[0]= .__bindgen_anon_1.vaddr }; > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 if vaddr.is_null() { > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 // SA= FETY: balances the vmap just done, with the same > `map`. > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 unsaf= e { bindings::drm_gem_fb_vunmap(self.as_raw(), > map.as_mut_ptr()) }; > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 retur= n Err(EINVAL); > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 } > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 Ok(FramebufferVmap { fb: self= , map, _p: PhantomData }) > +=C2=A0=C2=A0=C2=A0 } > +} > + > +/// An RAII guard over a CPU mapping of a [`Framebuffer`]'s plane-0 > backing pages, created by > +/// [`Framebuffer::vmap`]. > +/// > +/// The mapping is torn down when this guard is dropped, so an early > return between mapping and > +/// use can never leak it. > +pub struct FramebufferVmap<'a, T: KmsDriver> { > +=C2=A0=C2=A0=C2=A0 fb: &'a Framebuffer, > +=C2=A0=C2=A0=C2=A0 map: [bindings::iosys_map; 4], > +=C2=A0=C2=A0=C2=A0 _p: PhantomData, > +} > + > +impl<'a, T: KmsDriver> FramebufferVmap<'a, T> { > +=C2=A0=C2=A0=C2=A0 /// Plane 0's CPU virtual base address (guaranteed no= n-null for > the guard's lifetime). > +=C2=A0=C2=A0=C2=A0 #[inline] > +=C2=A0=C2=A0=C2=A0 pub fn as_ptr(&self) -> *const u8 { > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 // SAFETY: set non-null in `F= ramebuffer::vmap`; `vaddr` is > the active union member. > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 (unsafe { self.map[0].__bindg= en_anon_1.vaddr }) as *const u8 > +=C2=A0=C2=A0=C2=A0 } > +} > + > +impl<'a, T: KmsDriver> Drop for FramebufferVmap<'a, T> { > +=C2=A0=C2=A0=C2=A0 fn drop(&mut self) { > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 // SAFETY: `self.fb.as_raw()`= /`self.map` are exactly the > pair passed to > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 // `drm_gem_fb_vmap` in `Fram= ebuffer::vmap`, and the mapping > has not been released > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 // since. > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 unsafe { bindings::drm_gem_fb= _vunmap(self.fb.as_raw(), > self.map.as_mut_ptr()) }; > +=C2=A0=C2=A0=C2=A0 } > =C2=A0}