From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=198.175.65.11 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796344523; b=YAduD6ja3fPW7a96XHNlbFRzwM/miJTpyBe3VB5LpLB2tdSzW4LInr8R2QAjISxlF8oQ jAs+j55J7dx3w4E2+e+v+C6Otx6yZRLkacSAZWzLySFrtyTI0uOTKnAmUGDPqJ9HhhD0f /OPKQ3ohr7Mg7rzgjq3CEy8++zNzdYT518eoVBaI1+26i07h9JiNgU3Sy9EQtdCgI0wfC Y1Iaz74q4RBpBd3blfyeo4HpEFAPO9+tNRexA5wAFXbTdgIT+cVSk+pnA5tXIg6QDfrno JuuVVMgCSmvFNI8SZ5087JFEIBLIVGdNkksDcRwTZBj4cav4F4TgcvbJjfhqZz1P7Qw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796344523; h=DMARC-Filter:DKIM-Signature:X-CSE-ConnectionGUID:X-CSE-MsgGUID: X-IronPort-AV:X-IronPort-AV:Received:X-CSE-ConnectionGUID: X-CSE-MsgGUID:X-ExtLoop1:X-IronPort-AV:Received:Message-ID:Date: MIME-Version:User-Agent:Subject:To:Cc:References:Content-Language: From:In-Reply-To:Content-Type:Content-Transfer-Encoding; bh=eBq+ZREe3AbVRV5/jsA9/lmVSc0xBMjYD5aBQd8wbtc=; b=owsWx21G2CRlXoPJPGFHByY0a1Yn/CMBTS7UFFJ1+nAtrON4SofXpBqY/IyMvNeQLem/ gsicmfdL0DRNOejGhHtcoIZ18j/Jt1Avlz8FMV09MKBezc7jeuv4kWXZyWhdY7/kvLNrc D6wDGsEVNp/3sZz4trDLtASAFElb3zgc4tr8K0AtqP4nQwurQyDyCnKApnI9N8HV1sMIG n9oQEsnjDjODbHQpzhKk2eHcVOXibtjLrlCmYV2nKa4lZDYYUvtP5lBELpbleiEg6YW6Z eADJyYVVWiFfKpj4m8sx3nG1ZXaMC1O9KFCCSoh1UGxncbfj4g6i93iwD5NB45exLGw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.intel.com; spf=pass smtp.mailfrom=linux.intel.com; dkim=pass header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=ABphIrup; arc=none smtp.remote-ip=198.175.65.11 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.11]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B148B606A322; Fri, 4 Dec 2026 00:35:22 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B148B606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=ABphIrup DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796344523; x=1827880523; h=message-id:date:mime-version:subject:to:cc:references: from:in-reply-to:content-transfer-encoding; bh=eBq+ZREe3AbVRV5/jsA9/lmVSc0xBMjYD5aBQd8wbtc=; b=ABphIrupQVpWrg/3TUH/A7/tSVJGu7coNJa7AVdxZDu3wz1f/Ki2Sion XqI6Sl/ieHa+d+uxbo8a1+NKQya80DXG1xG9iVfYIU4MwhtWnSwRWUlgB 1TxkYsmqiSHTduck47Io/hMJDOo2vyjlfC7GCl9PU0GLwLNCLLRoeiQyp uQSCDK0BGdM12Ttvm75BylCGTkAE4RjAN4i5IXKUDmgVAu2PLZgxgjqpc MGjU4GkFOPurUtm6/k4ZwQuZ/MrwE/dNP9J7kj8s0mm3uTK//gRjOj0nl Sc7b5evAhxCrm2Ft+9FVu7VsU1483YtJxtbqfDR5UdQl34+IejC4AOQqy w==; X-CSE-ConnectionGUID: OGXovyL4Sr2Sj8TuEUIoeg== X-CSE-MsgGUID: AGYUmetjQEmguKS5zv8pJg== X-IronPort-AV: E=McAfee;i="6800,10657,11985"; a="5220001" X-IronPort-AV: E=Sophos;i="6.29,77,1794902400"; d="scan'208";a="5220001" Received: from fmviesa002.fm.intel.com ([10.60.135.142]) by orvoesa103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Dec 2026 16:35:22 -0800 X-CSE-ConnectionGUID: iP2PR94RTnmwau/MtwmjZQ== X-CSE-MsgGUID: yRZ5wIHBQ3K5EwdlHWH1zQ== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.29,77,1794902400"; d="scan'208";a="14189458" Received: from dapengmi-mobl1.ccr.corp.intel.com (HELO [10.124.241.239]) ([10.124.241.239]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Dec 2026 16:35:17 -0800 Message-ID: Date: Fri, 4 Dec 2026 08:35:14 +0800 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 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v4 1/4] perf/core: Do not account overflows of a throttled event To: Oancea Catalin , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim Cc: Kan Liang , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark , Thomas Gleixner , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, Ravi Bangoria References: <20261203113922.77972-1-john3@mailbox.org> <20261203113922.77972-2-john3@mailbox.org> Content-Language: en-US From: "Mi, Dapeng" In-Reply-To: <20261203113922.77972-2-john3@mailbox.org> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit The change log looks much clear now. Thanks. On 12/3/2026 7:39 PM, Oancea Catalin wrote: > A throttled group can stay stopped when the tick should start it > again. Its events then deliver nothing until the task is scheduled in > again. > > A throttle marks every event of the group with hw.interrupts == > MAX_INTERRUPTS and stops it. The tick unthrottles a group when it > finds that mark on the group leader. __perf_event_account_interrupt() > counts the overflow of an event which is already marked like any > other, and that overwrites the mark. > > This happens when a software event throttles the group of a hardware > leader. perf_event_throttle() marks the leader and then stops it, and > an NMI of the leader in between accounts one more overflow: > > software sibling hardware leader, NMI > perf_event_throttle_group() > perf_event_throttle(leader) > hw.interrupts = MAX_INTERRUPTS > perf_event_overflow() > hw.interrupts++, now 0 > pmu->stop() > > tick: the leader is not marked, no unthrottle > > A cycles event (kernel only, period 2000) leading a group with a > sched:sched_stat_runtime tracepoint event (period 1), 5 seconds on a > pinned task in a KVM guest: > > THROTTLE UNTHROTTLE tracepoint samples > before 108 7 43139 > after 508 507 202554 Nit: "before" and "after" are ambiguous, "W/O fix" and "With fix" would be clearer. Besides, it looks Sashiko raises new race condition issues, please double check. Thanks. > > It also happens when an interrupt handler reports the overflow of a > counter which a throttle has just stopped. The following patches make > the x86 handlers do that. Take two instructions events in one group on > an Intel CPU, the leader on fixed counter 0 and the sibling on a > general purpose counter, which overflow in the same PMI. The sibling > is handled first and throttles the group. Then the leader's overflow > is accounted and its mark wraps to 0. Without this change that group > got 29 THROTTLE records and no UNTHROTTLE record in 4 seconds. > > Return early for an event which is already throttled. Return 1, as the > throttle itself does, so that perf_swevent_overflow() and the other > callers which stop on that value still do. The sample is delivered as > before. > > An AI coding assistant (Claude Code) was used in the analysis and in > writing the patch. > > Fixes: 9734e25fbf5a ("perf: Fix the throttle logic for a group") > Signed-off-by: Oancea Catalin > Cc: stable@vger.kernel.org > --- > Sashiko's review of v3 asked for 1 instead of 0 here, because of the > loop in perf_swevent_overflow(): > https://lore.kernel.org/all/sashiko-outbox-247257@kernel.org/ > The test below (swloop.c) is for that. It needs root, or > perf_event_paranoid at 1 or lower. With v3 the tracepoint event > delivered up to 105 million samples in one loop, with interrupts off > for up to 3.6 seconds. With 1 the longest run is 399, and 401 without > the patch (the limit was 400 per tick). > > The same test gave the table in the changelog; "after" is this patch > alone. Debug prints (not part of the patch) show the sequence of the > diagram: in 5 seconds the leader was accounted 105 times while it was > marked, each time from NMI, inside the perf_event_throttle_group() of > the tracepoint event and before the leader was stopped. In that run > the group was running again 104 times without an UNTHROTTLE record. > > On a Core i7-14700T the same group gets 150 THROTTLE and 76 UNTHROTTLE > records in 5 seconds without the series, and 1317 of each with it. > 6.18.66 shows the same, in the guest and on that machine. > > The 29 THROTTLE records of the two instructions events are from a test > kernel with patches 2 to 4 but without this one, on the Core i7-14700T. > Both events delivered 11928 samples in the 4 seconds, instead of about > 400000. > > perf_event_account_interrupt() has other callers (Intel PEBS, AMD IBS, > powerpc). For an event which is already throttled they now get the > value they get when it is throttled. > > ---- swloop.c ---- > // SPDX-License-Identifier: GPL-2.0 > /* > * swloop: does a throttle from outside end the overflow loop of a software > * event? (Question raised in review of v3 patch 1/4.) It counts kernel > * cycles, so it needs root or perf_event_paranoid at 1 or lower. The id of > * sched:sched_stat_runtime is in its tracefs "id" file, which root can read. > * > * One group on the calling task, pinned to one CPU: > * H cycles, kernel only, sample_period (leader, hardware, NMI) > * E tracepoint sched:sched_stat_runtime, sample_period 1 > * The tracepoint adds the run time in ns to E, so every hit asks > * perf_swevent_overflow() for about a million overflows; the loop there ends > * when the event is throttled. If H's NMI throttles the group while that loop > * runs, E is marked throttled from outside. > * > * Output per run: samples of H and E, lost records, throttle records, the > * longest row of E samples (lost ones included) between two other records, > * and the longest time the task did not get to run in user mode. > * A THROTTLE record that follows another one without an UNTHROTTLE record in > * between is counted as "repeated_throttle" if H gave at most one sample in > * between (the group was throttled twice), and as "ran_without_unthrottle" > * if H gave more: H is stopped by a throttle, so the group was restarted > * without having been unthrottled. > * > * usage: swloop [buffer pages, power of 2] > */ > #define _GNU_SOURCE > #include > #include > #include > #include > #include > #include > #include > #include > #include > #include > #include > > static uint64_t now_ns(void) > { > struct timespec ts; > > clock_gettime(CLOCK_MONOTONIC, &ts); > return ts.tv_sec * 1000000000ull + ts.tv_nsec; > } > > static int ev_open(struct perf_event_attr *a, int group) > { > return syscall(SYS_perf_event_open, a, 0, -1, group, 0); > } > > int main(int argc, char **argv) > { > struct perf_event_attr h = { 0 }, e = { 0 }; > struct perf_event_mmap_page *mp; > uint64_t idh, ide, nh = 0, ne = 0, lost = 0, thr = 0, unthr = 0; > uint64_t run = 0, maxrun = 0, gap, maxgap = 0, t, last, end, big = 0; > uint64_t head, tail, mask, hs = 0, repeated = 0, ranwo = 0; > int throttled = 0; > size_t pages, len; > cpu_set_t set; > char *data; > int fh, fe; > > if (argc < 5) { > fprintf(stderr, "usage: swloop [pages]\n"); > return 2; > } > pages = argc > 5 ? strtoul(argv[5], NULL, 0) : 8192; > CPU_ZERO(&set); > CPU_SET(atoi(argv[1]), &set); > if (sched_setaffinity(0, sizeof(set), &set)) { > perror("sched_setaffinity"); > return 1; > } > > h.size = sizeof(h); > h.type = PERF_TYPE_HARDWARE; > h.config = PERF_COUNT_HW_CPU_CYCLES; > h.sample_period = strtoull(argv[3], NULL, 0); > h.sample_type = PERF_SAMPLE_IDENTIFIER; > h.exclude_user = 1; > h.exclude_hv = 1; > h.disabled = 1; > fh = ev_open(&h, -1); > if (fh < 0) { > perror("open H"); > return 1; > } > > e.size = sizeof(e); > e.type = PERF_TYPE_TRACEPOINT; > e.config = strtoull(argv[4], NULL, 0); > e.sample_period = 1; > e.sample_type = PERF_SAMPLE_IDENTIFIER; > fe = ev_open(&e, fh); > if (fe < 0) { > perror("open E"); > return 1; > } > > len = (pages + 1) * sysconf(_SC_PAGESIZE); > mp = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fh, 0); > if (mp == MAP_FAILED) { > perror("mmap"); > return 1; > } > data = (char *)mp + sysconf(_SC_PAGESIZE); > mask = pages * sysconf(_SC_PAGESIZE) - 1; > if (ioctl(fe, PERF_EVENT_IOC_SET_OUTPUT, fh) || > ioctl(fh, PERF_EVENT_IOC_ID, &idh) || ioctl(fe, PERF_EVENT_IOC_ID, &ide)) { > perror("ioctl"); > return 1; > } > ioctl(fh, PERF_EVENT_IOC_ENABLE, PERF_IOC_FLAG_GROUP); > > last = now_ns(); > end = last + strtoull(argv[2], NULL, 0) * 1000000000ull; > tail = 0; > for (;;) { > t = now_ns(); > gap = t - last; > last = t; > if (gap > maxgap) > maxgap = gap; > if (gap > 20000000) > big++; > if (t > end) > break; > > head = __atomic_load_n(&mp->data_head, __ATOMIC_ACQUIRE); > while (tail < head) { > struct perf_event_header hd; > uint64_t v[3] = { 0 }; > size_t i, n; > > for (i = 0; i < sizeof(hd); i++) > ((char *)&hd)[i] = data[(tail + i) & mask]; > n = hd.size - sizeof(hd); > if (n > sizeof(v)) > n = sizeof(v); > for (i = 0; i < n; i++) > ((char *)v)[i] = data[(tail + sizeof(hd) + i) & mask]; > tail += hd.size; > > if (hd.type == PERF_RECORD_SAMPLE && v[0] == ide) { > ne++; > run++; > } else if (hd.type == PERF_RECORD_LOST) { > /* only E can fill the buffer; count them with its row */ > lost += v[1]; > run += v[1]; > } else { > if (hd.type == PERF_RECORD_SAMPLE && v[0] == idh) { > nh++; > hs += throttled; > } else if (hd.type == PERF_RECORD_THROTTLE) { > thr++; > if (throttled && hs > 1) > ranwo++; > else if (throttled) > repeated++; > throttled = 1; > hs = 0; > } else if (hd.type == PERF_RECORD_UNTHROTTLE) { > unthr++; > throttled = 0; > } > if (run > maxrun) > maxrun = run; > run = 0; > } > } > if (run > maxrun) > maxrun = run; > __atomic_store_n(&mp->data_tail, tail, __ATOMIC_RELEASE); > last = now_ns(); /* do not count the time spent reading */ > } > ioctl(fh, PERF_EVENT_IOC_DISABLE, PERF_IOC_FLAG_GROUP); > > printf("hperiod=%llu H=%llu E=%llu lost=%llu throttle=%llu unthrottle=%llu longest_E_row=%llu longest_gap_ms=%.1f gaps_over_20ms=%llu repeated_throttle=%llu ran_without_unthrottle=%llu\n", > (unsigned long long)h.sample_period, (unsigned long long)nh, > (unsigned long long)ne, (unsigned long long)lost, > (unsigned long long)thr, (unsigned long long)unthr, > (unsigned long long)maxrun, maxgap / 1e6, (unsigned long long)big, > (unsigned long long)repeated, (unsigned long long)ranwo); > return 0; > } > ---- end of swloop.c ---- > > kernel/events/core.c | 9 +++++++++ > 1 file changed, 9 insertions(+) > > diff --git a/kernel/events/core.c b/kernel/events/core.c > index 4ab3919ac78e..4b47883e82af 100644 > --- a/kernel/events/core.c > +++ b/kernel/events/core.c > @@ -10773,6 +10773,15 @@ __perf_event_account_interrupt(struct perf_event *event, int throttle, > int ret = 0; > u64 seq; > > + /* > + * A throttled event can still report an overflow: from an NMI before > + * perf_event_throttle() has stopped it, or one that was pending when > + * it was stopped. Do not account it, that would overwrite the mark > + * which the tick needs on the group leader to unthrottle the group. > + */ > + if (unlikely(hwc->interrupts == MAX_INTERRUPTS)) > + return 1; > + > seq = __this_cpu_read(perf_throttled_seq); > if (seq != hwc->interrupts_seq) { > hwc->interrupts_seq = seq;