public inbox for linux-arm-kernel@lists.infradead.org
 help / color / mirror / Atom feed
* [PATCH v4] mm: cma: add a simple kernel module as the helper to test CMA
@ 2012-03-07 11:37 Barry Song
  2012-03-07 12:11 ` Michal Nazarewicz
  2012-07-03  7:20 ` Marek Szyprowski
  0 siblings, 2 replies; 8+ messages in thread
From: Barry Song @ 2012-03-07 11:37 UTC (permalink / raw)
  To: linux-arm-kernel

From: Barry Song <Baohua.Song@csr.com>

Any write request to /dev/cma_test will let the module to allocate memory from
CMA, for example:

1st time
$ echo 1024 > /dev/cma_test
will require cma_test to request 1MB(1024KB)
2nd time
$ echo 2048 > /dev/cma_test
will require cma_test to request 2MB(2048KB)

Any read request to /dev/cma_test will let the module to free the 1st valid
memory from CMA, for example:

1st time
$ cat /dev/cma_test
will require cma_test to free the 1MB allocated in the first write request
2nd time
$ cat /dev/cma_test
will require cma_test to free the 2MB allocated in the second write request

Signed-off-by: Barry Song <Baohua.Song@csr.com>
---
 tools/cma/Makefile   |   13 +++++
 tools/cma/cma_test.c |  140 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 153 insertions(+), 0 deletions(-)
 create mode 100644 tools/cma/Makefile
 create mode 100644 tools/cma/cma_test.c

diff --git a/tools/cma/Makefile b/tools/cma/Makefile
new file mode 100644
index 0000000..d15c2c0
--- /dev/null
+++ b/tools/cma/Makefile
@@ -0,0 +1,13 @@
+# Kernel modules
+#
+# To compile for ARM:
+# make ARCH=arm CC=arm-none-linux-gnueabi-gcc
+#
+obj-m	+= cma_test.o
+
+build: kernel_modules
+
+kernel_modules:
+	${MAKE} -C $(CURDIR)/../.. M=$(CURDIR)
+clean:
+	${MAKE} -C $(CURDIR)/../.. M=$(CURDIR) clean
diff --git a/tools/cma/cma_test.c b/tools/cma/cma_test.c
new file mode 100644
index 0000000..7eb96db
--- /dev/null
+++ b/tools/cma/cma_test.c
@@ -0,0 +1,140 @@
+/*
+ * kernel module helper for testing CMA
+ *
+ * Copyright (c) 2012 Cambridge Silicon Radio Limited, a CSR plc group company.
+ *
+ * Licensed under GPLv2 or later.
+ */
+
+#include <linux/device.h>
+#include <linux/dma-mapping.h>
+#include <linux/fs.h>
+#include <linux/miscdevice.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/spinlock.h>
+
+struct cma_allocation {
+	struct list_head list;
+	size_t size;
+	dma_addr_t dma;
+	void *virt;
+};
+
+static struct device *cma_dev;
+static LIST_HEAD(cma_allocations);
+static DEFINE_SPINLOCK(cma_lock);
+
+/*
+ * any read request will free the 1st allocated coherent memory, eg.
+ * cat /dev/cma_test
+ */
+static ssize_t
+cma_test_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
+{
+	struct cma_allocation *alloc = NULL;
+
+	spin_lock(&cma_lock);
+	if (!list_empty(&cma_allocations)) {
+		alloc = list_first_entry(&cma_allocations,
+			struct cma_allocation, list);
+		list_del(&alloc->list);
+	}
+	spin_unlock(&cma_lock);
+
+	if (!alloc)
+		return -EIDRM;
+
+	dma_free_coherent(cma_dev, alloc->size, alloc->virt,
+		alloc->dma);
+
+	_dev_info(cma_dev, "free: CM virt: %p dma: %p size:%zuK\n",
+		alloc->virt, (void *)alloc->dma, alloc->size / SZ_1K);
+	kfree(alloc);
+
+	return 0;
+}
+
+/*
+ * any write request will alloc a new coherent memory, eg.
+ * echo 1024 > /dev/cma_test
+ * will request 1024KiB by CMA
+ */
+static ssize_t
+cma_test_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
+{
+	struct cma_allocation *alloc;
+	unsigned long size;
+	int ret;
+
+	ret = kstrtoul_from_user(buf, count, 0, &size);
+	if (ret)
+		return ret;
+
+	if (!size)
+		return -EINVAL;
+
+	if (size > ~(size_t)0 / SZ_1K)
+		return -EOVERFLOW;
+
+	alloc = kmalloc(sizeof *alloc, GFP_KERNEL);
+	if (!alloc)
+		return -ENOMEM;
+
+	alloc->size = size * SZ_1K;
+	alloc->virt = dma_alloc_coherent(cma_dev, alloc->size,
+		&alloc->dma, GFP_KERNEL);
+
+	if (alloc->virt) {
+		_dev_info(cma_dev, "alloc: virt: %p dma: %p size: %zuK\n",
+			alloc->virt, (void *)alloc->dma, alloc->size / SZ_1K);
+
+		spin_lock(&cma_lock);
+		list_add_tail(&alloc->list, &cma_allocations);
+		spin_unlock(&cma_lock);
+
+		return count;
+	} else {
+		dev_err(cma_dev, "no mem in CMA area\n");
+		kfree(alloc);
+		return -ENOSPC;
+	}
+}
+
+static const struct file_operations cma_test_fops = {
+	.owner =    THIS_MODULE,
+	.read  =    cma_test_read,
+	.write =    cma_test_write,
+};
+
+static struct miscdevice cma_test_misc = {
+	.name = "cma_test",
+	.fops = &cma_test_fops,
+};
+
+static int __init cma_test_init(void)
+{
+	int ret = misc_register(&cma_test_misc);
+
+	if (unlikely(ret)) {
+		pr_err("failed to register cma test misc device!\n");
+		return ret;
+	}
+	cma_dev = cma_test_misc.this_device;
+	cma_dev->coherent_dma_mask = ~0;
+	_dev_info(cma_dev, "registered.\n");
+
+	return 0;
+}
+module_init(cma_test_init);
+
+static void __exit cma_test_exit(void)
+{
+	misc_deregister(&cma_test_misc);
+}
+module_exit(cma_test_exit);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Barry Song <Baohua.Song@csr.com>");
+MODULE_DESCRIPTION("kernel module to help the test of CMA");
+MODULE_ALIAS("cma_test");
-- 
1.7.1



Member of the CSR plc group of companies. CSR plc registered in England and Wales, registered number 4187346, registered office Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, United Kingdom
More information can be found at www.csr.com. Follow CSR on Twitter at http://twitter.com/CSR_PLC and read our blog at www.csr.com/blog

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v4] mm: cma: add a simple kernel module as the helper to test CMA
  2012-03-07 11:37 [PATCH v4] mm: cma: add a simple kernel module as the helper to test CMA Barry Song
@ 2012-03-07 12:11 ` Michal Nazarewicz
  2012-07-03  7:20 ` Marek Szyprowski
  1 sibling, 0 replies; 8+ messages in thread
From: Michal Nazarewicz @ 2012-03-07 12:11 UTC (permalink / raw)
  To: linux-arm-kernel

On Wed, 07 Mar 2012 12:37:39 +0100, Barry Song <Barry.Song@csr.com> wrote:
> Any write request to /dev/cma_test will let the module to allocate memory from
> CMA, for example:
>
> 1st time
> $ echo 1024 > /dev/cma_test
> will require cma_test to request 1MB(1024KB)
> 2nd time
> $ echo 2048 > /dev/cma_test
> will require cma_test to request 2MB(2048KB)
>
> Any read request to /dev/cma_test will let the module to free the 1st valid
> memory from CMA, for example:
>
> 1st time
> $ cat /dev/cma_test
> will require cma_test to free the 1MB allocated in the first write request
> 2nd time
> $ cat /dev/cma_test
> will require cma_test to free the 2MB allocated in the second write request
>
> Signed-off-by: Barry Song <Baohua.Song@csr.com>

Reviewed-by: Michal Nazarewicz <mina86@mina86.com>

-- 
Best regards,                                         _     _
.o. | Liege of Serenely Enlightened Majesty of      o' \,=./ `o
..o | Computer Science,  Micha? ?mina86? Nazarewicz    (o o)
ooo +----<email/xmpp: mpn@google.com>--------------ooO--(_)--Ooo--

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v4] mm: cma: add a simple kernel module as the helper to test CMA
  2012-03-07 11:37 [PATCH v4] mm: cma: add a simple kernel module as the helper to test CMA Barry Song
  2012-03-07 12:11 ` Michal Nazarewicz
@ 2012-07-03  7:20 ` Marek Szyprowski
  2012-07-03  7:39   ` Barry Song
  1 sibling, 1 reply; 8+ messages in thread
From: Marek Szyprowski @ 2012-07-03  7:20 UTC (permalink / raw)
  To: linux-arm-kernel

Hello,

On Wednesday, March 07, 2012 12:38 PM Barry Song wrote:

> From: Barry Song <Baohua.Song@csr.com>
> 
> Any write request to /dev/cma_test will let the module to allocate memory from
> CMA, for example:
> 
> 1st time
> $ echo 1024 > /dev/cma_test
> will require cma_test to request 1MB(1024KB)
> 2nd time
> $ echo 2048 > /dev/cma_test
> will require cma_test to request 2MB(2048KB)
> 
> Any read request to /dev/cma_test will let the module to free the 1st valid
> memory from CMA, for example:
> 
> 1st time
> $ cat /dev/cma_test
> will require cma_test to free the 1MB allocated in the first write request
> 2nd time
> $ cat /dev/cma_test
> will require cma_test to free the 2MB allocated in the second write request
> 
> Signed-off-by: Barry Song <Baohua.Song@csr.com>
> ---
>  tools/cma/Makefile   |   13 +++++
>  tools/cma/cma_test.c |  140 ++++++++++++++++++++++++++++++++++++++++++++++++++

I'm sorry for such a long delay in processing this patch, but I wanted first to be sure
that the core CMA code gets into mainline, then I got busy with other dma-mapping 
related stuff. 

IMHO the right place for such code is in drivers/misc instead of tools/cma. 

This patch contains a kernel module source code so it should be put with other kernel
modules. tools/ directory is aimed to contain user space code useful for developing and
testing Linux kernel.

Make sure that you also add Greg Kroah-Hartman <gregkh@linuxfoundation.org> on CC:,
he maintains misc drivers.

> (snipped)

Best regards
-- 
Marek Szyprowski
Samsung Poland R&D Center

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v4] mm: cma: add a simple kernel module as the helper to test CMA
  2012-07-03  7:20 ` Marek Szyprowski
@ 2012-07-03  7:39   ` Barry Song
  2012-07-03 12:54     ` Michal Nazarewicz
  0 siblings, 1 reply; 8+ messages in thread
From: Barry Song @ 2012-07-03  7:39 UTC (permalink / raw)
  To: linux-arm-kernel

Hi Marek,

2012/7/3 Marek Szyprowski <m.szyprowski@samsung.com>:
> Hello,
>
> On Wednesday, March 07, 2012 12:38 PM Barry Song wrote:
>
>> From: Barry Song <Baohua.Song@csr.com>
>>
>> Any write request to /dev/cma_test will let the module to allocate memory from
>> CMA, for example:
>>
>> 1st time
>> $ echo 1024 > /dev/cma_test
>> will require cma_test to request 1MB(1024KB)
>> 2nd time
>> $ echo 2048 > /dev/cma_test
>> will require cma_test to request 2MB(2048KB)
>>
>> Any read request to /dev/cma_test will let the module to free the 1st valid
>> memory from CMA, for example:
>>
>> 1st time
>> $ cat /dev/cma_test
>> will require cma_test to free the 1MB allocated in the first write request
>> 2nd time
>> $ cat /dev/cma_test
>> will require cma_test to free the 2MB allocated in the second write request
>>
>> Signed-off-by: Barry Song <Baohua.Song@csr.com>
>> ---
>>  tools/cma/Makefile   |   13 +++++
>>  tools/cma/cma_test.c |  140 ++++++++++++++++++++++++++++++++++++++++++++++++++
>
> I'm sorry for such a long delay in processing this patch, but I wanted first to be sure
> that the core CMA code gets into mainline, then I got busy with other dma-mapping
> related stuff.

now CMA stuff has been in mainline, i think it makes more senses if
you involve this while you let people merge CMA because it is a CMA
test component.

>
> IMHO the right place for such code is in drivers/misc instead of tools/cma.
>
> This patch contains a kernel module source code so it should be put with other kernel
> modules. tools/ directory is aimed to contain user space code useful for developing and
> testing Linux kernel.

this one is aiming to test CMA, it should be in tools/ as Michal
Nazarewicz and i have talked about that.
it is not a real driver at all.

>
> Make sure that you also add Greg Kroah-Hartman <gregkh@linuxfoundation.org> on CC:,
> he maintains misc drivers.
>
>> (snipped)
>
> Best regards
> --
> Marek Szyprowski
> Samsung Poland R&D Center

-barry

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v4] mm: cma: add a simple kernel module as the helper to test CMA
  2012-07-03  7:39   ` Barry Song
@ 2012-07-03 12:54     ` Michal Nazarewicz
  2012-07-04  6:52       ` Marek Szyprowski
  0 siblings, 1 reply; 8+ messages in thread
From: Michal Nazarewicz @ 2012-07-03 12:54 UTC (permalink / raw)
  To: linux-arm-kernel

> 2012/7/3 Marek Szyprowski <m.szyprowski@samsung.com>:
>> IMHO the right place for such code is in drivers/misc instead of tools/cma.
>>
>> This patch contains a kernel module source code so it should be put with other kernel
>> modules. tools/ directory is aimed to contain user space code useful for developing and
>> testing Linux kernel.

On Tue, 03 Jul 2012 09:39:34 +0200, Barry Song <21cnbao@gmail.com> wrote:
> this one is aiming to test CMA, it should be in tools/ as Michal
> Nazarewicz and i have talked about that.
> it is not a real driver at all.

Yeah, I feel like it's better placed in tools/ not to pollute other directories with
drivers that are of no use to regular users.  I don't have strong feelings about it
though, so I'm fine either way.

-- 
Best regards,                                         _     _
.o. | Liege of Serenely Enlightened Majesty of      o' \,=./ `o
..o | Computer Science,  Micha? ?mina86? Nazarewicz    (o o)
ooo +----<email/xmpp: mpn@google.com>--------------ooO--(_)--Ooo--

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v4] mm: cma: add a simple kernel module as the helper to test CMA
  2012-07-03 12:54     ` Michal Nazarewicz
@ 2012-07-04  6:52       ` Marek Szyprowski
  2012-07-04  6:56         ` Barry Song
  0 siblings, 1 reply; 8+ messages in thread
From: Marek Szyprowski @ 2012-07-04  6:52 UTC (permalink / raw)
  To: linux-arm-kernel

Hello,

On Tuesday, July 03, 2012 2:54 PM Michal Nazarewicz wrote:

> > 2012/7/3 Marek Szyprowski <m.szyprowski@samsung.com>:
> >> IMHO the right place for such code is in drivers/misc instead of tools/cma.
> >>
> >> This patch contains a kernel module source code so it should be put with other kernel
> >> modules. tools/ directory is aimed to contain user space code useful for developing and
> >> testing Linux kernel.
> 
> On Tue, 03 Jul 2012 09:39:34 +0200, Barry Song <21cnbao@gmail.com> wrote:
> > this one is aiming to test CMA, it should be in tools/ as Michal
> > Nazarewicz and i have talked about that.
> > it is not a real driver at all.
> 
> Yeah, I feel like it's better placed in tools/ not to pollute other directories with
> drivers that are of no use to regular users.  I don't have strong feelings about it
> though, so I'm fine either way.

Other test devices/drivers are in drivers directory (drivers/usb/misc/usbtest.c for
example), I don't see any good reason for polluting tools/ directory with kernel code. 

IMHO the right person to decide if it makes sense to have such drivers in drivers/mics/
or tools/cma/ is Greg KH. Barry, please resend it and ask Greg for the decision.

Best regards
-- 
Marek Szyprowski
Samsung Poland R&D Center

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v4] mm: cma: add a simple kernel module as the helper to test CMA
  2012-07-04  6:52       ` Marek Szyprowski
@ 2012-07-04  6:56         ` Barry Song
  2012-07-04  7:00           ` Barry Song
  0 siblings, 1 reply; 8+ messages in thread
From: Barry Song @ 2012-07-04  6:56 UTC (permalink / raw)
  To: linux-arm-kernel

