Received: by 2002:a05:6358:f14:b0:e5:3b68:ec04 with SMTP id b20csp4229296rwj; Tue, 20 Dec 2022 07:37:10 -0800 (PST) X-Google-Smtp-Source: AA0mqf5CccDKoOpeJK4jShJYtiV2/WtOgd33OcljHP9AtMDS6ujWgDVdQdXi5k5eH/0gWchPx6pU X-Received: by 2002:a05:6402:c0b:b0:46c:a763:5889 with SMTP id co11-20020a0564020c0b00b0046ca7635889mr56243765edb.25.1671550630012; Tue, 20 Dec 2022 07:37:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671550629; cv=none; d=google.com; s=arc-20160816; b=DlFLm0YiOzrQtna1MpJjvGXITvnLcPW98c3u2d28uh4jvuvXZRV+O8oynNp1YEeXfB QVj7woSkQiRXKCZ3NT0pDJYg+NDi3fBzh24Ngp168q24AcGAaZtnq3KaPmT8iyPWVtBs RZlL1P3ncJohbUO8J2OLQ5No7lgdLI9Wy1IxFyD/VAS3NGddHXyg3l1ElY0A1ji4FzgW kGToVKKvJHjrZBDkMjyqQuTM84A4kIqi+EWEeFQFDv4/lujbcUR/JcXHVHWQAcp2hsrD urjZlXYDx30J9cHMSt76TDqukzHs0IIvvpBiuQCbsgl+plrNTHHHQTu7mYfICYX8ubvp Th8Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=NHWMu04jyibAGOW4W1VvbeNX5B+1pD8aW975+MoTZKY=; b=BR7fztlJnVFx00LgYxjXzl8JhPLWp5Uh9qd7Bcah/hCzXx/v01veVw1IbSvEw471kK NZ08mKg51EOB7Y0F6G/DJ6BPkA9MiooQmEQXRTWTDyHhPixcRS15CAxalZcZG5knKIUT 7BwvFAzmlhYbNS6DJPDCaEL3fFYqK05UshtLDm47fvF2q1+jMXL8X2b+K6ltDLHlC5Qf qgWWIgLIW4dL93E7uDEWaodPvyYq+U7K540puy82RqcqPxPi+0iLXY3XRUJPV9RUwG3d sh9EuouKrLnjZA4qnDmWZ4r6lgKHGbGnUFRCoOv9b3oQ9sOoJDhKCETnofOI2exFgfNo ajjA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@mailo.com header.s=mailo header.b="l1PgyQg/"; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=mailo.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id b10-20020a056402350a00b00447d6f244c6si12618666edd.248.2022.12.20.07.36.54; Tue, 20 Dec 2022 07:37:09 -0800 (PST) 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; dkim=fail header.i=@mailo.com header.s=mailo header.b="l1PgyQg/"; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=mailo.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233888AbiLTO0X (ORCPT + 69 others); Tue, 20 Dec 2022 09:26:23 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52934 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231790AbiLTO0L (ORCPT ); Tue, 20 Dec 2022 09:26:11 -0500 Received: from msg-2.mailo.com (msg-2.mailo.com [213.182.54.12]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 36BA1323 for ; Tue, 20 Dec 2022 06:26:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=mailo.com; s=mailo; t=1671546341; bh=ocdMdN7aEukCgNOvKCL1W/7c1n4xooH9oWX7X7O+9ak=; h=X-EA-Auth:Date:From:To:Cc:Subject:Message-ID:References: MIME-Version:Content-Type:In-Reply-To; b=l1PgyQg/4arUTt0mAgJ10WbsIiwWHvd0ZrJHsbXkcIB0xKLObamq1xsuAGDc75zbP lIZXuTZfP/mfc7rC6G13Kc4g9Flw11LORFK6l9+oj6l6MYkxMcB8tNXqaUti1lfLT9 ndCZVUIew/ZK4W7KyopKYQRP5et/oIvjvCSeIwKE= Received: by b-5.in.mailobj.net [192.168.90.15] with ESMTP via ip-206.mailobj.net [213.182.55.206] Tue, 20 Dec 2022 15:25:41 +0100 (CET) X-EA-Auth: LE+8xCnx1iNwbhLYvZp7EU7RNbGwdu2L+4eniJiGWquz5sL9G98+IX8GwLepM7zVR20NCngc7VtGoFwZFxWZjewJfBbtndOl Date: Tue, 20 Dec 2022 19:55:36 +0530 From: Deepak R Varma To: "Gustavo A. R. Silva" Cc: Julia Lawall , "Gustavo A. R. Silva" , Saurabh Singh Sengar , Praveen Kumar , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: Re: kvzalloc vs kvcalloc Message-ID: References: <26b8353-dd33-1a57-d7b5-dc6a8583219@inria.fr> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=ham 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 Tue, Dec 20, 2022 at 08:13:19AM -0600, Gustavo A. R. Silva wrote: > > > On 12/20/22 01:48, Julia Lawall wrote: > > > > > > On Tue, 20 Dec 2022, Deepak R Varma wrote: > > > > > On Tue, Dec 20, 2022 at 08:39:09AM +0100, Julia Lawall wrote: > > > > > > > > > > > > On Tue, 20 Dec 2022, Deepak R Varma wrote: > > > > > > > > > On Tue, Dec 20, 2022 at 07:08:24AM +0100, Julia Lawall wrote: > > > > > > > > > > > > > > > > > > On Tue, 20 Dec 2022, Deepak R Varma wrote: > > > > > > > > > > > > > Hello Gustavo and Julia, > > > > > > > I was working on building a patch proposal using the kvmalloc.cocci file for a > > > > > > > driver. The recommendation from the semantic patch is to use kvzalloc instead of > > > > > > > a fallback memory allocation model. Please see my patch submitted here [1]. > > > > > > > > > > > > > > I also found another patch submitted by Gustavo [2] which suggests using > > > > > > > kvcalloc instead of kvzalloc. Unfortunately, I was not able to understand the > > > > > > > reasons/advantages using kvcalloc over kvzalloc. > > Look for the definitions of those functions and try to understand their differences. > In many cases you have go down the rabbit hole, but you should be able to get a good > grasp of the thing in question before hitting the bottom. :) > > Look for a couple of instances in the codebase where those functions are being used > and try to understand a bit of the context around them. In some cases reading the > commit logs is necessary. Hello Gustavo, Thank you very much for the suggestion here. I will get deeper into the codebase and try to self learn. Your advise on reading the past commit logs is useful as well. Thank you again! ./drv > > > > > > > > > > > > > The calloc variants are for zeroed arrays. zalloc variants just zero. > > > > > > > > > > Thank you Julia and sorry to have missed the references in my email: > > > > > > > > In Gustavo's case, the array has a certain number of elements of a certain > > > > size. I don't know if you have both pieces of information in your case. > > > > calloc functions take them in separately, and do the multiplication in a > > > > way that checks for overflows. > > > > > > That is correct and I do have both the pieces, the size and number. This > > > actually further optimizes the code. We can eliminate the array_size variable > > > with the kvcalloc implementation. It is not used beyond the memory allocation. > > > > > > Please this code snip: > > > > > > 853 int count = size >> PAGE_SHIFT; > > > 1 int array_size = count * sizeof(struct page *); > > > 2 int i = 0; > > > 3 int order_idx = 0; > > > 4 > > > 5 pages = kvzalloc(array_size, GFP_KERNEL); > > > 6 if (!pages) > > > 7 return NULL; > > > > > > Thank you for your advise. I will wait to see Gustavo has any further guidance. > > > I will send in a revision to my patch accordingly. > > > > Great. A calloc function definitely looks like a good choice here. > > As Julia suggested, and as you may had realized already, the calloc function is the > way to go, in this case. > > -- > Gustavo > >