Received: by 2002:a05:7208:13ce:b0:7f:395a:35b6 with SMTP id r14csp216596rbe; Wed, 28 Feb 2024 18:33:35 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCV18B0KFfoC2N+na7JYvUwOdn6vGdQwNB+fpMsCNb+PqlMIq0+snryaLu8fKLzqRwKA+YSaitsjK23FYVSTlGLSiT2YlW3MSC/jZ63tPw== X-Google-Smtp-Source: AGHT+IHBtc30HqJrs1XRUr6OKRavEeXQ/vT00dUb53R8vSu6WG5EfiScVqhhqCjeGyijJXL/oUX+ X-Received: by 2002:a17:90a:e60f:b0:29a:89db:7581 with SMTP id j15-20020a17090ae60f00b0029a89db7581mr865452pjy.13.1709174014878; Wed, 28 Feb 2024 18:33:34 -0800 (PST) Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id q18-20020a17090aa01200b00299272a8570si2481712pjp.101.2024.02.28.18.33.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Feb 2024 18:33:34 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-86048-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@outlook.com header.s=selector1 header.b=oUvZSpHz; arc=fail (signature failed); spf=pass (google.com: domain of linux-kernel+bounces-86048-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-86048-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 4A6B1B24379 for ; Thu, 29 Feb 2024 02:33:31 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id A56A025630; Thu, 29 Feb 2024 02:33:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=outlook.com header.i=@outlook.com header.b="oUvZSpHz" Received: from HK2PR02CU002.outbound.protection.outlook.com (mail-eastasiaazolkn19012004.outbound.protection.outlook.com [52.103.64.4]) (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 6C81E1DDC9; Thu, 29 Feb 2024 02:33:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=52.103.64.4 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709174003; cv=fail; b=JKJpvnU7ni53ilSucdSRvNo4zErTBVM5RMq8IDpYHZAEni6OMyFtCd9DKTKwdmdkcAcl6837kCNG8qVbTuezjtcJFD9/udrUNrlAgumuZlNu+LX0yhNnYkH7g+fHHmYO9PWrpoJLuh3SnyLTUYSon98ajNHApqMhccteKt4elBQ= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709174003; c=relaxed/simple; bh=ffqZ+XsPy8xa/IjLGdxigdwX2aqC0iLxE1MJ7o1jVqo=; h=Message-ID:Date:Subject:To:Cc:References:From:In-Reply-To: Content-Type:MIME-Version; b=WL5t+3d/OYWsL+zrLHrZdvOkN+QlEBliaQqF8deq9LBl7jnwZx1G/HoW5uNWhIsVKB/rCngFn2UtFb+MFadO7grSoftcDE+dEot5fEB0qNNL5VCBK8KNrbXRgZSnIM/QuQZVAWQD9mTrAqdiiZhyIwZ30kTAFJGW8lEDbanTkwM= 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=oUvZSpHz; arc=fail smtp.client-ip=52.103.64.4 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=DVnY7HF0mJTsVwelcxfsEoKWGk0Rs+5Pl6MyNBzjDnIUi2/LNsC7AYIsAoLkJeNO1YIL1GOAj/Vg/zSJz1MFXXL2Df3mHlhfNqadRbegwU44/MKo9ZTBRl+YhZnZhqcGeBqrXCaCimDPvI5ib8jzsqkKrXcuUAPQEtGraiFcQ/CN7boF8eKgHydZJ0tfNKd6QvZbCNv+nA2tSLMjqQM7R2FgkNriZxDRtNbC6uYsrRmkUYe8+2Yzg3BMRF8e79zXv8PGX7BWlBo/7AGedGH0+pKBzUuYWtvVZBQ+t2ZB767G+TVuvvS4cRE2pSTism9gZbPZXxUfxO2QHi632iHEag== 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=ggTFQEh+XzubuhV2mt5lt91snsAx8xWCdXsVVxEhR/I=; b=md9Z+/P9OBDouRuStYxg3arYd455HcQ/wyRfDaocL2MzMYDtkGyHxXMUU200c2EP7slEnJ0TuYq0NHNdtW1GtTopynXflxXWr/0IKhhojEpPbMa9IwiSNwXFOmI8Pp7Emcf07HvY5yOpQyYsR1VHPOl/GTbYI2Zc72RjGmJ9h/OXzS6XaChyrV9asPewqAz3KnWOmPUJPMd++vXwKW9yoGNyB02QZjVYIeBoILMT+nCRVEbwyGwNm+lee7A8Zsg+8Ncw78U4N2Hdanm+SZjcFRliwa9gXEU/XtFv+WSRUAO5Z5yssXwlFg6F6R6keFvG1N1tKMpph1TPM2Juw1dtHg== 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=ggTFQEh+XzubuhV2mt5lt91snsAx8xWCdXsVVxEhR/I=; b=oUvZSpHzMlr/Gvk0CMlDal+ToW4yqy2yT+25q+B9f/bWZan9sjoe2N8Igdi/kZ9SOS52mQYj6DV4K0Agf5uLtz5Yqhhry8mZ6Xs/5XHBpyy1q191jJ+Khtz0lDlKRF4MYRoSo/WptXfs2PEM1FUhfEe2mHsTdSWunKGnBpAcBFNZj4+x/zS/CDStAhItpdbhEnRZQZuXxNS2IjFiNVa/66TqZzb2k316WLOO0x8wl1HjaCc399c7HcyFh9OVxwesz7Nzg/KPtxBKKNaSRl0RnmaosYWvhh1Jgw1mjHJIgH9D0UBId5KMNAYLVL9XJpFfJt2PRCNQLZu0z5vkbxIdAw== Received: from SEZPR06MB6959.apcprd06.prod.outlook.com (2603:1096:101:1ed::14) by SEZPR06MB5920.apcprd06.prod.outlook.com (2603:1096:101:f0::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7316.36; Thu, 29 Feb 2024 02:33:16 +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:33:16 +0000 Message-ID: Date: Thu, 29 Feb 2024 10:33:11 +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: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-TMN: [PvppSeHBfACu94ZV4QNvvg6z9ENcigJIzXsZ1lV409HXin2ZC/RYvU2u2KQ3iVC/] X-ClientProxiedBy: KL1PR01CA0099.apcprd01.prod.exchangelabs.com (2603:1096:820:3::15) 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_|SEZPR06MB5920:EE_ X-MS-Office365-Filtering-Correlation-Id: 28fb2bbb-2439-486b-be6f-08dc38cec8ef X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: m/L0h7uDk+v3GJAGbbryFNvWe7SzwN3wqLSr9UkUyAjeOJ4yBRHeVfxGR6YyYhqvSv/Cqso3UvSFmcRR2LwLaCDnOC+ZgpBtE9DlNAwyXmOqZ1td4jxqVbr47YiQiQxOe5yfe23vYX96HpsMdcL/rQD5AyoT/RgKa7hKUIMq+WdJLsdjHYtzZnrRBv09lbFfH7SUjARMPF14MNVmY3QAX1ZKco9/Cf3znULCQ1UIiMGWdU5Q4OcNzwyUUVQfbjGJgmRL5/6L0JH1Bag7syeU5i40FZ9JTNy/lAHFCRakTC6S0JV6lWrVfJmN5hBO2ezVfXFtXRQpXDwsr34McWpiL6sxdmddZZvYgmVBt/IUMHiJim6fUhOhemb2K8Ag/ui3ibpqzyELg8E4WCLUfRM4tKnbOZW52JSA9s+UxW76YZcylPpXN4F4K0eoIjGg/kXSdZ5aJFaQyUQNjc8PTVlUDrI4K0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mk1VWZm?= =?utf-8?B?QnVpbnlLeHQvbmNKMnZXZEU4bEZmL3VNcngvN2VGaFlMdEZNVDJnVTg4TGtp?= =?utf-8?B?NlpiR25wdjdCYzRyMHpOeGxSZENRczVCRWlTcmFlUS90OG91TzB4anZUMWF1?= =?utf-8?B?VytFdHg4b09VQzM2YkdPbEtieUpkM2tLYnRKMy9xc01wYk5KeG5NMkxMcjAv?= =?utf-8?Q?pD8ob/d0kDkDeyXOruSAq6yjZ8DfcCMcR3/r/pH?= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 28fb2bbb-2439-486b-be6f-08dc38cec8ef 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:33:16.7221 (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: SEZPR06MB5920 On 2/29/2024 10:25 AM, Stephen Boyd wrote: > Quoting Yang Xiwen (2024-02-28 18:13:04) >> 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. > > Alright. > >> >>> >>> ----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. > > Ok. But you set best to the parent rate. So why not use 'core->rate' > directly as 'best'? I can't remember exactly. I just add this piece of code and found it's working. Is this field already filled prior to setting rate? Anyway, your suggestion is very reasonable. Maybe dear clk maintainers can fix it as i'm not familiar with clk core code. -- Best regards, Yang Xiwen