Hi Marek,

2012/7/4 Marek Szyprowski <m.szyprowski@samsung.com>:
> Hello,
>
> On Tuesday, July 03, 2012 2:54 PM Michal Nazarewicz wrote:
>
>> > 2012/7/3 Marek Szyprowski <m.szyprowski@samsung.com>:
>> >> IMHO the right place for such code is in drivers/misc instead of tools/cma.
>> >>
>> >> This patch contains a kernel module source code so it should be put with other kernel
>> >> modules. tools/ directory is aimed to contain user space code useful for developing and
>> >> testing Linux kernel.
>>
>> On Tue, 03 Jul 2012 09:39:34 +0200, Barry Song <21cnbao@gmail.com> wrote:
>> > this one is aiming to test CMA, it should be in tools/ as Michal
>> > Nazarewicz and i have talked about that.
>> > it is not a real driver at all.
>>
>> Yeah, I feel like it's better placed in tools/ not to pollute other directories with
>> drivers that are of no use to regular users.  I don't have strong feelings about it
>> though, so I'm fine either way.
>
> Other test devices/drivers are in drivers directory (drivers/usb/misc/usbtest.c for
> example), I don't see any good reason for polluting tools/ directory with kernel code.
>
> IMHO the right person to decide if it makes sense to have such drivers in drivers/mics/
> or tools/cma/ is Greg KH. Barry, please resend it and ask Greg for the decision.

sure. thanks.

>
> Best regards
> --
> Marek Szyprowski
> Samsung Poland R&D Center
>
>

-barry

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v4] mm: cma: add a simple kernel module as the helper to test CMA
  2012-07-04  6:56         ` Barry Song
@ 2012-07-04  7:00           ` Barry Song
  0 siblings, 0 replies; 8+ messages in thread
From: Barry Song @ 2012-07-04  7:00 UTC (permalink / raw)
  To: linux-arm-kernel

2012/7/4 Barry Song <21cnbao@gmail.com>:
> Hi Marek,
>
> 2012/7/4 Marek Szyprowski <m.szyprowski@samsung.com>:
>> Hello,
>>
>> On Tuesday, July 03, 2012 2:54 PM Michal Nazarewicz wrote:
>>
>>> > 2012/7/3 Marek Szyprowski <m.szyprowski@samsung.com>:
>>> >> IMHO the right place for such code is in drivers/misc instead of tools/cma.
>>> >>
>>> >> This patch contains a kernel module source code so it should be put with other kernel
>>> >> modules. tools/ directory is aimed to contain user space code useful for developing and
>>> >> testing Linux kernel.
>>>
>>> On Tue, 03 Jul 2012 09:39:34 +0200, Barry Song <21cnbao@gmail.com> wrote:
>>> > this one is aiming to test CMA, it should be in tools/ as Michal
>>> > Nazarewicz and i have talked about that.
>>> > it is not a real driver at all.
>>>
>>> Yeah, I feel like it's better placed in tools/ not to pollute other directories with
>>> drivers that are of no use to regular users.  I don't have strong feelings about it
>>> though, so I'm fine either way.
>>
>> Other test devices/drivers are in drivers directory (drivers/usb/misc/usbtest.c for
>> example), I don't see any good reason for polluting tools/ directory with kernel code.
>>
>> IMHO the right person to decide if it makes sense to have such drivers in drivers/mics/
>> or tools/cma/ is Greg KH. Barry, please resend it and ask Greg for the decision.
>
> sure. thanks.

and BTW, if the only issue is the dir it should be placed to let Greg
decide, would i also have your reviewed-by before sending to Greg KH?

>
>>
>> Best regards
>> --
>> Marek Szyprowski
>> Samsung Poland R&D Center
>>

-barry

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2012-07-04  7:00 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-03-07 11:37 [PATCH v4] mm: cma: add a simple kernel module as the helper to test CMA Barry Song
2012-03-07 12:11 ` Michal Nazarewicz
2012-07-03  7:20 ` Marek Szyprowski
2012-07-03  7:39   ` Barry Song
2012-07-03 12:54     ` Michal Nazarewicz
2012-07-04  6:52       ` Marek Szyprowski
2012-07-04  6:56         ` Barry Song
2012-07-04  7:00           ` Barry Song

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox