Skip to contentSkip to conversation

mm: cma: fix corruption cma_sysfs_alloc_pages_count

PATCH In review Minchan Kim 4 participants 10 messages Mar 24, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm: cma: fix corruption cma_sysfs_alloc_pages_count

struct cma_stat's lifespan for cma_sysfs is different with struct cma because kobject for sysfs requires dynamic object while CMA is static object[1]. When CMA is initialized, it couldn't use slab to allocate cma_stat since slab was not initialized yet. Thus, it allocates the dynamic object in subsys_initcall.

However, the cma allocation can happens before subsys_initcall then, it goes crash.

Dmitry reported[2]:

..

[    1.226190] [<c027762f>] (cma_sysfs_alloc_pages_count) from [<c027706f>] (cma_alloc+0x153/0x274)
[    1.226720] [<c027706f>] (cma_alloc) from [<c01112ab>] (__alloc_from_contiguous+0x37/0x8c)
[    1.227272] [<c01112ab>] (__alloc_from_contiguous) from [<c1104af9>] (atomic_pool_init+0x7b/0x126)
[    1.233596] [<c1104af9>] (atomic_pool_init) from [<c0101d69>] (do_one_initcall+0x45/0x1e4)
[    1.234188] [<c0101d69>] (do_one_initcall) from [<c1101141>] (kernel_init_freeable+0x157/0x1a6)
[    1.234741] [<c1101141>] (kernel_init_freeable) from [<c0a27fd1>] (kernel_init+0xd/0xe0)
[    1.235289] [<c0a27fd1>] (kernel_init) from [<c0100155>] (ret_from_fork+0x11/0x1c)

This patch moves those statistic fields of cma_stat into struct cma and introduces cma_kobject wrapper to follow kobject's rule.

At the same time, it fixes other routines based on suggestions[3][4].

[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://lore.kernel.org/linux-mm/[email protected]/
[3] https://lore.kernel.org/linux-mm/[email protected]/
[4] https://lore.kernel.org/linux-mm/[email protected]/

---
I belive it's worth to have separate patch rather than replacing original patch. It will also help to merge without conflict since we already filed other patch based on it. Strictly speaking, separating fix part and readbility part in this patch would be better but it's gray to separate them since most code in this patch was done while we were fixing the bug. Since we don't release it yet, I hope it will work. Otherwise, I can send a replacement patch inclucing all of changes happend until now with gathering SoB.

 mm/cma.c       |  4 +--
 mm/cma.h       | 25 +++++++-------
 mm/cma_sysfs.c | 88 ++++++++++++++++++++++++++++----------------------
 3 files changed, 65 insertions(+), 52 deletions(-)
mm/cma.c +2 -2
index 90e27458ddb7..08c45157911a 100644
@@ -509,11 +509,11 @@ struct page *cma_alloc(struct cma *cma, size_t count, unsigned int align,
 out:
 	if (page) {
 		count_vm_event(CMA_ALLOC_SUCCESS);
-		cma_sysfs_alloc_pages_count(cma, count);
+		cma_sysfs_account_success_pages(cma, count);
 	} else {
 		count_vm_event(CMA_ALLOC_FAIL);
 		if (cma)
-			cma_sysfs_fail_pages_count(cma, count);
+			cma_sysfs_account_fail_pages(cma, count);
 	}
 
 	return page;
mm/cma.h +14 -11
index 95d1aa2d808a..37b9b7858c8e 100644
@@ -5,12 +5,8 @@
 #include <linux/debugfs.h>
 #include <linux/kobject.h>
 
-struct cma_stat {
-	spinlock_t lock;
-	/* the number of CMA page successful allocations */
-	unsigned long nr_pages_succeeded;
-	/* the number of CMA page allocation failures */
-	unsigned long nr_pages_failed;
+struct cma_kobject {
+	struct cma *cma;
 	struct kobject kobj;
 };
 
@@ -27,7 +23,12 @@ struct cma {
 #endif
 	char name[CMA_MAX_NAME];
 #ifdef CONFIG_CMA_SYSFS
-	struct cma_stat	*stat;
+	/* the number of CMA page successful allocations */
+	atomic64_t nr_pages_succeeded;
+	/* the number of CMA page allocation failures */
+	atomic64_t nr_pages_failed;
+	/* kobject requires dynamic object */
+	struct cma_kobject *cma_kobj;
 #endif
 };
 
@@ -40,10 +41,12 @@ static inline unsigned long cma_bitmap_maxno(struct cma *cma)
 }
 
 #ifdef CONFIG_CMA_SYSFS
-void cma_sysfs_alloc_pages_count(struct cma *cma, size_t count);
-void cma_sysfs_fail_pages_count(struct cma *cma, size_t count);
+void cma_sysfs_account_success_pages(struct cma *cma, unsigned long nr_pages);
+void cma_sysfs_account_fail_pages(struct cma *cma, unsigned long nr_pages);
 #else
-static inline void cma_sysfs_alloc_pages_count(struct cma *cma, size_t count) {};
-static inline void cma_sysfs_fail_pages_count(struct cma *cma, size_t count) {};
+static inline void cma_sysfs_account_success_pages(struct cma *cma,
+						   unsigned long nr_pages) {};
+static inline void cma_sysfs_account_fail_pages(struct cma *cma,
+						unsigned long nr_pages) {};
 #endif
 #endif
mm/cma_sysfs.c +49 -39

143 lines not on this page · raw message

Signature · 1 line
2.31.0.291.g576ba9dcdaf-goog
#1 lore raw

24.03.2021 22:20, Minchan Kim пишет:

mm/cma_sysfs.c ATTRIBUTE_GROUPS(cma); · line 109

Quoted 9 lines

static int __init cma_sysfs_init(void)
{

mm/cma_sysfs.c +3 -1
-	int i = 0;
+	struct kobject *cma_kobj_root;
+	struct cma_kobject *cma_kobj;
 	struct cma *cma;
+	unsigned int i;
 
 	while (--i >= 0) {

Do you realize that this doesn't work anymore?

mm/cma_sysfs.c ATTRIBUTE_GROUPS(cma); · line 113

Quoted 8 lines
mm/cma_sysfs.c +2 -4
 		cma = &cma_areas[i];
-		kobject_put(&cma->stat->kobj);
-	}
 
-	kfree(cma_stats);
-	kobject_put(cma_kobj);
+		kobject_put(&cma->cma_kobj->kobj);
+		kfree(cma->cma_kobj);

Freeing a null pointer?

Quoted 3 lines
mm/cma_sysfs.c +3 -0
+		cma->cma_kobj = NULL;
+	}
+	kobject_put(cma_kobj_root);
#2 lore raw
Quoted 17 lines · Dmitry Osipenko

24.03.2021 22:20, Minchan Kim пишет:

Review

Quoted 9 lines

static int __init cma_sysfs_init(void)
{

(quoted diff) +3 -1
-	int i = 0;
+	struct kobject *cma_kobj_root;
+	struct cma_kobject *cma_kobj;
 	struct cma *cma;
+	unsigned int i;
 
 	while (--i >= 0) {

Do you realize that this doesn't work anymore?

Quoted 4 lines
(quoted diff) +0 -2
 		cma = &cma_areas[i];
-		kobject_put(&cma->stat->kobj);
-	}
Quoted 6 lines

Review

Quoted 4 lines
(quoted diff) +2 -2
-	kfree(cma_stats);
-	kobject_put(cma_kobj);
+		kobject_put(&cma->cma_kobj->kobj);
+		kfree(cma->cma_kobj);

Freeing a null pointer?

Need coffee.

mm/cma_sysfs.c +1 -5
index a670a80aad6f..73463be08df7 100644
@@ -79,8 +79,7 @@ static int __init cma_sysfs_init(void)
        struct kobject *cma_kobj_root;
        struct cma_kobject *cma_kobj;
        struct cma *cma;
-       unsigned int i;
-       int err;
+       int i, err;
 
        cma_kobj_root = kobject_create_and_add("cma", mm_kobj);
        if (!cma_kobj_root)
@@ -108,10 +107,7 @@ static int __init cma_sysfs_init(void)
 out:
        while (--i >= 0) {
                cma = &cma_areas[i];
-
                kobject_put(&cma->cma_kobj->kobj);
-               kfree(cma->cma_kobj);
-               cma->cma_kobj = NULL;
        }
        kobject_put(cma_kobj_root);
 
#3 lore raw
In reply to Dmitry Osipenko

24.03.2021 22:43, Dmitry Osipenko пишет:

Quoted 24 lines

24.03.2021 22:20, Minchan Kim пишет:

mm/cma_sysfs.c ATTRIBUTE_GROUPS(cma); · line 109

Quoted 9 lines

static int __init cma_sysfs_init(void)
{

mm/cma_sysfs.c +3 -1
-	int i = 0;
+	struct kobject *cma_kobj_root;
+	struct cma_kobject *cma_kobj;
 	struct cma *cma;
+	unsigned int i;
 
 	while (--i >= 0) {

Do you realize that this doesn't work anymore?

mm/cma_sysfs.c ATTRIBUTE_GROUPS(cma); · line 113

Quoted 8 lines
mm/cma_sysfs.c +2 -4
 		cma = &cma_areas[i];
-		kobject_put(&cma->stat->kobj);
-	}
 
-	kfree(cma_stats);
-	kobject_put(cma_kobj);
+		kobject_put(&cma->cma_kobj->kobj);
+		kfree(cma->cma_kobj);

Freeing a null pointer?

Quoted 4 lines

mm/cma_sysfs.c ATTRIBUTE_GROUPS(cma); · line 116

Quoted 3 lines
mm/cma_sysfs.c +3 -0
+		cma->cma_kobj = NULL;
+	}
+	kobject_put(cma_kobj_root);

Please try to simulate the errors and check that error path is working properly in the next version.

Alternatively, we could remove the cma_kobj_release entirely, like Greg suggested previously, and then don't care about cleaning up at all.

#4 lore raw
Quoted 30 lines · Dmitry Osipenko

24.03.2021 22:43, Dmitry Osipenko пишет:

Quoted 24 lines

24.03.2021 22:20, Minchan Kim пишет:

mm/cma_sysfs.c ATTRIBUTE_GROUPS(cma); · line 109

Quoted 9 lines

static int __init cma_sysfs_init(void)
{

mm/cma_sysfs.c +3 -1
-	int i = 0;
+	struct kobject *cma_kobj_root;
+	struct cma_kobject *cma_kobj;
 	struct cma *cma;
+	unsigned int i;
 
 	while (--i >= 0) {

Do you realize that this doesn't work anymore?

mm/cma_sysfs.c ATTRIBUTE_GROUPS(cma); · line 113

Quoted 8 lines
mm/cma_sysfs.c +2 -4
 		cma = &cma_areas[i];
-		kobject_put(&cma->stat->kobj);
-	}
 
-	kfree(cma_stats);
-	kobject_put(cma_kobj);
+		kobject_put(&cma->cma_kobj->kobj);
+		kfree(cma->cma_kobj);

Freeing a null pointer?

Quoted 4 lines

mm/cma_sysfs.c ATTRIBUTE_GROUPS(cma); · line 116

Quoted 3 lines
mm/cma_sysfs.c +3 -0
+		cma->cma_kobj = NULL;
+	}
+	kobject_put(cma_kobj_root);
Quoted 5 lines

Please try to simulate the errors and check that error path is working properly in the next version.

Alternatively, we could remove the cma_kobj_release entirely, like Greg suggested previously, and then don't care about cleaning up at all.

Does he suggested it to remove cma_kobj_release?(Initially, I did but was rejected from Greg)

#5 lore raw

24.03.2021 22:57, Minchan Kim пишет:

Quoted 31 lines · Dmitry Osipenko
Quoted 30 lines

24.03.2021 22:43, Dmitry Osipenko пишет:

Quoted 28 lines

24.03.2021 22:20, Minchan Kim пишет:

mm/cma_sysfs.c ATTRIBUTE_GROUPS(cma); · line 109

Quoted 9 lines

static int __init cma_sysfs_init(void)
{

mm/cma_sysfs.c +3 -1
-	int i = 0;
+	struct kobject *cma_kobj_root;
+	struct cma_kobject *cma_kobj;
 	struct cma *cma;
+	unsigned int i;
 
 	while (--i >= 0) {

Do you realize that this doesn't work anymore?

mm/cma_sysfs.c ATTRIBUTE_GROUPS(cma); · line 113

Quoted 8 lines
mm/cma_sysfs.c +2 -4
 		cma = &cma_areas[i];
-		kobject_put(&cma->stat->kobj);
-	}
 
-	kfree(cma_stats);
-	kobject_put(cma_kobj);
+		kobject_put(&cma->cma_kobj->kobj);
+		kfree(cma->cma_kobj);

Freeing a null pointer?

Quoted 3 lines
mm/cma_sysfs.c +3 -0
+		cma->cma_kobj = NULL;
+	}
+	kobject_put(cma_kobj_root);
Quoted 9 lines
Quoted 5 lines

Please try to simulate the errors and check that error path is working properly in the next version.

Alternatively, we could remove the cma_kobj_release entirely, like Greg suggested previously, and then don't care about cleaning up at all.

Does he suggested it to remove cma_kobj_release?(Initially, I did but was rejected from Greg)

Alright, I haven't followed the previous threads fully and only saw the reply where he suggested to removed it.

#6 lore raw
Quoted 38 lines · Dmitry Osipenko

24.03.2021 22:57, Minchan Kim пишет:

Quoted 37 lines · Dmitry Osipenko
Quoted 35 lines

24.03.2021 22:43, Dmitry Osipenko пишет:

Quoted 24 lines

24.03.2021 22:20, Minchan Kim пишет:

mm/cma_sysfs.c ATTRIBUTE_GROUPS(cma); · line 109

Quoted 9 lines

static int __init cma_sysfs_init(void)
{

mm/cma_sysfs.c +3 -1
-	int i = 0;
+	struct kobject *cma_kobj_root;
+	struct cma_kobject *cma_kobj;
 	struct cma *cma;
+	unsigned int i;
 
 	while (--i >= 0) {

Do you realize that this doesn't work anymore?

mm/cma_sysfs.c ATTRIBUTE_GROUPS(cma); · line 113

Quoted 8 lines
mm/cma_sysfs.c +2 -4
 		cma = &cma_areas[i];
-		kobject_put(&cma->stat->kobj);
-	}
 
-	kfree(cma_stats);
-	kobject_put(cma_kobj);
+		kobject_put(&cma->cma_kobj->kobj);
+		kfree(cma->cma_kobj);

Freeing a null pointer?

Quoted 4 lines

mm/cma_sysfs.c ATTRIBUTE_GROUPS(cma); · line 116

Quoted 3 lines
mm/cma_sysfs.c +3 -0
+		cma->cma_kobj = NULL;
+	}
+	kobject_put(cma_kobj_root);

Please try to simulate the errors and check that error path is working properly in the next version.

Alternatively, we could remove the cma_kobj_release entirely, like Greg suggested previously, and then don't care about cleaning up at all.

Quoted 6 lines
Quoted 3 lines

Does he suggested it to remove cma_kobj_release?(Initially, I did but was rejected from Greg)

Alright, I haven't followed the previous threads fully and only saw the reply where he suggested to removed it.

No problem. I just posted it new version. Hopefully, it tastes good for you. ;-)

Thanks for the review!

#7 lore raw
In reply to Minchan Kim
Quoted 37 lines · Minchan Kim

struct cma_stat's lifespan for cma_sysfs is different with struct cma because kobject for sysfs requires dynamic object while CMA is static object[1]. When CMA is initialized, it couldn't use slab to allocate cma_stat since slab was not initialized yet. Thus, it allocates the dynamic object in subsys_initcall.

However, the cma allocation can happens before subsys_initcall then, it goes crash.

Dmitry reported[2]:

..

[    1.226190] [<c027762f>] (cma_sysfs_alloc_pages_count) from [<c027706f>] (cma_alloc+0x153/0x274)
[    1.226720] [<c027706f>] (cma_alloc) from [<c01112ab>] (__alloc_from_contiguous+0x37/0x8c)
[    1.227272] [<c01112ab>] (__alloc_from_contiguous) from [<c1104af9>] (atomic_pool_init+0x7b/0x126)
[    1.233596] [<c1104af9>] (atomic_pool_init) from [<c0101d69>] (do_one_initcall+0x45/0x1e4)
[    1.234188] [<c0101d69>] (do_one_initcall) from [<c1101141>] (kernel_init_freeable+0x157/0x1a6)
[    1.234741] [<c1101141>] (kernel_init_freeable) from [<c0a27fd1>] (kernel_init+0xd/0xe0)
[    1.235289] [<c0a27fd1>] (kernel_init) from [<c0100155>] (ret_from_fork+0x11/0x1c)

This patch moves those statistic fields of cma_stat into struct cma and introduces cma_kobject wrapper to follow kobject's rule.

At the same time, it fixes other routines based on suggestions[3][4].

[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://lore.kernel.org/linux-mm/[email protected]/
[3] https://lore.kernel.org/linux-mm/[email protected]/
[4] https://lore.kernel.org/linux-mm/[email protected]/

Quoted 10 lines

---
I belive it's worth to have separate patch rather than replacing original patch. It will also help to merge without conflict since we already filed other patch based on it. Strictly speaking, separating fix part and readbility part in this patch would be better but it's gray to separate them since most code in this patch was done while we were fixing the bug. Since we don't release it yet, I hope it will work. Otherwise, I can send a replacement patch inclucing all of changes happend until now with gathering SoB.

If we still have a choice, we should not merge a patch that has a known serious problem, such as a crash. That's only done if the broken problematic patch has already been committed to a tree that doesn't allow rebasing, such as of course the main linux.git.

Here, I *think* it's just in linux-next and mmotm, so we still are allowed to fix the original patch.

thanks,

Signature · 2 lines
John Hubbard
NVIDIA
#8 lore raw
Quoted 49 lines · John Hubbard
Quoted 37 lines · Minchan Kim

struct cma_stat's lifespan for cma_sysfs is different with struct cma because kobject for sysfs requires dynamic object while CMA is static object[1]. When CMA is initialized, it couldn't use slab to allocate cma_stat since slab was not initialized yet. Thus, it allocates the dynamic object in subsys_initcall.

However, the cma allocation can happens before subsys_initcall then, it goes crash.

Dmitry reported[2]:

..

[    1.226190] [<c027762f>] (cma_sysfs_alloc_pages_count) from [<c027706f>] (cma_alloc+0x153/0x274)
[    1.226720] [<c027706f>] (cma_alloc) from [<c01112ab>] (__alloc_from_contiguous+0x37/0x8c)
[    1.227272] [<c01112ab>] (__alloc_from_contiguous) from [<c1104af9>] (atomic_pool_init+0x7b/0x126)
[    1.233596] [<c1104af9>] (atomic_pool_init) from [<c0101d69>] (do_one_initcall+0x45/0x1e4)
[    1.234188] [<c0101d69>] (do_one_initcall) from [<c1101141>] (kernel_init_freeable+0x157/0x1a6)
[    1.234741] [<c1101141>] (kernel_init_freeable) from [<c0a27fd1>] (kernel_init+0xd/0xe0)
[    1.235289] [<c0a27fd1>] (kernel_init) from [<c0100155>] (ret_from_fork+0x11/0x1c)

This patch moves those statistic fields of cma_stat into struct cma and introduces cma_kobject wrapper to follow kobject's rule.

At the same time, it fixes other routines based on suggestions[3][4].

[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://lore.kernel.org/linux-mm/[email protected]/
[3] https://lore.kernel.org/linux-mm/[email protected]/
[4] https://lore.kernel.org/linux-mm/[email protected]/

Quoted 10 lines

---
I belive it's worth to have separate patch rather than replacing original patch. It will also help to merge without conflict since we already filed other patch based on it. Strictly speaking, separating fix part and readbility part in this patch would be better but it's gray to separate them since most code in this patch was done while we were fixing the bug. Since we don't release it yet, I hope it will work. Otherwise, I can send a replacement patch inclucing all of changes happend until now with gathering SoB.

Quoted 7 lines

If we still have a choice, we should not merge a patch that has a known serious problem, such as a crash. That's only done if the broken problematic patch has already been committed to a tree that doesn't allow rebasing, such as of course the main linux.git.

Here, I *think* it's just in linux-next and mmotm, so we still are allowed to fix the original patch.

Yes, that's what we should do in case it's not upstream yet. Clean resend + re-apply.

Signature · 3 lines
Thanks,

David / dhildenb
#9 lore raw
Quoted 55 lines · David Hildenbrand
Quoted 54 lines · John Hubbard
Quoted 37 lines · Minchan Kim

struct cma_stat's lifespan for cma_sysfs is different with struct cma because kobject for sysfs requires dynamic object while CMA is static object[1]. When CMA is initialized, it couldn't use slab to allocate cma_stat since slab was not initialized yet. Thus, it allocates the dynamic object in subsys_initcall.

However, the cma allocation can happens before subsys_initcall then, it goes crash.

Dmitry reported[2]:

..

[    1.226190] [<c027762f>] (cma_sysfs_alloc_pages_count) from [<c027706f>] (cma_alloc+0x153/0x274)
[    1.226720] [<c027706f>] (cma_alloc) from [<c01112ab>] (__alloc_from_contiguous+0x37/0x8c)
[    1.227272] [<c01112ab>] (__alloc_from_contiguous) from [<c1104af9>] (atomic_pool_init+0x7b/0x126)
[    1.233596] [<c1104af9>] (atomic_pool_init) from [<c0101d69>] (do_one_initcall+0x45/0x1e4)
[    1.234188] [<c0101d69>] (do_one_initcall) from [<c1101141>] (kernel_init_freeable+0x157/0x1a6)
[    1.234741] [<c1101141>] (kernel_init_freeable) from [<c0a27fd1>] (kernel_init+0xd/0xe0)
[    1.235289] [<c0a27fd1>] (kernel_init) from [<c0100155>] (ret_from_fork+0x11/0x1c)

This patch moves those statistic fields of cma_stat into struct cma and introduces cma_kobject wrapper to follow kobject's rule.

At the same time, it fixes other routines based on suggestions[3][4].

[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://lore.kernel.org/linux-mm/[email protected]/
[3] https://lore.kernel.org/linux-mm/[email protected]/
[4] https://lore.kernel.org/linux-mm/[email protected]/

Quoted 10 lines

---
I belive it's worth to have separate patch rather than replacing original patch. It will also help to merge without conflict since we already filed other patch based on it. Strictly speaking, separating fix part and readbility part in this patch would be better but it's gray to separate them since most code in this patch was done while we were fixing the bug. Since we don't release it yet, I hope it will work. Otherwise, I can send a replacement patch inclucing all of changes happend until now with gathering SoB.

If we still have a choice, we should not merge a patch that has a known serious problem, such as a crash. That's only done if the broken problematic patch has already been committed to a tree that doesn't allow rebasing, such as of course the main linux.git.

Quoted 5 lines
Quoted 2 lines

Here, I *think* it's just in linux-next and mmotm, so we still are allowed to fix the original patch.

Yes, that's what we should do in case it's not upstream yet. Clean resend + re-apply.

Okay, let me replace the original one including all other patches.