From nobody Wed Sep 17 10:05:29 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1C82CC4332F for ; Tue, 20 Dec 2022 14:18:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233961AbiLTOSN (ORCPT ); Tue, 20 Dec 2022 09:18:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45458 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233948AbiLTORi (ORCPT ); Tue, 20 Dec 2022 09:17:38 -0500 Received: from msg-2.mailo.com (msg-2.mailo.com [213.182.54.12]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 593D2E0F2 for ; Tue, 20 Dec 2022 06:16:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=mailo.com; s=mailo; t=1671545803; bh=KFy9i0U+bz5w8ZNQPETAfc303KW3oDGaFSwUS0I9Z7Y=; h=X-EA-Auth:Date:From:To:Cc:Subject:Message-ID:MIME-Version: Content-Type; b=Afvz7mrsiDCgrGiDOYGru1q/4CJKU+h4mzfJJv9UqrwG4ed3Qi3AVAFKJIg4/7qee ClaTK7yGkMDRp5fmDVp9cUXfOH0XmGZ7NVxPyVzUi58fuBJ4xSS2110W6Uiq+KeT3p pQy4hAox9nBiqXnb+3sP6G2hYNF+lLAHQW4iEIfM= 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:16:43 +0100 (CET) X-EA-Auth: ht9cxGNE36MfwDJyGTplQ3KyBpF29jM0Yl/k5AFuB85lfHkYRBCHHBwHRBFEOuzVkAcq944OAyB/kI1/c0sM/TPaAgdnykvt Date: Tue, 20 Dec 2022 19:46:32 +0530 From: Deepak R Varma To: Russell King , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Praveen Kumar , Saurabh Singh Sengar , Julia Lawall , drv@mailo.com Subject: [PATCH v2] ARM/dma-mapping: use kvcalloc for fallback memory allocation need Message-ID: MIME-Version: 1.0 Content-Disposition: inline Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Current conditional determination of whether to use kzalloc or vzalloc has known issues such as "indefinite retry" when less than PAGE_SIZE memory is needed, but is unavailable. This LWN article [1] describes these issues in greater detail. Use helper function kvcalloc() instead which is more efficient in terms of performance and security. [1] https://lwn.net/Articles/711653/ This patch proposal is based on following Coccinelle warning using the kvmalloc.cocci semantic patch. arch/arm/mm/dma-mapping.c:858:28-29: WARNING opportunity for kvmalloc The semantic patch suggests using kvzalloc() helper function, however, this patch proposes to use kvcalloc instead. kvcalloc() helper function uses 2-factor argument form which is better from a security perspective as described in the following KSPP project commit. Commit 4e3fd7217105 ("wireguard: ratelimiter: use kvcalloc() instead of kv= zalloc()") Signed-off-by: Deepak R Varma --- Changes in v2: 1. Update patch subject to use kvcalloc 2. Use kvcalloc instead of kvzalloc helper function. Revise the patch proposal and the patch description accordingly. arch/arm/mm/dma-mapping.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/arch/arm/mm/dma-mapping.c b/arch/arm/mm/dma-mapping.c index c135f6e37a00..35092ecd30e1 100644 --- a/arch/arm/mm/dma-mapping.c +++ b/arch/arm/mm/dma-mapping.c @@ -851,14 +851,10 @@ static struct page **__iommu_alloc_buffer(struct devi= ce *dev, size_t size, { struct page **pages; int count =3D size >> PAGE_SHIFT; - int array_size =3D count * sizeof(struct page *); int i =3D 0; int order_idx =3D 0; - if (array_size <=3D PAGE_SIZE) - pages =3D kzalloc(array_size, GFP_KERNEL); - else - pages =3D vzalloc(array_size); + pages =3D kvcalloc(count, sizeof(struct page *), GFP_KERNEL); if (!pages) return NULL; -- 2.34.1