Received: by 2002:a05:6358:7058:b0:131:369:b2a3 with SMTP id 24csp7613658rwp; Tue, 18 Jul 2023 19:24:31 -0700 (PDT) X-Google-Smtp-Source: APBJJlFy28WmdWrhaR0u2CuRRm2ek+o5GydjyNhLj5rbCrNleTXhXorFC0aEVst2tMLV7x9fO2uz X-Received: by 2002:a17:906:101c:b0:993:f996:52d3 with SMTP id 28-20020a170906101c00b00993f99652d3mr1023519ejm.25.1689733471294; Tue, 18 Jul 2023 19:24:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689733471; cv=none; d=google.com; s=arc-20160816; b=CTL8t7jhcXE4IM5RogAxsHe5m82m5diQMERtPMmOcyzjxun3M7HM3bWhSAzW4kr4Qy uaROVgZAIVJigaPCQbqUNq4y2SxcASJUSBf8hqvdMPGMYelOVXsSLbhOHkgYBPYP2lzV +2fVTecL8CLWXqucFwAbo85iq+W9ghDvx5CYSyUQjtQoSy7XIHbysgnIeDnTuWoHDwm0 Nj2ogw3rtUrO3prmy4ZTUyfIhfyaFIMeivWDncQHruf/p8pkztJ3uBUV43mI2+bjwR45 rCjImPZx+R6X4kGc5RHCdASB9YIIVXKpnpflMnjQLXwt2t8+jUI/2GBVDg5GilwEeMYb wKzw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from:cc:to :content-language:subject:user-agent:mime-version:date:message-id; bh=xZIix82ROHhiyiQljU158oFcHYcrBNYtzsk4gj/a+oY=; fh=KbtmS4nsVLKi1MDYIwI9HqfiHBEz5uqg0EtU7LqzKU0=; b=CsUarX18+lP07MeGhGS2kKDzFjQYW9LyMC25czPWwGvZkD7QI6v5+mYGpIm7H1ssv0 eKcw4947SSgaTJo9KL1bAVladPIupFe/TCt77/QYeCfkyFlUJ6PF8HPcsXDLOKb7DNqk 3AmFe6HGKMpkmDOuPo1xz4/MLuURdGbSmTyWOclDCLMc9/qHqg6H4nYCgXn3uJ8Wcnio w/eqR8RHfWMjVsxMI8QYYQvohZSshihQgBhDQgPfVJC7rjaPMNR4G33IfP3RmEzhbBta LYEXOKcUIppgTubvkYRowloK6R5lBEEgoz3QiP4vriymrLgrK/rpgP+PLe9FgmIcwScW kW0w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id qx12-20020a170906fccc00b0099360544774si2043139ejb.614.2023.07.18.19.24.04; Tue, 18 Jul 2023 19:24:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229552AbjGSCNA (ORCPT + 99 others); Tue, 18 Jul 2023 22:13:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48112 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229481AbjGSCM7 (ORCPT ); Tue, 18 Jul 2023 22:12:59 -0400 Received: from mail.nfschina.com (unknown [42.101.60.195]) by lindbergh.monkeyblade.net (Postfix) with SMTP id 926E2FC; Tue, 18 Jul 2023 19:12:57 -0700 (PDT) Received: from [172.30.11.106] (unknown [180.167.10.98]) by mail.nfschina.com (Maildata Gateway V2.8.8) with ESMTPSA id CD47B60484988; Wed, 19 Jul 2023 10:12:47 +0800 (CST) Message-ID: <40db9ac5-84b7-dc98-786c-2e651404534b@nfschina.com> Date: Wed, 19 Jul 2023 10:12:47 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.8.0 Subject: Re: [PATCH] drm/i915/tv: avoid possible division by zero Content-Language: en-US To: Andrzej Hajda Cc: jani.nikula@linux.intel.com, joonas.lahtinen@linux.intel.com, rodrigo.vivi@intel.com, tvrtko.ursulin@linux.intel.com, airlied@gmail.com, daniel@ffwll.ch, nathan@kernel.org, ndesaulniers@google.com, trix@redhat.com, ville.syrjala@linux.intel.com, mripard@kernel.org, ankit.k.nautiyal@intel.com, intel-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org, linux-kernel@vger.kernel.org, llvm@lists.linux.dev, kernel-janitors@vger.kernel.org, Dan Carpenter X-MD-Sfrom: suhui@nfschina.com X-MD-SrcIP: 180.167.10.98 From: Su Hui In-Reply-To: <3c7dfc18-539f-2b0c-0c77-48b89ef96560@intel.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.1 required=5.0 tests=BAYES_00,RDNS_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2023/7/18 19:28, Andrzej Hajda wrote: > On 18.07.2023 12:10, Su Hui wrote: >> On 2023/7/18 13:39, Dan Carpenter wrote: >>> On Mon, Jul 17, 2023 at 04:52:51PM +0200, Andrzej Hajda wrote: >>>> On 17.07.2023 08:22, Su Hui wrote: >>>>> Clang warning: drivers/gpu/drm/i915/display/intel_tv.c: >>>>> line 991, column 22 Division by zero. >>>>> Assuming tv_mode->oversample=1 and (!tv_mode->progressive)=1, >>>>> then division by zero will happen. >>>>> >>>>> Fixes: 1bba5543e4fe ("drm/i915: Fix TV encoder clock computation") >>>>> Signed-off-by: Su Hui >>>>> --- >>>>>    drivers/gpu/drm/i915/display/intel_tv.c | 3 ++- >>>>>    1 file changed, 2 insertions(+), 1 deletion(-) >>>>> >>>>> diff --git a/drivers/gpu/drm/i915/display/intel_tv.c >>>>> b/drivers/gpu/drm/i915/display/intel_tv.c >>>>> index 36b479b46b60..82b54af51f23 100644 >>>>> --- a/drivers/gpu/drm/i915/display/intel_tv.c >>>>> +++ b/drivers/gpu/drm/i915/display/intel_tv.c >>>>> @@ -988,7 +988,8 @@ intel_tv_mode_to_mode(struct drm_display_mode >>>>> *mode, >>>>>                  const struct tv_mode *tv_mode, >>>>>                  int clock) >>>>>    { >>>>> -    mode->clock = clock / (tv_mode->oversample >> >>>>> !tv_mode->progressive); >>>>> +    mode->clock = clock / (tv_mode->oversample != 1 ? >>>>> +            tv_mode->oversample >> !tv_mode->progressive : 1); >>>> Seems too smart to me, why not just: >>>> mode->clock = clock / tv_mode->oversample; >>>> if (!tv_mode->progressive) >>>>      mode->clock <<= 1; >>> This is nice. >> >> mode->clock = clock / tv_mode->oversample << !tv_mode->progressive; >> >> But I think this one is much better,  it has less code and run faster. >> Should I resend v3 to add some explanation or follow Dan's advice? > > Speed gain here is irrelevant here, and disputable. > > One thing which could be problematic is that we could loose the least > significant bit in mode->clock, > in case non-progressive, but I am not sure if it really matters, as > mode->clock is not precise value anyway. > Alternatively we could 1st shift, then divide, but in this case > overflow can occur, at least in theory - I suspect there are no such > big clocks (in kHz). > > Finally I would agree with Dan, readability is better with conditional. > How about this one? - mode->clock = clock / (tv_mode->oversample >> !tv_mode->progressive); + mode->clock = clock; + if (tv_mode->oversample >> !tv_mode->progressive) + mode->clock /= tv_mode->oversample >> 1; Prevent loss of accuracy and also make it more readable. If it's OK, I will send v3 patch. By the way, do we need to print some error messages or do some things  when "tv_mode->oversample << !tv_mode->progressive" is zero? I'm not sure about this. Su Hui > Regards > Andrzej