From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.69 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1792700156; b=qguDX+BtEe93UedxgAsWc8pzg8WLCKt7wmK0NBkSOfB7CCGdHW6wfGDU4oRzOAvFNOXu f7SrgKpoNah4uZ1omAefwKov21EEqgBwSEolRyQF/eHBH5M76b0T5OmK31SZZB5Vm5o5s zYoC839HcdCYqY3KR8iAOfN5SpCs5yTZghIXefTLBSMKhklylp3fgsgitE+oV0wrzFngy VWaUGth0zlIJwo4FiNMO1AxnhXK4dfSk3OP4S7kpDWaZ+MpjrUC8QVdKwRBjC004LptAI +uVEPnoGplM7YNzgQxyWvkqGCuJwRUnbQ2BX+Vy2uG77jmBJcWAA1PlBfdSzTF0jVRQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1792700156; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=m93d6ML738qvEiSSbh/02KAktLjyqMoz7nlZaRp5/fY=; b=5fE17MSGPfzLfMH04k/ox4FHe1zURO85j5/NVUZZxFFjw1mmtwJB7C4egFUGJulvQ2EL RY5pPI//xJjNKzUSviRzkMe2GH/cv+Q6njCpsmdhITs10O7SI0agtuClNbEfYxMlEG7vS 8cX82iLUqU9HBCFKR1a4xOo7QIdM0OXBKLK+gxxWnvAocZEs8eQRDih3QE+L8xDjhTsai 1dJCeBXSzNjOgN1HulbZitsToveaSESEv2ZJa53nsNkVybvY29hlZLMvF1LFNWZRsydKR KSop1ije/fj3cTRq4Y7qM8/RoY/lUCZAfLU+Kr8knjd5cWOwSf89Oi2OP8fFh9r3XrQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20251104 header.b=rbmiBBUJ; arc=none smtp.remote-ip=74.125.82.69 Received: from mail-dl1-f69.google.com (mail-dl1-f69.google.com [74.125.82.69]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 25FE860A5BE2 for ; Thu, 22 Oct 2026 20:15:56 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 25FE860A5BE2 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, unprotected) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20251104 header.b=rbmiBBUJ Received: by mail-dl1-f69.google.com with SMTP id a92af1059eb24-151ba226e1bso312970c88.0 for ; Thu, 22 Oct 2026 13:15:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1792700155; x=1793304955; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=m93d6ML738qvEiSSbh/02KAktLjyqMoz7nlZaRp5/fY=; b=rbmiBBUJR3SL05JWWWJ162WeHaaOeyyTmao+2aSIDjlRjFJwHiyxM0THNMbFLcFm86 V40kiWmGI7QT1D31uZwltdXpFSTbzez84I1HxoHd+v3CBFaxoY0xexynotDkxIvzyan1 be2rnr9Uz53YEqJlWP5JSLoCtUrRbLxbblQY1haGjP8bzIYGHxBWzic1PkiMCRkaIeOP ypeBpMhspuocK+/OmH7AHHyzqtyGSouXsCBK+NX3MNtSlnD5cuc6UDpnKBNiK7o4GcjM Of2cDYJfMeQ73PFG3ztw678WsYNg1TQe+lLTuhTIG8NeSnDQEW+EOFOwI8vA17e7p32z p8/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1792700155; x=1793304955; h=content-type:cc: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:content-type; bh=m93d6ML738qvEiSSbh/02KAktLjyqMoz7nlZaRp5/fY=; b=JIlZJu+mhhlw5VzCthLj8lRalJ4WP9BDpxRTqQy/8ooFhPh0ZpNufjUB5rV8iClqhd j8E1WhXB/uBHLFKEEP/F5icjgZTXRDPm/R/ZdkS2zQ1q9ZGg+CQNbzequw51x++ob8mz FXHE1WQ+tVOzOlMkeAcBhCWPLsRkO6nwy9167PXIL/HM6WcbasLay2cSp5wtLamQkhmY NphsnEBRHAw4insuhFrnoNk0V7FeZEdnD+OB8NdCzjpSFR92uan+6CR5MOgR8Alpx5A9 tVPJhOdvPzM1Fk3FKtWkDFYwTbZe5V9oE+OGHSJBXeebZEArQCW1Cq3PcTzt8m1jV1oA C+MA== X-Forwarded-Encrypted: i=1; AKwUvBys8zNj/VNyHgDFL/CUmHV1xQdTlEjxJZi0AA9f9yuAOAoWTSHB8cY3cuq97xxROSusBz2THvBk7fGIoek=@vger.kernel.org X-Gm-Message-State: AFq9FYIYtp8d2SqZAikpciJUqqCdjx4CU/g3u3D+YL/Sc15GTUciG+Mw NwrY2Mu/ebRxdj4qlZmzhGqykciTtky6Hr4VenvRnWvp1/XDplDSNuP9RApWPjSXY9BNgn4MACH 0/e5JLNtFvg== X-Received: from dleb10-n2.prod.google.com ([2002:a05:701b:424a:20b0:18a:a95d:606c]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:701b:4186:10b0:194:23e7:b526 with SMTP id a92af1059eb24-195c77a8247mr373925c88.27.1792700155131; Thu, 22 Oct 2026 13:15:55 -0700 (PDT) Date: Thu, 22 Oct 2026 13:14:51 -0700 In-Reply-To: <20261022201453.1717523-1-irogers@google.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261021223905.715051-1-irogers@google.com> <20261022201453.1717523-1-irogers@google.com> X-Mailer: git-send-email 2.56.0.307.g5fe7bd680c-goog Message-ID: <20261022201453.1717523-19-irogers@google.com> Subject: [PATCH v8 18/19] perf maps: Fix lock inversion in maps__load_first() and bounds check in __maps__remove() From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org Cc: adrian.hunter@intel.com, elver@google.com, james.clark@linaro.org, jolsa@kernel.org, justinstitt@google.com, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, ljs@kernel.org, llvm@lists.linux.dev, mingo@redhat.com, morbo@google.com, nathan@kernel.org, ndesaulniers@google.com, peterz@infradead.org Content-Type: text/plain; charset="UTF-8" In maps__load_first(), take a reference to the first map and drop maps__lock(maps) before calling map__load() to avoid a read-to-write self-deadlock when loading kernel maps inserts extra trampoline maps. In __maps__remove(), check that the index returned by maps__by_address_index() and maps__by_name_index() is within nr_maps before indexing and removing the entry, and remove from maps_by_name before calling map__put(maps_by_address[address_idx]) so that callers such as maps__remove_maps() that pass maps_by_address[i] as map do not use a freed handle in maps__by_name_index() under REFCNT_CHECKING. Signed-off-by: Ian Rogers --- tools/perf/util/maps.c | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/tools/perf/util/maps.c b/tools/perf/util/maps.c index f808df2fe77b..e947506c60a5 100644 --- a/tools/perf/util/maps.c +++ b/tools/perf/util/maps.c @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-2.0 #include #include +#include #include #include "debug.h" #include "dso.h" @@ -548,20 +549,27 @@ static void __maps__remove(struct maps *maps, struct map *map) /* Slide later mappings over the one to remove */ address_idx = maps__by_address_index(maps, map); - map__put(maps_by_address[address_idx]); - memmove(&maps_by_address[address_idx], - &maps_by_address[address_idx + 1], - (nr_maps - address_idx - 1) * sizeof(*maps_by_address)); + if (address_idx >= nr_maps) + return; if (maps_by_name) { unsigned int name_idx = maps__by_name_index(maps, map); - map__put(maps_by_name[name_idx]); - memmove(&maps_by_name[name_idx], - &maps_by_name[name_idx + 1], - (nr_maps - name_idx - 1) * sizeof(*maps_by_name)); + if (name_idx < nr_maps) { + map__put(maps_by_name[name_idx]); + memmove(&maps_by_name[name_idx], + &maps_by_name[name_idx + 1], + (nr_maps - name_idx - 1) * sizeof(*maps_by_name)); + } else { + __maps__free_maps_by_name(maps); + } } + map__put(maps_by_address[address_idx]); + memmove(&maps_by_address[address_idx], + &maps_by_address[address_idx + 1], + (nr_maps - address_idx - 1) * sizeof(*maps_by_address)); + --RC_CHK_ACCESS(maps)->nr_maps; } @@ -1459,10 +1467,15 @@ int maps__merge_in(struct maps *kmaps, struct map *new_map) void maps__load_first(struct maps *maps) { - down_read(maps__lock(maps)); + struct map *first = NULL; + down_read(maps__lock(maps)); if (maps__nr_maps(maps) > 0) - map__load(maps__maps_by_address(maps)[0]); - + first = map__get(maps__maps_by_address(maps)[0]); up_read(maps__lock(maps)); + + if (first) { + map__load(first); + map__put(first); + } } -- 2.56.0.307.g5fe7bd680c-goog