Repository navigation
Description
With OpenSSL 3.0.8 we've observed a 10x performance regression in a gRPC client application with a hot stack like this:
CRYPTO_THREAD_write_lock (threads_pthread.c:112)
algorithm_do_map (core_algorithm.c:48)
algorithm_do_this (core_algorithm.c:122)
ossl_provider_doall_activated (provider_core.c:1431)
ossl_algorithm_do_all (core_algorithm.c:162)
ossl_method_construct (core_fetch.c:162)
inner_evp_generic_fetch (evp_fetch.c:344)
evp_generic_do_all (evp_fetch.c:654)
EVP_KEYMGMT_do_all_provided (keymgmt_meth.c:295)
sk_EVP_KEYMGMT_num (decoder_pkey.c:54)
ossl_decoder_ctx_setup_for_pkey (decoder_pkey.c:350)
OSSL_DECODER_CTX_new_for_pkey (decoder_pkey.c:450)
x509_pubkey_ex_d2i_ex (x_pubkey.c:208)
asn1_item_embed_d2i (tasn_dec.c:262)
asn1_template_noexp_d2i (tasn_dec.c:684)
asn1_template_ex_d2i (tasn_dec.c:558)
asn1_item_embed_d2i (tasn_dec.c:424)
asn1_template_noexp_d2i (tasn_dec.c:684)
asn1_template_ex_d2i (tasn_dec.c:558)
asn1_item_embed_d2i (tasn_dec.c:424)
asn1_item_ex_d2i_intern (tasn_dec.c:120)
ASN1_item_d2i_ex (tasn_dec.c:144)
d2i_X509_AUX (x_x509.c:201)
PEM_ASN1_read_bio (pem_oth.c:31)
x509_store_load_certs (libgrpc.so.27.0.0)
tsi_create_ssl_client_handshaker_factory_with_options (libgrpc.so.27.0.0)
grpc_ssl_channel_security_connector_create (libgrpc.so.27.0.0)
grpc_ssl_credentials::create_security_connector (libgrpc.so.27.0.0)
grpc_core::(anonymous namespace)::Chttp2SecureClientChannelFactory::CreateSubchannel (libgrpc.so.27.0.0)
grpc_core::ClientChannel::ClientChannelControlHelper::CreateSubchannel (libgrpc.so.27.0.0)
grpc_core::ChildPolicyHandler::Helper::CreateSubchannel (libgrpc.so.27.0.0)
grpc_core::(anonymous namespace)::PickFirst::AttemptToConnectUsingLatestUpdateArgsLocked (libgrpc.so.27.0.0)
grpc_core::ChildPolicyHandler::UpdateLocked (libgrpc.so.27.0.0)
grpc_core::ClientChannel::CreateOrUpdateLbPolicyLocked (libgrpc.so.27.0.0)
grpc_core::ClientChannel::OnResolverResultChangedLocked (libgrpc.so.27.0.0)
grpc_core::ClientChannel::ResolverResultHandler::ReportResult (libgrpc.so.27.0.0)
grpc_core::PollingResolver::OnRequestCompleteLocked (libgrpc.so.27.0.0)
std::_Function_handler<void (), grpc_core::PollingResolver::OnRequestComplete(grpc_core::Resolver::Result)::{lambda()#1}>::_M_invoke (libgrpc.so.27.0.0)
grpc_core::WorkSerializer::WorkSerializerImpl::Run (libgrpc.so.27.0.0)
grpc_core::WorkSerializer::Run (libgrpc.so.27.0.0)
grpc_core::PollingResolver::OnRequestComplete (libgrpc.so.27.0.0)
grpc_core::(anonymous namespace)::AresClientChannelDNSResolver::AresRequestWrapper::OnHostnameResolved (libgrpc.so.27.0.0)
grpc_core::ExecCtx::Flush (libgrpc.so.27.0.0)
end_worker (libgrpc.so.27.0.0)
pollset_work (libgrpc.so.27.0.0)
pollset_work (libgrpc.so.27.0.0)
grpc_pollset_work (libgrpc.so.27.0.0)
cq_pluck (libgrpc.so.27.0.0)
Clearly a minimum reproducer is a program that calls x509_store_load_certs() in multiple threads concurrently.
As well we can reproduce what we think is a similar bug using a single-threaded loop over SSL_CTX_new(TLS_method()), with OpenSSL 3.0.8 being 5x slower than OpenSSL 1.1.1 to execute that 10k times. 5x slower in a single-threaded program!
We believe this is due to design errors in OpenSSL 3.x's internal locking around various data structures that causes rw locks to be taken for writing when the caller only needs to be reading.
As well, we believe the code in ossl_algorithm_do_all() is fundamentally incorrect: it is not safe to take a lock on an object that one might destruct before taking the object out of its container!
Things like the provider list in OpenSSL should rarely ever change, thus those seem like a good candidate for user-land RCU (read-copy-update) or similar techniques so that readers can pay a lower cost still than taking an rw lock for reading. RW locks are problematic because readers sometimes must block when racing against writers in order to avoid writer starvation, whereas RCU and RCU-like schemes can allow any number of concurrent readers to make progress without blocking no matter the rate of write transactions while also not starving writers.
Activity
beldmit commentedon Apr 10, 2023
Could you please test the performance with 3.1 where it was significantly improved?
nicowilliams commentedon Apr 10, 2023
Now, some of this is probably bugs in gRPC. For example,
x509_store_load_certs()should probably only get called once, not many times.nicowilliams commentedon Apr 10, 2023
@beldmit well, not easily. The problem is that this is all in a monorepo, and it takes a great deal of work for us to introduce a new version of OpenSSL into it.
nicowilliams commentedon Apr 10, 2023
We'll try using
LD_PRELOADand we'll report.nicowilliams commentedon Apr 10, 2023
Ooof, also, gRPC (version
1.49.1) is doing 25SSL_CTX_new()calls per-connection! Clearly there's gRPC bugs here too. I'll open a ticket with them.paulidale commentedon Apr 10, 2023
25!? That's crazy high.
Ideally, there should be zero calls to SSL_CTX_new() per connection. Creating a SSL_CTX is an expensive operation (and it's become more expensive in 3.x than it was in 1.1.1).
nicowilliams commentedon Apr 11, 2023
@paulidale on the gRPC issue I opened @davidben wrote:
geofft commentedon Apr 14, 2023
In an internal test case with gRPC Python +
concurrent.futures.ThreadPoolExecutor, 32 threaded calls take between 1.2 to 2.9 seconds per call with OpenSSL 1.1.1, between 41.5 and 49.4 s with OpenSSL 3.0.8, and between 6.8 and 11.9 s with OpenSSL 3.1.0 (preloaded). So, yes, performance is significantly improved when compared to 3.0, but still a factor of four slower than 1.1.1.For comparison, serial calls take about 0.23 s on 1.1.1, 1.05 s on 3.0, and .55 s on 3.1. So again improved but noticeably worse than 1.1.1.
I'll see if I can turn this into an external test case. I'm pretty sure that the internal test case amounts to making 25 SSL connections (with their own context) per call, though there might be some internal timeouts/retries exacerbating this.
nhorman commentedon Jun 7, 2024
marking as inactive, will be closed at the completion of 3.4 dev without further input