From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.215.198 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1794348549; b=DyHZSxnT/r6g3AnTAxi6fsPXUyprTdmYnKDFMH+iYykR96KiSlD+CKpNJDo15VW2FlP4 7dpG5SkVc+zL/IuR/2IV4/rhCfE0uCVgqBd26O/8IxElCRviiVTDL6iipn9k9ji2p+KfK bHzi+J076a/dLBSXCc5ULydIE1jzgAn2uGVD0fcEYPk2FSDeIL2mAp+ts1SLylhBvuU8I 3MbOJZUqL5UF+BE5bdmOekc4vc43WGBh5v7rMi7MrNpTXBHGJRRd8qRm0/FpxKXwORItJ G/xw0solydwLUBcKe7gZZdGdBLrdYy7Lwv8whXhBVQnhIobCCWBZEs5YSHusDkwNcXg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794348549; 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=DK6c4Mv1slcrfPYKr6p5gvaMnuNouZYdLRcM1zyRnEY=; b=rq2MxiLA1sETDGCzeuv1G69KKx7eiuZjWoyMzdzIfmXzfQXeVrSqhBHnF2CIdYxbeCyc BU6UDhbJXupVMGp+UbHpyGS5VTJIYb2RuzOOQXbfLzz1QZAOvtkLg6Fps6+i97Ap5Ue4p JoFmTU+YPq+cZiHl5wvOvFid93nB6ZXy6tQCDg8LVEJOoqel4V2Ubb5mBhfE3/Ci2RuOU CgOytnBw6VT8xbiPY5ZQbFcXMOjSlrX/+si07agONUAP17FDUgMHN2Up5KNI3deOht4sq lUsqCAQDBYTOhVlIx9E287v4atdu6hYM3CqtjSDusdTUyMNmXDNDoDaU9lyPOrfKP0g== 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=20260707 header.b=TSbp4qcW; arc=none smtp.remote-ip=209.85.215.198 Received: from mail-pg1-f198.google.com (mail-pg1-f198.google.com [209.85.215.198]) (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 62565606AE1D for ; Tue, 10 Nov 2026 22:09:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 62565606AE1D 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.a=rsa-sha256 header.s=20260707 header.b=TSbp4qcW Received: by mail-pg1-f198.google.com with SMTP id 41be03b00d2f7-cdd769cc9deso1191855a12.0 for ; Tue, 10 Nov 2026 14:09:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1794348548; x=1794953348; 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=DK6c4Mv1slcrfPYKr6p5gvaMnuNouZYdLRcM1zyRnEY=; b=TSbp4qcW1eG0z5Qw/F7SqfuRGlcQtcIzIltsRGhT+/2qxfDZ/rQosQa66XQr0mEFdA /CXmWnRVpflGRpiiPRaz3f+Ypbq++MErK1u6QrGJHXuVNHrriSaz+6FnunLgKqvZC8Gq WOCiMWE7s2DXaPxTLFv027tlgAeEwdJT8tP2e3cOmfcW3osH/csTY2QurXTp3MmicTFo SqdKyIMhAzS7amPmIN0EZjKHtbQMUaqVEtJ0vzZBZfoifijALxtdKApUuXbjDoAZEDk5 kJVgRh6bkwrXZJNOP4QRc3LWQ3CoHnfhWDX2icAYYhVDkzZ51uN8elszC6pMGh5qWbXy OXjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794348548; x=1794953348; 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=DK6c4Mv1slcrfPYKr6p5gvaMnuNouZYdLRcM1zyRnEY=; b=uF5wD8nPQp4II4jELq97lNJo76LuSBZfw3SoW0pCCl8/47UrR6rB0nFoVSLPO3cxws i/NzDUAZYLy14mhh5OUGUnuMe9WUkV9LXakqZSI762w6DMDSrS8DsUxNG2B1hVncJbiR oepTMvRN6wNDqT7hclb97Gun/YHtq+nSBsjXd/FExYxi074goqKea26k7KDf+PWdMYv3 7d5ikU/oyY9HBpFQH4p7wjf31VRlvvR2ZGhYasrTMihJEX/F8c/SI/HjQjDE2xzJeQpK E2RhFMMf59cdyMScdLHnC1e19k5XgiUJ0mXIZd/13jsP78gslQtctVxEqU2stQpo+L5f OJNg== X-Forwarded-Encrypted: i=1; AL+hErGRYeg7lAmXWPi2ND3XMyFrCgK2FeZ6F04vmDsVClcEVcxjJxI9YfjqfcfIQ5KifyDzjUK+KJNFQO2v8L8=@vger.kernel.org X-Gm-Message-State: APuU4Z+6l+2ppP4YcRFFIb00Y2gmxa5oCZK0VuZvZwACT11JRYgz5IVw /rjSsJyrHEtEQCTCusowX10cN4vEHjBABZJhaO+wxORvCWrEe09AIlU/qw5hjW5WPRS0lO5sFV/ ajcnXTQ6wPw== X-Received: from pjbds9.prod.google.com ([2002:a17:90b:8c9:b0:3ab:fd4b:afa8]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:55c3:b0:39e:4c80:f67b with SMTP id 98e67ed59e1d1-3b182eec04dmr331818a91.26.1794348548273; Tue, 10 Nov 2026 14:09:08 -0800 (PST) Date: Tue, 10 Nov 2026 14:08:01 -0800 In-Reply-To: <20261110220803.2503416-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: <20261027231513.816148-1-irogers@google.com> <20261110220803.2503416-1-irogers@google.com> X-Mailer: git-send-email 2.56.0.542.g3ef89b7020-goog Message-ID: <20261110220803.2503416-19-irogers@google.com> Subject: [PATCH v3 18/20] perf session: Add dso__session() and session-scoped allocation wrappers From: Ian Rogers To: irogers@google.com, acme@kernel.org, haghdoost@uber.com, namhyung@kernel.org Cc: adrian.hunter@intel.com, james.clark@linaro.org, jolsa@kernel.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, mingo@redhat.com, peterz@infradead.org Content-Type: text/plain; charset="UTF-8" Add explicit session-scoped memory allocation wrappers: - perf_session__malloc(session, size) - perf_session__zalloc(session, size) - perf_session__calloc(session, nmemb, size) - perf_session__realloc(session, ptr, size) - perf_session__strdup(session, s) When an allocation fails and returns NULL, if a non-NULL struct perf_session is provided, the wrapper invokes perf_session__shrink() to evict unreferenced symbols, DSOs, and cached debuginfo across the session's machines and then retries the allocation once. This avoids the thrashing overhead of periodic heuristic shrinking while allowing perf to recover from memory exhaustion. Also plumb a back-pointer from struct dsos to its owning struct machine so machine__session(machine) and dso__session(dso) can resolve the active perf_session. Signed-off-by: Ian Rogers --- tools/perf/util/dso.c | 7 ++++ tools/perf/util/dso.h | 2 ++ tools/perf/util/dsos.c | 1 + tools/perf/util/dsos.h | 1 + tools/perf/util/machine.c | 1 + tools/perf/util/machine.h | 5 +++ tools/perf/util/session.c | 70 +++++++++++++++++++++++++++++++++++++++ tools/perf/util/session.h | 6 ++++ 8 files changed, 93 insertions(+) diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c index 184d49935bfd..7b71fd5e48a9 100644 --- a/tools/perf/util/dso.c +++ b/tools/perf/util/dso.c @@ -1907,6 +1907,13 @@ void dso__delete(struct dso *dso) RC_CHK_FREE(dso); } +struct perf_session *dso__session(const struct dso *dso) +{ + struct dsos *dsos = dso ? RC_CHK_ACCESS(dso)->dsos : NULL; + + return dsos ? machine__session(dsos->machine) : NULL; +} + bool dso__can_shrink_symbols(const struct dso *dso) { const char *name; diff --git a/tools/perf/util/dso.h b/tools/perf/util/dso.h index 7f217815ec07..9b01a86526c1 100644 --- a/tools/perf/util/dso.h +++ b/tools/perf/util/dso.h @@ -20,6 +20,7 @@ struct machine; struct map; struct perf_env; +struct perf_session; #define DSO__NAME_KALLSYMS "[kernel.kallsyms]" #define DSO__NAME_KCORE "[kernel.kcore]" @@ -791,6 +792,7 @@ static inline refcount_t *dso__refcnt(struct dso *dso) bool dso__can_shrink_symbols(const struct dso *dso); size_t dso__shrink(struct dso *dso); +struct perf_session *dso__session(const struct dso *dso); char *dso__filename_with_chroot(const struct dso *dso, const char *filename); diff --git a/tools/perf/util/dsos.c b/tools/perf/util/dsos.c index f6868ffcc3e1..d53a8ab217e6 100644 --- a/tools/perf/util/dsos.c +++ b/tools/perf/util/dsos.c @@ -18,6 +18,7 @@ void dsos__init(struct dsos *dsos) { init_rwsem(&dsos->lock); + dsos->machine = NULL; dsos->cnt = 0; dsos->allocated = 0; dsos->dsos = NULL; diff --git a/tools/perf/util/dsos.h b/tools/perf/util/dsos.h index 151efdba2aa4..d636e4c5cbaf 100644 --- a/tools/perf/util/dsos.h +++ b/tools/perf/util/dsos.h @@ -19,6 +19,7 @@ struct machine; */ struct dsos { struct rw_semaphore lock; + struct machine *machine; struct dso **dsos; unsigned int cnt; unsigned int allocated; diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c index dad06eb865e6..c89f47ba16f2 100644 --- a/tools/perf/util/machine.c +++ b/tools/perf/util/machine.c @@ -81,6 +81,7 @@ int machine__init(struct machine *machine, const char *root_dir, pid_t pid) memset(machine, 0, sizeof(*machine)); RB_CLEAR_NODE(&machine->rb_node); dsos__init(&machine->dsos); + machine->dsos.machine = machine; threads__init(&machine->threads); machine->kmaps = maps__new(machine); diff --git a/tools/perf/util/machine.h b/tools/perf/util/machine.h index 0f4c0ae5b56d..6192bd3e33bb 100644 --- a/tools/perf/util/machine.h +++ b/tools/perf/util/machine.h @@ -164,6 +164,11 @@ static inline struct perf_session *machines__session(struct machines *machines) return machines->session; } +static inline struct perf_session *machine__session(const struct machine *machine) +{ + return machine && machine->machines ? machine->machines->session : NULL; +} + void machines__process_guests(struct machines *machines, machine__process_t process, void *data); diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c index 3f35c7affc29..3d4bc9c0063a 100644 --- a/tools/perf/util/session.c +++ b/tools/perf/util/session.c @@ -4274,3 +4274,73 @@ size_t perf_session__shrink(struct perf_session *session) return machines__shrink(&session->machines); } + +static __thread bool perf_session__in_shrink; + +static bool perf_session__try_shrink(struct perf_session *session) +{ + size_t freed; + + if (!session || perf_session__in_shrink) + return false; + + perf_session__in_shrink = true; + freed = perf_session__shrink(session); + perf_session__in_shrink = false; + + return freed > 0; +} + +void *perf_session__malloc(struct perf_session *session, size_t size) +{ + void *ptr = malloc(size); + + if (unlikely(!ptr && size != 0) && perf_session__try_shrink(session)) + ptr = malloc(size); + + return ptr; +} + +void *perf_session__zalloc(struct perf_session *session, size_t size) +{ + void *ptr = zalloc(size); + + if (unlikely(!ptr && size != 0) && perf_session__try_shrink(session)) + ptr = zalloc(size); + + return ptr; +} + +void *perf_session__calloc(struct perf_session *session, size_t nmemb, size_t size) +{ + void *ptr = calloc(nmemb, size); + + if (unlikely(!ptr && nmemb != 0 && size != 0) && perf_session__try_shrink(session)) + ptr = calloc(nmemb, size); + + return ptr; +} + +void *perf_session__realloc(struct perf_session *session, void *ptr, size_t size) +{ + void *new_ptr = realloc(ptr, size); + + if (unlikely(!new_ptr && size != 0) && perf_session__try_shrink(session)) + new_ptr = realloc(ptr, size); + + return new_ptr; +} + +char *perf_session__strdup(struct perf_session *session, const char *s) +{ + char *dup; + + if (!s) + return NULL; + + dup = strdup(s); + if (unlikely(!dup) && perf_session__try_shrink(session)) + dup = strdup(s); + + return dup; +} diff --git a/tools/perf/util/session.h b/tools/perf/util/session.h index cbea6792a008..c6157eddcc43 100644 --- a/tools/perf/util/session.h +++ b/tools/perf/util/session.h @@ -213,4 +213,10 @@ uint16_t perf_session__e_machine(struct perf_session *session, uint32_t *e_flags size_t perf_session__shrink(struct perf_session *session); +void *perf_session__malloc(struct perf_session *session, size_t size); +void *perf_session__zalloc(struct perf_session *session, size_t size); +void *perf_session__calloc(struct perf_session *session, size_t nmemb, size_t size); +void *perf_session__realloc(struct perf_session *session, void *ptr, size_t size); +char *perf_session__strdup(struct perf_session *session, const char *s); + #endif /* __PERF_SESSION_H */ -- 2.56.0.542.g3ef89b7020-goog