Skip to content

Disastrous threaded (and not) performance #20698

Closed as not planned
Closed as not planned

Description

@nicowilliams

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

  1. added
    issue: bug report
    on Apr 10, 2023
  2. beldmit commented on Apr 10, 2023

    @beldmit
    Member

    Could you please test the performance with 3.1 where it was significantly improved?

  3. nicowilliams commented on Apr 10, 2023

    @nicowilliams
    Author

    Now, some of this is probably bugs in gRPC. For example, x509_store_load_certs() should probably only get called once, not many times.

  4. nicowilliams commented on Apr 10, 2023

    @nicowilliams
    Author

    @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.

  5. nicowilliams commented on Apr 10, 2023

    @nicowilliams
    Author

    We'll try using LD_PRELOAD and we'll report.

  6. nicowilliams commented on Apr 10, 2023

    @nicowilliams
    Author

    Ooof, also, gRPC (version 1.49.1) is doing 25 SSL_CTX_new() calls per-connection! Clearly there's gRPC bugs here too. I'll open a ticket with them.

  7. paulidale commented on Apr 10, 2023

    @paulidale
    Contributor

    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).

  8. added
    triaged: bug
    and removed
    issue: bug report
    on Apr 10, 2023
  9. nicowilliams commented on Apr 11, 2023

    @nicowilliams
    Author

    @paulidale on the gRPC issue I opened @davidben wrote:

    A correction, it's not a library context type. It's for related connections, like an instance of a TLS server. If two connections are unrelated, using different SSL_CTXs makes sense. This matters because some TLS features, like resumption, result in sharing state across connections and over-sharing means security and privacy issues.

    Not to say 25 contexts per connection makes sense. Something sounds wrong there. But a singleton SSL_CTX for the entire library would also not be right. It sounds like OpenSSL 3.0 has started making them expensive, but do keep in mind that typical workflows here are more complex than a singleton library context.

  10. geofft commented on Apr 14, 2023

    @geofft

    Could you please test the performance with 3.1 where it was significantly improved?

    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.

  11. added
    branch: master
    branch: 3.0
    branch: 3.1
    on Oct 9, 2023
  12. added
    branch: 3.2
    on Oct 26, 2023
  13. added
    triaged: performance
    on Nov 2, 2023
  14. nhorman commented on Jun 7, 2024

    @nhorman
    Contributor

    marking as inactive, will be closed at the completion of 3.4 dev without further input

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    branch: 3.0branch: 3.1branch: 3.2branch: mastertriaged: bugtriaged: performance

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions