Received: by 2002:a05:6a10:2785:0:0:0:0 with SMTP id ia5csp3454565pxb; Tue, 12 Jan 2021 15:26:42 -0800 (PST) X-Google-Smtp-Source: ABdhPJzzftiBPmhI0622AgCdGqZtZU+TgFbRWbfiX64NAssa0XDYzhzjq3OByhWb+EwVaaZfP3Pn X-Received: by 2002:a05:6402:2694:: with SMTP id w20mr1243596edd.200.1610494002359; Tue, 12 Jan 2021 15:26:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610494002; cv=none; d=google.com; s=arc-20160816; b=ml4R5gRpXvMHWgCfp2ehVtPN2xSacHnYZH4L853VvfZDL8fJcazO/5xI/nGkIRwPhS YNt+tB8HGsHdvWKkSz48MEntqRq7/rL32uISZXeIVoT+4uV/+WzQPE7LT4BkLWf9i/3j nV5lBFjCfoCkauE0mmyfyTPXRwdYnTjqEHwEYe5CZRUp8d16JpC7Lv1cPrjW0ruG+KL8 qkQ7k0BNVpWa/E/j1Rk/WxZ/0jXEy9hCQjOejrNTuqgoyKKwtGUCFQ0PU+zpVSaG4GqP QplBDXLlOOtV2U4vW/2Mmn5gxFHjCVV66PnFGoQZgo2ioqDwWp2K9PdkB7gjKEJe+YcW j31w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version; bh=D79yAvxFkBso3i6vK6yIO4bEcxdNj8cx5BWEVmuRDLo=; b=D3xGCBOUFa3yoNHRop+wl0HRiGQiT1W7l6SOQcYLlXf0a1q5JLmWwzAJyw/fD0xI1G SyZEbl2ZlOzBhUHAgNTG9cNsIBx5JIYxoswPN2yM+5sxh7S7QqZAb3+GZMz1qB3IgYaW 9NOe9uITCz51nb+zuzajJNwrNx3O5RyFhx6ra4qsfSZV1OdkxiiE40VHOqGeOiElsDdy vSZtbxvCOOodTqqQghCg15jfjv6ks/Xrsvp9+ELG6efH2IXRXDO4LW7pTAG7mqDlkbpW XlgSF+xRYpioH8daZpiODMFo/zMC34OY6vAS4/sIlp4gHERuR8k5DTuJIM1dz4FR4myy Brcw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id jg13si94180ejc.669.2021.01.12.15.26.18; Tue, 12 Jan 2021 15:26:42 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388471AbhALIrt (ORCPT + 99 others); Tue, 12 Jan 2021 03:47:49 -0500 Received: from mail-oi1-f173.google.com ([209.85.167.173]:46871 "EHLO mail-oi1-f173.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732738AbhALIrs (ORCPT ); Tue, 12 Jan 2021 03:47:48 -0500 Received: by mail-oi1-f173.google.com with SMTP id q205so1544799oig.13 for ; Tue, 12 Jan 2021 00:47:33 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=D79yAvxFkBso3i6vK6yIO4bEcxdNj8cx5BWEVmuRDLo=; b=VUJPK4pU03Pedm0UiUda30x3YSY43Tk2PKjThMPxOZRk2+fbJ0uxL43te2hzbFZssK JB/9JOpCvBcZ9E6tuJURUibIIgdVwFQ5u3I5vfL/28pnuCDo09eZYDGeL2oOlql+BIOo CvKAJa2eoHGCHCQ5x36ieDXZGTj7v+izEztzUVLWhvAlTtJ9GP0mNRI3vfNaj4YH2Y7U ACkcsTh3cbpnwME4ugTPSodOmjfVJEpEX0ZSNPzBi+XAJjM0wGM3dEjbC+HjuLss3QNP xF6sZ05ihltzokotjYXsoiQp/huWrHbtzHvBjAD9S2jnaakkjsAUzMDms2P+JFFwGBXS yLQA== X-Gm-Message-State: AOAM532RIQMQM5btr8PRRQobmFUd8W1qrB7P09cInVDVmx+9KJ6DnAnK /zWAG6i2aH9tx3v/seCFSqADEf2aFgimKCl4I1o= X-Received: by 2002:aca:4b16:: with SMTP id y22mr1719601oia.148.1610441227636; Tue, 12 Jan 2021 00:47:07 -0800 (PST) MIME-Version: 1.0 References: <20210111125702.360745-1-geert+renesas@glider.be> In-Reply-To: From: Geert Uytterhoeven Date: Tue, 12 Jan 2021 09:46:56 +0100 Message-ID: Subject: Re: [PATCH] drm/bridge: nwl-dsi: Avoid potential multiplication overflow on 32-bit To: Laurent Pinchart Cc: Andrzej Hajda , Neil Armstrong , Jonas Karlman , Jernej Skrabec , David Airlie , Daniel Vetter , DRI Development , Linux Kernel Mailing List Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Laurent, On Tue, Jan 12, 2021 at 5:38 AM Laurent Pinchart wrote: > On Mon, Jan 11, 2021 at 01:57:02PM +0100, Geert Uytterhoeven wrote: > > As nwl_dsi.lanes is u32, and NSEC_PER_SEC is 1000000000L, the second > > multiplication in > > > > dsi->lanes * 8 * NSEC_PER_SEC > > > > will overflow on a 32-bit platform. Fix this by making the constant > > unsigned long long, forcing 64-bit arithmetic. > > > > While iMX8 is arm64, this driver is currently used on 64-bit platforms > > only, where long is 64-bit, so this cannot happen. But the issue may > > start to happen when the driver is reused for a 32-bit SoC, or when code > > is copied for a new driver. > > > > Signed-off-by: Geert Uytterhoeven > > --- a/drivers/gpu/drm/bridge/nwl-dsi.c > > +++ b/drivers/gpu/drm/bridge/nwl-dsi.c > > @@ -195,7 +195,7 @@ static u32 ps2bc(struct nwl_dsi *dsi, unsigned long long ps) > > u32 bpp = mipi_dsi_pixel_format_to_bpp(dsi->format); > > > > return DIV64_U64_ROUND_UP(ps * dsi->mode.clock * bpp, > > - dsi->lanes * 8 * NSEC_PER_SEC); > > + dsi->lanes * 8ULL * NSEC_PER_SEC); > > I wonder if we could get rid of a whole class of bugs by turning > NSEC_PER_SEC into a ULL, but I suppose there are valid cases where a > 32-bit integer is enough. Indeed, and 64-bit arithmetic is more expensive on 32-bit platforms. I considered that change, but doing so would require updates all over the place (e.g. printing a value derived from NSEC_PER_SEC, divisions that need to be changed to do_div or div_u64(), ...) Note that the selftests already use such a definition. > Reviewed-by: Laurent Pinchart Thanks! > How did you come across this by the way ? https://lore.kernel.org/linux-renesas-soc/CAMuHMdXQvPY_mYicjPKjDSCwdO_rP-9PJOvqD0J6=S3Opr1ycg@mail.gmail.com/ and of course I grepped for similar use patterns... Gr{oetje,eeting}s, Geert -- Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that. -- Linus Torvalds