Skip to content

Commit

Permalink
Avoid raising and lowering the IRQL if not needed (#4007)
Browse files Browse the repository at this point in the history
* Avoid raising and lowering the IRQL if not needed

Signed-off-by: Alan Jowett <[email protected]>

* PR feedback

Signed-off-by: Alan Jowett <[email protected]>

---------

Signed-off-by: Alan Jowett <[email protected]>
Co-authored-by: Alan Jowett <[email protected]>
  • Loading branch information
Alan-Jowett and Alan Jowett authored Nov 13, 2024
1 parent 50df22f commit 3bdeb43
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
14 changes: 9 additions & 5 deletions libs/execution_context/ebpf_maps.c
Original file line number Diff line number Diff line change
Expand Up @@ -1177,10 +1177,9 @@ _Requires_lock_held_(map->partitions[partition].lock) static void _merge_hot_int
static void
_insert_into_hot_list(_Inout_ ebpf_core_lru_map_t* map, size_t partition, _Inout_ ebpf_lru_entry_t* entry)
{
bool lock_held = false;

ebpf_lru_key_state_t key_state = _get_key_state(map, partition, entry);
ebpf_lock_state_t state = 0;
bool is_preemptible = ebpf_is_preemptible();

switch (key_state) {
case EBPF_LRU_KEY_UNINITIALIZED:
Expand All @@ -1193,8 +1192,11 @@ _insert_into_hot_list(_Inout_ ebpf_core_lru_map_t* map, size_t partition, _Inout
return;
}

state = ebpf_lock_lock(&map->partitions[partition].lock);
lock_held = true;
if (!is_preemptible) {
ebpf_lock_lock_at_dispatch(&map->partitions[partition].lock);
} else {
state = ebpf_lock_lock(&map->partitions[partition].lock);
}

key_state = _get_key_state(map, partition, entry);

Expand Down Expand Up @@ -1223,7 +1225,9 @@ _insert_into_hot_list(_Inout_ ebpf_core_lru_map_t* map, size_t partition, _Inout

_merge_hot_into_cold_list_if_needed(map, partition);

if (lock_held) {
if (!is_preemptible) {
ebpf_lock_unlock_at_dispatch(&map->partitions[partition].lock);
} else {
ebpf_lock_unlock(&map->partitions[partition].lock, state);
}
}
Expand Down
12 changes: 12 additions & 0 deletions libs/runtime/ebpf_platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ _Requires_lock_held_(*lock) _Releases_lock_(*lock) _IRQL_requires_(DISPATCH_LEVE
KeReleaseSpinLock(lock, state);
}

_Requires_lock_not_held_(*lock) _Acquires_lock_(*lock) _IRQL_requires_max_(DISPATCH_LEVEL)
_IRQL_requires_(DISPATCH_LEVEL) void ebpf_lock_lock_at_dispatch(_Inout_ ebpf_lock_t* lock)
{
KeAcquireSpinLockAtDpcLevel(lock);
}

_Requires_lock_held_(*lock) _Releases_lock_(*lock)
_IRQL_requires_(DISPATCH_LEVEL) void ebpf_lock_unlock_at_dispatch(_Inout_ ebpf_lock_t* lock)
{
KeReleaseSpinLockFromDpcLevel(lock);
}

bool
ebpf_is_preemptible()
{
Expand Down
16 changes: 16 additions & 0 deletions libs/runtime/ebpf_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,22 @@ extern "C"
_Requires_lock_held_(*lock) _Releases_lock_(*lock) _IRQL_requires_(DISPATCH_LEVEL) void ebpf_lock_unlock(
_Inout_ ebpf_lock_t* lock, _IRQL_restores_ ebpf_lock_state_t state);

/**
* @brief Acquire exclusive access to the lock.
* @param[in, out] lock Pointer to memory location that contains the lock.
* @returns The previous lock_state required for unlock.
*/
_Requires_lock_not_held_(*lock) _Acquires_lock_(*lock) _IRQL_requires_(DISPATCH_LEVEL)
_IRQL_requires_max_(DISPATCH_LEVEL) void ebpf_lock_lock_at_dispatch(_Inout_ ebpf_lock_t* lock);

/**
* @brief Release exclusive access to the lock.
* @param[in, out] lock Pointer to memory location that contains the lock.
* @param[in] state The state returned from ebpf_lock_lock.
*/
_Requires_lock_held_(*lock) _Releases_lock_(*lock)
_IRQL_requires_(DISPATCH_LEVEL) void ebpf_lock_unlock_at_dispatch(_Inout_ ebpf_lock_t* lock);

/**
* @brief Raise the IRQL to new_irql.
*
Expand Down

0 comments on commit 3bdeb43

Please sign in to comment.