Received: by 2002:a05:7412:1703:b0:e2:908c:2ebd with SMTP id dm3csp440756rdb; Thu, 24 Aug 2023 10:19:44 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHCD5lHPnmRUdqePLU+J7aYWUQOKUw7dAv41gHPB46iAuy0PmO0d1iGSAKW/MpRpmGrgiNa X-Received: by 2002:a2e:914c:0:b0:2bc:c4ae:c565 with SMTP id q12-20020a2e914c000000b002bcc4aec565mr8559246ljg.28.1692897583759; Thu, 24 Aug 2023 10:19:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692897583; cv=none; d=google.com; s=arc-20160816; b=lQYnGMYNBfk0n5Hj7AOPR0ma7gcZsb5O1u3HE2UQMp9/We5hmMtk7FVB/7Sn+TR7WL +9aYaBozvIiW8Jh+9qTdxHfUHcaMEr3vGX46UbbSZ7zZMOZioi3p3ChZb4DlHKsPafuM iNijd0lx1+bF+9JGryudX4Bl707Hj06e4CEpIoZgtm00jZNAjkrrAfZmMtoJEWOOaK/y 0TdlbW7CSe2ZwQvZyLpYI2o2K4cWFMi4iGHwuVFW5nveZSGHfEQo9vam7RNy05m96yMI xItq5k9pA3MTIltttl8O9rYzSY9rOlCzJG0fu02gXkprO5XF1De0y2QkZIV3uerMbYcZ Y3Yg== 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=reYYoAziyBf/M6Jb1y8VlcklMAv0PuVxOCVPl3MPCCE=; fh=fwWQ0fhuYIHAmlzAKQJpGsw7r5YVIaWrGc3CKj8oIlE=; b=i7V+wjaZVBIUjRTu3hScnQ47pkPrNg7OxDjnWyu0oNHXIs1ONLEHIvQF2TBQ82WX52 kgpiu3E/vUI3wfZ/vkfD0187ar1x5mD/OCtuMdS0Uh6hO+GGF6v+4lWwL1+KtCHT1ylE 0IpuyTx3Er9ESg4+RevzxG8jOFDBhkWKSI2a9SY/NIzMc76y9H6pr+MjykuPBflara7T ZddNm1WvntUoJoK4VE9E90iq6QcOb3K0Zpiq//PcaEJESj2b83WEJ1cLa3LwAxtWbvxM owtaIokIyAi83GkAbUtO3CdYaTg2COtmlfPmExn8cVnuQ519i7SzBD+nDYgc/QqOKN8r tz4g== 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 j3-20020a17090686c300b0099bd03ad4fdsi10357227ejy.979.2023.08.24.10.19.17; Thu, 24 Aug 2023 10:19:43 -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 S242378AbjHXQyP (ORCPT + 99 others); Thu, 24 Aug 2023 12:54:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56968 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S242752AbjHXQyB (ORCPT ); Thu, 24 Aug 2023 12:54:01 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 3A4211989 for ; Thu, 24 Aug 2023 09:53:59 -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 1B4831007; Thu, 24 Aug 2023 09:54:39 -0700 (PDT) Received: from [10.1.197.60] (eglon.cambridge.arm.com [10.1.197.60]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 24F133F740; Thu, 24 Aug 2023 09:53:56 -0700 (PDT) Message-ID: Date: Thu, 24 Aug 2023 17:53:49 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux aarch64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0 Subject: Re: [PATCH v5 06/24] x86/resctrl: Track the number of dirty RMID a CLOSID has Content-Language: en-GB To: Fenghua Yu , x86@kernel.org, linux-kernel@vger.kernel.org Cc: 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 , peternewman@google.com, dfustini@baylibre.com References: <20230728164254.27562-1-james.morse@arm.com> <20230728164254.27562-7-james.morse@arm.com> <4d8773f7-4219-0bc5-e7df-1be81cba05e7@intel.com> From: James Morse In-Reply-To: <4d8773f7-4219-0bc5-e7df-1be81cba05e7@intel.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.8 required=5.0 tests=BAYES_00,NICE_REPLY_A, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_NONE 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 Fenghua On 15/08/2023 03:37, Fenghua Yu wrote: > On 7/28/23 09:42, James Morse wrote: >> MPAM's PMG bits extend its PARTID space, meaning the same PMG value can be >> used for different control groups. >> >> This means once a CLOSID is allocated, all its monitoring ids may still be >> dirty, and held in limbo. >> >> Keep track of the number of RMID held in limbo each CLOSID has. This will >> allow a future helper to find the 'cleanest' CLOSID when allocating. >> >> The array is only needed when CONFIG_RESCTRL_RMID_DEPENDS_ON_CLOSID is >> defined. This will never be the case on x86. >> diff --git a/arch/x86/kernel/cpu/resctrl/monitor.c b/arch/x86/kernel/cpu/resctrl/monitor.c >> index de91ca781d9f..44addc0126fc 100644 >> --- a/arch/x86/kernel/cpu/resctrl/monitor.c >> +++ b/arch/x86/kernel/cpu/resctrl/monitor.c >> @@ -43,6 +43,13 @@ struct rmid_entry { >>    */ >>   static LIST_HEAD(rmid_free_lru); >>   > > Better to add: > > #if CONFIG_RESCTRL_RMID_DEPENDS_ON_CLOSID >> +/** >> + * @closid_num_dirty_rmid    The number of dirty RMID each CLOSID has. >> + * Only allocated when CONFIG_RESCTRL_RMID_DEPENDS_ON_CLOSID is defined. >> + * Indexed by CLOSID. Protected by rdtgroup_mutex. >> + */ >> +static int *closid_num_dirty_rmid; > #endif > > Then the global variable won't exist on x86 to avoid confusion and space. > > Some code related to the CONFIG also needs to be changed accordingly. Uh-huh, that would force me to put #ifdef warts all over the code that accesses that variable. Modern compilers are really smart. Because this is static, the compiler is free to remove it if there are no users. All the users are behind if(IS_ENABLED()), meaning the compilers dead-code elimination will cull the lot, and this variable too: morse@eglon:~/kernel/mpam/build_x86_64/fs/resctrl$ nm -s monitor.o | grep closid_num_dirty morse@eglon:~/kernel/mpam/build_arm64/fs/resctrl$ nm -s monitor.o | grep closid_num_dirty 0000000000000000 b closid_num_dirty_rmid morse@eglon:~/kernel/mpam/build_arm64/fs/resctrl$ Using #ifdef is not only ugly - it prevents the compiler from seeing all the code, so the CI build systems get worse coverage. >> + >>   /** >>    * @rmid_limbo_count     count of currently unused but (potentially) >>    *     dirty RMIDs. >> @@ -285,6 +292,17 @@ int resctrl_arch_rmid_read(struct rdt_resource *r, struct >> rdt_domain *d, >>       return 0; >>   } >>   +static void limbo_release_entry(struct rmid_entry *entry) >> +{ >> +    lockdep_assert_held(&rdtgroup_mutex); >> + >> +    rmid_limbo_count--; >> +    list_add_tail(&entry->list, &rmid_free_lru); >> + >> +    if (IS_ENABLED(CONFIG_RESCTRL_RMID_DEPENDS_ON_CLOSID)) >> +        closid_num_dirty_rmid[entry->closid]--; > > > Maybe define some helpers (along with other similar ones) in resctrl.h like this: > > #ifdef CONFIG_RESCTRL_RMID_DEPENDS_ON_CLOSID > static inline void closid_num_dirty_rmid_dec(struct rmid_entry *entry) > { >         closid_num_dirty_rmid[entry->closid]--; > } > ... > #else > static inline void closid_num_dirty_rmid_dec(struct rmid_entry *unused) > { > } > ... > #endif > > Then directly call the helper here: > > +        closid_num_dirty_rmid_dec(entry); > > On x86 this is noop without and the compiler knows this. > occupy any space Literally more lines of code. > and cleaner code. Maybe, this would hide the IS_ENABLED() check - but moving that out as a single use helper would required closid_num_dirty_rmid[] to be exported from this file - which would prevent it being optimised out. You'd get the result you were trying to avoid. >> +} >> + >>   /* >>    * Check the RMIDs that are marked as busy for this domain. If the >>    * reported LLC occupancy is below the threshold clear the busy bit and >> @@ -321,10 +339,8 @@ void __check_limbo(struct rdt_domain *d, bool force_free) >>             if (force_free || !rmid_dirty) { >>               clear_bit(idx, d->rmid_busy_llc); >> -            if (!--entry->busy) { >> -                rmid_limbo_count--; >> -                list_add_tail(&entry->list, &rmid_free_lru); >> -            } >> +            if (!--entry->busy) >> +                limbo_release_entry(entry); >>           } >>           cur_idx = idx + 1; >>       } >> @@ -391,6 +407,8 @@ static void add_rmid_to_limbo(struct rmid_entry *entry) >>       u64 val = 0; >>       u32 idx; >>   +    lockdep_assert_held(&rdtgroup_mutex); >> + >>       idx = resctrl_arch_rmid_idx_encode(entry->closid, entry->rmid); >>         entry->busy = 0; >> @@ -416,9 +434,11 @@ static void add_rmid_to_limbo(struct rmid_entry *entry) >>       } >>       put_cpu(); >>   -    if (entry->busy) >> +    if (entry->busy) { >>           rmid_limbo_count++; >> -    else >> +        if (IS_ENABLED(CONFIG_RESCTRL_RMID_DEPENDS_ON_CLOSID)) >> +            closid_num_dirty_rmid[entry->closid]++; > > Ditto. > >> +    } else >>           list_add_tail(&entry->list, &rmid_free_lru); >>   } >>   @@ -782,13 +802,28 @@ void mbm_setup_overflow_handler(struct rdt_domain *dom, unsigned >> long delay_ms) >>   static int dom_data_init(struct rdt_resource *r) >>   { >>       u32 idx_limit = resctrl_arch_system_num_rmid_idx(); >> +    u32 num_closid = resctrl_arch_get_num_closid(r); >>       struct rmid_entry *entry = NULL; >>       u32 idx; >>       int i; >>   +    if (IS_ENABLED(CONFIG_RESCTRL_RMID_DEPENDS_ON_CLOSID)) { >> +        int *tmp; >> + >> +        tmp = kcalloc(num_closid, sizeof(int), GFP_KERNEL); >> +        if (!tmp) >> +            return -ENOMEM; >> + >> +        mutex_lock(&rdtgroup_mutex); > data_init() is called in __init. No need to lock here, right? __init code can still race with other callers - especially as there are CPUHP_AP_ONLINE_DYN cpuhp callbacks that are expected to sleep. This is about ensuring all accesses to those global variables are protected by the lock. This saves me a memory ordering headache. Thanks, James