* [RFC] kmem_cache_zalloc
@ 2002-03-27 19:39 Eric Sandeen
2002-03-27 19:47 ` Andrew Morton
` (2 more replies)
0 siblings, 3 replies; 11+ messages in thread
From: Eric Sandeen @ 2002-03-27 19:39 UTC (permalink / raw)
To: linux-kernel
In the interest of whittling down the changes that XFS makes to the core
kernel, I thought I'd start throwing out some the easier self-contained
modifications for discussion.
XFS adds a kmem_cache_zalloc function to mm/slab.c, it does what you
might expect: kmem_cache_alloc + memset
Is this something that might be considered for inclusion in the core
kernel, or should we roll it back into fs/xfs?
Thanks,
-Eric
--- 18.1/mm/slab.c Fri, 07 Dec 2001 09:35:49 +1100 kaos (linux-2.4/j/5_slab.c 1.2.1.2.1.2.1.3.1.3 644)
+++ 18.11/mm/slab.c Mon, 07 Jan 2002 13:27:25 +1100 kaos (linux-2.4/j/5_slab.c 1.2.1.2.1.7 644)
@@ -1567,6 +1567,23 @@ void kmem_cache_free (kmem_cache_t *cach
local_irq_restore(flags);
}
+void *
+kmem_cache_zalloc(kmem_cache_t *cachep, int flags)
+{
+ void *ptr;
+ ptr = __kmem_cache_alloc(cachep, flags);
+ if (ptr)
+#if DEBUG
+ memset(ptr, 0, cachep->objsize -
+ (cachep->flags & SLAB_RED_ZONE ? 2*BYTES_PER_WORD : 0));
+#else
+ memset(ptr, 0, cachep->objsize);
+#endif
+
+ return ptr;
+}
+
+
--
Eric Sandeen XFS for Linux http://oss.sgi.com/projects/xfs
sandeen@sgi.com SGI, Inc.
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [RFC] kmem_cache_zalloc
2002-03-27 19:39 Eric Sandeen
@ 2002-03-27 19:47 ` Andrew Morton
2002-03-27 19:52 ` Tigran Aivazian
2002-03-27 20:19 ` Christoph Hellwig
2 siblings, 0 replies; 11+ messages in thread
From: Andrew Morton @ 2002-03-27 19:47 UTC (permalink / raw)
To: Eric Sandeen; +Cc: linux-kernel
Eric Sandeen wrote:
>
> In the interest of whittling down the changes that XFS makes to the core
> kernel, I thought I'd start throwing out some the easier self-contained
> modifications for discussion.
>
> XFS adds a kmem_cache_zalloc function to mm/slab.c, it does what you
> might expect: kmem_cache_alloc + memset
>
> Is this something that might be considered for inclusion in the core
> kernel, or should we roll it back into fs/xfs?
Count me in. Clearly a very sane thing to do.
Needs an EXPORT_SYMBOL though.
-
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [RFC] kmem_cache_zalloc
2002-03-27 19:39 Eric Sandeen
2002-03-27 19:47 ` Andrew Morton
@ 2002-03-27 19:52 ` Tigran Aivazian
2002-03-27 20:17 ` Alan Cox
2002-03-27 20:19 ` Christoph Hellwig
2 siblings, 1 reply; 11+ messages in thread
From: Tigran Aivazian @ 2002-03-27 19:52 UTC (permalink / raw)
To: Eric Sandeen; +Cc: linux-kernel
On 27 Mar 2002, Eric Sandeen wrote:
> XFS adds a kmem_cache_zalloc function to mm/slab.c, it does what you
> might expect: kmem_cache_alloc + memset
Useful. However, when I suggested, similarly, a kzalloc() to match
kmalloc() people loudly objected that "it is non-standard" (as if kmalloc
was "standard" in some sense :)
I wonder if they (I can't remember who it was) will say
"kmem_cache_zalloc is a non-standard name"...
Regards,
Tigran
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [RFC] kmem_cache_zalloc
2002-03-27 19:52 ` Tigran Aivazian
@ 2002-03-27 20:17 ` Alan Cox
2002-03-29 22:17 ` Pavel Machek
0 siblings, 1 reply; 11+ messages in thread
From: Alan Cox @ 2002-03-27 20:17 UTC (permalink / raw)
To: Tigran Aivazian; +Cc: Eric Sandeen, linux-kernel
> was "standard" in some sense :)
> I wonder if they (I can't remember who it was) will say
> "kmem_cache_zalloc is a non-standard name"...
Much more useful would be kcalloc(). That way we can put all the missing
32bit overflow checking into kcalloc and remove a lot of crud from the
kernel where we have to keep doing maths checks.
Alan
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [RFC] kmem_cache_zalloc
2002-03-27 19:39 Eric Sandeen
2002-03-27 19:47 ` Andrew Morton
2002-03-27 19:52 ` Tigran Aivazian
@ 2002-03-27 20:19 ` Christoph Hellwig
2002-03-27 20:23 ` Jeff Garzik
2 siblings, 1 reply; 11+ messages in thread
From: Christoph Hellwig @ 2002-03-27 20:19 UTC (permalink / raw)
To: Eric Sandeen; +Cc: linux-kernel
On Wed, Mar 27, 2002 at 01:39:17PM -0600, Eric Sandeen wrote:
> In the interest of whittling down the changes that XFS makes to the core
> kernel, I thought I'd start throwing out some the easier self-contained
> modifications for discussion.
>
> XFS adds a kmem_cache_zalloc function to mm/slab.c, it does what you
> might expect: kmem_cache_alloc + memset
I'd really go for k(mem_)zalloc, but a kmem_cache_alloc leads people toward
writing bad code. The purpose of the slab allocator is to allow caching
readily constructed objects, a _zalloc destroys them on alloc.
I think code using kmem_cache_alloc really wants to use kzalloc and instead
of maintaining it's pool.
Christoph
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [RFC] kmem_cache_zalloc
2002-03-27 20:19 ` Christoph Hellwig
@ 2002-03-27 20:23 ` Jeff Garzik
0 siblings, 0 replies; 11+ messages in thread
From: Jeff Garzik @ 2002-03-27 20:23 UTC (permalink / raw)
To: linux-kernel
...and of course, there are proposals popping up from time to time, that
talk about doing background zeroing of pages.
Jeff
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [RFC] kmem_cache_zalloc
@ 2002-03-28 11:21 Dipankar Sarma
2002-03-28 11:25 ` David Woodhouse
0 siblings, 1 reply; 11+ messages in thread
From: Dipankar Sarma @ 2002-03-28 11:21 UTC (permalink / raw)
To: hch; +Cc: linux-kernel
In article <20020327201917.A23810@phoenix.infradead.org> Christoph Hellwig wrote:
> I'd really go for k(mem_)zalloc, but a kmem_cache_alloc leads people toward
> writing bad code. The purpose of the slab allocator is to allow caching
> readily constructed objects, a _zalloc destroys them on alloc.
I thought that the life span of an object is between
kmem_cache_alloc and kmem_cache_free. If you are expecting caching
beyond this, you may not get correct data. kmem_cache allocator
is supposed to quickly allocate fixed size structures avoiding
the need for frequent splitting and coalescing in the allocator.
Am I missing something here ?
Thanks
--
Dipankar Sarma <dipankar@in.ibm.com> http://lse.sourceforge.net
Linux Technology Center, IBM Software Lab, Bangalore, India.
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [RFC] kmem_cache_zalloc
2002-03-28 11:21 [RFC] kmem_cache_zalloc Dipankar Sarma
@ 2002-03-28 11:25 ` David Woodhouse
2002-03-28 11:40 ` Dipankar Sarma
0 siblings, 1 reply; 11+ messages in thread
From: David Woodhouse @ 2002-03-28 11:25 UTC (permalink / raw)
To: dipankar; +Cc: hch, linux-kernel
dipankar@in.ibm.com said:
> I thought that the life span of an object is between
> kmem_cache_alloc and kmem_cache_free. If you are expecting caching
> beyond this, you may not get correct data. kmem_cache allocator is
> supposed to quickly allocate fixed size structures avoiding the need
> for frequent splitting and coalescing in the allocator.
> Am I missing something here ?
Yes. Slab objects can be initialised once when a new page is added to the
slab, and returned to the slab in reusable form so that you don't have the
cost of complete initialisation on each allocation.
So if for example you have a semaphore in your slab object, instead of
initialising it on each kmem_cache_alloc() you do it once when the new pages
are added to the slab. Then you just make sure it's unlocked each time you
free a slab object.
--
dwmw2
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [RFC] kmem_cache_zalloc
2002-03-28 11:25 ` David Woodhouse
@ 2002-03-28 11:40 ` Dipankar Sarma
0 siblings, 0 replies; 11+ messages in thread
From: Dipankar Sarma @ 2002-03-28 11:40 UTC (permalink / raw)
To: David Woodhouse; +Cc: hch, linux-kernel
On Thu, Mar 28, 2002 at 11:25:12AM +0000, David Woodhouse wrote:
>
>
> dipankar@in.ibm.com said:
> > I thought that the life span of an object is between
> > kmem_cache_alloc and kmem_cache_free. If you are expecting caching
> > beyond this, you may not get correct data. kmem_cache allocator is
> > supposed to quickly allocate fixed size structures avoiding the need
> > for frequent splitting and coalescing in the allocator.
>
> > Am I missing something here ?
>
> Yes. Slab objects can be initialised once when a new page is added to the
> slab, and returned to the slab in reusable form so that you don't have the
> cost of complete initialisation on each allocation.
>
> So if for example you have a semaphore in your slab object, instead of
> initialising it on each kmem_cache_alloc() you do it once when the new pages
> are added to the slab. Then you just make sure it's unlocked each time you
> free a slab object.
Ok. That makes clear why hch thought kmem_cache_alloc() can lead
to people writing bad code.
Thanks
--
Dipankar Sarma <dipankar@in.ibm.com> http://lse.sourceforge.net
Linux Technology Center, IBM Software Lab, Bangalore, India.
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [RFC] kmem_cache_zalloc
2002-03-27 20:17 ` Alan Cox
@ 2002-03-29 22:17 ` Pavel Machek
2002-03-29 23:21 ` Alan Cox
0 siblings, 1 reply; 11+ messages in thread
From: Pavel Machek @ 2002-03-29 22:17 UTC (permalink / raw)
To: Alan Cox; +Cc: Tigran Aivazian, Eric Sandeen, linux-kernel
Hi!
> > was "standard" in some sense :)
> > I wonder if they (I can't remember who it was) will say
> > "kmem_cache_zalloc is a non-standard name"...
>
> Much more useful would be kcalloc(). That way we can put all the missing
> 32bit overflow checking into kcalloc and remove a lot of crud from the
> kernel where we have to keep doing maths checks.
What should kcalloc do?
Pavel
--
(about SSSCA) "I don't say this lightly. However, I really think that the U.S.
no longer is classifiable as a democracy, but rather as a plutocracy." --hpa
^ permalink raw reply [flat|nested] 11+ messages in thread
* Re: [RFC] kmem_cache_zalloc
2002-03-29 22:17 ` Pavel Machek
@ 2002-03-29 23:21 ` Alan Cox
0 siblings, 0 replies; 11+ messages in thread
From: Alan Cox @ 2002-03-29 23:21 UTC (permalink / raw)
To: Pavel Machek; +Cc: Alan Cox, Tigran Aivazian, Eric Sandeen, linux-kernel
> > Much more useful would be kcalloc(). That way we can put all the missing
> > 32bit overflow checking into kcalloc and remove a lot of crud from the
> > kernel where we have to keep doing maths checks.
>
> What should kcalloc do?
The same as calloc in standard C, but also make sure it does overflow checking.
^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2002-03-29 23:05 UTC | newest]
Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-03-28 11:21 [RFC] kmem_cache_zalloc Dipankar Sarma
2002-03-28 11:25 ` David Woodhouse
2002-03-28 11:40 ` Dipankar Sarma
-- strict thread matches above, loose matches on Subject: below --
2002-03-27 19:39 Eric Sandeen
2002-03-27 19:47 ` Andrew Morton
2002-03-27 19:52 ` Tigran Aivazian
2002-03-27 20:17 ` Alan Cox
2002-03-29 22:17 ` Pavel Machek
2002-03-29 23:21 ` Alan Cox
2002-03-27 20:19 ` Christoph Hellwig
2002-03-27 20:23 ` Jeff Garzik
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox