Skip to content

Commit

Permalink
Don't drop EspWifiPacketBuffer in a critical section
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Oct 30, 2023
1 parent 9567b5d commit 03ebc5d
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions esp-wifi/src/wifi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,23 +645,28 @@ unsafe extern "C" fn recv_cb(
len: u16,
eb: *mut c_types::c_void,
) -> esp_err_t {
critical_section::with(|cs| {
let result = critical_section::with(|cs| {
let packet = EspWifiPacketBuffer { buffer, len, eb };

match DATA_QUEUE_RX.borrow_ref_mut(cs).enqueue(packet) {
Ok(_) => {
#[cfg(feature = "embassy-net")]
embassy::RECEIVE_WAKER.wake();
DATA_QUEUE_RX.borrow_ref_mut(cs).enqueue(packet)
});

include::ESP_OK as esp_err_t
}
match result {
Ok(_) => {
#[cfg(feature = "embassy-net")]
embassy::RECEIVE_WAKER.wake();

_ => {
debug!("RX QUEUE FULL");
include::ESP_ERR_NO_MEM as esp_err_t
}
include::ESP_OK as esp_err_t
}
})

Err(buffer) => {
// EspWifiPacketBuffer::drop must not be called in a critical section
core::mem::drop(buffer);

debug!("RX QUEUE FULL");
include::ESP_ERR_NO_MEM as esp_err_t
}
}
}

pub(crate) static WIFI_TX_INFLIGHT: AtomicUsize = AtomicUsize::new(0);
Expand Down Expand Up @@ -1026,18 +1031,19 @@ fn rx_token_consume<R, F>(f: F) -> R
where
F: FnOnce(&mut [u8]) -> R,
{
critical_section::with(|cs| {
let mut data = critical_section::with(|cs| {
let mut queue = DATA_QUEUE_RX.borrow_ref_mut(cs);

let mut data = unwrap!(
unwrap!(
queue.dequeue(),
"unreachable: transmit()/receive() ensures there is a packet to process"
);
let buffer = data.as_slice_mut();
dump_packet_info(&buffer);
)
});

let buffer = data.as_slice_mut();
dump_packet_info(&buffer);

f(buffer)
})
f(buffer)
}

fn tx_token_consume<R, F>(len: usize, f: F) -> R
Expand Down

0 comments on commit 03ebc5d

Please sign in to comment.