Skip to contentSkip to conversation

MM: CMA: add a simple kernel module as the helper to test CMA

PATCH Superseded Barry Song 4 participants 16 messages Mar 7, 2012

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Barry Song

author
#0 lore raw

PATCH v3 1/1 MM: CMA: add a simple kernel module as the helper to test CMA

From: Barry Song <[email protected]>

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

---
-v2:
some minor cleanups by Michal Nazarewicz

 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
tools/cma/Makefile new file +13 -0
new file mode 100644
index 0000000..d15c2c0
@@ -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
tools/cma/cma_test.c new file +140 -0

140 lines not on this page · raw message

Signature · 6 lines
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
#1 lore raw
Quoted 15 lines · Barry Song

From: Barry Song<[email protected]>

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:

Quoted 6 lines

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

Any reason why using /dev not /proc or /sys?

#2 lore raw

2012/3/7 Cong Wang <[email protected]>:

Quoted 28 lines · Barry Song
Quoted 18 lines

From: Barry Song<[email protected]>

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:

Quoted 7 lines

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
Quoted 1 line

Any reason why using /dev not /proc or /sys?

pls note it is just a test module to verify CMA at runtime. it is not important either it is /dev, it is /proc or it is /sys.

any way to make things easier, we take that way.

-barry

#3 lore raw
Quoted 29 lines · Barry Song

2012/3/7 Cong Wang<[email protected]>:

Quoted 28 lines · Barry Song
Quoted 25 lines

From: Barry Song<[email protected]>

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
Quoted 4 lines
Quoted 1 line

Any reason why using /dev not /proc or /sys?

pls note it is just a test module to verify CMA at runtime. it is not important either it is /dev, it is /proc or it is /sys.

Why not important? You want it to be merged, don't you?

Quoted 3 lines

any way to make things easier, we take that way.

Why /dev interface is easier than /proc and /sys? Confused, actually I think /proc or /sys should be easier...

#4 lore raw

2012/3/7 Cong Wang <[email protected]>:

Quoted 47 lines · Barry Song
Quoted 35 lines

2012/3/7 Cong Wang<[email protected]>:

Quoted 33 lines · Barry Song
Quoted 28 lines

From: Barry Song<[email protected]>

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
Quoted 5 lines
Quoted 1 line

Any reason why using /dev not /proc or /sys?

pls note it is just a test module to verify CMA at runtime. it is not important either it is /dev, it is /proc or it is /sys.

Why not important? You want it to be merged, don't you?

Quoted 1 line
Quoted 5 lines
Quoted 2 lines

any way to make things easier, we take that way.

Why /dev interface is easier than /proc and /sys? Confused, actually I think /proc or /sys should be easier...

if you think so, pls do that and send v4 with your s-o-b.

-barry

#5 lore raw
Quoted 3 lines · Barry Song

if you think so, pls do that and send v4 with your s-o-b.

This is not a good attitude, nor helps your patch to be merged...

Be collaborative.

#6 lore raw

2012/3/7 Cong Wang <[email protected]>:

Quoted 9 lines · Barry Song
Quoted 4 lines

if you think so, pls do that and send v4 with your s-o-b.

This is not a good attitude, nor helps your patch to be merged...

Be collaborative.

some people like focusing on trivial things. i said i really didn't care about the details whether it is /dev, /proc, /sys. if you like a /proc, pls do it by yourself and i will agree. but i will not waste my time on that.

-barry

#7 lore raw
Quoted 7 lines · Barry Song

2012/3/7 Cong Wang<[email protected]>:

Quoted 6 lines · Barry Song
Quoted 4 lines

if you think so, pls do that and send v4 with your s-o-b.

Quoted 8 lines
Quoted 3 lines

This is not a good attitude, nor helps your patch to be merged...

Be collaborative.

some people like focusing on trivial things. i said i really didn't care about the details whether it is /dev, /proc, /sys. if you like a /proc, pls do it by yourself and i will agree. but i will not waste my time on that.

This is definitely not trivial... What's more, kernel developers *do* care about trivial things (trivial at kernel.org).

We are trying to make your patch perfect, you just refused, sigh...

#8 lore raw

2012/3/7 Cong Wang <[email protected]>:

Quoted 16 lines · Barry Song
Quoted 15 lines

2012/3/7 Cong Wang<[email protected]>:

