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
2 changes: 1 addition & 1 deletion drivers/gpu/drm/asahi/mmu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1254,7 +1254,7 @@ impl VmInner {
let _mutation = self.mapping_mutation();
// Allocate detached RAM tables before excluding the firmware. The shadow L2 is not
// the reserved shared-L2; only final leaf stores below already linked L3s are live.
self.page_table.prepare_map(node.start()..end)?;
self.page_table.prepare_driver_map(node.start()..end)?;
let uat_inner = self.uat_inner.clone();
let handoff = uat_inner.lock_m4_upper(self.is_kernel)?;
// Refuse a range holding another mapping's leaves before writing anything: the
Expand Down
107 changes: 86 additions & 21 deletions drivers/gpu/drm/asahi/pgtable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,17 @@ impl Default for Prot {
}
}

/// What a walk does about a table that its range needs and that does not exist.
#[derive(Clone, Copy, PartialEq, Eq)]
enum Missing {
/// Skips the part of the range below it.
Skip,
/// Allocates it if the limits of the client it is charged to admit it.
Admit,
/// Allocates it and charges it to the client whatever the limits.
Commit,
}

pub(crate) struct UatPageTable {
ttb: PhysicalAddr,
ttb_owned: bool,
Expand Down Expand Up @@ -574,7 +585,8 @@ impl UatPageTable {
/// Charges this page table's memory to a client from now on: the root and the tables
/// allocated so far at once, whatever the client's limits, and each further table once it is
/// allocated and ready to install. A table past the client's or the device's limit is freed
/// again and the map that needed it fails with `ENOMEM`; a table is charged only after every
/// again and the map that needed it fails with `ENOMEM`, except for a mapping that the driver
/// makes ([`Self::prepare_driver_map`]); a table is charged only after every
/// step that can fail, so one that is not installed leaves no charge. The tables are also
/// allocated as memory of the calling task's memory cgroup from then on. Everything stays
/// charged until the page table is dropped, which frees the tables.
Expand All @@ -595,11 +607,17 @@ impl UatPageTable {
}

/// Walks the leaves of `iova_range` without changing them.
fn with_pages<F>(&mut self, iova_range: Range<u64>, alloc: bool, free: bool, cb: F) -> Result
fn with_pages<F>(
&mut self,
iova_range: Range<u64>,
missing: Missing,
free: bool,
cb: F,
) -> Result
where
F: FnMut(u64, &[Pte]) -> Result,
{
self.walk(iova_range, alloc, free, false, cb)
self.walk(iova_range, missing, free, false, cb)
}

/// Walks the leaves of `iova_range`, which `cb` may change (`mutate`).
Expand All @@ -609,16 +627,16 @@ impl UatPageTable {
fn walk<F>(
&mut self,
iova_range: Range<u64>,
alloc: bool,
missing: Missing,
free: bool,
mutate: bool,
cb: F,
) -> Result
where
F: FnMut(u64, &[Pte]) -> Result,
{
let result = self.walk_inner(iova_range, alloc, free, mutate, cb);
if self.cleans_poc() && (alloc || free || mutate) {
let result = self.walk_inner(iova_range, missing, free, mutate, cb);
if self.cleans_poc() && (missing != Missing::Skip || free || mutate) {
poc_barrier();
}
result
Expand All @@ -627,7 +645,7 @@ impl UatPageTable {
fn walk_inner<F>(
&mut self,
iova_range: Range<u64>,
alloc: bool,
missing: Missing,
free: bool,
mutate: bool,
mut cb: F,
Expand All @@ -636,6 +654,7 @@ impl UatPageTable {
F: FnMut(u64, &[Pte]) -> Result,
{
let clean = self.cleans_poc();
let alloc = missing != Missing::Skip;
mod_pr_debug!(
"UATPageTable::with_pages: {:#x?} alloc={} free={}\n",
iova_range,
Expand Down Expand Up @@ -741,7 +760,11 @@ impl UatPageTable {
// Charge last: nothing below can fail, so a table that is
// not installed is freed with no charge left behind.
if let Some(charge) = charge.as_mut() {
charge.try_add(UAT_PGSZ as u64)?;
if missing == Missing::Commit {
charge.add(UAT_PGSZ as u64);
} else {
charge.try_add(UAT_PGSZ as u64)?;
}
}
if let Some(tables) = driver_tables.as_mut() {
// Cannot fail: the room was reserved above.
Expand Down Expand Up @@ -951,7 +974,7 @@ impl UatPageTable {
let expected = (range.end - range.start) >> UAT_PGBIT;
let mut visited = 0u64;
let mut permitted = true;
self.with_pages(range, false, false, |_, ptes| {
self.with_pages(range, Missing::Skip, false, |_, ptes| {
visited = visited.checked_add(ptes.len() as u64).ok_or(EOVERFLOW)?;
for pte in ptes {
let value = pte.load(Ordering::Acquire);
Expand Down Expand Up @@ -991,7 +1014,7 @@ impl UatPageTable {
let first = iova_range.start & self.ias_mask;
let oas_mask = self.oas_mask;
out.fill(u64::MAX);
self.with_pages(iova_range, false, false, |iova, ptes| {
self.with_pages(iova_range, Missing::Skip, false, |iova, ptes| {
let index = ((iova - first) >> UAT_PGBIT) as usize;
let slots = out.get_mut(index..index + ptes.len()).ok_or(EINVAL)?;
for (slot, pte) in slots.iter_mut().zip(ptes) {
Expand All @@ -1017,7 +1040,7 @@ impl UatPageTable {
}
let end = iova.checked_add(UAT_PGSZ as u64).ok_or(EINVAL)?;
let mut value = 0;
self.with_pages(iova..end, false, false, |_, ptes| {
self.with_pages(iova..end, Missing::Skip, false, |_, ptes| {
if let Some(pte) = ptes.first() {
value = pte.load(Ordering::Relaxed);
}
Expand All @@ -1031,23 +1054,36 @@ impl UatPageTable {

pub(crate) fn alloc_pages(&mut self, iova_range: Range<u64>) -> Result {
mod_pr_debug!("UATPageTable::alloc_pages: {:#x?}\n", iova_range);
self.with_pages(iova_range, true, false, |_, _| Ok(()))
self.with_pages(iova_range, Missing::Admit, false, |_, _| Ok(()))
}

/// Reserve all tables for a fixed mapping before publishing any leaves.
/// GPUVM and the fixed-address allocator may share a page table, so check
/// that neither owns a destination leaf before allowing failure rollback.
pub(crate) fn prepare_map(&mut self, range: Range<u64>) -> Result {
self.prepare_tables(range, Missing::Admit)
}

/// [`Self::prepare_map`] for a mapping that the driver makes, such as its view of a queue's
/// rings in the client's address space. Its tables are charged to the client like all others,
/// but the limits do not refuse them: the driver also makes these mappings to publish a command
/// that SUBMIT has accepted. Then no ioctl is left to return a refusal, and userspace takes the
/// command's failed fence for a lost device.
pub(crate) fn prepare_driver_map(&mut self, range: Range<u64>) -> Result {
self.prepare_tables(range, Missing::Commit)
}

fn prepare_tables(&mut self, range: Range<u64>, missing: Missing) -> Result {
if range.is_empty() || (range.start | range.end) & UAT_PGMSK as u64 != 0 {
return Err(EINVAL);
}
self.with_pages(range.clone(), false, false, |_, ptes| {
self.with_pages(range.clone(), Missing::Skip, false, |_, ptes| {
if ptes.iter().any(|pte| pte.load(Ordering::Acquire) != 0) {
return Err(EBUSY);
}
Ok(())
})?;
self.alloc_pages(range)
self.with_pages(range, missing, false, |_, _| Ok(()))
}

fn pte_bits(&self) -> u64 {
Expand Down Expand Up @@ -1090,7 +1126,7 @@ impl UatPageTable {
let pte_bits = self.pte_bits();
let prot = self.stored(prot);

self.walk(iova_range, true, false, true, |iova, ptes| {
self.walk(iova_range, Missing::Admit, false, true, |iova, ptes| {
for (idx, pte) in ptes.iter().enumerate() {
let ptev = pte.load(Ordering::Relaxed);
if ptev != 0 {
Expand Down Expand Up @@ -1139,7 +1175,7 @@ impl UatPageTable {
let prot_bits = self.stored(prot).as_pte() | self.pte_bits();
let mut cursor = SegmentCursor::new(segments, offset, one_page)?;

self.walk(iova_range, true, false, true, |iova, ptes| {
self.walk(iova_range, Missing::Admit, false, true, |iova, ptes| {
for (idx, pte) in ptes.iter().enumerate() {
let phys = cursor.next_page(iova + (idx * UAT_PGSZ) as u64)?;
let ptev = pte.load(Ordering::Relaxed);
Expand Down Expand Up @@ -1167,7 +1203,7 @@ impl UatPageTable {
/// store: its failure path clears the whole range it asked for, which would otherwise
/// also clear the other mapping's leaves.
pub(crate) fn ensure_unmapped(&mut self, iova_range: Range<u64>) -> Result {
self.walk(iova_range, false, false, false, |iova, ptes| {
self.walk(iova_range, Missing::Skip, false, false, |iova, ptes| {
match ptes.iter().position(|pte| pte.load(Ordering::Relaxed) != 0) {
Some(idx) => {
pr_err!(
Expand All @@ -1189,7 +1225,7 @@ impl UatPageTable {
prot
);
let prot = self.stored(prot);
self.walk(iova_range, true, false, true, |iova, ptes| {
self.walk(iova_range, Missing::Admit, false, true, |iova, ptes| {
for (idx, pte) in ptes.iter().enumerate() {
let ptev = pte.load(Ordering::Relaxed);
if ptev & PTE_TYPE_BITS != PTE_TYPE_LEAF_TABLE {
Expand All @@ -1208,7 +1244,7 @@ impl UatPageTable {

pub(crate) fn unmap_pages(&mut self, iova_range: Range<u64>) -> Result {
mod_pr_debug!("UATPageTable::unmap_pages: {:#x?}\n", iova_range);
self.walk(iova_range, false, false, true, |iova, ptes| {
self.walk(iova_range, Missing::Skip, false, true, |iova, ptes| {
for (idx, pte) in ptes.iter().enumerate() {
if pte.load(Ordering::Relaxed) & PTE_TYPE_LEAF_TABLE == 0 {
pr_err!(
Expand All @@ -1225,7 +1261,7 @@ impl UatPageTable {
/// Discards an aligned map operation that failed before acquiring a GPUVA owner.
/// Missing tables and leaves are expected; existing table storage stays allocated.
pub(crate) fn discard_partial_map(&mut self, iova_range: Range<u64>) -> Result {
self.walk(iova_range, false, false, true, |_, ptes| {
self.walk(iova_range, Missing::Skip, false, true, |_, ptes| {
for pte in ptes {
pte.store(0, Ordering::Relaxed);
}
Expand All @@ -1241,7 +1277,7 @@ impl Drop for UatPageTable {
}
mod_pr_debug!("UATPageTable::drop range: {:#x?}\n", &self.va_range);
if self
.with_pages(self.va_range.clone(), false, true, |iova, ptes| {
.with_pages(self.va_range.clone(), Missing::Skip, true, |iova, ptes| {
for (idx, pte) in ptes.iter().enumerate() {
if pte.load(Ordering::Relaxed) != 0 {
pr_err!(
Expand Down Expand Up @@ -1462,6 +1498,35 @@ mod pgtable_charge_tests {
Ok(())
}

static DRIVER_MAP: Ledger = Ledger::new();

#[test]
fn the_tables_of_a_driver_map_are_charged_but_never_refused() -> Result {
// Room for the root alone, against the client's and the device's limit.
DRIVER_MAP.set_limits(Limits {
device: PG,
client: PG,
});
let client = Client::new()?;
let mut pt = UatPageTable::new(42, 42)?;
pt.charge_to(Usage::new(&DRIVER_MAP, client.clone()));
let range = 64 * LEAF_SPAN..66 * LEAF_SPAN;
assert_eq!(pt.prepare_map(range.clone()), Err(ENOMEM));
assert_eq!((client.committed(), pt.tables), (PG, 0));
assert_eq!(DRIVER_MAP.refused.load(Ordering::Relaxed), 1);
// One level-1 table and two leaf tables, past both limits.
pt.prepare_driver_map(range.clone())?;
assert_eq!((client.committed(), client.resident()), (4 * PG, 4 * PG));
assert_eq!(pt.tables, 3);
assert_eq!(DRIVER_MAP.refused.load(Ordering::Relaxed), 1);
// They count against the next bind.
assert_eq!(pt.prepare_map(66 * LEAF_SPAN..67 * LEAF_SPAN), Err(ENOMEM));
drop(pt);
assert_eq!(client.committed(), 0);
assert_eq!(DRIVER_MAP.user_committed.load(Ordering::Relaxed), 0);
Ok(())
}

static REFUSE_SHARED: Ledger = Ledger::new();

#[test]
Expand Down
Loading
Loading