forked from microsoft/Windows-rust-driver-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.rs
More file actions
730 lines (659 loc) · 23.9 KB
/
Copy pathqueue.rs
File metadata and controls
730 lines (659 loc) · 23.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
// Copyright (c) Microsoft Corporation.
// License: MIT OR Apache-2.0
use core::sync::atomic::Ordering;
use wdk::{nt_success, paged_code, println, wdf};
use wdk_sys::{
call_unsafe_wdf_function_binding,
ntddk::{ExAllocatePool2, ExFreePool},
_WDF_EXECUTION_LEVEL,
_WDF_IO_QUEUE_DISPATCH_TYPE,
_WDF_SYNCHRONIZATION_SCOPE,
_WDF_TRI_STATE,
NTSTATUS,
POOL_FLAG_NON_PAGED,
SIZE_T,
STATUS_BUFFER_OVERFLOW,
STATUS_CANCELLED,
STATUS_INSUFFICIENT_RESOURCES,
STATUS_INVALID_DEVICE_REQUEST,
STATUS_SUCCESS,
WDFDEVICE,
WDFMEMORY,
WDFOBJECT,
WDFQUEUE,
WDFREQUEST,
WDFTIMER,
WDF_IO_QUEUE_CONFIG,
WDF_NO_HANDLE,
WDF_OBJECT_ATTRIBUTES,
WDF_TIMER_CONFIG,
};
use crate::{
queue_get_context,
request_get_context,
wdf_object_context::wdf_get_context_type_info,
AtomicI32,
QueueContext,
RequestContext,
WDF_IO_QUEUE_CONFIG_SIZE,
WDF_OBJECT_ATTRIBUTES_SIZE,
WDF_QUEUE_CONTEXT_TYPE_INFO,
WDF_TIMER_CONFIG_SIZE,
};
/// This routine will interlock increment a value only if the current value
/// is greater then the floor value.
///
/// The volatile keyword on the Target pointer is absolutely required, otherwise
/// the compiler might rearrange pointer dereferences and that cannot happen.
///
/// # Arguments:
///
/// * `target` - the value that will be pontetially incrmented
/// * `floor` - the value in which the Target value must be greater then if it
/// is to be incremented
///
/// # Return value:
///
/// The current value of Target. To detect failure, the return value will be
/// <= Floor + 1. It is +1 because we cannot increment from the Floor value
/// itself, so Floor+1 cannot be a successful return value.
fn echo_interlocked_increment_floor(target: &AtomicI32, floor: i32) -> i32 {
let mut current_value = target.load(Ordering::SeqCst);
loop {
if current_value <= floor {
return current_value;
}
// currentValue will be the value that used to be Target if the exchange
// was made or its current value if the exchange was not made.
//
match target.compare_exchange(
current_value,
current_value + 1,
Ordering::SeqCst,
Ordering::SeqCst,
) {
// If oldValue == currentValue, then no one updated Target in between
// the deref at the top and the InterlockecCompareExchange afterward
// and we have successfully incremented the value and can exit the loop.
Ok(_) => break,
Err(v) => current_value = v,
}
}
current_value + 1
}
/// Increment the value only if it is currently > 0.
///
/// # Arguments:
///
/// * `target` - the value to be incremented
///
/// # Return value:
///
/// Upon success, a value > 0. Upon failure, a value <= 0.
fn echo_interlocked_increment_gtzero(target: &AtomicI32) -> i32 {
echo_interlocked_increment_floor(target, 0)
}
/// The I/O dispatch callbacks for the frameworks device object
/// are configured in this function.
///
/// A single default I/O Queue is configured for serial request
/// processing, and a driver context memory allocation is created
/// to hold our structure `QUEUE_CONTEXT`.
///
/// This memory may be used by the driver automatically synchronized
/// by the Queue's presentation lock.
///
/// The lifetime of this memory is tied to the lifetime of the I/O
/// Queue object, and we register an optional destructor callback
/// to release any private allocations, and/or resources.
///
/// # Arguments:
///
/// * `device` - Handle to a framework device object.
///
/// # Return value:
///
/// * `NTSTATUS`
#[link_section = "PAGE"]
pub unsafe fn echo_queue_initialize(device: WDFDEVICE) -> NTSTATUS {
paged_code!();
let mut queue = WDF_NO_HANDLE as WDFQUEUE;
// Configure a default queue so that requests that are not
// configure-fowarded using WdfDeviceConfigureRequestDispatching to goto
// other queues get dispatched here.
let mut queue_config = WDF_IO_QUEUE_CONFIG {
Size: WDF_IO_QUEUE_CONFIG_SIZE,
PowerManaged: _WDF_TRI_STATE::WdfUseDefault,
DefaultQueue: u8::from(true),
DispatchType: _WDF_IO_QUEUE_DISPATCH_TYPE::WdfIoQueueDispatchSequential,
EvtIoRead: Some(echo_evt_io_read),
EvtIoWrite: Some(echo_evt_io_write),
..WDF_IO_QUEUE_CONFIG::default()
};
// Fill in a callback for destroy, and our QUEUE_CONTEXT size
let mut attributes = WDF_OBJECT_ATTRIBUTES {
Size: WDF_OBJECT_ATTRIBUTES_SIZE,
ExecutionLevel: _WDF_EXECUTION_LEVEL::WdfExecutionLevelInheritFromParent,
SynchronizationScope: _WDF_SYNCHRONIZATION_SCOPE::WdfSynchronizationScopeInheritFromParent,
ContextTypeInfo: wdf_get_context_type_info!(QueueContext),
EvtDestroyCallback: Some(echo_evt_io_queue_context_destroy),
..WDF_OBJECT_ATTRIBUTES::default()
};
// Create queue.
let nt_status = unsafe {
call_unsafe_wdf_function_binding!(
WdfIoQueueCreate,
device,
&raw mut queue_config,
&raw mut attributes,
&raw mut queue
)
};
if !nt_success(nt_status) {
println!("WdfIoQueueCreate failed {nt_status:#010X}");
return nt_status;
}
// Get our Driver Context memory from the returned Queue handle
let queue_context: *mut QueueContext = unsafe { queue_get_context(queue as WDFOBJECT) };
unsafe {
(*queue_context).buffer = core::ptr::null_mut();
(*queue_context).current_request = core::ptr::null_mut();
(*queue_context).current_status = STATUS_INVALID_DEVICE_REQUEST;
}
// Create the SpinLock.
let mut attributes = WDF_OBJECT_ATTRIBUTES {
Size: WDF_OBJECT_ATTRIBUTES_SIZE,
ExecutionLevel: _WDF_EXECUTION_LEVEL::WdfExecutionLevelInheritFromParent,
SynchronizationScope: _WDF_SYNCHRONIZATION_SCOPE::WdfSynchronizationScopeInheritFromParent,
ParentObject: queue as WDFOBJECT,
..WDF_OBJECT_ATTRIBUTES::default()
};
match wdf::SpinLock::create(&mut attributes) {
Err(status) => {
println!("SpinLock create failed {nt_status:#010X}");
return status;
}
Ok(spin_lock) => unsafe { (*queue_context).spin_lock = spin_lock },
}
// Create the Queue timer
//
// By not setting the synchronization scope and using the default at
// WdfIoQueueCreate, we are explicitly *not* serializing against the queue's
// lock. Instead, we will do that on our own.
let mut timer_config = WDF_TIMER_CONFIG {
Size: WDF_TIMER_CONFIG_SIZE,
EvtTimerFunc: Some(echo_evt_timer_func),
Period: 10_000, // 10 seconds, in milliseconds
AutomaticSerialization: u8::from(true),
TolerableDelay: 0,
..WDF_TIMER_CONFIG::default()
};
match wdf::Timer::create(&mut timer_config, &mut attributes) {
Err(status) => {
println!("Timer create failed {nt_status:#010X}");
return status;
}
Ok(wdftimer) => unsafe { (*queue_context).timer = wdftimer },
}
STATUS_SUCCESS
}
/// This is called when the Queue that our driver context memory
/// is associated with is destroyed.
///
/// # Arguments:
///
/// * `object` - Queue object to be freed.
///
/// # Return value:
///
/// * `VOID`
extern "C" fn echo_evt_io_queue_context_destroy(object: WDFOBJECT) {
let queue_context = unsafe { queue_get_context(object) };
// Release any resources pointed to in the queue context.
//
// The body of the queue context will be released after
// this callback handler returns
// If Queue context has an I/O buffer, release it
unsafe {
if !(*queue_context).buffer.is_null() {
ExFreePool((*queue_context).buffer);
(*queue_context).buffer = core::ptr::null_mut();
}
}
}
/// Decrements the cancel ownership count for the request. When the count
/// reaches zero ownership has been acquired.
///
/// # Arguments:
///
/// * `request_context` - the context which holds the count.
///
/// # Return value:
///
/// * TRUE if the caller can complete the request, FALSE otherwise
fn echo_decrement_request_cancel_ownership_count(request_context: *mut RequestContext) -> bool {
let result = unsafe {
(*request_context)
.cancel_completion_ownership_count
.fetch_sub(1, Ordering::SeqCst)
};
result - 1 == 0
}
/// Attempts to increment the request ownership count so that it cannot be
/// completed until the count has been decremented
///
/// # Arguments:
///
/// * `request_context` - the context which holds the count.
///
/// # Return value:
///
/// * TRUE if the count was incremented, FALSE otherwise
fn echo_increment_request_cancel_ownership_count(request_context: *mut RequestContext) -> bool {
// See comments in echo_interlocked_increment_floor as to why <= 1 is failure
//
(unsafe {
echo_interlocked_increment_gtzero(&(*request_context).cancel_completion_ownership_count)
}) > 1
}
/// Called when an I/O request is cancelled after the driver has marked
/// the request cancellable. This callback is not automatically synchronized
/// with the I/O callbacks since we have chosen not to use frameworks Device
/// or Queue level locking.
///
/// # Arguments:
///
/// * `request` - Request being cancelled.
///
/// # Return value:
///
/// * `VOID`
extern "C" fn echo_evt_request_cancel(request: WDFREQUEST) {
let queue = unsafe { call_unsafe_wdf_function_binding!(WdfRequestGetIoQueue, request) };
let queue_context = unsafe { queue_get_context(queue as WDFOBJECT) };
let request_context = unsafe { request_get_context(request as WDFOBJECT) };
println!("echo_evt_request_cancel called on Request {:?}", request);
// This book keeping is synchronized by the common
// Queue presentation lock which we are now acquiring
unsafe { (*queue_context).spin_lock.acquire() };
let complete_request: bool = echo_decrement_request_cancel_ownership_count(request_context);
if complete_request {
unsafe {
(*queue_context).current_request = core::ptr::null_mut();
}
} else {
unsafe {
(*queue_context).current_status = STATUS_CANCELLED;
}
}
unsafe { (*queue_context).spin_lock.release() };
// Complete the request outside of holding any locks
if complete_request {
unsafe {
call_unsafe_wdf_function_binding!(
WdfRequestCompleteWithInformation,
request,
STATUS_CANCELLED,
0
);
}
}
}
/// Setup the request, intialize its context and mark it as cancelable.
///
/// # Arguments:
///
/// * `request` - Request being set up.
/// * `queue` - Queue associated with the request
///
/// # Return value:
///
/// * `VOID`
fn echo_set_current_request(request: WDFREQUEST, queue: WDFQUEUE) {
let status: NTSTATUS;
let request_context = unsafe { request_get_context(request as WDFOBJECT) };
let queue_context = unsafe { queue_get_context(queue as WDFOBJECT) };
// Set the ownership count to one. When a caller wants to claim ownership,
// they will interlock decrement the count. When the count reaches zero,
// ownership has been acquired and the caller may complete the request.
unsafe {
(*request_context).cancel_completion_ownership_count = AtomicI32::new(1);
}
// Defer the completion to another thread from the timer dpc
unsafe { (*queue_context).spin_lock.acquire() };
unsafe {
(*queue_context).current_request = request;
(*queue_context).current_status = STATUS_SUCCESS;
}
// Set the cancel routine under the lock, otherwise if we set it outside
// of the lock, the timer could run and attempt to mark the request
// uncancelable before we can mark it cancelable on this thread. Use
// WdfRequestMarkCancelableEx here to prevent to deadlock with ourselves
// (cancel routine tries to acquire the queue object lock).
unsafe {
status = call_unsafe_wdf_function_binding!(
WdfRequestMarkCancelableEx,
request,
Some(echo_evt_request_cancel)
);
if !nt_success(status) {
(*queue_context).current_request = core::ptr::null_mut();
}
}
unsafe { (*queue_context).spin_lock.release() };
unsafe {
// Complete the request with an error when unable to mark it cancelable.
if !nt_success(status) {
call_unsafe_wdf_function_binding!(
WdfRequestCompleteWithInformation,
request,
status,
0
);
}
}
}
/// This event is called when the framework receives `IRP_MJ_READ` request.
/// It will copy the content from the queue-context buffer to the request
/// buffer. If the driver hasn't received any write request earlier, the read
/// returns zero.
///
/// # Arguments:
///
/// * `queue` - Handle to the framework queue object that is associated with the
/// I/O request.
/// * `request` - Handle to a framework request object.
/// * `length` - number of bytes to be read. The default property of the queue
/// is to not dispatch zero lenght read & write requests to the driver and
/// complete is with status success. So we will never get a zero length
/// request.
///
/// # Return value:
///
/// * `VOID`
extern "C" fn echo_evt_io_read(queue: WDFQUEUE, request: WDFREQUEST, mut length: usize) {
let queue_context = unsafe { queue_get_context(queue as WDFOBJECT) };
let mut memory = WDF_NO_HANDLE as WDFMEMORY;
let mut nt_status: NTSTATUS;
println!(
"echo_evt_io_read called! queue {:?}, request {:?}, length {:?}",
queue, request, length
);
// No data to read
unsafe {
if (*queue_context).buffer.is_null() {
call_unsafe_wdf_function_binding!(
WdfRequestCompleteWithInformation,
request,
STATUS_SUCCESS,
0,
);
return;
}
}
// Read what we have
unsafe {
if (*queue_context).length < length {
length = (*queue_context).length;
}
}
// Get the request memory
unsafe {
nt_status = call_unsafe_wdf_function_binding!(
WdfRequestRetrieveOutputMemory,
request,
&raw mut memory
);
if !nt_success(nt_status) {
println!("echo_evt_io_read Could not get request memory buffer {nt_status:#010X}");
call_unsafe_wdf_function_binding!(
WdfRequestCompleteWithInformation,
request,
nt_status,
0
);
return;
}
}
// Copy the memory out
unsafe {
nt_status = call_unsafe_wdf_function_binding!(
WdfMemoryCopyFromBuffer,
memory,
0,
(*queue_context).buffer,
length
);
if !nt_success(nt_status) {
println!("echo_evt_io_read: WdfMemoryCopyFromBuffer failed {nt_status:#010X}");
call_unsafe_wdf_function_binding!(WdfRequestComplete, request, nt_status);
return;
}
}
// Set transfer information
let [()] = unsafe {
[call_unsafe_wdf_function_binding!(
WdfRequestSetInformation,
request,
length as u64
)]
};
// Mark the request is cancelable. This must be the last thing we do because
// the cancel routine can run immediately after we set it. This means that
// CurrentRequest and CurrentStatus must be initialized before we mark the
// request cancelable.
echo_set_current_request(request, queue);
}
/// This event is invoked when the framework receives `IRP_MJ_WRITE` request.
/// This routine allocates memory buffer, copies the data from the request to
/// it, and stores the buffer pointer in the queue-context with the length
/// variable representing the buffers length. The actual completion of the
/// request is defered to the periodic timer dpc.
///
/// # Arguments:
///
/// * `queue` - Handle to the framework queue object that is associated with the
/// I/O request.
/// * `request` - Handle to a framework request object.
/// * `length` - number of bytes to be read. The default property of the queue
/// is to not dispatch zero lenght read & write requests to the driver and
/// complete is with status success. So we will never get a zero length
/// request.
///
/// # Return value:
///
/// * `VOID`
extern "C" fn echo_evt_io_write(queue: WDFQUEUE, request: WDFREQUEST, length: usize) {
/// Number of bytes in one kilobyte.
const BYTES_PER_KB: usize = 1024;
/// Max write length, in bytes, for testing
const MAX_WRITE_LENGTH: usize = 40 * BYTES_PER_KB;
/// Non-zero char literal (of one to four chars) for pool tag used in
/// `ExAllocatePool2`
const MEMORY_TAG: u32 = u32::from_be_bytes(*b"sam1");
let mut memory = WDF_NO_HANDLE as WDFMEMORY;
let mut status: NTSTATUS;
let queue_context = unsafe { queue_get_context(queue as WDFOBJECT) };
println!(
"echo_evt_io_write called! queue {:?}, request {:?}, length {:?}",
queue, request, length
);
if length > MAX_WRITE_LENGTH {
println!(
"echo_evt_io_write Buffer Length to big {:?}, Max is {:?}",
length, MAX_WRITE_LENGTH
);
unsafe {
call_unsafe_wdf_function_binding!(
WdfRequestCompleteWithInformation,
request,
STATUS_BUFFER_OVERFLOW,
0
);
}
}
// Get the memory buffer
unsafe {
status = call_unsafe_wdf_function_binding!(
WdfRequestRetrieveInputMemory,
request,
&raw mut memory
);
if !nt_success(status) {
println!("echo_evt_io_write Could not get request memory buffer {status:#010X}");
call_unsafe_wdf_function_binding!(WdfRequestComplete, request, status);
return;
}
}
// Release previous buffer if set
unsafe {
if !(*queue_context).buffer.is_null() {
ExFreePool((*queue_context).buffer);
(*queue_context).buffer = core::ptr::null_mut();
(*queue_context).length = 0;
}
(*queue_context).buffer =
ExAllocatePool2(POOL_FLAG_NON_PAGED, length as SIZE_T, MEMORY_TAG);
if (*queue_context).buffer.is_null() {
println!(
"echo_evt_io_write Could not allocate {:?} byte buffer",
length
);
call_unsafe_wdf_function_binding!(
WdfRequestComplete,
request,
STATUS_INSUFFICIENT_RESOURCES
);
return;
}
}
// Copy the memory in
unsafe {
status = call_unsafe_wdf_function_binding!(
WdfMemoryCopyToBuffer,
memory,
0,
(*queue_context).buffer,
length
);
if !nt_success(status) {
println!("echo_evt_io_write WdfMemoryCopyToBuffer failed {status:#010X}");
ExFreePool((*queue_context).buffer);
(*queue_context).buffer = core::ptr::null_mut();
(*queue_context).length = 0;
call_unsafe_wdf_function_binding!(WdfRequestComplete, request, status);
return;
}
(*queue_context).length = length;
}
// Set transfer information
unsafe {
call_unsafe_wdf_function_binding!(WdfRequestSetInformation, request, length as u64);
}
// Mark the request is cancelable. This must be the last thing we do because
// the cancel routine can run immediately after we set it. This means that
// CurrentRequest and CurrentStatus must be initialized before we mark the
// request cancelable.
echo_set_current_request(request, queue);
}
/// This is the `TimerDPC` the driver sets up to complete requests.
/// This function is registered when the WDFTIMER object is created.
///
/// This function does *NOT* automatically synchronize with the I/O Queue
/// callbacks and cancel routine, we must do it ourself in the routine.
///
/// # Arguments:
///
/// * `timer` - Handle to a framework Timer object.
///
/// # Return value:
///
/// * `VOID`
unsafe extern "C" fn echo_evt_timer_func(timer: WDFTIMER) {
// Default to failure. status is initialized so that the compiler does not
// think we are using an uninitialized value when completing the request.
let mut status;
let mut cancel = false;
let complete_request;
let queue: WDFQUEUE;
let request: WDFREQUEST;
let mut request_context: *mut RequestContext = core::ptr::null_mut();
unsafe {
queue = call_unsafe_wdf_function_binding!(WdfTimerGetParentObject, timer,) as WDFQUEUE;
}
let queue_context = unsafe { queue_get_context(queue as WDFOBJECT) };
// We must synchronize with the cancel routine which will be taking the
// request out of the context under this lock.
unsafe { (*queue_context).spin_lock.acquire() };
unsafe {
request = (*queue_context).current_request;
}
if !request.is_null() {
request_context = unsafe { request_get_context(request as WDFOBJECT) };
if echo_increment_request_cancel_ownership_count(request_context) {
cancel = true;
} else {
// What has happened is that the cancel routine has executed and
// has already claimed cancel ownership of the request, but has not
// yet acquired the object lock and cleared the CurrentRequest field
// in queueContext. In this case, do nothing and let the cancel
// routine run to completion and complete the request.
}
}
unsafe { (*queue_context).spin_lock.release() };
// If we could not claim cancel ownership, we are done.
if !cancel {
return;
}
// The request handle and requestContext are valid until we release
// the cancel ownership count we already acquired.
unsafe {
status = call_unsafe_wdf_function_binding!(WdfRequestUnmarkCancelable, request,);
if status == STATUS_CANCELLED {
complete_request = echo_decrement_request_cancel_ownership_count(request_context);
if complete_request {
println!(
"CustomTimerDPC Request {:?} is STATUS_CANCELLED, but claimed completion \
ownership",
request
);
} else {
println!(
"CustomTimerDPC Request {:?} is STATUS_CANCELLED, not completing",
request
);
}
} else {
println!(
"CustomTimerDPC successfully cleared cancel routine on request {:?}, status {:?}",
request, status
);
// Since we successfully removed the cancel routine (and we are not
// currently racing with it), there is no need to use an interlocked
// decrement to lower the cancel ownership count.
// 2 = the initial ownership count (1) plus the one increment
// acquired via echo_increment_request_cancel_ownership_count.
(*request_context)
.cancel_completion_ownership_count
.fetch_sub(2, Ordering::SeqCst);
complete_request = true;
}
}
if complete_request {
println!(
"CustomTimerDPC Completing request {:?}, status {:?}",
request, status
);
// Clear the current request out of the queue context and complete
// the request.
unsafe { (*queue_context).spin_lock.acquire() };
unsafe {
(*queue_context).current_request = core::ptr::null_mut();
status = (*queue_context).current_status;
}
unsafe { (*queue_context).spin_lock.release() };
unsafe {
call_unsafe_wdf_function_binding!(WdfRequestComplete, request, status);
}
}
}