Re: [PATCH 1/9] mm: add generic adaptive large memory allocation APIs

From: Changli Gao
Date: Thu May 13 2010 - 10:09:07 EST


On Thu, May 13, 2010 at 9:20 PM, Peter Zijlstra <peterz@xxxxxxxxxxxxx> wrote:
> On Thu, 2010-05-13 at 17:51 +0800, Changli Gao wrote:
>> +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);
>> +
>> + Â Â Â 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.

__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);

>
> So I would simply drop the gfp argument to avoid confusion.
>
>> +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
>> + Â Â Â Â Â Â Â Â Â Â Â 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.
>

There are some users, who release memory in atomic context. for
example: fs/file.c: fdmem.

--
Regardsï
Changli Gao(xiaosuo@xxxxxxxxx)
¢éì®&Þ~º&¶¬–+-±éÝ¥Šw®žË±Êâmébžìdz¹Þ)í…æèw*jg¬±¨¶‰šŽŠÝj/êäz¹ÞŠà2ŠÞ¨è­Ú&¢)ß«a¶Úþø®G«éh®æj:+v‰¨Šwè†Ù>Wš±êÞiÛaxPjØm¶Ÿÿà -»+ƒùdš_