From nobody Fri Oct 3 22:00:49 2025 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DA46E17CA1B for ; Sat, 23 Aug 2025 00:00:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755907242; cv=none; b=Li4423zPm8q2y3LrigTtJP93Zg4wykxaOxc1+As8yEMJ9UDctnpH+VmuOTCMqrGvNUM/MQ+s9ylG2H9iizVA8Lsk06yV7uCs6wP3ljI2cW6oc+iUZjNFVc3+6SGRBrgGV0KawVJqeFoch+F7qLhp3pWJpTU5JpEWFY1CDFXSmR0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755907242; c=relaxed/simple; bh=Z081sHystRdEW29SSVK9iJw2aQWk5mYfgw3Kk+9n+xE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=tCAz1X7FCLKRYBYvXkVyTnvQN665XaHl9Vkb42HxNGknwczojaZh7Y1bd5YiSDdTjV5vl6FsYivWL5O3CbbnWTLqICwjdAWIBySr6Pd6XtBqs5G4m7v+zv2Itvfnzg6TKYa+Lr126qh4F+lfMh7VYIDUGteUWZt6PQTiDmhL0Wc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=HWvi0TLM; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="HWvi0TLM" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-2445806b18aso34156975ad.1 for ; Fri, 22 Aug 2025 17:00:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1755907240; x=1756512040; darn=vger.kernel.org; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=ULpCq4HuDiQhtLU1R+s5TkMdJZ1jHlpW8rzbARN3QRo=; b=HWvi0TLMuc8W6Y92ErRZcF4LYR/L0z2cE5nJZQQDQbRjEtGqKPwzlvqyDVIi7+mqpv 0N5G/C+AlC1X34IxQzzsXOr5X8CXcMPL5Mhk0tjRJwD+w5kVRyhbIBcDgJTIvRP4XpvR CtwdgvOA/XC2OmvVFk+VrGXiYkseMKfPARrfAfq/J/Lu/zWGP8aPzlpruIU69NkGVRbs sEvAj8CQX6eJ42faQo3ur4du/HGSJXLTcZZAR8O7efkNWEPza8bsrwsSnFXgGcXjDMd4 fx78C4+paOShO7Sd2mkH5KTagBwkrAHj3YWUTnqFHoITxLMrdJzFdOS28WHZLKog/vuO DHCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755907240; x=1756512040; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ULpCq4HuDiQhtLU1R+s5TkMdJZ1jHlpW8rzbARN3QRo=; b=dy9fEOwtpcN3+H0PSpnD0oaoxfDCgJFVS0VfdMwEw70U1xM8wGRVZ/+cQuuDLKNv0h hOj3/FXttABeYe5NzRzaF75c5sW5TpF7b8eZFEBUqH+I6HMNlUH+RZdMjYZKJ9z/gqT9 QTHryiHTypqRgdlFIQ3FjYNPAke4OSMS32fvooobJ3DUi/4sqYI9y3qGQrEU0mOxzpgU G7LUWAY1sm4SXd4ehOgTmlKOJjGqMbeCnkft0Soly74U1O1z/35o5N+mZHycaAOd40zc l1b2AcLsCejPByQMhAnbz71k2juRQ5rhXnwc0mVqeZfioRiEgLcsGQ4qwheHewfI+dSx vimQ== X-Forwarded-Encrypted: i=1; AJvYcCWyZhvMoF9LMHxq7QUv4NJtsTyQtNHNQKineb+zgPnuyYRTQdHAaproi/fqHiI0Pb/maGO/rRJ+l9mWoOY=@vger.kernel.org X-Gm-Message-State: AOJu0YybP/UdoEaDB/LBKp0HxVimrYNgMwRGutRvi7jLorWlHLBEB9Jc CzKd8xAS8gSl6jvZ2wLotnY5cGLnXSw9qfF2xdKeMmXeUrJ/cybursp57f+EE7HFoM/PycEeQLB HuNwYVbtHzA== X-Google-Smtp-Source: AGHT+IEx0bUgQTSlXV+L1eqClL3ijNyH9m/aIoYnt0Qb5dB6eUafXwAr9MngWqDVKVS3G/tpzT9pRJaj3327 X-Received: from plzu5.prod.google.com ([2002:a17:902:82c5:b0:240:770f:72cb]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:2f44:b0:242:fba2:b8e4 with SMTP id d9443c01a7336-2462efaf1demr69588065ad.56.1755907240172; Fri, 22 Aug 2025 17:00:40 -0700 (PDT) Date: Fri, 22 Aug 2025 17:00:23 -0700 In-Reply-To: <20250823000024.724394-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250823000024.724394-1-irogers@google.com> X-Mailer: git-send-email 2.51.0.rc2.233.g662b1ed5c5-goog Message-ID: <20250823000024.724394-2-irogers@google.com> Subject: [PATCH v1 1/2] perf symbol-minimal: Fix ehdr reading in filename__read_build_id From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , James Clark , Collin Funk , "Masami Hiramatsu (Google)" , Stephen Brennan , Palmer Dabbelt , Haibo Xu , Dmitry Vyukov , Song Liu , Thomas Falcon , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The e_ident is part of the ehdr and so reading it a second time would mean the read ehdr was displaced by 16-bytes. Switch from stdio to open/read/lseek syscalls for similarity with the symbol-elf version of the function and so that later changes can alter then open flags. Fixes: fef8f648bb47 ("perf symbol: Fix use-after-free in filename__read_bui= ld_id") Signed-off-by: Ian Rogers --- tools/perf/util/symbol-minimal.c | 55 ++++++++++++++++---------------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/tools/perf/util/symbol-minimal.c b/tools/perf/util/symbol-mini= mal.c index 7201494c5c20..8d41bd7842df 100644 --- a/tools/perf/util/symbol-minimal.c +++ b/tools/perf/util/symbol-minimal.c @@ -4,7 +4,6 @@ =20 #include #include -#include #include #include #include @@ -88,11 +87,8 @@ int filename__read_debuglink(const char *filename __mayb= e_unused, */ int filename__read_build_id(const char *filename, struct build_id *bid) { - FILE *fp; - int ret =3D -1; + int fd, ret =3D -1; bool need_swap =3D false, elf32; - u8 e_ident[EI_NIDENT]; - int i; union { struct { Elf32_Ehdr ehdr32; @@ -103,28 +99,27 @@ int filename__read_build_id(const char *filename, stru= ct build_id *bid) Elf64_Phdr *phdr64; }; } hdrs; - void *phdr; - size_t phdr_size; - void *buf =3D NULL; - size_t buf_size =3D 0; + void *phdr, *buf =3D NULL; + ssize_t phdr_size, ehdr_size, buf_size =3D 0; =20 - fp =3D fopen(filename, "r"); - if (fp =3D=3D NULL) + fd =3D open(filename, O_RDONLY); + if (fd < 0) return -1; =20 - if (fread(e_ident, sizeof(e_ident), 1, fp) !=3D 1) + if (read(fd, hdrs.ehdr32.e_ident, EI_NIDENT) !=3D EI_NIDENT) goto out; =20 - if (memcmp(e_ident, ELFMAG, SELFMAG) || - e_ident[EI_VERSION] !=3D EV_CURRENT) + if (memcmp(hdrs.ehdr32.e_ident, ELFMAG, SELFMAG) || + hdrs.ehdr32.e_ident[EI_VERSION] !=3D EV_CURRENT) goto out; =20 - need_swap =3D check_need_swap(e_ident[EI_DATA]); - elf32 =3D e_ident[EI_CLASS] =3D=3D ELFCLASS32; + need_swap =3D check_need_swap(hdrs.ehdr32.e_ident[EI_DATA]); + elf32 =3D hdrs.ehdr32.e_ident[EI_CLASS] =3D=3D ELFCLASS32; + ehdr_size =3D (elf32 ? sizeof(hdrs.ehdr32) : sizeof(hdrs.ehdr64)) - EI_NI= DENT; =20 - if (fread(elf32 ? (void *)&hdrs.ehdr32 : (void *)&hdrs.ehdr64, - elf32 ? sizeof(hdrs.ehdr32) : sizeof(hdrs.ehdr64), - 1, fp) !=3D 1) + if (read(fd, + (elf32 ? (void *)&hdrs.ehdr32 : (void *)&hdrs.ehdr64) + EI_NIDENT, + ehdr_size) !=3D ehdr_size) goto out; =20 if (need_swap) { @@ -138,14 +133,18 @@ int filename__read_build_id(const char *filename, str= uct build_id *bid) hdrs.ehdr64.e_phnum =3D bswap_16(hdrs.ehdr64.e_phnum); } } - phdr_size =3D elf32 ? hdrs.ehdr32.e_phentsize * hdrs.ehdr32.e_phnum - : hdrs.ehdr64.e_phentsize * hdrs.ehdr64.e_phnum; + if ((elf32 && hdrs.ehdr32.e_phentsize !=3D sizeof(Elf32_Phdr)) || + (!elf32 && hdrs.ehdr64.e_phentsize !=3D sizeof(Elf64_Phdr))) + goto out; + + phdr_size =3D elf32 ? sizeof(Elf32_Phdr) * hdrs.ehdr32.e_phnum + : sizeof(Elf64_Phdr) * hdrs.ehdr64.e_phnum; phdr =3D malloc(phdr_size); if (phdr =3D=3D NULL) goto out; =20 - fseek(fp, elf32 ? hdrs.ehdr32.e_phoff : hdrs.ehdr64.e_phoff, SEEK_SET); - if (fread(phdr, phdr_size, 1, fp) !=3D 1) + lseek(fd, elf32 ? hdrs.ehdr32.e_phoff : hdrs.ehdr64.e_phoff, SEEK_SET); + if (read(fd, phdr, phdr_size) !=3D phdr_size) goto out_free; =20 if (elf32) @@ -153,8 +152,8 @@ int filename__read_build_id(const char *filename, struc= t build_id *bid) else hdrs.phdr64 =3D phdr; =20 - for (i =3D 0; i < elf32 ? hdrs.ehdr32.e_phnum : hdrs.ehdr64.e_phnum; i++)= { - size_t p_filesz; + for (int i =3D 0; i < (elf32 ? hdrs.ehdr32.e_phnum : hdrs.ehdr64.e_phnum)= ; i++) { + ssize_t p_filesz; =20 if (need_swap) { if (elf32) { @@ -180,8 +179,8 @@ int filename__read_build_id(const char *filename, struc= t build_id *bid) goto out_free; buf =3D tmp; } - fseek(fp, elf32 ? hdrs.phdr32[i].p_offset : hdrs.phdr64[i].p_offset, SEE= K_SET); - if (fread(buf, p_filesz, 1, fp) !=3D 1) + lseek(fd, elf32 ? hdrs.phdr32[i].p_offset : hdrs.phdr64[i].p_offset, SEE= K_SET); + if (read(fd, buf, p_filesz) !=3D p_filesz) goto out_free; =20 ret =3D read_build_id(buf, p_filesz, bid, need_swap); @@ -194,7 +193,7 @@ int filename__read_build_id(const char *filename, struc= t build_id *bid) free(buf); free(phdr); out: - fclose(fp); + close(fd); return ret; } =20 --=20 2.51.0.rc2.233.g662b1ed5c5-goog From nobody Fri Oct 3 22:00:49 2025 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2D75C13B7AE for ; Sat, 23 Aug 2025 00:00:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755907245; cv=none; b=DUCItULzsNrOfDXj+oTYtybj7WZgYAANagUPqDqncSpMtOYYJe0lZtI1biVq0Pb3mOUbp6FHY+feIXNWgsN2DR4Moow68dsEEuvZyaIz54ebmIDjo4cPdTlkeb/yRPpCgJpVNX96UIlbluZYd3dJGugU3FbcZrEnF8kllZu7g8U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755907245; c=relaxed/simple; bh=NM3DeuEEtAaAuRHK2RaSfe/aDNVsrnZNl/fWVpPhJkw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Content-Type; b=uQcSabYZwVn34zYAheFwg56xwpujxrWWk0UfrmuX/gGzPd2bNdkPA9fG8JwKVLpUW7/afNXqjfAESnkzoNl8drnoy2nxXzsGdSDk/Pe1cpcszogDfLcHla2G7w0v5x3999Dv+o25OhKPZehZS/ueHVREOCH0ZjICfJid18HZSOg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Is3HS8RH; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Is3HS8RH" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-24458274406so60511165ad.3 for ; Fri, 22 Aug 2025 17:00:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1755907242; x=1756512042; darn=vger.kernel.org; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=ouvN9Z/KSAqQFTzs2r7I/5ZrSF7aA646JZI4/Fn2ePU=; b=Is3HS8RHwHzdcDRxsbuZpkOAdrKTiVUqrRLGv9Fh9pluxO+BHB7JWd/Ds5T7MHwDVB 5wpkjBVlsh26UyQOj9dK0OjWBZXD2e1ckRZj0Roq2Jw8lph9CLQvfsH5EhkpaRQlqB5l g5+aXurOW8VyXq57IxpocS2XGwv+3raApRn1Ql9LS6QfkILPOLsEr1ak1zrO9HFYeQ/U 1dpmSNFshX/KikY/Tv1SqOnhJqCxw4n2PNmvevgQ59YYzglyrWI+cRiB4gWIy+Lpql18 Mt+j7ZNHoaq7ixOTD100kikTILOz62g9LDCMHvdkAlJVscaUo+P8f3MHRmiQwF/vukIG Xxtw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755907242; x=1756512042; h=to:from:subject:message-id:references:mime-version:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ouvN9Z/KSAqQFTzs2r7I/5ZrSF7aA646JZI4/Fn2ePU=; b=tnHSrZ5tU6F+KzyuAoVANTpV7RSh6rdH/1UpqPCJDtHplakeVYb85Hfkp6MSRTbWKu o6icuz5Mi9y6xYBjTi65RbjB51GUyyvtZZ+MpVIDxXIam0xocwEQtNlqaOZbVRC/Qmcd 7ZSN0WVr2Yz83KfwagpRurzpLXwcLuNx/JULG0fAntc8iM+1NUQbTmg18sSQX+HoZQXb SDujsIWGQbgh2dg5LkWoG1FIESignFAuRIKUPKYt/Z4UQ/JXy3N05TUsb2Em0yQWb6ih 4Md17OdSpSbD/Fkn3Hl5RYpGE02RevN1OqzLtIr/HuoCykrXZ7djEyKCUDWS3ONktlh2 keYQ== X-Forwarded-Encrypted: i=1; AJvYcCUylaqlDh086b1frN/5mP6nUf9YNYH/lkTnXtGAiQqNjPOUpTYjE2+JwiKRYgCQ4EvJ3SoExjyXgeds/e4=@vger.kernel.org X-Gm-Message-State: AOJu0YzjA845v7YvJNJIr5p2ox3FLM9Ufnz74EKrwAeFYH/Shkv0wVL3 G3tSbSvjR59OTCpGL2B7cwWRMSr5n3eZpSEM4ifouulyz6HnBFVqswJNebA8NmklHa8WIL9bwqq XMuji0P/leg== X-Google-Smtp-Source: AGHT+IHBE+a/Nwo51dwHhNmlO51ZDKIzz+BjXguGQZ6jdUmxmLzkgl3rDdG548uoUhX4kHE8w87O0lFUxGRq X-Received: from plgv17.prod.google.com ([2002:a17:902:e8d1:b0:23f:f68b:fa0c]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:903:2f44:b0:240:1850:cb18 with SMTP id d9443c01a7336-2462efaee75mr68904135ad.53.1755907242498; Fri, 22 Aug 2025 17:00:42 -0700 (PDT) Date: Fri, 22 Aug 2025 17:00:24 -0700 In-Reply-To: <20250823000024.724394-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250823000024.724394-1-irogers@google.com> X-Mailer: git-send-email 2.51.0.rc2.233.g662b1ed5c5-goog Message-ID: <20250823000024.724394-3-irogers@google.com> Subject: [PATCH v1 2/2] perf symbol: Add blocking argument to filename__read_build_id From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , James Clark , Collin Funk , "Masami Hiramatsu (Google)" , Stephen Brennan , Palmer Dabbelt , Haibo Xu , Dmitry Vyukov , Song Liu , Thomas Falcon , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When synthesizing build-ids, for build ID mmap2 events, they will be added for data mmaps if -d/--data is specified. The files opened for their build IDs may block on the open causing perf to hang during synthesis. There is some robustness in existing calls to filename__read_build_id by checking the file path is to a regular file, which unfortunately fails for symlinks. Rather than adding more is_regular_file calls, switch filename__read_build_id to take a "block" argument and specify O_NONBLOCK when this is false. The existing is_regular_file checking callers and the event synthesis callers are made to pass false and thereby avoiding the hang. Fixes: 53b00ff358dc ("perf record: Make --buildid-mmap the default") Signed-off-by: Ian Rogers --- tools/perf/bench/inject-buildid.c | 2 +- tools/perf/builtin-buildid-cache.c | 8 ++++---- tools/perf/builtin-inject.c | 4 ++-- tools/perf/tests/sdt.c | 2 +- tools/perf/util/build-id.c | 4 ++-- tools/perf/util/debuginfo.c | 8 ++++++-- tools/perf/util/dsos.c | 4 ++-- tools/perf/util/symbol-elf.c | 9 +++++---- tools/perf/util/symbol-minimal.c | 6 +++--- tools/perf/util/symbol.c | 8 ++++---- tools/perf/util/symbol.h | 2 +- tools/perf/util/synthetic-events.c | 2 +- 12 files changed, 32 insertions(+), 27 deletions(-) diff --git a/tools/perf/bench/inject-buildid.c b/tools/perf/bench/inject-bu= ildid.c index aad572a78d7f..12387ea88b9a 100644 --- a/tools/perf/bench/inject-buildid.c +++ b/tools/perf/bench/inject-buildid.c @@ -85,7 +85,7 @@ static int add_dso(const char *fpath, const struct stat *= sb __maybe_unused, if (typeflag =3D=3D FTW_D || typeflag =3D=3D FTW_SL) return 0; =20 - if (filename__read_build_id(fpath, &bid) < 0) + if (filename__read_build_id(fpath, &bid, /*block=3D*/true) < 0) return 0; =20 dso->name =3D realpath(fpath, NULL); diff --git a/tools/perf/builtin-buildid-cache.c b/tools/perf/builtin-buildi= d-cache.c index c98104481c8a..2e0f2004696a 100644 --- a/tools/perf/builtin-buildid-cache.c +++ b/tools/perf/builtin-buildid-cache.c @@ -180,7 +180,7 @@ static int build_id_cache__add_file(const char *filenam= e, struct nsinfo *nsi) struct nscookie nsc; =20 nsinfo__mountns_enter(nsi, &nsc); - err =3D filename__read_build_id(filename, &bid); + err =3D filename__read_build_id(filename, &bid, /*block=3D*/true); nsinfo__mountns_exit(&nsc); if (err < 0) { pr_debug("Couldn't read a build-id in %s\n", filename); @@ -204,7 +204,7 @@ static int build_id_cache__remove_file(const char *file= name, struct nsinfo *nsi) int err; =20 nsinfo__mountns_enter(nsi, &nsc); - err =3D filename__read_build_id(filename, &bid); + err =3D filename__read_build_id(filename, &bid, /*block=3D*/true); nsinfo__mountns_exit(&nsc); if (err < 0) { pr_debug("Couldn't read a build-id in %s\n", filename); @@ -280,7 +280,7 @@ static bool dso__missing_buildid_cache(struct dso *dso,= int parm __maybe_unused) if (!dso__build_id_filename(dso, filename, sizeof(filename), false)) return true; =20 - if (filename__read_build_id(filename, &bid) =3D=3D -1) { + if (filename__read_build_id(filename, &bid, /*block=3D*/true) =3D=3D -1) { if (errno =3D=3D ENOENT) return false; =20 @@ -309,7 +309,7 @@ static int build_id_cache__update_file(const char *file= name, struct nsinfo *nsi) int err; =20 nsinfo__mountns_enter(nsi, &nsc); - err =3D filename__read_build_id(filename, &bid); + err =3D filename__read_build_id(filename, &bid, /*block=3D*/true); nsinfo__mountns_exit(&nsc); if (err < 0) { pr_debug("Couldn't read a build-id in %s\n", filename); diff --git a/tools/perf/builtin-inject.c b/tools/perf/builtin-inject.c index 40ba6a94f719..a114b3fa1bea 100644 --- a/tools/perf/builtin-inject.c +++ b/tools/perf/builtin-inject.c @@ -680,12 +680,12 @@ static int dso__read_build_id(struct dso *dso) =20 mutex_lock(dso__lock(dso)); nsinfo__mountns_enter(dso__nsinfo(dso), &nsc); - if (filename__read_build_id(dso__long_name(dso), &bid) > 0) + if (filename__read_build_id(dso__long_name(dso), &bid, /*block=3D*/true) = > 0) dso__set_build_id(dso, &bid); else if (dso__nsinfo(dso)) { char *new_name =3D dso__filename_with_chroot(dso, dso__long_name(dso)); =20 - if (new_name && filename__read_build_id(new_name, &bid) > 0) + if (new_name && filename__read_build_id(new_name, &bid, /*block=3D*/true= ) > 0) dso__set_build_id(dso, &bid); free(new_name); } diff --git a/tools/perf/tests/sdt.c b/tools/perf/tests/sdt.c index 93baee2eae42..6132f1af3e22 100644 --- a/tools/perf/tests/sdt.c +++ b/tools/perf/tests/sdt.c @@ -31,7 +31,7 @@ static int build_id_cache__add_file(const char *filename) struct build_id bid =3D { .size =3D 0, }; int err; =20 - err =3D filename__read_build_id(filename, &bid); + err =3D filename__read_build_id(filename, &bid, /*block=3D*/true); if (err < 0) { pr_debug("Failed to read build id of %s\n", filename); return err; diff --git a/tools/perf/util/build-id.c b/tools/perf/util/build-id.c index a7018a3b0437..bf7f3268b9a2 100644 --- a/tools/perf/util/build-id.c +++ b/tools/perf/util/build-id.c @@ -115,7 +115,7 @@ int filename__snprintf_build_id(const char *pathname, c= har *sbuild_id, size_t sb struct build_id bid =3D { .size =3D 0, }; int ret; =20 - ret =3D filename__read_build_id(pathname, &bid); + ret =3D filename__read_build_id(pathname, &bid, /*block=3D*/true); if (ret < 0) return ret; =20 @@ -841,7 +841,7 @@ static int filename__read_build_id_ns(const char *filen= ame, int ret; =20 nsinfo__mountns_enter(nsi, &nsc); - ret =3D filename__read_build_id(filename, bid); + ret =3D filename__read_build_id(filename, bid, /*block=3D*/true); nsinfo__mountns_exit(&nsc); =20 return ret; diff --git a/tools/perf/util/debuginfo.c b/tools/perf/util/debuginfo.c index a44c70f93156..bb9ebd84ec2d 100644 --- a/tools/perf/util/debuginfo.c +++ b/tools/perf/util/debuginfo.c @@ -110,8 +110,12 @@ struct debuginfo *debuginfo__new(const char *path) if (!dso) goto out; =20 - /* Set the build id for DSO_BINARY_TYPE__BUILDID_DEBUGINFO */ - if (is_regular_file(path) && filename__read_build_id(path, &bid) > 0) + /* + * Set the build id for DSO_BINARY_TYPE__BUILDID_DEBUGINFO. Don't block + * incase the path isn't for a regular file. + */ + assert(!dso__has_build_id(dso)); + if (filename__read_build_id(path, &bid, /*block=3D*/false) > 0) dso__set_build_id(dso, &bid); =20 for (type =3D distro_dwarf_types; diff --git a/tools/perf/util/dsos.c b/tools/perf/util/dsos.c index 0a7645c7fae7..64c1d65b0149 100644 --- a/tools/perf/util/dsos.c +++ b/tools/perf/util/dsos.c @@ -81,13 +81,13 @@ static int dsos__read_build_ids_cb(struct dso *dso, voi= d *data) return 0; } nsinfo__mountns_enter(dso__nsinfo(dso), &nsc); - if (filename__read_build_id(dso__long_name(dso), &bid) > 0) { + if (filename__read_build_id(dso__long_name(dso), &bid, /*block=3D*/true) = > 0) { dso__set_build_id(dso, &bid); args->have_build_id =3D true; } else if (errno =3D=3D ENOENT && dso__nsinfo(dso)) { char *new_name =3D dso__filename_with_chroot(dso, dso__long_name(dso)); =20 - if (new_name && filename__read_build_id(new_name, &bid) > 0) { + if (new_name && filename__read_build_id(new_name, &bid, /*block=3D*/true= ) > 0) { dso__set_build_id(dso, &bid); args->have_build_id =3D true; } diff --git a/tools/perf/util/symbol-elf.c b/tools/perf/util/symbol-elf.c index 6d2c280a1730..033c79231a54 100644 --- a/tools/perf/util/symbol-elf.c +++ b/tools/perf/util/symbol-elf.c @@ -902,7 +902,7 @@ static int read_build_id(const char *filename, struct b= uild_id *bid) =20 #else // HAVE_LIBBFD_BUILDID_SUPPORT =20 -static int read_build_id(const char *filename, struct build_id *bid) +static int read_build_id(const char *filename, struct build_id *bid, bool = block) { size_t size =3D sizeof(bid->data); int fd, err =3D -1; @@ -911,7 +911,7 @@ static int read_build_id(const char *filename, struct b= uild_id *bid) if (size < BUILD_ID_SIZE) goto out; =20 - fd =3D open(filename, O_RDONLY); + fd =3D open(filename, block ? O_RDONLY : (O_RDONLY | O_NONBLOCK)); if (fd < 0) goto out; =20 @@ -934,7 +934,7 @@ static int read_build_id(const char *filename, struct b= uild_id *bid) =20 #endif // HAVE_LIBBFD_BUILDID_SUPPORT =20 -int filename__read_build_id(const char *filename, struct build_id *bid) +int filename__read_build_id(const char *filename, struct build_id *bid, bo= ol block) { struct kmod_path m =3D { .name =3D NULL, }; char path[PATH_MAX]; @@ -958,9 +958,10 @@ int filename__read_build_id(const char *filename, stru= ct build_id *bid) } close(fd); filename =3D path; + block =3D true; } =20 - err =3D read_build_id(filename, bid); + err =3D read_build_id(filename, bid, block); =20 if (m.comp) unlink(filename); diff --git a/tools/perf/util/symbol-minimal.c b/tools/perf/util/symbol-mini= mal.c index 8d41bd7842df..41e4ebe5eac5 100644 --- a/tools/perf/util/symbol-minimal.c +++ b/tools/perf/util/symbol-minimal.c @@ -85,7 +85,7 @@ int filename__read_debuglink(const char *filename __maybe= _unused, /* * Just try PT_NOTE header otherwise fails */ -int filename__read_build_id(const char *filename, struct build_id *bid) +int filename__read_build_id(const char *filename, struct build_id *bid, bo= ol block) { int fd, ret =3D -1; bool need_swap =3D false, elf32; @@ -102,7 +102,7 @@ int filename__read_build_id(const char *filename, struc= t build_id *bid) void *phdr, *buf =3D NULL; ssize_t phdr_size, ehdr_size, buf_size =3D 0; =20 - fd =3D open(filename, O_RDONLY); + fd =3D open(filename, block ? O_RDONLY : (O_RDONLY | O_NONBLOCK)); if (fd < 0) return -1; =20 @@ -323,7 +323,7 @@ int dso__load_sym(struct dso *dso, struct map *map __ma= ybe_unused, if (ret >=3D 0) RC_CHK_ACCESS(dso)->is_64_bit =3D ret; =20 - if (filename__read_build_id(ss->name, &bid) > 0) + if (filename__read_build_id(ss->name, &bid, /*block=3D*/true) > 0) dso__set_build_id(dso, &bid); return 0; } diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index e816e4220d33..3fed54de5401 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c @@ -1869,14 +1869,14 @@ int dso__load(struct dso *dso, struct map *map) =20 /* * Read the build id if possible. This is required for - * DSO_BINARY_TYPE__BUILDID_DEBUGINFO to work + * DSO_BINARY_TYPE__BUILDID_DEBUGINFO to work. Don't block in case path + * isn't for a regular file. */ - if (!dso__has_build_id(dso) && - is_regular_file(dso__long_name(dso))) { + if (!dso__has_build_id(dso)) { struct build_id bid =3D { .size =3D 0, }; =20 __symbol__join_symfs(name, PATH_MAX, dso__long_name(dso)); - if (filename__read_build_id(name, &bid) > 0) + if (filename__read_build_id(name, &bid, /*block=3D*/false) > 0) dso__set_build_id(dso, &bid); } =20 diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h index 3fb5d146d9b1..347106218799 100644 --- a/tools/perf/util/symbol.h +++ b/tools/perf/util/symbol.h @@ -140,7 +140,7 @@ struct symbol *dso__next_symbol(struct symbol *sym); =20 enum dso_type dso__type_fd(int fd); =20 -int filename__read_build_id(const char *filename, struct build_id *id); +int filename__read_build_id(const char *filename, struct build_id *id, boo= l block); int sysfs__read_build_id(const char *filename, struct build_id *bid); int modules__parse(const char *filename, void *arg, int (*process_module)(void *arg, const char *name, diff --git a/tools/perf/util/synthetic-events.c b/tools/perf/util/synthetic= -events.c index cb2c1ace304a..fcd1fd13c30e 100644 --- a/tools/perf/util/synthetic-events.c +++ b/tools/perf/util/synthetic-events.c @@ -401,7 +401,7 @@ static void perf_record_mmap2__read_build_id(struct per= f_record_mmap2 *event, nsi =3D nsinfo__new(event->pid); nsinfo__mountns_enter(nsi, &nc); =20 - rc =3D filename__read_build_id(event->filename, &bid) > 0 ? 0 : -1; + rc =3D filename__read_build_id(event->filename, &bid, /*block=3D*/false) = > 0 ? 0 : -1; =20 nsinfo__mountns_exit(&nc); nsinfo__put(nsi); --=20 2.51.0.rc2.233.g662b1ed5c5-goog