From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-dy1-f172.google.com (mail-dy1-f172.google.com [74.125.82.172]) (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 9021B421EEB for ; Tue, 31 Mar 2026 18:11:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774980662; cv=none; b=ZSekEWgbq8h3ZrlkAiyKiCmKI7sGIPC6Og5qapJGFgnI+i/+bqMar0v/UCpiLe5UuinaDM8uhhMrz7GCza5cInxbD0uhNGrOKFsEbNTK7wGfaWDhV1VzGrPCbjwFYFHIlKzmhHZjjbDDjfy6yI6LyLsG27qH3jVJNROtKxuOCDQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774980662; c=relaxed/simple; bh=EwPuWU0xZA73peF5qoUgJuri+8cLwRR7kQwVKdYbfm4=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=MZh7esUP6zWL7GlxVEryIWufYGtMzhgTRLpwlrA9a4VdCfRoKX2YUmt4sDj/4PfBPd+XgnSxEt7D55dEFu5/q+zk+t5fVA2htAVdLROjoB3ojKOe2xgF5XOIspYxdCxZRvmAu7VKUDUC4TUPROsOxdMrPRauiX4EyRCKJ++GkEk= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=wbinvd.org; spf=pass smtp.mailfrom=wbinvd.org; dkim=pass (2048-bit key) header.d=wbinvd.org header.i=@wbinvd.org header.b=LYTQCoBq; arc=none smtp.client-ip=74.125.82.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=wbinvd.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=wbinvd.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=wbinvd.org header.i=@wbinvd.org header.b="LYTQCoBq" Received: by mail-dy1-f172.google.com with SMTP id 5a478bee46e88-2c54c68db4dso4724235eec.0 for ; Tue, 31 Mar 2026 11:11:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=wbinvd.org; s=wbinvd; t=1774980660; x=1775585460; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=VkIAMUPZY84q092krccwnnsiBEiT1ZKWhmADyhwjHC8=; b=LYTQCoBqabEEv4XfrkICSHQ9sF+nV+1TRAIRqmAcSgDsnQ3T3cdnF6Y+FQnik1pkNl 0O6LJqmL0QXJeq0A9TU+eoIvo9FTV768mMiNtrA5g3g6EgUc8ynSwGHi8kIJzhZPu41E EKHF+8W+vDBSZnKvSpxDD3HfARaXS9CoJgMzkB6qT4iA/UaAipFpb36dy9moCTtsuSJ8 svUJ7Tf6xwUcR72C/heKXzx3f4usGxtmjZ6k3xByoFNmEcQyfqx9oJj97hcDREDlZgZA Rf+pZcGSicpRz0Kuo1vBO4QdS1RmEW0+s5GxMfuEdzSSETFJc5GFhSBFdku1Xw/zLkqc mvQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774980660; x=1775585460; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=VkIAMUPZY84q092krccwnnsiBEiT1ZKWhmADyhwjHC8=; b=KjQ733b+xj7G484FJ1Nis0P3DbYulrehB5BIHEgs4slQVEBJFBY1F3iNnGFn0cfHGA arPAW9lwDqZ3DUnoajQ3cQK9K05pjKJGhOkXf1lG68kKIw1l20+nGDAFnJrYAtuevDka zn4lcclgeWAHtUaq6hOysweR9xjnbklEZmv9Sb3TarkbLtBjmIamBoLOb7lNsr70/1b+ dyGVnPAi6OMEVjDfidxZGqzygAxBXrTMyGUXVeUGTxyIjwKGIHpKRK51fAN9bhdBmAa0 ygy+SUWoe80FHuyQqqMsynFewdd5XW+TekM7WOLZWNCg9J2tZfniI5IAMQYr113R2L1x dz2A== X-Gm-Message-State: AOJu0YwnwVgBqs2Gl9/Xeqf6Pize0aYT/08uWCqjWuZWkh+4OaDnv+14 euY2U7NjByFXv8+Kjsfgx4DLjSWzK16b1df2J9OTkO2p4MLAjL4AE1SyE2LkKAKIn7gkGdvjAuu 8P5KrBW5gwA== X-Gm-Gg: ATEYQzwECzBidu14OwJ6p7RAGLkFWFH9oCEM5Rhm+luCbNhCjKZgYWrPj5SBSIjf2O/ QbQCTompawLVCkEWKr0J437KFDJMDr3B64UpVZ3ts99ac0t9vwLfU4ExhkOMsWhJsVfc5Rz+XQB n3+rUFUci5RfoLd5Jx2/HRHzo0ns/FOHdMkWtPuM1o6krS+0z8K36c7cb2xVww7M9OO5TvlaKrN gFgKKppRwnCj7MSey69/utjHpfenn0zwctxxmw/0QoFLimwWkl5UYOWHwnr5K5ZQt/2DqPo8jpp vcytPdzaRpaBuKK0pkDuhg4azOOR6qGFeHd/KlcZplHPPJYK/rvy9pIhczv8sxAqk7ZauocbC8e rHwtkii4JDcGcf7DoGNML/Ysf01jMry6RN3o9d1/2tAvfolTra0Vr+vfkJo9VTIP/nKE7oSdU6V bq5s78i8bE56moUqpQIqGPT7siAg== X-Received: by 2002:a05:7301:1015:b0:2c5:50fe:c782 with SMTP id 5a478bee46e88-2c930c76e65mr161749eec.3.1774980659667; Tue, 31 Mar 2026 11:10:59 -0700 (PDT) Received: from mozart.vkv.me ([2001:5a8:468b:d015:d70b:ce67:6f19:d9e5]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-2c3c68b2c57sm11289860eec.16.2026.03.31.11.10.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 31 Mar 2026 11:10:59 -0700 (PDT) Date: Tue, 31 Mar 2026 11:10:56 -0700 From: Calvin Owens To: linux-kernel@vger.kernel.org Cc: linux-perf-users@vger.kernel.org, x86@kernel.org, Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , James Clark , Thomas Gleixner , Borislav Petkov , Dave Hansen , "H. Peter Anvin" Subject: Re: [PATCH 2/2] perf: Don't throttle based on NMI watchdog events Message-ID: References: <685061daed5b505ef604835b602e0202531f2315.1774969692.git.calvin@wbinvd.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: On Tuesday 03/31 at 10:22 -0700, Calvin Owens wrote: > On Tuesday 03/31 at 08:25 -0700, Calvin Owens wrote: > > @@ -663,6 +666,17 @@ void perf_sample_event_took(u64 sample_len_ns) > > if (avg_len <= max_len) > > return; > > > > + /* > > + * Very infrequent events like the perf counter hard watchdog > > + * can trigger spurious throttling: skip throttling if the prior > > + * NMI got here more than one second before this NMI began. > > + */ > > + now = local_clock(); > > + delta = now - __this_cpu_read(last_throttle_clock); > > + __this_cpu_write(last_throttle_clock, now); > > + if (delta - sample_len_ns > NSEC_PER_SEC) > > + return; > > Bah, Sashiko caught something obvious I missed: > > https://sashiko.dev/#/patchset/cover.1774969692.git.calvin%40wbinvd.org > > >> When the outer handler completes, its sample_len_ns (total execution > >> time) will be strictly greater than delta (time since the inner > >> handler finished). This guarantees delta < sample_len_ns, causing the > >> subtraction to underflow to a massive positive value. > >> > >> The condition > NSEC_PER_SEC will then evaluate to true, and the outer > >> handler will erroneously skip the perf throttling logic. Should this > >> check be rewritten to avoid subtraction, perhaps by using if (delta > > >> sample_len_ns + NSEC_PER_SEC)? > > The solution it proposed makes sense to me. I replied too quickly: I think Sashiko is actually wrong. It is assuming that sample_len_ns includes the latency of perf_sample_event_took(), but it does not. Nesting in the middle of the RMW of the percpu value strictly makes last_throttle_clock appear to have happened *sooner* to the outer NMI, so I think that case works. Thanks, apologies again for all the noise here, Calvin > > __report_avg = avg_len; > > __report_allowed = max_len; > > > > -- > > 2.47.3 > >