Locking around kmem_cache_destroy


 
Thread Tools Search this Thread
Operating Systems Linux Locking around kmem_cache_destroy
# 1  
Old 03-20-2008
Locking around kmem_cache_destroy

Hi There,

I want to make sure that kmem_cache_alloc is not called concurrently with kmem_cache_destroy on module exit and I want be able to use GFP_KERNEL for the kmem_cache_alloc calls.

Would a read/write lock be good fro this purpose or is there another method I should be using?

Regards,
Brendan
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

Locking Down SFTP

Hi List, I'm sure this has been done before and there must be several ways to skin the cat on this topic. What I am trying to achieve is set up user accounts for use with SFTP based on the standard OpenSSH protocol. And I want to be able to lock them down so that they cannot browse outside of... (2 Replies)
Discussion started by: landossa
2 Replies

2. UNIX for Advanced & Expert Users

File locking

i am working on a device runnin on linux....i have configured the samba server in the device such that my windows PC can access a shared folder in the device. Also in the device i am runnin some programs which access the files in this shared folder. What i want to know is whether there is any way i... (1 Reply)
Discussion started by: abhinx
1 Replies

3. Shell Programming and Scripting

Locking a script

Hello everyone Im sure this has been asked lots of times before, but after endless searching i cant find what i need. How can i make a bash script unreadable to all, including root, but still executable, im not worried about editing it again etc... , is there a way to convert it or something, i... (5 Replies)
Discussion started by: dave123
5 Replies

4. UNIX for Advanced & Expert Users

Locking issue

Hi everyone, I have got a requirement that, i need to check a specific folder,say /test/lock/ for few specific files, say *lock*, whether it has been locked or not.If any one of it is locked than i need to delete that file after a specific time, say after 10 minutes. I never have worked on... (4 Replies)
Discussion started by: Susant
4 Replies

5. Shell Programming and Scripting

locking mechanism

I have a shell script. How can use some kind of locking mechanism to ensure that the script is not being executed by two people at the same time? (3 Replies)
Discussion started by: tjay83
3 Replies

6. UNIX for Dummies Questions & Answers

how to do file locking?

how to ensure all files are being locked while updateing some files? example when i want to update the password and shadow file in unix. how do i implement file locking?? please advice thanks (2 Replies)
Discussion started by: yang
2 Replies

7. UNIX for Dummies Questions & Answers

file locking

how do i implement file locking in unix? example if i want to update a file, i would like to use file locking. how do i implement it? i am using shell script thanks (3 Replies)
Discussion started by: yang
3 Replies

8. UNIX for Dummies Questions & Answers

Keyboard keeps locking up

First off, let me start by saying that I am a total rookie when it comes to Unix so I will do my best to explain the situation. BACKGROUND:We are running AIX and using a third party Inventory Management software called Acclaim. My main interface terminal is just a "dummy" terminal hooked up to... (1 Reply)
Discussion started by: sstaszak11
1 Replies

9. UNIX for Dummies Questions & Answers

Locking in Unix?

Hi all! I want to know about Mapping Filesystems to Multiprocessor Systems.( lock ...) and Traditional UNIX Locking Primitives. Can you help me? (1 Reply)
Discussion started by: nguoidoius
1 Replies

10. UNIX for Advanced & Expert Users

File Locking

Hi, Does anyone know what happens when two processes are simultaneously trying to write to a single file in UNIX (AIX)? Cheers (1 Reply)
Discussion started by: penfold
1 Replies
Login or Register to Ask a Question
kmem_cache_create(9F)					   Kernel Functions for Drivers 				     kmem_cache_create(9F)

