Skip to content

Commit

Permalink
Remedy potential lock-order inversion.
Browse files Browse the repository at this point in the history
Rearrange locking and unlocking in get_space() and drop_space() to
avoid a potential lock-order inversion.
  • Loading branch information
madler committed Apr 3, 2019
1 parent da451f7 commit 1e847e6
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions pigz.c
Expand Up @@ -1384,9 +1384,9 @@ local struct space *get_space(struct pool *pool) {
// if a space is available, pull it from the list and return it
if (pool->head != NULL) {
space = pool->head;
possess(space->use);
pool->head = space->next;
twist(pool->have, BY, -1); // one less in pool
possess(space->use);
twist(space->use, TO, 1); // initially one user
space->len = 0;
return space;
Expand Down Expand Up @@ -1424,7 +1424,11 @@ local void grow_space(struct space *space) {
// Increment the use count to require one more drop before returning this space
// to the pool.
local void use_space(struct space *space) {
long use;

possess(space->use);
use = peek_lock(space->use);
assert(use != 0);
twist(space->use, BY, +1);
}

Expand All @@ -1438,14 +1442,14 @@ local void drop_space(struct space *space) {
possess(space->use);
use = peek_lock(space->use);
assert(use != 0);
twist(space->use, BY, -1);
if (use == 1) {
pool = space->pool;
possess(pool->have);
space->next = pool->head;
pool->head = space;
twist(pool->have, BY, +1);
}
twist(space->use, BY, -1);
}

// Free the memory and lock resources of a pool. Return number of spaces for
Expand Down

0 comments on commit 1e847e6

Please sign in to comment.