Linux-mm Archive on lore.kernel.org
 help / color / Atom feed
From: Pankaj Suryawanshi <pankajssuryawanshi@gmail.com>
To: linux-kernel@vger.kernel.org, linux-mm@kvack.org,
	 iommu@lists.linux-foundation.org,
	Vlastimil Babka <vbabka@suse.cz>,
	 Robin Murphy <robin.murphy@arm.com>,
	Michal Hocko <mhocko@kernel.org>,
	 pankaj.suryawanshi@einfochips.com, minchan@kernel.org,
	minchan.kim@gmail.com
Subject: cma_remap when using dma_alloc_attr :- DMA_ATTR_NO_KERNEL_MAPPING
Date: Sat, 13 Jul 2019 00:00:56 +0530
Message-ID: <CACDBo56EoKca9FJCnbztWZAARdUQs+B=dmCs+UxW27yHNu5pzQ@mail.gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 2549 bytes --]

Hello,

When we allocate cma memory using dma_alloc_attr using
DMA_ATTR_NO_KERNEL_MAPPING attribute. It will return physical address
without virtual mapping and thats the use case of this attribute. but lets
say some vpu/gpu drivers required virtual mapping of some part of the
allocation. then we dont have anything to remap that allocated memory to
virtual memory. and in 32-bit system it difficult for devices like android
to work all the time with virtual mapping, it degrade the performance.

For Example :

Lets say 4k video allocation required 300MB cma memory but not required
virtual mapping for all the 300MB, its require only 20MB virtually mapped
at some specific use case/point of video, and unmap virtual mapping after
uses, at that time this functions will be useful, it works like ioremap()
for cma_alloc() using dma apis.

/*
         * function call(s) to create virtual map of given physical memory
         * range [base, base+size) of CMA memory.
*/
void *cma_remap(__u32 base, __u32 size)
{
        struct page *page = phys_to_page(base);
        void *virt;

        pr_debug("cma: request to map 0x%08x for size 0x%08x\n",
                        base, size);

        size = PAGE_ALIGN(size);

        pgprot_t prot = get_dma_pgprot(DMA_ATTR, PAGE_KERNEL);

        if (PageHighMem(page)){
                virt = dma_alloc_remap(page, size, GFP_KERNEL, prot,
__builtin_return_address(0));
        }
        else
        {
                dma_remap(page, size, prot);
                virt = page_address(page);
        }

        if (!virt)
                pr_err("\x1b[31m" " cma: failed to map 0x%08x" "\x1b[0m\n",
                                base);
        else
                pr_debug("cma: 0x%08x is virtually mapped to 0x%08x\n",
                                base, (__u32) virt);

        return virt;
}

/*
         * function call(s) to remove virtual map of given virtual memory
         * range [virt, virt+size) of CMA memory.
*/

void cma_unmap(void *virt, __u32 size)
{
        size = PAGE_ALIGN(size);
        unsigned long pfn = virt_to_pfn(virt);
        struct page *page = pfn_to_page(pfn);

                if (PageHighMem(page))
                        dma_free_remap(virt, size);
                else
                        dma_remap(page, size, PAGE_KERNEL);

        pr_debug(" cma: virtual address 0x%08x is unmapped\n",
                        (__u32) virt);
}

This functions should be added in arch/arm/mm/dma-mapping.c file.

Please let me know if i am missing anything.

Regards,
Pankaj

[-- Attachment #2: Type: text/html, Size: 3117 bytes --]

<div dir="ltr">Hello,<br><br>When we allocate cma memory using dma_alloc_attr using DMA_ATTR_NO_KERNEL_MAPPING attribute. It will return physical address without virtual mapping and thats the use case of this attribute. but lets say some vpu/gpu drivers required virtual mapping of some part of the allocation. then we dont have anything to remap that allocated memory to virtual memory. and in 32-bit system it difficult for devices like android to work all the time with virtual mapping, it degrade the performance.<br><br>For Example :<br><br>Lets say 4k video allocation required 300MB cma memory but not required virtual mapping for all the 300MB, its require only 20MB virtually mapped at some specific use case/point of video, and unmap virtual mapping after uses, at that time this functions will be useful, it works like ioremap() for cma_alloc() using dma apis.<br><br>/*<br>         * function call(s) to create virtual map of given physical memory<br>         * range [base, base+size) of CMA memory.<br>*/<br>void *cma_remap(__u32 base, __u32 size)<br>{<br>        struct page *page = phys_to_page(base);<br>        void *virt;<br><br>        pr_debug(&quot;cma: request to map 0x%08x for size 0x%08x\n&quot;,<br>                        base, size);<br><br>        size = PAGE_ALIGN(size);<br><br>        pgprot_t prot = get_dma_pgprot(DMA_ATTR, PAGE_KERNEL);<br><br>        if (PageHighMem(page)){<br>                virt = dma_alloc_remap(page, size, GFP_KERNEL, prot, __builtin_return_address(0));<br>        }<br>        else<br>        {<br>                dma_remap(page, size, prot);<br>                virt = page_address(page);<br>        }<br><br>        if (!virt)<br>                pr_err(&quot;\x1b[31m&quot; &quot; cma: failed to map 0x%08x&quot; &quot;\x1b[0m\n&quot;,<br>                                base);<br>        else<br>                pr_debug(&quot;cma: 0x%08x is virtually mapped to 0x%08x\n&quot;,<br>                                base, (__u32) virt);<br><br>        return virt;<br>}<br><br>/*<br>         * function call(s) to remove virtual map of given virtual memory<br>         * range [virt, virt+size) of CMA memory.<br>*/<br><br>void cma_unmap(void *virt, __u32 size)<br>{<br>        size = PAGE_ALIGN(size);<br>        unsigned long pfn = virt_to_pfn(virt);<br>        struct page *page = pfn_to_page(pfn);<br><br>                if (PageHighMem(page))<br>                        dma_free_remap(virt, size);<br>                else<br>                        dma_remap(page, size, PAGE_KERNEL);<br><br>        pr_debug(&quot; cma: virtual address 0x%08x is unmapped\n&quot;,<br>                        (__u32) virt);<br>}<br><br><div>This functions should be added in arch/arm/mm/dma-mapping.c file.</div><div><br></div><div>Please let me know if i am missing anything.</div><div><br></div><div>Regards,</div><div>Pankaj<br></div></div>

             reply index

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-07-12 18:30 Pankaj Suryawanshi [this message]
2019-07-16 12:02 ` Robin Murphy
2019-07-16 12:10   ` Christoph Hellwig

Reply instructions:

You may reply publically to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CACDBo56EoKca9FJCnbztWZAARdUQs+B=dmCs+UxW27yHNu5pzQ@mail.gmail.com' \
    --to=pankajssuryawanshi@gmail.com \
    --cc=iommu@lists.linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mhocko@kernel.org \
    --cc=minchan.kim@gmail.com \
    --cc=minchan@kernel.org \
    --cc=pankaj.suryawanshi@einfochips.com \
    --cc=robin.murphy@arm.com \
    --cc=vbabka@suse.cz \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Linux-mm Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-mm/0 linux-mm/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-mm linux-mm/ https://lore.kernel.org/linux-mm \
		linux-mm@kvack.org linux-mm@archiver.kernel.org
	public-inbox-index linux-mm


Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kvack.linux-mm


AGPL code for this site: git clone https://public-inbox.org/ public-inbox