diff options
author | Mahendran Ganesh <opensource.ganesh@gmail.com> | 2014-12-12 19:57:01 -0500 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2014-12-13 15:42:50 -0500 |
commit | 40f9fb8cffc6a20ae269e3b43dfba7a4f65d7f50 (patch) | |
tree | 734307fb40afba740ff4b0a6b34a0807fba4cc64 | |
parent | af4ee5e977acb150371c28bd85cb7e34cac48b13 (diff) |
mm/zsmalloc: support allocating obj with size of ZS_MAX_ALLOC_SIZE
I sent a patch [1] for unnecessary check in zsmalloc. And Minchan Kim
found zsmalloc even does not support allocating an obj with the size of
ZS_MAX_ALLOC_SIZE in some situations.
For example:
In system with 64KB PAGE_SIZE and 32 bit of physical addr. Then:
ZS_MIN_ALLOC_SIZE is 32 bytes which is calculated by:
MAX(32, (ZS_MAX_PAGES_PER_ZSPAGE << PAGE_SHIFT >> OBJ_INDEX_BITS))
ZS_MAX_ALLOC_SIZE is 64KB(in current code, is PAGE_SIZE)
ZS_SIZE_CLASS_DELTA is 256 bytes
So, ZS_SIZE_CLASSES = (ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) /
ZS_SIZE_CLASS_DELTA + 1
= 256
In zs_create_pool(), the max size obj which can be allocated will be:
ZS_MIN_ALLOC_SIZE + i * ZS_SIZE_CLASS_DELTA = 32 + 255*256 = 65312
We can see that 65312 < 65536 (ZS_MAX_ALLOC_SIZE). So we can NOT
allocate objs with size ZS_MAX_ALLOC_SIZE(65536) which we promise upper
users we can do.
[1] http://lkml.iu.edu/hypermail/linux/kernel/1411.2/03835.html
[2] http://lkml.iu.edu/hypermail/linux/kernel/1411.2/04534.html
This patch fixes this issue by dynamiclly calculating zs_size_classes when
module is loaded, allocates buffer with size ZS_MAX_ALLOC_SIZE. Then the
max obj(size is ZS_MAX_ALLOC_SIZE) can be stored in it.
[akpm@linux-foundation.org: restore ZS_SIZE_CLASSES to fix bisectability]
Signed-off-by: Mahendran Ganesh <opensource.ganesh@gmail.com>
Suggested-by: Minchan Kim <minchan@kernel.org>
Cc: Nitin Gupta <ngupta@vflare.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
-rw-r--r-- | mm/zsmalloc.c | 38 |
1 files changed, 32 insertions, 6 deletions
diff --git a/mm/zsmalloc.c b/mm/zsmalloc.c index 83ecdb675c40..61e180931ca8 100644 --- a/mm/zsmalloc.c +++ b/mm/zsmalloc.c | |||
@@ -171,6 +171,11 @@ enum fullness_group { | |||
171 | }; | 171 | }; |
172 | 172 | ||
173 | /* | 173 | /* |
174 | * number of size_classes | ||
175 | */ | ||
176 | static int zs_size_classes; | ||
177 | |||
178 | /* | ||
174 | * We assign a page to ZS_ALMOST_EMPTY fullness group when: | 179 | * We assign a page to ZS_ALMOST_EMPTY fullness group when: |
175 | * n <= N / f, where | 180 | * n <= N / f, where |
176 | * n = number of allocated objects | 181 | * n = number of allocated objects |
@@ -214,7 +219,7 @@ struct link_free { | |||
214 | }; | 219 | }; |
215 | 220 | ||
216 | struct zs_pool { | 221 | struct zs_pool { |
217 | struct size_class *size_class[ZS_SIZE_CLASSES]; | 222 | struct size_class **size_class; |
218 | 223 | ||
219 | gfp_t flags; /* allocation flags used when growing pool */ | 224 | gfp_t flags; /* allocation flags used when growing pool */ |
220 | atomic_long_t pages_allocated; | 225 | atomic_long_t pages_allocated; |
@@ -785,7 +790,7 @@ static inline int __zs_cpu_up(struct mapping_area *area) | |||
785 | */ | 790 | */ |
786 | if (area->vm_buf) | 791 | if (area->vm_buf) |
787 | return 0; | 792 | return 0; |
788 | area->vm_buf = (char *)__get_free_page(GFP_KERNEL); | 793 | area->vm_buf = kmalloc(ZS_MAX_ALLOC_SIZE, GFP_KERNEL); |
789 | if (!area->vm_buf) | 794 | if (!area->vm_buf) |
790 | return -ENOMEM; | 795 | return -ENOMEM; |
791 | return 0; | 796 | return 0; |
@@ -793,8 +798,7 @@ static inline int __zs_cpu_up(struct mapping_area *area) | |||
793 | 798 | ||
794 | static inline void __zs_cpu_down(struct mapping_area *area) | 799 | static inline void __zs_cpu_down(struct mapping_area *area) |
795 | { | 800 | { |
796 | if (area->vm_buf) | 801 | kfree(area->vm_buf); |
797 | free_page((unsigned long)area->vm_buf); | ||
798 | area->vm_buf = NULL; | 802 | area->vm_buf = NULL; |
799 | } | 803 | } |
800 | 804 | ||
@@ -912,6 +916,17 @@ static int zs_register_cpu_notifier(void) | |||
912 | return notifier_to_errno(ret); | 916 | return notifier_to_errno(ret); |
913 | } | 917 | } |
914 | 918 | ||
919 | static void init_zs_size_classes(void) | ||
920 | { | ||
921 | int nr; | ||
922 | |||
923 | nr = (ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) / ZS_SIZE_CLASS_DELTA + 1; | ||
924 | if ((ZS_MAX_ALLOC_SIZE - ZS_MIN_ALLOC_SIZE) % ZS_SIZE_CLASS_DELTA) | ||
925 | nr += 1; | ||
926 | |||
927 | zs_size_classes = nr; | ||
928 | } | ||
929 | |||
915 | static void __exit zs_exit(void) | 930 | static void __exit zs_exit(void) |
916 | { | 931 | { |
917 | #ifdef CONFIG_ZPOOL | 932 | #ifdef CONFIG_ZPOOL |
@@ -929,6 +944,8 @@ static int __init zs_init(void) | |||
929 | return ret; | 944 | return ret; |
930 | } | 945 | } |
931 | 946 | ||
947 | init_zs_size_classes(); | ||
948 | |||
932 | #ifdef CONFIG_ZPOOL | 949 | #ifdef CONFIG_ZPOOL |
933 | zpool_register_driver(&zs_zpool_driver); | 950 | zpool_register_driver(&zs_zpool_driver); |
934 | #endif | 951 | #endif |
@@ -972,11 +989,18 @@ struct zs_pool *zs_create_pool(gfp_t flags) | |||
972 | if (!pool) | 989 | if (!pool) |
973 | return NULL; | 990 | return NULL; |
974 | 991 | ||
992 | pool->size_class = kcalloc(zs_size_classes, sizeof(struct size_class *), | ||
993 | GFP_KERNEL); | ||
994 | if (!pool->size_class) { | ||
995 | kfree(pool); | ||
996 | return NULL; | ||
997 | } | ||
998 | |||
975 | /* | 999 | /* |
976 | * Iterate reversly, because, size of size_class that we want to use | 1000 | * Iterate reversly, because, size of size_class that we want to use |
977 | * for merging should be larger or equal to current size. | 1001 | * for merging should be larger or equal to current size. |
978 | */ | 1002 | */ |
979 | for (i = ZS_SIZE_CLASSES - 1; i >= 0; i--) { | 1003 | for (i = zs_size_classes - 1; i >= 0; i--) { |
980 | int size; | 1004 | int size; |
981 | int pages_per_zspage; | 1005 | int pages_per_zspage; |
982 | struct size_class *class; | 1006 | struct size_class *class; |
@@ -1029,7 +1053,7 @@ void zs_destroy_pool(struct zs_pool *pool) | |||
1029 | { | 1053 | { |
1030 | int i; | 1054 | int i; |
1031 | 1055 | ||
1032 | for (i = 0; i < ZS_SIZE_CLASSES; i++) { | 1056 | for (i = 0; i < zs_size_classes; i++) { |
1033 | int fg; | 1057 | int fg; |
1034 | struct size_class *class = pool->size_class[i]; | 1058 | struct size_class *class = pool->size_class[i]; |
1035 | 1059 | ||
@@ -1047,6 +1071,8 @@ void zs_destroy_pool(struct zs_pool *pool) | |||
1047 | } | 1071 | } |
1048 | kfree(class); | 1072 | kfree(class); |
1049 | } | 1073 | } |
1074 | |||
1075 | kfree(pool->size_class); | ||
1050 | kfree(pool); | 1076 | kfree(pool); |
1051 | } | 1077 | } |
1052 | EXPORT_SYMBOL_GPL(zs_destroy_pool); | 1078 | EXPORT_SYMBOL_GPL(zs_destroy_pool); |