Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mutex locking in _pkcs11h_threading_cond{Init,Wait} functions. #65

Closed
wants to merge 1 commit into from

Conversation

arrowd
Copy link
Contributor

@arrowd arrowd commented Jan 20, 2024

According to pthread_cond_wait manpage, the mutex has to be locked by the same thread that is going to call pthread_cond_wait afterwards. This was not true before this change and was causing test-slotevent to busy-loop on FreeBSD.

Copy link
Member

@alonbl alonbl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much for finding this!

I have two questions.

lib/pkcs11h-threading.c Show resolved Hide resolved
rv = CKR_FUNCTION_FAILED;
goto cleanup;
}
unlock_mutex = 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should wait not fail if other holds the lock, can it be done?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could spin in a loop calling pthread_mutex_trylock until times passes out. But this is basically a spinlock.
Not sure if this is a good idea.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as far as I remember this was my initial use of cond as mutex cannot wait... yes, spin loop is bad, there must be a different solution, otherwise the pthread_cond_timedwait has little sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the mutex is only used for waiting on the condition variable, the amount of time it is locked should be really short. Maybe spinlock here is actually OK.

Anyways, this change doesn't make the code any worse than it was.

@alonbl
Copy link
Member

alonbl commented Jan 21, 2024

Hi,
I modified a bit this patch and saw Sponsored by: Serenity Cybersecurity, LLC comment in commit message, this is not something that I can apply. Please use the standard sign-off-by statement.

@arrowd
Copy link
Contributor Author

arrowd commented Jan 21, 2024

Let's just remove the "Sponsored by" tag, then.

@alonbl
Copy link
Member

alonbl commented Jan 21, 2024

please sign-off so we have no future issues, trivial issues I accept without.

According to pthread_cond_wait manpage, the mutex has to be locked by the same
thread that is going to call pthread_cond_wait afterwards. This was not true
before this change and was causing test-slotevent to busy-loop on FreeBSD.

Signed-off-by: Gleb Popov <[email protected]>
@alonbl
Copy link
Member

alonbl commented Jan 22, 2024

Thank you, merged with some minor changes.

@alonbl alonbl closed this Jan 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants