From mboxrd@z Thu Jan 1 00:00:00 1970 From: Changli Gao Subject: Re: [PATCH] fs: use kmalloc() to allocate fdmem if possible Date: Tue, 4 May 2010 12:20:43 +0800 Message-ID: References: <1272818776-7729-1-git-send-email-xiaosuo@gmail.com> <4BDDB6E9.4060703@redhat.com> <201005030915.FCD09385.FFHVOMJtSLOFQO@I-love.SAKURA.ne.jp> <4BDE67DA.4080004@redhat.com> <4BDE7E66.8030509@redhat.com> <4BDE8E24.5050200@redhat.com> <1272878032.2226.27.camel@edumazet-laptop> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: QUOTED-PRINTABLE Cc: Avi Kivity , Tetsuo Handa , jslaby@suse.cz, akpm@linux-foundation.org, paulmck@linux.vnet.ibm.com, adobriyan@gmail.com, mingo@elte.hu, peterz@infradead.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org To: Eric Dumazet Return-path: In-Reply-To: <1272878032.2226.27.camel@edumazet-laptop> Sender: linux-kernel-owner@vger.kernel.org List-Id: linux-fsdevel.vger.kernel.org On Mon, May 3, 2010 at 5:13 PM, Eric Dumazet w= rote: > > /* > =A0* Warning: if size is not a power of two, kmalloc() might > =A0* waste memory because of its requirements. > =A0*/ > void *kvmalloc(size_t size) > { > =A0 =A0 =A0 =A0void *ptr =3D kmalloc(size, GFP_KERNEL | __GFP_NOWARN)= ; > > =A0 =A0 =A0 =A0if (ptr) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0return ptr; > =A0 =A0 =A0 =A0return vmalloc(size); > } we can use alloc_pages_exact()/free_pages_exact() when size is larger than PAGE_SIZE, then size isn't needed to be a power of two. void *kvmalloc(size_t size) { void *ptr; if (size < PAGE_SIZE) return kmalloc(size, GFP_KERNEL); ptr =3D alloc_pages_exact(size, GFP_KERNEL | __GFP_NOWARN); if (ptr) return ptr; return vmalloc(size); } void *kvfree(void *ptr, size_t size) { BUG_ON(in_interrupt()); if (size < PAGE_SIZE) kfree(ptr); else if (is_vmalloc_addr(ptr)) vfree(ptr); else free_pages_exact(ptr, size); } > > void kvfree(void *ptr) > { > =A0 =A0 =A0 =A0BUG_ON(in_interrupt()); > =A0 =A0 =A0 =A0if (is_vmalloc_addr(ptr)) > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0vfree(ptr); > =A0 =A0 =A0 =A0else > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0kfree(ptr); > } >