Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions drivers/gpu/drm/asahi/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1773,21 +1773,15 @@ impl File {
return Err(EINVAL);
}

// REALTIME stays reserved. HIGH needs CAP_SYS_NICE or DRM master, as amdgpu and
// panthor require for raised queue priority; EACCES lets userspace fall back to MEDIUM.
// REALTIME stays reserved. HIGH needs CAP_SYS_NICE, DRM master on this device, or active
// KMS master on a display card (split KMS/render compositors open the render node); EACCES
// lets unprivileged userspace fall back to MEDIUM.
if data.priority == uapi::drm_asahi_priority_DRM_ASAHI_PRIORITY_REALTIME {
cls_pr_debug!(Errors, "queue_create: Invalid priority\n");
return Err(EINVAL);
}
// Keep support tied to a backend and admitted profile. G13/G14 and
// unqualified M3 profiles retain their previous rejection.
let high_supported = reg_data.gpu.as_any().is::<crate::g17::Gpu>();
#[cfg(CONFIG_DRM_ASAHI_M3_T8122)]
let high_supported = high_supported || reg_data.gpu.as_any()
.downcast_ref::<crate::m3::m3_drm::Backend>()
.is_some_and(|gpu| gpu.supports_high_priority());
if data.priority == uapi::drm_asahi_priority_DRM_ASAHI_PRIORITY_HIGH
&& !high_supported
&& !reg_data.gpu.supports_high_priority()
{
cls_pr_debug!(
Errors,
Expand All @@ -1799,10 +1793,11 @@ impl File {
// SAFETY: capable() reads the current ioctl task's credentials.
&& !unsafe { bindings::capable(bindings::CAP_SYS_NICE as i32) }
&& !file.is_current_master()
&& !drm::file::current_is_kms_master()
{
cls_pr_debug!(
Errors,
"queue_create: HIGH priority needs CAP_SYS_NICE or DRM master\n"
"queue_create: HIGH priority needs CAP_SYS_NICE, DRM master, or KMS master\n"
);
return Err(EACCES);
}
Expand Down
16 changes: 15 additions & 1 deletion drivers/gpu/drm/asahi/g14_v27_2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,20 @@ impl Runtime {
let state = Option::as_mut(&mut *guard).ok_or(ENODEV)?;
if !self.healthy() { return Err(EIO); }
if !state.accepting { return Err(ENODEV); }
state.pending.push((packet, 0), GFP_KERNEL)?;
let start = usize::from(
state.pending.first().is_some_and(|(_, idx)| *idx != 0)
|| state.waiting_bind.is_some(),
);
let at = start
+ state.pending[start..]
.iter()
.position(|(p, _)| p.urgency < packet.urgency)
.unwrap_or(state.pending.len() - start);
state.pending.reserve(1, GFP_KERNEL)?;
state
.pending
.insert_within_capacity(at, (packet, 0))
.map_err(|_| ENOMEM)?;
self.pump(state);
Ok(())
}
Expand Down Expand Up @@ -2451,6 +2464,7 @@ impl gpu::Gpu for Backend {
Ok(())
}
fn supports_vm_status(&self) -> bool { true }
fn supports_high_priority(&self) -> bool { true }
fn feature_flags(&self) -> u64 {
uapi::drm_asahi_feature_DRM_ASAHI_FEATURE_VM_STATUS as u64
| uapi::drm_asahi_feature_DRM_ASAHI_FEATURE_SCHEDULED_QUEUES as u64
Expand Down
4 changes: 4 additions & 0 deletions drivers/gpu/drm/asahi/g15/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,10 @@ impl Firmware {
}

impl gpu::Gpu for Gpu {
fn supports_high_priority(&self) -> bool {
true
}

fn max_cmdbuf_size(&self) -> usize {
command::MAX_CMDBUF
}
Expand Down
9 changes: 5 additions & 4 deletions drivers/gpu/drm/asahi/g15/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
//! execution context. The native ABI hardware-reservation wording remains a compatibility limit.
//! QUEUE_CREATE validates
//! `usc_exec_base` (4 GiB aligned, the window inside the user range) and refuses a failed VM or
//! GPU (EIO); HIGH/REALTIME are refused by the shared `file.rs`. Ray-query scratch layouts are
//! refused by the shared default `reserve_render_scs` (RENDER_RAY_QUERY is never advertised).
//! GPU (EIO); REALTIME is refused by `file.rs` and HIGH requires authorization. Ray-query scratch
//! layouts are refused by the shared default `reserve_render_scs` (RENDER_RAY_QUERY is never
//! advertised).
//!
//! QUEUE_DESTROY (dropping the queue) drains: the scheduler entity is torn down first, which
//! waits (killable) until every accepted job has been handed to the engine; the engine then runs
Expand Down Expand Up @@ -39,8 +40,8 @@ use crate::{file, mmu};

/// Per-queue state the engine reads.
pub(crate) struct QueueState {
/// Host priority: `DRM_ASAHI_PRIORITY_REALTIME - priority` as `file.rs` passes it (MEDIUM 2,
/// LOW 3; `file.rs` refuses HIGH and REALTIME). Smaller is served first.
/// Host priority: `DRM_ASAHI_PRIORITY_REALTIME - priority` as `file.rs` passes it (HIGH 1,
/// MEDIUM 2, LOW 3; `file.rs` refuses REALTIME). Smaller is served first.
pub(crate) priority: u32,
/// Completed stage time of the DRM file that created this queue.
pub(crate) usage: Arc<crate::usage::Client>,
Expand Down
4 changes: 4 additions & 0 deletions drivers/gpu/drm/asahi/g16/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,10 @@ impl Firmware {
}

impl gpu::Gpu for Gpu {
fn supports_high_priority(&self) -> bool {
true
}

fn syncobj_wait_hint(&self) -> Option<(u32, u32)> {
self.shared.syncobj_wait_hint()
}
Expand Down
9 changes: 5 additions & 4 deletions drivers/gpu/drm/asahi/g16/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
//! process (such as a compositor). It reserves no hardware graph or execution context.
//! QUEUE_CREATE validates
//! `usc_exec_base` (4 GiB aligned, the window inside the user range) and refuses a failed VM or
//! GPU (EIO); HIGH/REALTIME are refused by the shared `file.rs`. `render_scs_layout` (the
//! largest ray-query scratch layouts of the queue's render stages) is reserved at creation:
//! GPU (EIO); REALTIME is refused by `file.rs` and HIGH requires authorization.
//! `render_scs_layout` (the largest ray-query scratch layouts of the queue's render stages) is
//! reserved at creation:
//! EINVAL for a malformed layout, E2BIG beyond the largest SCS-capable UMA pool; a render
//! whose scratch exceeds the reservation is refused (EINVAL), as on the M4 Pro. The
//! reservation also sizes the SCS-capable render pools its VM creates for it
Expand Down Expand Up @@ -49,8 +50,8 @@ use crate::{file, mmu};

/// Per-queue state the engine reads.
pub(crate) struct QueueState {
/// Host priority: `DRM_ASAHI_PRIORITY_REALTIME - priority` as `file.rs` passes it (MEDIUM 2,
/// LOW 3; `file.rs` refuses HIGH and REALTIME). Smaller is served first.
/// Host priority: `DRM_ASAHI_PRIORITY_REALTIME - priority` as `file.rs` passes it (HIGH 1,
/// MEDIUM 2, LOW 3; `file.rs` refuses REALTIME). Smaller is served first.
pub(crate) priority: u32,
/// Completed stage time of the DRM file that created this queue (deduplicated per file).
pub(crate) usage: Arc<crate::usage::Client>,
Expand Down
4 changes: 4 additions & 0 deletions drivers/gpu/drm/asahi/g17/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3039,6 +3039,10 @@ impl Gpu {
}

impl gpu::Gpu for Gpu {
fn supports_high_priority(&self) -> bool {
true
}

fn max_cmdbuf_size(&self) -> usize {
command::MAX_STREAM
}
Expand Down
4 changes: 4 additions & 0 deletions drivers/gpu/drm/asahi/gpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,10 @@ pub(crate) trait Gpu: Send + Sync {
/// Release the backend's handle owner after closing user mapping admission.
/// Accepted jobs and firmware resource leases retain their own references.
fn vm_closed(&self, _vm: &mmu::Vm) {}
/// Whether this GPU backend schedules `DRM_ASAHI_PRIORITY_HIGH` queues.
fn supports_high_priority(&self) -> bool {
false
}
/// Create a new user command queue.
fn new_queue(
&self,
Expand Down
1 change: 1 addition & 0 deletions drivers/gpu/drm/asahi/m3/m3_drm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,7 @@ impl gpu::Gpu for Backend {
Ok(())
}
fn supports_vm_status(&self) -> bool { true }
fn supports_high_priority(&self) -> bool { Self::supports_high_priority(self) }
/// The features the 7.1.12 file layer reported for the M3 runtime: VM status (not its
/// mirror), scheduled queues, and the SoC's optional render and compute features.
fn feature_flags(&self) -> u64 {
Expand Down
8 changes: 7 additions & 1 deletion drivers/gpu/drm/asahi/m3/m3_submit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,13 @@ impl queue::Queue for Queue {
vm_job:KBox::pin_init(new_mutex!(Some(vm_job)),GFP_KERNEL)?},GFP_KERNEL)?;
let mut job=self.entity.new_job(1,Job{shared:self.shared.clone(),packet})?;
for sync in syncs.drain(0..in_sync_count) {if let Some(f)=sync.fence {job.add_dependency(f)?;}}
let mut job=job.arm();let finished=job.fences().finished();job.push();
let mut job=job.arm();
let finished={
let mut fences=job.fences();
fences.set_dont_pipeline();
fences.finished()
};
job.push();
for mut sync in syncs {if let Some(chain)=sync.chain_fence.take() {
sync.syncobj.add_point(chain,&finished,sync.timeline_value);
} else {sync.syncobj.replace_fence(Some(&finished));}}
Expand Down
50 changes: 50 additions & 0 deletions drivers/gpu/drm/drm_auth.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@
*/

#include <linux/export.h>
#include <linux/fdtable.h>
#include <linux/fs.h>
#include <linux/slab.h>

#include <drm/drm_auth.h>
#include <drm/drm_drv.h>
#include <drm/drm_file.h>
#include <drm/drm_ioctl.h>
#include <drm/drm_lease.h>
#include <drm/drm_print.h>

Expand Down Expand Up @@ -91,6 +94,53 @@ bool drm_is_current_master(struct drm_file *fpriv)
}
EXPORT_SYMBOL(drm_is_current_master);

static int drm_match_kms_master_fd(const void *data, struct file *filp,
unsigned int fd)
{
struct inode *inode;
struct drm_file *fpriv;
struct drm_device *dev;

if (!filp)
return 0;

inode = file_inode(filp);
if (!S_ISCHR(inode->i_mode) || imajor(inode) != DRM_MAJOR)
return 0;

if (!filp->f_op || filp->f_op->open != drm_open)
return 0;

fpriv = filp->private_data;
if (!fpriv || !drm_is_primary_client(fpriv))
return 0;

dev = fpriv->minor->dev;
if (drm_dev_is_unplugged(dev) ||
!drm_core_check_feature(dev, DRIVER_MODESET))
return 0;

return drm_is_current_master(fpriv) ? 1 : 0;
}

/**
* drm_current_is_kms_master - checks whether current holds master on a KMS card
*
* On split KMS/render platforms (where the display controller and render-only
* GPU are separate DRM devices), a Wayland compositor holds DRM master on the
* KMS primary node while opening the GPU render node. Checking whether the
* calling task currently owns an active KMS master fd lets render-only GPU
* drivers grant elevated queue priority to the active compositor without
* requiring CAP_SYS_NICE. Any live DRIVER_MODESET primary node counts, and
* priority is checked at queue creation so existing queues retain their
* priority across a VT switch or DROP_MASTER (matching amdgpu).
*/
bool drm_current_is_kms_master(void)
{
return iterate_fd(current->files, 0, drm_match_kms_master_fd, NULL) != 0;
}
EXPORT_SYMBOL(drm_current_is_kms_master);

int drm_getmagic(struct drm_device *dev, void *data, struct drm_file *file_priv)
{
struct drm_auth *auth = data;
Expand Down
1 change: 1 addition & 0 deletions include/drm/drm_auth.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ struct drm_master *drm_master_get(struct drm_master *master);
struct drm_master *drm_file_get_master(struct drm_file *file_priv);
void drm_master_put(struct drm_master **master);
bool drm_is_current_master(struct drm_file *fpriv);
bool drm_current_is_kms_master(void);

struct drm_master *drm_master_create(struct drm_device *dev);

Expand Down
5 changes: 3 additions & 2 deletions include/uapi/drm/asahi_drm.h
Original file line number Diff line number Diff line change
Expand Up @@ -1254,8 +1254,9 @@ enum drm_asahi_priority {
/**
* @DRM_ASAHI_PRIORITY_HIGH: High priority queue.
*
* Requires CAP_SYS_NICE or DRM master; otherwise queue creation fails
* with -EACCES.
* Requires CAP_SYS_NICE, DRM master on this device, or active DRM
* master on a KMS display card; otherwise queue creation fails with
* -EACCES.
*/
DRM_ASAHI_PRIORITY_HIGH = 2,

Expand Down
1 change: 1 addition & 0 deletions rust/bindings/bindings_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ const vm_flags_t RUST_CONST_HELPER_VM_NOHUGEPAGE = VM_NOHUGEPAGE;

const u32 RUST_CONST_HELPER_DRM_EXEC_INTERRUPTIBLE_WAIT = DRM_EXEC_INTERRUPTIBLE_WAIT;
const u32 RUST_CONST_HELPER_DRM_EXEC_IGNORE_DUPLICATES = DRM_EXEC_IGNORE_DUPLICATES;
const unsigned long RUST_CONST_HELPER_DRM_SCHED_FENCE_DONT_PIPELINE = DRM_SCHED_FENCE_DONT_PIPELINE;

#if IS_ENABLED(CONFIG_GPU_BUDDY)
const unsigned long RUST_CONST_HELPER_GPU_BUDDY_RANGE_ALLOCATION = GPU_BUDDY_RANGE_ALLOCATION;
Expand Down
6 changes: 6 additions & 0 deletions rust/kernel/drm/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,9 @@ impl<T: DriverFile> File<T> {
}

impl<T: DriverFile> super::private::Sealed for File<T> {}

/// Returns whether the calling task holds DRM master on an active KMS display device.
pub fn current_is_kms_master() -> bool {
// SAFETY: `drm_current_is_kms_master()` inspects `current->files` under its own lock.
unsafe { bindings::drm_current_is_kms_master() }
}
17 changes: 17 additions & 0 deletions rust/kernel/drm/sched.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,23 @@ impl<'a> JobFences<'a> {
// SAFETY: self.0.finished is always a valid fence
unsafe { Fence::get_raw(&mut self.0.finished) }
}

/// Marks the job's finished fence with `DRM_SCHED_FENCE_DONT_PIPELINE`.
///
/// Prevents cross-entity dependency pipelining on a shared scheduler so a
/// job on another entity waits until this job finishes rather than only
/// until it is handed to `run_job`. Same-entity dependencies still
/// pipeline.
pub fn set_dont_pipeline(&mut self) {
// SAFETY: `self.0` is an armed job's valid `drm_sched_fence` and
// `finished.flags` is atomically updated by `set_bit()`.
unsafe {
bindings::set_bit(
bindings::DRM_SCHED_FENCE_DONT_PIPELINE as usize,
addr_of_mut!(self.0.finished.flags),
);
}
}
}

struct EntityInner<T: JobImpl> {
Expand Down
Loading
Loading