Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 36262C636CC for ; Mon, 20 Feb 2023 17:27:33 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 681DC858E1; Mon, 20 Feb 2023 18:27:30 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=suse.de header.i=@suse.de header.b="JdiYZrJe"; dkim=permerror (0-bit key) header.d=suse.de header.i=@suse.de header.b="Ox3K5tWM"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 0E10B85918; Mon, 20 Feb 2023 18:27:28 +0100 (CET) Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 59D3A858A7 for ; Mon, 20 Feb 2023 18:27:25 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=msuchanek@suse.de Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id 19CFA227E7; Mon, 20 Feb 2023 17:27:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1676914045; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=4EsHjptyUwWQzbhGz9eJLnhg+40X0gau9qjNQWsWwOA=; b=JdiYZrJev6QrYodIKKkxu320c/0nfKGDJ/ZZ5YgrZqgqwZ4tI9iJxaykrJY8901MiOf2rM PyOPe6dnM8N5A9sWz1kvT6D2ds+iyyEN2fTk0uz9/JTTm97srciNLpHLJ9Ke3T/CRZznOD Zi8Mftdez0TQKdmTad217uoiveBBMKo= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1676914045; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=4EsHjptyUwWQzbhGz9eJLnhg+40X0gau9qjNQWsWwOA=; b=Ox3K5tWMp7uuz0MTdBVh9aiSgU1Dda+taLcyP0gfrMd22QPe2JbZWJ8IT+PEB/AogD2hmQ zbFD7QSYbT1kjQDQ== Received: from kitsune.suse.cz (kitsune.suse.cz [10.100.12.127]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id 7EBFF2C141; Mon, 20 Feb 2023 17:27:24 +0000 (UTC) Date: Mon, 20 Feb 2023 18:27:23 +0100 From: Michal =?iso-8859-1?Q?Such=E1nek?= To: Sean Anderson Cc: Samuel Holland , Lukasz Majewski , Simon Glass , Dario Binacchi , Jean-Jacques Hiblot , Neil Armstrong , Peng Fan , u-boot@lists.denx.de Subject: Re: [PATCH 2/6] clk: Fix error handling in clk_get_rate() Message-ID: <20230220172723.GM19419@kitsune.suse.cz> References: <20230220055940.41890-1-samuel@sholland.org> <20230220055940.41890-3-samuel@sholland.org> <20230220103744.GH19419@kitsune.suse.cz> <3344084e-c3a4-4c56-9e17-cc7d38243ed8@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <3344084e-c3a4-4c56-9e17-cc7d38243ed8@gmail.com> User-Agent: Mutt/1.10.1 (2018-07-13) X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.6 at phobos.denx.de X-Virus-Status: Clean Hello, On Mon, Feb 20, 2023 at 11:08:41AM -0500, Sean Anderson wrote: > On 2/20/23 05:37, Michal Suchánek wrote: > > Hello, > > > > On Sun, Feb 19, 2023 at 11:59:35PM -0600, Samuel Holland wrote: > > > log_ret() cannot work with unsigned values, and the assignment to 'ret' > > > incorrectly truncates the rate from long to int. > > > > > > Fixes: 5c5992cb90cf ("clk: Add debugging for return values") > > > Signed-off-by: Samuel Holland > > > --- > > > > > > drivers/clk/clk-uclass.c | 7 +------ > > > 1 file changed, 1 insertion(+), 6 deletions(-) > > > > > > diff --git a/drivers/clk/clk-uclass.c b/drivers/clk/clk-uclass.c > > > index dc3e9d6a261..78299dbceb2 100644 > > > --- a/drivers/clk/clk-uclass.c > > > +++ b/drivers/clk/clk-uclass.c > > > @@ -471,7 +471,6 @@ void clk_free(struct clk *clk) > > > ulong clk_get_rate(struct clk *clk) > > > { > > > const struct clk_ops *ops; > > > - int ret; > > > debug("%s(clk=%p)\n", __func__, clk); > > > if (!clk_valid(clk)) > > > @@ -481,11 +480,7 @@ ulong clk_get_rate(struct clk *clk) > > > if (!ops->get_rate) > > > return -ENOSYS; > > > - ret = ops->get_rate(clk); > > > - if (ret) > > > - return log_ret(ret); > > > - > > > - return 0; > > > + return ops->get_rate(clk); > > > } > > > > This is generally poor design of the clock stuff in u-boot. > > > > It returns -ERROR for many error conditions, but also 0 for other error > > conditions. > > > > Some error handling checks for 0, some for errval, some casts to int and > > checks for <= 0. > > > > I think that using -ERROR for clocks does not make much sense in u-boot. > > > > Even in the kernel the errval checks are pretty much limited to places > > where integers are used to store page frame numbers or pointers, that is > > errptr stored in an integer. For adresses it is pretty easy to make sure > > that the last page is not mapped making the error pointers invalid > > (although bugs in that part happened too). > > > > For clocks no such guarantee exists. The only apparently invalid clock > > is 0, and the correct fix is to fix up the clock code to return 0 on > > error, always. It's a lot of code to fix, though. > > > > If you do not want to fix everything then the correct thing to do is > > make ret ulong, and check for errval *and* 0. > > > > There is not much point in returning detailed error codes in u-boot, > > anyway. It's not like there is some userspace that could interpret them. > > Most errors are logged when they happen if ever, and callers only check > > if error happened or not. > > > > Thanks > > > > Michal > > clk_get_parent is the only place where we return a clock pointer directly. > Everywhere else, we have an integer return we can use. This function is > different of course because CCF is broken and assumes there is only one > canonical struct clk for a logical clock. So it can't initialize a caller-passed > struct clk, but instead has to return the one true struct clk. > > I agree with Michal here. The signature should really be > > int clk_get_parent(struct clk *child, struct clk *parent) > > but for now there is no point confusing the rest of the clock subsystem to make > it work with error pointers. This is clk_get_rate. It returns a clock rate. A clock rate of 2.5GHz is not out of question, and cannot be expressed with a signed integer. Hence clock rate should not be treated as signed, and -ERROR should not be returned as clock rate. Thanks Michal