Received: by 2002:a05:7208:13ce:b0:7f:395a:35b6 with SMTP id r14csp209734rbe; Wed, 28 Feb 2024 18:13:23 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCV54nCJGZ+BF9YmQBzu6y11dgTivnrsJONjSXboF4xg+Uhy16rpk9hsypAc1coGte3GdUve+WtUrZkmiJB+DanFMOm4HsjsHXTxzV0Uow== X-Google-Smtp-Source: AGHT+IGyGXyPy3Xtce/mPl8B0f7MABzyi485IaAQ9wddq+/Bcav5ZsA9b1+M3GpU5iX9tOpoIyJv X-Received: by 2002:a05:6870:f722:b0:21f:c705:7d8c with SMTP id ej34-20020a056870f72200b0021fc7057d8cmr763864oab.26.1709172803242; Wed, 28 Feb 2024 18:13:23 -0800 (PST) Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id m8-20020a635808000000b005cdfaea88adsi342882pgb.774.2024.02.28.18.13.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Feb 2024 18:13:23 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-86033-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@outlook.com header.s=selector1 header.b=H8KxVPLe; arc=fail (signature failed); spf=pass (google.com: domain of linux-kernel+bounces-86033-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-86033-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=outlook.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id D347128868A for ; Thu, 29 Feb 2024 02:13:22 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A07F536B01; Thu, 29 Feb 2024 02:13:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b="H8KxVPLe" Received: from HK2PR02CU002.outbound.protection.outlook.com (mail-eastasiaazolkn19012005.outbound.protection.outlook.com [52.103.64.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 21251364D2; Thu, 29 Feb 2024 02:13:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.64.5 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709172796; cv=fail; b=sQeegtwSkOwKUJbXE9YMvlmXhTckrx7J6i8s4c+C9kViPjgfiv3wrR2YxZ+d2NeOU+AWrU3p9GELQLnJO5qF75icIavhO2OSa01GrRLPGsiuLMnYF7cJhO24LcM1pDbYw/IQp6jiVnexjKJVzUXhsmrqq0bJuzoifYiR/d7nx48= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709172796; c=relaxed/simple; bh=wbi4srXdKHrjmv14pdnng9DUZlWPOnDlrYoVMA1M4Jo=; h=Message-ID:Date:Subject:To:Cc:References:From:In-Reply-To: Content-Type:MIME-Version; b=tSGDMbO01N+RiCkoETxDcaQC8UeKiI4fmz+VRdAiu9IBstCIsp6w/+BfACU41+hFoIyF5yZom+fnrT123KNGAq3qgsOS7gnWAJoQ2xHeQPZhosssqyDsvPcn5i3XinhDDgVRAfbzEJMdXMhEoadgjh9EvuFRIwp7bZsPtxD16Hw= ARC-Authentication-Results:i=2; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com; spf=pass smtp.mailfrom=outlook.com; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b=H8KxVPLe; arc=fail smtp.client-ip=52.103.64.5 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=outlook.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=outlook.com ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=I4vX3Ip5EDSWlYj84GOntbZJQX6dp6ugzohjMcjhcRhvaxnY5itQfa6cToWpvONPzJndyrucQv2LkJ2Y9rAhAy0QE/iekVudrC1w7UMNOrVkMhZEV1J5yP5/Ib/WPWXQVpi/HSoaGLPdJuGuKOwN9S0zNDuF8sDxqBt9F7qPFhkd+152+NcLqHqsAailC3BQZyXmoLJ67nLl5XNQdDSGKv6XF2FNSsXdFwTETSG+u7uw4GgVw1k0nOsqKYZwczoYn9gvgmeNti+RFbCjFUgfstK1y8TqvQLhcY9s9/3hCk6ohBzPzpijyHoYizP/zQkfObcef24g0P1ZAR/8B3GDOA== 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=uqBYaFC5VZzEwZ6JjvrVcXpOLfE04WcvcEvHIG0mrtA=; b=BfFRjTgsxyPd6sMOLNH/iFJyT16ViVLBg+yGs2L4y38mDkVDp6X6GQ1Y1L3an0CON6jAHjCbZseg0xtGxs3tBYoLNwlvGFQpCP0mcjOuqWuJ8g0fYJDI+k2Qlbnm17WfrLrIUlCkb+GDuslHddJon0xPn1WS3pO4wJ4yBWYf0ovE5LPmm0T/aZz1O4T3rs8NMOFlqtkZEgcvYbD/u6dBvh/t9yObzCHOTeoq1AQPtXUK5/0P77bGQRHVo4U7u3iq2nHJd8JpTaTJIpThzZwyy01vFeiaxeQsFSChFMq9Bj72sHrnCwEFe14VoijPmkq9TM3ndn6XWR0T5V1/SbLrkA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=uqBYaFC5VZzEwZ6JjvrVcXpOLfE04WcvcEvHIG0mrtA=; b=H8KxVPLe4Co4rkYnzQBpSsCK3V9hrleLKML93EaBmjKjrcEmrGvMA480ioE0XlYTSw2wVMi6N9nei5Q1SlfnNcRrZNajrcXA8GGkdFgd1Yuxnz8hCBp/rm+17MTmIr2sioGfvfUCtGCa85Mp5vnSbcMtVy1d2TzNGjnIvzCCFTcvZ8ihOAcHx2CiX8q5yzGeKXV6/FU0MwFkHxgclryqPSD3r5SNqtMULaJVzRw8J6gsLHTJl83/B3/VZezdDBO2BZXFoY6NUpCQh67Zi50DcIydbgmyGgUYNjVFyMDnXPBqBqo5GtIlp3Xnch60CDKMwVEi9h5akK+e+5cIdh9RVw== Received: from SEZPR06MB6959.apcprd06.prod.outlook.com (2603:1096:101:1ed::14) by SEZPR06MB6874.apcprd06.prod.outlook.com (2603:1096:101:18d::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7316.41; Thu, 29 Feb 2024 02:13:10 +0000 Received: from SEZPR06MB6959.apcprd06.prod.outlook.com ([fe80::53da:a8a:83cb:b9ad]) by SEZPR06MB6959.apcprd06.prod.outlook.com ([fe80::53da:a8a:83cb:b9ad%4]) with mapi id 15.20.7316.035; Thu, 29 Feb 2024 02:13:10 +0000 Message-ID: Date: Thu, 29 Feb 2024 10:13:04 +0800 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH RESEND] clk: set initial best mux parent to current parent when determining rate Content-Language: en-US To: Stephen Boyd , Michael Turquette , Yang Xiwen via B4 Relay Cc: linux-clk@vger.kernel.org, linux-kernel@vger.kernel.org References: <20240224-mux-v1-1-608cc704ef43@outlook.com> <0db752d31016823ccd3799925255a74a.sboyd@kernel.org> From: Yang Xiwen In-Reply-To: <0db752d31016823ccd3799925255a74a.sboyd@kernel.org> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-TMN: [6su2EQ3dWBncFc+3eQ9kVG5T8raO4FBGT4BrUSzE2msZNvijy5tkdg96tPHEN7H9] X-ClientProxiedBy: KL1P15301CA0063.APCP153.PROD.OUTLOOK.COM (2603:1096:820:3d::21) To SEZPR06MB6959.apcprd06.prod.outlook.com (2603:1096:101:1ed::14) X-Microsoft-Original-Message-ID: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: SEZPR06MB6959:EE_|SEZPR06MB6874:EE_ X-MS-Office365-Filtering-Correlation-Id: 8ffd0150-c678-4aa2-4290-08dc38cbf98a X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: tzoF4E5crxx5D+9uCcNWiFgZjadLwxFfixniygDxM18Y76rhcBm0F8w05w6dsg11csde1MQg0WI6+90NnzOc66MWBoUe+1ilbwEpdxnqhdNy8JOlyTExItOavOlX/28qqAIyrKW/9S3QdV2HVZXMOEyHadV7wvtELbAO+sSZG/aMTRIH+LkiCaOuGMEm7WBS+DIGLg9TP4VhbVlaxNbNulmGX4d+9xuURdjXU3iEThkpiKEjMLs0G5A3f/JSJoBgtO2lwI48LpGYd3hsJgBE+qqVZeo9x72q4IieHAILxWB+cZHqysybeKGXX0UwAWG0cP1VRnUy9A3kLGDW+at/4zJJelywQfDRYmU6mdXc1/4Rp5hKzL7kh8DIGuiOfmsesZfg799OnGtTygRGdUZlb1MLZkZUwQnrRPCk70TbtPG/2SwjlZN0KQnJOzh0U+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nFZVUtlVmpzTHRIeTFL?= =?utf-8?B?dzBPL1dYSnBzWTZudVVEVHVMc0hHSVk2Nk03WVBSNEswQy81NXdtY2JxOUph?= =?utf-8?B?N0JXTzRoSFgzdnYxNzZWTE1jajQ0Q0h6MDEvY2t6WEdsa1pTdTFndEhvWUhs?= =?utf-8?B?Tm5RNXhYdStGNWV6ekZOdGFsMXdadzZJOXdZKzg5aElxTjFRNTdCL25kRzFR?= =?utf-8?Q?JPcLGSfRfMTfHF9SwpGcCcpGXp0uoJlwB16cF4Z?= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 8ffd0150-c678-4aa2-4290-08dc38cbf98a X-MS-Exchange-CrossTenant-AuthSource: SEZPR06MB6959.apcprd06.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 29 Feb 2024 02:13:09.7916 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: SEZPR06MB6874 On 2/29/2024 9:58 AM, Stephen Boyd wrote: > Quoting Yang Xiwen via B4 Relay (2024-02-23 09:18:52) >> From: Yang Xiwen >> >> Originally, the initial clock rate is hardcoded to 0, this can lead to >> some problem when setting a very small rate with CLK_MUX_ROUND_NEAREST. > > Did you mean CLK_MUX_ROUND_CLOSEST? You are right :). > >> >> For example, if the lowest possible rate privided by the mux is 1000Hz, > > s/privided/provided/ > >> setting a rate below 500Hz will fail, because no clock can provide a >> better rate than the non-existant 0. But it should succeed with 1000Hz >> being set. >> >> Setting the initial best parent to current parent could solve this bug >> very well. >> >> Signed-off-by: Yang Xiwen >> --- >> This is actually a v2 of [1], but seems too simple to have a unittest. >> It's tested in a mmc host driver. > > It's not too simple for a unittest. > >> >> [1]: https://lore.kernel.org/linux-clk/20230421-clk-v3-1-9ff79e7e7fed@outlook.com/ > > In that thread I asked you to please Cc Maxime. Please do that. > >> --- >> drivers/clk/clk.c | 4 ++++ >> 1 file changed, 4 insertions(+) >> >> diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c >> index 2253c154a824..d98cebd7ff03 100644 >> --- a/drivers/clk/clk.c >> +++ b/drivers/clk/clk.c >> @@ -649,6 +649,10 @@ int clk_mux_determine_rate_flags(struct clk_hw *hw, >> >> /* find the parent that can provide the fastest rate <= rate */ >> num_parents = core->num_parents; >> + if (core->parent) { >> + best_parent = core->parent; >> + best = clk_core_get_rate_nolock(best_parent); >> + } > > Is the problem that we're not using abs_diff()? No, i think. It has nothing to do with the code here. It's because of the initial best_parent/best_parent_rate. > > ----8<---- > diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c > index a3bc7fb90d0f..91023345595f 100644 > --- a/drivers/clk/clk.c > +++ b/drivers/clk/clk.c > @@ -542,7 +542,7 @@ static bool mux_is_better_rate(unsigned long rate, unsigned long now, > unsigned long best, unsigned long flags) > { > if (flags & CLK_MUX_ROUND_CLOSEST) > - return abs(now - rate) < abs(best - rate); > + return abs_diff(now, rate) < abs_diff(best, rate); Without this patch, the initial `best` rate would be always 0. This is wrong for most cases, 0Hz might (usually) be unavailable. We should use a valid rate(i.e. current rate) initially. > > return now <= rate && now > best; > } -- Best regards, Yang Xiwen