From: Peter Zijlstra on
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.

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.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo(a)vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
From: Peter Zijlstra on
On Thu, 2010-05-13 at 22:08 +0800, Changli Gao wrote:
> > 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 from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo(a)vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/