Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp719990rwd; Wed, 7 Jun 2023 06:08:31 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4JPY2YsQaQq3DkxE4QanvyEqyhNi1lDcGweTBUANbJcbF1ejIxgU5PDoM62mNceb/5zspa X-Received: by 2002:a05:6a20:258b:b0:10b:dca5:74e5 with SMTP id k11-20020a056a20258b00b0010bdca574e5mr2237362pzd.0.1686143310827; Wed, 07 Jun 2023 06:08:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686143310; cv=none; d=google.com; s=arc-20160816; b=Noq3bk5MoscMZxocZsAjyeXDZfU3xTUT6fe7+ZJO9J+S+S6lm23X0ZXNlwEIbXZsuL VVwZsAWyhByd2ctqWqfVoyeQYAte4cdrMak096sFfFnazooZhSPLNNKOipmk6Lb/M016 21RxBfdXAS5sx25V6X7a3LPqhMfSjN4ZD70GNk1t+pZPs5w405eMlN13zqUHl1LO7moQ nNcKyMP+Z5YaSq5AsVvXvXrDUEO/3fhX2aDSjJM6KQryIVcayphPWYj3VqsZrc3L6k2j dts4Zs+KtmEcREmW+CugNd3HlLiPYN5BYKFd7Ve3onaGcEAEXNj9kNq1xGy0zKBlPcSe EKKQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=hGJqdQiK4MKfPRMFEElKPIlLSoaxgQujZcPcywtmuLE=; b=0wpFR3AF7wrYLWwWsnXzppzKEJAOrEUDEBfjRIJuQ8fRcyhNNaHNdD829z739Gb48l JZDL9Fg78miuHk97TC7eIYAPpyLSVodLc7XcC7rqHeuO+75aDm8cUuWlEdJEnLaWXVDf 054wEL/gYUw3SdrsQARN0LC6oG2YnLiDt8HQm8DBHrizYUoF6NDkEOuG3yA0BLvCEwIO pLfL9vwMzzo56fDYLGoPw1Bl8Hh/ONkHOtCpLNktOmsSK8wp6ujGjgiWNmw8dy/3iolA dvZj96oWAQU22pGelhpdS8qw4u81gaH42V06qZ7uy3QkXt7Z/zoGsr5Dru4obuEuxPeE DQUw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=vx9H63ZE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 19-20020a621713000000b0062d7d3c6cadsi8535401pfx.333.2023.06.07.06.08.15; Wed, 07 Jun 2023 06:08:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=vx9H63ZE; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235157AbjFGMwU (ORCPT + 99 others); Wed, 7 Jun 2023 08:52:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59500 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235568AbjFGMwQ (ORCPT ); Wed, 7 Jun 2023 08:52:16 -0400 Received: from mail-qt1-x834.google.com (mail-qt1-x834.google.com [IPv6:2607:f8b0:4864:20::834]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7589019AE for ; Wed, 7 Jun 2023 05:51:52 -0700 (PDT) Received: by mail-qt1-x834.google.com with SMTP id d75a77b69052e-3f9a81da5d7so205431cf.0 for ; Wed, 07 Jun 2023 05:51:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1686142311; x=1688734311; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=hGJqdQiK4MKfPRMFEElKPIlLSoaxgQujZcPcywtmuLE=; b=vx9H63ZEKwP8kf2QmtQ5uIY2hNqkPc7muS69VQPvFQvC7m37HvsT/pqFCNYWwt26bY ORN9HwT7kORDv951avMLH/QW4H+YjyalvyUuvkjFG7/yaTglyG8Cie0CC9KAzUNqW4cI 8+HanzbDZPc4noU+1JxQuPiMPJsKROvKQIRAi2/8yDnQCme5V0UhLSp2qpMGYHeL/Iyg vV+HXyGrwJz/pFX4ngrVDMEBvpuMh/SJs/cZd3EfISpgHwL52kj/6LjNeLM0y1h2ro/A 0wLqB2FtqqZhFNmAvjBJY0e12bQbvuq/dADgWwFR2yC1wf7juC7gSY7dZiMrVhYSoD/v TJ/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686142311; x=1688734311; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=hGJqdQiK4MKfPRMFEElKPIlLSoaxgQujZcPcywtmuLE=; b=LD+5xMIys583IioMfVEHNdcfNZA0okxMtXX+LwGCAPFBfDyHCQYDnznYvlA5BIF6Uh RQvYmp7s2CZ/VoPvlJCmHOgo6EZGnToyTQAMNQcmi82YytMA4g2KcxtZvG+OVAVqJCF5 +Yu+tub+OZOTYeI07W5YxTV2tQZRQkqgqaTQTzVtaFrZSdor2sC9vTIAe3bUXZXDy6K2 LQcwBae/d+DWR/L0xImaCLIgu6KiK7JPgJwJxo6DqCFoHCIsaT5XLYUM+eB2oFjjVTLP 1lVC/jRdVs7A/wHU13SuTuj6lC188WV8JN2CE8LjoAvENpibnfDUxYXJZiczg3G77hpy 4tuA== X-Gm-Message-State: AC+VfDzoJkPVdNBk1QBjCJO0ip9loC5iFG1+CMg9r2if25u8yDITqKXP e+rvDDBB6ZCfZvDonotR9PBM8Ue2vntp5Mc12E2A8D87ORURBGeLJEekpg== X-Received: by 2002:a05:622a:c5:b0:3f9:6930:1307 with SMTP id p5-20020a05622a00c500b003f969301307mr213974qtw.22.1686142311175; Wed, 07 Jun 2023 05:51:51 -0700 (PDT) MIME-Version: 1.0 References: <20230525180209.19497-1-james.morse@arm.com> <20230525180209.19497-13-james.morse@arm.com> <7ff7fcf9-725b-fb3d-43f3-b80a1df3a001@arm.com> In-Reply-To: <7ff7fcf9-725b-fb3d-43f3-b80a1df3a001@arm.com> From: Peter Newman Date: Wed, 7 Jun 2023 14:51:40 +0200 Message-ID: Subject: Re: [PATCH v4 12/24] x86/resctrl: Make resctrl_arch_rmid_read() retry when it is interrupted To: James Morse Cc: x86@kernel.org, linux-kernel@vger.kernel.org, Fenghua Yu , Reinette Chatre , Thomas Gleixner , Ingo Molnar , Borislav Petkov , H Peter Anvin , Babu Moger , shameerali.kolothum.thodi@huawei.com, D Scott Phillips OS , carl@os.amperecomputing.com, lcherian@marvell.com, bobo.shaobowang@huawei.com, tan.shaopeng@fujitsu.com, xingxin.hx@openanolis.org, baolin.wang@linux.alibaba.com, Jamie Iles , Xin Hao , dfustini@baylibre.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-17.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL,USER_IN_DEF_SPF_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi James, On Tue, Jun 6, 2023 at 7:03=E2=80=AFPM James Morse wr= ote: > On 06/06/2023 09:49, Peter Newman wrote: > > It looks like if __rmid_read() is interrupted by an occupancy counter > > read between writing QM_EVTSEL and reading QM_CTR, it will not perform > > any update to am->prev_msr, and the interrupted read will return the > > same counter value as in the interrupting read. > > Yup, that's a problem. I was only looking at the mbm state in memory, not= the CPU register. > I think the fix is to read back QM_EVTSEL after reading QM_CTR. I'll do t= his in > __rmid_read() to avoid returning -EINTR. It creates two retry loops which= is annoying, but > making the window larger means you're more likely to see false positives. > > ----------------------------%<---------------------------- > diff --git a/arch/x86/kernel/cpu/resctrl/monitor.c b/arch/x86/kernel/cpu/= resctrl > /monitor.c > index e24390d2e661..aeba035bb680 100644 > --- a/arch/x86/kernel/cpu/resctrl/monitor.c > +++ b/arch/x86/kernel/cpu/resctrl/monitor.c > @@ -101,6 +101,7 @@ static inline u64 get_corrected_mbm_count(u32 rmid, u= nsigned > long val) > > static int __rmid_read(u32 rmid, enum resctrl_event_id eventid, u64 *val= ) > { > + u32 _rmid, _eventid; > u64 msr_val; > > /* > @@ -110,9 +111,15 @@ static int __rmid_read(u32 rmid, enum resctrl_event_= id eventid, u64 *val) > * IA32_QM_CTR.data (bits 61:0) reports the monitored data. > * IA32_QM_CTR.Error (bit 63) and IA32_QM_CTR.Unavailable (bit 62= ) > * are error bits. > + * QM_EVTSEL is re-read to detect if this function was interrupte= d by > + * another call, meaning the QM_CTR value may belong to a differe= nt > + * event. > */ > - wrmsr(MSR_IA32_QM_EVTSEL, eventid, rmid); > - rdmsrl(MSR_IA32_QM_CTR, msr_val); > + do { > + wrmsr(MSR_IA32_QM_EVTSEL, eventid, rmid); > + rdmsrl(MSR_IA32_QM_CTR, msr_val); > + rdmsr(MSR_IA32_QM_EVTSEL, _eventid, _rmid); > + } while (eventid !=3D _eventid || rmid !=3D _rmid); > > if (msr_val & RMID_VAL_ERROR) > return -EIO; I happen to be tracking the cost of resctrl_arch_rmid_read() calls, so I measured the impact of your fix on my AMD EPYC 7B12: with both this and the soft RMID series[1] applied: Base switch 7955 0.23% Hard RMID Switch 8476 6.80% Soft RMID Switch 10173 28.17% CLOSID+Soft RMID 10740 35.32% then adding EVTSEL read-back patch: Base switch 7985 Hard RMID Switch 8540 6.96% Soft RMID Switch 11015 37.95% CLOSID+Soft RMID 11590 45.16% The Soft RMID switches contain two __rmid_read() calls, so this implies each QM_EVTSEL read-back is around 420 cycles on this AMD implementation. Even if you don't agree with my plan to add resctrl_arch_rmid_read() calls to context switches, there should be cheaper ways to handle this. -Peter [1] https://lore.kernel.org/lkml/20230421141723.2405942-4-peternewman@googl= e.com/