Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp2292354ybv; Mon, 24 Feb 2020 02:44:48 -0800 (PST) X-Google-Smtp-Source: APXvYqwt2s5RffS2VO9QEuix+1kOkrP5C/IcrPLAPNSkC1CTmB9TNzdznzFAr+DuIcfZ+AcxBhLb X-Received: by 2002:a05:6830:114c:: with SMTP id x12mr39853214otq.324.1582541088372; Mon, 24 Feb 2020 02:44:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1582541088; cv=none; d=google.com; s=arc-20160816; b=omNpd/80jR9tzqA2EdMf+6BBVMLCe2ORlRpGgea8ziHddo0D9uDxbSSnGczL3B9Oar rRIS2gQXtCw415RgcPaSFxw0Ane9PYsN5+sXbk3S0C8U2UrkhHHDreUjimWUG0Aqpreh NF1S26gcZRQTQteXTOtYgVXDYiAX0YkyGQ9lWW2Iw2qutvhIwslt4aSeH7/G41M2tNgR X35poi3DNod/8MeoT7TC/wTvHj7bjHB1ra1B2JRYuUfnT06KiKS2JKRH35GDHEXE5w8T EWAKRGLYAZehf4grJrQWXgno6+E/adVYIr+O/dnCXGZm4WaZowNQQDcLi3Nh9Z2IiFLS 7EiQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=AA1fht4PBBWxg1RDOE7Wcol+BSgek677G89ovTk8KfU=; b=oIbg6SzpzQhp37rglKVjygnzaFd5PD0zLN/Bd5zhU7tDgN83iLCH07NoIPEOl4M1b1 TpU6qVHfzqGUC6+FVwrOUsrsMySoXfAIUzi9UERW6WhYdmnZQX9BPjkyRNgPdmsKQ8it yxyAQGEHRZMEUOGTjvsyzwV4HPr+8p5h6eXJiaVcmwoyTY8qnNGyXcSaKLWKt3q7aS6l dNHC3Y1z8SjYPxUh+D2Nsppa7BhTiLqCkqf3HQKwLVLMopWeeeXDIZEm7jIV9y5KPQVh rvQA6DOG0DaTHtEkVd0EWigJf/5/vcuYofy/PYv0v4FJZdo9iDOt3B3tSCb0H+J1/IJx uUZQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e64si4999679oib.4.2020.02.24.02.44.36; Mon, 24 Feb 2020 02:44:48 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727275AbgBXKnF (ORCPT + 99 others); Mon, 24 Feb 2020 05:43:05 -0500 Received: from mga02.intel.com ([134.134.136.20]:10855 "EHLO mga02.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726509AbgBXKnF (ORCPT ); Mon, 24 Feb 2020 05:43:05 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga101.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 24 Feb 2020 02:43:04 -0800 X-IronPort-AV: E=Sophos;i="5.70,479,1574150400"; d="scan'208";a="225935345" Received: from aslawinx-mobl1.ger.corp.intel.com (HELO [10.249.154.74]) ([10.249.154.74]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-SHA; 24 Feb 2020 02:43:02 -0800 Subject: Re: [PATCH] Intel: Skylake: Fix inconsistent IS_ERR and PTR_ERR To: Cezary Rojewski , Pierre-Louis Bossart , Joe Perches , Xu Wang , "Slawinski, AmadeuszX" Cc: linux-kernel@vger.kernel.org, alsa-devel@alsa-project.org, tiwai@suse.com References: <20200221101112.3104-1-vulab@iscas.ac.cn> <1247da797bc0a860e845989241385e124e589063.camel@perches.com> <8e96c207-cdf8-2d1f-755e-be60555c8728@linux.intel.com> From: =?UTF-8?Q?Amadeusz_S=c5=82awi=c5=84ski?= Message-ID: Date: Mon, 24 Feb 2020 11:42:59 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.5.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2/23/2020 4:59 PM, Cezary Rojewski wrote: > On 2020-02-21 16:40, Pierre-Louis Bossart wrote: >> On 2/21/20 8:41 AM, Joe Perches wrote: >>> On Fri, 2020-02-21 at 18:11 +0800, Xu Wang wrote: >>>> PTR_ERR should access the value just tested by IS_ERR. >>>> In skl_clk_dev_probe(),it is inconsistent. > > Please include all maintainers of given driver when submitting the > patch, thank you. > >>> [] >>>> diff --git a/sound/soc/intel/skylake/skl-ssp-clk.c >>>> b/sound/soc/intel/skylake/skl-ssp-clk.c >>> [] >>>> @@ -384,7 +384,7 @@ static int skl_clk_dev_probe(struct >>>> platform_device *pdev) >>>>                   &clks[i], clk_pdata, i); >>>>           if (IS_ERR(data->clk[data->avail_clk_cnt])) { >>>> -            ret = PTR_ERR(data->clk[data->avail_clk_cnt++]); >>>> +            ret = PTR_ERR(data->clk[data->avail_clk_cnt]); >>> >>> NAK. >>> >>> This is not inconsistent and you are removing the ++ >>> which is a post increment.  Likely that is necessary. >>> >>> You could write the access and the increment as two >>> separate statements if it confuses you. >> >> Well to be fair the code is far from clear. > > Thanks for notifying, Pierre. > > Although NAK is upheld here. Proposed change is likely to introduce > regression. > >> >> the post-increment is likely needed because of the error handling in >> unregister_src_clk 1 >>          data->clk[data->avail_clk_cnt] = register_skl_clk(dev, >>                  &clks[i], clk_pdata, i); >> >>          if (IS_ERR(data->clk[data->avail_clk_cnt])) { >>              ret = PTR_ERR(data->clk[data->avail_clk_cnt++]); >>              goto err_unreg_skl_clk; >>          } >>      } >> >>      platform_set_drvdata(pdev, data); >> >>      return 0; >> >> err_unreg_skl_clk: >>      unregister_src_clk(data); >> >> static void unregister_src_clk(struct skl_clk_data *dclk) >> { >>      while (dclk->avail_clk_cnt--) >>          clkdev_drop(dclk->clk[dclk->avail_clk_cnt]->lookup); >> } >> >> So the post-increment is cancelled in the while(). >> >> That said, the avail_clk_cnt field is never initialized or incremented >> in normal usages so the code looks quite suspicious indeed. > > As basically entire old Skylake code, so no surprises here : ) > struct skl_clk_data::avail_clk_cnt field is initialized with 0 via > devm_kzalloc in skl_clk_dev_probe(). > >> >> gitk tells me this patch is likely the culprit: >> >> 6ee927f2f01466 ('ASoC: Intel: Skylake: Fix NULL ptr dereference when >> unloading clk dev') >> >> -        data->clk[i] = register_skl_clk(dev, &clks[i], clk_pdata, i); >> -        if (IS_ERR(data->clk[i])) { >> -            ret = PTR_ERR(data->clk[i]); >> +        data->clk[data->avail_clk_cnt] = register_skl_clk(dev, >> +                &clks[i], clk_pdata, i); >> + >> +        if (IS_ERR(data->clk[data->avail_clk_cnt])) { >> +            ret = PTR_ERR(data->clk[data->avail_clk_cnt++]); >>               goto err_unreg_skl_clk; >>           } >> - >> -        data->avail_clk_cnt++; >> >> That last removal is probably wrong. Cezary and Amadeusz, you may want >> to look at this? > > Indeed, code looks wrong. Idk what are we even dropping in > unregister_src_clk() if register_skl_clk() fails and avail_clk_cnt gets > incremented anyway. > > In general usage of while(ptr->counter--) (example of which is present > in unregister_src_clk()) is prone to errors. Decrementation happens > regardless of while's check outcome and caller may receive back handle > in invalid state. > > Amadeo, your thoughts? > Right, there is a problem with how we do increment available clock counter. It should be done in success path, sent fix. Amadeusz