Received: by 10.223.164.202 with SMTP id h10csp1000749wrb; Wed, 15 Nov 2017 11:24:45 -0800 (PST) X-Google-Smtp-Source: AGs4zMaltKb+3hOoDoDhIR+r7vX+rzbxiJs/N8PpA8Y+klFmdSM56E1wDZ9Erv3pnVUatHVQihg2 X-Received: by 10.98.102.219 with SMTP id s88mr13807779pfj.191.1510773885532; Wed, 15 Nov 2017 11:24:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1510773885; cv=none; d=google.com; s=arc-20160816; b=yJHi6NUosqM30rnSKwqhzcqYEdIl9zGkIAviQOTRlbGfumsgy/Dh2Kv9+v1VYLl9gV YN/TbAkj9fP/3XhV1QzdsKh6tL7qFhc9RWLcKDTG/cvcCIqBh5wmIAt9G9LjyUeIyKVg n4JvKEqAJCxwuYY0J35jjAEkyGFt9avZIXOgem1UQis4Zm/Owq7RvWHkgM0Q1u2DmVQm VlYekhuU0/GjujYwk6tkK2BnbRvEzlACERPYXI9EPaxoCxN8A0+Cyd6q/puuEbuAkryf vVOhWCqMeU8xkctgS+t9UOasaJQ3dVNmnQ+Exa2yF6VBzO1IX+0bLEUfJFFtUeVqNrxU j7zA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:dlp-filter:cms-type :in-reply-to:user-agent:date:message-id:from:cc:to:subject :content-transfer-encoding:mime-version:dkim-signature:dkim-filter :arc-authentication-results; bh=LcwZ3LncAPw/8TeSnAPmaAj51AKcqvDjeZpTrAUpPU0=; b=rWoaHLRseVf8wbL6LZKl+u3Gl56nOvHin3TFS6OC6V/sCK/i13Me6IUPC9PWz8RA8L zK1B4QgdbxXaZMAbqSMkD485V+q0uC/lhkbWjmF7IwBoEuth4sLnzQQt/iRCkKRmtSG3 3Cpsf8AvFnVpa+nkefkJ43w8VBVa3ae5XgBHvdjEtIeteT4MAeHaMAxEYRsYjKG1PHFr jWw2vMWorSnxGvdpUnXYOgKnxsaZmrrj4TkFKy8CcVofhW53AREBoBNK99FpC2UKYuyo wh8xXZCmpHMlcNKRVr8Gv4ecnr8e9rfMlar29+JRGWaSpsw48/Uabc3khG1s4L+sXJjg b1AQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@samsung.com header.s=mail20170921 header.b=fr5aD7tX; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=samsung.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 134si1165490pgg.733.2017.11.15.11.24.31; Wed, 15 Nov 2017 11:24:45 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@samsung.com header.s=mail20170921 header.b=fr5aD7tX; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=samsung.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757108AbdKOREi (ORCPT + 90 others); Wed, 15 Nov 2017 12:04:38 -0500 Received: from mailout4.samsung.com ([203.254.224.34]:63451 "EHLO mailout4.samsung.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753905AbdKOREc (ORCPT ); Wed, 15 Nov 2017 12:04:32 -0500 Received: from epcas1p2.samsung.com (unknown [182.195.41.46]) by mailout4.samsung.com (KnoxPortal) with ESMTP id 20171115170430epoutp04578af825c4988a80fd9ef3c33f57a497~3UTDuoYU11519215192epoutp04M; Wed, 15 Nov 2017 17:04:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 mailout4.samsung.com 20171115170430epoutp04578af825c4988a80fd9ef3c33f57a497~3UTDuoYU11519215192epoutp04M DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=samsung.com; s=mail20170921; t=1510765470; bh=LcwZ3LncAPw/8TeSnAPmaAj51AKcqvDjeZpTrAUpPU0=; h=Subject:To:Cc:From:Date:In-reply-to:References:From; b=fr5aD7tXS0reb+eh/Y92sFaUktQkIEmFXV5171hmfUED0Ht7/xdgYeRWZIzaupryf 3nDSas8Pcc/J1awG0uYwYZGs9QeQ6DoyXAVnwk9i/3qI6k7kP2XvDeulFfomgcce4j VebWU5bEUr4HtWJXZOmgBCXbk4p7LMo0Y+Z5GBx4= Received: from epsmges1p4.samsung.com (unknown [182.195.40.94]) by epcas1p4.samsung.com (KnoxPortal) with ESMTP id 20171115170430epcas1p44be20409215e8998bda8fa2e37c33e08~3UTDgBDM31692516925epcas1p43; Wed, 15 Nov 2017 17:04:30 +0000 (GMT) Received: from epcas1p4.samsung.com ( [182.195.41.48]) by epsmges1p4.samsung.com (Symantec Messaging Gateway) with SMTP id 01.E9.04510.E937C0A5; Thu, 16 Nov 2017 02:04:30 +0900 (KST) Received: from epsmgms2p1new.samsung.com (unknown [182.195.42.142]) by epcas1p3.samsung.com (KnoxPortal) with ESMTP id 20171115170430epcas1p324d530f30d72b365e79b7f00ca23dfbe~3UTDPZd5a1942419424epcas1p3R; Wed, 15 Nov 2017 17:04:30 +0000 (GMT) X-AuditID: b6c32a38-1d5ff7000000119e-a6-5a0c739e32e0 Received: from epmmp2 ( [203.254.227.17]) by epsmgms2p1new.samsung.com (Symantec Messaging Gateway) with SMTP id 27.A1.04095.D937C0A5; Thu, 16 Nov 2017 02:04:29 +0900 (KST) MIME-version: 1.0 Content-transfer-encoding: 8BIT Content-type: text/plain; charset="utf-8" Received: from [10.253.100.37] by mmp2.samsung.com (Oracle Communications Messaging Server 7.0.5.31.0 64bit (built May 5 2014)) with ESMTPA id <0OZG0091PY3BC5B0@mmp2.samsung.com>; Thu, 16 Nov 2017 02:04:29 +0900 (KST) Subject: Re: [RFC PATCH] drivers: base: dma-coherent: find free region without alignment To: Marek Szyprowski , hch@lst.de, robin.murphy@arm.com, gregkh@linuxfoundation.org, iommu@lists.linux-foundation.org Cc: akpm@linux-foundation.org, mhocko@suse.com, vbabka@suse.cz, linux-mm@kvack.org, linux-kernel@vger.kernel.org, jaewon31.kim@gmail.com From: Jaewon Kim Message-id: <5A0C7391.3090801@samsung.com> Date: Thu, 16 Nov 2017 02:04:17 +0900 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.7.2 In-reply-to: <94f35456-b661-908a-bdc0-ddd74cbaf9ec@samsung.com> X-Brightmail-Tracker: H4sIAAAAAAAAA02SbUhTYRTHeXa3e+fL6jZbPViZXcjQ2tpdm15LU8niUn6QIhCj1kUf5mov truF60v2ZrZMaoboTDH6UNliNVfaq2S+oWZlJiVofYioCNMsMgVr2y3q2/+c8zvnec6fI8Xk DXis1GixI5uFM1F4pPj240S1soGPzle3DMcwF3xenDl2yYczV691ipjGtg3M6eZawLy4ewFn xry/JMz1jlGCeVOZyTyaeCdh6o5Ugswo1tvgBewdzyjB+r+6CbanZlbMttV7Cbbq1WXA3ntd irOVgSbA+gIvxWx/YwfBTvnjcqPyUVoR4gqRLR5ZCqyFRoshndq2Q79Jr0tW00o6lUmh4i2c GaVT2Tm5yi1GU/DbVPxBzuQIpnI5nqfWbkyzWR12FF9k5e3p1C6a1qhodYpKo9GotCm712t0 QWQvKuod6CGKTyhLqqrdeCnoWeECEVJIauHbphrgApFSOdkK4MeJdiJUkJM/ACyfVrqANAz5 +woF5gaAZZc7QYiRkQvgdNWYOMRg5HLYMbg/lMbIRPjhm1ss8GMADg49EIUKMWQ+fHSpBQ8V FpJVANaWNhGhACNdAJ66Uh+eipOr4ZdGt0R4IQk+bx7CQ1pMroTfao+GtYLMg631k2E+gsyA 1ef7woMgeZKAQ8+6xMJu2fBoWfcfHQM/dQcIYZ0lcLAzXeBPA1hx0ycSm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CgmkJ5akZqemFqQWwWSZODilGhh1/Oze f58cOfN5cvs5dT0vNpdbbQ4N1d9c67dmbRCRSa5nb689IMG7z+pKUuhN85qdYb/m8HlfN50v te7BOXWOw3uTXd5Janz2zpynYPOI+5FleMLbWtvoOfO8ah4XGNneaaoznTrHZWvPSrPy/WVM aQ7h/frPuJcI3Hv5/YTxy+bZUp0e65RYijMSDbWYi4oTAUM87j2UAgAA X-CMS-MailID: 20171115170430epcas1p324d530f30d72b365e79b7f00ca23dfbe X-Msg-Generator: CA CMS-TYPE: 101P DLP-Filter: Pass X-CFilter-Loop: Reflected X-CMS-RootMailID: 20171114084234epcas2p44ac00494b49aa798f709c5bbdf92127a X-RootMTR: 20171114084234epcas2p44ac00494b49aa798f709c5bbdf92127a References: <20171114084229.13512-1-jaewon31.kim@samsung.com> <94f35456-b661-908a-bdc0-ddd74cbaf9ec@samsung.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hello Marek On 2017년 11월 14일 20:07, Marek Szyprowski wrote: > Hi Jaewon, > > On 2017-11-14 09:42, Jaewon Kim wrote: >> dma-coherent uses bitmap API which internally consider align based on the >> requested size. Depending on some usage pattern, using align, I think, may >> be good for fast search and anti-fragmentation. But with the align, an >> allocation may be failed. >> >> This is a example, total size is 30MB, only few memory at front is being >> used, and 9MB is being requsted. Then 9MB will be aligned to 16MB. The >> first try on offset 0MB will be failed because of others already using. The >> second try on offset 16MB will be failed because of ouf of bound. >> >> So if the align is not necessary on dma-coherent, this patch removes the >> align policy to allow allocation without increasing the total size. > > You are right that keeping strict alignment is waste of memory for large > allocations. However for the smaller ones, typically under 1MiB, it helps > to reduce memory fragmentation. The alignment of the allocated buffers is > de-facto guaranteed by the memory management framework in Linux kernel > and there are drivers that depends on this feature. > > Maybe it would make sense to keep alignment for buffers smaller than some > predefined value (like 1MiB), something similar to config > ARM_DMA_IOMMU_ALIGNMENT in arch/arm/Kconfig. Otherwise I would expect that > some drivers will be broken by this patch. Thank you for your comment. I looked ARM_DMA_IOMMU_ALIGNMENT in ARM, it looks similar but it is using bitmap_find_next_zero_area rather than bitmap_find_free_region. bitmap_find_next_zero_area apply aligning only onto offset but not onto size. So I think ARM_DMA_IOMMU_ALIGNMENT way is not perfect on this dma-coherent APIs which tries to align even on size. Let me say another way where each reserved_mem from device tree can decide if it wants aligning. This could be implemented like below. I need to change other dma-coherent APIs though. I will wait for your comment on this. --- a/drivers/base/dma-coherent.c +++ b/drivers/base/dma-coherent.c @@ -17,6 +17,7 @@ struct dma_coherent_mem { unsigned long *bitmap; spinlock_t spinlock; bool use_dev_dma_pfn_offset; + bool no_align; }; static struct dma_coherent_mem *dma_coherent_default_memory __ro_after_init; @@ -162,7 +163,6 @@ EXPORT_SYMBOL(dma_mark_declared_memory_occupied); static void *__dma_alloc_from_coherent(struct dma_coherent_mem *mem, ssize_t size, dma_addr_t *dma_handle) { - int order = get_order(size); unsigned long flags; int pageno; void *ret; @@ -172,9 +172,21 @@ static void *__dma_alloc_from_coherent(struct dma_coherent_mem *mem, if (unlikely(size > (mem->size << PAGE_SHIFT))) goto err; - pageno = bitmap_find_free_region(mem->bitmap, mem->size, order); - if (unlikely(pageno < 0)) - goto err; + if (mem->no_align) { + int nr_page = PAGE_ALIGN(size) >> PAGE_SHIFT; + + pageno = bitmap_find_next_zero_area(mem->bitmap, mem->size, 0, + nr_page, 0); + if (unlikely(pageno >= mem->size)) + goto err; + bitmap_set(mem->bitmap, pageno, nr_page); + } else { + int order = get_order(size); + + pageno = bitmap_find_free_region(mem->bitmap, mem->size, order); + if (unlikely(pageno < 0)) + goto err; + } /* * Memory was found in the coherent area. @@ -346,6 +358,7 @@ static struct reserved_mem *dma_reserved_default_memory __initdata; static int rmem_dma_device_init(struct reserved_mem *rmem, struct device *dev) { struct dma_coherent_mem *mem = rmem->priv; + unsigned long node = rmem->fdt_node; int ret; if (!mem) { @@ -360,6 +373,8 @@ static int rmem_dma_device_init(struct reserved_mem *rmem, struct device *dev) } mem->use_dev_dma_pfn_offset = true; rmem->priv = mem; + if (of_get_flat_dt_prop(node, "no-align", NULL)) + mem->no_align = true; dma_assign_coherent_memory(dev, mem); return 0; } > >> Signed-off-by: Jaewon Kim >> --- >> drivers/base/dma-coherent.c | 8 +++++--- >> 1 file changed, 5 insertions(+), 3 deletions(-) >> >> diff --git a/drivers/base/dma-coherent.c b/drivers/base/dma-coherent.c >> index 744f64f43454..b86a96d0cd07 100644 >> --- a/drivers/base/dma-coherent.c >> +++ b/drivers/base/dma-coherent.c >> @@ -162,7 +162,7 @@ EXPORT_SYMBOL(dma_mark_declared_memory_occupied); >> static void *__dma_alloc_from_coherent(struct dma_coherent_mem *mem, >> ssize_t size, dma_addr_t *dma_handle) >> { >> - int order = get_order(size); >> + int nr_page = PAGE_ALIGN(size) >> PAGE_SHIFT; >> unsigned long flags; >> int pageno; >> void *ret; >> @@ -172,9 +172,11 @@ static void *__dma_alloc_from_coherent(struct dma_coherent_mem *mem, >> if (unlikely(size > (mem->size << PAGE_SHIFT))) >> goto err; >> - pageno = bitmap_find_free_region(mem->bitmap, mem->size, order); >> - if (unlikely(pageno < 0)) >> + pageno = bitmap_find_next_zero_area(mem->bitmap, mem->size, 0, >> + nr_page, 0); >> + if (unlikely(pageno >= mem->size)) { >> goto err; >> + bitmap_set(mem->bitmap, pageno, nr_page); >> /* >> * Memory was found in the coherent area. > > Best regards From 1584039629110857838@xxx Tue Nov 14 11:11:50 +0000 2017 X-GM-THRID: 1584030386968339474 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread