From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pl1-x634.google.com (mail-pl1-x634.google.com [IPv6:2607:f8b0:4864:20::634]) by sourceware.org (Postfix) with ESMTPS id 934433858412 for ; Thu, 14 Mar 2024 13:25:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 934433858412 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 934433858412 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::634 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710422758; cv=none; b=icdaoDmI4wi5sOneZW+YE9F65FiMqW2jYGezZqDCsCGKMKKR2TUHswdOJ/eFxb69mamiP2Xt3ea4X9BSslQGSZ8WQz1qAo2B086uh0hGXECpU84mIuQdaF6K5hsMMi9e0oZj40O1e2g/IHT0trTUVWIpTib6zqRkO4soU9Mucv4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710422758; c=relaxed/simple; bh=p++lijyTVRpmIz1EoK/wg74+qQ33wf71UB0m1mmlsdw=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=B+CC0ZTbBxEuvl7cWyvzNE8PB1TS8XVKQBUhcuXSfeAsLyz6jU/GPGxj+cnl3ZlQGmvhsMCb/zoYC6C/4AVFvgHWeOgwnSPcWGJMPCvnIPEdVkEDTxVW0cqk+k30/aubUuLjjwaE6GHT6EGCw2QfwmOTAxgOKYoqRY23MQhGW+U= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x634.google.com with SMTP id d9443c01a7336-1dc5d0162bcso7035765ad.0 for ; Thu, 14 Mar 2024 06:25:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710422754; x=1711027554; 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=i4wlbv93mptFz2hxUef44j6zSQnFXniN9CRIdvT9tOw=; b=BCm9sMGSWzFZgSGYQqqJ+YrOZJ1n9/ZG5Nhi/tKATy2kk5bnAkrsXbEV8unw/2B4MI lSK09HkDj2heJmeYuuLtnbfMQiGgMGZX2mTZ4niWKp5clr9+6m5bnCx1j/0Gj0tTD5EJ 8UO5d3OL9w0nHkAamBDt3hxUp2QWITzTbPmDoo53B0DN8qMvUij9xyarQIv0QJViuhnR 1SBgHV4fNUePsQ1yG6wM9X85VJ5igalwh4GjUrpEKk2IQFr1XAQaI50sDocmj0XOA+7/ KDK1ENjysj1zgjkWddTRh72eDHsdFzcKLY5mlNI0kzPKIdgH4toIOnlGBFeOGM6DM7GD s8Xw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710422754; x=1711027554; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=i4wlbv93mptFz2hxUef44j6zSQnFXniN9CRIdvT9tOw=; b=lYs+jdb4dDZ+/cyEOEIbd2rDTvT2hlOTTIM8oFuPdtCtWCIKMC6uFSFbSDgL+aqtpJ 67rfJVUbKa250K+K/Zue8aKNp9O7XuGuiZh11WXMZByXchcblwyWVxT4/yxOcK09Phsm JfQdAmnHjptCUQEtETV6nxwkFxo5K4PodD+JHQT9+b20sBJoZby2yxwD/JqxYqZ5y0sy x+QCSP+KzTYoOL2SR0nIfwAMQjo1nryeGOHNnvY+QOWtyrjYOHwLrOj4Id37TqJcKO5i 5n7Tbj+Nn2xu8zTNfVZvCj54OITx2NjtYKFF/lHYu4tKj2u/RVfOpy61+Hc1UosD2RCx 26Ew== X-Gm-Message-State: AOJu0Yy1YsG0wPlJdcHnSPSw4v7//B5rT1GDneHnPnxia+5FhgOi1Raf Z/v/cTPSqJ+W+gpKvrJa5EeG94LumJQN6Ex1xL5OMTdKEiW1KEmUICcs5s5L X-Google-Smtp-Source: AGHT+IERjZK8VDTgWSL2k+s5L6hWfdFUPrypifzEGYLqwz1TgjfeMXua7xJkBigxTsmCLhtyYohQDA== X-Received: by 2002:a17:902:a385:b0:1dd:8ee9:62c0 with SMTP id x5-20020a170902a38500b001dd8ee962c0mr1671687pla.34.1710422754446; Thu, 14 Mar 2024 06:25:54 -0700 (PDT) Received: from gnu-cfl-3.localdomain ([172.58.89.72]) by smtp.gmail.com with ESMTPSA id n5-20020a170903110500b001dd54c4320esm1643536plh.256.2024.03.14.06.25.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Mar 2024 06:25:53 -0700 (PDT) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id D5BD774071A; Thu, 14 Mar 2024 06:25:52 -0700 (PDT) From: "H.J. Lu" To: binutils@sourceware.org Cc: goldstein.w.n@gmail.com, sam@gentoo.org, amodra@gmail.com Subject: [PATCH v10 4/6] elf: Don't cache symbol nor relocation tables with mmap Date: Thu, 14 Mar 2024 06:25:50 -0700 Message-ID: <20240314132552.83916-5-hjl.tools@gmail.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240314132552.83916-1-hjl.tools@gmail.com> References: <20240314132552.83916-1-hjl.tools@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3019.4 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,RCVD_IN_ABUSEAT,RCVD_IN_DNSWL_NONE,RCVD_IN_SBL_CSS,SPF_HELO_NONE,SPF_PASS,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 List-Id: During a "-j 8" LLVM 17 debug build on a machine with 32GB RAM and 16GB swap, ld was killed by kernel because of out of memory: [79437.949336] oom-kill:constraint=CONSTRAINT_NONE,nodemask=(null),cpuset=/,mems_allowed=0,global_oom,task_memcg=/user.slice/user-1000.slice/session-9.scope,task=ld,pid=797431,uid=1000 [79437.949349] Out of memory: Killed process 797431 (ld) total-vm:9219600kB, anon-rss:6558156kB, file-rss:1792kB, shmem-rss:0kB, UID:1000 pgtables:17552kB oom_score_adj:0 Don't cache symbol nor relocation tables if they are mapped in. Data to link the 3.5GB clang executable in LLVM 17 debug build on Linux/x86-64 with 32GB RAM is: stdio mmap improvement user 86.73 87.02 -0.3% system 9.55 9.21 3.6% total 100.40 97.66 0.7% maximum set(GB) 17.34 13.14 24% page faults 4047667 3042877 25% and data to link the 275M cc1plus executable in GCC 14 stage 1 build is: user 5.41 5.44 -0.5% system 0.80 0.76 5% total 6.25 6.26 -0.2% maximum set(MB) 1323 968 27% page faults 323451 236371 27% These improve the overall system performance for parallel build by reducing memory usage and page faults. Also rename _bfd_link_keep_memory to _bfd_elf_link_keep_memory. Since the --no-keep-memory linker option causes: https://sourceware.org/bugzilla/show_bug.cgi?id=31458 this is opt-in by each backend. bfd/ * elf-bfd.h (_bfd_elf_link_keep_memory): New. * elf32-i386.c (elf_i386_scan_relocs): Replace _bfd_link_keep_memory with _bfd_elf_link_keep_memory. * elf64-x86-64.c (elf_x86_64_scan_relocs): Likewise. * elflink.c (_bfd_elf_link_iterate_on_relocs): Likewise. (elf_link_add_object_symbols): Likewise. (init_reloc_cookie): Likewise. (init_reloc_cookie_rels): Likewise. (_bfd_elf_link_keep_memory): New. * libbfd-in.h (_bfd_link_keep_memory): Removed. * linker.c (_bfd_link_keep_memory): Likewise. * libbfd.h: Regenerated. --- bfd/elf-bfd.h | 3 ++ bfd/elf32-i386.c | 2 +- bfd/elf64-x86-64.c | 2 +- bfd/elflink.c | 69 ++++++++++++++++++++++++++++++++++++++-------- bfd/libbfd-in.h | 3 -- bfd/libbfd.h | 3 -- bfd/linker.c | 35 ----------------------- 7 files changed, 62 insertions(+), 55 deletions(-) diff --git a/bfd/elf-bfd.h b/bfd/elf-bfd.h index e7c2cd19bed..b9ec590cd4e 100644 --- a/bfd/elf-bfd.h +++ b/bfd/elf-bfd.h @@ -3144,6 +3144,9 @@ extern bool _bfd_elf_mmap_section_contents extern void _bfd_elf_munmap_section_contents (asection *, void *); +extern bool _bfd_elf_link_keep_memory + (struct bfd_link_info *); + /* Large common section. */ extern asection _bfd_elf_large_com_section; diff --git a/bfd/elf32-i386.c b/bfd/elf32-i386.c index 0f963c373be..d1bd11094de 100644 --- a/bfd/elf32-i386.c +++ b/bfd/elf32-i386.c @@ -1932,7 +1932,7 @@ elf_i386_scan_relocs (bfd *abfd, if (elf_section_data (sec)->this_hdr.contents != contents) { - if (!converted && !_bfd_link_keep_memory (info)) + if (!converted && !_bfd_elf_link_keep_memory (info)) _bfd_elf_munmap_section_contents (sec, contents); else { diff --git a/bfd/elf64-x86-64.c b/bfd/elf64-x86-64.c index 2e3f86840e5..5e1fad069d1 100644 --- a/bfd/elf64-x86-64.c +++ b/bfd/elf64-x86-64.c @@ -2590,7 +2590,7 @@ elf_x86_64_scan_relocs (bfd *abfd, struct bfd_link_info *info, if (elf_section_data (sec)->this_hdr.contents != contents) { - if (!converted && !_bfd_link_keep_memory (info)) + if (!converted && !_bfd_elf_link_keep_memory (info)) _bfd_elf_munmap_section_contents (sec, contents); else { diff --git a/bfd/elflink.c b/bfd/elflink.c index a7abda161cb..99920fcf776 100644 --- a/bfd/elflink.c +++ b/bfd/elflink.c @@ -4205,10 +4205,9 @@ _bfd_elf_link_iterate_on_relocs || bfd_is_abs_section (o->output_section)) continue; - internal_relocs = _bfd_elf_link_info_read_relocs (abfd, info, - o, NULL, - NULL, - _bfd_link_keep_memory (info)); + internal_relocs = _bfd_elf_link_info_read_relocs + (abfd, info, o, NULL, NULL, + _bfd_elf_link_keep_memory (info)); if (internal_relocs == NULL) return false; @@ -5574,10 +5573,9 @@ elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info) && (s->flags & SEC_DEBUGGING) != 0)) continue; - internal_relocs = _bfd_elf_link_info_read_relocs (abfd, info, - s, NULL, - NULL, - _bfd_link_keep_memory (info)); + internal_relocs = _bfd_elf_link_info_read_relocs + (abfd, info, s, NULL, NULL, + _bfd_elf_link_keep_memory (info)); if (internal_relocs == NULL) goto error_free_vers; @@ -13638,7 +13636,7 @@ init_reloc_cookie (struct elf_reloc_cookie *cookie, info->callbacks->einfo (_("%P%X: can not read symbols: %E\n")); return false; } - if (_bfd_link_keep_memory (info) ) + if (_bfd_elf_link_keep_memory (info) ) { symtab_hdr->contents = (bfd_byte *) cookie->locsyms; info->cache_size += (cookie->locsymcount @@ -13675,9 +13673,9 @@ init_reloc_cookie_rels (struct elf_reloc_cookie *cookie, } else { - cookie->rels = _bfd_elf_link_info_read_relocs (abfd, info, sec, - NULL, NULL, - _bfd_link_keep_memory (info)); + cookie->rels = _bfd_elf_link_info_read_relocs + (abfd, info, sec, NULL, NULL, + _bfd_elf_link_keep_memory (info)); if (cookie->rels == NULL) return false; cookie->rel = cookie->rels; @@ -15607,3 +15605,50 @@ _bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info, return true; } + +/* Return false if linker should avoid caching relocation information + and symbol tables of input files in memory. */ + +bool +_bfd_elf_link_keep_memory (struct bfd_link_info *info) +{ +#ifdef USE_MMAP + /* Don't cache symbol nor relocation tables if they are mapped in. + NB: Since the --no-keep-memory linker option causes: + + https://sourceware.org/bugzilla/show_bug.cgi?id=31458 + + this is opt-in by each backend. */ + const struct elf_backend_data *bed + = get_elf_backend_data (info->output_bfd); + if (bed->use_mmap) + return false; +#endif + bfd *abfd; + bfd_size_type size; + + if (!info->keep_memory) + return false; + + if (info->max_cache_size == (bfd_size_type) -1) + return true; + + abfd = info->input_bfds; + size = info->cache_size; + do + { + if (size >= info->max_cache_size) + { + /* Over the limit. Reduce the memory usage. */ + info->keep_memory = false; + return false; + } + if (!abfd) + break; + size += abfd->alloc_size; + abfd = abfd->link.next; + } + while (1); + + return true; +} diff --git a/bfd/libbfd-in.h b/bfd/libbfd-in.h index 889b221a950..7bfc58f12d8 100644 --- a/bfd/libbfd-in.h +++ b/bfd/libbfd-in.h @@ -848,9 +848,6 @@ extern bfd_byte * _bfd_write_unsigned_leb128 extern struct bfd_link_info *_bfd_get_link_info (bfd *) ATTRIBUTE_HIDDEN; -extern bool _bfd_link_keep_memory (struct bfd_link_info *) - ATTRIBUTE_HIDDEN; - extern uintptr_t _bfd_pagesize ATTRIBUTE_HIDDEN; extern uintptr_t _bfd_pagesize_m1 ATTRIBUTE_HIDDEN; extern uintptr_t _bfd_minimum_mmap_size ATTRIBUTE_HIDDEN; diff --git a/bfd/libbfd.h b/bfd/libbfd.h index 876c1f223c7..36fe6c0ebd0 100644 --- a/bfd/libbfd.h +++ b/bfd/libbfd.h @@ -854,9 +854,6 @@ extern bfd_byte * _bfd_write_unsigned_leb128 extern struct bfd_link_info *_bfd_get_link_info (bfd *) ATTRIBUTE_HIDDEN; -extern bool _bfd_link_keep_memory (struct bfd_link_info *) - ATTRIBUTE_HIDDEN; - extern uintptr_t _bfd_pagesize ATTRIBUTE_HIDDEN; extern uintptr_t _bfd_pagesize_m1 ATTRIBUTE_HIDDEN; extern uintptr_t _bfd_minimum_mmap_size ATTRIBUTE_HIDDEN; diff --git a/bfd/linker.c b/bfd/linker.c index 36cca9624c2..eb42a78b622 100644 --- a/bfd/linker.c +++ b/bfd/linker.c @@ -3556,38 +3556,3 @@ _bfd_nolink_bfd_define_start_stop (struct bfd_link_info *info ATTRIBUTE_UNUSED, { return (struct bfd_link_hash_entry *) _bfd_ptr_bfd_null_error (sec->owner); } - -/* Return false if linker should avoid caching relocation infomation - and symbol tables of input files in memory. */ - -bool -_bfd_link_keep_memory (struct bfd_link_info * info) -{ - bfd *abfd; - bfd_size_type size; - - if (!info->keep_memory) - return false; - - if (info->max_cache_size == (bfd_size_type) -1) - return true; - - abfd = info->input_bfds; - size = info->cache_size; - do - { - if (size >= info->max_cache_size) - { - /* Over the limit. Reduce the memory usage. */ - info->keep_memory = false; - return false; - } - if (!abfd) - break; - size += abfd->alloc_size; - abfd = abfd->link.next; - } - while (1); - - return true; -} -- 2.44.0