mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 06:28:39 +00:00
Merge pull request #376 from DancinGhost/perf/rmapi-control-single-resource-scan
rmapi: scan the resource slab once per control lookup
This commit is contained in:
@ -326,6 +326,17 @@ fn find_method(class: u32, cmd: u32) -> Option<&'static ControlMethod> {
|
||||
.map(|index| &METHODS[index])
|
||||
}
|
||||
|
||||
/// The client's root object (`handle == client`), which has parent 0.
|
||||
const fn root_object(client: u32) -> ObjectRef {
|
||||
ObjectRef {
|
||||
client,
|
||||
handle: client,
|
||||
parent: 0,
|
||||
class: ROOT_CLASS,
|
||||
internal_class: CLIENT_CLASS_ID,
|
||||
}
|
||||
}
|
||||
|
||||
/// The object's class first, then its ancestors, as `resControlLookup` walks them.
|
||||
fn object_method(class: u32, cmd: u32) -> Option<&'static ControlMethod> {
|
||||
if let Some(method) = find_method(class, cmd) {
|
||||
@ -397,13 +408,7 @@ impl<const C: usize, const R: usize, A: RmAlloc> Server<C, R, A> {
|
||||
pub fn object(&self, client: u32, handle: u32) -> Option<ObjectRef> {
|
||||
self.client_slot(client)?;
|
||||
if handle == client {
|
||||
return Some(ObjectRef {
|
||||
client,
|
||||
handle,
|
||||
parent: 0,
|
||||
class: ROOT_CLASS,
|
||||
internal_class: CLIENT_CLASS_ID,
|
||||
});
|
||||
return Some(root_object(client));
|
||||
}
|
||||
self.resource_slot(client, handle)
|
||||
.and_then(|slot| self.resources[slot])
|
||||
@ -627,10 +632,28 @@ impl<const C: usize, const R: usize, A: RmAlloc> Server<C, R, A> {
|
||||
entry.is_some_and(|resource| resource.client == client && resource.handle == handle)
|
||||
})
|
||||
}
|
||||
fn invalidated(&self, client: u32, handle: u32) -> bool {
|
||||
self.resource_slot(client, handle)
|
||||
.and_then(|slot| self.resources[slot])
|
||||
.is_some_and(|resource| resource.invalidated)
|
||||
/// The control target: [`Self::object`] plus the resource's `invalidated` flag, read
|
||||
/// from one resource-slab scan (the control path used to scan twice for the same
|
||||
/// `(client, handle)`). Fails with `OBJECT_NOT_FOUND`, then `RESOURCE_LOST`.
|
||||
fn control_target(&self, client: u32, handle: u32) -> Result<ObjectRef, NvStatus> {
|
||||
if self.client_slot(client).is_none() {
|
||||
return Err(status::OBJECT_NOT_FOUND);
|
||||
}
|
||||
let resource = self
|
||||
.resource_slot(client, handle)
|
||||
.and_then(|slot| self.resources[slot]);
|
||||
let object = if handle == client {
|
||||
root_object(client)
|
||||
} else {
|
||||
match resource {
|
||||
Some(resource) => resource.object(),
|
||||
None => return Err(status::OBJECT_NOT_FOUND),
|
||||
}
|
||||
};
|
||||
if resource.is_some_and(|resource| resource.invalidated) {
|
||||
return Err(status::RESOURCE_LOST);
|
||||
}
|
||||
Ok(object)
|
||||
}
|
||||
fn object_exists(&self, client: u32, handle: u32) -> bool {
|
||||
handle == client || self.resource_slot(client, handle).is_some()
|
||||
@ -1315,12 +1338,10 @@ impl<const C: usize, const R: usize, A: RmAlloc> Server<C, R, A> {
|
||||
negotiated_version,
|
||||
);
|
||||
}
|
||||
let Some(object) = self.object(request.client, request.object) else {
|
||||
return status::OBJECT_NOT_FOUND;
|
||||
let object = match self.control_target(request.client, request.object) {
|
||||
Ok(object) => object,
|
||||
Err(status) => return status,
|
||||
};
|
||||
if self.invalidated(request.client, request.object) {
|
||||
return status::RESOURCE_LOST;
|
||||
}
|
||||
let synth;
|
||||
let method = if let Some(method) = object_method(object.internal_class, request.cmd) {
|
||||
method
|
||||
|
||||
@ -330,6 +330,124 @@ fn control_table_lookup() {
|
||||
assert!(is_null_command(0x20800000) && !is_null_command(0x101));
|
||||
}
|
||||
|
||||
/// Constructs every class, makes `dependant` depend on `owner` (so freeing `owner`
|
||||
/// invalidates `dependant` without freeing it) and records which objects reach `control`.
|
||||
struct Dependant {
|
||||
dependant: u32,
|
||||
owner: u32,
|
||||
controlled: Vec<u32>,
|
||||
}
|
||||
impl Objects for Dependant {
|
||||
fn construct(
|
||||
&mut self,
|
||||
_: &ObjectRef,
|
||||
_: &'static ResourceDescriptor,
|
||||
_: &mut [u8],
|
||||
_: &Scope,
|
||||
) -> NvStatus {
|
||||
status::OK
|
||||
}
|
||||
fn destruct(&mut self, _: &ObjectRef) {}
|
||||
fn control(&mut self, object: &ObjectRef, _: &ControlMethod, _: &mut [u8]) -> NvStatus {
|
||||
self.controlled.push(object.handle);
|
||||
status::OK
|
||||
}
|
||||
fn depends_on(&self, candidate: &ObjectRef, object: &ObjectRef) -> bool {
|
||||
candidate.handle == self.dependant && object.handle == self.owner
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn control_target_reports_missing_then_lost_before_dispatch() {
|
||||
const DEVICE: u32 = 0x100;
|
||||
const SUBDEVICE: u32 = 0x101;
|
||||
const SPACE: u32 = 0x110;
|
||||
// A command the subdevice class exports, with 564-byte parameters.
|
||||
const CMD: u32 = 0x2080_0102;
|
||||
let mut server = std::boxed::Box::new(Server::<4, 64>::new());
|
||||
let mut objects = Dependant {
|
||||
dependant: SUBDEVICE,
|
||||
owner: SPACE,
|
||||
controlled: Vec::new(),
|
||||
};
|
||||
let mut request = Alloc {
|
||||
root: 0,
|
||||
parent: 0,
|
||||
new: 0,
|
||||
class: 0x41,
|
||||
flags: 0,
|
||||
};
|
||||
assert_eq!(
|
||||
server.alloc(&CALLER, &mut request, false, &mut [], &mut objects),
|
||||
status::OK
|
||||
);
|
||||
let root = request.new;
|
||||
for (parent, handle, class) in [
|
||||
(root, DEVICE, 0x80),
|
||||
(DEVICE, SUBDEVICE, 0x2080),
|
||||
(DEVICE, SPACE, 0x90f1),
|
||||
] {
|
||||
let size = resources::parameter_size(class, true).unwrap() as usize;
|
||||
let mut params = std::vec![0u8; size];
|
||||
let mut alloc = Alloc {
|
||||
root,
|
||||
parent,
|
||||
new: handle,
|
||||
class,
|
||||
flags: 0,
|
||||
};
|
||||
assert_eq!(
|
||||
server.alloc(&CALLER, &mut alloc, true, &mut params, &mut objects),
|
||||
status::OK,
|
||||
"{handle:#x}"
|
||||
);
|
||||
}
|
||||
let control = |server: &mut Server<4, 64>, objects: &mut Dependant, object: u32| {
|
||||
let request = Control {
|
||||
client: root,
|
||||
object,
|
||||
cmd: CMD,
|
||||
flags: 0,
|
||||
params_size: 564,
|
||||
pointer_nonnull: true,
|
||||
};
|
||||
server.control(
|
||||
&CALLER,
|
||||
&request,
|
||||
&mut [0; 564],
|
||||
&EMPTY,
|
||||
objects,
|
||||
&mut UserWrites::default(),
|
||||
)
|
||||
};
|
||||
// A live object reaches the class; an unknown handle does not.
|
||||
assert_eq!(control(&mut server, &mut objects, SUBDEVICE), status::OK);
|
||||
assert_eq!(
|
||||
control(&mut server, &mut objects, 0xcafe_0001),
|
||||
status::OBJECT_NOT_FOUND
|
||||
);
|
||||
// The client's root passes the lookup and fails only at the method lookup.
|
||||
assert_eq!(
|
||||
control(&mut server, &mut objects, root),
|
||||
status::NOT_SUPPORTED
|
||||
);
|
||||
// Freeing the space invalidates its dependant, which keeps its handle but is lost.
|
||||
assert_eq!(
|
||||
server.free(&CALLER, root, SPACE, &mut objects).status,
|
||||
status::OK
|
||||
);
|
||||
assert!(server.object(root, SUBDEVICE).is_some());
|
||||
assert_eq!(
|
||||
control(&mut server, &mut objects, SUBDEVICE),
|
||||
status::RESOURCE_LOST
|
||||
);
|
||||
assert_eq!(
|
||||
control(&mut server, &mut objects, SPACE),
|
||||
status::OBJECT_NOT_FOUND
|
||||
);
|
||||
assert_eq!(objects.controlled, [SUBDEVICE]);
|
||||
}
|
||||
|
||||
fn client_for<const C: usize, const R: usize>(
|
||||
server: &mut Server<C, R>,
|
||||
objects: &mut AnyClass,
|
||||
|
||||
Reference in New Issue
Block a user