From patchwork Mon Jul 6 21:14:39 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 138616 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2780B4BA2E2B for ; Mon, 6 Jul 2026 21:17:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2780B4BA2E2B Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=n6FaHl/l X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pg1-x530.google.com (mail-pg1-x530.google.com [IPv6:2607:f8b0:4864:20::530]) by sourceware.org (Postfix) with ESMTPS id 9161B4BA2E14 for ; Mon, 6 Jul 2026 21:15:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9161B4BA2E14 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 9161B4BA2E14 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::530 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1783372534; cv=none; b=UAtLuQoaN7xwTcDBymGwfCSgtNbAMzdHX3xxpiK4p0XkEfl/V8UAb0h19LkQxYxWG6hzt63kiE2NzoD7+7PNaCLkwMcJ7zDm3pqkNUpsGcrlai9/wBYeWZPnlDSQONBf+EjIG7hZ8wDG+rKEcZXAbkG0+Q1NqQSQkoHvyGTqGcc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1783372534; c=relaxed/simple; bh=jJnjIbY8Kxu2sSKMM+cs4shLk7042RhlihG7HclTMEE=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=k132pYs49TZjokBG4xowyO+bwevJ2/lrw+gZAVspLEJWim/AeFt4+5NLkp9cfiYHObEqFIpWzlbsrbBsHLKZ76O1LT2R4WDNeWcE0nJpGB2wuoYA2MWppeqMQdInf6VQHDsasWOQWwFEB+WacRD8Rd8Hs9ViFdVfPbHQY4x4Gsc= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=n6FaHl/l DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9161B4BA2E14 Received: by mail-pg1-x530.google.com with SMTP id 41be03b00d2f7-c9cf07d2df6so2305134a12.2 for ; Mon, 06 Jul 2026 14:15:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1783372533; x=1783977333; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=TfE3ubozfsTDyYWz4RVKoypK6AxOoNW1iX5f6CTwGQk=; b=n6FaHl/lXlFvuFbz8WTqEnQ/W3bhliit1acZ5cWCZuo2edA6zTTiOVYABd95pVmkSx JDYuxnowyRhNGB3VjdKlhgEPnx9/k9XWcinrheMazXnMJ1gtGewjO55fGU3DSO3Zfouf urY+6jNfCxCr5XpVi4QEgA8iVELjMc1j3s+eZKFdHTCM/j3nAPzzwy0SFBa3989UiW48 I3mliEau7VJy4SNyczEjxbNDS4FhN7WLQSELcQMHRLEpTeewRBU4YSPW1qPJYiOwEDi1 bu5ARCIAHzF+ct8BMuYboPU8UF5P0TneG+mKD6+MjEroGrD/YWrb6UelWx/xUMwuuC2F NdPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783372533; x=1783977333; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=TfE3ubozfsTDyYWz4RVKoypK6AxOoNW1iX5f6CTwGQk=; b=bC3ozcyue3dhXaAJyX6nJHCys3LIltTeBv9v8+F8KDUyWGuyIQxzTyYmYwtWiIAw4o wsgF6fD5HSwx4NybJ6TN4JhoAo9c4mEFJ1BurCgtkZeUQ8eS5KGBmAFQeGgHhGbmWpXR UPQR6r+yT8xDEfCJUzTUorz/0KQKMUl++ltJXOYqiyptZa8aBa34RiCxOsGnGMBE0G47 /MTcs9V/iAASOiEsSWTw8C4+Y2RuSx+eT6CvccgimYFgzF1iEQnpU52uwpObY9UMty6V a0wFUmdHgcPh9xkWiBTa+P3/u+0Dv8rKB2h1s5QIWGebHHfzY4v0GRqn1jzPPEHPEZMG 0NIA== X-Gm-Message-State: AOJu0YyEi69+zlhyGSQRNCFnkY8++toRdeLz1P54TgIaXBNP1FAG9qJG wEL3FZKNBaFwbKcDdw13O2U0UYS9YAD0ETuY1zQTduaMVHLqYFIupq3heJxp0Jg/iTN/Uy7qEIh k4GUZ X-Gm-Gg: AfdE7cmhsXmWlCSk7CXN1iZBGcV6rC8NkZXtZcVBFZBspgJ0Z4NmIQwNvAZhVKOgWNW CXEsRidJg/avr1kb/mMQTsyJK3zkyhF3BBkksiT452kZvwMAq/ZXyBeNQuTlydnFEqdEuLIkpI2 6pUEOOB3PaHWehyS+Cs21nYzLemSBtH3amyhvC6tSaqTyZ48B25h7mn/dyG3fhSjKv3kjr/1PLY G841CL1SmfaVT5ejeLvv4N3W+Q5ThNPmRn8K00g7fNDJjV4K48qdDphVWu9kyq8uSiOIDgyc1wk 6kDBkuII5oWhg71MopHdD8uJt8lLj0Fle5q48S14j4Y54yDM3vON0O5ELnGlYpigIp6KbdYeEt7 wXLoyxkfd+KhfGaVAca3j8hUWPxL3hdMdY0Aq6m8oc8f06+wP/q1dENH7z0itsZcqun52Cs7MIQ V7T/Z/0aI5R3UiTCxbv9oC6b0k X-Received: by 2002:a05:6a20:6a24:b0:3bd:3800:7538 with SMTP id adf61e73a8af0-3c08eec7c4amr2796708637.33.1783372533432; Mon, 06 Jul 2026 14:15:33 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c2:2485:a9ff:45ba:83f5:84f5]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-311747f598dsm517650eec.6.2026.07.06.14.15.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Jul 2026 14:15:32 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: "Andreas K . Huettel" , DJ Delorie Subject: [PATCH 3/8] elf: Avoid redundant ld.so.cache reload after first load Date: Mon, 6 Jul 2026 18:14:39 -0300 Message-ID: <20260706211524.3801466-4-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260706211524.3801466-1-adhemerval.zanella@linaro.org> References: <20260706211524.3801466-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, TXREP shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on 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 _dl_check_ldsocache_needs_loading only stored the stat fields it compares (mtime, ino, size, dev) on the path where a cache was already loaded. On the very first call CACHE is NULL and the function returned "needs loading" without recording those fields, leaving new_cache_file_time zero. The next call then copied that zero value into cache_file_time and compared it against the freshly stat'd values, which always differed, forcing a second, unnecessary load (munmap + mmap + re-parse) of an unchanged cache at every startup. It can be shown with repro: $ cat << EOF > repro.c #include int main (void) { dlopen ("does-not-exist-xyz.so.99", RTLD_NOW); return 0; } EOF $ gcc repro.c -o repro $ strace -f -e trace=openat elf/ld.so --library-path . ./repro 2>&1 | grep -c "/etc/ld.so.cache" The result should be 1, instead of 2. Record the stat fields as soon as the stat succeeds, before the CACHE == NULL early return, so the following call has an accurate baseline and does not spuriously reload. Reviewed-by: DJ Delorie --- elf/dl-cache.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/elf/dl-cache.c b/elf/dl-cache.c index 498196d0aa3..03bcd5bd06e 100644 --- a/elf/dl-cache.c +++ b/elf/dl-cache.c @@ -433,17 +433,20 @@ _dl_check_ldsocache_needs_loading (void) if (rv < 0) return false; - /* Any file is better than no file (likely the first time - through). */ - if (cache == NULL) - return true; - - /* Store the fields we check, in order they're likely to differ. */ + /* Store the fields we check, in order they're likely to differ. We + must do this even for the first load (CACHE == NULL below), so that + the next call copies an accurate NEW_CACHE_FILE_TIME into + CACHE_FILE_TIME and does not spuriously reload the unchanged cache. */ new_cache_file_time.mtime = new_cache_file_stat.st_mtime; new_cache_file_time.ino = new_cache_file_stat.st_ino; new_cache_file_time.size = new_cache_file_stat.st_size; new_cache_file_time.dev = new_cache_file_stat.st_dev; + /* Any file is better than no file (likely the first time + through). */ + if (cache == NULL) + return true; + /* At this point, NEW_CACHE_FILE_TIME is valid as well as CACHE_FILE_TIME, so we compare them. */ return (memcmp (&new_cache_file_time, &cache_file_time,