mm: add generic adaptive large memory allocation APIs
Subsystems (9)
PATCH 1/9 mm: add generic adaptive large memory allocation APIs
generic adaptive large memory allocation APIs
kv*alloc are used to allocate large contiguous memory and the users don't mind whether the memory is physically or virtually contiguous. The allocator always try its best to allocate physically contiguous memory first.
In this patch set, some APIs are introduced: kvmalloc(), kvzalloc(), kvcalloc(), kvrealloc(), kvfree() and kvfree_inatomic().
----
include/linux/mm.h | 31 ++++++++++++++ include/linux/vmalloc.h | 1 mm/nommu.c | 6 ++ mm/util.c | 104 ++++++++++++++++++++++++++++++++++++++++++++++++ mm/vmalloc.c | 14 ++++++ 5 files changed, 156 insertions(+)
include/linux/mm.h +31 -0
@@ -1467,5 +1467,36 @@ extern int soft_offline_page(struct page *page, int flags);extern void dump_page (struct page *page); +void *__kvmalloc (size_t size,gfp_t flags); + +static inline void *kvmalloc (size_t size) +{ +return __kvmalloc (size,0 ); +} + +static inline void *kvzalloc (size_t size) +{ +return __kvmalloc (size, __GFP_ZERO); +} + +static inline void *kvcalloc (size_t n,size_t size) +{ +return __kvmalloc (n * size, __GFP_ZERO); +} + +void __kvfree (void *ptr,bool inatomic); + +static inline void kvfree (void *ptr) +{ +__kvfree (ptr, false); +} + +static inline void kvfree_inatomic (void *ptr) +{ +__kvfree (ptr, true); +} + +void *kvrealloc (void *ptr,size_t newsize); +#endif /* __KERNEL__ */ #endif /* _LINUX_MM_H */
include/linux/vmalloc.h +1 -0
@@ -60,6 +60,7 @@ extern void *__vmalloc(unsigned long size, gfp_t gfp_mask, pgprot_t prot);extern void *__vmalloc_area (struct vm_struct *area,gfp_t gfp_mask,pgprot_t prot);extern void vfree (const void *addr); +extern unsigned long vsize (const void *addr);extern void *vmap (struct page **pages,unsigned int count,unsigned long flags,pgprot_t prot);
mm/nommu.c +6 -0
@@ -223,6 +223,12 @@ void vfree(const void *addr) }EXPORT_SYMBOL (vfree); +unsigned long vsize (const void *addr) +{ +return ksize (addr); +} +EXPORT_SYMBOL (vsize); +void *__vmalloc (unsigned long size,gfp_t gfp_mask,pgprot_t prot) {/*
mm/util.c +104 -0
116 lines not on this page · raw message
mm/vmalloc.c +14 -0
@@ -1413,6 +1413,20 @@ void vfree(const void *addr)EXPORT_SYMBOL (vfree);/** +* vsize - get the actual amount of memory allocated by vmalloc() +* @addr: memory base address +*/ +unsigned long vsize (const void *addr) +{ +struct vmap_area *va; + + va =find_vmap_area ((unsigned long )addr); + +return va->va_end - va->va_start -PAGE_SIZE ; +} +EXPORT_SYMBOL (vsize); + +/** * vunmap - release virtual mapping obtained by vmap() * @addr: memory base address *
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Peter Zijlstra
NAKQuoted 15 lines · Changli Gao
mm/util.c +15
-0+void *__kvmalloc (size_t size,gfp_t flags) +{ +void *ptr; + +if (size <PAGE_SIZE ) +return kmalloc (size,GFP_KERNEL | flags); + size =PAGE_ALIGN (size); +if (is_power_of_2 (size)) + ptr = (void *)__get_free_pages (GFP_KERNEL | flags | + __GFP_NOWARN,get_order (size)); +else + ptr =alloc_pages_exact (size,GFP_KERNEL | flags | + __GFP_NOWARN); +if (ptr !=NULL ) { +virt_to_head_page (ptr)->private = size;
mm/util.c int __attribute__((weak)) get_user_pages_fast(unsigned long start, · line 317
Quoted 10 lines
mm/util.c +10
-0+return ptr; + } + + ptr =vmalloc (size); +if (ptr !=NULL && (flags & __GFP_ZERO)) +memset (ptr,0 , size); + +return ptr; +} +EXPORT_SYMBOL (__kvmalloc);
So if I do kvmalloc(size, GFP_ATOMIC) I get GFP_KERNEL|GFP_ATOMIC, which is not a recommended variation because one should not mix __GFP_WAIT and __GFP_HIGH.
So I would simply drop the gfp argument to avoid confusion.
Quoted 19 lines
mm/util.c +19
-0+void __kvfree (void *ptr,bool inatomic) +{ +if (unlikely (ZERO_OR_NULL_PTR (ptr))) +return ; +if (is_vmalloc_addr (ptr)) { +if (inatomic) { +struct work_struct *work; + + work = ptr; +BUILD_BUG_ON (sizeof (struct work_struct ) >PAGE_SIZE ); +INIT_WORK (work, kvfree_work); +schedule_work (work); + }else { +vfree (ptr); + } + }else { +struct page *page; + + page =virt_to_head_page (ptr);
mm/util.c int __attribute__((weak)) get_user_pages_fast(unsigned long start, · line 352
Quoted 10 lines
mm/util.c +10
-0+if (PageSlab (page) ||PageCompound (page)) +kfree (ptr); +else if (is_power_of_2 (page->private)) +free_pages ((unsigned long )ptr, +get_order (page->private)); +else +free_pages_exact (ptr, page->private); + } +} +EXPORT_SYMBOL (__kvfree);
NAK, I really utterly dislike that inatomic argument. The alloc side doesn't function in atomic context either. Please keep the thing symmetric in that regards.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Re: [PATCH 1/9] mm: add generic adaptive large memory allocationAPIs
Quoted 3 lines · Peter Zijlstra
NAK, I really utterly dislike that inatomic argument. The alloc side doesn't function in atomic context either. Please keep the thing symmetric in that regards.
Excuse me. kmalloc(GFP_KERNEL) may sleep (and therefore cannot be used in atomic context). However, kfree() for memory allocated with kmalloc(GFP_KERNEL) never sleep (and therefore can be used in atomic context).
Why kmalloc() and kfree() are NOT kept symmetric?
Re: [PATCH 1/9] mm: add generic adaptive large memory allocationAPIs
Quoted 9 lines
Quoted 3 lines · Peter Zijlstra
NAK, I really utterly dislike that inatomic argument. The alloc side doesn't function in atomic context either. Please keep the thing symmetric in that regards.
Excuse me.
kmalloc(GFP_KERNEL)may sleep (and therefore cannot be used in atomic context). However,kfree()for memory allocated withkmalloc(GFP_KERNEL)never sleep (and therefore can be used in atomic context).
Whykmalloc()andkfree()are NOT kept symmetric?
In kmalloc case, we need to consider both kmalloc(GFP_KERNEL)/kfree() pair and kmalloc(GFP_ATOMIC)/kfree() pair. latter is mainly used on atomic context. To make kfree() atomic help to keep the implementation simple.
But kvmalloc don't have GFP_ATOMIC feautre. that's big difference.
A message that is not in the archive (sent off-list or not yet received): 201005132236.ADJ57893.FLFFMtOVJHOOSQ-JPay3/[email protected]
Re: [PATCH 1/9] mm: add generic adaptive large memory allocationAPIs
Quoted 9 lines
Quoted 3 lines · Peter Zijlstra
NAK, I really utterly dislike that inatomic argument. The alloc side doesn't function in atomic context either. Please keep the thing symmetric in that regards.
Excuse me.
kmalloc(GFP_KERNEL)may sleep (and therefore cannot be used in atomic context). However,kfree()for memory allocated withkmalloc(GFP_KERNEL)never sleep (and therefore can be used in atomic context).
Whykmalloc()andkfree()are NOT kept symmetric?
In kmalloc case, we need to consider both kmalloc(GFP_KERNEL)/kfree() pair and kmalloc(GFP_ATOMIC)/kfree() pair. latter is mainly used on atomic context. To make kfree() atomic help to keep the implementation simple.
But kvmalloc don't have GFP_ATOMIC feautre. that's big difference.
Re: [PATCH 1/9] mm: add generic adaptive large memory allocationAPIs
In reply to Peter ZijlstraQuoted 3 lines · Peter Zijlstra
NAK, I really utterly dislike that inatomic argument. The alloc side doesn't function in atomic context either. Please keep the thing symmetric in that regards.
Excuse me. kmalloc(GFP_KERNEL) may sleep (and therefore cannot be used in atomic context). However, kfree() for memory allocated with kmalloc(GFP_KERNEL) never sleep (and therefore can be used in atomic context).
Why kmalloc() and kfree() are NOT kept symmetric?
T24gVGh1LCBNYXkgMTMsIDIwMTAgYXQgOToyMCBQTSwgUGV0ZXIgWmlqbHN0cmEgPHBldGVyekBp bmZyYWRlYWQub3JnPiB3cm90ZToKPiBPbiBUaHUsIDIwMTAtMDUtMTMgYXQgMTc6NTEgKzA4MDAs IENoYW5nbGkgR2FvIHdyb3RlOgo+PiArdm9pZCAqX19rdm1hbGxvYyhzaXplX3Qgc2l6ZSwgZ2Zw X3QgZmxhZ3MpCj4+ICt7Cj4+ICsgwqAgwqAgwqAgdm9pZCAqcHRyOwo+PiArCj4+ICsgwqAgwqAg wqAgaWYgKHNpemUgPCBQQUdFX1NJWkUpCj4+ICsgwqAgwq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KgIMKgIMKgIGVsc2Ug aWYgKGlzX3Bvd2VyX29mXzIocGFnZS0+cHJpdmF0ZSkpCj4+ICsgwqAgwqAgwqAgwqAgwqAgwqAg wqAgwqAgwqAgwqAgwqAgZnJlZV9wYWdlcygodW5zaWduZWQgbG9uZylwdHIsCj4+ICsgwqAgwqAg wqAgwqAgwqAgwqAgwqAgwqAgwqAgwqAgwqAgwqAgwqAgwqAgwqAgwqAgwqBnZXRfb3JkZXIocGFn ZS0+cHJpdmF0ZSkpOwo+PiArIMKgIMKgIMKgIMKgIMKgIMKgIMKgIGVsc2UKPj4gKyDCoCDCoCDC oCDCoCDCoCDCoCDCoCDCoCDCoCDCoCDCoCBmcmVlX3BhZ2VzX2V4YWN0KHB0ciwgcGFnZS0+cHJp dmF0ZSk7Cj4+ICsgwqAgwqAgwqAgfQo+PiArfQo+PiArRVhQT1JUX1NZTUJPTChfX2t2ZnJlZSk7 Cj4KPiBOQUssIEkgcmVhbGx5IHV0dGVybHkgZGlzbGlrZSB0aGF0IGluYXRvbWljIGFyZ3VtZW50 LiBUaGUgYWxsb2Mgc2lkZQo+IGRvZXNuJ3QgZnVuY3Rpb24gaW4gYXRvbWljIGNvbnRleHQgZWl0 aGVyLiBQbGVhc2Uga2VlcCB0aGUgdGhpbmcKPiBzeW1tZXRyaWMgaW4gdGhhdCByZWdhcmRzLgo+ CgpUaGVyZSBhcmUgc29tZSB1c2Vycywgd2hvIHJlbGVhc2UgbWVtb3J5IGluIGF0b21pYyBjb250 ZXh0LiBmb3IKZXhhbXBsZTogZnMvZmlsZS5jOiBmZG1lbS4KCi0tIApSZWdhcmRz77yMCkNoYW5n bGkgR2FvKHhpYW9zdW9AZ21haWwuY29tKQo=
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 7 lines · Changli Gao
Quoted 4 lines
NAK, I really utterly dislike that inatomic argument. The alloc side doesn't function in atomic context either. Please keep the thing symmetric in that regards.
There are some users, who release memory in atomic context. for
example:fs/file.c: fdmem.
urgh, but yeah, aside from not using vmalloc to allocate fd tables one needs to deal with this.
But if that is the only one, I'd let them do the workqueue thing that's already there. If there really are more people wanting to do this, then maybe add: kvfree_atomic().
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 9 lines · Peter Zijlstra
Quoted 7 lines · Changli Gao
Quoted 4 lines
NAK, I really utterly dislike that inatomic argument. The alloc side doesn't function in atomic context either. Please keep the thing symmetric in that regards.
There are some users, who release memory in atomic context. for
example:fs/file.c: fdmem.
Quoted 7 lines
urgh, but yeah, aside from not using vmalloc to allocate fd tables one needs to deal with this.
But if that is the only one, I'd let them do the workqueue thing that's already there. If there really are more people wanting to do this, then maybe add:
kvfree_atomic().
Tetsuo has pointed another one in apparmor.
http://kernel.ubuntu.com/git?p=jj/ubuntu-lucid.git;a=blobdiff;f=security/apparmor/match.c;h=d2cd55419acfcae85cb748c8f837a4384a3a0d29;hp=afc2dd2260edffcf88521ae86458ad03aa8ea12c;hb=f5eba4b0a01cc671affa429ba1512b6de7caeb5b;hpb=abdff9ddaf2644d0f9962490f73e030806ba90d3
, though apparmor hasn't been merged into mainline.
Signature · 8 lines
Regards, Changli Gao([email protected]) -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 9 lines · Peter Zijlstra
Quoted 7 lines · Changli Gao
Quoted 4 lines
NAK, I really utterly dislike that inatomic argument. The alloc side doesn't function in atomic context either. Please keep the thing symmetric in that regards.
There are some users, who release memory in atomic context. for
example:fs/file.c: fdmem.
Quoted 7 lines
urgh, but yeah, aside from not using vmalloc to allocate fd tables one needs to deal with this.
But if that is the only one, I'd let them do the workqueue thing that's already there. If there really are more people wanting to do this, then maybe add:
kvfree_atomic().
Tetsuo has pointed another one in apparmor.
http://kernel.ubuntu.com/git?p=jj/ubuntu-lucid.git;a=blobdiff;f=security/apparmor/match.c;h=d2cd55419acfcae85cb748c8f837a4384a3a0d29;hp=afc2dd2260edffcf88521ae86458ad03aa8ea12c;hb=f5eba4b0a01cc671affa429ba1512b6de7caeb5b;hpb=abdff9ddaf2644d0f9962490f73e030806ba90d3
, though apparmor hasn't been merged into mainline.
Signature · 6 lines
Regards, Changli Gao([email protected]) _______________________________________________ Containers mailing list [email protected] https://lists.linux-foundation.org/mailman/listinfo/containers
A message that is not in the archive (sent off-list or not yet received): AANLkTinLT5g5SKjqmQlS2kxvvMq1gsi1jPDgOKTnrT-q-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org
Quoted 7 lines · Changli Gao
Quoted 4 lines
NAK, I really utterly dislike that inatomic argument. The alloc side doesn't function in atomic context either. Please keep the thing symmetric in that regards.
There are some users, who release memory in atomic context. for
example:fs/file.c: fdmem.
urgh, but yeah, aside from not using vmalloc to allocate fd tables one needs to deal with this.
But if that is the only one, I'd let them do the workqueue thing that's already there. If there really are more people wanting to do this, then maybe add: kvfree_atomic().
Quoted 22 lines · Peter Zijlstra
Quoted 21 lines · Changli Gao
mm/util.c +21
-0+void *__kvmalloc (size_t size,gfp_t flags) +{ + void *ptr; + + if (size < PAGE_SIZE) + return kmalloc(size, GFP_KERNEL | flags); + size = PAGE_ALIGN(size); + if (is_power_of_2(size)) + ptr = (void *)__get_free_pages(GFP_KERNEL | flags | + __GFP_NOWARN, get_order(size)); + else + ptr = alloc_pages_exact(size, GFP_KERNEL | flags | + __GFP_NOWARN); + if (ptr != NULL) { + virt_to_head_page(ptr)->private = size; + return ptr; + } + + ptr = vmalloc(size); + if (ptr != NULL && (flags & __GFP_ZERO)) + memset(ptr, 0, size);
Quoted 8 lines
mm/util.c int __attribute__((weak)) get_user_pages_fast(unsigned long start, · line 317
Quoted 4 lines
mm/util.c +4
-0+ + return ptr; +} +EXPORT_SYMBOL (__kvmalloc);So if I do
kvmalloc(size, GFP_ATOMIC)I get GFP_KERNEL|GFP_ATOMIC, which is not a recommended variation because one should not mix__GFP_WAITand__GFP_HIGH.
__kvmalloc() is only for internal use(kvmalloc, kvcalloc, and kvzalloc), and the only value of flags is __GFP_ZERO. How about replacing flags with a bool variable zero?
void *__kvmalloc(size_t size, bool zero);
Or check the value of flags in the front of __kvmalloc().
BUG_ON((flags & (~__GFP_ZERO)) != 0);
Quoted 28 lines
So I would simply drop the gfp argument to avoid confusion.
Quoted 25 lines
mm/util.c +25
-0+void __kvfree (void *ptr,bool inatomic) +{ + if (unlikely(ZERO_OR_NULL_PTR(ptr))) + return; + if (is_vmalloc_addr(ptr)) { + if (inatomic) { + struct work_struct *work; + + work = ptr; + BUILD_BUG_ON(sizeof(struct work_struct) > PAGE_SIZE); + INIT_WORK(work, kvfree_work); + schedule_work(work); + } else { + vfree(ptr); + } + } else { + struct page *page; + + page = virt_to_head_page(ptr); + if (PageSlab(page) || PageCompound(page)) + kfree(ptr); + else if (is_power_of_2(page->private)) + free_pages((unsigned long)ptr, + get_order(page->private)); + else
Quoted 9 lines
Review
Quoted 4 lines
+
free_pages_exact(ptr, page->private);
+ }(quoted diff) +2
-0+} +EXPORT_SYMBOL(__kvfree);NAK, I really utterly dislike that inatomic argument. The alloc side doesn't function in atomic context either. Please keep the thing symmetric in that regards.
There are some users, who release memory in atomic context. for
example: fs/file.c: fdmem.
Signature · 6 lines
Regards, Changli Gao([email protected]) _______________________________________________ Containers mailing list [email protected] https://lists.linux-foundation.org/mailman/listinfo/containers
A message that is not in the archive (sent off-list or not yet received): 1273744285-8128-1-git-send-email-xiaosuo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
Peter Zijlstra
NAKQuoted 15 lines · Changli Gao
mm/util.c +15
-0+void *__kvmalloc (size_t size,gfp_t flags) +{ +void *ptr; + +if (size <PAGE_SIZE ) +return kmalloc (size,GFP_KERNEL | flags); + size =PAGE_ALIGN (size); +if (is_power_of_2 (size)) + ptr = (void *)__get_free_pages (GFP_KERNEL | flags | + __GFP_NOWARN,get_order (size)); +else + ptr =alloc_pages_exact (size,GFP_KERNEL | flags | + __GFP_NOWARN); +if (ptr !=NULL ) { +virt_to_head_page (ptr)->private = size;
mm/util.c int __attribute__((weak)) get_user_pages_fast(unsigned long start, · line 317
Quoted 10 lines
mm/util.c +10
-0+return ptr; + } + + ptr =vmalloc (size); +if (ptr !=NULL && (flags & __GFP_ZERO)) +memset (ptr,0 , size); + +return ptr; +} +EXPORT_SYMBOL (__kvmalloc);
So if I do kvmalloc(size, GFP_ATOMIC) I get GFP_KERNEL|GFP_ATOMIC, which is not a recommended variation because one should not mix __GFP_WAIT and __GFP_HIGH.
So I would simply drop the gfp argument to avoid confusion.
Quoted 19 lines
mm/util.c +19
-0+void __kvfree (void *ptr,bool inatomic) +{ +if (unlikely (ZERO_OR_NULL_PTR (ptr))) +return ; +if (is_vmalloc_addr (ptr)) { +if (inatomic) { +struct work_struct *work; + + work = ptr; +BUILD_BUG_ON (sizeof (struct work_struct ) >PAGE_SIZE ); +INIT_WORK (work, kvfree_work); +schedule_work (work); + }else { +vfree (ptr); + } + }else { +struct page *page; + + page =virt_to_head_page (ptr);
mm/util.c int __attribute__((weak)) get_user_pages_fast(unsigned long start, · line 352
Quoted 10 lines
mm/util.c +10
-0+if (PageSlab (page) ||PageCompound (page)) +kfree (ptr); +else if (is_power_of_2 (page->private)) +free_pages ((unsigned long )ptr, +get_order (page->private)); +else +free_pages_exact (ptr, page->private); + } +} +EXPORT_SYMBOL (__kvfree);
NAK, I really utterly dislike that inatomic argument. The alloc side doesn't function in atomic context either. Please keep the thing symmetric in that regards.
PATCH 1/9 mm: add generic adaptive large memory allocation APIs
In reply to a message not in the archiveinclude/linux/mm.h extern int soft_offline_page(struct page *page, int flags); · line 1484
Quoted 4 lines · Changli Gao
include/linux/mm.h +3
-0+static inline void *kvcalloc (size_t n,size_t size) +{ +return __kvmalloc (n * size, __GFP_ZERO);
This needs multiply overflow checking like kcalloc.
milton
PATCH 1/9 mm: add generic adaptive large memory allocation APIs
In reply to Changli Gaoinclude/linux/mm.h extern int soft_offline_page(struct page *page, int flags); · line 1484
Quoted 4 lines · Changli Gao
include/linux/mm.h +3
-0+static inline void *kvcalloc (size_t n,size_t size) +{ +return __kvmalloc (n * size, __GFP_ZERO);
This needs multiply overflow checking like kcalloc.
milton
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 9 lines · Milton Miller
include/linux/mm.h extern int soft_offline_page(struct page *page, int flags); · line 1484
Quoted 4 lines · Changli Gao
include/linux/mm.h +3
-0+static inline void *kvcalloc (size_t n,size_t size) +{ + return __kvmalloc(n * size, __GFP_ZERO);This needs multiply overflow checking like kcalloc.
Thanks.
Signature · 6 lines
Regards, Changli Gao([email protected]) -- To unsubscribe from this list: send the line "unsubscribe linux-ext4" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
A message that is not in the archive (sent off-list or not yet received): 1273761576_4060-qolCYAvwHvWlVDA81TZ6el6hYfS7NtTn@public.gmane.org
Quoted 9 lines · Milton Miller
include/linux/mm.h extern int soft_offline_page(struct page *page, int flags); · line 1484
Quoted 4 lines · Changli Gao
include/linux/mm.h +3
-0+static inline void *kvcalloc (size_t n,size_t size) +{ + return __kvmalloc(n * size, __GFP_ZERO);This needs multiply overflow checking like kcalloc.
Thanks.
Signature · 6 lines
Regards, Changli Gao([email protected]) _______________________________________________ Containers mailing list [email protected] https://lists.linux-foundation.org/mailman/listinfo/containers