From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 42D70C4332F for ; Tue, 20 Dec 2022 14:14:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:Content-Type: Content-Transfer-Encoding:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:In-Reply-To:From:References:Cc:To:Subject: MIME-Version:Date:Message-ID:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=ulAt+C4q8HZS/kyCyCNRiQ9ArR4IE3tOtYyDPEVqfko=; b=0dbwuN/+l/Zcgj vnlwg+52E5ktUQoMTSXMbTf9gAzqRXwilXTgllLoWfmY+/Uh3PTR24onaH9yPDROncma9hVy7H24i vw7eSdhs6kkifB9zCAFmMRCXGhXKz0DDeTjVM679CxkDZoSr2HCSY1c83/iQGELQk7vT+msIr8+Zu Q6ppHm0neudTcitQkxby4+JRZir2wyJJRdmsNhsxYadf2pQ+J8nFUyDgnaER1jm2XUo9hLCBVyppj +usezB9pTGtExlqFtfmsQZy8HXb7D8cPMQ+GpIZKQOFZtdMS7A4oyxDLnWJTVOTjJBePaYaCy4Dit R6Vv5fECbLkl0pPb2reg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1p7dNI-00GXD3-9o; Tue, 20 Dec 2022 14:13:44 +0000 Received: from omta37.uswest2.a.cloudfilter.net ([35.89.44.36]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1p7dNF-00GX6Y-8p for linux-arm-kernel@lists.infradead.org; Tue, 20 Dec 2022 14:13:42 +0000 Received: from eig-obgw-6017a.ext.cloudfilter.net ([10.0.30.175]) by cmsmtp with ESMTP id 7XHSpBaTmYHR17dN2pL7ZT; Tue, 20 Dec 2022 14:13:28 +0000 Received: from gator4166.hostgator.com ([108.167.133.22]) by cmsmtp with ESMTP id 7dN1pYaGd3Dmj7dN1p2HsI; Tue, 20 Dec 2022 14:13:27 +0000 X-Authority-Analysis: v=2.4 cv=K5wxogaI c=1 sm=1 tr=0 ts=63a1c307 a=1YbLdUo/zbTtOZ3uB5T3HA==:117 a=wTog8WU66it3cfrESHnF4A==:17 a=dLZJa+xiwSxG16/P+YVxDGlgEgI=:19 a=IkcTkHD0fZMA:10 a=sHyYjHe8cH0A:10 a=wYkD_t78qR0A:10 a=639K83N8K81KkhpRCrwA:9 a=QEXdDO2ut3YA:10 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=embeddedor.com; s=default; h=Content-Transfer-Encoding:Content-Type: In-Reply-To:From:References:Cc:To:Subject:MIME-Version:Date:Message-ID:Sender :Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help: List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=ru/CWfY1zVyiTyJwoTKjmy7OCqcrMyO4TLbc3pFhpbc=; b=rV+/MUtKiXZK+lMISNI1rK+Ntl CKJeMbqOHAUowT9ymyRLjYRrBUcG+dL1AHa7B/kxrzkXx2pmFLz6FWsN0Zwgvv9TzfquDWS2826ow QikhurLgHTVQFgwrpuqIgMXRCh7UlezVA+fDuSJJVma2DKakKHcEcOouBLFeTl3/yx2U5zzL1+Vj+ ybjnVF1mIG2SHgogY7CV4z96Cqsy00H1IPpaln35twdy2l3F0kDVO7YwpXwjC67XDkNfoCu3rshLN urcWl/OIFzuIWxub94U9QCz2vts8xoma99s+ONW0r1jGRGvofkX+UXCloFbdbYppOrgfQSeQfj3/4 JDiWjqJA==; Received: from 187-162-31-110.static.axtel.net ([187.162.31.110]:43264 helo=[192.168.15.7]) by gator4166.hostgator.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.95) (envelope-from ) id 1p7dN0-001StS-JD; Tue, 20 Dec 2022 08:13:26 -0600 Message-ID: Date: Tue, 20 Dec 2022 08:13:19 -0600 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.2 Subject: Re: kvzalloc vs kvcalloc Content-Language: en-US To: Julia Lawall , Deepak R Varma Cc: "Gustavo A. R. Silva" , Saurabh Singh Sengar , Praveen Kumar , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org References: <26b8353-dd33-1a57-d7b5-dc6a8583219@inria.fr> From: "Gustavo A. R. Silva" In-Reply-To: <26b8353-dd33-1a57-d7b5-dc6a8583219@inria.fr> X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - gator4166.hostgator.com X-AntiAbuse: Original Domain - lists.infradead.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - embeddedor.com X-BWhitelist: no X-Source-IP: 187.162.31.110 X-Source-L: No X-Exim-ID: 1p7dN0-001StS-JD X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 187-162-31-110.static.axtel.net ([192.168.15.7]) [187.162.31.110]:43264 X-Source-Auth: gustavo@embeddedor.com X-Email-Count: 2 X-Org: HG=hgshared;ORG=hostgator; X-Source-Cap: Z3V6aWRpbmU7Z3V6aWRpbmU7Z2F0b3I0MTY2Lmhvc3RnYXRvci5jb20= X-Local-Domain: yes X-CMAE-Envelope: MS4xfOT3smW3197Z+3yhS/uBNQDjPBSdDA543catY2LDS1Q+q/awv4HVtyzvpp5ndXd4LBLmzLoJjx/mCsXN4T4DqruWo/lHE6ErxBLyfgmWmV2Svn6CxZR4 +Gw97qAKrbDcJLx5aP+jh+cwWFQknlRyTZXSiu43oDWZVlE6o4T9qTRjUOlEzsJXQ9loPaN/YNJDuOiW+QsZXbMZSwMyUXH5PB6Ji2DKxR8enRq8f/M53ld3 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20221220_061341_466473_208FA183 X-CRM114-Status: GOOD ( 14.79 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="us-ascii"; Format="flowed" Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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. >>>>> >>>>> 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 _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel