Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp1238262pxb; Fri, 21 Jan 2022 13:04:57 -0800 (PST) X-Google-Smtp-Source: ABdhPJzahSiw9DN6fp86QJrS3P7b4vnQfJnkQEto1mkPGYM2YfZiCP2YEQkwvxau3+sm81jY+ZSt X-Received: by 2002:a17:90a:cf8e:: with SMTP id i14mr2434645pju.237.1642799097083; Fri, 21 Jan 2022 13:04:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1642799097; cv=none; d=google.com; s=arc-20160816; b=JSWlDWl3b9M1zoadrfbNY6OCcsnigcai2eFyZVVi6XixuSalKVbD93v7Pp0YyilwJw m95s1lxkHP+Ko/Gyel5lrRB2s0P7i7OfHbx2if/fiP7UIv3gAItGNC6oP15D0UdvFpBB ppQGifozKjOBsJ3BDiZwC+x8DTKZoKz5D5mD89QSUEgSHOZYorGXFnnoLr2NkTq2IUa1 BWt6ne9qYU0RlwP+QC914uQSI2ARRAJ1uazR1Q1nLneONsJP5FW2nygRTopWOHv8H8TR 1yfeweza7sQZrn4laUia7g4l0hnNXMZLMhXYiiq5+0JRFUUoND0A420VjaXaODOjP9MR 70lw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from; bh=iTimLbkvtsK/znYb7eBdJPah7yqByjvI0J68r4iXQ+Q=; b=e11rGzu3iF5JrWDydLD2L7Ji/HfuUKqfNwzwsQM5kYqWVvT4+LtZheSY/EELqU1QYT 9xxMFksY8on6wQ128bUUhlHj+0EM5t/0quUXdmd1uHqk2HnLdKwKoSoAGWPulqKHATq+ Bhc2Cd6Bo8fzJe4mdLkyjQPXCN4Dco7Omq7uAMmvFT74mLPVVbaOTWMJSIHETlB0Wd7R HvqwI/jfRftq+L8odMWyizeOiW6y3z0aNQxgGcCvPUhS9hAWF7XVJadueSWin7hsCcgD IG8LeDg143RtSWAeBAL2odrWBr8lWDRunV8RsW0TnSxMwLdHbOlBlPs6kS312Cbuwlgi TIcQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=mediatek.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g21si7361200pgb.633.2022.01.21.13.04.44; Fri, 21 Jan 2022 13:04:57 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=mediatek.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1358383AbiATDeH (ORCPT + 99 others); Wed, 19 Jan 2022 22:34:07 -0500 Received: from mailgw01.mediatek.com ([60.244.123.138]:41472 "EHLO mailgw01.mediatek.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S234287AbiATDeG (ORCPT ); Wed, 19 Jan 2022 22:34:06 -0500 X-UUID: 0e3172ea1eef46cebc6361e5b4b2992f-20220120 X-UUID: 0e3172ea1eef46cebc6361e5b4b2992f-20220120 Received: from mtkmbs10n2.mediatek.inc [(172.21.101.183)] by mailgw01.mediatek.com (envelope-from ) (Generic MTA with TLSv1.2 ECDHE-RSA-AES256-GCM-SHA384 256/256) with ESMTP id 1117506135; Thu, 20 Jan 2022 11:34:01 +0800 Received: from mtkcas11.mediatek.inc (172.21.101.40) by mtkmbs10n2.mediatek.inc (172.21.101.183) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.2.792.3; Thu, 20 Jan 2022 11:34:00 +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 11:33:59 +0800 From: To: CC: , , , , , , , , , , , , , , , , , , , , , Guangming Subject: [PATCH v4] dma-buf: system_heap: Add a size check for allocation Date: Thu, 20 Jan 2022 11:34:50 +0800 Message-ID: <20220120033450.90164-1-guangming.cao@mediatek.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain X-MTK: N Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.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. And it can prevent consuming too much time for invalid allocations. Signed-off-by: Guangming --- drivers/dma-buf/heaps/system_heap.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/drivers/dma-buf/heaps/system_heap.c b/drivers/dma-buf/heaps/system_heap.c index 23a7e74ef966..bd6f255620e2 100644 --- a/drivers/dma-buf/heaps/system_heap.c +++ b/drivers/dma-buf/heaps/system_heap.c @@ -347,6 +347,13 @@ 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 consuming + * too much time for invalid allocations. + */ + if (len >> PAGE_SHIFT > totalram_pages()) + return -EINVAL; buffer = kzalloc(sizeof(*buffer), GFP_KERNEL); if (!buffer) return ERR_PTR(-ENOMEM); -- 2.17.1