From patchwork Tue Aug 7 11:33:58 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mauro Carvalho Chehab X-Patchwork-Id: 10558577 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 89F3515A6 for ; Tue, 7 Aug 2018 11:34:04 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 77608283A5 for ; Tue, 7 Aug 2018 11:34:04 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6B458290C1; Tue, 7 Aug 2018 11:34:04 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0799F283A5 for ; Tue, 7 Aug 2018 11:34:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727155AbeHGNr4 (ORCPT ); Tue, 7 Aug 2018 09:47:56 -0400 Received: from bombadil.infradead.org ([198.137.202.133]:44654 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726555AbeHGNr4 (ORCPT ); Tue, 7 Aug 2018 09:47:56 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=Sender:Message-Id:Date:Subject:Cc:To: From:Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=4rBq8Fp3hvJxZ8VM8/Wb9B8kx6WBDYAwWneYNfc5z8g=; b=dTYyiKkix2J2uVaiJYuyU4EyR0 y8huUhMn9sT3GMYVJWaPUvyfjWoKBsYw1YlHKnLACQ6qHJTz5w+5T2dsjE4wXk4l+l4yvqAaV/zxB /HxriLafkf0hTdQKrLAUxkAxIdFhRpZXZhMzOQwG2ZqlzvIc6wNRCLqUFEQwQ7zSUDY+7cbo6nntU Nft6EWt3rVnbui88zoqzxE3WSx47aSDN4IYLz6OYWFsj7v68l+TlEdrkC5hiZ3QtJHC9ntggtK4rX 2m+BYoPHxBMCKqWebQdesqOpeE371nuiHpsxRcyOvfNOtfMd4vOJVquKihAuunT0Zf4O2priIg6hI MdDYtZPA==; Received: from [179.177.31.111] (helo=bombadil.infradead.org) by bombadil.infradead.org with esmtpsa (Exim 4.90_1 #2 (Red Hat Linux)) id 1fn0FV-0003aE-16; Tue, 07 Aug 2018 11:34:01 +0000 Received: from mchehab by bombadil.infradead.org with local (Exim 4.91) (envelope-from ) id 1fn0FS-0006id-UG; Tue, 07 Aug 2018 07:33:58 -0400 From: Mauro Carvalho Chehab Cc: Mauro Carvalho Chehab , Linux Media Mailing List , Mauro Carvalho Chehab , Hans Verkuil , Jiri Kosina Subject: [PATCH] media: vivid: shut up warnings due to a non-trivial logic Date: Tue, 7 Aug 2018 07:33:58 -0400 Message-Id: X-Mailer: git-send-email 2.17.1 To: unlisted-recipients:; (no To-header on input) Sender: linux-media-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-media@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The vivid driver uses a complex logic to save one kalloc/kfree allocation. That non-trivial way of allocating data causes smatch to warn: drivers/media/platform/vivid/vivid-core.c:869 vivid_create_instance() warn: potentially one past the end of array 'dev->query_dv_timings_qmenu[dev->query_dv_timings_size]' drivers/media/platform/vivid/vivid-core.c:869 vivid_create_instance() warn: potentially one past the end of array 'dev->query_dv_timings_qmenu[dev->query_dv_timings_size]' I also needed to read the code several times in order to understand what it was desired there. It turns that the logic was right, although confusing to read. As it is doing allocations on a non-standard way, let's add some documentation while shutting up the false positive. Signed-off-by: Mauro Carvalho Chehab --- drivers/media/platform/vivid/vivid-core.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/drivers/media/platform/vivid/vivid-core.c b/drivers/media/platform/vivid/vivid-core.c index 31db363602e5..4c235ea27987 100644 --- a/drivers/media/platform/vivid/vivid-core.c +++ b/drivers/media/platform/vivid/vivid-core.c @@ -647,6 +647,7 @@ static int vivid_create_instance(struct platform_device *pdev, int inst) unsigned node_type = node_types[inst]; unsigned int allocator = allocators[inst]; v4l2_std_id tvnorms_cap = 0, tvnorms_out = 0; + char *strings_base; int ret; int i; @@ -859,17 +860,33 @@ static int vivid_create_instance(struct platform_device *pdev, int inst) /* create a string array containing the names of all the preset timings */ while (v4l2_dv_timings_presets[dev->query_dv_timings_size].bt.width) dev->query_dv_timings_size++; + + /* + * In order to save one allocation and an extra free, let's optimize + * the allocation here: we'll use the first elements of the + * dev->query_dv_timings_qmenu to store the timing strings pointer, + * adding an extra space there to a store a string up to 32 bytes. + * So, instead of allocating an array with size of: + * dev->query_dv_timings_size * (sizeof(void *) + * it will allocate: + * dev->query_dv_timings_size * (sizeof(void *) + + * dev->query_dv_timings_size * 32 + */ dev->query_dv_timings_qmenu = kmalloc_array(dev->query_dv_timings_size, (sizeof(void *) + 32), GFP_KERNEL); if (dev->query_dv_timings_qmenu == NULL) goto free_dev; + + /* Sets strings_base to be after the space to store the pointers */ + strings_base = ((char *)&dev->query_dv_timings_qmenu) + + dev->query_dv_timings_size * sizeof(void *); + for (i = 0; i < dev->query_dv_timings_size; i++) { const struct v4l2_bt_timings *bt = &v4l2_dv_timings_presets[i].bt; - char *p = (char *)&dev->query_dv_timings_qmenu[dev->query_dv_timings_size]; + char *p = strings_base + i * 32; u32 htot, vtot; - p += i * 32; dev->query_dv_timings_qmenu[i] = p; htot = V4L2_DV_BT_FRAME_WIDTH(bt);