Quoted 11 lines · Barry Song
Quoted 5 lines

if you think so, pls do that and send v4 with your s-o-b.

This is not a good attitude, nor helps your patch to be merged...

Be collaborative.

Quoted 8 lines
Quoted 4 lines

some people like focusing on trivial things. i said i really didn't care about the details whether it is /dev, /proc, /sys. if you like a /proc, pls do it by yourself and i will agree. but i will not waste my time on that.

This is definitely not trivial... What's more, kernel developers *do* care about trivial things (trivial at kernel.org).

that is definitely trivial as we are talking about things in a test module. and we are not talking about any bug in the test module as well.

Quoted 2 lines

We are trying to make your patch perfect, you just refused, sigh...

pls note i just care about people have an easy way to test the newest contiguous memory allocator. pls be focusing on valuable things.

i said if you like /proc, pls do and i agree.

-barry

#9 lore raw
Quoted 3 lines · Barry Song

that is definitely trivial as we are talking about things in a test module. and we are not talking about any bug in the test module as well.

I am quite sure *any* patches have bugs will not be merged, IOW, you seem don't want your patch merged. :) Fine. That is all.

#10 lore raw

2012/3/7 Cong Wang <[email protected]>:

Quoted 9 lines · Barry Song
Quoted 4 lines

that is definitely trivial as we are talking about things in a test module. and we are not talking about any bug in the test module as well.

I am quite sure *any* patches have bugs will not be merged, IOW, you seem don't want your patch merged. :) Fine. That is all.

Cong, i was not againest what you said as a techinical issue. definitely, it can be a /proc, it is also able to be /sys if you like. /proc is probably better than /dev from the original design thinking in kernel.

here i focused on people have this module to test CMA at runtime. i also thought Michal also wants this module to show people the usage of CMA as well.

i'll much appreciate if you have time to move it to /proc.

-barry

#11 lore raw
In reply to Cong Wang

2012/3/7 Cong Wang <[email protected]>:

Quoted 9 lines · Barry Song
Quoted 4 lines

if you think so, pls do that and send v4 with your s-o-b.

This is not a good attitude, nor helps your patch to be merged...

Be collaborative.

pls just focus on techinical discussion and note you are not the man to decide whether the patch should be merged. Many people know how to make patches merged well.
you said that just like you are the one maintaining MM. Be modest, man!

-barry

#12 lore raw
In reply to Cong Wang
Quoted 20 lines · Barry Song
Quoted 19 lines

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
Quoted 1 line · Cong Wang

Any reason why using /dev not /proc or /sys?

Because /proc is for processes and /sys is for stable, documented APIs.

If anything, debugfs might make some sense, but since the module needs struct device anyway, it can just register a misc device, which is simpler and does not require debugfs to be compiled.

Signature · 4 lines
Best regards,                                         _     _
.o. | Liege of Serenely Enlightened Majesty of      o' \,=./ `o
..o | Computer Science,  Micha? ?mina86? Nazarewicz    (o o)
ooo +----<email/xmpp: [email protected]>--------------ooO--(_)--Ooo--
#13 lore raw

2012/3/7 Michal Nazarewicz <[email protected]>:

Quoted 33 lines
Quoted 25 lines · Barry Song
Quoted 24 lines

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
Quoted 2 lines · Cong Wang

Any reason why using /dev not /proc or /sys?

Quoted 6 lines

Because /proc is for processes and /sys is for stable, documented APIs.

If anything, debugfs might make some sense, but since the module needs struct
device anyway, it can just register a misc device, which is simpler and does not require debugfs to be compiled.

Michal, thanks for clarification. actually after i sent my mail to agree Cong's /proc might be ok, then i had more thinking, i came back to think the original /dev is better since anyway we need to device to alloc CM.
then we let the device's read/write to handle alloc/free. it is much more direct for a test module.

Quoted 7 lines

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

-barry

#14 lore raw
In reply to Barry Song

Thanks! Some minor comments, hopefully last ones:

Quoted 3 lines · Barry Song
tools/cma/cma_test.c new file +3 -0
+static ssize_t
+cma_test_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
+{

[...]

tools/cma/cma_test.c line 51

Quoted 1 line
tools/cma/cma_test.c new file +1 -0
+	_dev_info(cma_dev, "free: CM virt: %p dma: %p size:%uK\n",

This should read: ?size: %zuK?. ?z? is the proper modifier to use for size_t type.

Quoted 4 lines
tools/cma/cma_test.c new file +4 -0
+		alloc->virt, (void *)alloc->dma, alloc->size / SZ_1K);
+	kfree(alloc);
+
+	return 0;

tools/cma/cma_test.c line 70

Quoted 10 lines
tools/cma/cma_test.c new file +9 -0
+}
 
+static ssize_t
+cma_test_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
+{
+	struct cma_allocation *alloc;
+	size_t size;
+	int ret;
+
+	ret = kstrtouint_from_user(buf, count, 0, &size);

kstrtouint_from_user() expects pointer to unsigned int, size_t is not always unsigned int. It may be unsigned long. As such, size should be of type unsigned long and this line should use kstrtoulong_from_user().

[...]

tools/cma/cma_test.c line 90

Quoted 3 lines
tools/cma/cma_test.c new file +3 -0
+	if (alloc->virt) {
+		_dev_info(cma_dev, "alloc: virt: %p dma: %p size: %uK\n",
+			alloc->virt, (void *)alloc->dma, size);

Like previously, this should be ?size: %zuK?.

[...]

Quoted 1 line

+}

Signature · 4 lines
Best regards,                                         _     _
.o. | Liege of Serenely Enlightened Majesty of      o' \,=./ `o
..o | Computer Science,  Micha? ?mina86? Nazarewicz    (o o)
ooo +----<email/xmpp: [email protected]>--------------ooO--(_)--Ooo--
#15 lore raw

2012/3/7 Michal Nazarewicz <[email protected]>:

Quoted 14 lines

Thanks! ?Some minor comments, hopefully last ones:

Quoted 5 lines · Barry Song
(quoted diff) +2 -0
+static ssize_t
+cma_test_read(struct file *file, char __user *buf, size_t count, loff_t

*ppos)
+{

[...]

Quoted 5 lines
Quoted 1 line

+ ? ? ? _dev_info(cma_dev, "free: CM virt: %p dma: %p size:%uK\n",

This should read: ?size: %zuK?. ??z? is the proper modifier to use for size_t type.

ok. thanks.

Quoted 9 lines
Quoted 7 lines

+ ? ? ? ? ? ? ? alloc->virt, (void *)alloc->dma, alloc->size / SZ_1K);
+ ? ? ? kfree(alloc);

tools/cma/cma_test.c new file +3 -0
+
+ ? ? ? return 0;
+}
Quoted 14 lines

Review

Quoted 9 lines
(quoted diff) +2 -0
+static ssize_t
+cma_test_write(struct file *file, const char __user *buf, size_t count,

loff_t *ppos)

(quoted diff) +6 -0
+{
+ ? ? ? struct cma_allocation *alloc;
+ ? ? ? size_t size;
+ ? ? ? int ret;
+
+ ? ? ? ret = kstrtouint_from_user(buf, count, 0, &size);

kstrtouint_from_user() expects pointer to unsigned int, size_t is not always unsigned int. ?It may be unsigned long. ?As such, size should be of type unsigned long and this line should use kstrtoulong_from_user().

it comes to a compiling warning, so re-define "size" to unsigned long:

cma_test_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)

 {
        struct cma_allocation *alloc;
-       size_t size;
+       unsigned long size;
        int ret;

- ret = kstrtouint_from_user(buf, count, 0, &size);

+       ret = kstrtoul_from_user(buf, count, 0, &size);
        if (ret)
                return ret;

@@ -86,8 +86,8 @@ cma_test_write(struct file *file, const char __user *buf, size_t count, loff_t *

                &alloc->dma, GFP_KERNEL);

        if (alloc->virt) {

- _dev_info(cma_dev, "alloc: virt: %p dma: %p size: %uK\n",
- alloc->virt, (void *)alloc->dma, size);
+ _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);
Quoted 13 lines

[...]

Quoted 3 lines

+ ? ? ? if (alloc->virt) {
+ ? ? ? ? ? ? ? _dev_info(cma_dev, "alloc: virt: %p dma: %p size: %uK\n",
+ ? ? ? ? ? ? ? ? ? ? ? alloc->virt, (void *)alloc->dma, size);

Like previously, this should be ?size: %zuK?.

[...]

Quoted 8 lines
Quoted 1 line

+}

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

-barry