Received: by 2002:a05:6358:7058:b0:131:369:b2a3 with SMTP id 24csp5858879rwp; Mon, 17 Jul 2023 10:28:59 -0700 (PDT) X-Google-Smtp-Source: APBJJlGMM+s7Xh6UmyN2CFCiFnppULQ69AiTQs7OyRQ+Gjj//IXqBh6C/Ns1cMUGmAubD2oadByV X-Received: by 2002:a17:903:1109:b0:1b3:d6c8:7008 with SMTP id n9-20020a170903110900b001b3d6c87008mr13350612plh.57.1689614938999; Mon, 17 Jul 2023 10:28:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689614938; cv=none; d=google.com; s=arc-20160816; b=S3EvZ+q+63QPhbt403SBj1S0CSBgISUNYi3Kf7q0ZBzDQJMsyN9ulOzTCEXMLX2Y4P Iak6Yo1DzXKFaVqo7OK6qJVb3Oj5/PbMmznaE8N4QqJxYeq2WBpvC6Z8uwg74Q4xcmRX xX4nTzqm5WwXnaCsCOFT0S2cbB8xtlADjOBOTKhcjiSwJ9RP3KLgQZ9REeZIKg9RtmFI 4Uywm4smNs9/YZ56LEC/0sTQheQdpn7pWhyNoFkeowyXiXlPdiiSNdDA47wAUVgghq8a kPngxpRHRb6NoVxijoQ0XRGzGjssklVoIaqjNjP73JK0I1yl1fBSvmRIrXt9oW3QwWeL seBQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id; bh=EEDNEGfDZkC91O2577JSt1ydjk4XeHAw4FvfvAiNWG0=; fh=fvhjIpPy9mUZO++1M4nSZvKV9LqxnhH4nfskhlXpySo=; b=upZdghm6I+zRNz130a4RQwdr2atqXaqspOMOjBZGlW2ztECAF1314Gj+swkxT177Ph GKaav1Ss8Y6XpmfNFeeYeF6DNDSMaYo8tUBSe/ujkuarHgkNw3xnoRRy3L0HFCH1aZD+ m6tCd8TJdg7fl67ernaxzFwM3qZec0TTjeDIe1Y/BxCOOPBRVrnurwP2gGUANMS4G+wJ CEohM+UGmBMrjcz0qbHuzjFKpH5h5tolGBmSy9zkmZqgHUipaA6a9Yo2qDAJLjWpSPAj olIqUi6iOgtjpz1BMYo5aXx6TipBkuQ+r8r91MrRm/ml5jrEM3Nnafv9fkXZIGePRF5r zFJA== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w19-20020a170902c79300b001b8a73eaa73si126153pla.429.2023.07.17.10.28.45; Mon, 17 Jul 2023 10:28:58 -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; 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=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229764AbjGQRHg (ORCPT + 99 others); Mon, 17 Jul 2023 13:07:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60698 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229669AbjGQRHe (ORCPT ); Mon, 17 Jul 2023 13:07:34 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id ED68C131 for ; Mon, 17 Jul 2023 10:07:32 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 1D8F1D75; Mon, 17 Jul 2023 10:08:16 -0700 (PDT) Received: from [10.57.79.8] (unknown [10.57.79.8]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 2F3413F738; Mon, 17 Jul 2023 10:07:27 -0700 (PDT) Message-ID: Date: Mon, 17 Jul 2023 18:07:21 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.13.0 Subject: Re: [PATCH v4 12/24] x86/resctrl: Make resctrl_arch_rmid_read() retry when it is interrupted Content-Language: en-US To: Peter Newman 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 References: <20230525180209.19497-1-james.morse@arm.com> <20230525180209.19497-13-james.morse@arm.com> <7ff7fcf9-725b-fb3d-43f3-b80a1df3a001@arm.com> From: James Morse In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,NICE_REPLY_A, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE 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 Peter, On 6/7/23 13:51, Peter Newman wrote: > On Tue, Jun 6, 2023 at 7:03 PM James Morse wrote: >> 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 this 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, unsigned >> 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 interrupted by >> + * another call, meaning the QM_CTR value may belong to a different >> + * 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 != _eventid || rmid != _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: > 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. Oooer. I assumed writes might have tedious side-effects but reads would cheap. I suppose its because another CPU may have modified this value in the meantime. > 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. Yup, I've swapped this for a sequence counter[0], which should push that cost into the noise. Anything left will be the cost of the atomics. Thanks, James [0] barely tested: ------------------%<------------------ diff --git a/arch/x86/kernel/cpu/resctrl/monitor.c b/arch/x86/kernel/cpu/resctrl/monitor.c index 238831d53479..86d3a1b99be6 100644 --- a/arch/x86/kernel/cpu/resctrl/monitor.c +++ b/arch/x86/kernel/cpu/resctrl/monitor.c @@ -16,6 +16,7 @@ */ #include +#include #include #include @@ -24,6 +25,9 @@ #include "internal.h" +/* Sequence number for writes to IA32 QM_EVTSEL */ +static DEFINE_PER_CPU(u64, qm_evtsel_seq); + struct rmid_entry { /* * Some architectures's resctrl_arch_rmid_read() needs the CLOSID value @@ -178,8 +182,7 @@ static inline struct rmid_entry *__rmid_entry(u32 idx) static int __rmid_read(u32 rmid, enum resctrl_event_id eventid, u64 *val) { - u32 _rmid, _eventid; - u64 msr_val; + u64 msr_val, seq; /* * As per the SDM, when IA32_QM_EVTSEL.EvtID (bits 7:0) is configured @@ -188,15 +191,16 @@ 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 interrupted by - * another call, meaning the QM_CTR value may belong to a different - * event. + * A per-cpu sequence counter is incremented each time QM_EVTSEL is + * written. This is used to detect if this function was interrupted by + * another call without re-reading the MSRs. Retry the MSR read when + * this happens as the QM_CTR value may belong to a different event. */ do { + seq = this_cpu_inc_return(qm_evtsel_seq); wrmsr(MSR_IA32_QM_EVTSEL, eventid, rmid); rdmsrl(MSR_IA32_QM_CTR, msr_val); - rdmsr(MSR_IA32_QM_EVTSEL, _eventid, _rmid); - } while (eventid != _eventid || rmid != _rmid); + } while (seq != this_cpu_read(qm_evtsel_seq)); if (msr_val & RMID_VAL_ERROR) return -EIO; ------------------%<------------------