Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp7429909rwd; Tue, 6 Jun 2023 10:33:21 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7rdcQXBVL6lRI3AxbvC1npbSUmD8LFjbgkcTYHAQLR5Dn8TR6siOg7J3nJ92yG0RSTR8fc X-Received: by 2002:a05:6a20:a221:b0:107:10b6:4c84 with SMTP id u33-20020a056a20a22100b0010710b64c84mr346159pzk.21.1686072801091; Tue, 06 Jun 2023 10:33:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686072801; cv=none; d=google.com; s=arc-20160816; b=Oa2q/QGSr6S96A0e0dx4yC0S01dw2Q02FScCD7IAHFe9sMJv1QKExFSww1++FMhdKb nRTeCVhpjpJ2DY6Q+k2DgKCf8olZeFK2g0aHZLnR/HVBKaclBrTVAUxaFoXQt9IiBJXz rFSVM2byZl43shrhCTY6KddAorGJZ48uB8WqyjavaHvUCYtYr2tpO5STc4DdNziAAGL0 D0pOoDH9nwC+RXpj6mC56kVkUCnNvBpgbjdSUP6iL60jeYA/xVASd3b+CNpBJQTW6uez r/lcipGixXfP9zxQ8ho92FzFvIdRggofYclahR6JCjz2iW9s7k9cjgDo6HnzH3nzBvI0 yo6A== 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=Eq4SfQlZNkJGtGC/TCgQOXQmCzw8JYchbZgFXi2Sn70=; b=pDbJKY6ozbTPn7JmxHd73wlK4DVpTUpp8t64xH4xiomztVKZ4/MWHWJoLo15UhGyHJ YvK9aT30ez8bfEQ+ewMHI2rofSxC31p0Ecn4wHnc06aLowkMDskyXc7+LY4Cx5I7D6Wy eGpyhUjpa+aeyoXGWYh6tGsE24u2lV8BjwBAAB6CUvnjInaMW9CNwyR4PkKiRiWB0pWP kGPv5nGy2lSRXM8Cl24KBQeQxdi+K5pGeBAM88i/77CvatS+yCiIWDytFQKIEJBAO95N 5yWqS+Rh0OEmF8iMF+r1OTMXBnoc+X4Ktf7PGgWw23O43knsstruNlH15Php7WtrrB/b plBQ== 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 j10-20020a637a4a000000b0053b8f80c663si7653343pgn.2.2023.06.06.10.33.07; Tue, 06 Jun 2023 10:33:21 -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 S238354AbjFFRD4 (ORCPT + 99 others); Tue, 6 Jun 2023 13:03:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49696 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233340AbjFFRDy (ORCPT ); Tue, 6 Jun 2023 13:03:54 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id BF40310C2 for ; Tue, 6 Jun 2023 10:03:52 -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 864612F4; Tue, 6 Jun 2023 10:04:37 -0700 (PDT) Received: from [10.1.196.47] (eglon.cambridge.arm.com [10.1.196.47]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 5C1233F663; Tue, 6 Jun 2023 10:03:49 -0700 (PDT) Message-ID: <7ff7fcf9-725b-fb3d-43f3-b80a1df3a001@arm.com> Date: Tue, 6 Jun 2023 18:03:43 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux aarch64; rv:102.0) Gecko/20100101 Thunderbird/102.10.0 Subject: Re: [PATCH v4 12/24] x86/resctrl: Make resctrl_arch_rmid_read() retry when it is interrupted Content-Language: en-GB 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> From: James Morse In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,NICE_REPLY_A, RCVD_IN_DNSWL_MED,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 06/06/2023 09:49, Peter Newman wrote: > On Thu, May 25, 2023 at 8:03 PM James Morse wrote: >> +interrupted: >> + am = get_arch_mbm_state(hw_dom, rmid, eventid); >> + if (am) >> + start_msr_val = atomic64_read(&am->prev_msr); >> + >> ret = __rmid_read(rmid, eventid, &msr_val); >> if (ret) >> return ret; >> >> am = get_arch_mbm_state(hw_dom, rmid, eventid); >> if (am) { >> - am->chunks += mbm_overflow_count(am->prev_msr, msr_val, >> - hw_res->mbm_width); >> - chunks = get_corrected_mbm_count(rmid, am->chunks); >> - am->prev_msr = msr_val; >> + old_msr_val = atomic64_cmpxchg(&am->prev_msr, start_msr_val, >> + msr_val); >> + if (old_msr_val != start_msr_val) >> + goto interrupted; >> + >> + chunks = mbm_overflow_count(start_msr_val, msr_val, >> + hw_res->mbm_width); >> + atomic64_add(chunks, &am->chunks); >> + >> + chunks = get_corrected_mbm_count(rmid, >> + atomic64_read(&am->chunks)); >> } else { >> chunks = msr_val; >> } > > 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; ----------------------------%<---------------------------- Thanks! James