From patchwork Wed Mar 1 20:34:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak R Varma X-Patchwork-Id: 63091 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp3856032wrd; Wed, 1 Mar 2023 12:37:42 -0800 (PST) X-Google-Smtp-Source: AK7set+pBVvl6lmo3xiSGj4EahpY35uNOc0EpcR9puMbzB7QLPqqFhHEZ523rna2+E6I7hIBW8Ph X-Received: by 2002:a17:90b:3ec8:b0:237:40a5:7cb9 with SMTP id rm8-20020a17090b3ec800b0023740a57cb9mr9017386pjb.5.1677703061662; Wed, 01 Mar 2023 12:37:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677703061; cv=none; d=google.com; s=arc-20160816; b=wk9nknCYoQgAtHte/8+OZzAsCk7NEjpwLbOkJTt48lfBX1MAvlJONSth+KEROqgiXB ElwXDW6R8SJh862/sS20BnyocbSxCrJVBMuQLyKjo5W9OkBlwrbxXfTPqe1WaET20DX4 U4mZjB7dEDA/Sws6fR+nvsnVEJF5vJJzPWm1BNT+muBYJP+IMHFUdn0GcS3CXqck84mA MOZUpVDL8WmW0Z3cUThnERNSmV00FIq4QqEvxVXoGFvAc5zex0JC4wJME5rJ9G4mkEm+ sST22PA1UT6ma2DvPRYwgOrxUvHUlcllqx5mw8pJ6B6G4VJsCAYy9NeUsPQLoepg4bC8 HVyA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-disposition:mime-version:message-id :subject:cc:to:from:date:dkim-signature; bh=7UtCXgoy7Vg8AORv/dVfPDLDpQq4mIFHPEFMv41YNNU=; b=oya0O1dQ/XA8Di32oFSjHFHSPbhmH11QzmFg1CKb0sgx4jSQb6U+z0ysn75ONPxEI1 m2k2mMELCpvEoV1qA2YkIMelmgm5MzUASTTogmDi4gLZXoArrKniOXY9F3aEQO0ds86S k6Ej3pCPGTwOdprpOXmWVyh6Y/LFCXyROCA3hhkUdKqKtPWGkGV/XSU1oUbSQvvJtKzO 6IaJPLCVvh77mH/jSI84GOuhZDREZgUn8p3pnd0PS79zeUt4PiuzpIqImJ/oCrnItF+N JucHi00wt0RcL7zvEsUwJM7cfNYGuI5MfPjecCXu4JALd7y4ol6lt4UyNxOlbbVOU/X5 dB/Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@mailo.com header.s=mailo header.b=SV67Cf92; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r29-20020a63441d000000b004e63b6b399esi12680591pga.570.2023.03.01.12.37.26; Wed, 01 Mar 2023 12:37:41 -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=SV67Cf92; 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 S229625AbjCAUej (ORCPT + 99 others); Wed, 1 Mar 2023 15:34:39 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43512 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229685AbjCAUef (ORCPT ); Wed, 1 Mar 2023 15:34:35 -0500 Received: from msg-1.mailo.com (msg-1.mailo.com [213.182.54.11]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 44BCC1EFE6 for ; Wed, 1 Mar 2023 12:34:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=mailo.com; s=mailo; t=1677702845; bh=cqhsqYK7s7Yv/LkadhMlwHKVAf/GwA6BgH0SYBCjK3c=; h=X-EA-Auth:Date:From:To:Cc:Subject:Message-ID:MIME-Version: Content-Type; b=SV67Cf92RdpesLA+epnLUUwQ8ZLgNohyNqZRhFLAOq80COPiPXB3hgR8mznJZVfjP 3m4oCFXFjFOWfXXrJYEogMtoHzSh8bbkQB/OfYvZ8CZZU/f0xUt03CjFDnzdZbLiym jE1WEG4Y17PijLn7zmJseTyZsGkBlq6nu3DiUj90= Received: by b221-5.in.mailobj.net [192.168.90.25] with ESMTP via ip-20.mailobj.net [213.182.54.20] Wed, 1 Mar 2023 21:34:05 +0100 (CET) X-EA-Auth: /wndk3dqHvi9hZWviS3qFQdI+Q4UcspbU5HoMfbA2+G+CvBhgYULaiM74ySQHlU5cEzfS6+lxcj//p4t1DkKjgyorGybEa/n Date: Thu, 2 Mar 2023 02:04:01 +0530 From: Deepak R Varma To: Christoph Hellwig , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Russell King Cc: Saurabh Singh Sengar , Praveen Kumar , Deepak R Varma Subject: [PATCH v3 RESEND] ARM/dma-mapping: use kvcalloc for fallback memory allocation need Message-ID: MIME-Version: 1.0 Content-Disposition: inline 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1759199165846315321?= X-GMAIL-MSGID: =?utf-8?q?1759199165846315321?= 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 kvzalloc()") Signed-off-by: Deepak R Varma --- Changes in v3: 1. Use updated maintainer list to send the patch. Suggested by "Russell King (Oracle)" 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 8bc01071474a..1c3a1a49cbe2 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 device *dev, size_t size, { struct page **pages; int count = size >> PAGE_SHIFT; - int array_size = count * sizeof(struct page *); int i = 0; int order_idx = 0; - if (array_size <= PAGE_SIZE) - pages = kzalloc(array_size, GFP_KERNEL); - else - pages = vzalloc(array_size); + pages = kvcalloc(count, sizeof(struct page *), GFP_KERNEL); if (!pages) return NULL;