From patchwork Thu Jan 20 08:52:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Guangming.Cao" X-Patchwork-Id: 12718453 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 58834C433FE for ; Thu, 20 Jan 2022 08:53:37 +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-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:CC:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=/SqJ7Pcel2CkZmsxkp8VreXiYY1NHN+APNV3jxQgBm0=; b=DuxRSGv394P71k 1xmI0uI+sRyomSatYyySDxtctkCTJUX/1+x8VA+cDvLqAiEt/htUHY4U5M6LWQQWDDaKqhORFDtKA EDRec7kL77VYpUZQWuMQR7SsbDeUxDBwhxsLUJMfSTG+3b0EIN5BjgyN2lUjt58WmgrUEh8s/48Qi ykoPBIgcdFp6S/KvbvPFef4mrFwtuka4a6mQYFqfyeCtGeNowwjDqFFlsqRBBoXbmp5BRmg+Xqdjg SLgc8zoFg7L3fj6KICxBQDdmHeaw0cz0Qi4v45MVyz951FhgjQ7CjlYoLB2n+j8Zr5D6YWIcEU3/X DgAzsaQLZxSCAuGOma8g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1nATBD-009wI9-3O; Thu, 20 Jan 2022 08:52:27 +0000 Received: from mailgw01.mediatek.com ([216.200.240.184]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1nATB4-009wCk-Oo; Thu, 20 Jan 2022 08:52:22 +0000 X-UUID: 7cdba5cdbd494ac28ea34d09dff1d48d-20220120 X-UUID: 7cdba5cdbd494ac28ea34d09dff1d48d-20220120 Received: from mtkcas66.mediatek.inc [(172.29.193.44)] by mailgw01.mediatek.com (envelope-from ) (musrelay.mediatek.com ESMTP with TLSv1.2 ECDHE-RSA-AES256-SHA384 256/256) with ESMTP id 1687681759; Thu, 20 Jan 2022 01:52:16 -0700 Received: from mtkexhb02.mediatek.inc (172.21.101.103) by MTKMBS62N2.mediatek.inc (172.29.193.42) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Thu, 20 Jan 2022 00:52:15 -0800 Received: from mtkcas11.mediatek.inc (172.21.101.40) by mtkexhb02.mediatek.inc (172.21.101.103) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Thu, 20 Jan 2022 16:52:01 +0800 Received: from mszswglt01.gcn.mediatek.inc (10.16.20.20) by mtkcas11.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.0.1497.2 via Frontend Transport; Thu, 20 Jan 2022 16:52:00 +0800 From: To: CC: , , , , , , , , , , , , , , , , , , , , , Guangming Subject: [PATCH v6] dma-buf: system_heap: Add a size check for allocation Date: Thu, 20 Jan 2022 16:52:59 +0800 Message-ID: <20220120085259.98147-1-guangming.cao@mediatek.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <4f2631d3-0845-cae6-6fc0-9c00ce6301ce@amd.com> References: <4f2631d3-0845-cae6-6fc0-9c00ce6301ce@amd.com> MIME-Version: 1.0 X-MTK: N X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220120_005218_816432_99AE9AAC X-CRM114-Status: GOOD ( 12.27 ) 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: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: Guangming Add a size check for allocation since the allocation size should be always less than the total DRAM size on system heap. Adding this check can prevent comsuming too much time for invalid allocations. Signed-off-by: Guangming --- drivers/dma-buf/heaps/system_heap.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/drivers/dma-buf/heaps/system_heap.c b/drivers/dma-buf/heaps/system_heap.c index 23a7e74ef966..b65e597a742f 100644 --- a/drivers/dma-buf/heaps/system_heap.c +++ b/drivers/dma-buf/heaps/system_heap.c @@ -347,6 +347,14 @@ static struct dma_buf *system_heap_allocate(struct dma_heap *heap, struct page *page, *tmp_page; int i, ret = -ENOMEM; + /* + * Size check. The "len" should be less than totalram since system_heap + * memory is comes from system. Adding check here can prevent comsuming + * too much time for invalid allocations. + */ + if (PFN_DOWN(len) > totalram_pages()) + return ERR_PTR(-EINVAL); + buffer = kzalloc(sizeof(*buffer), GFP_KERNEL); if (!buffer) return ERR_PTR(-ENOMEM);