From patchwork Thu Dec 7 10:32:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 81651 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B874938618DC for ; Thu, 7 Dec 2023 10:34:31 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id BB80A38618B8 for ; Thu, 7 Dec 2023 10:32:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BB80A38618B8 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org BB80A38618B8 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701945170; cv=none; b=vVY5LsigRrR7MeT5rxFmqG+/R3Juowxe/jdd/oYSQLH4iAdYzFeJNM+pFXfDobZfWc3OWFS3EaKybG6PEj0orRx93gtteyeip4C2/UA67C8lkcnaOOLsHKaedFSUOcj2XQxMiXPS+Zq16rbqebjqLoZoyVvc9kjUQ+Dnis7DA94= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701945170; c=relaxed/simple; bh=NXPWa4ReOXIkfCPjd/vG8iHGQnn/YvwDy2YyhZY392A=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=DVAxB6SJcaL91rWJhDdRm+pP98eRY8jLUgUHayvaANRuMpXpWOwR36B6VcQTeqf6ESr7vUapDDmHlJDn1R7wEDlyxPxgwF9dBhbdKTTxAgUZItMtL9yaIr6MlXnjjNx9ZwQb6hpGrt6hzshSj79xuamokXEy2diEjNBVVcjiFqA= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701945168; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=GWNN5bydB1ePxIqBh4NqoLSXsxS555oloIYfakg6wio=; b=SBON5QjyZo6PmKJOiKeBOQOtD9+oBeXPKq2nJQi7gxaHYpxtznJTO8xgXiH1xNmTC/Hqvd iGMDr/UbJAx2g/lQRH4Jnyy2eQ2s0OiWXzphyP7+H9mi3HZn2NhYUw27ASIHTeVj/eV+3W X/F1mEYqr7CsoJ7a9TkBT5THMMt9/OA= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-237-Wvx_0zmSMUmEUxvI_h7ESg-1; Thu, 07 Dec 2023 05:32:46 -0500 X-MC-Unique: Wvx_0zmSMUmEUxvI_h7ESg-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B2CE23C0264F for ; Thu, 7 Dec 2023 10:32:46 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.39.192.131]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 2CA9B3C2E for ; Thu, 7 Dec 2023 10:32:46 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH v3 28/32] elf: Add fast path to dlopen for fully-opened maps In-Reply-To: Message-ID: References: X-From-Line: a4400193c257759deb92bf9bf431b1fd71e6707f Mon Sep 17 00:00:00 2001 Date: Thu, 07 Dec 2023 11:32:44 +0100 User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.3 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org --- elf/dl-open.c | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/elf/dl-open.c b/elf/dl-open.c index 52e44804cf..a8c9945f2b 100644 --- a/elf/dl-open.c +++ b/elf/dl-open.c @@ -533,6 +533,23 @@ call_dl_init (void *closure) _dl_init (args->new, args->argc, args->argv, args->env); } +/* Return true if the object does not need any processing beyond the + l_direct_opencount update. Needs to be kept in sync with the logic + in dl_o-en_worker_begin after the l->l_searchlist.r_list != NULL check. + MODE is the dlopen mode argument. */ +static bool +is_already_fully_open (struct link_map_private *map, int mode) +{ + return (map != NULL /* An existing map was found. */ + /* dlopen completed initialization of this map. Maps with + l_type == lt_library start out as partially initialized. */ + && map->l_searchlist.r_list != NULL + /* The object is already in the global scope if requested. */ + && (!(mode & RTLD_GLOBAL) || map->l_global) + /* The object is already NODELETE if requested. */ + && (!(mode & RTLD_NODELETE) || map->l_rw->l_nodelete_active)); +} + static void dl_open_worker_begin (void *a) { @@ -559,9 +576,10 @@ dl_open_worker_begin (void *a) _dl_debug_initialize (0, args->nsid); /* Load the named object. */ - struct link_map_private *new; - args->map = new = _dl_map_object (args->caller_map, file, lt_loaded, 0, - mode | __RTLD_CALLMAP, args->nsid); + struct link_map_private *new = args->map; + if (new == NULL) + args->map = new = _dl_map_new_object (args->caller_map, file, lt_loaded, 0, + mode | __RTLD_CALLMAP, args->nsid); /* If the pointer returned is NULL this means the RTLD_NOLOAD flag is set and the object is not already loaded. */ @@ -578,7 +596,7 @@ dl_open_worker_begin (void *a) /* This object is directly loaded. */ ++new->l_rw->l_direct_opencount; - /* It was already open. */ + /* It was already open. See is_already_fully_open above. */ if (__glibc_unlikely (new->l_searchlist.r_list != NULL)) { /* Let the user know about the opencount. */ @@ -881,7 +899,6 @@ no more namespaces available for dlmopen()")); struct dl_open_args args; args.file = file; args.mode = mode; - args.map = NULL; args.nsid = nsid; /* args.libc_already_loaded is always assigned by dl_open_worker (before any explicit/non-local returns). */ @@ -906,6 +923,15 @@ no more namespaces available for dlmopen()")); else args.caller_map = NULL; + args.map = _dl_lookup_map (args.nsid, file); + if (is_already_fully_open (args.map, mode)) + { + /* We can use the fast path. */ + ++args.map->l_rw->l_direct_opencount; + __rtld_lock_unlock_recursive (GL(dl_load_lock)); + return args.map; + } + struct dl_exception exception; int errcode = _dl_catch_exception (&exception, dl_open_worker, &args);