NAME
kmem_cache_create, kmem_cache_alloc, kmem_cache_free, kmem_cache_destroy - kernel memory cache allocator operations SYNOPSIS
#include <sys/types.h> #include <sys/kmem.h> kmem_cache_t *kmem_cache_create(char *name, size_t bufsize, size_t align, int (*constructor)(void *, void *, int), void (*destructor)(void *, void *), void (*reclaim)(void *), void *private, void *vmp, int cflags); void kmem_cache_destroy(kmem_cache_t *cp); void *kmem_cache_alloc(kmem_cache_t *cp, int kmflag); void kmem_cache_free(kmem_cache_t *cp, void *obj); [Synopsis for callback functions:] int (*constructor)(void *buf, void *un, int kmflags); void (*destructor)(void *buf, void *un); INTERFACE LEVEL
Solaris DDI specific (Solaris DDI) PARAMETERS
The parameters for the kmem_cache_* functions are as follows: name Descriptive name of a kstat(9S) structure of class kmem_cache. Only alphanumeric characters can be used in name. bufsize Size of the objects it manages. align Required object alignment. constructor Pointer to an object constructor function. Parameters are defined below. destructor Pointer to an object destructor function. Parameters are defined below. reclaim Drivers should pass NULL. private Pass-through argument for constructor/destructor. vmp Drivers should pass NULL. cflags Drivers must pass 0. kmflag Possible flags are: KM_SLEEP Allow sleeping (blocking) until memory is available. KM_NOSLEEP Return NULL immediately if memory is not available. KM_PUSHPAGE Allow the allocation to use reserved memory. *obj Pointer to the object allocated by kmem_cache_alloc(). The parameters for the callback constructor function are as follows: void *buf Pointer to the object to be constructed. void *un The private parameter from the call to kmem_cache_create(); it is typically a pointer to the soft-state structure. int kmflags Propagated kmflag values. The parameters for the callback destructor function are as follows: void *buf Pointer to the object to be deconstructed. void *un The private parameter from the call to kmem_cache_create(); it is typically a pointer to the soft-state structure. DESCRIPTION
In many cases, the cost of initializing and destroying an object exceeds the cost of allocating and freeing memory for it. The functions described here address this condition. Object caching is a technique for dealing with objects that are: o frequently allocated and freed, and o have setup and initialization costs. The idea is to allow the allocator and its clients to cooperate to preserve the invariant portion of an object's initial state, or con- structed state, between uses, so it does not have to be destroyed and re-created every time the object is used. For example, an object con- taining a mutex only needs to have mutex_init() applied once, the first time the object is allocated. The object can then be freed and reallocated many times without incurring the expense of mutex_destroy() and mutex_init() each time. An object's embedded locks, condition variables, reference counts, lists of other objects, and read-only data all generally qualify as constructed state. The essential require- ment is that the client must free the object (using kmem_cache_free()) in its constructed state. The allocator cannot enforce this, so pro- gramming errors will lead to hard-to-find bugs. A driver should call kmem_cache_create() at the time of _init(9E) or attach(9E), and call the corresponding kmem_cache_destroy() at the time of _fini(9E) or detach(9E). kmem_cache_create() creates a cache of objects, each of size size bytes, aligned on an align boundary. Drivers not requiring a specific alignment can pass 0. name identifies the cache for statistics and debugging. constructor and destructor convert plain memory into objects and back again; constructor can fail if it needs to allocate memory but cannot. private is a parameter passed to the constructor and destructor callbacks to support parameterized caches (for example, a pointer to an instance of the driver's soft-state structure). To facilitate debugging, kmem_cache_create() creates a kstat(9S) structure of class kmem_cache and name name. It returns an opaque pointer to the object cache. kmem_cache_alloc() gets an object from the cache. The object will be in its constructed state. kmflag has either KM_SLEEP or KM_NOSLEEP set, indicating whether it is acceptable to wait for memory if none is currently available. A small pool of reserved memory is available to allow the system to progress toward the goal of freeing additional memory while in a low memory situation. The KM_PUSHPAGE flag enables use of this reserved memory pool on an allocation. This flag can be used by drivers that implement strategy(9E) on memory allocations associated with a single I/O operation. The driver guarantees that the I/O operation will com- plete (or timeout) and, on completion, that the memory will be returned. The KM_PUSHPAGE flag should be used only in kmem_cache_alloc() calls. All allocations from a given cache should be consistent in their use of the flag. A driver that adheres to these restrictions can guarantee progress in a low memory situation without resorting to complex private allocation and queuing schemes. If KM_PUSHPAGE is speci- fied, KM_SLEEP can also be used without causing deadlock. kmem_cache_free() returns an object to the cache. The object must be in its constructed state. kmem_cache_destroy() destroys the cache and releases all associated resources. All allocated objects must have been previously freed. CONTEXT
Constructors can be invoked during any call to kmem_cache_alloc(), and will run in that context. Similarly, destructors can be invoked dur- ing any call to kmem_cache_free(), and can also be invoked during kmem_cache_destroy(). Therefore, the functions that a constructor or destructor invokes must be appropriate in that context. kmem_cache_create() and kmem_cache_destroy() must not be called from interrupt context. kmem_cache_alloc() can be called from interrupt context only if the KM_NOSLEEP flag is set. It can be called from user or kernel context with any valid flag. kmem_cache_free() can be called from user, kernel, or interrupt context. EXAMPLES
Example 1: Object Caching Consider the following data structure: struct foo { kmutex_t foo_lock; kcondvar_t foo_cv; struct bar *foo_barlist; int foo_refcnt; }; Assume that a foo structure cannot be freed until there are no outstanding references to it (foo_refcnt == 0) and all of its pending bar events (whatever they are) have completed (foo_barlist == NULL). The life cycle of a dynamically allocated foo would be something like this: foo = kmem_alloc(sizeof (struct foo), KM_SLEEP); mutex_init(&foo->foo_lock, ...); cv_init(&foo->foo_cv, ...); foo->foo_refcnt = 0; foo->foo_barlist = NULL; use foo; ASSERT(foo->foo_barlist == NULL); ASSERT(foo->foo_refcnt == 0); cv_destroy(&foo->foo_cv); mutex_destroy(&foo->foo_lock); kmem_free(foo); Notice that between each use of a foo object we perform a sequence of operations that constitutes nothing more expensive overhead. All of this overhead (that is, everything other than use foo above) can be eliminated by object caching. int foo_constructor(void *buf, void *arg, int tags) { struct foo *foo = buf; mutex_init(&foo->foo_lock, ...); cv_init(&foo->foo_cv, ...); foo->foo_refcnt = 0; foo->foo_barlist = NULL; return(0); } void foo_destructor(void *buf, void *arg) { struct foo *foo = buf; ASSERT(foo->foo_barlist == NULL); ASSERT(foo->foo_refcnt == 0); cv_destroy(&foo->foo_cv); mutex_destroy(&foo->foo_lock); } un = ddi_get_soft_state(foo_softc, instance); (void) snprintf(buf, KSTAT_STRLEN, "foo%d_cache", ddi_get_instance(dip)); foo_cache = kmem_cache_create(buf, sizeof (struct foo), 0, foo_constructor, foo_destructor, NULL, un, 0); To allocate, use, and free a foo object: foo = kmem_cache_alloc(foo_cache, KM_SLEEP); use foo; kmem_cache_free(foo_cache, foo); This makes foo allocation fast, because the allocator will usually do nothing more than fetch an already-constructed foo from the cache. foo_constructor and foo_destructor will be invoked only to populate and drain the cache, respectively. RETURN VALUES
If successful, the constructor function must return 0. If KM_NOSLEEP is set and memory cannot be allocated without sleeping, the construc- tor must return -1. kmem_cache_create() returns a pointer to the allocated cache. If the name parameter contains non-alphanumeric characters, kmem_cache_cre- ate() returns NULL. If successful, kmem_cache_alloc() returns a pointer to the allocated object. If KM_NOSLEEP is set and memory cannot be allocated without sleeping, kmem_cache_alloc() returns NULL. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |Evolving | +-----------------------------+-----------------------------+ SEE ALSO
condvar(9F), kmem_alloc(9F), mutex(9F), kstat(9S) Writing Device Drivers The Slab Allocator: An Object-Caching Kernel Memory Allocator, Bonwick, J.; USENIX Summer 1994 Technical Conference(1994). SunOS 5.10 14 Jan 2003 kmem_cache_create(9F)