Received: by 2002:a05:7412:518d:b0:e2:908c:2ebd with SMTP id fn13csp534232rdb; Thu, 5 Oct 2023 13:14:08 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGuqz9Q9dMpEZde7BGTQ3KFy4jGhBhzCSD4imJGz+tLVZxB269VejGnxjk5zad1U40QQyZQ X-Received: by 2002:a17:902:ed54:b0:1b8:76ce:9dab with SMTP id y20-20020a170902ed5400b001b876ce9dabmr6183156plb.41.1696536847851; Thu, 05 Oct 2023 13:14:07 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1696536847; cv=pass; d=google.com; s=arc-20160816; b=bYfYrvefl3qZQh/EgxDmSrGnwPqgrlkGLp0yS7GUqfngU24K9Rywdnc7euTfot7gfQ tUWg8KEHs+N1Kt1HisAQ7hX/2b/LaPifN6PEc4jXE4V1+VeU1iuUw3rhg22ALcX3erD3 ebI/L6cF7pcBkHd6T1OEZbp8Cz4vjFEI7/Q/2a9Umh5YcPP3jLwHI0kg18HD0tgvRlEw FLCN2/DIlf1OavRP4gYu+Pk89BHFfvo6MVIPZdX8917lbSqFRk/SVZVgY64xjcNPjSeo IX7CTIejMzXSNc+7JKMy19auPT8BlddqAfeGTATEkGydTz18dJslj+U+ZmaTVrvQi/fW LTlw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:content-transfer-encoding :in-reply-to:from:references:cc:to:content-language:subject:reply-to :user-agent:date:message-id:dkim-signature; bh=nNeLIjsDi0LgiIpVcKF4IAsmdeIseSBZuE52iSzYl5g=; fh=nHe58DcWeCzuC3s+kEoLUAitmv89lT2czrir+f8RX5g=; b=jnQ49LkD9cCzHNBlNLyvtiL1iQNNG6AVbYFCQGdPtieKnf3Pboa9QROu/JbQuJpKzD +StbeDyRHI+feHvpFr+CV41pZWqCrj0nKZ3w/kiTjjxe4hfgvQ4b6ZSWQMd5ze9pvLO7 EydGbvkL4BP+2duy0ZD5b8krTWc1VHFzRet90hJrS623TOmrSLksNsDRijTs/FeRVt20 Hmd3Sk7+X7oE80cB5XESZ4EHzcUOaAPBwY2cEq94Nekuufq9ti0VsEmrISF+tZkQJzpY exfKnu3VGquZtJqO1l6j0hPjuDq8Y694lMOEryEaRbB1ATmvAD9t0NSHlnHyL8WXdNfD s/iA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=paiPyU6T; arc=pass (i=1 spf=pass spfdomain=amd.com dkim=pass dkdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Return-Path: Received: from fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id jj10-20020a170903048a00b001b222cd9826si2058095plb.349.2023.10.05.13.14.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Oct 2023 13:14:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@amd.com header.s=selector1 header.b=paiPyU6T; arc=pass (i=1 spf=pass spfdomain=amd.com dkim=pass dkdomain=amd.com dmarc=pass fromdomain=amd.com); spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amd.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id EE4A8807F4A4; Thu, 5 Oct 2023 13:14:04 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229852AbjJEUNz (ORCPT + 99 others); Thu, 5 Oct 2023 16:13:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40412 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229932AbjJEUNy (ORCPT ); Thu, 5 Oct 2023 16:13:54 -0400 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2043.outbound.protection.outlook.com [40.107.93.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4FE48B6 for ; Thu, 5 Oct 2023 13:13:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Hrn6Ssj71MK7CUCRRznSD/1HVNgo3GSoff0S3vpMQ/6Argr23AOJJT5LU2KwziscG6nqGy0RH9hx87CtexBZF3Nrg4kbu9Hf79ADzESw/cKAw/ff9fwihs/WPSt/DbI+8hJNMFdQISUGtMFJjj296zempnzvsXQqQ9/Rw0eKquZZxP9PZJ/zD5nr8aQ5OQbiBh3HzDuyXbniuW3n2M+AFUo+aQKpFCfLQoT+AwW0bSZET7Vti+QeJQSklzR0Jmn99nRaRO+2TgTO8Pk7nnEUtCpbFRmHPHhfSNG3g+/a6IZJ/JXb2JAVXtLm31wJA98j56TIOgxP1WS0W+W9di5IjQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=nNeLIjsDi0LgiIpVcKF4IAsmdeIseSBZuE52iSzYl5g=; b=dJOW9oXyoXR2H2li1CQcViR/89ZULQOTt+FxaIir7rPGeZIYLv/GqvYNS4f5AEPfun7YQE6D1gjGFdh3v644Sh/OfUL+I0775rkbNXNIH+xsKvwgY48EYxd6bg5wR0Itq8stgWL3iRQiqNKqVXyrDYL+xVrSJ853xY0KePiXWpiwaK91kjtdI0JWHmSX+dwNobwQEtdoIBzUP6J3WzRo9QkT+nN3FySEVUPiw8H/xgnuDn22ErHcylj7gcRykOVDSMznzDVIQvEwCV83vHg6EsaoIGSnHGLgetEBmbO+jbIMnRVLn/yu1w3BqYBrDAD3jPiHSDQw8pHxj5OYh7TFvA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=nNeLIjsDi0LgiIpVcKF4IAsmdeIseSBZuE52iSzYl5g=; b=paiPyU6Tgniny+0NYnhIYnYw0qQ2VTzalUqBg2Qc/cLm/WEoQNwWpZe+8vpvA1/XCd5/ncL0MBCCexBC4feiUrs49gHdfywY+xjvYMCdVNfGK2tbXQww1ggzRfXt5dUfBBegUrYSss4PHkXdqWMGbj+UcEx5k1hs63Ef0E5EYdE= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=amd.com; Received: from MW3PR12MB4553.namprd12.prod.outlook.com (2603:10b6:303:2c::19) by SA0PR12MB4510.namprd12.prod.outlook.com (2603:10b6:806:94::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6838.35; Thu, 5 Oct 2023 20:13:47 +0000 Received: from MW3PR12MB4553.namprd12.prod.outlook.com ([fe80::b5d1:8b74:fe73:bd39]) by MW3PR12MB4553.namprd12.prod.outlook.com ([fe80::b5d1:8b74:fe73:bd39%6]) with mapi id 15.20.6838.033; Thu, 5 Oct 2023 20:13:47 +0000 Message-ID: <2b5e62a3-3b5c-c98a-6b5e-909a021a8d4e@amd.com> Date: Thu, 5 Oct 2023 15:13:42 -0500 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.5.0 Reply-To: babu.moger@amd.com Subject: Re: [PATCH v6 10/24] x86/resctrl: Allocate the cleanest CLOSID by searching closid_num_dirty_rmid Content-Language: en-US To: James Morse , x86@kernel.org, linux-kernel@vger.kernel.org Cc: 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 , peternewman@google.com, dfustini@baylibre.com, amitsinght@marvell.com References: <20230914172138.11977-1-james.morse@arm.com> <20230914172138.11977-11-james.morse@arm.com> From: "Moger, Babu" In-Reply-To: <20230914172138.11977-11-james.morse@arm.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-ClientProxiedBy: DM6PR02CA0151.namprd02.prod.outlook.com (2603:10b6:5:332::18) To MW3PR12MB4553.namprd12.prod.outlook.com (2603:10b6:303:2c::19) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: MW3PR12MB4553:EE_|SA0PR12MB4510:EE_ X-MS-Office365-Filtering-Correlation-Id: 62d11312-bd4c-4da3-88b4-08dbc5df950b X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: m1/hxNwdGPJLBoVlqr5sZYZZ3ro1PSO3t/6ZZGANw5t34wJ2PP6IHfrRZ0yeA/eANL7rYKlC1S7e2XIy+K/HxWlXtVZRmKa3nNzoaU2vIXLt28HFr2s9Zjl3INKf9dxyxe0d8Vehm4S4Sie9XpZSfJPlIkYm+t51X0ExbPNsdyuu+p5K5I9b8+NbgQx7YiYIhO8+KzoFuwZqFUAcS837EDKAOVbvsEnenNsG+gVavur1WxazBNm9NdDbF9JGv/q4GUHetLfc0eGVQvztiUMtseUGltOsmI6YQf9qmEQiau5fKy9x7ndvfjLhBEv/OnRzmOcnXu2ISitMqeBexfwOPWILgujQrl4rE51iWiO1I5T1Gjg+BtP3f1ywEfBKljV+/STBEkK9BG7JaQl8UebI5m2N0BOBh7SGEUTLLWmJHtv+956kDPJ7q2s+T43hm5BMMW0rl9gSoajN9gWWObN6OcDSg+t5NxGC4+ufVoKflUnnA39udJQ/RUZAedDkeTAirKxbv1ubusqxZ1q9SbctYTXyqcP/xIN8AR2b13COcutlZYgIQfcrr9GYZ9LM/+Lh7M0C5iXdK98MIkBlBqGkKvvl/nCGXgMnWCoi6VsWKcwVDG3IjcqLJz3UjsBUTKQuEwXcHT7dt04ozLRh5tPbyfHrb92TcDHUckwxOY828tQ= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:MW3PR12MB4553.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(396003)(39860400002)(366004)(346002)(376002)(136003)(230922051799003)(451199024)(1800799009)(186009)(64100799003)(31686004)(41300700001)(66946007)(66556008)(5660300002)(478600001)(66476007)(83380400001)(54906003)(7416002)(6666004)(6486002)(316002)(53546011)(8936002)(8676002)(4326008)(6506007)(6512007)(2616005)(2906002)(26005)(38100700002)(36756003)(31696002)(14143004)(45980500001)(43740500002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?B?NFZBNU11eVQxM2x1ZEhId2lNSUIvMU9Zc01uYnJ5bStSRFhnUzg5QUhHOWUr?= =?utf-8?B?ZW0vTXhxMzllL08vVkE4SWd2VytweVlUZGFHaXJsd1FLZ0Fwb0FkeHRrbG9L?= =?utf-8?B?WGdzSjhzendsREowTUJvYWh6Y0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A1ZVc4YkJYeGs1bGx1NXQ5d3AxS3JZb01YUUxJQWZSUWt3Z2JwY1N3?= =?utf-8?B?N2ZLOFJNd1lHRU1HdHZVWXFJS21PQm9tNnFvUnlDcE82cHdXTkxTajI1YVdn?= =?utf-8?B?b2NzL1pFS0ZCNVF1YmdKWlFXS0VzUzVmclVwSzhuL2QwNHVsMWs1VExLNDhH?= =?utf-8?Q?Qocc=3D?= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 62d11312-bd4c-4da3-88b4-08dbc5df950b X-MS-Exchange-CrossTenant-AuthSource: MW3PR12MB4553.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 05 Oct 2023 20:13:47.4014 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: zy/yU7DK9zwr0SjL1YdaPryynFfCX0hsGtPxUxB1c6/Pp/hsYz+jhWeJfoFKpJW8 X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4510 X-Spam-Status: No, score=-2.3 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Thu, 05 Oct 2023 13:14:05 -0700 (PDT) Hi James, On 9/14/2023 12:21 PM, 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. > > Instead of allocating the first free CLOSID, on architectures where > CONFIG_RESCTRL_RMID_DEPENDS_ON_COSID is enabled, search > closid_num_dirty_rmid[] to find the cleanest CLOSID. > > The CLOSID found is returned to closid_alloc() for the free list > to be updated. > > Reviewed-by: Shaopeng Tan > Tested-by: Shaopeng Tan > Tested-By: Peter Newman > Signed-off-by: James Morse > --- > Changes since v4: > * Dropped stale section from comment > --- > arch/x86/kernel/cpu/resctrl/internal.h | 2 ++ > arch/x86/kernel/cpu/resctrl/monitor.c | 42 ++++++++++++++++++++++++++ > arch/x86/kernel/cpu/resctrl/rdtgroup.c | 19 +++++++++--- > 3 files changed, 58 insertions(+), 5 deletions(-) > > diff --git a/arch/x86/kernel/cpu/resctrl/internal.h b/arch/x86/kernel/cpu/resctrl/internal.h > index ad6e874d9ed2..f06d3d3e0808 100644 > --- a/arch/x86/kernel/cpu/resctrl/internal.h > +++ b/arch/x86/kernel/cpu/resctrl/internal.h > @@ -558,5 +558,7 @@ void rdt_domain_reconfigure_cdp(struct rdt_resource *r); > void __init thread_throttle_mode_init(void); > void __init mbm_config_rftype_init(const char *config); > void rdt_staged_configs_clear(void); > +bool closid_allocated(unsigned int closid); > +int resctrl_find_cleanest_closid(void); > > #endif /* _ASM_X86_RESCTRL_INTERNAL_H */ > diff --git a/arch/x86/kernel/cpu/resctrl/monitor.c b/arch/x86/kernel/cpu/resctrl/monitor.c > index 0c783301d106..0bbed8c62d42 100644 > --- a/arch/x86/kernel/cpu/resctrl/monitor.c > +++ b/arch/x86/kernel/cpu/resctrl/monitor.c > @@ -388,6 +388,48 @@ static struct rmid_entry *resctrl_find_free_rmid(u32 closid) > return ERR_PTR(-ENOSPC); > } > > +/** > + * resctrl_find_cleanest_closid() - Find a CLOSID where all the associated > + * RMID are clean, or the CLOSID that has > + * the most clean RMID. > + * > + * MPAM's equivalent of RMID are per-CLOSID, meaning a freshly allocated CLOSID > + * may not be able to allocate clean RMID. To avoid this the allocator will > + * choose the CLOSID with the most clean RMID. > + * > + * When the CLOSID and RMID are independent numbers, the first free CLOSID will > + * be returned. > + */ > +int resctrl_find_cleanest_closid(void) > +{ > + u32 cleanest_closid = ~0, iter_num_dirty; Just naming num_dirty should have been fine.  I will leave it you. > + int i = 0; > + > + lockdep_assert_held(&rdtgroup_mutex); > + > + if (!IS_ENABLED(CONFIG_RESCTRL_RMID_DEPENDS_ON_CLOSID)) > + return -EIO; > + > + for (i = 0; i < closids_supported(); i++) { > + if (closid_allocated(i)) > + continue; > + > + iter_num_dirty = closid_num_dirty_rmid[i]; > + if (iter_num_dirty == 0) > + return i; > + > + if (cleanest_closid == ~0) > + cleanest_closid = i; > + > + if (iter_num_dirty < closid_num_dirty_rmid[cleanest_closid]) > + cleanest_closid = i; > + } > + > + if (cleanest_closid == ~0) > + return -ENOSPC; > + return cleanest_closid; Line before the return looks clean. * if (cleanest_closid == ~0) + return -ENOSPC; + + return cleanest_closid; Thanks Babu > +} > + > /* > * For MPAM the RMID value is not unique, and has to be considered with > * the CLOSID. The (CLOSID, RMID) pair is allocated on all domains, which > diff --git a/arch/x86/kernel/cpu/resctrl/rdtgroup.c b/arch/x86/kernel/cpu/resctrl/rdtgroup.c > index fa449ee0d1a7..1f8f1c417a4b 100644 > --- a/arch/x86/kernel/cpu/resctrl/rdtgroup.c > +++ b/arch/x86/kernel/cpu/resctrl/rdtgroup.c > @@ -132,11 +132,20 @@ static void closid_init(void) > > static int closid_alloc(void) > { > - u32 closid = ffs(closid_free_map); > + u32 closid; > + int err; > > - if (closid == 0) > - return -ENOSPC; > - closid--; > + if (IS_ENABLED(CONFIG_RESCTRL_RMID_DEPENDS_ON_CLOSID)) { > + err = resctrl_find_cleanest_closid(); > + if (err < 0) > + return err; > + closid = err; > + } else { > + closid = ffs(closid_free_map); > + if (closid == 0) > + return -ENOSPC; > + closid--; > + } > clear_bit(closid, &closid_free_map); > > return closid; > @@ -154,7 +163,7 @@ void closid_free(int closid) > * Return: true if @closid is currently associated with a resource group, > * false if @closid is free > */ > -static bool closid_allocated(unsigned int closid) > +bool closid_allocated(unsigned int closid) > { > return !test_bit(closid, &closid_free_map); > }