From patchwork Tue Jan 3 10:56:38 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: 38375 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp4552210wrt; Tue, 3 Jan 2023 03:03:43 -0800 (PST) X-Google-Smtp-Source: AMrXdXta/FTXcmvMrvLxD9gO0NlSjrlYXcM5ytbwIB8b6B4t0+rnU4snlNtsNiP14AaeSAkL8EID X-Received: by 2002:a05:6a21:c08d:b0:af:f1d1:6f7f with SMTP id bn13-20020a056a21c08d00b000aff1d16f7fmr59326068pzc.31.1672743823386; Tue, 03 Jan 2023 03:03:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672743823; cv=none; d=google.com; s=arc-20160816; b=NBnwVgnnDJjZ7m/5pltm/bq5q18dL1UTDe1Egl6Z5++rCTDVhzsT+q0Pe+yangHLYV dFQ6FkEoIpsn7JsBoGPBhlbskM3ajxiRtKU91BY1YTs1TDUJocKXphuSImA4qUw/JsAt WmTvHZRO1qmAFsNRVIdMCJdEkWRzP6TQVDHRRvBxmT7dBnqxeY3DExZzYGfci+Bkv0u6 WRAfxDjsoRwhWRdny3hydYeazDgOyutrD9adOD6C+H9mWzk7utkY1w4s6FpUJ2p717EF 7Njq2NreFtZB3NCm5iFZCqjw3y5Rg/f6TAXGfwRGsvdtVZanqTXVLfnzCiRAT5CpnTYh dzWA== 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=3nYvH0OueJM0EDts386LqahcYgtAZY5WRuWiJu2/Lwc=; b=uozVthGD6JlAwEiiPlaZFltQxpmhZTogWT5xFH2PFwoe4BOkc0qaOFCD1Zjbi4V9ID V0MmFAPY4qawC9QyHVjvOj9jfLE+ALKcR6032XlIJIHSaQiAYabry1RV/YbDmj+aJUVC o0XQYcDe286Bt3UVYAoejcMFBwWml5L6UeU7KHZ3RsEo/2gbR7T+yPJ4mMOUvJs8JdDM Gv06HGeIvhRP7unz6NeY/8Cl+Ls+TYw/vLUKfyGIiLtfZJ8wVhDWllqe8SdITK+KQTkG MAOdDQgNSD+UHBXCayTeRVlIxWRCjZklJT4KQSJFrvFoDkpLZHEksHQnFeXsyqQs4izk DDWQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@mailo.com header.s=mailo header.b=eyFY5Of7; 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 o13-20020a634e4d000000b00478bc014a29si32844336pgl.43.2023.01.03.03.03.24; Tue, 03 Jan 2023 03:03:43 -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=eyFY5Of7; 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 S237336AbjACK44 (ORCPT + 99 others); Tue, 3 Jan 2023 05:56:56 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43060 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237406AbjACK4y (ORCPT ); Tue, 3 Jan 2023 05:56:54 -0500 Received: from msg-2.mailo.com (msg-2.mailo.com [213.182.54.12]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8E879E096 for ; Tue, 3 Jan 2023 02:56:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=mailo.com; s=mailo; t=1672743403; bh=oNYF4pzEaAnYnEpzvzjLME7qGqVRc2/8UlmMC64c5Jg=; h=X-EA-Auth:Date:From:To:Cc:Subject:Message-ID:MIME-Version: Content-Type; b=eyFY5Of7InJ/X3RnIWhI6rtfOeGmAM+D27h+U03rwAkGd5luBQ9nMhX/VTiq4FAyY Bv8gVUGk4fkjT8s2VCeIhqkOT6ceV60uL85Sn+s//90fb5nSj6YXw+86mk0mRbOe06 6GrU6ogsxCUYu4Au/YI+KYGqX9d09xBueG8NDIEs= Received: by b-1.in.mailobj.net [192.168.90.11] with ESMTP via ip-206.mailobj.net [213.182.55.206] Tue, 3 Jan 2023 11:56:43 +0100 (CET) X-EA-Auth: rdJ5S1EG72eSCgTZuI9ynn+6BkPv/y5JmmNxDwmqIicELpIhZGvO4XFp6KW03WAhZL/vghevtD3SkgaYJc9kQsFk9KB2Axsg Date: Tue, 3 Jan 2023 16:26:38 +0530 From: Deepak R Varma To: Christoph Hellwig , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Russell King , Saurabh Singh Sengar , Praveen Kumar , Deepak R Varma , Julia Lawall Subject: [PATCH v3] 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?1752743199000671614?= X-GMAIL-MSGID: =?utf-8?q?1753999027760001949?= 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(-) -- 2.34.1 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 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;