xref: /titanic_53/usr/src/uts/common/os/kmem.c (revision 47bb26641401860744e5251a300d7ce94d5aea47)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57d692464Sdp201428  * Common Development and Distribution License (the "License").
67d692464Sdp201428  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22b942e89bSDavid Valin  * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
23*47bb2664SMatthew Ahrens  * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
240c833d64SJosef 'Jeff' Sipek  * Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
28b5fca8f8Stomee  * Kernel memory allocator, as described in the following two papers and a
29b5fca8f8Stomee  * statement about the consolidator:
307c478bd9Sstevel@tonic-gate  *
317c478bd9Sstevel@tonic-gate  * Jeff Bonwick,
327c478bd9Sstevel@tonic-gate  * The Slab Allocator: An Object-Caching Kernel Memory Allocator.
337c478bd9Sstevel@tonic-gate  * Proceedings of the Summer 1994 Usenix Conference.
347c478bd9Sstevel@tonic-gate  * Available as /shared/sac/PSARC/1994/028/materials/kmem.pdf.
357c478bd9Sstevel@tonic-gate  *
367c478bd9Sstevel@tonic-gate  * Jeff Bonwick and Jonathan Adams,
377c478bd9Sstevel@tonic-gate  * Magazines and vmem: Extending the Slab Allocator to Many CPUs and
387c478bd9Sstevel@tonic-gate  * Arbitrary Resources.
397c478bd9Sstevel@tonic-gate  * Proceedings of the 2001 Usenix Conference.
407c478bd9Sstevel@tonic-gate  * Available as /shared/sac/PSARC/2000/550/materials/vmem.pdf.
41b5fca8f8Stomee  *
42b5fca8f8Stomee  * kmem Slab Consolidator Big Theory Statement:
43b5fca8f8Stomee  *
44b5fca8f8Stomee  * 1. Motivation
45b5fca8f8Stomee  *
46b5fca8f8Stomee  * As stated in Bonwick94, slabs provide the following advantages over other
47b5fca8f8Stomee  * allocation structures in terms of memory fragmentation:
48b5fca8f8Stomee  *
49b5fca8f8Stomee  *  - Internal fragmentation (per-buffer wasted space) is minimal.
50b5fca8f8Stomee  *  - Severe external fragmentation (unused buffers on the free list) is
51b5fca8f8Stomee  *    unlikely.
52b5fca8f8Stomee  *
53b5fca8f8Stomee  * Segregating objects by size eliminates one source of external fragmentation,
54b5fca8f8Stomee  * and according to Bonwick:
55b5fca8f8Stomee  *
56b5fca8f8Stomee  *   The other reason that slabs reduce external fragmentation is that all
57b5fca8f8Stomee  *   objects in a slab are of the same type, so they have the same lifetime
58b5fca8f8Stomee  *   distribution. The resulting segregation of short-lived and long-lived
59b5fca8f8Stomee  *   objects at slab granularity reduces the likelihood of an entire page being
60b5fca8f8Stomee  *   held hostage due to a single long-lived allocation [Barrett93, Hanson90].
61b5fca8f8Stomee  *
62b5fca8f8Stomee  * While unlikely, severe external fragmentation remains possible. Clients that
63b5fca8f8Stomee  * allocate both short- and long-lived objects from the same cache cannot
64b5fca8f8Stomee  * anticipate the distribution of long-lived objects within the allocator's slab
65b5fca8f8Stomee  * implementation. Even a small percentage of long-lived objects distributed
66b5fca8f8Stomee  * randomly across many slabs can lead to a worst case scenario where the client
67b5fca8f8Stomee  * frees the majority of its objects and the system gets back almost none of the
68b5fca8f8Stomee  * slabs. Despite the client doing what it reasonably can to help the system
69b5fca8f8Stomee  * reclaim memory, the allocator cannot shake free enough slabs because of
70b5fca8f8Stomee  * lonely allocations stubbornly hanging on. Although the allocator is in a
71b5fca8f8Stomee  * position to diagnose the fragmentation, there is nothing that the allocator
72b5fca8f8Stomee  * by itself can do about it. It only takes a single allocated object to prevent
73b5fca8f8Stomee  * an entire slab from being reclaimed, and any object handed out by
74b5fca8f8Stomee  * kmem_cache_alloc() is by definition in the client's control. Conversely,
75b5fca8f8Stomee  * although the client is in a position to move a long-lived object, it has no
76b5fca8f8Stomee  * way of knowing if the object is causing fragmentation, and if so, where to
77b5fca8f8Stomee  * move it. A solution necessarily requires further cooperation between the
78b5fca8f8Stomee  * allocator and the client.
79b5fca8f8Stomee  *
80b5fca8f8Stomee  * 2. Move Callback
81b5fca8f8Stomee  *
82b5fca8f8Stomee  * The kmem slab consolidator therefore adds a move callback to the
83b5fca8f8Stomee  * allocator/client interface, improving worst-case external fragmentation in
84b5fca8f8Stomee  * kmem caches that supply a function to move objects from one memory location
85b5fca8f8Stomee  * to another. In a situation of low memory kmem attempts to consolidate all of
86b5fca8f8Stomee  * a cache's slabs at once; otherwise it works slowly to bring external
87b5fca8f8Stomee  * fragmentation within the 1/8 limit guaranteed for internal fragmentation,
88b5fca8f8Stomee  * thereby helping to avoid a low memory situation in the future.
89b5fca8f8Stomee  *
90b5fca8f8Stomee  * The callback has the following signature:
91b5fca8f8Stomee  *
92b5fca8f8Stomee  *   kmem_cbrc_t move(void *old, void *new, size_t size, void *user_arg)
93b5fca8f8Stomee  *
94b5fca8f8Stomee  * It supplies the kmem client with two addresses: the allocated object that
95b5fca8f8Stomee  * kmem wants to move and a buffer selected by kmem for the client to use as the
96b5fca8f8Stomee  * copy destination. The callback is kmem's way of saying "Please get off of
97b5fca8f8Stomee  * this buffer and use this one instead." kmem knows where it wants to move the
98b5fca8f8Stomee  * object in order to best reduce fragmentation. All the client needs to know
99b5fca8f8Stomee  * about the second argument (void *new) is that it is an allocated, constructed
100b5fca8f8Stomee  * object ready to take the contents of the old object. When the move function
101b5fca8f8Stomee  * is called, the system is likely to be low on memory, and the new object
102b5fca8f8Stomee  * spares the client from having to worry about allocating memory for the
103b5fca8f8Stomee  * requested move. The third argument supplies the size of the object, in case a
104b5fca8f8Stomee  * single move function handles multiple caches whose objects differ only in
105b5fca8f8Stomee  * size (such as zio_buf_512, zio_buf_1024, etc). Finally, the same optional
106b5fca8f8Stomee  * user argument passed to the constructor, destructor, and reclaim functions is
107b5fca8f8Stomee  * also passed to the move callback.
108b5fca8f8Stomee  *
109b5fca8f8Stomee  * 2.1 Setting the Move Callback
110b5fca8f8Stomee  *
111b5fca8f8Stomee  * The client sets the move callback after creating the cache and before
112b5fca8f8Stomee  * allocating from it:
113b5fca8f8Stomee  *
114b5fca8f8Stomee  *	object_cache = kmem_cache_create(...);
115b5fca8f8Stomee  *      kmem_cache_set_move(object_cache, object_move);
116b5fca8f8Stomee  *
117b5fca8f8Stomee  * 2.2 Move Callback Return Values
118b5fca8f8Stomee  *
119b5fca8f8Stomee  * Only the client knows about its own data and when is a good time to move it.
120b5fca8f8Stomee  * The client is cooperating with kmem to return unused memory to the system,
121b5fca8f8Stomee  * and kmem respectfully accepts this help at the client's convenience. When
122b5fca8f8Stomee  * asked to move an object, the client can respond with any of the following:
123b5fca8f8Stomee  *
124b5fca8f8Stomee  *   typedef enum kmem_cbrc {
125b5fca8f8Stomee  *           KMEM_CBRC_YES,
126b5fca8f8Stomee  *           KMEM_CBRC_NO,
127b5fca8f8Stomee  *           KMEM_CBRC_LATER,
128b5fca8f8Stomee  *           KMEM_CBRC_DONT_NEED,
129b5fca8f8Stomee  *           KMEM_CBRC_DONT_KNOW
130b5fca8f8Stomee  *   } kmem_cbrc_t;
131b5fca8f8Stomee  *
132b5fca8f8Stomee  * The client must not explicitly kmem_cache_free() either of the objects passed
133b5fca8f8Stomee  * to the callback, since kmem wants to free them directly to the slab layer
134b5fca8f8Stomee  * (bypassing the per-CPU magazine layer). The response tells kmem which of the
135b5fca8f8Stomee  * objects to free:
136b5fca8f8Stomee  *
137b5fca8f8Stomee  *       YES: (Did it) The client moved the object, so kmem frees the old one.
138b5fca8f8Stomee  *        NO: (Never) The client refused, so kmem frees the new object (the
139b5fca8f8Stomee  *            unused copy destination). kmem also marks the slab of the old
140b5fca8f8Stomee  *            object so as not to bother the client with further callbacks for
141b5fca8f8Stomee  *            that object as long as the slab remains on the partial slab list.
142b5fca8f8Stomee  *            (The system won't be getting the slab back as long as the
143b5fca8f8Stomee  *            immovable object holds it hostage, so there's no point in moving
144b5fca8f8Stomee  *            any of its objects.)
145b5fca8f8Stomee  *     LATER: The client is using the object and cannot move it now, so kmem
146b5fca8f8Stomee  *            frees the new object (the unused copy destination). kmem still
147b5fca8f8Stomee  *            attempts to move other objects off the slab, since it expects to
148b5fca8f8Stomee  *            succeed in clearing the slab in a later callback. The client
149b5fca8f8Stomee  *            should use LATER instead of NO if the object is likely to become
150b5fca8f8Stomee  *            movable very soon.
151b5fca8f8Stomee  * DONT_NEED: The client no longer needs the object, so kmem frees the old along
152b5fca8f8Stomee  *            with the new object (the unused copy destination). This response
153b5fca8f8Stomee  *            is the client's opportunity to be a model citizen and give back as
154b5fca8f8Stomee  *            much as it can.
155b5fca8f8Stomee  * DONT_KNOW: The client does not know about the object because
156b5fca8f8Stomee  *            a) the client has just allocated the object and not yet put it
157b5fca8f8Stomee  *               wherever it expects to find known objects
158b5fca8f8Stomee  *            b) the client has removed the object from wherever it expects to
159b5fca8f8Stomee  *               find known objects and is about to free it, or
160b5fca8f8Stomee  *            c) the client has freed the object.
161b5fca8f8Stomee  *            In all these cases (a, b, and c) kmem frees the new object (the
162b5fca8f8Stomee  *            unused copy destination) and searches for the old object in the
163b5fca8f8Stomee  *            magazine layer. If found, the object is removed from the magazine
164b5fca8f8Stomee  *            layer and freed to the slab layer so it will no longer hold the
165b5fca8f8Stomee  *            slab hostage.
166b5fca8f8Stomee  *
167b5fca8f8Stomee  * 2.3 Object States
168b5fca8f8Stomee  *
169b5fca8f8Stomee  * Neither kmem nor the client can be assumed to know the object's whereabouts
170b5fca8f8Stomee  * at the time of the callback. An object belonging to a kmem cache may be in
171b5fca8f8Stomee  * any of the following states:
172b5fca8f8Stomee  *
173b5fca8f8Stomee  * 1. Uninitialized on the slab
174b5fca8f8Stomee  * 2. Allocated from the slab but not constructed (still uninitialized)
175b5fca8f8Stomee  * 3. Allocated from the slab, constructed, but not yet ready for business
176b5fca8f8Stomee  *    (not in a valid state for the move callback)
177b5fca8f8Stomee  * 4. In use (valid and known to the client)
178b5fca8f8Stomee  * 5. About to be freed (no longer in a valid state for the move callback)
179b5fca8f8Stomee  * 6. Freed to a magazine (still constructed)
180b5fca8f8Stomee  * 7. Allocated from a magazine, not yet ready for business (not in a valid
181b5fca8f8Stomee  *    state for the move callback), and about to return to state #4
182b5fca8f8Stomee  * 8. Deconstructed on a magazine that is about to be freed
183b5fca8f8Stomee  * 9. Freed to the slab
184b5fca8f8Stomee  *
185b5fca8f8Stomee  * Since the move callback may be called at any time while the object is in any
186b5fca8f8Stomee  * of the above states (except state #1), the client needs a safe way to
187b5fca8f8Stomee  * determine whether or not it knows about the object. Specifically, the client
188b5fca8f8Stomee  * needs to know whether or not the object is in state #4, the only state in
189b5fca8f8Stomee  * which a move is valid. If the object is in any other state, the client should
190b5fca8f8Stomee  * immediately return KMEM_CBRC_DONT_KNOW, since it is unsafe to access any of
191b5fca8f8Stomee  * the object's fields.
192b5fca8f8Stomee  *
193b5fca8f8Stomee  * Note that although an object may be in state #4 when kmem initiates the move
194b5fca8f8Stomee  * request, the object may no longer be in that state by the time kmem actually
195b5fca8f8Stomee  * calls the move function. Not only does the client free objects
196b5fca8f8Stomee  * asynchronously, kmem itself puts move requests on a queue where thay are
197b5fca8f8Stomee  * pending until kmem processes them from another context. Also, objects freed
198b5fca8f8Stomee  * to a magazine appear allocated from the point of view of the slab layer, so
199b5fca8f8Stomee  * kmem may even initiate requests for objects in a state other than state #4.
200b5fca8f8Stomee  *
201b5fca8f8Stomee  * 2.3.1 Magazine Layer
202b5fca8f8Stomee  *
203b5fca8f8Stomee  * An important insight revealed by the states listed above is that the magazine
204b5fca8f8Stomee  * layer is populated only by kmem_cache_free(). Magazines of constructed
205b5fca8f8Stomee  * objects are never populated directly from the slab layer (which contains raw,
206b5fca8f8Stomee  * unconstructed objects). Whenever an allocation request cannot be satisfied
207b5fca8f8Stomee  * from the magazine layer, the magazines are bypassed and the request is
208b5fca8f8Stomee  * satisfied from the slab layer (creating a new slab if necessary). kmem calls
209b5fca8f8Stomee  * the object constructor only when allocating from the slab layer, and only in
210b5fca8f8Stomee  * response to kmem_cache_alloc() or to prepare the destination buffer passed in
211b5fca8f8Stomee  * the move callback. kmem does not preconstruct objects in anticipation of
212b5fca8f8Stomee  * kmem_cache_alloc().
213b5fca8f8Stomee  *
214b5fca8f8Stomee  * 2.3.2 Object Constructor and Destructor
215b5fca8f8Stomee  *
216b5fca8f8Stomee  * If the client supplies a destructor, it must be valid to call the destructor
217b5fca8f8Stomee  * on a newly created object (immediately after the constructor).
218b5fca8f8Stomee  *
219b5fca8f8Stomee  * 2.4 Recognizing Known Objects
220b5fca8f8Stomee  *
221b5fca8f8Stomee  * There is a simple test to determine safely whether or not the client knows
222b5fca8f8Stomee  * about a given object in the move callback. It relies on the fact that kmem
223b5fca8f8Stomee  * guarantees that the object of the move callback has only been touched by the
224b5fca8f8Stomee  * client itself or else by kmem. kmem does this by ensuring that none of the
225b5fca8f8Stomee  * cache's slabs are freed to the virtual memory (VM) subsystem while a move
226b5fca8f8Stomee  * callback is pending. When the last object on a slab is freed, if there is a
227b5fca8f8Stomee  * pending move, kmem puts the slab on a per-cache dead list and defers freeing
228b5fca8f8Stomee  * slabs on that list until all pending callbacks are completed. That way,
229b5fca8f8Stomee  * clients can be certain that the object of a move callback is in one of the
230b5fca8f8Stomee  * states listed above, making it possible to distinguish known objects (in
231b5fca8f8Stomee  * state #4) using the two low order bits of any pointer member (with the
232b5fca8f8Stomee  * exception of 'char *' or 'short *' which may not be 4-byte aligned on some
233b5fca8f8Stomee  * platforms).
234b5fca8f8Stomee  *
235b5fca8f8Stomee  * The test works as long as the client always transitions objects from state #4
236b5fca8f8Stomee  * (known, in use) to state #5 (about to be freed, invalid) by setting the low
237b5fca8f8Stomee  * order bit of the client-designated pointer member. Since kmem only writes
238b5fca8f8Stomee  * invalid memory patterns, such as 0xbaddcafe to uninitialized memory and
239b5fca8f8Stomee  * 0xdeadbeef to freed memory, any scribbling on the object done by kmem is
240b5fca8f8Stomee  * guaranteed to set at least one of the two low order bits. Therefore, given an
241b5fca8f8Stomee  * object with a back pointer to a 'container_t *o_container', the client can
242b5fca8f8Stomee  * test
243b5fca8f8Stomee  *
244b5fca8f8Stomee  *      container_t *container = object->o_container;
245b5fca8f8Stomee  *      if ((uintptr_t)container & 0x3) {
246b5fca8f8Stomee  *              return (KMEM_CBRC_DONT_KNOW);
247b5fca8f8Stomee  *      }
248b5fca8f8Stomee  *
249b5fca8f8Stomee  * Typically, an object will have a pointer to some structure with a list or
250b5fca8f8Stomee  * hash where objects from the cache are kept while in use. Assuming that the
251b5fca8f8Stomee  * client has some way of knowing that the container structure is valid and will
252b5fca8f8Stomee  * not go away during the move, and assuming that the structure includes a lock
253b5fca8f8Stomee  * to protect whatever collection is used, then the client would continue as
254b5fca8f8Stomee  * follows:
255b5fca8f8Stomee  *
256b5fca8f8Stomee  *	// Ensure that the container structure does not go away.
257b5fca8f8Stomee  *      if (container_hold(container) == 0) {
258b5fca8f8Stomee  *              return (KMEM_CBRC_DONT_KNOW);
259b5fca8f8Stomee  *      }
260b5fca8f8Stomee  *      mutex_enter(&container->c_objects_lock);
261b5fca8f8Stomee  *      if (container != object->o_container) {
262b5fca8f8Stomee  *              mutex_exit(&container->c_objects_lock);
263b5fca8f8Stomee  *              container_rele(container);
264b5fca8f8Stomee  *              return (KMEM_CBRC_DONT_KNOW);
265b5fca8f8Stomee  *      }
266b5fca8f8Stomee  *
267b5fca8f8Stomee  * At this point the client knows that the object cannot be freed as long as
268b5fca8f8Stomee  * c_objects_lock is held. Note that after acquiring the lock, the client must
269b5fca8f8Stomee  * recheck the o_container pointer in case the object was removed just before
270b5fca8f8Stomee  * acquiring the lock.
271b5fca8f8Stomee  *
272b5fca8f8Stomee  * When the client is about to free an object, it must first remove that object
273b5fca8f8Stomee  * from the list, hash, or other structure where it is kept. At that time, to
274b5fca8f8Stomee  * mark the object so it can be distinguished from the remaining, known objects,
275b5fca8f8Stomee  * the client sets the designated low order bit:
276b5fca8f8Stomee  *
277b5fca8f8Stomee  *      mutex_enter(&container->c_objects_lock);
278b5fca8f8Stomee  *      object->o_container = (void *)((uintptr_t)object->o_container | 0x1);
279b5fca8f8Stomee  *      list_remove(&container->c_objects, object);
280b5fca8f8Stomee  *      mutex_exit(&container->c_objects_lock);
281b5fca8f8Stomee  *
282b5fca8f8Stomee  * In the common case, the object is freed to the magazine layer, where it may
283b5fca8f8Stomee  * be reused on a subsequent allocation without the overhead of calling the
284b5fca8f8Stomee  * constructor. While in the magazine it appears allocated from the point of
285b5fca8f8Stomee  * view of the slab layer, making it a candidate for the move callback. Most
286b5fca8f8Stomee  * objects unrecognized by the client in the move callback fall into this
287b5fca8f8Stomee  * category and are cheaply distinguished from known objects by the test
288b5fca8f8Stomee  * described earlier. Since recognition is cheap for the client, and searching
289b5fca8f8Stomee  * magazines is expensive for kmem, kmem defers searching until the client first
290b5fca8f8Stomee  * returns KMEM_CBRC_DONT_KNOW. As long as the needed effort is reasonable, kmem
291b5fca8f8Stomee  * elsewhere does what it can to avoid bothering the client unnecessarily.
292b5fca8f8Stomee  *
293b5fca8f8Stomee  * Invalidating the designated pointer member before freeing the object marks
294b5fca8f8Stomee  * the object to be avoided in the callback, and conversely, assigning a valid
295b5fca8f8Stomee  * value to the designated pointer member after allocating the object makes the
296b5fca8f8Stomee  * object fair game for the callback:
297b5fca8f8Stomee  *
298b5fca8f8Stomee  *      ... allocate object ...
299b5fca8f8Stomee  *      ... set any initial state not set by the constructor ...
300b5fca8f8Stomee  *
301b5fca8f8Stomee  *      mutex_enter(&container->c_objects_lock);
302b5fca8f8Stomee  *      list_insert_tail(&container->c_objects, object);
303b5fca8f8Stomee  *      membar_producer();
304b5fca8f8Stomee  *      object->o_container = container;
305b5fca8f8Stomee  *      mutex_exit(&container->c_objects_lock);
306b5fca8f8Stomee  *
307b5fca8f8Stomee  * Note that everything else must be valid before setting o_container makes the
308b5fca8f8Stomee  * object fair game for the move callback. The membar_producer() call ensures
309b5fca8f8Stomee  * that all the object's state is written to memory before setting the pointer
310b5fca8f8Stomee  * that transitions the object from state #3 or #7 (allocated, constructed, not
311b5fca8f8Stomee  * yet in use) to state #4 (in use, valid). That's important because the move
312b5fca8f8Stomee  * function has to check the validity of the pointer before it can safely
313b5fca8f8Stomee  * acquire the lock protecting the collection where it expects to find known
314b5fca8f8Stomee  * objects.
315b5fca8f8Stomee  *
316b5fca8f8Stomee  * This method of distinguishing known objects observes the usual symmetry:
317b5fca8f8Stomee  * invalidating the designated pointer is the first thing the client does before
318b5fca8f8Stomee  * freeing the object, and setting the designated pointer is the last thing the
319b5fca8f8Stomee  * client does after allocating the object. Of course, the client is not
320b5fca8f8Stomee  * required to use this method. Fundamentally, how the client recognizes known
321b5fca8f8Stomee  * objects is completely up to the client, but this method is recommended as an
322b5fca8f8Stomee  * efficient and safe way to take advantage of the guarantees made by kmem. If
323b5fca8f8Stomee  * the entire object is arbitrary data without any markable bits from a suitable
324b5fca8f8Stomee  * pointer member, then the client must find some other method, such as
325b5fca8f8Stomee  * searching a hash table of known objects.
326b5fca8f8Stomee  *
327b5fca8f8Stomee  * 2.5 Preventing Objects From Moving
328b5fca8f8Stomee  *
329b5fca8f8Stomee  * Besides a way to distinguish known objects, the other thing that the client
330b5fca8f8Stomee  * needs is a strategy to ensure that an object will not move while the client
331b5fca8f8Stomee  * is actively using it. The details of satisfying this requirement tend to be
332b5fca8f8Stomee  * highly cache-specific. It might seem that the same rules that let a client
333b5fca8f8Stomee  * remove an object safely should also decide when an object can be moved
334b5fca8f8Stomee  * safely. However, any object state that makes a removal attempt invalid is
335b5fca8f8Stomee  * likely to be long-lasting for objects that the client does not expect to
336b5fca8f8Stomee  * remove. kmem knows nothing about the object state and is equally likely (from
337b5fca8f8Stomee  * the client's point of view) to request a move for any object in the cache,
338b5fca8f8Stomee  * whether prepared for removal or not. Even a low percentage of objects stuck
339b5fca8f8Stomee  * in place by unremovability will defeat the consolidator if the stuck objects
340b5fca8f8Stomee  * are the same long-lived allocations likely to hold slabs hostage.
341b5fca8f8Stomee  * Fundamentally, the consolidator is not aimed at common cases. Severe external
342b5fca8f8Stomee  * fragmentation is a worst case scenario manifested as sparsely allocated
343b5fca8f8Stomee  * slabs, by definition a low percentage of the cache's objects. When deciding
344b5fca8f8Stomee  * what makes an object movable, keep in mind the goal of the consolidator: to
345b5fca8f8Stomee  * bring worst-case external fragmentation within the limits guaranteed for
346b5fca8f8Stomee  * internal fragmentation. Removability is a poor criterion if it is likely to
347b5fca8f8Stomee  * exclude more than an insignificant percentage of objects for long periods of
348b5fca8f8Stomee  * time.
349b5fca8f8Stomee  *
350b5fca8f8Stomee  * A tricky general solution exists, and it has the advantage of letting you
351b5fca8f8Stomee  * move any object at almost any moment, practically eliminating the likelihood
352b5fca8f8Stomee  * that an object can hold a slab hostage. However, if there is a cache-specific
353b5fca8f8Stomee  * way to ensure that an object is not actively in use in the vast majority of
354b5fca8f8Stomee  * cases, a simpler solution that leverages this cache-specific knowledge is
355b5fca8f8Stomee  * preferred.
356b5fca8f8Stomee  *
357b5fca8f8Stomee  * 2.5.1 Cache-Specific Solution
358b5fca8f8Stomee  *
359b5fca8f8Stomee  * As an example of a cache-specific solution, the ZFS znode cache takes
360b5fca8f8Stomee  * advantage of the fact that the vast majority of znodes are only being
361b5fca8f8Stomee  * referenced from the DNLC. (A typical case might be a few hundred in active
362b5fca8f8Stomee  * use and a hundred thousand in the DNLC.) In the move callback, after the ZFS
363b5fca8f8Stomee  * client has established that it recognizes the znode and can access its fields
364b5fca8f8Stomee  * safely (using the method described earlier), it then tests whether the znode
365b5fca8f8Stomee  * is referenced by anything other than the DNLC. If so, it assumes that the
366b5fca8f8Stomee  * znode may be in active use and is unsafe to move, so it drops its locks and
367b5fca8f8Stomee  * returns KMEM_CBRC_LATER. The advantage of this strategy is that everywhere
368b5fca8f8Stomee  * else znodes are used, no change is needed to protect against the possibility
369b5fca8f8Stomee  * of the znode moving. The disadvantage is that it remains possible for an
370b5fca8f8Stomee  * application to hold a znode slab hostage with an open file descriptor.
371b5fca8f8Stomee  * However, this case ought to be rare and the consolidator has a way to deal
372b5fca8f8Stomee  * with it: If the client responds KMEM_CBRC_LATER repeatedly for the same
373b5fca8f8Stomee  * object, kmem eventually stops believing it and treats the slab as if the
374b5fca8f8Stomee  * client had responded KMEM_CBRC_NO. Having marked the hostage slab, kmem can
375b5fca8f8Stomee  * then focus on getting it off of the partial slab list by allocating rather
376b5fca8f8Stomee  * than freeing all of its objects. (Either way of getting a slab off the
377b5fca8f8Stomee  * free list reduces fragmentation.)
378b5fca8f8Stomee  *
379b5fca8f8Stomee  * 2.5.2 General Solution
380b5fca8f8Stomee  *
381b5fca8f8Stomee  * The general solution, on the other hand, requires an explicit hold everywhere
382b5fca8f8Stomee  * the object is used to prevent it from moving. To keep the client locking
383b5fca8f8Stomee  * strategy as uncomplicated as possible, kmem guarantees the simplifying
384b5fca8f8Stomee  * assumption that move callbacks are sequential, even across multiple caches.
385b5fca8f8Stomee  * Internally, a global queue processed by a single thread supports all caches
386b5fca8f8Stomee  * implementing the callback function. No matter how many caches supply a move
387b5fca8f8Stomee  * function, the consolidator never moves more than one object at a time, so the
388b5fca8f8Stomee  * client does not have to worry about tricky lock ordering involving several
389b5fca8f8Stomee  * related objects from different kmem caches.
390b5fca8f8Stomee  *
391b5fca8f8Stomee  * The general solution implements the explicit hold as a read-write lock, which
392b5fca8f8Stomee  * allows multiple readers to access an object from the cache simultaneously
393b5fca8f8Stomee  * while a single writer is excluded from moving it. A single rwlock for the
394b5fca8f8Stomee  * entire cache would lock out all threads from using any of the cache's objects
395b5fca8f8Stomee  * even though only a single object is being moved, so to reduce contention,
396b5fca8f8Stomee  * the client can fan out the single rwlock into an array of rwlocks hashed by
397b5fca8f8Stomee  * the object address, making it probable that moving one object will not
398b5fca8f8Stomee  * prevent other threads from using a different object. The rwlock cannot be a
399b5fca8f8Stomee  * member of the object itself, because the possibility of the object moving
400b5fca8f8Stomee  * makes it unsafe to access any of the object's fields until the lock is
401b5fca8f8Stomee  * acquired.
402b5fca8f8Stomee  *
403b5fca8f8Stomee  * Assuming a small, fixed number of locks, it's possible that multiple objects
404b5fca8f8Stomee  * will hash to the same lock. A thread that needs to use multiple objects in
405b5fca8f8Stomee  * the same function may acquire the same lock multiple times. Since rwlocks are
406b5fca8f8Stomee  * reentrant for readers, and since there is never more than a single writer at
407b5fca8f8Stomee  * a time (assuming that the client acquires the lock as a writer only when
408b5fca8f8Stomee  * moving an object inside the callback), there would seem to be no problem.
409b5fca8f8Stomee  * However, a client locking multiple objects in the same function must handle
410b5fca8f8Stomee  * one case of potential deadlock: Assume that thread A needs to prevent both
411b5fca8f8Stomee  * object 1 and object 2 from moving, and thread B, the callback, meanwhile
412b5fca8f8Stomee  * tries to move object 3. It's possible, if objects 1, 2, and 3 all hash to the
413b5fca8f8Stomee  * same lock, that thread A will acquire the lock for object 1 as a reader
414b5fca8f8Stomee  * before thread B sets the lock's write-wanted bit, preventing thread A from
415b5fca8f8Stomee  * reacquiring the lock for object 2 as a reader. Unable to make forward
416b5fca8f8Stomee  * progress, thread A will never release the lock for object 1, resulting in
417b5fca8f8Stomee  * deadlock.
418b5fca8f8Stomee  *
419b5fca8f8Stomee  * There are two ways of avoiding the deadlock just described. The first is to
420b5fca8f8Stomee  * use rw_tryenter() rather than rw_enter() in the callback function when
421b5fca8f8Stomee  * attempting to acquire the lock as a writer. If tryenter discovers that the
422b5fca8f8Stomee  * same object (or another object hashed to the same lock) is already in use, it
423b5fca8f8Stomee  * aborts the callback and returns KMEM_CBRC_LATER. The second way is to use
424b5fca8f8Stomee  * rprwlock_t (declared in common/fs/zfs/sys/rprwlock.h) instead of rwlock_t,
425b5fca8f8Stomee  * since it allows a thread to acquire the lock as a reader in spite of a
426b5fca8f8Stomee  * waiting writer. This second approach insists on moving the object now, no
427b5fca8f8Stomee  * matter how many readers the move function must wait for in order to do so,
428b5fca8f8Stomee  * and could delay the completion of the callback indefinitely (blocking
429b5fca8f8Stomee  * callbacks to other clients). In practice, a less insistent callback using
430b5fca8f8Stomee  * rw_tryenter() returns KMEM_CBRC_LATER infrequently enough that there seems
431b5fca8f8Stomee  * little reason to use anything else.
432b5fca8f8Stomee  *
433b5fca8f8Stomee  * Avoiding deadlock is not the only problem that an implementation using an
434b5fca8f8Stomee  * explicit hold needs to solve. Locking the object in the first place (to
435b5fca8f8Stomee  * prevent it from moving) remains a problem, since the object could move
436b5fca8f8Stomee  * between the time you obtain a pointer to the object and the time you acquire
437b5fca8f8Stomee  * the rwlock hashed to that pointer value. Therefore the client needs to
438b5fca8f8Stomee  * recheck the value of the pointer after acquiring the lock, drop the lock if
439b5fca8f8Stomee  * the value has changed, and try again. This requires a level of indirection:
440b5fca8f8Stomee  * something that points to the object rather than the object itself, that the
441b5fca8f8Stomee  * client can access safely while attempting to acquire the lock. (The object
442b5fca8f8Stomee  * itself cannot be referenced safely because it can move at any time.)
443b5fca8f8Stomee  * The following lock-acquisition function takes whatever is safe to reference
444b5fca8f8Stomee  * (arg), follows its pointer to the object (using function f), and tries as
445b5fca8f8Stomee  * often as necessary to acquire the hashed lock and verify that the object
446b5fca8f8Stomee  * still has not moved:
447b5fca8f8Stomee  *
448b5fca8f8Stomee  *      object_t *
449b5fca8f8Stomee  *      object_hold(object_f f, void *arg)
450b5fca8f8Stomee  *      {
451b5fca8f8Stomee  *              object_t *op;
452b5fca8f8Stomee  *
453b5fca8f8Stomee  *              op = f(arg);
454b5fca8f8Stomee  *              if (op == NULL) {
455b5fca8f8Stomee  *                      return (NULL);
456b5fca8f8Stomee  *              }
457b5fca8f8Stomee  *
458b5fca8f8Stomee  *              rw_enter(OBJECT_RWLOCK(op), RW_READER);
459b5fca8f8Stomee  *              while (op != f(arg)) {
460b5fca8f8Stomee  *                      rw_exit(OBJECT_RWLOCK(op));
461b5fca8f8Stomee  *                      op = f(arg);
462b5fca8f8Stomee  *                      if (op == NULL) {
463b5fca8f8Stomee  *                              break;
464b5fca8f8Stomee  *                      }
465b5fca8f8Stomee  *                      rw_enter(OBJECT_RWLOCK(op), RW_READER);
466b5fca8f8Stomee  *              }
467b5fca8f8Stomee  *
468b5fca8f8Stomee  *              return (op);
469b5fca8f8Stomee  *      }
470b5fca8f8Stomee  *
471b5fca8f8Stomee  * The OBJECT_RWLOCK macro hashes the object address to obtain the rwlock. The
472b5fca8f8Stomee  * lock reacquisition loop, while necessary, almost never executes. The function
473b5fca8f8Stomee  * pointer f (used to obtain the object pointer from arg) has the following type
474b5fca8f8Stomee  * definition:
475b5fca8f8Stomee  *
476b5fca8f8Stomee  *      typedef object_t *(*object_f)(void *arg);
477b5fca8f8Stomee  *
478b5fca8f8Stomee  * An object_f implementation is likely to be as simple as accessing a structure
479b5fca8f8Stomee  * member:
480b5fca8f8Stomee  *
481b5fca8f8Stomee  *      object_t *
482b5fca8f8Stomee  *      s_object(void *arg)
483b5fca8f8Stomee  *      {
484b5fca8f8Stomee  *              something_t *sp = arg;
485b5fca8f8Stomee  *              return (sp->s_object);
486b5fca8f8Stomee  *      }
487b5fca8f8Stomee  *
488b5fca8f8Stomee  * The flexibility of a function pointer allows the path to the object to be
489b5fca8f8Stomee  * arbitrarily complex and also supports the notion that depending on where you
490b5fca8f8Stomee  * are using the object, you may need to get it from someplace different.
491b5fca8f8Stomee  *
492b5fca8f8Stomee  * The function that releases the explicit hold is simpler because it does not
493b5fca8f8Stomee  * have to worry about the object moving:
494b5fca8f8Stomee  *
495b5fca8f8Stomee  *      void
496b5fca8f8Stomee  *      object_rele(object_t *op)
497b5fca8f8Stomee  *      {
498b5fca8f8Stomee  *              rw_exit(OBJECT_RWLOCK(op));
499b5fca8f8Stomee  *      }
500b5fca8f8Stomee  *
501b5fca8f8Stomee  * The caller is spared these details so that obtaining and releasing an
502b5fca8f8Stomee  * explicit hold feels like a simple mutex_enter()/mutex_exit() pair. The caller
503b5fca8f8Stomee  * of object_hold() only needs to know that the returned object pointer is valid
504b5fca8f8Stomee  * if not NULL and that the object will not move until released.
505b5fca8f8Stomee  *
506b5fca8f8Stomee  * Although object_hold() prevents an object from moving, it does not prevent it
507b5fca8f8Stomee  * from being freed. The caller must take measures before calling object_hold()
508b5fca8f8Stomee  * (afterwards is too late) to ensure that the held object cannot be freed. The
509b5fca8f8Stomee  * caller must do so without accessing the unsafe object reference, so any lock
510b5fca8f8Stomee  * or reference count used to ensure the continued existence of the object must
511b5fca8f8Stomee  * live outside the object itself.
512b5fca8f8Stomee  *
513b5fca8f8Stomee  * Obtaining a new object is a special case where an explicit hold is impossible
514b5fca8f8Stomee  * for the caller. Any function that returns a newly allocated object (either as
515b5fca8f8Stomee  * a return value, or as an in-out paramter) must return it already held; after
516b5fca8f8Stomee  * the caller gets it is too late, since the object cannot be safely accessed
517b5fca8f8Stomee  * without the level of indirection described earlier. The following
518b5fca8f8Stomee  * object_alloc() example uses the same code shown earlier to transition a new
519b5fca8f8Stomee  * object into the state of being recognized (by the client) as a known object.
520b5fca8f8Stomee  * The function must acquire the hold (rw_enter) before that state transition
521b5fca8f8Stomee  * makes the object movable:
522b5fca8f8Stomee  *
523b5fca8f8Stomee  *      static object_t *
524b5fca8f8Stomee  *      object_alloc(container_t *container)
525b5fca8f8Stomee  *      {
5264d4c4c43STom Erickson  *              object_t *object = kmem_cache_alloc(object_cache, 0);
527b5fca8f8Stomee  *              ... set any initial state not set by the constructor ...
528b5fca8f8Stomee  *              rw_enter(OBJECT_RWLOCK(object), RW_READER);
529b5fca8f8Stomee  *              mutex_enter(&container->c_objects_lock);
530b5fca8f8Stomee  *              list_insert_tail(&container->c_objects, object);
531b5fca8f8Stomee  *              membar_producer();
532b5fca8f8Stomee  *              object->o_container = container;
533b5fca8f8Stomee  *              mutex_exit(&container->c_objects_lock);
534b5fca8f8Stomee  *              return (object);
535b5fca8f8Stomee  *      }
536b5fca8f8Stomee  *
537b5fca8f8Stomee  * Functions that implicitly acquire an object hold (any function that calls
538b5fca8f8Stomee  * object_alloc() to supply an object for the caller) need to be carefully noted
539b5fca8f8Stomee  * so that the matching object_rele() is not neglected. Otherwise, leaked holds
540b5fca8f8Stomee  * prevent all objects hashed to the affected rwlocks from ever being moved.
541b5fca8f8Stomee  *
542b5fca8f8Stomee  * The pointer to a held object can be hashed to the holding rwlock even after
543b5fca8f8Stomee  * the object has been freed. Although it is possible to release the hold
544b5fca8f8Stomee  * after freeing the object, you may decide to release the hold implicitly in
545b5fca8f8Stomee  * whatever function frees the object, so as to release the hold as soon as
546b5fca8f8Stomee  * possible, and for the sake of symmetry with the function that implicitly
547b5fca8f8Stomee  * acquires the hold when it allocates the object. Here, object_free() releases
548b5fca8f8Stomee  * the hold acquired by object_alloc(). Its implicit object_rele() forms a
549b5fca8f8Stomee  * matching pair with object_hold():
550b5fca8f8Stomee  *
551b5fca8f8Stomee  *      void
552b5fca8f8Stomee  *      object_free(object_t *object)
553b5fca8f8Stomee  *      {
554b5fca8f8Stomee  *              container_t *container;
555b5fca8f8Stomee  *
556b5fca8f8Stomee  *              ASSERT(object_held(object));
557b5fca8f8Stomee  *              container = object->o_container;
558b5fca8f8Stomee  *              mutex_enter(&container->c_objects_lock);
559b5fca8f8Stomee  *              object->o_container =
560b5fca8f8Stomee  *                  (void *)((uintptr_t)object->o_container | 0x1);
561b5fca8f8Stomee  *              list_remove(&container->c_objects, object);
562b5fca8f8Stomee  *              mutex_exit(&container->c_objects_lock);
563b5fca8f8Stomee  *              object_rele(object);
564b5fca8f8Stomee  *              kmem_cache_free(object_cache, object);
565b5fca8f8Stomee  *      }
566b5fca8f8Stomee  *
567b5fca8f8Stomee  * Note that object_free() cannot safely accept an object pointer as an argument
568b5fca8f8Stomee  * unless the object is already held. Any function that calls object_free()
569b5fca8f8Stomee  * needs to be carefully noted since it similarly forms a matching pair with
570b5fca8f8Stomee  * object_hold().
571b5fca8f8Stomee  *
572b5fca8f8Stomee  * To complete the picture, the following callback function implements the
573b5fca8f8Stomee  * general solution by moving objects only if they are currently unheld:
574b5fca8f8Stomee  *
575b5fca8f8Stomee  *      static kmem_cbrc_t
576b5fca8f8Stomee  *      object_move(void *buf, void *newbuf, size_t size, void *arg)
577b5fca8f8Stomee  *      {
578b5fca8f8Stomee  *              object_t *op = buf, *np = newbuf;
579b5fca8f8Stomee  *              container_t *container;
580b5fca8f8Stomee  *
581b5fca8f8Stomee  *              container = op->o_container;
582b5fca8f8Stomee  *              if ((uintptr_t)container & 0x3) {
583b5fca8f8Stomee  *                      return (KMEM_CBRC_DONT_KNOW);
584b5fca8f8Stomee  *              }
585b5fca8f8Stomee  *
586b5fca8f8Stomee  *	        // Ensure that the container structure does not go away.
587b5fca8f8Stomee  *              if (container_hold(container) == 0) {
588b5fca8f8Stomee  *                      return (KMEM_CBRC_DONT_KNOW);
589b5fca8f8Stomee  *              }
590b5fca8f8Stomee  *
591b5fca8f8Stomee  *              mutex_enter(&container->c_objects_lock);
592b5fca8f8Stomee  *              if (container != op->o_container) {
593b5fca8f8Stomee  *                      mutex_exit(&container->c_objects_lock);
594b5fca8f8Stomee  *                      container_rele(container);
595b5fca8f8Stomee  *                      return (KMEM_CBRC_DONT_KNOW);
596b5fca8f8Stomee  *              }
597b5fca8f8Stomee  *
598b5fca8f8Stomee  *              if (rw_tryenter(OBJECT_RWLOCK(op), RW_WRITER) == 0) {
599b5fca8f8Stomee  *                      mutex_exit(&container->c_objects_lock);
600b5fca8f8Stomee  *                      container_rele(container);
601b5fca8f8Stomee  *                      return (KMEM_CBRC_LATER);
602b5fca8f8Stomee  *              }
603b5fca8f8Stomee  *
604b5fca8f8Stomee  *              object_move_impl(op, np); // critical section
605b5fca8f8Stomee  *              rw_exit(OBJECT_RWLOCK(op));
606b5fca8f8Stomee  *
607b5fca8f8Stomee  *              op->o_container = (void *)((uintptr_t)op->o_container | 0x1);
608b5fca8f8Stomee  *              list_link_replace(&op->o_link_node, &np->o_link_node);
609b5fca8f8Stomee  *              mutex_exit(&container->c_objects_lock);
610b5fca8f8Stomee  *              container_rele(container);
611b5fca8f8Stomee  *              return (KMEM_CBRC_YES);
612b5fca8f8Stomee  *      }
613b5fca8f8Stomee  *
614b5fca8f8Stomee  * Note that object_move() must invalidate the designated o_container pointer of
615b5fca8f8Stomee  * the old object in the same way that object_free() does, since kmem will free
616b5fca8f8Stomee  * the object in response to the KMEM_CBRC_YES return value.
617b5fca8f8Stomee  *
618b5fca8f8Stomee  * The lock order in object_move() differs from object_alloc(), which locks
619b5fca8f8Stomee  * OBJECT_RWLOCK first and &container->c_objects_lock second, but as long as the
620b5fca8f8Stomee  * callback uses rw_tryenter() (preventing the deadlock described earlier), it's
621b5fca8f8Stomee  * not a problem. Holding the lock on the object list in the example above
622b5fca8f8Stomee  * through the entire callback not only prevents the object from going away, it
623b5fca8f8Stomee  * also allows you to lock the list elsewhere and know that none of its elements
624b5fca8f8Stomee  * will move during iteration.
625b5fca8f8Stomee  *
626b5fca8f8Stomee  * Adding an explicit hold everywhere an object from the cache is used is tricky
627b5fca8f8Stomee  * and involves much more change to client code than a cache-specific solution
628b5fca8f8Stomee  * that leverages existing state to decide whether or not an object is
629b5fca8f8Stomee  * movable. However, this approach has the advantage that no object remains
630b5fca8f8Stomee  * immovable for any significant length of time, making it extremely unlikely
631b5fca8f8Stomee  * that long-lived allocations can continue holding slabs hostage; and it works
632b5fca8f8Stomee  * for any cache.
633b5fca8f8Stomee  *
634b5fca8f8Stomee  * 3. Consolidator Implementation
635b5fca8f8Stomee  *
636b5fca8f8Stomee  * Once the client supplies a move function that a) recognizes known objects and
637b5fca8f8Stomee  * b) avoids moving objects that are actively in use, the remaining work is up
638b5fca8f8Stomee  * to the consolidator to decide which objects to move and when to issue
639b5fca8f8Stomee  * callbacks.
640b5fca8f8Stomee  *
641b5fca8f8Stomee  * The consolidator relies on the fact that a cache's slabs are ordered by
642b5fca8f8Stomee  * usage. Each slab has a fixed number of objects. Depending on the slab's
643b5fca8f8Stomee  * "color" (the offset of the first object from the beginning of the slab;
644b5fca8f8Stomee  * offsets are staggered to mitigate false sharing of cache lines) it is either
645b5fca8f8Stomee  * the maximum number of objects per slab determined at cache creation time or
646b5fca8f8Stomee  * else the number closest to the maximum that fits within the space remaining
647b5fca8f8Stomee  * after the initial offset. A completely allocated slab may contribute some
648b5fca8f8Stomee  * internal fragmentation (per-slab overhead) but no external fragmentation, so
649b5fca8f8Stomee  * it is of no interest to the consolidator. At the other extreme, slabs whose
650b5fca8f8Stomee  * objects have all been freed to the slab are released to the virtual memory
651b5fca8f8Stomee  * (VM) subsystem (objects freed to magazines are still allocated as far as the
652b5fca8f8Stomee  * slab is concerned). External fragmentation exists when there are slabs
653b5fca8f8Stomee  * somewhere between these extremes. A partial slab has at least one but not all
654b5fca8f8Stomee  * of its objects allocated. The more partial slabs, and the fewer allocated
655b5fca8f8Stomee  * objects on each of them, the higher the fragmentation. Hence the
656b5fca8f8Stomee  * consolidator's overall strategy is to reduce the number of partial slabs by
657b5fca8f8Stomee  * moving allocated objects from the least allocated slabs to the most allocated
658b5fca8f8Stomee  * slabs.
659b5fca8f8Stomee  *
660b5fca8f8Stomee  * Partial slabs are kept in an AVL tree ordered by usage. Completely allocated
661b5fca8f8Stomee  * slabs are kept separately in an unordered list. Since the majority of slabs
662b5fca8f8Stomee  * tend to be completely allocated (a typical unfragmented cache may have
663b5fca8f8Stomee  * thousands of complete slabs and only a single partial slab), separating
664b5fca8f8Stomee  * complete slabs improves the efficiency of partial slab ordering, since the
665b5fca8f8Stomee  * complete slabs do not affect the depth or balance of the AVL tree. This
666b5fca8f8Stomee  * ordered sequence of partial slabs acts as a "free list" supplying objects for
667b5fca8f8Stomee  * allocation requests.
668b5fca8f8Stomee  *
669b5fca8f8Stomee  * Objects are always allocated from the first partial slab in the free list,
670b5fca8f8Stomee  * where the allocation is most likely to eliminate a partial slab (by
671b5fca8f8Stomee  * completely allocating it). Conversely, when a single object from a completely
672b5fca8f8Stomee  * allocated slab is freed to the slab, that slab is added to the front of the
673b5fca8f8Stomee  * free list. Since most free list activity involves highly allocated slabs
674b5fca8f8Stomee  * coming and going at the front of the list, slabs tend naturally toward the
675b5fca8f8Stomee  * ideal order: highly allocated at the front, sparsely allocated at the back.
676b5fca8f8Stomee  * Slabs with few allocated objects are likely to become completely free if they
677b5fca8f8Stomee  * keep a safe distance away from the front of the free list. Slab misorders
678b5fca8f8Stomee  * interfere with the natural tendency of slabs to become completely free or
679b5fca8f8Stomee  * completely allocated. For example, a slab with a single allocated object
680b5fca8f8Stomee  * needs only a single free to escape the cache; its natural desire is
681b5fca8f8Stomee  * frustrated when it finds itself at the front of the list where a second
682b5fca8f8Stomee  * allocation happens just before the free could have released it. Another slab
683b5fca8f8Stomee  * with all but one object allocated might have supplied the buffer instead, so
684b5fca8f8Stomee  * that both (as opposed to neither) of the slabs would have been taken off the
685b5fca8f8Stomee  * free list.
686b5fca8f8Stomee  *
687b5fca8f8Stomee  * Although slabs tend naturally toward the ideal order, misorders allowed by a
688b5fca8f8Stomee  * simple list implementation defeat the consolidator's strategy of merging
689b5fca8f8Stomee  * least- and most-allocated slabs. Without an AVL tree to guarantee order, kmem
690b5fca8f8Stomee  * needs another way to fix misorders to optimize its callback strategy. One
691b5fca8f8Stomee  * approach is to periodically scan a limited number of slabs, advancing a
692b5fca8f8Stomee  * marker to hold the current scan position, and to move extreme misorders to
693b5fca8f8Stomee  * the front or back of the free list and to the front or back of the current
694b5fca8f8Stomee  * scan range. By making consecutive scan ranges overlap by one slab, the least
695b5fca8f8Stomee  * allocated slab in the current range can be carried along from the end of one
696b5fca8f8Stomee  * scan to the start of the next.
697b5fca8f8Stomee  *
698b5fca8f8Stomee  * Maintaining partial slabs in an AVL tree relieves kmem of this additional
699b5fca8f8Stomee  * task, however. Since most of the cache's activity is in the magazine layer,
700b5fca8f8Stomee  * and allocations from the slab layer represent only a startup cost, the
701b5fca8f8Stomee  * overhead of maintaining a balanced tree is not a significant concern compared
702b5fca8f8Stomee  * to the opportunity of reducing complexity by eliminating the partial slab
703b5fca8f8Stomee  * scanner just described. The overhead of an AVL tree is minimized by
704b5fca8f8Stomee  * maintaining only partial slabs in the tree and keeping completely allocated
705b5fca8f8Stomee  * slabs separately in a list. To avoid increasing the size of the slab
706b5fca8f8Stomee  * structure the AVL linkage pointers are reused for the slab's list linkage,
707b5fca8f8Stomee  * since the slab will always be either partial or complete, never stored both
708b5fca8f8Stomee  * ways at the same time. To further minimize the overhead of the AVL tree the
709b5fca8f8Stomee  * compare function that orders partial slabs by usage divides the range of
710b5fca8f8Stomee  * allocated object counts into bins such that counts within the same bin are
711b5fca8f8Stomee  * considered equal. Binning partial slabs makes it less likely that allocating
712b5fca8f8Stomee  * or freeing a single object will change the slab's order, requiring a tree
713b5fca8f8Stomee  * reinsertion (an avl_remove() followed by an avl_add(), both potentially
714b5fca8f8Stomee  * requiring some rebalancing of the tree). Allocation counts closest to
715b5fca8f8Stomee  * completely free and completely allocated are left unbinned (finely sorted) to
716b5fca8f8Stomee  * better support the consolidator's strategy of merging slabs at either
717b5fca8f8Stomee  * extreme.
718b5fca8f8Stomee  *
719b5fca8f8Stomee  * 3.1 Assessing Fragmentation and Selecting Candidate Slabs
720b5fca8f8Stomee  *
721b5fca8f8Stomee  * The consolidator piggybacks on the kmem maintenance thread and is called on
722b5fca8f8Stomee  * the same interval as kmem_cache_update(), once per cache every fifteen
723b5fca8f8Stomee  * seconds. kmem maintains a running count of unallocated objects in the slab
724b5fca8f8Stomee  * layer (cache_bufslab). The consolidator checks whether that number exceeds
725b5fca8f8Stomee  * 12.5% (1/8) of the total objects in the cache (cache_buftotal), and whether
726b5fca8f8Stomee  * there is a significant number of slabs in the cache (arbitrarily a minimum
727b5fca8f8Stomee  * 101 total slabs). Unused objects that have fallen out of the magazine layer's
728b5fca8f8Stomee  * working set are included in the assessment, and magazines in the depot are
729b5fca8f8Stomee  * reaped if those objects would lift cache_bufslab above the fragmentation
730b5fca8f8Stomee  * threshold. Once the consolidator decides that a cache is fragmented, it looks
731b5fca8f8Stomee  * for a candidate slab to reclaim, starting at the end of the partial slab free
732b5fca8f8Stomee  * list and scanning backwards. At first the consolidator is choosy: only a slab
733b5fca8f8Stomee  * with fewer than 12.5% (1/8) of its objects allocated qualifies (or else a
734b5fca8f8Stomee  * single allocated object, regardless of percentage). If there is difficulty
735b5fca8f8Stomee  * finding a candidate slab, kmem raises the allocation threshold incrementally,
736b5fca8f8Stomee  * up to a maximum 87.5% (7/8), so that eventually the consolidator will reduce
737b5fca8f8Stomee  * external fragmentation (unused objects on the free list) below 12.5% (1/8),
738b5fca8f8Stomee  * even in the worst case of every slab in the cache being almost 7/8 allocated.
739b5fca8f8Stomee  * The threshold can also be lowered incrementally when candidate slabs are easy
740b5fca8f8Stomee  * to find, and the threshold is reset to the minimum 1/8 as soon as the cache
741b5fca8f8Stomee  * is no longer fragmented.
742b5fca8f8Stomee  *
743b5fca8f8Stomee  * 3.2 Generating Callbacks
744b5fca8f8Stomee  *
745b5fca8f8Stomee  * Once an eligible slab is chosen, a callback is generated for every allocated
746b5fca8f8Stomee  * object on the slab, in the hope that the client will move everything off the
747b5fca8f8Stomee  * slab and make it reclaimable. Objects selected as move destinations are
748b5fca8f8Stomee  * chosen from slabs at the front of the free list. Assuming slabs in the ideal
749b5fca8f8Stomee  * order (most allocated at the front, least allocated at the back) and a
750b5fca8f8Stomee  * cooperative client, the consolidator will succeed in removing slabs from both
751b5fca8f8Stomee  * ends of the free list, completely allocating on the one hand and completely
752b5fca8f8Stomee  * freeing on the other. Objects selected as move destinations are allocated in
753b5fca8f8Stomee  * the kmem maintenance thread where move requests are enqueued. A separate
754b5fca8f8Stomee  * callback thread removes pending callbacks from the queue and calls the
755b5fca8f8Stomee  * client. The separate thread ensures that client code (the move function) does
756b5fca8f8Stomee  * not interfere with internal kmem maintenance tasks. A map of pending
757b5fca8f8Stomee  * callbacks keyed by object address (the object to be moved) is checked to
758b5fca8f8Stomee  * ensure that duplicate callbacks are not generated for the same object.
759b5fca8f8Stomee  * Allocating the move destination (the object to move to) prevents subsequent
760b5fca8f8Stomee  * callbacks from selecting the same destination as an earlier pending callback.
761b5fca8f8Stomee  *
762b5fca8f8Stomee  * Move requests can also be generated by kmem_cache_reap() when the system is
763b5fca8f8Stomee  * desperate for memory and by kmem_cache_move_notify(), called by the client to
764b5fca8f8Stomee  * notify kmem that a move refused earlier with KMEM_CBRC_LATER is now possible.
765b5fca8f8Stomee  * The map of pending callbacks is protected by the same lock that protects the
766b5fca8f8Stomee  * slab layer.
767b5fca8f8Stomee  *
768b5fca8f8Stomee  * When the system is desperate for memory, kmem does not bother to determine
769b5fca8f8Stomee  * whether or not the cache exceeds the fragmentation threshold, but tries to
770b5fca8f8Stomee  * consolidate as many slabs as possible. Normally, the consolidator chews
771b5fca8f8Stomee  * slowly, one sparsely allocated slab at a time during each maintenance
772b5fca8f8Stomee  * interval that the cache is fragmented. When desperate, the consolidator
773b5fca8f8Stomee  * starts at the last partial slab and enqueues callbacks for every allocated
774b5fca8f8Stomee  * object on every partial slab, working backwards until it reaches the first
775b5fca8f8Stomee  * partial slab. The first partial slab, meanwhile, advances in pace with the
776b5fca8f8Stomee  * consolidator as allocations to supply move destinations for the enqueued
777b5fca8f8Stomee  * callbacks use up the highly allocated slabs at the front of the free list.
778b5fca8f8Stomee  * Ideally, the overgrown free list collapses like an accordion, starting at
779b5fca8f8Stomee  * both ends and ending at the center with a single partial slab.
780b5fca8f8Stomee  *
781b5fca8f8Stomee  * 3.3 Client Responses
782b5fca8f8Stomee  *
783b5fca8f8Stomee  * When the client returns KMEM_CBRC_NO in response to the move callback, kmem
784b5fca8f8Stomee  * marks the slab that supplied the stuck object non-reclaimable and moves it to
785b5fca8f8Stomee  * front of the free list. The slab remains marked as long as it remains on the
786b5fca8f8Stomee  * free list, and it appears more allocated to the partial slab compare function
787b5fca8f8Stomee  * than any unmarked slab, no matter how many of its objects are allocated.
788b5fca8f8Stomee  * Since even one immovable object ties up the entire slab, the goal is to
789b5fca8f8Stomee  * completely allocate any slab that cannot be completely freed. kmem does not
790b5fca8f8Stomee  * bother generating callbacks to move objects from a marked slab unless the
791b5fca8f8Stomee  * system is desperate.
792b5fca8f8Stomee  *
793b5fca8f8Stomee  * When the client responds KMEM_CBRC_LATER, kmem increments a count for the
794b5fca8f8Stomee  * slab. If the client responds LATER too many times, kmem disbelieves and
795b5fca8f8Stomee  * treats the response as a NO. The count is cleared when the slab is taken off
796b5fca8f8Stomee  * the partial slab list or when the client moves one of the slab's objects.
797b5fca8f8Stomee  *
798b5fca8f8Stomee  * 4. Observability
799b5fca8f8Stomee  *
800b5fca8f8Stomee  * A kmem cache's external fragmentation is best observed with 'mdb -k' using
801b5fca8f8Stomee  * the ::kmem_slabs dcmd. For a complete description of the command, enter
802b5fca8f8Stomee  * '::help kmem_slabs' at the mdb prompt.
8037c478bd9Sstevel@tonic-gate  */
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate #include <sys/kmem_impl.h>
8067c478bd9Sstevel@tonic-gate #include <sys/vmem_impl.h>
8077c478bd9Sstevel@tonic-gate #include <sys/param.h>
8087c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
8097c478bd9Sstevel@tonic-gate #include <sys/vm.h>
8107c478bd9Sstevel@tonic-gate #include <sys/proc.h>
8117c478bd9Sstevel@tonic-gate #include <sys/tuneable.h>
8127c478bd9Sstevel@tonic-gate #include <sys/systm.h>
8137c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
8147c478bd9Sstevel@tonic-gate #include <sys/debug.h>
815b5fca8f8Stomee #include <sys/sdt.h>
8167c478bd9Sstevel@tonic-gate #include <sys/mutex.h>
8177c478bd9Sstevel@tonic-gate #include <sys/bitmap.h>
8187c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
8197c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
8207c478bd9Sstevel@tonic-gate #include <sys/disp.h>
8217c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
8227c478bd9Sstevel@tonic-gate #include <sys/log.h>
8237c478bd9Sstevel@tonic-gate #include <sys/callb.h>
8247c478bd9Sstevel@tonic-gate #include <sys/taskq.h>
8257c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
8267c478bd9Sstevel@tonic-gate #include <sys/reboot.h>
8277c478bd9Sstevel@tonic-gate #include <sys/id32.h>
8287c478bd9Sstevel@tonic-gate #include <sys/zone.h>
829f4b3ec61Sdh155122 #include <sys/netstack.h>
830b5fca8f8Stomee #ifdef	DEBUG
831b5fca8f8Stomee #include <sys/random.h>
832b5fca8f8Stomee #endif
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate extern void streams_msg_init(void);
8357c478bd9Sstevel@tonic-gate extern int segkp_fromheap;
8367c478bd9Sstevel@tonic-gate extern void segkp_cache_free(void);
8376e00b116SPeter Telford extern int callout_init_done;
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate struct kmem_cache_kstat {
8407c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_buf_size;
8417c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_align;
8427c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_chunk_size;
8437c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_slab_size;
8447c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_alloc;
8457c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_alloc_fail;
8467c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_free;
8477c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_depot_alloc;
8487c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_depot_free;
8497c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_depot_contention;
8507c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_slab_alloc;
8517c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_slab_free;
8527c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_buf_constructed;
8537c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_buf_avail;
8547c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_buf_inuse;
8557c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_buf_total;
8567c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_buf_max;
8577c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_slab_create;
8587c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_slab_destroy;
8597c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_vmem_source;
8607c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_hash_size;
8617c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_hash_lookup_depth;
8627c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_hash_rescale;
8637c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_full_magazines;
8647c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_empty_magazines;
8657c478bd9Sstevel@tonic-gate 	kstat_named_t	kmc_magazine_size;
866686031edSTom Erickson 	kstat_named_t	kmc_reap; /* number of kmem_cache_reap() calls */
867686031edSTom Erickson 	kstat_named_t	kmc_defrag; /* attempts to defrag all partial slabs */
868686031edSTom Erickson 	kstat_named_t	kmc_scan; /* attempts to defrag one partial slab */
869686031edSTom Erickson 	kstat_named_t	kmc_move_callbacks; /* sum of yes, no, later, dn, dk */
870b5fca8f8Stomee 	kstat_named_t	kmc_move_yes;
871b5fca8f8Stomee 	kstat_named_t	kmc_move_no;
872b5fca8f8Stomee 	kstat_named_t	kmc_move_later;
873b5fca8f8Stomee 	kstat_named_t	kmc_move_dont_need;
874686031edSTom Erickson 	kstat_named_t	kmc_move_dont_know; /* obj unrecognized by client ... */
875686031edSTom Erickson 	kstat_named_t	kmc_move_hunt_found; /* ... but found in mag layer */
876686031edSTom Erickson 	kstat_named_t	kmc_move_slabs_freed; /* slabs freed by consolidator */
877686031edSTom Erickson 	kstat_named_t	kmc_move_reclaimable; /* buffers, if consolidator ran */
8787c478bd9Sstevel@tonic-gate } kmem_cache_kstat = {
8797c478bd9Sstevel@tonic-gate 	{ "buf_size",		KSTAT_DATA_UINT64 },
8807c478bd9Sstevel@tonic-gate 	{ "align",		KSTAT_DATA_UINT64 },
8817c478bd9Sstevel@tonic-gate 	{ "chunk_size",		KSTAT_DATA_UINT64 },
8827c478bd9Sstevel@tonic-gate 	{ "slab_size",		KSTAT_DATA_UINT64 },
8837c478bd9Sstevel@tonic-gate 	{ "alloc",		KSTAT_DATA_UINT64 },
8847c478bd9Sstevel@tonic-gate 	{ "alloc_fail",		KSTAT_DATA_UINT64 },
8857c478bd9Sstevel@tonic-gate 	{ "free",		KSTAT_DATA_UINT64 },
8867c478bd9Sstevel@tonic-gate 	{ "depot_alloc",	KSTAT_DATA_UINT64 },
8877c478bd9Sstevel@tonic-gate 	{ "depot_free",		KSTAT_DATA_UINT64 },
8887c478bd9Sstevel@tonic-gate 	{ "depot_contention",	KSTAT_DATA_UINT64 },
8897c478bd9Sstevel@tonic-gate 	{ "slab_alloc",		KSTAT_DATA_UINT64 },
8907c478bd9Sstevel@tonic-gate 	{ "slab_free",		KSTAT_DATA_UINT64 },
8917c478bd9Sstevel@tonic-gate 	{ "buf_constructed",	KSTAT_DATA_UINT64 },
8927c478bd9Sstevel@tonic-gate 	{ "buf_avail",		KSTAT_DATA_UINT64 },
8937c478bd9Sstevel@tonic-gate 	{ "buf_inuse",		KSTAT_DATA_UINT64 },
8947c478bd9Sstevel@tonic-gate 	{ "buf_total",		KSTAT_DATA_UINT64 },
8957c478bd9Sstevel@tonic-gate 	{ "buf_max",		KSTAT_DATA_UINT64 },
8967c478bd9Sstevel@tonic-gate 	{ "slab_create",	KSTAT_DATA_UINT64 },
8977c478bd9Sstevel@tonic-gate 	{ "slab_destroy",	KSTAT_DATA_UINT64 },
8987c478bd9Sstevel@tonic-gate 	{ "vmem_source",	KSTAT_DATA_UINT64 },
8997c478bd9Sstevel@tonic-gate 	{ "hash_size",		KSTAT_DATA_UINT64 },
9007c478bd9Sstevel@tonic-gate 	{ "hash_lookup_depth",	KSTAT_DATA_UINT64 },
9017c478bd9Sstevel@tonic-gate 	{ "hash_rescale",	KSTAT_DATA_UINT64 },
9027c478bd9Sstevel@tonic-gate 	{ "full_magazines",	KSTAT_DATA_UINT64 },
9037c478bd9Sstevel@tonic-gate 	{ "empty_magazines",	KSTAT_DATA_UINT64 },
9047c478bd9Sstevel@tonic-gate 	{ "magazine_size",	KSTAT_DATA_UINT64 },
905686031edSTom Erickson 	{ "reap",		KSTAT_DATA_UINT64 },
906686031edSTom Erickson 	{ "defrag",		KSTAT_DATA_UINT64 },
907686031edSTom Erickson 	{ "scan",		KSTAT_DATA_UINT64 },
908b5fca8f8Stomee 	{ "move_callbacks",	KSTAT_DATA_UINT64 },
909b5fca8f8Stomee 	{ "move_yes",		KSTAT_DATA_UINT64 },
910b5fca8f8Stomee 	{ "move_no",		KSTAT_DATA_UINT64 },
911b5fca8f8Stomee 	{ "move_later",		KSTAT_DATA_UINT64 },
912b5fca8f8Stomee 	{ "move_dont_need",	KSTAT_DATA_UINT64 },
913b5fca8f8Stomee 	{ "move_dont_know",	KSTAT_DATA_UINT64 },
914b5fca8f8Stomee 	{ "move_hunt_found",	KSTAT_DATA_UINT64 },
915686031edSTom Erickson 	{ "move_slabs_freed",	KSTAT_DATA_UINT64 },
916686031edSTom Erickson 	{ "move_reclaimable",	KSTAT_DATA_UINT64 },
9177c478bd9Sstevel@tonic-gate };
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate static kmutex_t kmem_cache_kstat_lock;
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate /*
9227c478bd9Sstevel@tonic-gate  * The default set of caches to back kmem_alloc().
9237c478bd9Sstevel@tonic-gate  * These sizes should be reevaluated periodically.
9247c478bd9Sstevel@tonic-gate  *
9257c478bd9Sstevel@tonic-gate  * We want allocations that are multiples of the coherency granularity
9267c478bd9Sstevel@tonic-gate  * (64 bytes) to be satisfied from a cache which is a multiple of 64
9277c478bd9Sstevel@tonic-gate  * bytes, so that it will be 64-byte aligned.  For all multiples of 64,
9287c478bd9Sstevel@tonic-gate  * the next kmem_cache_size greater than or equal to it must be a
9297c478bd9Sstevel@tonic-gate  * multiple of 64.
930dce01e3fSJonathan W Adams  *
931dce01e3fSJonathan W Adams  * We split the table into two sections:  size <= 4k and size > 4k.  This
932dce01e3fSJonathan W Adams  * saves a lot of space and cache footprint in our cache tables.
9337c478bd9Sstevel@tonic-gate  */
9347c478bd9Sstevel@tonic-gate static const int kmem_alloc_sizes[] = {
9357c478bd9Sstevel@tonic-gate 	1 * 8,
9367c478bd9Sstevel@tonic-gate 	2 * 8,
9377c478bd9Sstevel@tonic-gate 	3 * 8,
9387c478bd9Sstevel@tonic-gate 	4 * 8,		5 * 8,		6 * 8,		7 * 8,
9397c478bd9Sstevel@tonic-gate 	4 * 16,		5 * 16,		6 * 16,		7 * 16,
9407c478bd9Sstevel@tonic-gate 	4 * 32,		5 * 32,		6 * 32,		7 * 32,
9417c478bd9Sstevel@tonic-gate 	4 * 64,		5 * 64,		6 * 64,		7 * 64,
9427c478bd9Sstevel@tonic-gate 	4 * 128,	5 * 128,	6 * 128,	7 * 128,
9437c478bd9Sstevel@tonic-gate 	P2ALIGN(8192 / 7, 64),
9447c478bd9Sstevel@tonic-gate 	P2ALIGN(8192 / 6, 64),
9457c478bd9Sstevel@tonic-gate 	P2ALIGN(8192 / 5, 64),
9467c478bd9Sstevel@tonic-gate 	P2ALIGN(8192 / 4, 64),
9477c478bd9Sstevel@tonic-gate 	P2ALIGN(8192 / 3, 64),
9487c478bd9Sstevel@tonic-gate 	P2ALIGN(8192 / 2, 64),
9497c478bd9Sstevel@tonic-gate };
9507c478bd9Sstevel@tonic-gate 
951dce01e3fSJonathan W Adams static const int kmem_big_alloc_sizes[] = {
952dce01e3fSJonathan W Adams 	2 * 4096,	3 * 4096,
953dce01e3fSJonathan W Adams 	2 * 8192,	3 * 8192,
954dce01e3fSJonathan W Adams 	4 * 8192,	5 * 8192,	6 * 8192,	7 * 8192,
955dce01e3fSJonathan W Adams 	8 * 8192,	9 * 8192,	10 * 8192,	11 * 8192,
956dce01e3fSJonathan W Adams 	12 * 8192,	13 * 8192,	14 * 8192,	15 * 8192,
957dce01e3fSJonathan W Adams 	16 * 8192
958dce01e3fSJonathan W Adams };
959dce01e3fSJonathan W Adams 
960dce01e3fSJonathan W Adams #define	KMEM_MAXBUF		4096
961dce01e3fSJonathan W Adams #define	KMEM_BIG_MAXBUF_32BIT	32768
962dce01e3fSJonathan W Adams #define	KMEM_BIG_MAXBUF		131072
963dce01e3fSJonathan W Adams 
964dce01e3fSJonathan W Adams #define	KMEM_BIG_MULTIPLE	4096	/* big_alloc_sizes must be a multiple */
965dce01e3fSJonathan W Adams #define	KMEM_BIG_SHIFT		12	/* lg(KMEM_BIG_MULTIPLE) */
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate static kmem_cache_t *kmem_alloc_table[KMEM_MAXBUF >> KMEM_ALIGN_SHIFT];
968dce01e3fSJonathan W Adams static kmem_cache_t *kmem_big_alloc_table[KMEM_BIG_MAXBUF >> KMEM_BIG_SHIFT];
969dce01e3fSJonathan W Adams 
970dce01e3fSJonathan W Adams #define	KMEM_ALLOC_TABLE_MAX	(KMEM_MAXBUF >> KMEM_ALIGN_SHIFT)
971dce01e3fSJonathan W Adams static size_t kmem_big_alloc_table_max = 0;	/* # of filled elements */
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate static kmem_magtype_t kmem_magtype[] = {
9747c478bd9Sstevel@tonic-gate 	{ 1,	8,	3200,	65536	},
9757c478bd9Sstevel@tonic-gate 	{ 3,	16,	256,	32768	},
9767c478bd9Sstevel@tonic-gate 	{ 7,	32,	64,	16384	},
9777c478bd9Sstevel@tonic-gate 	{ 15,	64,	0,	8192	},
9787c478bd9Sstevel@tonic-gate 	{ 31,	64,	0,	4096	},
9797c478bd9Sstevel@tonic-gate 	{ 47,	64,	0,	2048	},
9807c478bd9Sstevel@tonic-gate 	{ 63,	64,	0,	1024	},
9817c478bd9Sstevel@tonic-gate 	{ 95,	64,	0,	512	},
9827c478bd9Sstevel@tonic-gate 	{ 143,	64,	0,	0	},
9837c478bd9Sstevel@tonic-gate };
9847c478bd9Sstevel@tonic-gate 
9857c478bd9Sstevel@tonic-gate static uint32_t kmem_reaping;
9867c478bd9Sstevel@tonic-gate static uint32_t kmem_reaping_idspace;
9877c478bd9Sstevel@tonic-gate 
9887c478bd9Sstevel@tonic-gate /*
9897c478bd9Sstevel@tonic-gate  * kmem tunables
9907c478bd9Sstevel@tonic-gate  */
9917c478bd9Sstevel@tonic-gate clock_t kmem_reap_interval;	/* cache reaping rate [15 * HZ ticks] */
9927c478bd9Sstevel@tonic-gate int kmem_depot_contention = 3;	/* max failed tryenters per real interval */
9937c478bd9Sstevel@tonic-gate pgcnt_t kmem_reapahead = 0;	/* start reaping N pages before pageout */
9947c478bd9Sstevel@tonic-gate int kmem_panic = 1;		/* whether to panic on error */
9957c478bd9Sstevel@tonic-gate int kmem_logging = 1;		/* kmem_log_enter() override */
9967c478bd9Sstevel@tonic-gate uint32_t kmem_mtbf = 0;		/* mean time between failures [default: off] */
9977c478bd9Sstevel@tonic-gate size_t kmem_transaction_log_size; /* transaction log size [2% of memory] */
9987c478bd9Sstevel@tonic-gate size_t kmem_content_log_size;	/* content log size [2% of memory] */
9997c478bd9Sstevel@tonic-gate size_t kmem_failure_log_size;	/* failure log [4 pages per CPU] */
10007c478bd9Sstevel@tonic-gate size_t kmem_slab_log_size;	/* slab create log [4 pages per CPU] */
10017c478bd9Sstevel@tonic-gate size_t kmem_content_maxsave = 256; /* KMF_CONTENTS max bytes to log */
10027c478bd9Sstevel@tonic-gate size_t kmem_lite_minsize = 0;	/* minimum buffer size for KMF_LITE */
10037c478bd9Sstevel@tonic-gate size_t kmem_lite_maxalign = 1024; /* maximum buffer alignment for KMF_LITE */
10047c478bd9Sstevel@tonic-gate int kmem_lite_pcs = 4;		/* number of PCs to store in KMF_LITE mode */
10057c478bd9Sstevel@tonic-gate size_t kmem_maxverify;		/* maximum bytes to inspect in debug routines */
10067c478bd9Sstevel@tonic-gate size_t kmem_minfirewall;	/* hardware-enforced redzone threshold */
10077c478bd9Sstevel@tonic-gate 
1008dce01e3fSJonathan W Adams #ifdef _LP64
1009dce01e3fSJonathan W Adams size_t	kmem_max_cached = KMEM_BIG_MAXBUF;	/* maximum kmem_alloc cache */
1010dce01e3fSJonathan W Adams #else
1011dce01e3fSJonathan W Adams size_t	kmem_max_cached = KMEM_BIG_MAXBUF_32BIT; /* maximum kmem_alloc cache */
1012dce01e3fSJonathan W Adams #endif
1013dce01e3fSJonathan W Adams 
10147c478bd9Sstevel@tonic-gate #ifdef DEBUG
10157c478bd9Sstevel@tonic-gate int kmem_flags = KMF_AUDIT | KMF_DEADBEEF | KMF_REDZONE | KMF_CONTENTS;
10167c478bd9Sstevel@tonic-gate #else
10177c478bd9Sstevel@tonic-gate int kmem_flags = 0;
10187c478bd9Sstevel@tonic-gate #endif
10197c478bd9Sstevel@tonic-gate int kmem_ready;
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate static kmem_cache_t	*kmem_slab_cache;
10227c478bd9Sstevel@tonic-gate static kmem_cache_t	*kmem_bufctl_cache;
10237c478bd9Sstevel@tonic-gate static kmem_cache_t	*kmem_bufctl_audit_cache;
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate static kmutex_t		kmem_cache_lock;	/* inter-cache linkage only */
1026b5fca8f8Stomee static list_t		kmem_caches;
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate static taskq_t		*kmem_taskq;
10297c478bd9Sstevel@tonic-gate static kmutex_t		kmem_flags_lock;
10307c478bd9Sstevel@tonic-gate static vmem_t		*kmem_metadata_arena;
10317c478bd9Sstevel@tonic-gate static vmem_t		*kmem_msb_arena;	/* arena for metadata caches */
10327c478bd9Sstevel@tonic-gate static vmem_t		*kmem_cache_arena;
10337c478bd9Sstevel@tonic-gate static vmem_t		*kmem_hash_arena;
10347c478bd9Sstevel@tonic-gate static vmem_t		*kmem_log_arena;
10357c478bd9Sstevel@tonic-gate static vmem_t		*kmem_oversize_arena;
10367c478bd9Sstevel@tonic-gate static vmem_t		*kmem_va_arena;
10377c478bd9Sstevel@tonic-gate static vmem_t		*kmem_default_arena;
10387c478bd9Sstevel@tonic-gate static vmem_t		*kmem_firewall_va_arena;
10397c478bd9Sstevel@tonic-gate static vmem_t		*kmem_firewall_arena;
10407c478bd9Sstevel@tonic-gate 
1041b5fca8f8Stomee /*
1042b5fca8f8Stomee  * Define KMEM_STATS to turn on statistic gathering. By default, it is only
1043b5fca8f8Stomee  * turned on when DEBUG is also defined.
1044b5fca8f8Stomee  */
1045b5fca8f8Stomee #ifdef	DEBUG
1046b5fca8f8Stomee #define	KMEM_STATS
1047b5fca8f8Stomee #endif	/* DEBUG */
1048b5fca8f8Stomee 
1049b5fca8f8Stomee #ifdef	KMEM_STATS
1050b5fca8f8Stomee #define	KMEM_STAT_ADD(stat)			((stat)++)
1051b5fca8f8Stomee #define	KMEM_STAT_COND_ADD(cond, stat)		((void) (!(cond) || (stat)++))
1052b5fca8f8Stomee #else
1053b5fca8f8Stomee #define	KMEM_STAT_ADD(stat)			/* nothing */
1054b5fca8f8Stomee #define	KMEM_STAT_COND_ADD(cond, stat)		/* nothing */
1055b5fca8f8Stomee #endif	/* KMEM_STATS */
1056b5fca8f8Stomee 
1057b5fca8f8Stomee /*
1058b5fca8f8Stomee  * kmem slab consolidator thresholds (tunables)
1059b5fca8f8Stomee  */
1060686031edSTom Erickson size_t kmem_frag_minslabs = 101;	/* minimum total slabs */
1061686031edSTom Erickson size_t kmem_frag_numer = 1;		/* free buffers (numerator) */
1062686031edSTom Erickson size_t kmem_frag_denom = KMEM_VOID_FRACTION; /* buffers (denominator) */
1063b5fca8f8Stomee /*
1064b5fca8f8Stomee  * Maximum number of slabs from which to move buffers during a single
1065b5fca8f8Stomee  * maintenance interval while the system is not low on memory.
1066b5fca8f8Stomee  */
1067686031edSTom Erickson size_t kmem_reclaim_max_slabs = 1;
1068b5fca8f8Stomee /*
1069b5fca8f8Stomee  * Number of slabs to scan backwards from the end of the partial slab list
1070b5fca8f8Stomee  * when searching for buffers to relocate.
1071b5fca8f8Stomee  */
1072686031edSTom Erickson size_t kmem_reclaim_scan_range = 12;
1073b5fca8f8Stomee 
1074b5fca8f8Stomee #ifdef	KMEM_STATS
1075b5fca8f8Stomee static struct {
1076b5fca8f8Stomee 	uint64_t kms_callbacks;
1077b5fca8f8Stomee 	uint64_t kms_yes;
1078b5fca8f8Stomee 	uint64_t kms_no;
1079b5fca8f8Stomee 	uint64_t kms_later;
1080b5fca8f8Stomee 	uint64_t kms_dont_need;
1081b5fca8f8Stomee 	uint64_t kms_dont_know;
1082b5fca8f8Stomee 	uint64_t kms_hunt_found_mag;
1083686031edSTom Erickson 	uint64_t kms_hunt_found_slab;
1084b5fca8f8Stomee 	uint64_t kms_hunt_alloc_fail;
1085b5fca8f8Stomee 	uint64_t kms_hunt_lucky;
1086b5fca8f8Stomee 	uint64_t kms_notify;
1087b5fca8f8Stomee 	uint64_t kms_notify_callbacks;
1088b5fca8f8Stomee 	uint64_t kms_disbelief;
1089b5fca8f8Stomee 	uint64_t kms_already_pending;
1090b5fca8f8Stomee 	uint64_t kms_callback_alloc_fail;
109125e2c9cfStomee 	uint64_t kms_callback_taskq_fail;
1092686031edSTom Erickson 	uint64_t kms_endscan_slab_dead;
1093b5fca8f8Stomee 	uint64_t kms_endscan_slab_destroyed;
1094b5fca8f8Stomee 	uint64_t kms_endscan_nomem;
1095b5fca8f8Stomee 	uint64_t kms_endscan_refcnt_changed;
1096b5fca8f8Stomee 	uint64_t kms_endscan_nomove_changed;
1097b5fca8f8Stomee 	uint64_t kms_endscan_freelist;
1098b5fca8f8Stomee 	uint64_t kms_avl_update;
1099b5fca8f8Stomee 	uint64_t kms_avl_noupdate;
1100b5fca8f8Stomee 	uint64_t kms_no_longer_reclaimable;
1101b5fca8f8Stomee 	uint64_t kms_notify_no_longer_reclaimable;
1102686031edSTom Erickson 	uint64_t kms_notify_slab_dead;
1103686031edSTom Erickson 	uint64_t kms_notify_slab_destroyed;
1104b5fca8f8Stomee 	uint64_t kms_alloc_fail;
1105b5fca8f8Stomee 	uint64_t kms_constructor_fail;
1106b5fca8f8Stomee 	uint64_t kms_dead_slabs_freed;
1107b5fca8f8Stomee 	uint64_t kms_defrags;
1108686031edSTom Erickson 	uint64_t kms_scans;
1109b5fca8f8Stomee 	uint64_t kms_scan_depot_ws_reaps;
1110b5fca8f8Stomee 	uint64_t kms_debug_reaps;
1111686031edSTom Erickson 	uint64_t kms_debug_scans;
1112b5fca8f8Stomee } kmem_move_stats;
1113b5fca8f8Stomee #endif	/* KMEM_STATS */
1114b5fca8f8Stomee 
1115b5fca8f8Stomee /* consolidator knobs */
1116b5fca8f8Stomee static boolean_t kmem_move_noreap;
1117b5fca8f8Stomee static boolean_t kmem_move_blocked;
1118b5fca8f8Stomee static boolean_t kmem_move_fulltilt;
1119b5fca8f8Stomee static boolean_t kmem_move_any_partial;
1120b5fca8f8Stomee 
1121b5fca8f8Stomee #ifdef	DEBUG
1122b5fca8f8Stomee /*
1123686031edSTom Erickson  * kmem consolidator debug tunables:
1124b5fca8f8Stomee  * Ensure code coverage by occasionally running the consolidator even when the
1125b5fca8f8Stomee  * caches are not fragmented (they may never be). These intervals are mean time
1126b5fca8f8Stomee  * in cache maintenance intervals (kmem_cache_update).
1127b5fca8f8Stomee  */
1128686031edSTom Erickson uint32_t kmem_mtb_move = 60;	/* defrag 1 slab (~15min) */
1129686031edSTom Erickson uint32_t kmem_mtb_reap = 1800;	/* defrag all slabs (~7.5hrs) */
1130b5fca8f8Stomee #endif	/* DEBUG */
1131b5fca8f8Stomee 
1132b5fca8f8Stomee static kmem_cache_t	*kmem_defrag_cache;
1133b5fca8f8Stomee static kmem_cache_t	*kmem_move_cache;
1134b5fca8f8Stomee static taskq_t		*kmem_move_taskq;
1135b5fca8f8Stomee 
1136b5fca8f8Stomee static void kmem_cache_scan(kmem_cache_t *);
1137b5fca8f8Stomee static void kmem_cache_defrag(kmem_cache_t *);
1138b942e89bSDavid Valin static void kmem_slab_prefill(kmem_cache_t *, kmem_slab_t *);
1139b5fca8f8Stomee 
1140b5fca8f8Stomee 
11417c478bd9Sstevel@tonic-gate kmem_log_header_t	*kmem_transaction_log;
11427c478bd9Sstevel@tonic-gate kmem_log_header_t	*kmem_content_log;
11437c478bd9Sstevel@tonic-gate kmem_log_header_t	*kmem_failure_log;
11447c478bd9Sstevel@tonic-gate kmem_log_header_t	*kmem_slab_log;
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate static int		kmem_lite_count; /* # of PCs in kmem_buftag_lite_t */
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate #define	KMEM_BUFTAG_LITE_ENTER(bt, count, caller)			\
11497c478bd9Sstevel@tonic-gate 	if ((count) > 0) {						\
11507c478bd9Sstevel@tonic-gate 		pc_t *_s = ((kmem_buftag_lite_t *)(bt))->bt_history;	\
11517c478bd9Sstevel@tonic-gate 		pc_t *_e;						\
11527c478bd9Sstevel@tonic-gate 		/* memmove() the old entries down one notch */		\
11537c478bd9Sstevel@tonic-gate 		for (_e = &_s[(count) - 1]; _e > _s; _e--)		\
11547c478bd9Sstevel@tonic-gate 			*_e = *(_e - 1);				\
11557c478bd9Sstevel@tonic-gate 		*_s = (uintptr_t)(caller);				\
11567c478bd9Sstevel@tonic-gate 	}
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate #define	KMERR_MODIFIED	0	/* buffer modified while on freelist */
11597c478bd9Sstevel@tonic-gate #define	KMERR_REDZONE	1	/* redzone violation (write past end of buf) */
11607c478bd9Sstevel@tonic-gate #define	KMERR_DUPFREE	2	/* freed a buffer twice */
11617c478bd9Sstevel@tonic-gate #define	KMERR_BADADDR	3	/* freed a bad (unallocated) address */
11627c478bd9Sstevel@tonic-gate #define	KMERR_BADBUFTAG	4	/* buftag corrupted */
11637c478bd9Sstevel@tonic-gate #define	KMERR_BADBUFCTL	5	/* bufctl corrupted */
11647c478bd9Sstevel@tonic-gate #define	KMERR_BADCACHE	6	/* freed a buffer to the wrong cache */
11657c478bd9Sstevel@tonic-gate #define	KMERR_BADSIZE	7	/* alloc size != free size */
11667c478bd9Sstevel@tonic-gate #define	KMERR_BADBASE	8	/* buffer base address wrong */
11677c478bd9Sstevel@tonic-gate 
11687c478bd9Sstevel@tonic-gate struct {
11697c478bd9Sstevel@tonic-gate 	hrtime_t	kmp_timestamp;	/* timestamp of panic */
11707c478bd9Sstevel@tonic-gate 	int		kmp_error;	/* type of kmem error */
11717c478bd9Sstevel@tonic-gate 	void		*kmp_buffer;	/* buffer that induced panic */
11727c478bd9Sstevel@tonic-gate 	void		*kmp_realbuf;	/* real start address for buffer */
11737c478bd9Sstevel@tonic-gate 	kmem_cache_t	*kmp_cache;	/* buffer's cache according to client */
11747c478bd9Sstevel@tonic-gate 	kmem_cache_t	*kmp_realcache;	/* actual cache containing buffer */
11757c478bd9Sstevel@tonic-gate 	kmem_slab_t	*kmp_slab;	/* slab accoring to kmem_findslab() */
11767c478bd9Sstevel@tonic-gate 	kmem_bufctl_t	*kmp_bufctl;	/* bufctl */
11777c478bd9Sstevel@tonic-gate } kmem_panic_info;
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate 
11807c478bd9Sstevel@tonic-gate static void
11817c478bd9Sstevel@tonic-gate copy_pattern(uint64_t pattern, void *buf_arg, size_t size)
11827c478bd9Sstevel@tonic-gate {
11837c478bd9Sstevel@tonic-gate 	uint64_t *bufend = (uint64_t *)((char *)buf_arg + size);
11847c478bd9Sstevel@tonic-gate 	uint64_t *buf = buf_arg;
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate 	while (buf < bufend)
11877c478bd9Sstevel@tonic-gate 		*buf++ = pattern;
11887c478bd9Sstevel@tonic-gate }
11897c478bd9Sstevel@tonic-gate 
11907c478bd9Sstevel@tonic-gate static void *
11917c478bd9Sstevel@tonic-gate verify_pattern(uint64_t pattern, void *buf_arg, size_t size)
11927c478bd9Sstevel@tonic-gate {
11937c478bd9Sstevel@tonic-gate 	uint64_t *bufend = (uint64_t *)((char *)buf_arg + size);
11947c478bd9Sstevel@tonic-gate 	uint64_t *buf;
11957c478bd9Sstevel@tonic-gate 
11967c478bd9Sstevel@tonic-gate 	for (buf = buf_arg; buf < bufend; buf++)
11977c478bd9Sstevel@tonic-gate 		if (*buf != pattern)
11987c478bd9Sstevel@tonic-gate 			return (buf);
11997c478bd9Sstevel@tonic-gate 	return (NULL);
12007c478bd9Sstevel@tonic-gate }
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate static void *
12037c478bd9Sstevel@tonic-gate verify_and_copy_pattern(uint64_t old, uint64_t new, void *buf_arg, size_t size)
12047c478bd9Sstevel@tonic-gate {
12057c478bd9Sstevel@tonic-gate 	uint64_t *bufend = (uint64_t *)((char *)buf_arg + size);
12067c478bd9Sstevel@tonic-gate 	uint64_t *buf;
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate 	for (buf = buf_arg; buf < bufend; buf++) {
12097c478bd9Sstevel@tonic-gate 		if (*buf != old) {
12107c478bd9Sstevel@tonic-gate 			copy_pattern(old, buf_arg,
12117c478bd9Sstevel@tonic-gate 			    (char *)buf - (char *)buf_arg);
12127c478bd9Sstevel@tonic-gate 			return (buf);
12137c478bd9Sstevel@tonic-gate 		}
12147c478bd9Sstevel@tonic-gate 		*buf = new;
12157c478bd9Sstevel@tonic-gate 	}
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate 	return (NULL);
12187c478bd9Sstevel@tonic-gate }
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate static void
12217c478bd9Sstevel@tonic-gate kmem_cache_applyall(void (*func)(kmem_cache_t *), taskq_t *tq, int tqflag)
12227c478bd9Sstevel@tonic-gate {
12237c478bd9Sstevel@tonic-gate 	kmem_cache_t *cp;
12247c478bd9Sstevel@tonic-gate 
12257c478bd9Sstevel@tonic-gate 	mutex_enter(&kmem_cache_lock);
1226b5fca8f8Stomee 	for (cp = list_head(&kmem_caches); cp != NULL;
1227b5fca8f8Stomee 	    cp = list_next(&kmem_caches, cp))
12287c478bd9Sstevel@tonic-gate 		if (tq != NULL)
12297c478bd9Sstevel@tonic-gate 			(void) taskq_dispatch(tq, (task_func_t *)func, cp,
12307c478bd9Sstevel@tonic-gate 			    tqflag);
12317c478bd9Sstevel@tonic-gate 		else
12327c478bd9Sstevel@tonic-gate 			func(cp);
12337c478bd9Sstevel@tonic-gate 	mutex_exit(&kmem_cache_lock);
12347c478bd9Sstevel@tonic-gate }
12357c478bd9Sstevel@tonic-gate 
12367c478bd9Sstevel@tonic-gate static void
12377c478bd9Sstevel@tonic-gate kmem_cache_applyall_id(void (*func)(kmem_cache_t *), taskq_t *tq, int tqflag)
12387c478bd9Sstevel@tonic-gate {
12397c478bd9Sstevel@tonic-gate 	kmem_cache_t *cp;
12407c478bd9Sstevel@tonic-gate 
12417c478bd9Sstevel@tonic-gate 	mutex_enter(&kmem_cache_lock);
1242b5fca8f8Stomee 	for (cp = list_head(&kmem_caches); cp != NULL;
1243b5fca8f8Stomee 	    cp = list_next(&kmem_caches, cp)) {
12447c478bd9Sstevel@tonic-gate 		if (!(cp->cache_cflags & KMC_IDENTIFIER))
12457c478bd9Sstevel@tonic-gate 			continue;
12467c478bd9Sstevel@tonic-gate 		if (tq != NULL)
12477c478bd9Sstevel@tonic-gate 			(void) taskq_dispatch(tq, (task_func_t *)func, cp,
12487c478bd9Sstevel@tonic-gate 			    tqflag);
12497c478bd9Sstevel@tonic-gate 		else
12507c478bd9Sstevel@tonic-gate 			func(cp);
12517c478bd9Sstevel@tonic-gate 	}
12527c478bd9Sstevel@tonic-gate 	mutex_exit(&kmem_cache_lock);
12537c478bd9Sstevel@tonic-gate }
12547c478bd9Sstevel@tonic-gate 
12557c478bd9Sstevel@tonic-gate /*
12567c478bd9Sstevel@tonic-gate  * Debugging support.  Given a buffer address, find its slab.
12577c478bd9Sstevel@tonic-gate  */
12587c478bd9Sstevel@tonic-gate static kmem_slab_t *
12597c478bd9Sstevel@tonic-gate kmem_findslab(kmem_cache_t *cp, void *buf)
12607c478bd9Sstevel@tonic-gate {
12617c478bd9Sstevel@tonic-gate 	kmem_slab_t *sp;
12627c478bd9Sstevel@tonic-gate 
12637c478bd9Sstevel@tonic-gate 	mutex_enter(&cp->cache_lock);
1264b5fca8f8Stomee 	for (sp = list_head(&cp->cache_complete_slabs); sp != NULL;
1265b5fca8f8Stomee 	    sp = list_next(&cp->cache_complete_slabs, sp)) {
1266b5fca8f8Stomee 		if (KMEM_SLAB_MEMBER(sp, buf)) {
1267b5fca8f8Stomee 			mutex_exit(&cp->cache_lock);
1268b5fca8f8Stomee 			return (sp);
1269b5fca8f8Stomee 		}
1270b5fca8f8Stomee 	}
1271b5fca8f8Stomee 	for (sp = avl_first(&cp->cache_partial_slabs); sp != NULL;
1272b5fca8f8Stomee 	    sp = AVL_NEXT(&cp->cache_partial_slabs, sp)) {
12737c478bd9Sstevel@tonic-gate 		if (KMEM_SLAB_MEMBER(sp, buf)) {
12747c478bd9Sstevel@tonic-gate 			mutex_exit(&cp->cache_lock);
12757c478bd9Sstevel@tonic-gate 			return (sp);
12767c478bd9Sstevel@tonic-gate 		}
12777c478bd9Sstevel@tonic-gate 	}
12787c478bd9Sstevel@tonic-gate 	mutex_exit(&cp->cache_lock);
12797c478bd9Sstevel@tonic-gate 
12807c478bd9Sstevel@tonic-gate 	return (NULL);
12817c478bd9Sstevel@tonic-gate }
12827c478bd9Sstevel@tonic-gate 
12837c478bd9Sstevel@tonic-gate static void
12847c478bd9Sstevel@tonic-gate kmem_error(int error, kmem_cache_t *cparg, void *bufarg)
12857c478bd9Sstevel@tonic-gate {
12867c478bd9Sstevel@tonic-gate 	kmem_buftag_t *btp = NULL;
12877c478bd9Sstevel@tonic-gate 	kmem_bufctl_t *bcp = NULL;
12887c478bd9Sstevel@tonic-gate 	kmem_cache_t *cp = cparg;
12897c478bd9Sstevel@tonic-gate 	kmem_slab_t *sp;
12907c478bd9Sstevel@tonic-gate 	uint64_t *off;
12917c478bd9Sstevel@tonic-gate 	void *buf = bufarg;
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate 	kmem_logging = 0;	/* stop logging when a bad thing happens */
12947c478bd9Sstevel@tonic-gate 
12957c478bd9Sstevel@tonic-gate 	kmem_panic_info.kmp_timestamp = gethrtime();
12967c478bd9Sstevel@tonic-gate 
12977c478bd9Sstevel@tonic-gate 	sp = kmem_findslab(cp, buf);
12987c478bd9Sstevel@tonic-gate 	if (sp == NULL) {
1299b5fca8f8Stomee 		for (cp = list_tail(&kmem_caches); cp != NULL;
1300b5fca8f8Stomee 		    cp = list_prev(&kmem_caches, cp)) {
13017c478bd9Sstevel@tonic-gate 			if ((sp = kmem_findslab(cp, buf)) != NULL)
13027c478bd9Sstevel@tonic-gate 				break;
13037c478bd9Sstevel@tonic-gate 		}
13047c478bd9Sstevel@tonic-gate 	}
13057c478bd9Sstevel@tonic-gate 
13067c478bd9Sstevel@tonic-gate 	if (sp == NULL) {
13077c478bd9Sstevel@tonic-gate 		cp = NULL;
13087c478bd9Sstevel@tonic-gate 		error = KMERR_BADADDR;
13097c478bd9Sstevel@tonic-gate 	} else {
13107c478bd9Sstevel@tonic-gate 		if (cp != cparg)
13117c478bd9Sstevel@tonic-gate 			error = KMERR_BADCACHE;
13127c478bd9Sstevel@tonic-gate 		else
13137c478bd9Sstevel@tonic-gate 			buf = (char *)bufarg - ((uintptr_t)bufarg -
13147c478bd9Sstevel@tonic-gate 			    (uintptr_t)sp->slab_base) % cp->cache_chunksize;
13157c478bd9Sstevel@tonic-gate 		if (buf != bufarg)
13167c478bd9Sstevel@tonic-gate 			error = KMERR_BADBASE;
13177c478bd9Sstevel@tonic-gate 		if (cp->cache_flags & KMF_BUFTAG)
13187c478bd9Sstevel@tonic-gate 			btp = KMEM_BUFTAG(cp, buf);
13197c478bd9Sstevel@tonic-gate 		if (cp->cache_flags & KMF_HASH) {
13207c478bd9Sstevel@tonic-gate 			mutex_enter(&cp->cache_lock);
13217c478bd9Sstevel@tonic-gate 			for (bcp = *KMEM_HASH(cp, buf); bcp; bcp = bcp->bc_next)
13227c478bd9Sstevel@tonic-gate 				if (bcp->bc_addr == buf)
13237c478bd9Sstevel@tonic-gate 					break;
13247c478bd9Sstevel@tonic-gate 			mutex_exit(&cp->cache_lock);
13257c478bd9Sstevel@tonic-gate 			if (bcp == NULL && btp != NULL)
13267c478bd9Sstevel@tonic-gate 				bcp = btp->bt_bufctl;
13277c478bd9Sstevel@tonic-gate 			if (kmem_findslab(cp->cache_bufctl_cache, bcp) ==
13287c478bd9Sstevel@tonic-gate 			    NULL || P2PHASE((uintptr_t)bcp, KMEM_ALIGN) ||
13297c478bd9Sstevel@tonic-gate 			    bcp->bc_addr != buf) {
13307c478bd9Sstevel@tonic-gate 				error = KMERR_BADBUFCTL;
13317c478bd9Sstevel@tonic-gate 				bcp = NULL;
13327c478bd9Sstevel@tonic-gate 			}
13337c478bd9Sstevel@tonic-gate 		}
13347c478bd9Sstevel@tonic-gate 	}
13357c478bd9Sstevel@tonic-gate 
13367c478bd9Sstevel@tonic-gate 	kmem_panic_info.kmp_error = error;
13377c478bd9Sstevel@tonic-gate 	kmem_panic_info.kmp_buffer = bufarg;
13387c478bd9Sstevel@tonic-gate 	kmem_panic_info.kmp_realbuf = buf;
13397c478bd9Sstevel@tonic-gate 	kmem_panic_info.kmp_cache = cparg;
13407c478bd9Sstevel@tonic-gate 	kmem_panic_info.kmp_realcache = cp;
13417c478bd9Sstevel@tonic-gate 	kmem_panic_info.kmp_slab = sp;
13427c478bd9Sstevel@tonic-gate 	kmem_panic_info.kmp_bufctl = bcp;
13437c478bd9Sstevel@tonic-gate 
13447c478bd9Sstevel@tonic-gate 	printf("kernel memory allocator: ");
13457c478bd9Sstevel@tonic-gate 
13467c478bd9Sstevel@tonic-gate 	switch (error) {
13477c478bd9Sstevel@tonic-gate 
13487c478bd9Sstevel@tonic-gate 	case KMERR_MODIFIED:
13497c478bd9Sstevel@tonic-gate 		printf("buffer modified after being freed\n");
13507c478bd9Sstevel@tonic-gate 		off = verify_pattern(KMEM_FREE_PATTERN, buf, cp->cache_verify);
13517c478bd9Sstevel@tonic-gate 		if (off == NULL)	/* shouldn't happen */
13527c478bd9Sstevel@tonic-gate 			off = buf;
13537c478bd9Sstevel@tonic-gate 		printf("modification occurred at offset 0x%lx "
13547c478bd9Sstevel@tonic-gate 		    "(0x%llx replaced by 0x%llx)\n",
13557c478bd9Sstevel@tonic-gate 		    (uintptr_t)off - (uintptr_t)buf,
13567c478bd9Sstevel@tonic-gate 		    (longlong_t)KMEM_FREE_PATTERN, (longlong_t)*off);
13577c478bd9Sstevel@tonic-gate 		break;
13587c478bd9Sstevel@tonic-gate 
13597c478bd9Sstevel@tonic-gate 	case KMERR_REDZONE:
13607c478bd9Sstevel@tonic-gate 		printf("redzone violation: write past end of buffer\n");
13617c478bd9Sstevel@tonic-gate 		break;
13627c478bd9Sstevel@tonic-gate 
13637c478bd9Sstevel@tonic-gate 	case KMERR_BADADDR:
13647c478bd9Sstevel@tonic-gate 		printf("invalid free: buffer not in cache\n");
13657c478bd9Sstevel@tonic-gate 		break;
13667c478bd9Sstevel@tonic-gate 
13677c478bd9Sstevel@tonic-gate 	case KMERR_DUPFREE:
13687c478bd9Sstevel@tonic-gate 		printf("duplicate free: buffer freed twice\n");
13697c478bd9Sstevel@tonic-gate 		break;
13707c478bd9Sstevel@tonic-gate 
13717c478bd9Sstevel@tonic-gate 	case KMERR_BADBUFTAG:
13727c478bd9Sstevel@tonic-gate 		printf("boundary tag corrupted\n");
13737c478bd9Sstevel@tonic-gate 		printf("bcp ^ bxstat = %lx, should be %lx\n",
13747c478bd9Sstevel@tonic-gate 		    (intptr_t)btp->bt_bufctl ^ btp->bt_bxstat,
13757c478bd9Sstevel@tonic-gate 		    KMEM_BUFTAG_FREE);
13767c478bd9Sstevel@tonic-gate 		break;
13777c478bd9Sstevel@tonic-gate 
13787c478bd9Sstevel@tonic-gate 	case KMERR_BADBUFCTL:
13797c478bd9Sstevel@tonic-gate 		printf("bufctl corrupted\n");
13807c478bd9Sstevel@tonic-gate 		break;
13817c478bd9Sstevel@tonic-gate 
13827c478bd9Sstevel@tonic-gate 	case KMERR_BADCACHE:
13837c478bd9Sstevel@tonic-gate 		printf("buffer freed to wrong cache\n");
13847c478bd9Sstevel@tonic-gate 		printf("buffer was allocated from %s,\n", cp->cache_name);
13857c478bd9Sstevel@tonic-gate 		printf("caller attempting free to %s.\n", cparg->cache_name);
13867c478bd9Sstevel@tonic-gate 		break;
13877c478bd9Sstevel@tonic-gate 
13887c478bd9Sstevel@tonic-gate 	case KMERR_BADSIZE:
13897c478bd9Sstevel@tonic-gate 		printf("bad free: free size (%u) != alloc size (%u)\n",
13907c478bd9Sstevel@tonic-gate 		    KMEM_SIZE_DECODE(((uint32_t *)btp)[0]),
13917c478bd9Sstevel@tonic-gate 		    KMEM_SIZE_DECODE(((uint32_t *)btp)[1]));
13927c478bd9Sstevel@tonic-gate 		break;
13937c478bd9Sstevel@tonic-gate 
13947c478bd9Sstevel@tonic-gate 	case KMERR_BADBASE:
13957c478bd9Sstevel@tonic-gate 		printf("bad free: free address (%p) != alloc address (%p)\n",
13967c478bd9Sstevel@tonic-gate 		    bufarg, buf);
13977c478bd9Sstevel@tonic-gate 		break;
13987c478bd9Sstevel@tonic-gate 	}
13997c478bd9Sstevel@tonic-gate 
14007c478bd9Sstevel@tonic-gate 	printf("buffer=%p  bufctl=%p  cache: %s\n",
14017c478bd9Sstevel@tonic-gate 	    bufarg, (void *)bcp, cparg->cache_name);
14027c478bd9Sstevel@tonic-gate 
14037c478bd9Sstevel@tonic-gate 	if (bcp != NULL && (cp->cache_flags & KMF_AUDIT) &&
14047c478bd9Sstevel@tonic-gate 	    error != KMERR_BADBUFCTL) {
14057c478bd9Sstevel@tonic-gate 		int d;
14067c478bd9Sstevel@tonic-gate 		timestruc_t ts;
14077c478bd9Sstevel@tonic-gate 		kmem_bufctl_audit_t *bcap = (kmem_bufctl_audit_t *)bcp;
14087c478bd9Sstevel@tonic-gate 
14097c478bd9Sstevel@tonic-gate 		hrt2ts(kmem_panic_info.kmp_timestamp - bcap->bc_timestamp, &ts);
14107c478bd9Sstevel@tonic-gate 		printf("previous transaction on buffer %p:\n", buf);
14117c478bd9Sstevel@tonic-gate 		printf("thread=%p  time=T-%ld.%09ld  slab=%p  cache: %s\n",
14127c478bd9Sstevel@tonic-gate 		    (void *)bcap->bc_thread, ts.tv_sec, ts.tv_nsec,
14137c478bd9Sstevel@tonic-gate 		    (void *)sp, cp->cache_name);
14147c478bd9Sstevel@tonic-gate 		for (d = 0; d < MIN(bcap->bc_depth, KMEM_STACK_DEPTH); d++) {
14157c478bd9Sstevel@tonic-gate 			ulong_t off;
14167c478bd9Sstevel@tonic-gate 			char *sym = kobj_getsymname(bcap->bc_stack[d], &off);
14177c478bd9Sstevel@tonic-gate 			printf("%s+%lx\n", sym ? sym : "?", off);
14187c478bd9Sstevel@tonic-gate 		}
14197c478bd9Sstevel@tonic-gate 	}
14207c478bd9Sstevel@tonic-gate 	if (kmem_panic > 0)
14217c478bd9Sstevel@tonic-gate 		panic("kernel heap corruption detected");
14227c478bd9Sstevel@tonic-gate 	if (kmem_panic == 0)
14237c478bd9Sstevel@tonic-gate 		debug_enter(NULL);
14247c478bd9Sstevel@tonic-gate 	kmem_logging = 1;	/* resume logging */
14257c478bd9Sstevel@tonic-gate }
14267c478bd9Sstevel@tonic-gate 
14277c478bd9Sstevel@tonic-gate static kmem_log_header_t *
14287c478bd9Sstevel@tonic-gate kmem_log_init(size_t logsize)
14297c478bd9Sstevel@tonic-gate {
14307c478bd9Sstevel@tonic-gate 	kmem_log_header_t *lhp;
14317c478bd9Sstevel@tonic-gate 	int nchunks = 4 * max_ncpus;
14327c478bd9Sstevel@tonic-gate 	size_t lhsize = (size_t)&((kmem_log_header_t *)0)->lh_cpu[max_ncpus];
14337c478bd9Sstevel@tonic-gate 	int i;
14347c478bd9Sstevel@tonic-gate 
14357c478bd9Sstevel@tonic-gate 	/*
14367c478bd9Sstevel@tonic-gate 	 * Make sure that lhp->lh_cpu[] is nicely aligned
14377c478bd9Sstevel@tonic-gate 	 * to prevent false sharing of cache lines.
14387c478bd9Sstevel@tonic-gate 	 */
14397c478bd9Sstevel@tonic-gate 	lhsize = P2ROUNDUP(lhsize, KMEM_ALIGN);
14407c478bd9Sstevel@tonic-gate 	lhp = vmem_xalloc(kmem_log_arena, lhsize, 64, P2NPHASE(lhsize, 64), 0,
14417c478bd9Sstevel@tonic-gate 	    NULL, NULL, VM_SLEEP);
14427c478bd9Sstevel@tonic-gate 	bzero(lhp, lhsize);
14437c478bd9Sstevel@tonic-gate 
14447c478bd9Sstevel@tonic-gate 	mutex_init(&lhp->lh_lock, NULL, MUTEX_DEFAULT, NULL);
14457c478bd9Sstevel@tonic-gate 	lhp->lh_nchunks = nchunks;
14467c478bd9Sstevel@tonic-gate 	lhp->lh_chunksize = P2ROUNDUP(logsize / nchunks + 1, PAGESIZE);
14477c478bd9Sstevel@tonic-gate 	lhp->lh_base = vmem_alloc(kmem_log_arena,
14487c478bd9Sstevel@tonic-gate 	    lhp->lh_chunksize * nchunks, VM_SLEEP);
14497c478bd9Sstevel@tonic-gate 	lhp->lh_free = vmem_alloc(kmem_log_arena,
14507c478bd9Sstevel@tonic-gate 	    nchunks * sizeof (int), VM_SLEEP);
14517c478bd9Sstevel@tonic-gate 	bzero(lhp->lh_base, lhp->lh_chunksize * nchunks);
14527c478bd9Sstevel@tonic-gate 
14537c478bd9Sstevel@tonic-gate 	for (i = 0; i < max_ncpus; i++) {
14547c478bd9Sstevel@tonic-gate 		kmem_cpu_log_header_t *clhp = &lhp->lh_cpu[i];
14557c478bd9Sstevel@tonic-gate 		mutex_init(&clhp->clh_lock, NULL, MUTEX_DEFAULT, NULL);
14567c478bd9Sstevel@tonic-gate 		clhp->clh_chunk = i;
14577c478bd9Sstevel@tonic-gate 	}
14587c478bd9Sstevel@tonic-gate 
14597c478bd9Sstevel@tonic-gate 	for (i = max_ncpus; i < nchunks; i++)
14607c478bd9Sstevel@tonic-gate 		lhp->lh_free[i] = i;
14617c478bd9Sstevel@tonic-gate 
14627c478bd9Sstevel@tonic-gate 	lhp->lh_head = max_ncpus;
14637c478bd9Sstevel@tonic-gate 	lhp->lh_tail = 0;
14647c478bd9Sstevel@tonic-gate 
14657c478bd9Sstevel@tonic-gate 	return (lhp);
14667c478bd9Sstevel@tonic-gate }
14677c478bd9Sstevel@tonic-gate 
14687c478bd9Sstevel@tonic-gate static void *
14697c478bd9Sstevel@tonic-gate kmem_log_enter(kmem_log_header_t *lhp, void *data, size_t size)
14707c478bd9Sstevel@tonic-gate {
14717c478bd9Sstevel@tonic-gate 	void *logspace;
14727c478bd9Sstevel@tonic-gate 	kmem_cpu_log_header_t *clhp = &lhp->lh_cpu[CPU->cpu_seqid];
14737c478bd9Sstevel@tonic-gate 
14747c478bd9Sstevel@tonic-gate 	if (lhp == NULL || kmem_logging == 0 || panicstr)
14757c478bd9Sstevel@tonic-gate 		return (NULL);
14767c478bd9Sstevel@tonic-gate 
14777c478bd9Sstevel@tonic-gate 	mutex_enter(&clhp->clh_lock);
14787c478bd9Sstevel@tonic-gate 	clhp->clh_hits++;
14797c478bd9Sstevel@tonic-gate 	if (size > clhp->clh_avail) {
14807c478bd9Sstevel@tonic-gate 		mutex_enter(&lhp->lh_lock);
14817c478bd9Sstevel@tonic-gate 		lhp->lh_hits++;
14827c478bd9Sstevel@tonic-gate 		lhp->lh_free[lhp->lh_tail] = clhp->clh_chunk;
14837c478bd9Sstevel@tonic-gate 		lhp->lh_tail = (lhp->lh_tail + 1) % lhp->lh_nchunks;
14847c478bd9Sstevel@tonic-gate 		clhp->clh_chunk = lhp->lh_free[lhp->lh_head];
14857c478bd9Sstevel@tonic-gate 		lhp->lh_head = (lhp->lh_head + 1) % lhp->lh_nchunks;
14867c478bd9Sstevel@tonic-gate 		clhp->clh_current = lhp->lh_base +
14877c478bd9Sstevel@tonic-gate 		    clhp->clh_chunk * lhp->lh_chunksize;
14887c478bd9Sstevel@tonic-gate 		clhp->clh_avail = lhp->lh_chunksize;
14897c478bd9Sstevel@tonic-gate 		if (size > lhp->lh_chunksize)
14907c478bd9Sstevel@tonic-gate 			size = lhp->lh_chunksize;
14917c478bd9Sstevel@tonic-gate 		mutex_exit(&lhp->lh_lock);
14927c478bd9Sstevel@tonic-gate 	}
14937c478bd9Sstevel@tonic-gate 	logspace = clhp->clh_current;
14947c478bd9Sstevel@tonic-gate 	clhp->clh_current += size;
14957c478bd9Sstevel@tonic-gate 	clhp->clh_avail -= size;
14967c478bd9Sstevel@tonic-gate 	bcopy(data, logspace, size);
14977c478bd9Sstevel@tonic-gate 	mutex_exit(&clhp->clh_lock);
14987c478bd9Sstevel@tonic-gate 	return (logspace);
14997c478bd9Sstevel@tonic-gate }
15007c478bd9Sstevel@tonic-gate 
15017c478bd9Sstevel@tonic-gate #define	KMEM_AUDIT(lp, cp, bcp)						\
15027c478bd9Sstevel@tonic-gate {									\
15037c478bd9Sstevel@tonic-gate 	kmem_bufctl_audit_t *_bcp = (kmem_bufctl_audit_t *)(bcp);	\
15047c478bd9Sstevel@tonic-gate 	_bcp->bc_timestamp = gethrtime();				\
15057c478bd9Sstevel@tonic-gate 	_bcp->bc_thread = curthread;					\
15067c478bd9Sstevel@tonic-gate 	_bcp->bc_depth = getpcstack(_bcp->bc_stack, KMEM_STACK_DEPTH);	\
15077c478bd9Sstevel@tonic-gate 	_bcp->bc_lastlog = kmem_log_enter((lp), _bcp, sizeof (*_bcp));	\
15087c478bd9Sstevel@tonic-gate }
15097c478bd9Sstevel@tonic-gate 
15107c478bd9Sstevel@tonic-gate static void
15117c478bd9Sstevel@tonic-gate kmem_log_event(kmem_log_header_t *lp, kmem_cache_t *cp,
15127c478bd9Sstevel@tonic-gate     kmem_slab_t *sp, void *addr)
15137c478bd9Sstevel@tonic-gate {
15147c478bd9Sstevel@tonic-gate 	kmem_bufctl_audit_t bca;
15157c478bd9Sstevel@tonic-gate 
15167c478bd9Sstevel@tonic-gate 	bzero(&bca, sizeof (kmem_bufctl_audit_t));
15177c478bd9Sstevel@tonic-gate 	bca.bc_addr = addr;
15187c478bd9Sstevel@tonic-gate 	bca.bc_slab = sp;
15197c478bd9Sstevel@tonic-gate 	bca.bc_cache = cp;
15207c478bd9Sstevel@tonic-gate 	KMEM_AUDIT(lp, cp, &bca);
15217c478bd9Sstevel@tonic-gate }
15227c478bd9Sstevel@tonic-gate 
15237c478bd9Sstevel@tonic-gate /*
15247c478bd9Sstevel@tonic-gate  * Create a new slab for cache cp.
15257c478bd9Sstevel@tonic-gate  */
15267c478bd9Sstevel@tonic-gate static kmem_slab_t *
15277c478bd9Sstevel@tonic-gate kmem_slab_create(kmem_cache_t *cp, int kmflag)
15287c478bd9Sstevel@tonic-gate {
15297c478bd9Sstevel@tonic-gate 	size_t slabsize = cp->cache_slabsize;
15307c478bd9Sstevel@tonic-gate 	size_t chunksize = cp->cache_chunksize;
15317c478bd9Sstevel@tonic-gate 	int cache_flags = cp->cache_flags;
15327c478bd9Sstevel@tonic-gate 	size_t color, chunks;
15337c478bd9Sstevel@tonic-gate 	char *buf, *slab;
15347c478bd9Sstevel@tonic-gate 	kmem_slab_t *sp;
15357c478bd9Sstevel@tonic-gate 	kmem_bufctl_t *bcp;
15367c478bd9Sstevel@tonic-gate 	vmem_t *vmp = cp->cache_arena;
15377c478bd9Sstevel@tonic-gate 
1538b5fca8f8Stomee 	ASSERT(MUTEX_NOT_HELD(&cp->cache_lock));
1539b5fca8f8Stomee 
15407c478bd9Sstevel@tonic-gate 	color = cp->cache_color + cp->cache_align;
15417c478bd9Sstevel@tonic-gate 	if (color > cp->cache_maxcolor)
15427c478bd9Sstevel@tonic-gate 		color = cp->cache_mincolor;
15437c478bd9Sstevel@tonic-gate 	cp->cache_color = color;
15447c478bd9Sstevel@tonic-gate 
15457c478bd9Sstevel@tonic-gate 	slab = vmem_alloc(vmp, slabsize, kmflag & KM_VMFLAGS);
15467c478bd9Sstevel@tonic-gate 
15477c478bd9Sstevel@tonic-gate 	if (slab == NULL)
15487c478bd9Sstevel@tonic-gate 		goto vmem_alloc_failure;
15497c478bd9Sstevel@tonic-gate 
15507c478bd9Sstevel@tonic-gate 	ASSERT(P2PHASE((uintptr_t)slab, vmp->vm_quantum) == 0);
15517c478bd9Sstevel@tonic-gate 
1552b5fca8f8Stomee 	/*
1553b5fca8f8Stomee 	 * Reverify what was already checked in kmem_cache_set_move(), since the
1554b5fca8f8Stomee 	 * consolidator depends (for correctness) on slabs being initialized
1555b5fca8f8Stomee 	 * with the 0xbaddcafe memory pattern (setting a low order bit usable by
1556b5fca8f8Stomee 	 * clients to distinguish uninitialized memory from known objects).
1557b5fca8f8Stomee 	 */
1558b5fca8f8Stomee 	ASSERT((cp->cache_move == NULL) || !(cp->cache_cflags & KMC_NOTOUCH));
15597c478bd9Sstevel@tonic-gate 	if (!(cp->cache_cflags & KMC_NOTOUCH))
15607c478bd9Sstevel@tonic-gate 		copy_pattern(KMEM_UNINITIALIZED_PATTERN, slab, slabsize);
15617c478bd9Sstevel@tonic-gate 
15627c478bd9Sstevel@tonic-gate 	if (cache_flags & KMF_HASH) {
15637c478bd9Sstevel@tonic-gate 		if ((sp = kmem_cache_alloc(kmem_slab_cache, kmflag)) == NULL)
15647c478bd9Sstevel@tonic-gate 			goto slab_alloc_failure;
15657c478bd9Sstevel@tonic-gate 		chunks = (slabsize - color) / chunksize;
15667c478bd9Sstevel@tonic-gate 	} else {
15677c478bd9Sstevel@tonic-gate 		sp = KMEM_SLAB(cp, slab);
15687c478bd9Sstevel@tonic-gate 		chunks = (slabsize - sizeof (kmem_slab_t) - color) / chunksize;
15697c478bd9Sstevel@tonic-gate 	}
15707c478bd9Sstevel@tonic-gate 
15717c478bd9Sstevel@tonic-gate 	sp->slab_cache	= cp;
15727c478bd9Sstevel@tonic-gate 	sp->slab_head	= NULL;
15737c478bd9Sstevel@tonic-gate 	sp->slab_refcnt	= 0;
15747c478bd9Sstevel@tonic-gate 	sp->slab_base	= buf = slab + color;
15757c478bd9Sstevel@tonic-gate 	sp->slab_chunks	= chunks;
1576b5fca8f8Stomee 	sp->slab_stuck_offset = (uint32_t)-1;
1577b5fca8f8Stomee 	sp->slab_later_count = 0;
1578b5fca8f8Stomee 	sp->slab_flags = 0;
15797c478bd9Sstevel@tonic-gate 
15807c478bd9Sstevel@tonic-gate 	ASSERT(chunks > 0);
15817c478bd9Sstevel@tonic-gate 	while (chunks-- != 0) {
15827c478bd9Sstevel@tonic-gate 		if (cache_flags & KMF_HASH) {
15837c478bd9Sstevel@tonic-gate 			bcp = kmem_cache_alloc(cp->cache_bufctl_cache, kmflag);
15847c478bd9Sstevel@tonic-gate 			if (bcp == NULL)
15857c478bd9Sstevel@tonic-gate 				goto bufctl_alloc_failure;
15867c478bd9Sstevel@tonic-gate 			if (cache_flags & KMF_AUDIT) {
15877c478bd9Sstevel@tonic-gate 				kmem_bufctl_audit_t *bcap =
15887c478bd9Sstevel@tonic-gate 				    (kmem_bufctl_audit_t *)bcp;
15897c478bd9Sstevel@tonic-gate 				bzero(bcap, sizeof (kmem_bufctl_audit_t));
15907c478bd9Sstevel@tonic-gate 				bcap->bc_cache = cp;
15917c478bd9Sstevel@tonic-gate 			}
15927c478bd9Sstevel@tonic-gate 			bcp->bc_addr = buf;
15937c478bd9Sstevel@tonic-gate 			bcp->bc_slab = sp;
15947c478bd9Sstevel@tonic-gate 		} else {
15957c478bd9Sstevel@tonic-gate 			bcp = KMEM_BUFCTL(cp, buf);
15967c478bd9Sstevel@tonic-gate 		}
15977c478bd9Sstevel@tonic-gate 		if (cache_flags & KMF_BUFTAG) {
15987c478bd9Sstevel@tonic-gate 			kmem_buftag_t *btp = KMEM_BUFTAG(cp, buf);
15997c478bd9Sstevel@tonic-gate 			btp->bt_redzone = KMEM_REDZONE_PATTERN;
16007c478bd9Sstevel@tonic-gate 			btp->bt_bufctl = bcp;
16017c478bd9Sstevel@tonic-gate 			btp->bt_bxstat = (intptr_t)bcp ^ KMEM_BUFTAG_FREE;
16027c478bd9Sstevel@tonic-gate 			if (cache_flags & KMF_DEADBEEF) {
16037c478bd9Sstevel@tonic-gate 				copy_pattern(KMEM_FREE_PATTERN, buf,
16047c478bd9Sstevel@tonic-gate 				    cp->cache_verify);
16057c478bd9Sstevel@tonic-gate 			}
16067c478bd9Sstevel@tonic-gate 		}
16077c478bd9Sstevel@tonic-gate 		bcp->bc_next = sp->slab_head;
16087c478bd9Sstevel@tonic-gate 		sp->slab_head = bcp;
16097c478bd9Sstevel@tonic-gate 		buf += chunksize;
16107c478bd9Sstevel@tonic-gate 	}
16117c478bd9Sstevel@tonic-gate 
16127c478bd9Sstevel@tonic-gate 	kmem_log_event(kmem_slab_log, cp, sp, slab);
16137c478bd9Sstevel@tonic-gate 
16147c478bd9Sstevel@tonic-gate 	return (sp);
16157c478bd9Sstevel@tonic-gate 
16167c478bd9Sstevel@tonic-gate bufctl_alloc_failure:
16177c478bd9Sstevel@tonic-gate 
16187c478bd9Sstevel@tonic-gate 	while ((bcp = sp->slab_head) != NULL) {
16197c478bd9Sstevel@tonic-gate 		sp->slab_head = bcp->bc_next;
16207c478bd9Sstevel@tonic-gate 		kmem_cache_free(cp->cache_bufctl_cache, bcp);
16217c478bd9Sstevel@tonic-gate 	}
16227c478bd9Sstevel@tonic-gate 	kmem_cache_free(kmem_slab_cache, sp);
16237c478bd9Sstevel@tonic-gate 
16247c478bd9Sstevel@tonic-gate slab_alloc_failure:
16257c478bd9Sstevel@tonic-gate 
16267c478bd9Sstevel@tonic-gate 	vmem_free(vmp, slab, slabsize);
16277c478bd9Sstevel@tonic-gate 
16287c478bd9Sstevel@tonic-gate vmem_alloc_failure:
16297c478bd9Sstevel@tonic-gate 
16307c478bd9Sstevel@tonic-gate 	kmem_log_event(kmem_failure_log, cp, NULL, NULL);
16311a5e258fSJosef 'Jeff' Sipek 	atomic_inc_64(&cp->cache_alloc_fail);
16327c478bd9Sstevel@tonic-gate 
16337c478bd9Sstevel@tonic-gate 	return (NULL);
16347c478bd9Sstevel@tonic-gate }
16357c478bd9Sstevel@tonic-gate 
16367c478bd9Sstevel@tonic-gate /*
16377c478bd9Sstevel@tonic-gate  * Destroy a slab.
16387c478bd9Sstevel@tonic-gate  */
16397c478bd9Sstevel@tonic-gate static void
16407c478bd9Sstevel@tonic-gate kmem_slab_destroy(kmem_cache_t *cp, kmem_slab_t *sp)
16417c478bd9Sstevel@tonic-gate {
16427c478bd9Sstevel@tonic-gate 	vmem_t *vmp = cp->cache_arena;
16437c478bd9Sstevel@tonic-gate 	void *slab = (void *)P2ALIGN((uintptr_t)sp->slab_base, vmp->vm_quantum);
16447c478bd9Sstevel@tonic-gate 
1645b5fca8f8Stomee 	ASSERT(MUTEX_NOT_HELD(&cp->cache_lock));
1646b5fca8f8Stomee 	ASSERT(sp->slab_refcnt == 0);
1647b5fca8f8Stomee 
16487c478bd9Sstevel@tonic-gate 	if (cp->cache_flags & KMF_HASH) {
16497c478bd9Sstevel@tonic-gate 		kmem_bufctl_t *bcp;
16507c478bd9Sstevel@tonic-gate 		while ((bcp = sp->slab_head) != NULL) {
16517c478bd9Sstevel@tonic-gate 			sp->slab_head = bcp->bc_next;
16527c478bd9Sstevel@tonic-gate 			kmem_cache_free(cp->cache_bufctl_cache, bcp);
16537c478bd9Sstevel@tonic-gate 		}
16547c478bd9Sstevel@tonic-gate 		kmem_cache_free(kmem_slab_cache, sp);
16557c478bd9Sstevel@tonic-gate 	}
16567c478bd9Sstevel@tonic-gate 	vmem_free(vmp, slab, cp->cache_slabsize);
16577c478bd9Sstevel@tonic-gate }
16587c478bd9Sstevel@tonic-gate 
16597c478bd9Sstevel@tonic-gate static void *
1660b942e89bSDavid Valin kmem_slab_alloc_impl(kmem_cache_t *cp, kmem_slab_t *sp, boolean_t prefill)
16617c478bd9Sstevel@tonic-gate {
16627c478bd9Sstevel@tonic-gate 	kmem_bufctl_t *bcp, **hash_bucket;
16637c478bd9Sstevel@tonic-gate 	void *buf;
1664b942e89bSDavid Valin 	boolean_t new_slab = (sp->slab_refcnt == 0);
16657c478bd9Sstevel@tonic-gate 
1666b5fca8f8Stomee 	ASSERT(MUTEX_HELD(&cp->cache_lock));
16677c478bd9Sstevel@tonic-gate 	/*
1668b5fca8f8Stomee 	 * kmem_slab_alloc() drops cache_lock when it creates a new slab, so we
1669b5fca8f8Stomee 	 * can't ASSERT(avl_is_empty(&cp->cache_partial_slabs)) here when the
1670b942e89bSDavid Valin 	 * slab is newly created.
16717c478bd9Sstevel@tonic-gate 	 */
1672b942e89bSDavid Valin 	ASSERT(new_slab || (KMEM_SLAB_IS_PARTIAL(sp) &&
1673b5fca8f8Stomee 	    (sp == avl_first(&cp->cache_partial_slabs))));
1674b5fca8f8Stomee 	ASSERT(sp->slab_cache == cp);
16757c478bd9Sstevel@tonic-gate 
1676b5fca8f8Stomee 	cp->cache_slab_alloc++;
16779f1b636aStomee 	cp->cache_bufslab--;
16787c478bd9Sstevel@tonic-gate 	sp->slab_refcnt++;
16797c478bd9Sstevel@tonic-gate 
16807c478bd9Sstevel@tonic-gate 	bcp = sp->slab_head;
1681b942e89bSDavid Valin 	sp->slab_head = bcp->bc_next;
16827c478bd9Sstevel@tonic-gate 
16837c478bd9Sstevel@tonic-gate 	if (cp->cache_flags & KMF_HASH) {
16847c478bd9Sstevel@tonic-gate 		/*
16857c478bd9Sstevel@tonic-gate 		 * Add buffer to allocated-address hash table.
16867c478bd9Sstevel@tonic-gate 		 */
16877c478bd9Sstevel@tonic-gate 		buf = bcp->bc_addr;
16887c478bd9Sstevel@tonic-gate 		hash_bucket = KMEM_HASH(cp, buf);
16897c478bd9Sstevel@tonic-gate 		bcp->bc_next = *hash_bucket;
16907c478bd9Sstevel@tonic-gate 		*hash_bucket = bcp;
16917c478bd9Sstevel@tonic-gate 		if ((cp->cache_flags & (KMF_AUDIT | KMF_BUFTAG)) == KMF_AUDIT) {
16927c478bd9Sstevel@tonic-gate 			KMEM_AUDIT(kmem_transaction_log, cp, bcp);
16937c478bd9Sstevel@tonic-gate 		}
16947c478bd9Sstevel@tonic-gate 	} else {
16957c478bd9Sstevel@tonic-gate 		buf = KMEM_BUF(cp, bcp);
16967c478bd9Sstevel@tonic-gate 	}
16977c478bd9Sstevel@tonic-gate 
16987c478bd9Sstevel@tonic-gate 	ASSERT(KMEM_SLAB_MEMBER(sp, buf));
1699b942e89bSDavid Valin 
1700b942e89bSDavid Valin 	if (sp->slab_head == NULL) {
1701b942e89bSDavid Valin 		ASSERT(KMEM_SLAB_IS_ALL_USED(sp));
1702b942e89bSDavid Valin 		if (new_slab) {
1703b942e89bSDavid Valin 			ASSERT(sp->slab_chunks == 1);
1704b942e89bSDavid Valin 		} else {
1705b942e89bSDavid Valin 			ASSERT(sp->slab_chunks > 1); /* the slab was partial */
1706b942e89bSDavid Valin 			avl_remove(&cp->cache_partial_slabs, sp);
1707b942e89bSDavid Valin 			sp->slab_later_count = 0; /* clear history */
1708b942e89bSDavid Valin 			sp->slab_flags &= ~KMEM_SLAB_NOMOVE;
1709b942e89bSDavid Valin 			sp->slab_stuck_offset = (uint32_t)-1;
1710b942e89bSDavid Valin 		}
1711b942e89bSDavid Valin 		list_insert_head(&cp->cache_complete_slabs, sp);
1712b942e89bSDavid Valin 		cp->cache_complete_slab_count++;
1713b942e89bSDavid Valin 		return (buf);
1714b942e89bSDavid Valin 	}
1715b942e89bSDavid Valin 
1716b942e89bSDavid Valin 	ASSERT(KMEM_SLAB_IS_PARTIAL(sp));
1717b942e89bSDavid Valin 	/*
1718b942e89bSDavid Valin 	 * Peek to see if the magazine layer is enabled before
1719b942e89bSDavid Valin 	 * we prefill.  We're not holding the cpu cache lock,
1720b942e89bSDavid Valin 	 * so the peek could be wrong, but there's no harm in it.
1721b942e89bSDavid Valin 	 */
1722b942e89bSDavid Valin 	if (new_slab && prefill && (cp->cache_flags & KMF_PREFILL) &&
1723b942e89bSDavid Valin 	    (KMEM_CPU_CACHE(cp)->cc_magsize != 0))  {
1724b942e89bSDavid Valin 		kmem_slab_prefill(cp, sp);
1725b942e89bSDavid Valin 		return (buf);
1726b942e89bSDavid Valin 	}
1727b942e89bSDavid Valin 
1728b942e89bSDavid Valin 	if (new_slab) {
1729b942e89bSDavid Valin 		avl_add(&cp->cache_partial_slabs, sp);
1730b942e89bSDavid Valin 		return (buf);
1731b942e89bSDavid Valin 	}
1732b942e89bSDavid Valin 
1733b942e89bSDavid Valin 	/*
1734b942e89bSDavid Valin 	 * The slab is now more allocated than it was, so the
1735b942e89bSDavid Valin 	 * order remains unchanged.
1736b942e89bSDavid Valin 	 */
1737b942e89bSDavid Valin 	ASSERT(!avl_update(&cp->cache_partial_slabs, sp));
1738b5fca8f8Stomee 	return (buf);
1739b5fca8f8Stomee }
17407c478bd9Sstevel@tonic-gate 
1741b5fca8f8Stomee /*
1742b5fca8f8Stomee  * Allocate a raw (unconstructed) buffer from cp's slab layer.
1743b5fca8f8Stomee  */
1744b5fca8f8Stomee static void *
1745b5fca8f8Stomee kmem_slab_alloc(kmem_cache_t *cp, int kmflag)
1746b5fca8f8Stomee {
1747b5fca8f8Stomee 	kmem_slab_t *sp;
1748b5fca8f8Stomee 	void *buf;
17494d4c4c43STom Erickson 	boolean_t test_destructor;
1750b5fca8f8Stomee 
1751b5fca8f8Stomee 	mutex_enter(&cp->cache_lock);
17524d4c4c43STom Erickson 	test_destructor = (cp->cache_slab_alloc == 0);
1753b5fca8f8Stomee 	sp = avl_first(&cp->cache_partial_slabs);
1754b5fca8f8Stomee 	if (sp == NULL) {
1755b5fca8f8Stomee 		ASSERT(cp->cache_bufslab == 0);
1756b5fca8f8Stomee 
1757b5fca8f8Stomee 		/*
1758b5fca8f8Stomee 		 * The freelist is empty.  Create a new slab.
1759b5fca8f8Stomee 		 */
1760b5fca8f8Stomee 		mutex_exit(&cp->cache_lock);
1761b5fca8f8Stomee 		if ((sp = kmem_slab_create(cp, kmflag)) == NULL) {
1762b5fca8f8Stomee 			return (NULL);
1763b5fca8f8Stomee 		}
1764b5fca8f8Stomee 		mutex_enter(&cp->cache_lock);
1765b5fca8f8Stomee 		cp->cache_slab_create++;
1766b5fca8f8Stomee 		if ((cp->cache_buftotal += sp->slab_chunks) > cp->cache_bufmax)
1767b5fca8f8Stomee 			cp->cache_bufmax = cp->cache_buftotal;
1768b5fca8f8Stomee 		cp->cache_bufslab += sp->slab_chunks;
1769b5fca8f8Stomee 	}
1770b5fca8f8Stomee 
1771b942e89bSDavid Valin 	buf = kmem_slab_alloc_impl(cp, sp, B_TRUE);
1772b5fca8f8Stomee 	ASSERT((cp->cache_slab_create - cp->cache_slab_destroy) ==
1773b5fca8f8Stomee 	    (cp->cache_complete_slab_count +
1774b5fca8f8Stomee 	    avl_numnodes(&cp->cache_partial_slabs) +
1775b5fca8f8Stomee 	    (cp->cache_defrag == NULL ? 0 : cp->cache_defrag->kmd_deadcount)));
17767c478bd9Sstevel@tonic-gate 	mutex_exit(&cp->cache_lock);
17777c478bd9Sstevel@tonic-gate 
17784d4c4c43STom Erickson 	if (test_destructor && cp->cache_destructor != NULL) {
17794d4c4c43STom Erickson 		/*
17804d4c4c43STom Erickson 		 * On the first kmem_slab_alloc(), assert that it is valid to
17814d4c4c43STom Erickson 		 * call the destructor on a newly constructed object without any
17824d4c4c43STom Erickson 		 * client involvement.
17834d4c4c43STom Erickson 		 */
17844d4c4c43STom Erickson 		if ((cp->cache_constructor == NULL) ||
17854d4c4c43STom Erickson 		    cp->cache_constructor(buf, cp->cache_private,
17864d4c4c43STom Erickson 		    kmflag) == 0) {
17874d4c4c43STom Erickson 			cp->cache_destructor(buf, cp->cache_private);
17884d4c4c43STom Erickson 		}
17894d4c4c43STom Erickson 		copy_pattern(KMEM_UNINITIALIZED_PATTERN, buf,
17904d4c4c43STom Erickson 		    cp->cache_bufsize);
17914d4c4c43STom Erickson 		if (cp->cache_flags & KMF_DEADBEEF) {
17924d4c4c43STom Erickson 			copy_pattern(KMEM_FREE_PATTERN, buf, cp->cache_verify);
17934d4c4c43STom Erickson 		}
17944d4c4c43STom Erickson 	}
17954d4c4c43STom Erickson 
17967c478bd9Sstevel@tonic-gate 	return (buf);
17977c478bd9Sstevel@tonic-gate }
17987c478bd9Sstevel@tonic-gate 
1799b5fca8f8Stomee static void kmem_slab_move_yes(kmem_cache_t *, kmem_slab_t *, void *);
1800b5fca8f8Stomee 
18017c478bd9Sstevel@tonic-gate /*
18027c478bd9Sstevel@tonic-gate  * Free a raw (unconstructed) buffer to cp's slab layer.
18037c478bd9Sstevel@tonic-gate  */
18047c478bd9Sstevel@tonic-gate static void
18057c478bd9Sstevel@tonic-gate kmem_slab_free(kmem_cache_t *cp, void *buf)
18067c478bd9Sstevel@tonic-gate {
18077c478bd9Sstevel@tonic-gate 	kmem_slab_t *sp;
18087c478bd9Sstevel@tonic-gate 	kmem_bufctl_t *bcp, **prev_bcpp;
18097c478bd9Sstevel@tonic-gate 
18107c478bd9Sstevel@tonic-gate 	ASSERT(buf != NULL);
18117c478bd9Sstevel@tonic-gate 
18127c478bd9Sstevel@tonic-gate 	mutex_enter(&cp->cache_lock);
18137c478bd9Sstevel@tonic-gate 	cp->cache_slab_free++;
18147c478bd9Sstevel@tonic-gate 
18157c478bd9Sstevel@tonic-gate 	if (cp->cache_flags & KMF_HASH) {
18167c478bd9Sstevel@tonic-gate 		/*
18177c478bd9Sstevel@tonic-gate 		 * Look up buffer in allocated-address hash table.
18187c478bd9Sstevel@tonic-gate 		 */
18197c478bd9Sstevel@tonic-gate 		prev_bcpp = KMEM_HASH(cp, buf);
18207c478bd9Sstevel@tonic-gate 		while ((bcp = *prev_bcpp) != NULL) {
18217c478bd9Sstevel@tonic-gate 			if (bcp->bc_addr == buf) {
18227c478bd9Sstevel@tonic-gate 				*prev_bcpp = bcp->bc_next;
18237c478bd9Sstevel@tonic-gate 				sp = bcp->bc_slab;
18247c478bd9Sstevel@tonic-gate 				break;
18257c478bd9Sstevel@tonic-gate 			}
18267c478bd9Sstevel@tonic-gate 			cp->cache_lookup_depth++;
18277c478bd9Sstevel@tonic-gate 			prev_bcpp = &bcp->bc_next;
18287c478bd9Sstevel@tonic-gate 		}
18297c478bd9Sstevel@tonic-gate 	} else {
18307c478bd9Sstevel@tonic-gate 		bcp = KMEM_BUFCTL(cp, buf);
18317c478bd9Sstevel@tonic-gate 		sp = KMEM_SLAB(cp, buf);
18327c478bd9Sstevel@tonic-gate 	}
18337c478bd9Sstevel@tonic-gate 
18347c478bd9Sstevel@tonic-gate 	if (bcp == NULL || sp->slab_cache != cp || !KMEM_SLAB_MEMBER(sp, buf)) {
18357c478bd9Sstevel@tonic-gate 		mutex_exit(&cp->cache_lock);
18367c478bd9Sstevel@tonic-gate 		kmem_error(KMERR_BADADDR, cp, buf);
18377c478bd9Sstevel@tonic-gate 		return;
18387c478bd9Sstevel@tonic-gate 	}
18397c478bd9Sstevel@tonic-gate 
1840b5fca8f8Stomee 	if (KMEM_SLAB_OFFSET(sp, buf) == sp->slab_stuck_offset) {
1841b5fca8f8Stomee 		/*
1842b5fca8f8Stomee 		 * If this is the buffer that prevented the consolidator from
1843b5fca8f8Stomee 		 * clearing the slab, we can reset the slab flags now that the
1844b5fca8f8Stomee 		 * buffer is freed. (It makes sense to do this in
1845b5fca8f8Stomee 		 * kmem_cache_free(), where the client gives up ownership of the
1846b5fca8f8Stomee 		 * buffer, but on the hot path the test is too expensive.)
1847b5fca8f8Stomee 		 */
1848b5fca8f8Stomee 		kmem_slab_move_yes(cp, sp, buf);
1849b5fca8f8Stomee 	}
1850b5fca8f8Stomee 
18517c478bd9Sstevel@tonic-gate 	if ((cp->cache_flags & (KMF_AUDIT | KMF_BUFTAG)) == KMF_AUDIT) {
18527c478bd9Sstevel@tonic-gate 		if (cp->cache_flags & KMF_CONTENTS)
18537c478bd9Sstevel@tonic-gate 			((kmem_bufctl_audit_t *)bcp)->bc_contents =
18547c478bd9Sstevel@tonic-gate 			    kmem_log_enter(kmem_content_log, buf,
18557c478bd9Sstevel@tonic-gate 			    cp->cache_contents);
18567c478bd9Sstevel@tonic-gate 		KMEM_AUDIT(kmem_transaction_log, cp, bcp);
18577c478bd9Sstevel@tonic-gate 	}
18587c478bd9Sstevel@tonic-gate 
18597c478bd9Sstevel@tonic-gate 	bcp->bc_next = sp->slab_head;
18607c478bd9Sstevel@tonic-gate 	sp->slab_head = bcp;
18617c478bd9Sstevel@tonic-gate 
18629f1b636aStomee 	cp->cache_bufslab++;
18637c478bd9Sstevel@tonic-gate 	ASSERT(sp->slab_refcnt >= 1);
1864b5fca8f8Stomee 
18657c478bd9Sstevel@tonic-gate 	if (--sp->slab_refcnt == 0) {
18667c478bd9Sstevel@tonic-gate 		/*
18677c478bd9Sstevel@tonic-gate 		 * There are no outstanding allocations from this slab,
18687c478bd9Sstevel@tonic-gate 		 * so we can reclaim the memory.
18697c478bd9Sstevel@tonic-gate 		 */
1870b5fca8f8Stomee 		if (sp->slab_chunks == 1) {
1871b5fca8f8Stomee 			list_remove(&cp->cache_complete_slabs, sp);
1872b5fca8f8Stomee 			cp->cache_complete_slab_count--;
1873b5fca8f8Stomee 		} else {
1874b5fca8f8Stomee 			avl_remove(&cp->cache_partial_slabs, sp);
1875b5fca8f8Stomee 		}
1876b5fca8f8Stomee 
18777c478bd9Sstevel@tonic-gate 		cp->cache_buftotal -= sp->slab_chunks;
18789f1b636aStomee 		cp->cache_bufslab -= sp->slab_chunks;
1879b5fca8f8Stomee 		/*
1880b5fca8f8Stomee 		 * Defer releasing the slab to the virtual memory subsystem
1881b5fca8f8Stomee 		 * while there is a pending move callback, since we guarantee
1882b5fca8f8Stomee 		 * that buffers passed to the move callback have only been
1883b5fca8f8Stomee 		 * touched by kmem or by the client itself. Since the memory
1884b5fca8f8Stomee 		 * patterns baddcafe (uninitialized) and deadbeef (freed) both
1885b5fca8f8Stomee 		 * set at least one of the two lowest order bits, the client can
1886b5fca8f8Stomee 		 * test those bits in the move callback to determine whether or
1887b5fca8f8Stomee 		 * not it knows about the buffer (assuming that the client also
1888b5fca8f8Stomee 		 * sets one of those low order bits whenever it frees a buffer).
1889b5fca8f8Stomee 		 */
1890b5fca8f8Stomee 		if (cp->cache_defrag == NULL ||
1891b5fca8f8Stomee 		    (avl_is_empty(&cp->cache_defrag->kmd_moves_pending) &&
1892b5fca8f8Stomee 		    !(sp->slab_flags & KMEM_SLAB_MOVE_PENDING))) {
1893b5fca8f8Stomee 			cp->cache_slab_destroy++;
18947c478bd9Sstevel@tonic-gate 			mutex_exit(&cp->cache_lock);
18957c478bd9Sstevel@tonic-gate 			kmem_slab_destroy(cp, sp);
1896b5fca8f8Stomee 		} else {
1897b5fca8f8Stomee 			list_t *deadlist = &cp->cache_defrag->kmd_deadlist;
1898b5fca8f8Stomee 			/*
1899b5fca8f8Stomee 			 * Slabs are inserted at both ends of the deadlist to
1900b5fca8f8Stomee 			 * distinguish between slabs freed while move callbacks
1901b5fca8f8Stomee 			 * are pending (list head) and a slab freed while the
1902b5fca8f8Stomee 			 * lock is dropped in kmem_move_buffers() (list tail) so
1903b5fca8f8Stomee 			 * that in both cases slab_destroy() is called from the
1904b5fca8f8Stomee 			 * right context.
1905b5fca8f8Stomee 			 */
1906b5fca8f8Stomee 			if (sp->slab_flags & KMEM_SLAB_MOVE_PENDING) {
1907b5fca8f8Stomee 				list_insert_tail(deadlist, sp);
1908b5fca8f8Stomee 			} else {
1909b5fca8f8Stomee 				list_insert_head(deadlist, sp);
1910b5fca8f8Stomee 			}
1911b5fca8f8Stomee 			cp->cache_defrag->kmd_deadcount++;
1912b5fca8f8Stomee 			mutex_exit(&cp->cache_lock);
1913b5fca8f8Stomee 		}
19147c478bd9Sstevel@tonic-gate 		return;
19157c478bd9Sstevel@tonic-gate 	}
1916b5fca8f8Stomee 
1917b5fca8f8Stomee 	if (bcp->bc_next == NULL) {
1918b5fca8f8Stomee 		/* Transition the slab from completely allocated to partial. */
1919b5fca8f8Stomee 		ASSERT(sp->slab_refcnt == (sp->slab_chunks - 1));
1920b5fca8f8Stomee 		ASSERT(sp->slab_chunks > 1);
1921b5fca8f8Stomee 		list_remove(&cp->cache_complete_slabs, sp);
1922b5fca8f8Stomee 		cp->cache_complete_slab_count--;
1923b5fca8f8Stomee 		avl_add(&cp->cache_partial_slabs, sp);
1924b5fca8f8Stomee 	} else {
1925b5fca8f8Stomee #ifdef	DEBUG
1926b5fca8f8Stomee 		if (avl_update_gt(&cp->cache_partial_slabs, sp)) {
1927b5fca8f8Stomee 			KMEM_STAT_ADD(kmem_move_stats.kms_avl_update);
1928b5fca8f8Stomee 		} else {
1929b5fca8f8Stomee 			KMEM_STAT_ADD(kmem_move_stats.kms_avl_noupdate);
1930b5fca8f8Stomee 		}
1931b5fca8f8Stomee #else
1932b5fca8f8Stomee 		(void) avl_update_gt(&cp->cache_partial_slabs, sp);
1933b5fca8f8Stomee #endif
1934b5fca8f8Stomee 	}
1935b5fca8f8Stomee 
1936b5fca8f8Stomee 	ASSERT((cp->cache_slab_create - cp->cache_slab_destroy) ==
1937b5fca8f8Stomee 	    (cp->cache_complete_slab_count +
1938b5fca8f8Stomee 	    avl_numnodes(&cp->cache_partial_slabs) +
1939b5fca8f8Stomee 	    (cp->cache_defrag == NULL ? 0 : cp->cache_defrag->kmd_deadcount)));
19407c478bd9Sstevel@tonic-gate 	mutex_exit(&cp->cache_lock);
19417c478bd9Sstevel@tonic-gate }
19427c478bd9Sstevel@tonic-gate 
1943b5fca8f8Stomee /*
1944b5fca8f8Stomee  * Return -1 if kmem_error, 1 if constructor fails, 0 if successful.
1945b5fca8f8Stomee  */
19467c478bd9Sstevel@tonic-gate static int
19477c478bd9Sstevel@tonic-gate kmem_cache_alloc_debug(kmem_cache_t *cp, void *buf, int kmflag, int construct,
19487c478bd9Sstevel@tonic-gate     caddr_t caller)
19497c478bd9Sstevel@tonic-gate {
19507c478bd9Sstevel@tonic-gate 	kmem_buftag_t *btp = KMEM_BUFTAG(cp, buf);
19517c478bd9Sstevel@tonic-gate 	kmem_bufctl_audit_t *bcp = (kmem_bufctl_audit_t *)btp->bt_bufctl;
19527c478bd9Sstevel@tonic-gate 	uint32_t mtbf;
19537c478bd9Sstevel@tonic-gate 
19547c478bd9Sstevel@tonic-gate 	if (btp->bt_bxstat != ((intptr_t)bcp ^ KMEM_BUFTAG_FREE)) {
19557c478bd9Sstevel@tonic-gate 		kmem_error(KMERR_BADBUFTAG, cp, buf);
19567c478bd9Sstevel@tonic-gate 		return (-1);
19577c478bd9Sstevel@tonic-gate 	}
19587c478bd9Sstevel@tonic-gate 
19597c478bd9Sstevel@tonic-gate 	btp->bt_bxstat = (intptr_t)bcp ^ KMEM_BUFTAG_ALLOC;
19607c478bd9Sstevel@tonic-gate 
19617c478bd9Sstevel@tonic-gate 	if ((cp->cache_flags & KMF_HASH) && bcp->bc_addr != buf) {
19627c478bd9Sstevel@tonic-gate 		kmem_error(KMERR_BADBUFCTL, cp, buf);
19637c478bd9Sstevel@tonic-gate 		return (-1);
19647c478bd9Sstevel@tonic-gate 	}
19657c478bd9Sstevel@tonic-gate 
19667c478bd9Sstevel@tonic-gate 	if (cp->cache_flags & KMF_DEADBEEF) {
19677c478bd9Sstevel@tonic-gate 		if (!construct && (cp->cache_flags & KMF_LITE)) {
19687c478bd9Sstevel@tonic-gate 			if (*(uint64_t *)buf != KMEM_FREE_PATTERN) {
19697c478bd9Sstevel@tonic-gate 				kmem_error(KMERR_MODIFIED, cp, buf);
19707c478bd9Sstevel@tonic-gate 				return (-1);
19717c478bd9Sstevel@tonic-gate 			}
19727c478bd9Sstevel@tonic-gate 			if (cp->cache_constructor != NULL)
19737c478bd9Sstevel@tonic-gate 				*(uint64_t *)buf = btp->bt_redzone;
19747c478bd9Sstevel@tonic-gate 			else
19757c478bd9Sstevel@tonic-gate 				*(uint64_t *)buf = KMEM_UNINITIALIZED_PATTERN;
19767c478bd9Sstevel@tonic-gate 		} else {
19777c478bd9Sstevel@tonic-gate 			construct = 1;
19787c478bd9Sstevel@tonic-gate 			if (verify_and_copy_pattern(KMEM_FREE_PATTERN,
19797c478bd9Sstevel@tonic-gate 			    KMEM_UNINITIALIZED_PATTERN, buf,
19807c478bd9Sstevel@tonic-gate 			    cp->cache_verify)) {
19817c478bd9Sstevel@tonic-gate 				kmem_error(KMERR_MODIFIED, cp, buf);
19827c478bd9Sstevel@tonic-gate 				return (-1);
19837c478bd9Sstevel@tonic-gate 			}
19847c478bd9Sstevel@tonic-gate 		}
19857c478bd9Sstevel@tonic-gate 	}
19867c478bd9Sstevel@tonic-gate 	btp->bt_redzone = KMEM_REDZONE_PATTERN;
19877c478bd9Sstevel@tonic-gate 
19887c478bd9Sstevel@tonic-gate 	if ((mtbf = kmem_mtbf | cp->cache_mtbf) != 0 &&
19897c478bd9Sstevel@tonic-gate 	    gethrtime() % mtbf == 0 &&
19907c478bd9Sstevel@tonic-gate 	    (kmflag & (KM_NOSLEEP | KM_PANIC)) == KM_NOSLEEP) {
19917c478bd9Sstevel@tonic-gate 		kmem_log_event(kmem_failure_log, cp, NULL, NULL);
19927c478bd9Sstevel@tonic-gate 		if (!construct && cp->cache_destructor != NULL)
19937c478bd9Sstevel@tonic-gate 			cp->cache_destructor(buf, cp->cache_private);
19947c478bd9Sstevel@tonic-gate 	} else {
19957c478bd9Sstevel@tonic-gate 		mtbf = 0;
19967c478bd9Sstevel@tonic-gate 	}
19977c478bd9Sstevel@tonic-gate 
19987c478bd9Sstevel@tonic-gate 	if (mtbf || (construct && cp->cache_constructor != NULL &&
19997c478bd9Sstevel@tonic-gate 	    cp->cache_constructor(buf, cp->cache_private, kmflag) != 0)) {
20001a5e258fSJosef 'Jeff' Sipek 		atomic_inc_64(&cp->cache_alloc_fail);
20017c478bd9Sstevel@tonic-gate 		btp->bt_bxstat = (intptr_t)bcp ^ KMEM_BUFTAG_FREE;
20027c478bd9Sstevel@tonic-gate 		if (cp->cache_flags & KMF_DEADBEEF)
20037c478bd9Sstevel@tonic-gate 			copy_pattern(KMEM_FREE_PATTERN, buf, cp->cache_verify);
20047c478bd9Sstevel@tonic-gate 		kmem_slab_free(cp, buf);
2005b5fca8f8Stomee 		return (1);
20067c478bd9Sstevel@tonic-gate 	}
20077c478bd9Sstevel@tonic-gate 
20087c478bd9Sstevel@tonic-gate 	if (cp->cache_flags & KMF_AUDIT) {
20097c478bd9Sstevel@tonic-gate 		KMEM_AUDIT(kmem_transaction_log, cp, bcp);
20107c478bd9Sstevel@tonic-gate 	}
20117c478bd9Sstevel@tonic-gate 
20127c478bd9Sstevel@tonic-gate 	if ((cp->cache_flags & KMF_LITE) &&
20137c478bd9Sstevel@tonic-gate 	    !(cp->cache_cflags & KMC_KMEM_ALLOC)) {
20147c478bd9Sstevel@tonic-gate 		KMEM_BUFTAG_LITE_ENTER(btp, kmem_lite_count, caller);
20157c478bd9Sstevel@tonic-gate 	}
20167c478bd9Sstevel@tonic-gate 
20177c478bd9Sstevel@tonic-gate 	return (0);
20187c478bd9Sstevel@tonic-gate }
20197c478bd9Sstevel@tonic-gate 
20207c478bd9Sstevel@tonic-gate static int
20217c478bd9Sstevel@tonic-gate kmem_cache_free_debug(kmem_cache_t *cp, void *buf, caddr_t caller)
20227c478bd9Sstevel@tonic-gate {
20237c478bd9Sstevel@tonic-gate 	kmem_buftag_t *btp = KMEM_BUFTAG(cp, buf);
20247c478bd9Sstevel@tonic-gate 	kmem_bufctl_audit_t *bcp = (kmem_bufctl_audit_t *)btp->bt_bufctl;
20257c478bd9Sstevel@tonic-gate 	kmem_slab_t *sp;
20267c478bd9Sstevel@tonic-gate 
20277c478bd9Sstevel@tonic-gate 	if (btp->bt_bxstat != ((intptr_t)bcp ^ KMEM_BUFTAG_ALLOC)) {
20287c478bd9Sstevel@tonic-gate 		if (btp->bt_bxstat == ((intptr_t)bcp ^ KMEM_BUFTAG_FREE)) {
20297c478bd9Sstevel@tonic-gate 			kmem_error(KMERR_DUPFREE, cp, buf);
20307c478bd9Sstevel@tonic-gate 			return (-1);
20317c478bd9Sstevel@tonic-gate 		}
20327c478bd9Sstevel@tonic-gate 		sp = kmem_findslab(cp, buf);
20337c478bd9Sstevel@tonic-gate 		if (sp == NULL || sp->slab_cache != cp)
20347c478bd9Sstevel@tonic-gate 			kmem_error(KMERR_BADADDR, cp, buf);
20357c478bd9Sstevel@tonic-gate 		else
20367c478bd9Sstevel@tonic-gate 			kmem_error(KMERR_REDZONE, cp, buf);
20377c478bd9Sstevel@tonic-gate 		return (-1);
20387c478bd9Sstevel@tonic-gate 	}
20397c478bd9Sstevel@tonic-gate 
20407c478bd9Sstevel@tonic-gate 	btp->bt_bxstat = (intptr_t)bcp ^ KMEM_BUFTAG_FREE;
20417c478bd9Sstevel@tonic-gate 
20427c478bd9Sstevel@tonic-gate 	if ((cp->cache_flags & KMF_HASH) && bcp->bc_addr != buf) {
20437c478bd9Sstevel@tonic-gate 		kmem_error(KMERR_BADBUFCTL, cp, buf);
20447c478bd9Sstevel@tonic-gate 		return (-1);
20457c478bd9Sstevel@tonic-gate 	}
20467c478bd9Sstevel@tonic-gate 
20477c478bd9Sstevel@tonic-gate 	if (btp->bt_redzone != KMEM_REDZONE_PATTERN) {
20487c478bd9Sstevel@tonic-gate 		kmem_error(KMERR_REDZONE, cp, buf);
20497c478bd9Sstevel@tonic-gate 		return (-1);
20507c478bd9Sstevel@tonic-gate 	}
20517c478bd9Sstevel@tonic-gate 
20527c478bd9Sstevel@tonic-gate 	if (cp->cache_flags & KMF_AUDIT) {
20537c478bd9Sstevel@tonic-gate 		if (cp->cache_flags & KMF_CONTENTS)
20547c478bd9Sstevel@tonic-gate 			bcp->bc_contents = kmem_log_enter(kmem_content_log,
20557c478bd9Sstevel@tonic-gate 			    buf, cp->cache_contents);
20567c478bd9Sstevel@tonic-gate 		KMEM_AUDIT(kmem_transaction_log, cp, bcp);
20577c478bd9Sstevel@tonic-gate 	}
20587c478bd9Sstevel@tonic-gate 
20597c478bd9Sstevel@tonic-gate 	if ((cp->cache_flags & KMF_LITE) &&
20607c478bd9Sstevel@tonic-gate 	    !(cp->cache_cflags & KMC_KMEM_ALLOC)) {
20617c478bd9Sstevel@tonic-gate 		KMEM_BUFTAG_LITE_ENTER(btp, kmem_lite_count, caller);
20627c478bd9Sstevel@tonic-gate 	}
20637c478bd9Sstevel@tonic-gate 
20647c478bd9Sstevel@tonic-gate 	if (cp->cache_flags & KMF_DEADBEEF) {
20657c478bd9Sstevel@tonic-gate 		if (cp->cache_flags & KMF_LITE)
20667c478bd9Sstevel@tonic-gate 			btp->bt_redzone = *(uint64_t *)buf;
20677c478bd9Sstevel@tonic-gate 		else if (cp->cache_destructor != NULL)
20687c478bd9Sstevel@tonic-gate 			cp->cache_destructor(buf, cp->cache_private);
20697c478bd9Sstevel@tonic-gate 
20707c478bd9Sstevel@tonic-gate 		copy_pattern(KMEM_FREE_PATTERN, buf, cp->cache_verify);
20717c478bd9Sstevel@tonic-gate 	}
20727c478bd9Sstevel@tonic-gate 
20737c478bd9Sstevel@tonic-gate 	return (0);
20747c478bd9Sstevel@tonic-gate }
20757c478bd9Sstevel@tonic-gate 
20767c478bd9Sstevel@tonic-gate /*
20777c478bd9Sstevel@tonic-gate  * Free each object in magazine mp to cp's slab layer, and free mp itself.
20787c478bd9Sstevel@tonic-gate  */
20797c478bd9Sstevel@tonic-gate static void
20807c478bd9Sstevel@tonic-gate kmem_magazine_destroy(kmem_cache_t *cp, kmem_magazine_t *mp, int nrounds)
20817c478bd9Sstevel@tonic-gate {
20827c478bd9Sstevel@tonic-gate 	int round;
20837c478bd9Sstevel@tonic-gate 
2084b5fca8f8Stomee 	ASSERT(!list_link_active(&cp->cache_link) ||
2085b5fca8f8Stomee 	    taskq_member(kmem_taskq, curthread));
20867c478bd9Sstevel@tonic-gate 
20877c478bd9Sstevel@tonic-gate 	for (round = 0; round < nrounds; round++) {
20887c478bd9Sstevel@tonic-gate 		void *buf = mp->mag_round[round];
20897c478bd9Sstevel@tonic-gate 
20907c478bd9Sstevel@tonic-gate 		if (cp->cache_flags & KMF_DEADBEEF) {
20917c478bd9Sstevel@tonic-gate 			if (verify_pattern(KMEM_FREE_PATTERN, buf,
20927c478bd9Sstevel@tonic-gate 			    cp->cache_verify) != NULL) {
20937c478bd9Sstevel@tonic-gate 				kmem_error(KMERR_MODIFIED, cp, buf);
20947c478bd9Sstevel@tonic-gate 				continue;
20957c478bd9Sstevel@tonic-gate 			}
20967c478bd9Sstevel@tonic-gate 			if ((cp->cache_flags & KMF_LITE) &&
20977c478bd9Sstevel@tonic-gate 			    cp->cache_destructor != NULL) {
20987c478bd9Sstevel@tonic-gate 				kmem_buftag_t *btp = KMEM_BUFTAG(cp, buf);
20997c478bd9Sstevel@tonic-gate 				*(uint64_t *)buf = btp->bt_redzone;
21007c478bd9Sstevel@tonic-gate 				cp->cache_destructor(buf, cp->cache_private);
21017c478bd9Sstevel@tonic-gate 				*(uint64_t *)buf = KMEM_FREE_PATTERN;
21027c478bd9Sstevel@tonic-gate 			}
21037c478bd9Sstevel@tonic-gate 		} else if (cp->cache_destructor != NULL) {
21047c478bd9Sstevel@tonic-gate 			cp->cache_destructor(buf, cp->cache_private);
21057c478bd9Sstevel@tonic-gate 		}
21067c478bd9Sstevel@tonic-gate 
21077c478bd9Sstevel@tonic-gate 		kmem_slab_free(cp, buf);
21087c478bd9Sstevel@tonic-gate 	}
21097c478bd9Sstevel@tonic-gate 	ASSERT(KMEM_MAGAZINE_VALID(cp, mp));
21107c478bd9Sstevel@tonic-gate 	kmem_cache_free(cp->cache_magtype->mt_cache, mp);
21117c478bd9Sstevel@tonic-gate }
21127c478bd9Sstevel@tonic-gate 
21137c478bd9Sstevel@tonic-gate /*
21147c478bd9Sstevel@tonic-gate  * Allocate a magazine from the depot.
21157c478bd9Sstevel@tonic-gate  */
21167c478bd9Sstevel@tonic-gate static kmem_magazine_t *
21177c478bd9Sstevel@tonic-gate kmem_depot_alloc(kmem_cache_t *cp, kmem_maglist_t *mlp)
21187c478bd9Sstevel@tonic-gate {
21197c478bd9Sstevel@tonic-gate 	kmem_magazine_t *mp;
21207c478bd9Sstevel@tonic-gate 
21217c478bd9Sstevel@tonic-gate 	/*
21227c478bd9Sstevel@tonic-gate 	 * If we can't get the depot lock without contention,
21237c478bd9Sstevel@tonic-gate 	 * update our contention count.  We use the depot
21247c478bd9Sstevel@tonic-gate 	 * contention rate to determine whether we need to
21257c478bd9Sstevel@tonic-gate 	 * increase the magazine size for better scalability.
21267c478bd9Sstevel@tonic-gate 	 */
21277c478bd9Sstevel@tonic-gate 	if (!mutex_tryenter(&cp->cache_depot_lock)) {
21287c478bd9Sstevel@tonic-gate 		mutex_enter(&cp->cache_depot_lock);
21297c478bd9Sstevel@tonic-gate 		cp->cache_depot_contention++;
21307c478bd9Sstevel@tonic-gate 	}
21317c478bd9Sstevel@tonic-gate 
21327c478bd9Sstevel@tonic-gate 	if ((mp = mlp->ml_list) != NULL) {
21337c478bd9Sstevel@tonic-gate 		ASSERT(KMEM_MAGAZINE_VALID(cp, mp));
21347c478bd9Sstevel@tonic-gate 		mlp->ml_list = mp->mag_next;
21357c478bd9Sstevel@tonic-gate 		if (--mlp->ml_total < mlp->ml_min)
21367c478bd9Sstevel@tonic-gate 			mlp->ml_min = mlp->ml_total;
21377c478bd9Sstevel@tonic-gate 		mlp->ml_alloc++;
21387c478bd9Sstevel@tonic-gate 	}
21397c478bd9Sstevel@tonic-gate 
21407c478bd9Sstevel@tonic-gate 	mutex_exit(&cp->cache_depot_lock);
21417c478bd9Sstevel@tonic-gate 
21427c478bd9Sstevel@tonic-gate 	return (mp);
21437c478bd9Sstevel@tonic-gate }
21447c478bd9Sstevel@tonic-gate 
21457c478bd9Sstevel@tonic-gate /*
21467c478bd9Sstevel@tonic-gate  * Free a magazine to the depot.
21477c478bd9Sstevel@tonic-gate  */
21487c478bd9Sstevel@tonic-gate static void
21497c478bd9Sstevel@tonic-gate kmem_depot_free(kmem_cache_t *cp, kmem_maglist_t *mlp, kmem_magazine_t *mp)
21507c478bd9Sstevel@tonic-gate {
21517c478bd9Sstevel@tonic-gate 	mutex_enter(&cp->cache_depot_lock);
21527c478bd9Sstevel@tonic-gate 	ASSERT(KMEM_MAGAZINE_VALID(cp, mp));
21537c478bd9Sstevel@tonic-gate 	mp->mag_next = mlp->ml_list;
21547c478bd9Sstevel@tonic-gate 	mlp->ml_list = mp;
21557c478bd9Sstevel@tonic-gate 	mlp->ml_total++;
21567c478bd9Sstevel@tonic-gate 	mutex_exit(&cp->cache_depot_lock);
21577c478bd9Sstevel@tonic-gate }
21587c478bd9Sstevel@tonic-gate 
21597c478bd9Sstevel@tonic-gate /*
21607c478bd9Sstevel@tonic-gate  * Update the working set statistics for cp's depot.
21617c478bd9Sstevel@tonic-gate  */
21627c478bd9Sstevel@tonic-gate static void
21637c478bd9Sstevel@tonic-gate kmem_depot_ws_update(kmem_cache_t *cp)
21647c478bd9Sstevel@tonic-gate {
21657c478bd9Sstevel@tonic-gate 	mutex_enter(&cp->cache_depot_lock);
21667c478bd9Sstevel@tonic-gate 	cp->cache_full.ml_reaplimit = cp->cache_full.ml_min;
21677c478bd9Sstevel@tonic-gate 	cp->cache_full.ml_min = cp->cache_full.ml_total;
21687c478bd9Sstevel@tonic-gate 	cp->cache_empty.ml_reaplimit = cp->cache_empty.ml_min;
21697c478bd9Sstevel@tonic-gate 	cp->cache_empty.ml_min = cp->cache_empty.ml_total;
21707c478bd9Sstevel@tonic-gate 	mutex_exit(&cp->cache_depot_lock);
21717c478bd9Sstevel@tonic-gate }
21727c478bd9Sstevel@tonic-gate 
21737c478bd9Sstevel@tonic-gate /*
21740c833d64SJosef 'Jeff' Sipek  * Set the working set statistics for cp's depot to zero.  (Everything is
21750c833d64SJosef 'Jeff' Sipek  * eligible for reaping.)
21760c833d64SJosef 'Jeff' Sipek  */
21770c833d64SJosef 'Jeff' Sipek static void
21780c833d64SJosef 'Jeff' Sipek kmem_depot_ws_zero(kmem_cache_t *cp)
21790c833d64SJosef 'Jeff' Sipek {
21800c833d64SJosef 'Jeff' Sipek 	mutex_enter(&cp->cache_depot_lock);
21810c833d64SJosef 'Jeff' Sipek 	cp->cache_full.ml_reaplimit = cp->cache_full.ml_total;
21820c833d64SJosef 'Jeff' Sipek 	cp->cache_full.ml_min = cp->cache_full.ml_total;
21830c833d64SJosef 'Jeff' Sipek 	cp->cache_empty.ml_reaplimit = cp->cache_empty.ml_total;
21840c833d64SJosef 'Jeff' Sipek 	cp->cache_empty.ml_min = cp->cache_empty.ml_total;
21850c833d64SJosef 'Jeff' Sipek 	mutex_exit(&cp->cache_depot_lock);
21860c833d64SJosef 'Jeff' Sipek }
21870c833d64SJosef 'Jeff' Sipek 
21880c833d64SJosef 'Jeff' Sipek /*
2189*47bb2664SMatthew Ahrens  * The number of bytes to reap before we call kpreempt(). The default (1MB)
2190*47bb2664SMatthew Ahrens  * causes us to preempt reaping up to hundreds of times per second. Using a
2191*47bb2664SMatthew Ahrens  * larger value (1GB) causes this to have virtually no effect.
2192*47bb2664SMatthew Ahrens  */
2193*47bb2664SMatthew Ahrens size_t kmem_reap_preempt_bytes = 1024 * 1024;
2194*47bb2664SMatthew Ahrens 
2195*47bb2664SMatthew Ahrens /*
21967c478bd9Sstevel@tonic-gate  * Reap all magazines that have fallen out of the depot's working set.
21977c478bd9Sstevel@tonic-gate  */
21987c478bd9Sstevel@tonic-gate static void
21997c478bd9Sstevel@tonic-gate kmem_depot_ws_reap(kmem_cache_t *cp)
22007c478bd9Sstevel@tonic-gate {
2201*47bb2664SMatthew Ahrens 	size_t bytes = 0;
22027c478bd9Sstevel@tonic-gate 	long reap;
22037c478bd9Sstevel@tonic-gate 	kmem_magazine_t *mp;
22047c478bd9Sstevel@tonic-gate 
2205b5fca8f8Stomee 	ASSERT(!list_link_active(&cp->cache_link) ||
2206b5fca8f8Stomee 	    taskq_member(kmem_taskq, curthread));
22077c478bd9Sstevel@tonic-gate 
22087c478bd9Sstevel@tonic-gate 	reap = MIN(cp->cache_full.ml_reaplimit, cp->cache_full.ml_min);
2209*47bb2664SMatthew Ahrens 	while (reap-- &&
2210*47bb2664SMatthew Ahrens 	    (mp = kmem_depot_alloc(cp, &cp->cache_full)) != NULL) {
22117c478bd9Sstevel@tonic-gate 		kmem_magazine_destroy(cp, mp, cp->cache_magtype->mt_magsize);
2212*47bb2664SMatthew Ahrens 		bytes += cp->cache_magtype->mt_magsize * cp->cache_bufsize;
2213*47bb2664SMatthew Ahrens 		if (bytes > kmem_reap_preempt_bytes) {
2214*47bb2664SMatthew Ahrens 			kpreempt(KPREEMPT_SYNC);
2215*47bb2664SMatthew Ahrens 			bytes = 0;
2216*47bb2664SMatthew Ahrens 		}
2217*47bb2664SMatthew Ahrens 	}
22187c478bd9Sstevel@tonic-gate 
22197c478bd9Sstevel@tonic-gate 	reap = MIN(cp->cache_empty.ml_reaplimit, cp->cache_empty.ml_min);
2220*47bb2664SMatthew Ahrens 	while (reap-- &&
2221*47bb2664SMatthew Ahrens 	    (mp = kmem_depot_alloc(cp, &cp->cache_empty)) != NULL) {
22227c478bd9Sstevel@tonic-gate 		kmem_magazine_destroy(cp, mp, 0);
2223*47bb2664SMatthew Ahrens 		bytes += cp->cache_magtype->mt_magsize * cp->cache_bufsize;
2224*47bb2664SMatthew Ahrens 		if (bytes > kmem_reap_preempt_bytes) {
2225*47bb2664SMatthew Ahrens 			kpreempt(KPREEMPT_SYNC);
2226*47bb2664SMatthew Ahrens 			bytes = 0;
2227*47bb2664SMatthew Ahrens 		}
2228*47bb2664SMatthew Ahrens 	}
22297c478bd9Sstevel@tonic-gate }
22307c478bd9Sstevel@tonic-gate 
22317c478bd9Sstevel@tonic-gate static void
22327c478bd9Sstevel@tonic-gate kmem_cpu_reload(kmem_cpu_cache_t *ccp, kmem_magazine_t *mp, int rounds)
22337c478bd9Sstevel@tonic-gate {
22347c478bd9Sstevel@tonic-gate 	ASSERT((ccp->cc_loaded == NULL && ccp->cc_rounds == -1) ||
22357c478bd9Sstevel@tonic-gate 	    (ccp->cc_loaded && ccp->cc_rounds + rounds == ccp->cc_magsize));
22367c478bd9Sstevel@tonic-gate 	ASSERT(ccp->cc_magsize > 0);
22377c478bd9Sstevel@tonic-gate 
22387c478bd9Sstevel@tonic-gate 	ccp->cc_ploaded = ccp->cc_loaded;
22397c478bd9Sstevel@tonic-gate 	ccp->cc_prounds = ccp->cc_rounds;
22407c478bd9Sstevel@tonic-gate 	ccp->cc_loaded = mp;
22417c478bd9Sstevel@tonic-gate 	ccp->cc_rounds = rounds;
22427c478bd9Sstevel@tonic-gate }
22437c478bd9Sstevel@tonic-gate 
22447c478bd9Sstevel@tonic-gate /*
22459dd77bc8SDave Plauger  * Intercept kmem alloc/free calls during crash dump in order to avoid
22469dd77bc8SDave Plauger  * changing kmem state while memory is being saved to the dump device.
22479dd77bc8SDave Plauger  * Otherwise, ::kmem_verify will report "corrupt buffers".  Note that
22489dd77bc8SDave Plauger  * there are no locks because only one CPU calls kmem during a crash
22499dd77bc8SDave Plauger  * dump. To enable this feature, first create the associated vmem
22509dd77bc8SDave Plauger  * arena with VMC_DUMPSAFE.
22519dd77bc8SDave Plauger  */
22529dd77bc8SDave Plauger static void *kmem_dump_start;	/* start of pre-reserved heap */
22539dd77bc8SDave Plauger static void *kmem_dump_end;	/* end of heap area */
22549dd77bc8SDave Plauger static void *kmem_dump_curr;	/* current free heap pointer */
22559dd77bc8SDave Plauger static size_t kmem_dump_size;	/* size of heap area */
22569dd77bc8SDave Plauger 
22579dd77bc8SDave Plauger /* append to each buf created in the pre-reserved heap */
22589dd77bc8SDave Plauger typedef struct kmem_dumpctl {
22599dd77bc8SDave Plauger 	void	*kdc_next;	/* cache dump free list linkage */
22609dd77bc8SDave Plauger } kmem_dumpctl_t;
22619dd77bc8SDave Plauger 
22629dd77bc8SDave Plauger #define	KMEM_DUMPCTL(cp, buf)	\
22639dd77bc8SDave Plauger 	((kmem_dumpctl_t *)P2ROUNDUP((uintptr_t)(buf) + (cp)->cache_bufsize, \
22649dd77bc8SDave Plauger 	    sizeof (void *)))
22659dd77bc8SDave Plauger 
22669dd77bc8SDave Plauger /* set non zero for full report */
22679dd77bc8SDave Plauger uint_t kmem_dump_verbose = 0;
22689dd77bc8SDave Plauger 
22699dd77bc8SDave Plauger /* stats for overize heap */
22709dd77bc8SDave Plauger uint_t kmem_dump_oversize_allocs = 0;
22719dd77bc8SDave Plauger uint_t kmem_dump_oversize_max = 0;
22729dd77bc8SDave Plauger 
22739dd77bc8SDave Plauger static void
22749dd77bc8SDave Plauger kmem_dumppr(char **pp, char *e, const char *format, ...)
22759dd77bc8SDave Plauger {
22769dd77bc8SDave Plauger 	char *p = *pp;
22779dd77bc8SDave Plauger 
22789dd77bc8SDave Plauger 	if (p < e) {
22799dd77bc8SDave Plauger 		int n;
22809dd77bc8SDave Plauger 		va_list ap;
22819dd77bc8SDave Plauger 
22829dd77bc8SDave Plauger 		va_start(ap, format);
22839dd77bc8SDave Plauger 		n = vsnprintf(p, e - p, format, ap);
22849dd77bc8SDave Plauger 		va_end(ap);
22859dd77bc8SDave Plauger 		*pp = p + n;
22869dd77bc8SDave Plauger 	}
22879dd77bc8SDave Plauger }
22889dd77bc8SDave Plauger 
22899dd77bc8SDave Plauger /*
22909dd77bc8SDave Plauger  * Called when dumpadm(1M) configures dump parameters.
22919dd77bc8SDave Plauger  */
22929dd77bc8SDave Plauger void
22939dd77bc8SDave Plauger kmem_dump_init(size_t size)
22949dd77bc8SDave Plauger {
22953608e2e0SJohn Levon 	/* Our caller ensures size is always set. */
22963608e2e0SJohn Levon 	ASSERT3U(size, >, 0);
22973608e2e0SJohn Levon 
22989dd77bc8SDave Plauger 	if (kmem_dump_start != NULL)
22999dd77bc8SDave Plauger 		kmem_free(kmem_dump_start, kmem_dump_size);
23009dd77bc8SDave Plauger 
23019dd77bc8SDave Plauger 	kmem_dump_start = kmem_alloc(size, KM_SLEEP);
23029dd77bc8SDave Plauger 	kmem_dump_size = size;
23039dd77bc8SDave Plauger 	kmem_dump_curr = kmem_dump_start;
23049dd77bc8SDave Plauger 	kmem_dump_end = (void *)((char *)kmem_dump_start + size);
23059dd77bc8SDave Plauger 	copy_pattern(KMEM_UNINITIALIZED_PATTERN, kmem_dump_start, size);
23069dd77bc8SDave Plauger }
23079dd77bc8SDave Plauger 
23089dd77bc8SDave Plauger /*
23099dd77bc8SDave Plauger  * Set flag for each kmem_cache_t if is safe to use alternate dump
23109dd77bc8SDave Plauger  * memory. Called just before panic crash dump starts. Set the flag
23119dd77bc8SDave Plauger  * for the calling CPU.
23129dd77bc8SDave Plauger  */
23139dd77bc8SDave Plauger void
23149dd77bc8SDave Plauger kmem_dump_begin(void)
23159dd77bc8SDave Plauger {
23169dd77bc8SDave Plauger 	kmem_cache_t *cp;
23179dd77bc8SDave Plauger 
23183608e2e0SJohn Levon 	ASSERT(panicstr != NULL);
23193608e2e0SJohn Levon 
23209dd77bc8SDave Plauger 	for (cp = list_head(&kmem_caches); cp != NULL;
23219dd77bc8SDave Plauger 	    cp = list_next(&kmem_caches, cp)) {
23229dd77bc8SDave Plauger 		kmem_cpu_cache_t *ccp = KMEM_CPU_CACHE(cp);
23239dd77bc8SDave Plauger 
23249dd77bc8SDave Plauger 		if (cp->cache_arena->vm_cflags & VMC_DUMPSAFE) {
23259dd77bc8SDave Plauger 			cp->cache_flags |= KMF_DUMPDIVERT;
23269dd77bc8SDave Plauger 			ccp->cc_flags |= KMF_DUMPDIVERT;
23279dd77bc8SDave Plauger 			ccp->cc_dump_rounds = ccp->cc_rounds;
23289dd77bc8SDave Plauger 			ccp->cc_dump_prounds = ccp->cc_prounds;
23299dd77bc8SDave Plauger 			ccp->cc_rounds = ccp->cc_prounds = -1;
23309dd77bc8SDave Plauger 		} else {
23319dd77bc8SDave Plauger 			cp->cache_flags |= KMF_DUMPUNSAFE;
23329dd77bc8SDave Plauger 			ccp->cc_flags |= KMF_DUMPUNSAFE;
23339dd77bc8SDave Plauger 		}
23349dd77bc8SDave Plauger 	}
23359dd77bc8SDave Plauger }
23369dd77bc8SDave Plauger 
23379dd77bc8SDave Plauger /*
23389dd77bc8SDave Plauger  * finished dump intercept
23399dd77bc8SDave Plauger  * print any warnings on the console
23409dd77bc8SDave Plauger  * return verbose information to dumpsys() in the given buffer
23419dd77bc8SDave Plauger  */
23429dd77bc8SDave Plauger size_t
23439dd77bc8SDave Plauger kmem_dump_finish(char *buf, size_t size)
23449dd77bc8SDave Plauger {
23459dd77bc8SDave Plauger 	int percent = 0;
23469dd77bc8SDave Plauger 	size_t used;
23479dd77bc8SDave Plauger 	char *e = buf + size;
23489dd77bc8SDave Plauger 	char *p = buf;
23499dd77bc8SDave Plauger 
23503608e2e0SJohn Levon 	if (kmem_dump_curr == kmem_dump_end) {
23513608e2e0SJohn Levon 		cmn_err(CE_WARN, "exceeded kmem_dump space of %lu "
23523608e2e0SJohn Levon 		    "bytes: kmem state in dump may be inconsistent",
23533608e2e0SJohn Levon 		    kmem_dump_size);
23543608e2e0SJohn Levon 	}
23553608e2e0SJohn Levon 
23563608e2e0SJohn Levon 	if (kmem_dump_verbose == 0)
23579dd77bc8SDave Plauger 		return (0);
23589dd77bc8SDave Plauger 
23599dd77bc8SDave Plauger 	used = (char *)kmem_dump_curr - (char *)kmem_dump_start;
23609dd77bc8SDave Plauger 	percent = (used * 100) / kmem_dump_size;
23619dd77bc8SDave Plauger 
23629dd77bc8SDave Plauger 	kmem_dumppr(&p, e, "%% heap used,%d\n", percent);
23639dd77bc8SDave Plauger 	kmem_dumppr(&p, e, "used bytes,%ld\n", used);
23649dd77bc8SDave Plauger 	kmem_dumppr(&p, e, "heap size,%ld\n", kmem_dump_size);
23659dd77bc8SDave Plauger 	kmem_dumppr(&p, e, "Oversize allocs,%d\n",
23669dd77bc8SDave Plauger 	    kmem_dump_oversize_allocs);
23679dd77bc8SDave Plauger 	kmem_dumppr(&p, e, "Oversize max size,%ld\n",
23689dd77bc8SDave Plauger 	    kmem_dump_oversize_max);
23699dd77bc8SDave Plauger 
23709dd77bc8SDave Plauger 	/* return buffer size used */
23719dd77bc8SDave Plauger 	if (p < e)
23729dd77bc8SDave Plauger 		bzero(p, e - p);
23739dd77bc8SDave Plauger 	return (p - buf);
23749dd77bc8SDave Plauger }
23759dd77bc8SDave Plauger 
23769dd77bc8SDave Plauger /*
23779dd77bc8SDave Plauger  * Allocate a constructed object from alternate dump memory.
23789dd77bc8SDave Plauger  */
23799dd77bc8SDave Plauger void *
23809dd77bc8SDave Plauger kmem_cache_alloc_dump(kmem_cache_t *cp, int kmflag)
23819dd77bc8SDave Plauger {
23829dd77bc8SDave Plauger 	void *buf;
23839dd77bc8SDave Plauger 	void *curr;
23849dd77bc8SDave Plauger 	char *bufend;
23859dd77bc8SDave Plauger 
23869dd77bc8SDave Plauger 	/* return a constructed object */
23873608e2e0SJohn Levon 	if ((buf = cp->cache_dump.kd_freelist) != NULL) {
23883608e2e0SJohn Levon 		cp->cache_dump.kd_freelist = KMEM_DUMPCTL(cp, buf)->kdc_next;
23899dd77bc8SDave Plauger 		return (buf);
23909dd77bc8SDave Plauger 	}
23919dd77bc8SDave Plauger 
23929dd77bc8SDave Plauger 	/* create a new constructed object */
23939dd77bc8SDave Plauger 	curr = kmem_dump_curr;
23949dd77bc8SDave Plauger 	buf = (void *)P2ROUNDUP((uintptr_t)curr, cp->cache_align);
23959dd77bc8SDave Plauger 	bufend = (char *)KMEM_DUMPCTL(cp, buf) + sizeof (kmem_dumpctl_t);
23969dd77bc8SDave Plauger 
23979dd77bc8SDave Plauger 	/* hat layer objects cannot cross a page boundary */
23989dd77bc8SDave Plauger 	if (cp->cache_align < PAGESIZE) {
23999dd77bc8SDave Plauger 		char *page = (char *)P2ROUNDUP((uintptr_t)buf, PAGESIZE);
24009dd77bc8SDave Plauger 		if (bufend > page) {
24019dd77bc8SDave Plauger 			bufend += page - (char *)buf;
24029dd77bc8SDave Plauger 			buf = (void *)page;
24039dd77bc8SDave Plauger 		}
24049dd77bc8SDave Plauger 	}
24059dd77bc8SDave Plauger 
24069dd77bc8SDave Plauger 	/* fall back to normal alloc if reserved area is used up */
24079dd77bc8SDave Plauger 	if (bufend > (char *)kmem_dump_end) {
24089dd77bc8SDave Plauger 		kmem_dump_curr = kmem_dump_end;
24093608e2e0SJohn Levon 		cp->cache_dump.kd_alloc_fails++;
24109dd77bc8SDave Plauger 		return (NULL);
24119dd77bc8SDave Plauger 	}
24129dd77bc8SDave Plauger 
24139dd77bc8SDave Plauger 	/*
24149dd77bc8SDave Plauger 	 * Must advance curr pointer before calling a constructor that
24159dd77bc8SDave Plauger 	 * may also allocate memory.
24169dd77bc8SDave Plauger 	 */
24179dd77bc8SDave Plauger 	kmem_dump_curr = bufend;
24189dd77bc8SDave Plauger 
24199dd77bc8SDave Plauger 	/* run constructor */
24209dd77bc8SDave Plauger 	if (cp->cache_constructor != NULL &&
24219dd77bc8SDave Plauger 	    cp->cache_constructor(buf, cp->cache_private, kmflag)
24229dd77bc8SDave Plauger 	    != 0) {
24239dd77bc8SDave Plauger #ifdef DEBUG
24249dd77bc8SDave Plauger 		printf("name='%s' cache=0x%p: kmem cache constructor failed\n",
24259dd77bc8SDave Plauger 		    cp->cache_name, (void *)cp);
24269dd77bc8SDave Plauger #endif
24279dd77bc8SDave Plauger 		/* reset curr pointer iff no allocs were done */
24289dd77bc8SDave Plauger 		if (kmem_dump_curr == bufend)
24299dd77bc8SDave Plauger 			kmem_dump_curr = curr;
24309dd77bc8SDave Plauger 
24313608e2e0SJohn Levon 		cp->cache_dump.kd_alloc_fails++;
24329dd77bc8SDave Plauger 		/* fall back to normal alloc if the constructor fails */
24339dd77bc8SDave Plauger 		return (NULL);
24349dd77bc8SDave Plauger 	}
24359dd77bc8SDave Plauger 
24369dd77bc8SDave Plauger 	return (buf);
24379dd77bc8SDave Plauger }
24389dd77bc8SDave Plauger 
24399dd77bc8SDave Plauger /*
24409dd77bc8SDave Plauger  * Free a constructed object in alternate dump memory.
24419dd77bc8SDave Plauger  */
24429dd77bc8SDave Plauger int
24439dd77bc8SDave Plauger kmem_cache_free_dump(kmem_cache_t *cp, void *buf)
24449dd77bc8SDave Plauger {
24459dd77bc8SDave Plauger 	/* save constructed buffers for next time */
24469dd77bc8SDave Plauger 	if ((char *)buf >= (char *)kmem_dump_start &&
24479dd77bc8SDave Plauger 	    (char *)buf < (char *)kmem_dump_end) {
24483608e2e0SJohn Levon 		KMEM_DUMPCTL(cp, buf)->kdc_next = cp->cache_dump.kd_freelist;
24493608e2e0SJohn Levon 		cp->cache_dump.kd_freelist = buf;
24509dd77bc8SDave Plauger 		return (0);
24519dd77bc8SDave Plauger 	}
24529dd77bc8SDave Plauger 
24539dd77bc8SDave Plauger 	/* just drop buffers that were allocated before dump started */
24549dd77bc8SDave Plauger 	if (kmem_dump_curr < kmem_dump_end)
24559dd77bc8SDave Plauger 		return (0);
24569dd77bc8SDave Plauger 
24579dd77bc8SDave Plauger 	/* fall back to normal free if reserved area is used up */
24589dd77bc8SDave Plauger 	return (1);
24599dd77bc8SDave Plauger }
24609dd77bc8SDave Plauger 
24619dd77bc8SDave Plauger /*
24627c478bd9Sstevel@tonic-gate  * Allocate a constructed object from cache cp.
24637c478bd9Sstevel@tonic-gate  */
24647c478bd9Sstevel@tonic-gate void *
24657c478bd9Sstevel@tonic-gate kmem_cache_alloc(kmem_cache_t *cp, int kmflag)
24667c478bd9Sstevel@tonic-gate {
24677c478bd9Sstevel@tonic-gate 	kmem_cpu_cache_t *ccp = KMEM_CPU_CACHE(cp);
24687c478bd9Sstevel@tonic-gate 	kmem_magazine_t *fmp;
24697c478bd9Sstevel@tonic-gate 	void *buf;
24707c478bd9Sstevel@tonic-gate 
24717c478bd9Sstevel@tonic-gate 	mutex_enter(&ccp->cc_lock);
24727c478bd9Sstevel@tonic-gate 	for (;;) {
24737c478bd9Sstevel@tonic-gate 		/*
24747c478bd9Sstevel@tonic-gate 		 * If there's an object available in the current CPU's
24757c478bd9Sstevel@tonic-gate 		 * loaded magazine, just take it and return.
24767c478bd9Sstevel@tonic-gate 		 */
24777c478bd9Sstevel@tonic-gate 		if (ccp->cc_rounds > 0) {
24787c478bd9Sstevel@tonic-gate 			buf = ccp->cc_loaded->mag_round[--ccp->cc_rounds];
24797c478bd9Sstevel@tonic-gate 			ccp->cc_alloc++;
24807c478bd9Sstevel@tonic-gate 			mutex_exit(&ccp->cc_lock);
24819dd77bc8SDave Plauger 			if (ccp->cc_flags & (KMF_BUFTAG | KMF_DUMPUNSAFE)) {
24829dd77bc8SDave Plauger 				if (ccp->cc_flags & KMF_DUMPUNSAFE) {
24839dd77bc8SDave Plauger 					ASSERT(!(ccp->cc_flags &
24849dd77bc8SDave Plauger 					    KMF_DUMPDIVERT));
24853608e2e0SJohn Levon 					cp->cache_dump.kd_unsafe++;
24869dd77bc8SDave Plauger 				}
24877c478bd9Sstevel@tonic-gate 				if ((ccp->cc_flags & KMF_BUFTAG) &&
24887c478bd9Sstevel@tonic-gate 				    kmem_cache_alloc_debug(cp, buf, kmflag, 0,
2489b5fca8f8Stomee 				    caller()) != 0) {
24907c478bd9Sstevel@tonic-gate 					if (kmflag & KM_NOSLEEP)
24917c478bd9Sstevel@tonic-gate 						return (NULL);
24927c478bd9Sstevel@tonic-gate 					mutex_enter(&ccp->cc_lock);
24937c478bd9Sstevel@tonic-gate 					continue;
24947c478bd9Sstevel@tonic-gate 				}
24959dd77bc8SDave Plauger 			}
24967c478bd9Sstevel@tonic-gate 			return (buf);
24977c478bd9Sstevel@tonic-gate 		}
24987c478bd9Sstevel@tonic-gate 
24997c478bd9Sstevel@tonic-gate 		/*
25007c478bd9Sstevel@tonic-gate 		 * The loaded magazine is empty.  If the previously loaded
25017c478bd9Sstevel@tonic-gate 		 * magazine was full, exchange them and try again.
25027c478bd9Sstevel@tonic-gate 		 */
25037c478bd9Sstevel@tonic-gate 		if (ccp->cc_prounds > 0) {
25047c478bd9Sstevel@tonic-gate 			kmem_cpu_reload(ccp, ccp->cc_ploaded, ccp->cc_prounds);
25057c478bd9Sstevel@tonic-gate 			continue;
25067c478bd9Sstevel@tonic-gate 		}
25077c478bd9Sstevel@tonic-gate 
25087c478bd9Sstevel@tonic-gate 		/*
25099dd77bc8SDave Plauger 		 * Return an alternate buffer at dump time to preserve
25109dd77bc8SDave Plauger 		 * the heap.
25119dd77bc8SDave Plauger 		 */
25129dd77bc8SDave Plauger 		if (ccp->cc_flags & (KMF_DUMPDIVERT | KMF_DUMPUNSAFE)) {
25139dd77bc8SDave Plauger 			if (ccp->cc_flags & KMF_DUMPUNSAFE) {
25149dd77bc8SDave Plauger 				ASSERT(!(ccp->cc_flags & KMF_DUMPDIVERT));
25159dd77bc8SDave Plauger 				/* log it so that we can warn about it */
25163608e2e0SJohn Levon 				cp->cache_dump.kd_unsafe++;
25179dd77bc8SDave Plauger 			} else {
25189dd77bc8SDave Plauger 				if ((buf = kmem_cache_alloc_dump(cp, kmflag)) !=
25199dd77bc8SDave Plauger 				    NULL) {
25209dd77bc8SDave Plauger 					mutex_exit(&ccp->cc_lock);
25219dd77bc8SDave Plauger 					return (buf);
25229dd77bc8SDave Plauger 				}
25239dd77bc8SDave Plauger 				break;		/* fall back to slab layer */
25249dd77bc8SDave Plauger 			}
25259dd77bc8SDave Plauger 		}
25269dd77bc8SDave Plauger 
25279dd77bc8SDave Plauger 		/*
25287c478bd9Sstevel@tonic-gate 		 * If the magazine layer is disabled, break out now.
25297c478bd9Sstevel@tonic-gate 		 */
25307c478bd9Sstevel@tonic-gate 		if (ccp->cc_magsize == 0)
25317c478bd9Sstevel@tonic-gate 			break;
25327c478bd9Sstevel@tonic-gate 
25337c478bd9Sstevel@tonic-gate 		/*
25347c478bd9Sstevel@tonic-gate 		 * Try to get a full magazine from the depot.
25357c478bd9Sstevel@tonic-gate 		 */
25367c478bd9Sstevel@tonic-gate 		fmp = kmem_depot_alloc(cp, &cp->cache_full);
25377c478bd9Sstevel@tonic-gate 		if (fmp != NULL) {
25387c478bd9Sstevel@tonic-gate 			if (ccp->cc_ploaded != NULL)
25397c478bd9Sstevel@tonic-gate 				kmem_depot_free(cp, &cp->cache_empty,
25407c478bd9Sstevel@tonic-gate 				    ccp->cc_ploaded);
25417c478bd9Sstevel@tonic-gate 			kmem_cpu_reload(ccp, fmp, ccp->cc_magsize);
25427c478bd9Sstevel@tonic-gate 			continue;
25437c478bd9Sstevel@tonic-gate 		}
25447c478bd9Sstevel@tonic-gate 
25457c478bd9Sstevel@tonic-gate 		/*
25467c478bd9Sstevel@tonic-gate 		 * There are no full magazines in the depot,
25477c478bd9Sstevel@tonic-gate 		 * so fall through to the slab layer.
25487c478bd9Sstevel@tonic-gate 		 */
25497c478bd9Sstevel@tonic-gate 		break;
25507c478bd9Sstevel@tonic-gate 	}
25517c478bd9Sstevel@tonic-gate 	mutex_exit(&ccp->cc_lock);
25527c478bd9Sstevel@tonic-gate 
25537c478bd9Sstevel@tonic-gate 	/*
25547c478bd9Sstevel@tonic-gate 	 * We couldn't allocate a constructed object from the magazine layer,
25557c478bd9Sstevel@tonic-gate 	 * so get a raw buffer from the slab layer and apply its constructor.
25567c478bd9Sstevel@tonic-gate 	 */
25577c478bd9Sstevel@tonic-gate 	buf = kmem_slab_alloc(cp, kmflag);
25587c478bd9Sstevel@tonic-gate 
25597c478bd9Sstevel@tonic-gate 	if (buf == NULL)
25607c478bd9Sstevel@tonic-gate 		return (NULL);
25617c478bd9Sstevel@tonic-gate 
25627c478bd9Sstevel@tonic-gate 	if (cp->cache_flags & KMF_BUFTAG) {
25637c478bd9Sstevel@tonic-gate 		/*
25647c478bd9Sstevel@tonic-gate 		 * Make kmem_cache_alloc_debug() apply the constructor for us.
25657c478bd9Sstevel@tonic-gate 		 */
2566b5fca8f8Stomee 		int rc = kmem_cache_alloc_debug(cp, buf, kmflag, 1, caller());
2567b5fca8f8Stomee 		if (rc != 0) {
25687c478bd9Sstevel@tonic-gate 			if (kmflag & KM_NOSLEEP)
25697c478bd9Sstevel@tonic-gate 				return (NULL);
25707c478bd9Sstevel@tonic-gate 			/*
25717c478bd9Sstevel@tonic-gate 			 * kmem_cache_alloc_debug() detected corruption
2572b5fca8f8Stomee 			 * but didn't panic (kmem_panic <= 0). We should not be
2573b5fca8f8Stomee 			 * here because the constructor failed (indicated by a
2574b5fca8f8Stomee 			 * return code of 1). Try again.
25757c478bd9Sstevel@tonic-gate 			 */
2576b5fca8f8Stomee 			ASSERT(rc == -1);
25777c478bd9Sstevel@tonic-gate 			return (kmem_cache_alloc(cp, kmflag));
25787c478bd9Sstevel@tonic-gate 		}
25797c478bd9Sstevel@tonic-gate 		return (buf);
25807c478bd9Sstevel@tonic-gate 	}
25817c478bd9Sstevel@tonic-gate 
25827c478bd9Sstevel@tonic-gate 	if (cp->cache_constructor != NULL &&
25837c478bd9Sstevel@tonic-gate 	    cp->cache_constructor(buf, cp->cache_private, kmflag) != 0) {
25841a5e258fSJosef 'Jeff' Sipek 		atomic_inc_64(&cp->cache_alloc_fail);
25857c478bd9Sstevel@tonic-gate 		kmem_slab_free(cp, buf);
25867c478bd9Sstevel@tonic-gate 		return (NULL);
25877c478bd9Sstevel@tonic-gate 	}
25887c478bd9Sstevel@tonic-gate 
25897c478bd9Sstevel@tonic-gate 	return (buf);
25907c478bd9Sstevel@tonic-gate }
25917c478bd9Sstevel@tonic-gate 
25927c478bd9Sstevel@tonic-gate /*
2593b5fca8f8Stomee  * The freed argument tells whether or not kmem_cache_free_debug() has already
2594b5fca8f8Stomee  * been called so that we can avoid the duplicate free error. For example, a
2595b5fca8f8Stomee  * buffer on a magazine has already been freed by the client but is still
2596b5fca8f8Stomee  * constructed.
2597b5fca8f8Stomee  */
2598b5fca8f8Stomee static void
2599b5fca8f8Stomee kmem_slab_free_constructed(kmem_cache_t *cp, void *buf, boolean_t freed)
2600b5fca8f8Stomee {
2601b5fca8f8Stomee 	if (!freed && (cp->cache_flags & KMF_BUFTAG))
2602b5fca8f8Stomee 		if (kmem_cache_free_debug(cp, buf, caller()) == -1)
2603b5fca8f8Stomee 			return;
2604b5fca8f8Stomee 
2605b5fca8f8Stomee 	/*
2606b5fca8f8Stomee 	 * Note that if KMF_DEADBEEF is in effect and KMF_LITE is not,
2607b5fca8f8Stomee 	 * kmem_cache_free_debug() will have already applied the destructor.
2608b5fca8f8Stomee 	 */
2609b5fca8f8Stomee 	if ((cp->cache_flags & (KMF_DEADBEEF | KMF_LITE)) != KMF_DEADBEEF &&
2610b5fca8f8Stomee 	    cp->cache_destructor != NULL) {
2611b5fca8f8Stomee 		if (cp->cache_flags & KMF_DEADBEEF) {	/* KMF_LITE implied */
2612b5fca8f8Stomee 			kmem_buftag_t *btp = KMEM_BUFTAG(cp, buf);
2613b5fca8f8Stomee 			*(uint64_t *)buf = btp->bt_redzone;
2614b5fca8f8Stomee 			cp->cache_destructor(buf, cp->cache_private);
2615b5fca8f8Stomee 			*(uint64_t *)buf = KMEM_FREE_PATTERN;
2616b5fca8f8Stomee 		} else {
2617b5fca8f8Stomee 			cp->cache_destructor(buf, cp->cache_private);
2618b5fca8f8Stomee 		}
2619b5fca8f8Stomee 	}
2620b5fca8f8Stomee 
2621b5fca8f8Stomee 	kmem_slab_free(cp, buf);
2622b5fca8f8Stomee }
2623b5fca8f8Stomee 
2624b5fca8f8Stomee /*
2625b942e89bSDavid Valin  * Used when there's no room to free a buffer to the per-CPU cache.
2626b942e89bSDavid Valin  * Drops and re-acquires &ccp->cc_lock, and returns non-zero if the
2627b942e89bSDavid Valin  * caller should try freeing to the per-CPU cache again.
2628b942e89bSDavid Valin  * Note that we don't directly install the magazine in the cpu cache,
2629b942e89bSDavid Valin  * since its state may have changed wildly while the lock was dropped.
2630b942e89bSDavid Valin  */
2631b942e89bSDavid Valin static int
2632b942e89bSDavid Valin kmem_cpucache_magazine_alloc(kmem_cpu_cache_t *ccp, kmem_cache_t *cp)
2633b942e89bSDavid Valin {
2634b942e89bSDavid Valin 	kmem_magazine_t *emp;
2635b942e89bSDavid Valin 	kmem_magtype_t *mtp;
2636b942e89bSDavid Valin 
2637b942e89bSDavid Valin 	ASSERT(MUTEX_HELD(&ccp->cc_lock));
2638b942e89bSDavid Valin 	ASSERT(((uint_t)ccp->cc_rounds == ccp->cc_magsize ||
2639b942e89bSDavid Valin 	    ((uint_t)ccp->cc_rounds == -1)) &&
2640b942e89bSDavid Valin 	    ((uint_t)ccp->cc_prounds == ccp->cc_magsize ||
2641b942e89bSDavid Valin 	    ((uint_t)ccp->cc_prounds == -1)));
2642b942e89bSDavid Valin 
2643b942e89bSDavid Valin 	emp = kmem_depot_alloc(cp, &cp->cache_empty);
2644b942e89bSDavid Valin 	if (emp != NULL) {
2645b942e89bSDavid Valin 		if (ccp->cc_ploaded != NULL)
2646b942e89bSDavid Valin 			kmem_depot_free(cp, &cp->cache_full,
2647b942e89bSDavid Valin 			    ccp->cc_ploaded);
2648b942e89bSDavid Valin 		kmem_cpu_reload(ccp, emp, 0);
2649b942e89bSDavid Valin 		return (1);
2650b942e89bSDavid Valin 	}
2651b942e89bSDavid Valin 	/*
2652b942e89bSDavid Valin 	 * There are no empty magazines in the depot,
2653b942e89bSDavid Valin 	 * so try to allocate a new one.  We must drop all locks
2654b942e89bSDavid Valin 	 * across kmem_cache_alloc() because lower layers may
2655b942e89bSDavid Valin 	 * attempt to allocate from this cache.
2656b942e89bSDavid Valin 	 */
2657b942e89bSDavid Valin 	mtp = cp->cache_magtype;
2658b942e89bSDavid Valin 	mutex_exit(&ccp->cc_lock);
2659b942e89bSDavid Valin 	emp = kmem_cache_alloc(mtp->mt_cache, KM_NOSLEEP);
2660b942e89bSDavid Valin 	mutex_enter(&ccp->cc_lock);
2661b942e89bSDavid Valin 
2662b942e89bSDavid Valin 	if (emp != NULL) {
2663b942e89bSDavid Valin 		/*
2664b942e89bSDavid Valin 		 * We successfully allocated an empty magazine.
2665b942e89bSDavid Valin 		 * However, we had to drop ccp->cc_lock to do it,
2666b942e89bSDavid Valin 		 * so the cache's magazine size may have changed.
2667b942e89bSDavid Valin 		 * If so, free the magazine and try again.
2668b942e89bSDavid Valin 		 */
2669b942e89bSDavid Valin 		if (ccp->cc_magsize != mtp->mt_magsize) {
2670b942e89bSDavid Valin 			mutex_exit(&ccp->cc_lock);
2671b942e89bSDavid Valin 			kmem_cache_free(mtp->mt_cache, emp);
2672b942e89bSDavid Valin 			mutex_enter(&ccp->cc_lock);
2673b942e89bSDavid Valin 			return (1);
2674b942e89bSDavid Valin 		}
2675b942e89bSDavid Valin 
2676b942e89bSDavid Valin 		/*
2677b942e89bSDavid Valin 		 * We got a magazine of the right size.  Add it to
2678b942e89bSDavid Valin 		 * the depot and try the whole dance again.
2679b942e89bSDavid Valin 		 */
2680b942e89bSDavid Valin 		kmem_depot_free(cp, &cp->cache_empty, emp);
2681b942e89bSDavid Valin 		return (1);
2682b942e89bSDavid Valin 	}
2683b942e89bSDavid Valin 
2684b942e89bSDavid Valin 	/*
2685b942e89bSDavid Valin 	 * We couldn't allocate an empty magazine,
2686b942e89bSDavid Valin 	 * so fall through to the slab layer.
2687b942e89bSDavid Valin 	 */
2688b942e89bSDavid Valin 	return (0);
2689b942e89bSDavid Valin }
2690b942e89bSDavid Valin 
2691b942e89bSDavid Valin /*
26927c478bd9Sstevel@tonic-gate  * Free a constructed object to cache cp.
26937c478bd9Sstevel@tonic-gate  */
26947c478bd9Sstevel@tonic-gate void
26957c478bd9Sstevel@tonic-gate kmem_cache_free(kmem_cache_t *cp, void *buf)
26967c478bd9Sstevel@tonic-gate {
26977c478bd9Sstevel@tonic-gate 	kmem_cpu_cache_t *ccp = KMEM_CPU_CACHE(cp);
26987c478bd9Sstevel@tonic-gate 
2699b5fca8f8Stomee 	/*
2700b5fca8f8Stomee 	 * The client must not free either of the buffers passed to the move
2701b5fca8f8Stomee 	 * callback function.
2702b5fca8f8Stomee 	 */
2703b5fca8f8Stomee 	ASSERT(cp->cache_defrag == NULL ||
2704b5fca8f8Stomee 	    cp->cache_defrag->kmd_thread != curthread ||
2705b5fca8f8Stomee 	    (buf != cp->cache_defrag->kmd_from_buf &&
2706b5fca8f8Stomee 	    buf != cp->cache_defrag->kmd_to_buf));
2707b5fca8f8Stomee 
27089dd77bc8SDave Plauger 	if (ccp->cc_flags & (KMF_BUFTAG | KMF_DUMPDIVERT | KMF_DUMPUNSAFE)) {
27099dd77bc8SDave Plauger 		if (ccp->cc_flags & KMF_DUMPUNSAFE) {
27109dd77bc8SDave Plauger 			ASSERT(!(ccp->cc_flags & KMF_DUMPDIVERT));
27119dd77bc8SDave Plauger 			/* log it so that we can warn about it */
27123608e2e0SJohn Levon 			cp->cache_dump.kd_unsafe++;
27139dd77bc8SDave Plauger 		} else if (KMEM_DUMPCC(ccp) && !kmem_cache_free_dump(cp, buf)) {
27149dd77bc8SDave Plauger 			return;
27159dd77bc8SDave Plauger 		}
27169dd77bc8SDave Plauger 		if (ccp->cc_flags & KMF_BUFTAG) {
27177c478bd9Sstevel@tonic-gate 			if (kmem_cache_free_debug(cp, buf, caller()) == -1)
27187c478bd9Sstevel@tonic-gate 				return;
27199dd77bc8SDave Plauger 		}
27209dd77bc8SDave Plauger 	}
27217c478bd9Sstevel@tonic-gate 
27227c478bd9Sstevel@tonic-gate 	mutex_enter(&ccp->cc_lock);
2723b942e89bSDavid Valin 	/*
2724b942e89bSDavid Valin 	 * Any changes to this logic should be reflected in kmem_slab_prefill()
2725b942e89bSDavid Valin 	 */
27267c478bd9Sstevel@tonic-gate 	for (;;) {
27277c478bd9Sstevel@tonic-gate 		/*
27287c478bd9Sstevel@tonic-gate 		 * If there's a slot available in the current CPU's
27297c478bd9Sstevel@tonic-gate 		 * loaded magazine, just put the object there and return.
27307c478bd9Sstevel@tonic-gate 		 */
27317c478bd9Sstevel@tonic-gate 		if ((uint_t)ccp->cc_rounds < ccp->cc_magsize) {
27327c478bd9Sstevel@tonic-gate 			ccp->cc_loaded->mag_round[ccp->cc_rounds++] = buf;
27337c478bd9Sstevel@tonic-gate 			ccp->cc_free++;
27347c478bd9Sstevel@tonic-gate 			mutex_exit(&ccp->cc_lock);
27357c478bd9Sstevel@tonic-gate 			return;
27367c478bd9Sstevel@tonic-gate 		}
27377c478bd9Sstevel@tonic-gate 
27387c478bd9Sstevel@tonic-gate 		/*
27397c478bd9Sstevel@tonic-gate 		 * The loaded magazine is full.  If the previously loaded
27407c478bd9Sstevel@tonic-gate 		 * magazine was empty, exchange them and try again.
27417c478bd9Sstevel@tonic-gate 		 */
27427c478bd9Sstevel@tonic-gate 		if (ccp->cc_prounds == 0) {
27437c478bd9Sstevel@tonic-gate 			kmem_cpu_reload(ccp, ccp->cc_ploaded, ccp->cc_prounds);
27447c478bd9Sstevel@tonic-gate 			continue;
27457c478bd9Sstevel@tonic-gate 		}
27467c478bd9Sstevel@tonic-gate 
27477c478bd9Sstevel@tonic-gate 		/*
27487c478bd9Sstevel@tonic-gate 		 * If the magazine layer is disabled, break out now.
27497c478bd9Sstevel@tonic-gate 		 */
27507c478bd9Sstevel@tonic-gate 		if (ccp->cc_magsize == 0)
27517c478bd9Sstevel@tonic-gate 			break;
27527c478bd9Sstevel@tonic-gate 
2753b942e89bSDavid Valin 		if (!kmem_cpucache_magazine_alloc(ccp, cp)) {
27547c478bd9Sstevel@tonic-gate 			/*
2755b942e89bSDavid Valin 			 * We couldn't free our constructed object to the
2756b942e89bSDavid Valin 			 * magazine layer, so apply its destructor and free it
2757b942e89bSDavid Valin 			 * to the slab layer.
27587c478bd9Sstevel@tonic-gate 			 */
27597c478bd9Sstevel@tonic-gate 			break;
27607c478bd9Sstevel@tonic-gate 		}
2761b942e89bSDavid Valin 	}
27627c478bd9Sstevel@tonic-gate 	mutex_exit(&ccp->cc_lock);
2763b942e89bSDavid Valin 	kmem_slab_free_constructed(cp, buf, B_TRUE);
2764b942e89bSDavid Valin }
2765b942e89bSDavid Valin 
2766b942e89bSDavid Valin static void
2767b942e89bSDavid Valin kmem_slab_prefill(kmem_cache_t *cp, kmem_slab_t *sp)
2768b942e89bSDavid Valin {
2769b942e89bSDavid Valin 	kmem_cpu_cache_t *ccp = KMEM_CPU_CACHE(cp);
2770b942e89bSDavid Valin 	int cache_flags = cp->cache_flags;
2771b942e89bSDavid Valin 
2772b942e89bSDavid Valin 	kmem_bufctl_t *next, *head;
2773b942e89bSDavid Valin 	size_t nbufs;
27747c478bd9Sstevel@tonic-gate 
27757c478bd9Sstevel@tonic-gate 	/*
2776b942e89bSDavid Valin 	 * Completely allocate the newly created slab and put the pre-allocated
2777b942e89bSDavid Valin 	 * buffers in magazines. Any of the buffers that cannot be put in
2778b942e89bSDavid Valin 	 * magazines must be returned to the slab.
27797c478bd9Sstevel@tonic-gate 	 */
2780b942e89bSDavid Valin 	ASSERT(MUTEX_HELD(&cp->cache_lock));
2781b942e89bSDavid Valin 	ASSERT((cache_flags & (KMF_PREFILL|KMF_BUFTAG)) == KMF_PREFILL);
2782b942e89bSDavid Valin 	ASSERT(cp->cache_constructor == NULL);
2783b942e89bSDavid Valin 	ASSERT(sp->slab_cache == cp);
2784b942e89bSDavid Valin 	ASSERT(sp->slab_refcnt == 1);
2785b942e89bSDavid Valin 	ASSERT(sp->slab_head != NULL && sp->slab_chunks > sp->slab_refcnt);
2786b942e89bSDavid Valin 	ASSERT(avl_find(&cp->cache_partial_slabs, sp, NULL) == NULL);
2787b942e89bSDavid Valin 
2788b942e89bSDavid Valin 	head = sp->slab_head;
2789b942e89bSDavid Valin 	nbufs = (sp->slab_chunks - sp->slab_refcnt);
2790b942e89bSDavid Valin 	sp->slab_head = NULL;
2791b942e89bSDavid Valin 	sp->slab_refcnt += nbufs;
2792b942e89bSDavid Valin 	cp->cache_bufslab -= nbufs;
2793b942e89bSDavid Valin 	cp->cache_slab_alloc += nbufs;
2794b942e89bSDavid Valin 	list_insert_head(&cp->cache_complete_slabs, sp);
2795b942e89bSDavid Valin 	cp->cache_complete_slab_count++;
2796b942e89bSDavid Valin 	mutex_exit(&cp->cache_lock);
2797b942e89bSDavid Valin 	mutex_enter(&ccp->cc_lock);
2798b942e89bSDavid Valin 
2799b942e89bSDavid Valin 	while (head != NULL) {
2800b942e89bSDavid Valin 		void *buf = KMEM_BUF(cp, head);
2801b942e89bSDavid Valin 		/*
2802b942e89bSDavid Valin 		 * If there's a slot available in the current CPU's
2803b942e89bSDavid Valin 		 * loaded magazine, just put the object there and
2804b942e89bSDavid Valin 		 * continue.
2805b942e89bSDavid Valin 		 */
2806b942e89bSDavid Valin 		if ((uint_t)ccp->cc_rounds < ccp->cc_magsize) {
2807b942e89bSDavid Valin 			ccp->cc_loaded->mag_round[ccp->cc_rounds++] =
2808b942e89bSDavid Valin 			    buf;
2809b942e89bSDavid Valin 			ccp->cc_free++;
2810b942e89bSDavid Valin 			nbufs--;
2811b942e89bSDavid Valin 			head = head->bc_next;
2812b942e89bSDavid Valin 			continue;
2813b942e89bSDavid Valin 		}
2814b942e89bSDavid Valin 
2815b942e89bSDavid Valin 		/*
2816b942e89bSDavid Valin 		 * The loaded magazine is full.  If the previously
2817b942e89bSDavid Valin 		 * loaded magazine was empty, exchange them and try
2818b942e89bSDavid Valin 		 * again.
2819b942e89bSDavid Valin 		 */
2820b942e89bSDavid Valin 		if (ccp->cc_prounds == 0) {
2821b942e89bSDavid Valin 			kmem_cpu_reload(ccp, ccp->cc_ploaded,
2822b942e89bSDavid Valin 			    ccp->cc_prounds);
2823b942e89bSDavid Valin 			continue;
2824b942e89bSDavid Valin 		}
2825b942e89bSDavid Valin 
2826b942e89bSDavid Valin 		/*
2827b942e89bSDavid Valin 		 * If the magazine layer is disabled, break out now.
2828b942e89bSDavid Valin 		 */
2829b942e89bSDavid Valin 
2830b942e89bSDavid Valin 		if (ccp->cc_magsize == 0) {
2831b942e89bSDavid Valin 			break;
2832b942e89bSDavid Valin 		}
2833b942e89bSDavid Valin 
2834b942e89bSDavid Valin 		if (!kmem_cpucache_magazine_alloc(ccp, cp))
2835b942e89bSDavid Valin 			break;
2836b942e89bSDavid Valin 	}
2837b942e89bSDavid Valin 	mutex_exit(&ccp->cc_lock);
2838b942e89bSDavid Valin 	if (nbufs != 0) {
2839b942e89bSDavid Valin 		ASSERT(head != NULL);
2840b942e89bSDavid Valin 
2841b942e89bSDavid Valin 		/*
2842b942e89bSDavid Valin 		 * If there was a failure, return remaining objects to
2843b942e89bSDavid Valin 		 * the slab
2844b942e89bSDavid Valin 		 */
2845b942e89bSDavid Valin 		while (head != NULL) {
2846b942e89bSDavid Valin 			ASSERT(nbufs != 0);
2847b942e89bSDavid Valin 			next = head->bc_next;
2848b942e89bSDavid Valin 			head->bc_next = NULL;
2849b942e89bSDavid Valin 			kmem_slab_free(cp, KMEM_BUF(cp, head));
2850b942e89bSDavid Valin 			head = next;
2851b942e89bSDavid Valin 			nbufs--;
2852b942e89bSDavid Valin 		}
2853b942e89bSDavid Valin 	}
2854b942e89bSDavid Valin 	ASSERT(head == NULL);
2855b942e89bSDavid Valin 	ASSERT(nbufs == 0);
2856b942e89bSDavid Valin 	mutex_enter(&cp->cache_lock);
28577c478bd9Sstevel@tonic-gate }
28587c478bd9Sstevel@tonic-gate 
28597c478bd9Sstevel@tonic-gate void *
28607c478bd9Sstevel@tonic-gate kmem_zalloc(size_t size, int kmflag)
28617c478bd9Sstevel@tonic-gate {
2862dce01e3fSJonathan W Adams 	size_t index;
28637c478bd9Sstevel@tonic-gate 	void *buf;
28647c478bd9Sstevel@tonic-gate 
2865dce01e3fSJonathan W Adams 	if ((index = ((size - 1) >> KMEM_ALIGN_SHIFT)) < KMEM_ALLOC_TABLE_MAX) {
28667c478bd9Sstevel@tonic-gate 		kmem_cache_t *cp = kmem_alloc_table[index];
28677c478bd9Sstevel@tonic-gate 		buf = kmem_cache_alloc(cp, kmflag);
28687c478bd9Sstevel@tonic-gate 		if (buf != NULL) {
28699dd77bc8SDave Plauger 			if ((cp->cache_flags & KMF_BUFTAG) && !KMEM_DUMP(cp)) {
28707c478bd9Sstevel@tonic-gate 				kmem_buftag_t *btp = KMEM_BUFTAG(cp, buf);
28717c478bd9Sstevel@tonic-gate 				((uint8_t *)buf)[size] = KMEM_REDZONE_BYTE;
28727c478bd9Sstevel@tonic-gate 				((uint32_t *)btp)[1] = KMEM_SIZE_ENCODE(size);
28737c478bd9Sstevel@tonic-gate 
28747c478bd9Sstevel@tonic-gate 				if (cp->cache_flags & KMF_LITE) {
28757c478bd9Sstevel@tonic-gate 					KMEM_BUFTAG_LITE_ENTER(btp,
28767c478bd9Sstevel@tonic-gate 					    kmem_lite_count, caller());
28777c478bd9Sstevel@tonic-gate 				}
28787c478bd9Sstevel@tonic-gate 			}
28797c478bd9Sstevel@tonic-gate 			bzero(buf, size);
28807c478bd9Sstevel@tonic-gate 		}
28817c478bd9Sstevel@tonic-gate 	} else {
28827c478bd9Sstevel@tonic-gate 		buf = kmem_alloc(size, kmflag);
28837c478bd9Sstevel@tonic-gate 		if (buf != NULL)
28847c478bd9Sstevel@tonic-gate 			bzero(buf, size);
28857c478bd9Sstevel@tonic-gate 	}
28867c478bd9Sstevel@tonic-gate 	return (buf);
28877c478bd9Sstevel@tonic-gate }
28887c478bd9Sstevel@tonic-gate 
28897c478bd9Sstevel@tonic-gate void *
28907c478bd9Sstevel@tonic-gate kmem_alloc(size_t size, int kmflag)
28917c478bd9Sstevel@tonic-gate {
2892dce01e3fSJonathan W Adams 	size_t index;
2893dce01e3fSJonathan W Adams 	kmem_cache_t *cp;
28947c478bd9Sstevel@tonic-gate 	void *buf;
28957c478bd9Sstevel@tonic-gate 
2896dce01e3fSJonathan W Adams 	if ((index = ((size - 1) >> KMEM_ALIGN_SHIFT)) < KMEM_ALLOC_TABLE_MAX) {
2897dce01e3fSJonathan W Adams 		cp = kmem_alloc_table[index];
2898dce01e3fSJonathan W Adams 		/* fall through to kmem_cache_alloc() */
2899dce01e3fSJonathan W Adams 
2900dce01e3fSJonathan W Adams 	} else if ((index = ((size - 1) >> KMEM_BIG_SHIFT)) <
2901dce01e3fSJonathan W Adams 	    kmem_big_alloc_table_max) {
2902dce01e3fSJonathan W Adams 		cp = kmem_big_alloc_table[index];
2903dce01e3fSJonathan W Adams 		/* fall through to kmem_cache_alloc() */
2904dce01e3fSJonathan W Adams 
2905dce01e3fSJonathan W Adams 	} else {
2906dce01e3fSJonathan W Adams 		if (size == 0)
2907dce01e3fSJonathan W Adams 			return (NULL);
2908dce01e3fSJonathan W Adams 
2909dce01e3fSJonathan W Adams 		buf = vmem_alloc(kmem_oversize_arena, size,
2910dce01e3fSJonathan W Adams 		    kmflag & KM_VMFLAGS);
2911dce01e3fSJonathan W Adams 		if (buf == NULL)
2912dce01e3fSJonathan W Adams 			kmem_log_event(kmem_failure_log, NULL, NULL,
2913dce01e3fSJonathan W Adams 			    (void *)size);
29149dd77bc8SDave Plauger 		else if (KMEM_DUMP(kmem_slab_cache)) {
29159dd77bc8SDave Plauger 			/* stats for dump intercept */
29169dd77bc8SDave Plauger 			kmem_dump_oversize_allocs++;
29179dd77bc8SDave Plauger 			if (size > kmem_dump_oversize_max)
29189dd77bc8SDave Plauger 				kmem_dump_oversize_max = size;
29199dd77bc8SDave Plauger 		}
2920dce01e3fSJonathan W Adams 		return (buf);
2921dce01e3fSJonathan W Adams 	}
2922dce01e3fSJonathan W Adams 
29237c478bd9Sstevel@tonic-gate 	buf = kmem_cache_alloc(cp, kmflag);
29249dd77bc8SDave Plauger 	if ((cp->cache_flags & KMF_BUFTAG) && !KMEM_DUMP(cp) && buf != NULL) {
29257c478bd9Sstevel@tonic-gate 		kmem_buftag_t *btp = KMEM_BUFTAG(cp, buf);
29267c478bd9Sstevel@tonic-gate 		((uint8_t *)buf)[size] = KMEM_REDZONE_BYTE;
29277c478bd9Sstevel@tonic-gate 		((uint32_t *)btp)[1] = KMEM_SIZE_ENCODE(size);
29287c478bd9Sstevel@tonic-gate 
29297c478bd9Sstevel@tonic-gate 		if (cp->cache_flags & KMF_LITE) {
2930dce01e3fSJonathan W Adams 			KMEM_BUFTAG_LITE_ENTER(btp, kmem_lite_count, caller());
29317c478bd9Sstevel@tonic-gate 		}
29327c478bd9Sstevel@tonic-gate 	}
29337c478bd9Sstevel@tonic-gate 	return (buf);
29347c478bd9Sstevel@tonic-gate }
29357c478bd9Sstevel@tonic-gate 
29367c478bd9Sstevel@tonic-gate void
29377c478bd9Sstevel@tonic-gate kmem_free(void *buf, size_t size)
29387c478bd9Sstevel@tonic-gate {
2939dce01e3fSJonathan W Adams 	size_t index;
2940dce01e3fSJonathan W Adams 	kmem_cache_t *cp;
29417c478bd9Sstevel@tonic-gate 
2942dce01e3fSJonathan W Adams 	if ((index = (size - 1) >> KMEM_ALIGN_SHIFT) < KMEM_ALLOC_TABLE_MAX) {
2943dce01e3fSJonathan W Adams 		cp = kmem_alloc_table[index];
2944dce01e3fSJonathan W Adams 		/* fall through to kmem_cache_free() */
2945dce01e3fSJonathan W Adams 
2946dce01e3fSJonathan W Adams 	} else if ((index = ((size - 1) >> KMEM_BIG_SHIFT)) <
2947dce01e3fSJonathan W Adams 	    kmem_big_alloc_table_max) {
2948dce01e3fSJonathan W Adams 		cp = kmem_big_alloc_table[index];
2949dce01e3fSJonathan W Adams 		/* fall through to kmem_cache_free() */
2950dce01e3fSJonathan W Adams 
2951dce01e3fSJonathan W Adams 	} else {
295296992ee7SEthindra Ramamurthy 		EQUIV(buf == NULL, size == 0);
2953dce01e3fSJonathan W Adams 		if (buf == NULL && size == 0)
2954dce01e3fSJonathan W Adams 			return;
2955dce01e3fSJonathan W Adams 		vmem_free(kmem_oversize_arena, buf, size);
2956dce01e3fSJonathan W Adams 		return;
2957dce01e3fSJonathan W Adams 	}
2958dce01e3fSJonathan W Adams 
29599dd77bc8SDave Plauger 	if ((cp->cache_flags & KMF_BUFTAG) && !KMEM_DUMP(cp)) {
29607c478bd9Sstevel@tonic-gate 		kmem_buftag_t *btp = KMEM_BUFTAG(cp, buf);
29617c478bd9Sstevel@tonic-gate 		uint32_t *ip = (uint32_t *)btp;
29627c478bd9Sstevel@tonic-gate 		if (ip[1] != KMEM_SIZE_ENCODE(size)) {
29637c478bd9Sstevel@tonic-gate 			if (*(uint64_t *)buf == KMEM_FREE_PATTERN) {
29647c478bd9Sstevel@tonic-gate 				kmem_error(KMERR_DUPFREE, cp, buf);
29657c478bd9Sstevel@tonic-gate 				return;
29667c478bd9Sstevel@tonic-gate 			}
29677c478bd9Sstevel@tonic-gate 			if (KMEM_SIZE_VALID(ip[1])) {
29687c478bd9Sstevel@tonic-gate 				ip[0] = KMEM_SIZE_ENCODE(size);
29697c478bd9Sstevel@tonic-gate 				kmem_error(KMERR_BADSIZE, cp, buf);
29707c478bd9Sstevel@tonic-gate 			} else {
29717c478bd9Sstevel@tonic-gate 				kmem_error(KMERR_REDZONE, cp, buf);
29727c478bd9Sstevel@tonic-gate 			}
29737c478bd9Sstevel@tonic-gate 			return;
29747c478bd9Sstevel@tonic-gate 		}
29757c478bd9Sstevel@tonic-gate 		if (((uint8_t *)buf)[size] != KMEM_REDZONE_BYTE) {
29767c478bd9Sstevel@tonic-gate 			kmem_error(KMERR_REDZONE, cp, buf);
29777c478bd9Sstevel@tonic-gate 			return;
29787c478bd9Sstevel@tonic-gate 		}
29797c478bd9Sstevel@tonic-gate 		btp->bt_redzone = KMEM_REDZONE_PATTERN;
29807c478bd9Sstevel@tonic-gate 		if (cp->cache_flags & KMF_LITE) {
29817c478bd9Sstevel@tonic-gate 			KMEM_BUFTAG_LITE_ENTER(btp, kmem_lite_count,
29827c478bd9Sstevel@tonic-gate 			    caller());
29837c478bd9Sstevel@tonic-gate 		}
29847c478bd9Sstevel@tonic-gate 	}
29857c478bd9Sstevel@tonic-gate 	kmem_cache_free(cp, buf);
29867c478bd9Sstevel@tonic-gate }
29877c478bd9Sstevel@tonic-gate 
29887c478bd9Sstevel@tonic-gate void *
29897c478bd9Sstevel@tonic-gate kmem_firewall_va_alloc(vmem_t *vmp, size_t size, int vmflag)
29907c478bd9Sstevel@tonic-gate {
29917c478bd9Sstevel@tonic-gate 	size_t realsize = size + vmp->vm_quantum;
29927c478bd9Sstevel@tonic-gate 	void *addr;
29937c478bd9Sstevel@tonic-gate 
29947c478bd9Sstevel@tonic-gate 	/*
29957c478bd9Sstevel@tonic-gate 	 * Annoying edge case: if 'size' is just shy of ULONG_MAX, adding
29967c478bd9Sstevel@tonic-gate 	 * vm_quantum will cause integer wraparound.  Check for this, and
29977c478bd9Sstevel@tonic-gate 	 * blow off the firewall page in this case.  Note that such a
29987c478bd9Sstevel@tonic-gate 	 * giant allocation (the entire kernel address space) can never
29997c478bd9Sstevel@tonic-gate 	 * be satisfied, so it will either fail immediately (VM_NOSLEEP)
30007c478bd9Sstevel@tonic-gate 	 * or sleep forever (VM_SLEEP).  Thus, there is no need for a
30017c478bd9Sstevel@tonic-gate 	 * corresponding check in kmem_firewall_va_free().
30027c478bd9Sstevel@tonic-gate 	 */
30037c478bd9Sstevel@tonic-gate 	if (realsize < size)
30047c478bd9Sstevel@tonic-gate 		realsize = size;
30057c478bd9Sstevel@tonic-gate 
30067c478bd9Sstevel@tonic-gate 	/*
30077c478bd9Sstevel@tonic-gate 	 * While boot still owns resource management, make sure that this
30087c478bd9Sstevel@tonic-gate 	 * redzone virtual address allocation is properly accounted for in
30097c478bd9Sstevel@tonic-gate 	 * OBPs "virtual-memory" "available" lists because we're
30107c478bd9Sstevel@tonic-gate 	 * effectively claiming them for a red zone.  If we don't do this,
30117c478bd9Sstevel@tonic-gate 	 * the available lists become too fragmented and too large for the
30127c478bd9Sstevel@tonic-gate 	 * current boot/kernel memory list interface.
30137c478bd9Sstevel@tonic-gate 	 */
30147c478bd9Sstevel@tonic-gate 	addr = vmem_alloc(vmp, realsize, vmflag | VM_NEXTFIT);
30157c478bd9Sstevel@tonic-gate 
30167c478bd9Sstevel@tonic-gate 	if (addr != NULL && kvseg.s_base == NULL && realsize != size)
30177c478bd9Sstevel@tonic-gate 		(void) boot_virt_alloc((char *)addr + size, vmp->vm_quantum);
30187c478bd9Sstevel@tonic-gate 
30197c478bd9Sstevel@tonic-gate 	return (addr);
30207c478bd9Sstevel@tonic-gate }
30217c478bd9Sstevel@tonic-gate 
30227c478bd9Sstevel@tonic-gate void
30237c478bd9Sstevel@tonic-gate kmem_firewall_va_free(vmem_t *vmp, void *addr, size_t size)
30247c478bd9Sstevel@tonic-gate {
30257c478bd9Sstevel@tonic-gate 	ASSERT((kvseg.s_base == NULL ?
30267c478bd9Sstevel@tonic-gate 	    va_to_pfn((char *)addr + size) :
30277c478bd9Sstevel@tonic-gate 	    hat_getpfnum(kas.a_hat, (caddr_t)addr + size)) == PFN_INVALID);
30287c478bd9Sstevel@tonic-gate 
30297c478bd9Sstevel@tonic-gate 	vmem_free(vmp, addr, size + vmp->vm_quantum);
30307c478bd9Sstevel@tonic-gate }
30317c478bd9Sstevel@tonic-gate 
30327c478bd9Sstevel@tonic-gate /*
30337c478bd9Sstevel@tonic-gate  * Try to allocate at least `size' bytes of memory without sleeping or
30347c478bd9Sstevel@tonic-gate  * panicking. Return actual allocated size in `asize'. If allocation failed,
30357c478bd9Sstevel@tonic-gate  * try final allocation with sleep or panic allowed.
30367c478bd9Sstevel@tonic-gate  */
30377c478bd9Sstevel@tonic-gate void *
30387c478bd9Sstevel@tonic-gate kmem_alloc_tryhard(size_t size, size_t *asize, int kmflag)
30397c478bd9Sstevel@tonic-gate {
30407c478bd9Sstevel@tonic-gate 	void *p;
30417c478bd9Sstevel@tonic-gate 
30427c478bd9Sstevel@tonic-gate 	*asize = P2ROUNDUP(size, KMEM_ALIGN);
30437c478bd9Sstevel@tonic-gate 	do {
30447c478bd9Sstevel@tonic-gate 		p = kmem_alloc(*asize, (kmflag | KM_NOSLEEP) & ~KM_PANIC);
30457c478bd9Sstevel@tonic-gate 		if (p != NULL)
30467c478bd9Sstevel@tonic-gate 			return (p);
30477c478bd9Sstevel@tonic-gate 		*asize += KMEM_ALIGN;
30487c478bd9Sstevel@tonic-gate 	} while (*asize <= PAGESIZE);
30497c478bd9Sstevel@tonic-gate 
30507c478bd9Sstevel@tonic-gate 	*asize = P2ROUNDUP(size, KMEM_ALIGN);
30517c478bd9Sstevel@tonic-gate 	return (kmem_alloc(*asize, kmflag));
30527c478bd9Sstevel@tonic-gate }
30537c478bd9Sstevel@tonic-gate 
30547c478bd9Sstevel@tonic-gate /*
30557c478bd9Sstevel@tonic-gate  * Reclaim all unused memory from a cache.
30567c478bd9Sstevel@tonic-gate  */
30577c478bd9Sstevel@tonic-gate static void
30587c478bd9Sstevel@tonic-gate kmem_cache_reap(kmem_cache_t *cp)
30597c478bd9Sstevel@tonic-gate {
3060b5fca8f8Stomee 	ASSERT(taskq_member(kmem_taskq, curthread));
3061686031edSTom Erickson 	cp->cache_reap++;
3062b5fca8f8Stomee 
30637c478bd9Sstevel@tonic-gate 	/*
30647c478bd9Sstevel@tonic-gate 	 * Ask the cache's owner to free some memory if possible.
30657c478bd9Sstevel@tonic-gate 	 * The idea is to handle things like the inode cache, which
30667c478bd9Sstevel@tonic-gate 	 * typically sits on a bunch of memory that it doesn't truly
30677c478bd9Sstevel@tonic-gate 	 * *need*.  Reclaim policy is entirely up to the owner; this
30687c478bd9Sstevel@tonic-gate 	 * callback is just an advisory plea for help.
30697c478bd9Sstevel@tonic-gate 	 */
3070b5fca8f8Stomee 	if (cp->cache_reclaim != NULL) {
3071b5fca8f8Stomee 		long delta;
3072b5fca8f8Stomee 
3073b5fca8f8Stomee 		/*
3074b5fca8f8Stomee 		 * Reclaimed memory should be reapable (not included in the
3075b5fca8f8Stomee 		 * depot's working set).
3076b5fca8f8Stomee 		 */
3077b5fca8f8Stomee 		delta = cp->cache_full.ml_total;
30787c478bd9Sstevel@tonic-gate 		cp->cache_reclaim(cp->cache_private);
3079b5fca8f8Stomee 		delta = cp->cache_full.ml_total - delta;
3080b5fca8f8Stomee 		if (delta > 0) {
3081b5fca8f8Stomee 			mutex_enter(&cp->cache_depot_lock);
3082b5fca8f8Stomee 			cp->cache_full.ml_reaplimit += delta;
3083b5fca8f8Stomee 			cp->cache_full.ml_min += delta;
3084b5fca8f8Stomee 			mutex_exit(&cp->cache_depot_lock);
3085b5fca8f8Stomee 		}
3086b5fca8f8Stomee 	}
30877c478bd9Sstevel@tonic-gate 
30887c478bd9Sstevel@tonic-gate 	kmem_depot_ws_reap(cp);
3089b5fca8f8Stomee 
3090b5fca8f8Stomee 	if (cp->cache_defrag != NULL && !kmem_move_noreap) {
3091b5fca8f8Stomee 		kmem_cache_defrag(cp);
3092b5fca8f8Stomee 	}
30937c478bd9Sstevel@tonic-gate }
30947c478bd9Sstevel@tonic-gate 
30957c478bd9Sstevel@tonic-gate static void
30967c478bd9Sstevel@tonic-gate kmem_reap_timeout(void *flag_arg)
30977c478bd9Sstevel@tonic-gate {
30987c478bd9Sstevel@tonic-gate 	uint32_t *flag = (uint32_t *)flag_arg;
30997c478bd9Sstevel@tonic-gate 
31007c478bd9Sstevel@tonic-gate 	ASSERT(flag == &kmem_reaping || flag == &kmem_reaping_idspace);
31017c478bd9Sstevel@tonic-gate 	*flag = 0;
31027c478bd9Sstevel@tonic-gate }
31037c478bd9Sstevel@tonic-gate 
31047c478bd9Sstevel@tonic-gate static void
31057c478bd9Sstevel@tonic-gate kmem_reap_done(void *flag)
31067c478bd9Sstevel@tonic-gate {
31076e00b116SPeter Telford 	if (!callout_init_done) {
31086e00b116SPeter Telford 		/* can't schedule a timeout at this point */
31096e00b116SPeter Telford 		kmem_reap_timeout(flag);
31106e00b116SPeter Telford 	} else {
31117c478bd9Sstevel@tonic-gate 		(void) timeout(kmem_reap_timeout, flag, kmem_reap_interval);
31127c478bd9Sstevel@tonic-gate 	}
31136e00b116SPeter Telford }
31147c478bd9Sstevel@tonic-gate 
31157c478bd9Sstevel@tonic-gate static void
31167c478bd9Sstevel@tonic-gate kmem_reap_start(void *flag)
31177c478bd9Sstevel@tonic-gate {
31187c478bd9Sstevel@tonic-gate 	ASSERT(flag == &kmem_reaping || flag == &kmem_reaping_idspace);
31197c478bd9Sstevel@tonic-gate 
31207c478bd9Sstevel@tonic-gate 	if (flag == &kmem_reaping) {
31217c478bd9Sstevel@tonic-gate 		kmem_cache_applyall(kmem_cache_reap, kmem_taskq, TQ_NOSLEEP);
31227c478bd9Sstevel@tonic-gate 		/*
31237c478bd9Sstevel@tonic-gate 		 * if we have segkp under heap, reap segkp cache.
31247c478bd9Sstevel@tonic-gate 		 */
31257c478bd9Sstevel@tonic-gate 		if (segkp_fromheap)
31267c478bd9Sstevel@tonic-gate 			segkp_cache_free();
31277c478bd9Sstevel@tonic-gate 	}
31287c478bd9Sstevel@tonic-gate 	else
31297c478bd9Sstevel@tonic-gate 		kmem_cache_applyall_id(kmem_cache_reap, kmem_taskq, TQ_NOSLEEP);
31307c478bd9Sstevel@tonic-gate 
31317c478bd9Sstevel@tonic-gate 	/*
31327c478bd9Sstevel@tonic-gate 	 * We use taskq_dispatch() to schedule a timeout to clear
31337c478bd9Sstevel@tonic-gate 	 * the flag so that kmem_reap() becomes self-throttling:
31347c478bd9Sstevel@tonic-gate 	 * we won't reap again until the current reap completes *and*
31357c478bd9Sstevel@tonic-gate 	 * at least kmem_reap_interval ticks have elapsed.
31367c478bd9Sstevel@tonic-gate 	 */
31377c478bd9Sstevel@tonic-gate 	if (!taskq_dispatch(kmem_taskq, kmem_reap_done, flag, TQ_NOSLEEP))
31387c478bd9Sstevel@tonic-gate 		kmem_reap_done(flag);
31397c478bd9Sstevel@tonic-gate }
31407c478bd9Sstevel@tonic-gate 
31417c478bd9Sstevel@tonic-gate static void
31427c478bd9Sstevel@tonic-gate kmem_reap_common(void *flag_arg)
31437c478bd9Sstevel@tonic-gate {
31447c478bd9Sstevel@tonic-gate 	uint32_t *flag = (uint32_t *)flag_arg;
31457c478bd9Sstevel@tonic-gate 
31467c478bd9Sstevel@tonic-gate 	if (MUTEX_HELD(&kmem_cache_lock) || kmem_taskq == NULL ||
314775d94465SJosef 'Jeff' Sipek 	    atomic_cas_32(flag, 0, 1) != 0)
31487c478bd9Sstevel@tonic-gate 		return;
31497c478bd9Sstevel@tonic-gate 
31507c478bd9Sstevel@tonic-gate 	/*
31517c478bd9Sstevel@tonic-gate 	 * It may not be kosher to do memory allocation when a reap is called
31529321cd04SJosef 'Jeff' Sipek 	 * (for example, if vmem_populate() is in the call chain).  So we
31539321cd04SJosef 'Jeff' Sipek 	 * start the reap going with a TQ_NOALLOC dispatch.  If the dispatch
31549321cd04SJosef 'Jeff' Sipek 	 * fails, we reset the flag, and the next reap will try again.
31557c478bd9Sstevel@tonic-gate 	 */
31567c478bd9Sstevel@tonic-gate 	if (!taskq_dispatch(kmem_taskq, kmem_reap_start, flag, TQ_NOALLOC))
31577c478bd9Sstevel@tonic-gate 		*flag = 0;
31587c478bd9Sstevel@tonic-gate }
31597c478bd9Sstevel@tonic-gate 
31607c478bd9Sstevel@tonic-gate /*
31617c478bd9Sstevel@tonic-gate  * Reclaim all unused memory from all caches.  Called from the VM system
31627c478bd9Sstevel@tonic-gate  * when memory gets tight.
31637c478bd9Sstevel@tonic-gate  */
31647c478bd9Sstevel@tonic-gate void
31657c478bd9Sstevel@tonic-gate kmem_reap(void)
31667c478bd9Sstevel@tonic-gate {
31677c478bd9Sstevel@tonic-gate 	kmem_reap_common(&kmem_reaping);
31687c478bd9Sstevel@tonic-gate }
31697c478bd9Sstevel@tonic-gate 
31707c478bd9Sstevel@tonic-gate /*
31717c478bd9Sstevel@tonic-gate  * Reclaim all unused memory from identifier arenas, called when a vmem
31727c478bd9Sstevel@tonic-gate  * arena not back by memory is exhausted.  Since reaping memory-backed caches
31737c478bd9Sstevel@tonic-gate  * cannot help with identifier exhaustion, we avoid both a large amount of
31747c478bd9Sstevel@tonic-gate  * work and unwanted side-effects from reclaim callbacks.
31757c478bd9Sstevel@tonic-gate  */
31767c478bd9Sstevel@tonic-gate void
31777c478bd9Sstevel@tonic-gate kmem_reap_idspace(void)
31787c478bd9Sstevel@tonic-gate {
31797c478bd9Sstevel@tonic-gate 	kmem_reap_common(&kmem_reaping_idspace);
31807c478bd9Sstevel@tonic-gate }
31817c478bd9Sstevel@tonic-gate 
31827c478bd9Sstevel@tonic-gate /*
31837c478bd9Sstevel@tonic-gate  * Purge all magazines from a cache and set its magazine limit to zero.
31847c478bd9Sstevel@tonic-gate  * All calls are serialized by the kmem_taskq lock, except for the final
31857c478bd9Sstevel@tonic-gate  * call from kmem_cache_destroy().
31867c478bd9Sstevel@tonic-gate  */
31877c478bd9Sstevel@tonic-gate static void
31887c478bd9Sstevel@tonic-gate kmem_cache_magazine_purge(kmem_cache_t *cp)
31897c478bd9Sstevel@tonic-gate {
31907c478bd9Sstevel@tonic-gate 	kmem_cpu_cache_t *ccp;
31917c478bd9Sstevel@tonic-gate 	kmem_magazine_t *mp, *pmp;
31927c478bd9Sstevel@tonic-gate 	int rounds, prounds, cpu_seqid;
31937c478bd9Sstevel@tonic-gate 
3194b5fca8f8Stomee 	ASSERT(!list_link_active(&cp->cache_link) ||
3195b5fca8f8Stomee 	    taskq_member(kmem_taskq, curthread));
31967c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&cp->cache_lock));
31977c478bd9Sstevel@tonic-gate 
31987c478bd9Sstevel@tonic-gate 	for (cpu_seqid = 0; cpu_seqid < max_ncpus; cpu_seqid++) {
31997c478bd9Sstevel@tonic-gate 		ccp = &cp->cache_cpu[cpu_seqid];
32007c478bd9Sstevel@tonic-gate 
32017c478bd9Sstevel@tonic-gate 		mutex_enter(&ccp->cc_lock);
32027c478bd9Sstevel@tonic-gate 		mp = ccp->cc_loaded;
32037c478bd9Sstevel@tonic-gate 		pmp = ccp->cc_ploaded;
32047c478bd9Sstevel@tonic-gate 		rounds = ccp->cc_rounds;
32057c478bd9Sstevel@tonic-gate 		prounds = ccp->cc_prounds;
32067c478bd9Sstevel@tonic-gate 		ccp->cc_loaded = NULL;
32077c478bd9Sstevel@tonic-gate 		ccp->cc_ploaded = NULL;
32087c478bd9Sstevel@tonic-gate 		ccp->cc_rounds = -1;
32097c478bd9Sstevel@tonic-gate 		ccp->cc_prounds = -1;
32107c478bd9Sstevel@tonic-gate 		ccp->cc_magsize = 0;
32117c478bd9Sstevel@tonic-gate 		mutex_exit(&ccp->cc_lock);
32127c478bd9Sstevel@tonic-gate 
32137c478bd9Sstevel@tonic-gate 		if (mp)
32147c478bd9Sstevel@tonic-gate 			kmem_magazine_destroy(cp, mp, rounds);
32157c478bd9Sstevel@tonic-gate 		if (pmp)
32167c478bd9Sstevel@tonic-gate 			kmem_magazine_destroy(cp, pmp, prounds);
32177c478bd9Sstevel@tonic-gate 	}
32187c478bd9Sstevel@tonic-gate 
32190c833d64SJosef 'Jeff' Sipek 	kmem_depot_ws_zero(cp);
32207c478bd9Sstevel@tonic-gate 	kmem_depot_ws_reap(cp);
32217c478bd9Sstevel@tonic-gate }
32227c478bd9Sstevel@tonic-gate 
32237c478bd9Sstevel@tonic-gate /*
32247c478bd9Sstevel@tonic-gate  * Enable per-cpu magazines on a cache.
32257c478bd9Sstevel@tonic-gate  */
32267c478bd9Sstevel@tonic-gate static void
32277c478bd9Sstevel@tonic-gate kmem_cache_magazine_enable(kmem_cache_t *cp)
32287c478bd9Sstevel@tonic-gate {
32297c478bd9Sstevel@tonic-gate 	int cpu_seqid;
32307c478bd9Sstevel@tonic-gate 
32317c478bd9Sstevel@tonic-gate 	if (cp->cache_flags & KMF_NOMAGAZINE)
32327c478bd9Sstevel@tonic-gate 		return;
32337c478bd9Sstevel@tonic-gate 
32347c478bd9Sstevel@tonic-gate 	for (cpu_seqid = 0; cpu_seqid < max_ncpus; cpu_seqid++) {
32357c478bd9Sstevel@tonic-gate 		kmem_cpu_cache_t *ccp = &cp->cache_cpu[cpu_seqid];
32367c478bd9Sstevel@tonic-gate 		mutex_enter(&ccp->cc_lock);
32377c478bd9Sstevel@tonic-gate 		ccp->cc_magsize = cp->cache_magtype->mt_magsize;
32387c478bd9Sstevel@tonic-gate 		mutex_exit(&ccp->cc_lock);
32397c478bd9Sstevel@tonic-gate 	}
32407c478bd9Sstevel@tonic-gate 
32417c478bd9Sstevel@tonic-gate }
32427c478bd9Sstevel@tonic-gate 
32437c478bd9Sstevel@tonic-gate /*
32440c833d64SJosef 'Jeff' Sipek  * Reap (almost) everything right now.
3245fa9e4066Sahrens  */
3246fa9e4066Sahrens void
3247fa9e4066Sahrens kmem_cache_reap_now(kmem_cache_t *cp)
3248fa9e4066Sahrens {
3249b5fca8f8Stomee 	ASSERT(list_link_active(&cp->cache_link));
3250b5fca8f8Stomee 
32510c833d64SJosef 'Jeff' Sipek 	kmem_depot_ws_zero(cp);
3252fa9e4066Sahrens 
3253fa9e4066Sahrens 	(void) taskq_dispatch(kmem_taskq,
3254fa9e4066Sahrens 	    (task_func_t *)kmem_depot_ws_reap, cp, TQ_SLEEP);
3255fa9e4066Sahrens 	taskq_wait(kmem_taskq);
3256fa9e4066Sahrens }
3257fa9e4066Sahrens 
3258fa9e4066Sahrens /*
32597c478bd9Sstevel@tonic-gate  * Recompute a cache's magazine size.  The trade-off is that larger magazines
32607c478bd9Sstevel@tonic-gate  * provide a higher transfer rate with the depot, while smaller magazines
32617c478bd9Sstevel@tonic-gate  * reduce memory consumption.  Magazine resizing is an expensive operation;
32627c478bd9Sstevel@tonic-gate  * it should not be done frequently.
32637c478bd9Sstevel@tonic-gate  *
32647c478bd9Sstevel@tonic-gate  * Changes to the magazine size are serialized by the kmem_taskq lock.
32657c478bd9Sstevel@tonic-gate  *
32667c478bd9Sstevel@tonic-gate  * Note: at present this only grows the magazine size.  It might be useful
32677c478bd9Sstevel@tonic-gate  * to allow shrinkage too.
32687c478bd9Sstevel@tonic-gate  */
32697c478bd9Sstevel@tonic-gate static void
32707c478bd9Sstevel@tonic-gate kmem_cache_magazine_resize(kmem_cache_t *cp)
32717c478bd9Sstevel@tonic-gate {
32727c478bd9Sstevel@tonic-gate 	kmem_magtype_t *mtp = cp->cache_magtype;
32737c478bd9Sstevel@tonic-gate 
32747c478bd9Sstevel@tonic-gate 	ASSERT(taskq_member(kmem_taskq, curthread));
32757c478bd9Sstevel@tonic-gate 
32767c478bd9Sstevel@tonic-gate 	if (cp->cache_chunksize < mtp->mt_maxbuf) {
32777c478bd9Sstevel@tonic-gate 		kmem_cache_magazine_purge(cp);
32787c478bd9Sstevel@tonic-gate 		mutex_enter(&cp->cache_depot_lock);
32797c478bd9Sstevel@tonic-gate 		cp->cache_magtype = ++mtp;
32807c478bd9Sstevel@tonic-gate 		cp->cache_depot_contention_prev =
32817c478bd9Sstevel@tonic-gate 		    cp->cache_depot_contention + INT_MAX;
32827c478bd9Sstevel@tonic-gate 		mutex_exit(&cp->cache_depot_lock);
32837c478bd9Sstevel@tonic-gate 		kmem_cache_magazine_enable(cp);
32847c478bd9Sstevel@tonic-gate 	}
32857c478bd9Sstevel@tonic-gate }
32867c478bd9Sstevel@tonic-gate 
32877c478bd9Sstevel@tonic-gate /*
32887c478bd9Sstevel@tonic-gate  * Rescale a cache's hash table, so that the table size is roughly the
32897c478bd9Sstevel@tonic-gate  * cache size.  We want the average lookup time to be extremely small.
32907c478bd9Sstevel@tonic-gate  */
32917c478bd9Sstevel@tonic-gate static void
32927c478bd9Sstevel@tonic-gate kmem_hash_rescale(kmem_cache_t *cp)
32937c478bd9Sstevel@tonic-gate {
32947c478bd9Sstevel@tonic-gate 	kmem_bufctl_t **old_table, **new_table, *bcp;
32957c478bd9Sstevel@tonic-gate 	size_t old_size, new_size, h;
32967c478bd9Sstevel@tonic-gate 
32977c478bd9Sstevel@tonic-gate 	ASSERT(taskq_member(kmem_taskq, curthread));
32987c478bd9Sstevel@tonic-gate 
32997c478bd9Sstevel@tonic-gate 	new_size = MAX(KMEM_HASH_INITIAL,
33007c478bd9Sstevel@tonic-gate 	    1 << (highbit(3 * cp->cache_buftotal + 4) - 2));
33017c478bd9Sstevel@tonic-gate 	old_size = cp->cache_hash_mask + 1;
33027c478bd9Sstevel@tonic-gate 
33037c478bd9Sstevel@tonic-gate 	if ((old_size >> 1) <= new_size && new_size <= (old_size << 1))
33047c478bd9Sstevel@tonic-gate 		return;
33057c478bd9Sstevel@tonic-gate 
33067c478bd9Sstevel@tonic-gate 	new_table = vmem_alloc(kmem_hash_arena, new_size * sizeof (void *),
33077c478bd9Sstevel@tonic-gate 	    VM_NOSLEEP);
33087c478bd9Sstevel@tonic-gate 	if (new_table == NULL)
33097c478bd9Sstevel@tonic-gate 		return;
33107c478bd9Sstevel@tonic-gate 	bzero(new_table, new_size * sizeof (void *));
33117c478bd9Sstevel@tonic-gate 
33127c478bd9Sstevel@tonic-gate 	mutex_enter(&cp->cache_lock);
33137c478bd9Sstevel@tonic-gate 
33147c478bd9Sstevel@tonic-gate 	old_size = cp->cache_hash_mask + 1;
33157c478bd9Sstevel@tonic-gate 	old_table = cp->cache_hash_table;
33167c478bd9Sstevel@tonic-gate 
33177c478bd9Sstevel@tonic-gate 	cp->cache_hash_mask = new_size - 1;
33187c478bd9Sstevel@tonic-gate 	cp->cache_hash_table = new_table;
33197c478bd9Sstevel@tonic-gate 	cp->cache_rescale++;
33207c478bd9Sstevel@tonic-gate 
33217c478bd9Sstevel@tonic-gate 	for (h = 0; h < old_size; h++) {
33227c478bd9Sstevel@tonic-gate 		bcp = old_table[h];
33237c478bd9Sstevel@tonic-gate 		while (bcp != NULL) {
33247c478bd9Sstevel@tonic-gate 			void *addr = bcp->bc_addr;
33257c478bd9Sstevel@tonic-gate 			kmem_bufctl_t *next_bcp = bcp->bc_next;
33267c478bd9Sstevel@tonic-gate 			kmem_bufctl_t **hash_bucket = KMEM_HASH(cp, addr);
33277c478bd9Sstevel@tonic-gate 			bcp->bc_next = *hash_bucket;
33287c478bd9Sstevel@tonic-gate 			*hash_bucket = bcp;
33297c478bd9Sstevel@tonic-gate 			bcp = next_bcp;
33307c478bd9Sstevel@tonic-gate 		}
33317c478bd9Sstevel@tonic-gate 	}
33327c478bd9Sstevel@tonic-gate 
33337c478bd9Sstevel@tonic-gate 	mutex_exit(&cp->cache_lock);
33347c478bd9Sstevel@tonic-gate 
33357c478bd9Sstevel@tonic-gate 	vmem_free(kmem_hash_arena, old_table, old_size * sizeof (void *));
33367c478bd9Sstevel@tonic-gate }
33377c478bd9Sstevel@tonic-gate 
33387c478bd9Sstevel@tonic-gate /*
3339b5fca8f8Stomee  * Perform periodic maintenance on a cache: hash rescaling, depot working-set
3340b5fca8f8Stomee  * update, magazine resizing, and slab consolidation.
33417c478bd9Sstevel@tonic-gate  */
33427c478bd9Sstevel@tonic-gate static void
33437c478bd9Sstevel@tonic-gate kmem_cache_update(kmem_cache_t *cp)
33447c478bd9Sstevel@tonic-gate {
33457c478bd9Sstevel@tonic-gate 	int need_hash_rescale = 0;
33467c478bd9Sstevel@tonic-gate 	int need_magazine_resize = 0;
33477c478bd9Sstevel@tonic-gate 
33487c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&kmem_cache_lock));
33497c478bd9Sstevel@tonic-gate 
33507c478bd9Sstevel@tonic-gate 	/*
33517c478bd9Sstevel@tonic-gate 	 * If the cache has become much larger or smaller than its hash table,
33527c478bd9Sstevel@tonic-gate 	 * fire off a request to rescale the hash table.
33537c478bd9Sstevel@tonic-gate 	 */
33547c478bd9Sstevel@tonic-gate 	mutex_enter(&cp->cache_lock);
33557c478bd9Sstevel@tonic-gate 
33567c478bd9Sstevel@tonic-gate 	if ((cp->cache_flags & KMF_HASH) &&
33577c478bd9Sstevel@tonic-gate 	    (cp->cache_buftotal > (cp->cache_hash_mask << 1) ||
33587c478bd9Sstevel@tonic-gate 	    (cp->cache_buftotal < (cp->cache_hash_mask >> 1) &&
33597c478bd9Sstevel@tonic-gate 	    cp->cache_hash_mask > KMEM_HASH_INITIAL)))
33607c478bd9Sstevel@tonic-gate 		need_hash_rescale = 1;
33617c478bd9Sstevel@tonic-gate 
33627c478bd9Sstevel@tonic-gate 	mutex_exit(&cp->cache_lock);
33637c478bd9Sstevel@tonic-gate 
33647c478bd9Sstevel@tonic-gate 	/*
33657c478bd9Sstevel@tonic-gate 	 * Update the depot working set statistics.
33667c478bd9Sstevel@tonic-gate 	 */
33677c478bd9Sstevel@tonic-gate 	kmem_depot_ws_update(cp);
33687c478bd9Sstevel@tonic-gate 
33697c478bd9Sstevel@tonic-gate 	/*
33707c478bd9Sstevel@tonic-gate 	 * If there's a lot of contention in the depot,
33717c478bd9Sstevel@tonic-gate 	 * increase the magazine size.
33727c478bd9Sstevel@tonic-gate 	 */
33737c478bd9Sstevel@tonic-gate 	mutex_enter(&cp->cache_depot_lock);
33747c478bd9Sstevel@tonic-gate 
33757c478bd9Sstevel@tonic-gate 	if (cp->cache_chunksize < cp->cache_magtype->mt_maxbuf &&
33767c478bd9Sstevel@tonic-gate 	    (int)(cp->cache_depot_contention -
33777c478bd9Sstevel@tonic-gate 	    cp->cache_depot_contention_prev) > kmem_depot_contention)
33787c478bd9Sstevel@tonic-gate 		need_magazine_resize = 1;
33797c478bd9Sstevel@tonic-gate 
33807c478bd9Sstevel@tonic-gate 	cp->cache_depot_contention_prev = cp->cache_depot_contention;
33817c478bd9Sstevel@tonic-gate 
33827c478bd9Sstevel@tonic-gate 	mutex_exit(&cp->cache_depot_lock);
33837c478bd9Sstevel@tonic-gate 
33847c478bd9Sstevel@tonic-gate 	if (need_hash_rescale)
33857c478bd9Sstevel@tonic-gate 		(void) taskq_dispatch(kmem_taskq,
33867c478bd9Sstevel@tonic-gate 		    (task_func_t *)kmem_hash_rescale, cp, TQ_NOSLEEP);
33877c478bd9Sstevel@tonic-gate 
33887c478bd9Sstevel@tonic-gate 	if (need_magazine_resize)
33897c478bd9Sstevel@tonic-gate 		(void) taskq_dispatch(kmem_taskq,
33907c478bd9Sstevel@tonic-gate 		    (task_func_t *)kmem_cache_magazine_resize, cp, TQ_NOSLEEP);
3391b5fca8f8Stomee 
3392b5fca8f8Stomee 	if (cp->cache_defrag != NULL)
3393b5fca8f8Stomee 		(void) taskq_dispatch(kmem_taskq,
3394b5fca8f8Stomee 		    (task_func_t *)kmem_cache_scan, cp, TQ_NOSLEEP);
33957c478bd9Sstevel@tonic-gate }
33967c478bd9Sstevel@tonic-gate 
3397d67944fbSScott Rotondo static void kmem_update(void *);
3398d67944fbSScott Rotondo 
33997c478bd9Sstevel@tonic-gate static void
34007c478bd9Sstevel@tonic-gate kmem_update_timeout(void *dummy)
34017c478bd9Sstevel@tonic-gate {
34027c478bd9Sstevel@tonic-gate 	(void) timeout(kmem_update, dummy, kmem_reap_interval);
34037c478bd9Sstevel@tonic-gate }
34047c478bd9Sstevel@tonic-gate 
34057c478bd9Sstevel@tonic-gate static void
34067c478bd9Sstevel@tonic-gate kmem_update(void *dummy)
34077c478bd9Sstevel@tonic-gate {
34087c478bd9Sstevel@tonic-gate 	kmem_cache_applyall(kmem_cache_update, NULL, TQ_NOSLEEP);
34097c478bd9Sstevel@tonic-gate 
34107c478bd9Sstevel@tonic-gate 	/*
34117c478bd9Sstevel@tonic-gate 	 * We use taskq_dispatch() to reschedule the timeout so that
34127c478bd9Sstevel@tonic-gate 	 * kmem_update() becomes self-throttling: it won't schedule
34137c478bd9Sstevel@tonic-gate 	 * new tasks until all previous tasks have completed.
34147c478bd9Sstevel@tonic-gate 	 */
34157c478bd9Sstevel@tonic-gate 	if (!taskq_dispatch(kmem_taskq, kmem_update_timeout, dummy, TQ_NOSLEEP))
34167c478bd9Sstevel@tonic-gate 		kmem_update_timeout(NULL);
34177c478bd9Sstevel@tonic-gate }
34187c478bd9Sstevel@tonic-gate 
34197c478bd9Sstevel@tonic-gate static int
34207c478bd9Sstevel@tonic-gate kmem_cache_kstat_update(kstat_t *ksp, int rw)
34217c478bd9Sstevel@tonic-gate {
34227c478bd9Sstevel@tonic-gate 	struct kmem_cache_kstat *kmcp = &kmem_cache_kstat;
34237c478bd9Sstevel@tonic-gate 	kmem_cache_t *cp = ksp->ks_private;
34247c478bd9Sstevel@tonic-gate 	uint64_t cpu_buf_avail;
34257c478bd9Sstevel@tonic-gate 	uint64_t buf_avail = 0;
34267c478bd9Sstevel@tonic-gate 	int cpu_seqid;
3427686031edSTom Erickson 	long reap;
34287c478bd9Sstevel@tonic-gate 
34297c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&kmem_cache_kstat_lock));
34307c478bd9Sstevel@tonic-gate 
34317c478bd9Sstevel@tonic-gate 	if (rw == KSTAT_WRITE)
34327c478bd9Sstevel@tonic-gate 		return (EACCES);
34337c478bd9Sstevel@tonic-gate 
34347c478bd9Sstevel@tonic-gate 	mutex_enter(&cp->cache_lock);
34357c478bd9Sstevel@tonic-gate 
34367c478bd9Sstevel@tonic-gate 	kmcp->kmc_alloc_fail.value.ui64		= cp->cache_alloc_fail;
34377c478bd9Sstevel@tonic-gate 	kmcp->kmc_alloc.value.ui64		= cp->cache_slab_alloc;
34387c478bd9Sstevel@tonic-gate 	kmcp->kmc_free.value.ui64		= cp->cache_slab_free;
34397c478bd9Sstevel@tonic-gate 	kmcp->kmc_slab_alloc.value.ui64		= cp->cache_slab_alloc;
34407c478bd9Sstevel@tonic-gate 	kmcp->kmc_slab_free.value.ui64		= cp->cache_slab_free;
34417c478bd9Sstevel@tonic-gate 
34427c478bd9Sstevel@tonic-gate 	for (cpu_seqid = 0; cpu_seqid < max_ncpus; cpu_seqid++) {
34437c478bd9Sstevel@tonic-gate 		kmem_cpu_cache_t *ccp = &cp->cache_cpu[cpu_seqid];
34447c478bd9Sstevel@tonic-gate 
34457c478bd9Sstevel@tonic-gate 		mutex_enter(&ccp->cc_lock);
34467c478bd9Sstevel@tonic-gate 
34477c478bd9Sstevel@tonic-gate 		cpu_buf_avail = 0;
34487c478bd9Sstevel@tonic-gate 		if (ccp->cc_rounds > 0)
34497c478bd9Sstevel@tonic-gate 			cpu_buf_avail += ccp->cc_rounds;
34507c478bd9Sstevel@tonic-gate 		if (ccp->cc_prounds > 0)
34517c478bd9Sstevel@tonic-gate 			cpu_buf_avail += ccp->cc_prounds;
34527c478bd9Sstevel@tonic-gate 
34537c478bd9Sstevel@tonic-gate 		kmcp->kmc_alloc.value.ui64	+= ccp->cc_alloc;
34547c478bd9Sstevel@tonic-gate 		kmcp->kmc_free.value.ui64	+= ccp->cc_free;
34557c478bd9Sstevel@tonic-gate 		buf_avail			+= cpu_buf_avail;
34567c478bd9Sstevel@tonic-gate 
34577c478bd9Sstevel@tonic-gate 		mutex_exit(&ccp->cc_lock);
34587c478bd9Sstevel@tonic-gate 	}
34597c478bd9Sstevel@tonic-gate 
34607c478bd9Sstevel@tonic-gate 	mutex_enter(&cp->cache_depot_lock);
34617c478bd9Sstevel@tonic-gate 
34627c478bd9Sstevel@tonic-gate 	kmcp->kmc_depot_alloc.value.ui64	= cp->cache_full.ml_alloc;
34637c478bd9Sstevel@tonic-gate 	kmcp->kmc_depot_free.value.ui64		= cp->cache_empty.ml_alloc;
34647c478bd9Sstevel@tonic-gate 	kmcp->kmc_depot_contention.value.ui64	= cp->cache_depot_contention;
34657c478bd9Sstevel@tonic-gate 	kmcp->kmc_full_magazines.value.ui64	= cp->cache_full.ml_total;
34667c478bd9Sstevel@tonic-gate 	kmcp->kmc_empty_magazines.value.ui64	= cp->cache_empty.ml_total;
34677c478bd9Sstevel@tonic-gate 	kmcp->kmc_magazine_size.value.ui64	=
34687c478bd9Sstevel@tonic-gate 	    (cp->cache_flags & KMF_NOMAGAZINE) ?
34697c478bd9Sstevel@tonic-gate 	    0 : cp->cache_magtype->mt_magsize;
34707c478bd9Sstevel@tonic-gate 
34717c478bd9Sstevel@tonic-gate 	kmcp->kmc_alloc.value.ui64		+= cp->cache_full.ml_alloc;
34727c478bd9Sstevel@tonic-gate 	kmcp->kmc_free.value.ui64		+= cp->cache_empty.ml_alloc;
34737c478bd9Sstevel@tonic-gate 	buf_avail += cp->cache_full.ml_total * cp->cache_magtype->mt_magsize;
34747c478bd9Sstevel@tonic-gate 
3475686031edSTom Erickson 	reap = MIN(cp->cache_full.ml_reaplimit, cp->cache_full.ml_min);
3476686031edSTom Erickson 	reap = MIN(reap, cp->cache_full.ml_total);
3477686031edSTom Erickson 
34787c478bd9Sstevel@tonic-gate 	mutex_exit(&cp->cache_depot_lock);
34797c478bd9Sstevel@tonic-gate 
34807c478bd9Sstevel@tonic-gate 	kmcp->kmc_buf_size.value.ui64	= cp->cache_bufsize;
34817c478bd9Sstevel@tonic-gate 	kmcp->kmc_align.value.ui64	= cp->cache_align;
34827c478bd9Sstevel@tonic-gate 	kmcp->kmc_chunk_size.value.ui64	= cp->cache_chunksize;
34837c478bd9Sstevel@tonic-gate 	kmcp->kmc_slab_size.value.ui64	= cp->cache_slabsize;
34847c478bd9Sstevel@tonic-gate 	kmcp->kmc_buf_constructed.value.ui64 = buf_avail;
34859f1b636aStomee 	buf_avail += cp->cache_bufslab;
34867c478bd9Sstevel@tonic-gate 	kmcp->kmc_buf_avail.value.ui64	= buf_avail;
34877c478bd9Sstevel@tonic-gate 	kmcp->kmc_buf_inuse.value.ui64	= cp->cache_buftotal - buf_avail;
34887c478bd9Sstevel@tonic-gate 	kmcp->kmc_buf_total.value.ui64	= cp->cache_buftotal;
34897c478bd9Sstevel@tonic-gate 	kmcp->kmc_buf_max.value.ui64	= cp->cache_bufmax;
34907c478bd9Sstevel@tonic-gate 	kmcp->kmc_slab_create.value.ui64	= cp->cache_slab_create;
34917c478bd9Sstevel@tonic-gate 	kmcp->kmc_slab_destroy.value.ui64	= cp->cache_slab_destroy;
34927c478bd9Sstevel@tonic-gate 	kmcp->kmc_hash_size.value.ui64	= (cp->cache_flags & KMF_HASH) ?
34937c478bd9Sstevel@tonic-gate 	    cp->cache_hash_mask + 1 : 0;
34947c478bd9Sstevel@tonic-gate 	kmcp->kmc_hash_lookup_depth.value.ui64	= cp->cache_lookup_depth;
34957c478bd9Sstevel@tonic-gate 	kmcp->kmc_hash_rescale.value.ui64	= cp->cache_rescale;
34967c478bd9Sstevel@tonic-gate 	kmcp->kmc_vmem_source.value.ui64	= cp->cache_arena->vm_id;
3497686031edSTom Erickson 	kmcp->kmc_reap.value.ui64	= cp->cache_reap;
34987c478bd9Sstevel@tonic-gate 
3499b5fca8f8Stomee 	if (cp->cache_defrag == NULL) {
3500b5fca8f8Stomee 		kmcp->kmc_move_callbacks.value.ui64	= 0;
3501b5fca8f8Stomee 		kmcp->kmc_move_yes.value.ui64		= 0;
3502b5fca8f8Stomee 		kmcp->kmc_move_no.value.ui64		= 0;
3503b5fca8f8Stomee 		kmcp->kmc_move_later.value.ui64		= 0;
3504b5fca8f8Stomee 		kmcp->kmc_move_dont_need.value.ui64	= 0;
3505b5fca8f8Stomee 		kmcp->kmc_move_dont_know.value.ui64	= 0;
3506b5fca8f8Stomee 		kmcp->kmc_move_hunt_found.value.ui64	= 0;
3507686031edSTom Erickson 		kmcp->kmc_move_slabs_freed.value.ui64	= 0;
3508686031edSTom Erickson 		kmcp->kmc_defrag.value.ui64		= 0;
3509686031edSTom Erickson 		kmcp->kmc_scan.value.ui64		= 0;
3510686031edSTom Erickson 		kmcp->kmc_move_reclaimable.value.ui64	= 0;
3511b5fca8f8Stomee 	} else {
3512686031edSTom Erickson 		int64_t reclaimable;
3513686031edSTom Erickson 
3514b5fca8f8Stomee 		kmem_defrag_t *kd = cp->cache_defrag;
3515b5fca8f8Stomee 		kmcp->kmc_move_callbacks.value.ui64	= kd->kmd_callbacks;
3516b5fca8f8Stomee 		kmcp->kmc_move_yes.value.ui64		= kd->kmd_yes;
3517b5fca8f8Stomee 		kmcp->kmc_move_no.value.ui64		= kd->kmd_no;
3518b5fca8f8Stomee 		kmcp->kmc_move_later.value.ui64		= kd->kmd_later;
3519b5fca8f8Stomee 		kmcp->kmc_move_dont_need.value.ui64	= kd->kmd_dont_need;
3520b5fca8f8Stomee 		kmcp->kmc_move_dont_know.value.ui64	= kd->kmd_dont_know;
3521b5fca8f8Stomee 		kmcp->kmc_move_hunt_found.value.ui64	= kd->kmd_hunt_found;
3522686031edSTom Erickson 		kmcp->kmc_move_slabs_freed.value.ui64	= kd->kmd_slabs_freed;
3523686031edSTom Erickson 		kmcp->kmc_defrag.value.ui64		= kd->kmd_defrags;
3524686031edSTom Erickson 		kmcp->kmc_scan.value.ui64		= kd->kmd_scans;
3525686031edSTom Erickson 
3526686031edSTom Erickson 		reclaimable = cp->cache_bufslab - (cp->cache_maxchunks - 1);
3527686031edSTom Erickson 		reclaimable = MAX(reclaimable, 0);
3528686031edSTom Erickson 		reclaimable += ((uint64_t)reap * cp->cache_magtype->mt_magsize);
3529686031edSTom Erickson 		kmcp->kmc_move_reclaimable.value.ui64	= reclaimable;
3530b5fca8f8Stomee 	}
3531b5fca8f8Stomee 
35327c478bd9Sstevel@tonic-gate 	mutex_exit(&cp->cache_lock);
35337c478bd9Sstevel@tonic-gate 	return (0);
35347c478bd9Sstevel@tonic-gate }
35357c478bd9Sstevel@tonic-gate 
35367c478bd9Sstevel@tonic-gate /*
35377c478bd9Sstevel@tonic-gate  * Return a named statistic about a particular cache.
35387c478bd9Sstevel@tonic-gate  * This shouldn't be called very often, so it's currently designed for
35397c478bd9Sstevel@tonic-gate  * simplicity (leverages existing kstat support) rather than efficiency.
35407c478bd9Sstevel@tonic-gate  */
35417c478bd9Sstevel@tonic-gate uint64_t
35427c478bd9Sstevel@tonic-gate kmem_cache_stat(kmem_cache_t *cp, char *name)
35437c478bd9Sstevel@tonic-gate {
35447c478bd9Sstevel@tonic-gate 	int i;
35457c478bd9Sstevel@tonic-gate 	kstat_t *ksp = cp->cache_kstat;
35467c478bd9Sstevel@tonic-gate 	kstat_named_t *knp = (kstat_named_t *)&kmem_cache_kstat;
35477c478bd9Sstevel@tonic-gate 	uint64_t value = 0;
35487c478bd9Sstevel@tonic-gate 
35497c478bd9Sstevel@tonic-gate 	if (ksp != NULL) {
35507c478bd9Sstevel@tonic-gate 		mutex_enter(&kmem_cache_kstat_lock);
35517c478bd9Sstevel@tonic-gate 		(void) kmem_cache_kstat_update(ksp, KSTAT_READ);
35527c478bd9Sstevel@tonic-gate 		for (i = 0; i < ksp->ks_ndata; i++) {
35537c478bd9Sstevel@tonic-gate 			if (strcmp(knp[i].name, name) == 0) {
35547c478bd9Sstevel@tonic-gate 				value = knp[i].value.ui64;
35557c478bd9Sstevel@tonic-gate 				break;
35567c478bd9Sstevel@tonic-gate 			}
35577c478bd9Sstevel@tonic-gate 		}
35587c478bd9Sstevel@tonic-gate 		mutex_exit(&kmem_cache_kstat_lock);
35597c478bd9Sstevel@tonic-gate 	}
35607c478bd9Sstevel@tonic-gate 	return (value);
35617c478bd9Sstevel@tonic-gate }
35627c478bd9Sstevel@tonic-gate 
35637c478bd9Sstevel@tonic-gate /*
35647c478bd9Sstevel@tonic-gate  * Return an estimate of currently available kernel heap memory.
35657c478bd9Sstevel@tonic-gate  * On 32-bit systems, physical memory may exceed virtual memory,
35667c478bd9Sstevel@tonic-gate  * we just truncate the result at 1GB.
35677c478bd9Sstevel@tonic-gate  */
35687c478bd9Sstevel@tonic-gate size_t
35697c478bd9Sstevel@tonic-gate kmem_avail(void)
35707c478bd9Sstevel@tonic-gate {
35717c478bd9Sstevel@tonic-gate 	spgcnt_t rmem = availrmem - tune.t_minarmem;
35727c478bd9Sstevel@tonic-gate 	spgcnt_t fmem = freemem - minfree;
35737c478bd9Sstevel@tonic-gate 
35747c478bd9Sstevel@tonic-gate 	return ((size_t)ptob(MIN(MAX(MIN(rmem, fmem), 0),
35757c478bd9Sstevel@tonic-gate 	    1 << (30 - PAGESHIFT))));
35767c478bd9Sstevel@tonic-gate }
35777c478bd9Sstevel@tonic-gate 
35787c478bd9Sstevel@tonic-gate /*
35797c478bd9Sstevel@tonic-gate  * Return the maximum amount of memory that is (in theory) allocatable
35807c478bd9Sstevel@tonic-gate  * from the heap. This may be used as an estimate only since there
35817c478bd9Sstevel@tonic-gate  * is no guarentee this space will still be available when an allocation
35827c478bd9Sstevel@tonic-gate  * request is made, nor that the space may be allocated in one big request
35837c478bd9Sstevel@tonic-gate  * due to kernel heap fragmentation.
35847c478bd9Sstevel@tonic-gate  */
35857c478bd9Sstevel@tonic-gate size_t
35867c478bd9Sstevel@tonic-gate kmem_maxavail(void)
35877c478bd9Sstevel@tonic-gate {
35887c478bd9Sstevel@tonic-gate 	spgcnt_t pmem = availrmem - tune.t_minarmem;
35897c478bd9Sstevel@tonic-gate 	spgcnt_t vmem = btop(vmem_size(heap_arena, VMEM_FREE));
35907c478bd9Sstevel@tonic-gate 
35917c478bd9Sstevel@tonic-gate 	return ((size_t)ptob(MAX(MIN(pmem, vmem), 0)));
35927c478bd9Sstevel@tonic-gate }
35937c478bd9Sstevel@tonic-gate 
3594fa9e4066Sahrens /*
3595fa9e4066Sahrens  * Indicate whether memory-intensive kmem debugging is enabled.
3596fa9e4066Sahrens  */
3597fa9e4066Sahrens int
3598fa9e4066Sahrens kmem_debugging(void)
3599fa9e4066Sahrens {
3600fa9e4066Sahrens 	return (kmem_flags & (KMF_AUDIT | KMF_REDZONE));
3601fa9e4066Sahrens }
3602fa9e4066Sahrens 
3603b5fca8f8Stomee /* binning function, sorts finely at the two extremes */
3604b5fca8f8Stomee #define	KMEM_PARTIAL_SLAB_WEIGHT(sp, binshift)				\
3605b5fca8f8Stomee 	((((sp)->slab_refcnt <= (binshift)) ||				\
3606b5fca8f8Stomee 	    (((sp)->slab_chunks - (sp)->slab_refcnt) <= (binshift)))	\
3607b5fca8f8Stomee 	    ? -(sp)->slab_refcnt					\
3608b5fca8f8Stomee 	    : -((binshift) + ((sp)->slab_refcnt >> (binshift))))
3609b5fca8f8Stomee 
3610b5fca8f8Stomee /*
3611b5fca8f8Stomee  * Minimizing the number of partial slabs on the freelist minimizes
3612b5fca8f8Stomee  * fragmentation (the ratio of unused buffers held by the slab layer). There are
3613b5fca8f8Stomee  * two ways to get a slab off of the freelist: 1) free all the buffers on the
3614b5fca8f8Stomee  * slab, and 2) allocate all the buffers on the slab. It follows that we want
3615b5fca8f8Stomee  * the most-used slabs at the front of the list where they have the best chance
3616b5fca8f8Stomee  * of being completely allocated, and the least-used slabs at a safe distance
3617b5fca8f8Stomee  * from the front to improve the odds that the few remaining buffers will all be
3618b5fca8f8Stomee  * freed before another allocation can tie up the slab. For that reason a slab
3619b5fca8f8Stomee  * with a higher slab_refcnt sorts less than than a slab with a lower
3620b5fca8f8Stomee  * slab_refcnt.
3621b5fca8f8Stomee  *
3622b5fca8f8Stomee  * However, if a slab has at least one buffer that is deemed unfreeable, we
3623b5fca8f8Stomee  * would rather have that slab at the front of the list regardless of
3624b5fca8f8Stomee  * slab_refcnt, since even one unfreeable buffer makes the entire slab
3625b5fca8f8Stomee  * unfreeable. If the client returns KMEM_CBRC_NO in response to a cache_move()
3626b5fca8f8Stomee  * callback, the slab is marked unfreeable for as long as it remains on the
3627b5fca8f8Stomee  * freelist.
3628b5fca8f8Stomee  */
3629b5fca8f8Stomee static int
3630b5fca8f8Stomee kmem_partial_slab_cmp(const void *p0, const void *p1)
3631b5fca8f8Stomee {
3632b5fca8f8Stomee 	const kmem_cache_t *cp;
3633b5fca8f8Stomee 	const kmem_slab_t *s0 = p0;
3634b5fca8f8Stomee 	const kmem_slab_t *s1 = p1;
3635b5fca8f8Stomee 	int w0, w1;
3636b5fca8f8Stomee 	size_t binshift;
3637b5fca8f8Stomee 
3638b5fca8f8Stomee 	ASSERT(KMEM_SLAB_IS_PARTIAL(s0));
3639b5fca8f8Stomee 	ASSERT(KMEM_SLAB_IS_PARTIAL(s1));
3640b5fca8f8Stomee 	ASSERT(s0->slab_cache == s1->slab_cache);
3641b5fca8f8Stomee 	cp = s1->slab_cache;
3642b5fca8f8Stomee 	ASSERT(MUTEX_HELD(&cp->cache_lock));
3643b5fca8f8Stomee 	binshift = cp->cache_partial_binshift;
3644b5fca8f8Stomee 
3645b5fca8f8Stomee 	/* weight of first slab */
3646b5fca8f8Stomee 	w0 = KMEM_PARTIAL_SLAB_WEIGHT(s0, binshift);
3647b5fca8f8Stomee 	if (s0->slab_flags & KMEM_SLAB_NOMOVE) {
3648b5fca8f8Stomee 		w0 -= cp->cache_maxchunks;
3649b5fca8f8Stomee 	}
3650b5fca8f8Stomee 
3651b5fca8f8Stomee 	/* weight of second slab */
3652b5fca8f8Stomee 	w1 = KMEM_PARTIAL_SLAB_WEIGHT(s1, binshift);
3653b5fca8f8Stomee 	if (s1->slab_flags & KMEM_SLAB_NOMOVE) {
3654b5fca8f8Stomee 		w1 -= cp->cache_maxchunks;
3655b5fca8f8Stomee 	}
3656b5fca8f8Stomee 
3657b5fca8f8Stomee 	if (w0 < w1)
3658b5fca8f8Stomee 		return (-1);
3659b5fca8f8Stomee 	if (w0 > w1)
3660b5fca8f8Stomee 		return (1);
3661b5fca8f8Stomee 
3662b5fca8f8Stomee 	/* compare pointer values */
3663b5fca8f8Stomee 	if ((uintptr_t)s0 < (uintptr_t)s1)
3664b5fca8f8Stomee 		return (-1);
3665b5fca8f8Stomee 	if ((uintptr_t)s0 > (uintptr_t)s1)
3666b5fca8f8Stomee 		return (1);
3667b5fca8f8Stomee 
3668b5fca8f8Stomee 	return (0);
3669b5fca8f8Stomee }
3670b5fca8f8Stomee 
3671b5fca8f8Stomee /*
3672b5fca8f8Stomee  * It must be valid to call the destructor (if any) on a newly created object.
3673b5fca8f8Stomee  * That is, the constructor (if any) must leave the object in a valid state for
3674b5fca8f8Stomee  * the destructor.
3675b5fca8f8Stomee  */
36767c478bd9Sstevel@tonic-gate kmem_cache_t *
36777c478bd9Sstevel@tonic-gate kmem_cache_create(
36787c478bd9Sstevel@tonic-gate 	char *name,		/* descriptive name for this cache */
36797c478bd9Sstevel@tonic-gate 	size_t bufsize,		/* size of the objects it manages */
36807c478bd9Sstevel@tonic-gate 	size_t align,		/* required object alignment */
36817c478bd9Sstevel@tonic-gate 	int (*constructor)(void *, void *, int), /* object constructor */
36827c478bd9Sstevel@tonic-gate 	void (*destructor)(void *, void *),	/* object destructor */
36837c478bd9Sstevel@tonic-gate 	void (*reclaim)(void *), /* memory reclaim callback */
36847c478bd9Sstevel@tonic-gate 	void *private,		/* pass-thru arg for constr/destr/reclaim */
36857c478bd9Sstevel@tonic-gate 	vmem_t *vmp,		/* vmem source for slab allocation */
36867c478bd9Sstevel@tonic-gate 	int cflags)		/* cache creation flags */
36877c478bd9Sstevel@tonic-gate {
36887c478bd9Sstevel@tonic-gate 	int cpu_seqid;
36897c478bd9Sstevel@tonic-gate 	size_t chunksize;
3690b5fca8f8Stomee 	kmem_cache_t *cp;
36917c478bd9Sstevel@tonic-gate 	kmem_magtype_t *mtp;
36927c478bd9Sstevel@tonic-gate 	size_t csize = KMEM_CACHE_SIZE(max_ncpus);
36937c478bd9Sstevel@tonic-gate 
36947c478bd9Sstevel@tonic-gate #ifdef	DEBUG
36957c478bd9Sstevel@tonic-gate 	/*
36967c478bd9Sstevel@tonic-gate 	 * Cache names should conform to the rules for valid C identifiers
36977c478bd9Sstevel@tonic-gate 	 */
36987c478bd9Sstevel@tonic-gate 	if (!strident_valid(name)) {
36997c478bd9Sstevel@tonic-gate 		cmn_err(CE_CONT,
37007c478bd9Sstevel@tonic-gate 		    "kmem_cache_create: '%s' is an invalid cache name\n"
37017c478bd9Sstevel@tonic-gate 		    "cache names must conform to the rules for "
37027c478bd9Sstevel@tonic-gate 		    "C identifiers\n", name);
37037c478bd9Sstevel@tonic-gate 	}
37047c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
37057c478bd9Sstevel@tonic-gate 
37067c478bd9Sstevel@tonic-gate 	if (vmp == NULL)
37077c478bd9Sstevel@tonic-gate 		vmp = kmem_default_arena;
37087c478bd9Sstevel@tonic-gate 
37097c478bd9Sstevel@tonic-gate 	/*
37107c478bd9Sstevel@tonic-gate 	 * If this kmem cache has an identifier vmem arena as its source, mark
37117c478bd9Sstevel@tonic-gate 	 * it such to allow kmem_reap_idspace().
37127c478bd9Sstevel@tonic-gate 	 */
37137c478bd9Sstevel@tonic-gate 	ASSERT(!(cflags & KMC_IDENTIFIER));   /* consumer should not set this */
37147c478bd9Sstevel@tonic-gate 	if (vmp->vm_cflags & VMC_IDENTIFIER)
37157c478bd9Sstevel@tonic-gate 		cflags |= KMC_IDENTIFIER;
37167c478bd9Sstevel@tonic-gate 
37177c478bd9Sstevel@tonic-gate 	/*
37187c478bd9Sstevel@tonic-gate 	 * Get a kmem_cache structure.  We arrange that cp->cache_cpu[]
37197c478bd9Sstevel@tonic-gate 	 * is aligned on a KMEM_CPU_CACHE_SIZE boundary to prevent
37207c478bd9Sstevel@tonic-gate 	 * false sharing of per-CPU data.
37217c478bd9Sstevel@tonic-gate 	 */
37227c478bd9Sstevel@tonic-gate 	cp = vmem_xalloc(kmem_cache_arena, csize, KMEM_CPU_CACHE_SIZE,
37237c478bd9Sstevel@tonic-gate 	    P2NPHASE(csize, KMEM_CPU_CACHE_SIZE), 0, NULL, NULL, VM_SLEEP);
37247c478bd9Sstevel@tonic-gate 	bzero(cp, csize);
3725b5fca8f8Stomee 	list_link_init(&cp->cache_link);
37267c478bd9Sstevel@tonic-gate 
37277c478bd9Sstevel@tonic-gate 	if (align == 0)
37287c478bd9Sstevel@tonic-gate 		align = KMEM_ALIGN;
37297c478bd9Sstevel@tonic-gate 
37307c478bd9Sstevel@tonic-gate 	/*
37317c478bd9Sstevel@tonic-gate 	 * If we're not at least KMEM_ALIGN aligned, we can't use free
37327c478bd9Sstevel@tonic-gate 	 * memory to hold bufctl information (because we can't safely
37337c478bd9Sstevel@tonic-gate 	 * perform word loads and stores on it).
37347c478bd9Sstevel@tonic-gate 	 */
37357c478bd9Sstevel@tonic-gate 	if (align < KMEM_ALIGN)
37367c478bd9Sstevel@tonic-gate 		cflags |= KMC_NOTOUCH;
37377c478bd9Sstevel@tonic-gate 
3738de710d24SJosef 'Jeff' Sipek 	if (!ISP2(align) || align > vmp->vm_quantum)
37397c478bd9Sstevel@tonic-gate 		panic("kmem_cache_create: bad alignment %lu", align);
37407c478bd9Sstevel@tonic-gate 
37417c478bd9Sstevel@tonic-gate 	mutex_enter(&kmem_flags_lock);
37427c478bd9Sstevel@tonic-gate 	if (kmem_flags & KMF_RANDOMIZE)
37437c478bd9Sstevel@tonic-gate 		kmem_flags = (((kmem_flags | ~KMF_RANDOM) + 1) & KMF_RANDOM) |
37447c478bd9Sstevel@tonic-gate 		    KMF_RANDOMIZE;
37457c478bd9Sstevel@tonic-gate 	cp->cache_flags = (kmem_flags | cflags) & KMF_DEBUG;
37467c478bd9Sstevel@tonic-gate 	mutex_exit(&kmem_flags_lock);
37477c478bd9Sstevel@tonic-gate 
37487c478bd9Sstevel@tonic-gate 	/*
37497c478bd9Sstevel@tonic-gate 	 * Make sure all the various flags are reasonable.
37507c478bd9Sstevel@tonic-gate 	 */
37517c478bd9Sstevel@tonic-gate 	ASSERT(!(cflags & KMC_NOHASH) || !(cflags & KMC_NOTOUCH));
37527c478bd9Sstevel@tonic-gate 
37537c478bd9Sstevel@tonic-gate 	if (cp->cache_flags & KMF_LITE) {
37547c478bd9Sstevel@tonic-gate 		if (bufsize >= kmem_lite_minsize &&
37557c478bd9Sstevel@tonic-gate 		    align <= kmem_lite_maxalign &&
37567c478bd9Sstevel@tonic-gate 		    P2PHASE(bufsize, kmem_lite_maxalign) != 0) {
37577c478bd9Sstevel@tonic-gate 			cp->cache_flags |= KMF_BUFTAG;
37587c478bd9Sstevel@tonic-gate 			cp->cache_flags &= ~(KMF_AUDIT | KMF_FIREWALL);
37597c478bd9Sstevel@tonic-gate 		} else {
37607c478bd9Sstevel@tonic-gate 			cp->cache_flags &= ~KMF_DEBUG;
37617c478bd9Sstevel@tonic-gate 		}
37627c478bd9Sstevel@tonic-gate 	}
37637c478bd9Sstevel@tonic-gate 
37647c478bd9Sstevel@tonic-gate 	if (cp->cache_flags & KMF_DEADBEEF)
37657c478bd9Sstevel@tonic-gate 		cp->cache_flags |= KMF_REDZONE;
37667c478bd9Sstevel@tonic-gate 
37677c478bd9Sstevel@tonic-gate 	if ((cflags & KMC_QCACHE) && (cp->cache_flags & KMF_AUDIT))
37687c478bd9Sstevel@tonic-gate 		cp->cache_flags |= KMF_NOMAGAZINE;
37697c478bd9Sstevel@tonic-gate 
37707c478bd9Sstevel@tonic-gate 	if (cflags & KMC_NODEBUG)
37717c478bd9Sstevel@tonic-gate 		cp->cache_flags &= ~KMF_DEBUG;
37727c478bd9Sstevel@tonic-gate 
37737c478bd9Sstevel@tonic-gate 	if (cflags & KMC_NOTOUCH)
37747c478bd9Sstevel@tonic-gate 		cp->cache_flags &= ~KMF_TOUCH;
37757c478bd9Sstevel@tonic-gate 
3776b942e89bSDavid Valin 	if (cflags & KMC_PREFILL)
3777b942e89bSDavid Valin 		cp->cache_flags |= KMF_PREFILL;
3778b942e89bSDavid Valin 
37797c478bd9Sstevel@tonic-gate 	if (cflags & KMC_NOHASH)
37807c478bd9Sstevel@tonic-gate 		cp->cache_flags &= ~(KMF_AUDIT | KMF_FIREWALL);
37817c478bd9Sstevel@tonic-gate 
37827c478bd9Sstevel@tonic-gate 	if (cflags & KMC_NOMAGAZINE)
37837c478bd9Sstevel@tonic-gate 		cp->cache_flags |= KMF_NOMAGAZINE;
37847c478bd9Sstevel@tonic-gate 
37857c478bd9Sstevel@tonic-gate 	if ((cp->cache_flags & KMF_AUDIT) && !(cflags & KMC_NOTOUCH))
37867c478bd9Sstevel@tonic-gate 		cp->cache_flags |= KMF_REDZONE;
37877c478bd9Sstevel@tonic-gate 
37887c478bd9Sstevel@tonic-gate 	if (!(cp->cache_flags & KMF_AUDIT))
37897c478bd9Sstevel@tonic-gate 		cp->cache_flags &= ~KMF_CONTENTS;
37907c478bd9Sstevel@tonic-gate 
37917c478bd9Sstevel@tonic-gate 	if ((cp->cache_flags & KMF_BUFTAG) && bufsize >= kmem_minfirewall &&
37927c478bd9Sstevel@tonic-gate 	    !(cp->cache_flags & KMF_LITE) && !(cflags & KMC_NOHASH))
37937c478bd9Sstevel@tonic-gate 		cp->cache_flags |= KMF_FIREWALL;
37947c478bd9Sstevel@tonic-gate 
37957c478bd9Sstevel@tonic-gate 	if (vmp != kmem_default_arena || kmem_firewall_arena == NULL)
37967c478bd9Sstevel@tonic-gate 		cp->cache_flags &= ~KMF_FIREWALL;
37977c478bd9Sstevel@tonic-gate 
37987c478bd9Sstevel@tonic-gate 	if (cp->cache_flags & KMF_FIREWALL) {
37997c478bd9Sstevel@tonic-gate 		cp->cache_flags &= ~KMF_BUFTAG;
38007c478bd9Sstevel@tonic-gate 		cp->cache_flags |= KMF_NOMAGAZINE;
38017c478bd9Sstevel@tonic-gate 		ASSERT(vmp == kmem_default_arena);
38027c478bd9Sstevel@tonic-gate 		vmp = kmem_firewall_arena;
38037c478bd9Sstevel@tonic-gate 	}
38047c478bd9Sstevel@tonic-gate 
38057c478bd9Sstevel@tonic-gate 	/*
38067c478bd9Sstevel@tonic-gate 	 * Set cache properties.
38077c478bd9Sstevel@tonic-gate 	 */
38087c478bd9Sstevel@tonic-gate 	(void) strncpy(cp->cache_name, name, KMEM_CACHE_NAMELEN);
3809b5fca8f8Stomee 	strident_canon(cp->cache_name, KMEM_CACHE_NAMELEN + 1);
38107c478bd9Sstevel@tonic-gate 	cp->cache_bufsize = bufsize;
38117c478bd9Sstevel@tonic-gate 	cp->cache_align = align;
38127c478bd9Sstevel@tonic-gate 	cp->cache_constructor = constructor;
38137c478bd9Sstevel@tonic-gate 	cp->cache_destructor = destructor;
38147c478bd9Sstevel@tonic-gate 	cp->cache_reclaim = reclaim;
38157c478bd9Sstevel@tonic-gate 	cp->cache_private = private;
38167c478bd9Sstevel@tonic-gate 	cp->cache_arena = vmp;
38177c478bd9Sstevel@tonic-gate 	cp->cache_cflags = cflags;
38187c478bd9Sstevel@tonic-gate 
38197c478bd9Sstevel@tonic-gate 	/*
38207c478bd9Sstevel@tonic-gate 	 * Determine the chunk size.
38217c478bd9Sstevel@tonic-gate 	 */
38227c478bd9Sstevel@tonic-gate 	chunksize = bufsize;
38237c478bd9Sstevel@tonic-gate 
38247c478bd9Sstevel@tonic-gate 	if (align >= KMEM_ALIGN) {
38257c478bd9Sstevel@tonic-gate 		chunksize = P2ROUNDUP(chunksize, KMEM_ALIGN);
38267c478bd9Sstevel@tonic-gate 		cp->cache_bufctl = chunksize - KMEM_ALIGN;
38277c478bd9Sstevel@tonic-gate 	}
38287c478bd9Sstevel@tonic-gate 
38297c478bd9Sstevel@tonic-gate 	if (cp->cache_flags & KMF_BUFTAG) {
38307c478bd9Sstevel@tonic-gate 		cp->cache_bufctl = chunksize;
38317c478bd9Sstevel@tonic-gate 		cp->cache_buftag = chunksize;
38327c478bd9Sstevel@tonic-gate 		if (cp->cache_flags & KMF_LITE)
38337c478bd9Sstevel@tonic-gate 			chunksize += KMEM_BUFTAG_LITE_SIZE(kmem_lite_count);
38347c478bd9Sstevel@tonic-gate 		else
38357c478bd9Sstevel@tonic-gate 			chunksize += sizeof (kmem_buftag_t);
38367c478bd9Sstevel@tonic-gate 	}
38377c478bd9Sstevel@tonic-gate 
38387c478bd9Sstevel@tonic-gate 	if (cp->cache_flags & KMF_DEADBEEF) {
38397c478bd9Sstevel@tonic-gate 		cp->cache_verify = MIN(cp->cache_buftag, kmem_maxverify);
38407c478bd9Sstevel@tonic-gate 		if (cp->cache_flags & KMF_LITE)
38417c478bd9Sstevel@tonic-gate 			cp->cache_verify = sizeof (uint64_t);
38427c478bd9Sstevel@tonic-gate 	}
38437c478bd9Sstevel@tonic-gate 
38447c478bd9Sstevel@tonic-gate 	cp->cache_contents = MIN(cp->cache_bufctl, kmem_content_maxsave);
38457c478bd9Sstevel@tonic-gate 
38467c478bd9Sstevel@tonic-gate 	cp->cache_chunksize = chunksize = P2ROUNDUP(chunksize, align);
38477c478bd9Sstevel@tonic-gate 
38487c478bd9Sstevel@tonic-gate 	/*
38497c478bd9Sstevel@tonic-gate 	 * Now that we know the chunk size, determine the optimal slab size.
38507c478bd9Sstevel@tonic-gate 	 */
38517c478bd9Sstevel@tonic-gate 	if (vmp == kmem_firewall_arena) {
38527c478bd9Sstevel@tonic-gate 		cp->cache_slabsize = P2ROUNDUP(chunksize, vmp->vm_quantum);
38537c478bd9Sstevel@tonic-gate 		cp->cache_mincolor = cp->cache_slabsize - chunksize;
38547c478bd9Sstevel@tonic-gate 		cp->cache_maxcolor = cp->cache_mincolor;
38557c478bd9Sstevel@tonic-gate 		cp->cache_flags |= KMF_HASH;
38567c478bd9Sstevel@tonic-gate 		ASSERT(!(cp->cache_flags & KMF_BUFTAG));
38577c478bd9Sstevel@tonic-gate 	} else if ((cflags & KMC_NOHASH) || (!(cflags & KMC_NOTOUCH) &&
38587c478bd9Sstevel@tonic-gate 	    !(cp->cache_flags & KMF_AUDIT) &&
38597c478bd9Sstevel@tonic-gate 	    chunksize < vmp->vm_quantum / KMEM_VOID_FRACTION)) {
38607c478bd9Sstevel@tonic-gate 		cp->cache_slabsize = vmp->vm_quantum;
38617c478bd9Sstevel@tonic-gate 		cp->cache_mincolor = 0;
38627c478bd9Sstevel@tonic-gate 		cp->cache_maxcolor =
38637c478bd9Sstevel@tonic-gate 		    (cp->cache_slabsize - sizeof (kmem_slab_t)) % chunksize;
38647c478bd9Sstevel@tonic-gate 		ASSERT(chunksize + sizeof (kmem_slab_t) <= cp->cache_slabsize);
38657c478bd9Sstevel@tonic-gate 		ASSERT(!(cp->cache_flags & KMF_AUDIT));
38667c478bd9Sstevel@tonic-gate 	} else {
38677c478bd9Sstevel@tonic-gate 		size_t chunks, bestfit, waste, slabsize;
38687c478bd9Sstevel@tonic-gate 		size_t minwaste = LONG_MAX;
38697c478bd9Sstevel@tonic-gate 
38707c478bd9Sstevel@tonic-gate 		for (chunks = 1; chunks <= KMEM_VOID_FRACTION; chunks++) {
38717c478bd9Sstevel@tonic-gate 			slabsize = P2ROUNDUP(chunksize * chunks,
38727c478bd9Sstevel@tonic-gate 			    vmp->vm_quantum);
38737c478bd9Sstevel@tonic-gate 			chunks = slabsize / chunksize;
38747c478bd9Sstevel@tonic-gate 			waste = (slabsize % chunksize) / chunks;
38757c478bd9Sstevel@tonic-gate 			if (waste < minwaste) {
38767c478bd9Sstevel@tonic-gate 				minwaste = waste;
38777c478bd9Sstevel@tonic-gate 				bestfit = slabsize;
38787c478bd9Sstevel@tonic-gate 			}
38797c478bd9Sstevel@tonic-gate 		}
38807c478bd9Sstevel@tonic-gate 		if (cflags & KMC_QCACHE)
38817c478bd9Sstevel@tonic-gate 			bestfit = VMEM_QCACHE_SLABSIZE(vmp->vm_qcache_max);
38827c478bd9Sstevel@tonic-gate 		cp->cache_slabsize = bestfit;
38837c478bd9Sstevel@tonic-gate 		cp->cache_mincolor = 0;
38847c478bd9Sstevel@tonic-gate 		cp->cache_maxcolor = bestfit % chunksize;
38857c478bd9Sstevel@tonic-gate 		cp->cache_flags |= KMF_HASH;
38867c478bd9Sstevel@tonic-gate 	}
38877c478bd9Sstevel@tonic-gate 
3888b5fca8f8Stomee 	cp->cache_maxchunks = (cp->cache_slabsize / cp->cache_chunksize);
3889b5fca8f8Stomee 	cp->cache_partial_binshift = highbit(cp->cache_maxchunks / 16) + 1;
3890b5fca8f8Stomee 
3891b942e89bSDavid Valin 	/*
3892b942e89bSDavid Valin 	 * Disallowing prefill when either the DEBUG or HASH flag is set or when
3893b942e89bSDavid Valin 	 * there is a constructor avoids some tricky issues with debug setup
3894b942e89bSDavid Valin 	 * that may be revisited later. We cannot allow prefill in a
3895b942e89bSDavid Valin 	 * metadata cache because of potential recursion.
3896b942e89bSDavid Valin 	 */
3897b942e89bSDavid Valin 	if (vmp == kmem_msb_arena ||
3898b942e89bSDavid Valin 	    cp->cache_flags & (KMF_HASH | KMF_BUFTAG) ||
3899b942e89bSDavid Valin 	    cp->cache_constructor != NULL)
3900b942e89bSDavid Valin 		cp->cache_flags &= ~KMF_PREFILL;
3901b942e89bSDavid Valin 
39027c478bd9Sstevel@tonic-gate 	if (cp->cache_flags & KMF_HASH) {
39037c478bd9Sstevel@tonic-gate 		ASSERT(!(cflags & KMC_NOHASH));
39047c478bd9Sstevel@tonic-gate 		cp->cache_bufctl_cache = (cp->cache_flags & KMF_AUDIT) ?
39057c478bd9Sstevel@tonic-gate 		    kmem_bufctl_audit_cache : kmem_bufctl_cache;
39067c478bd9Sstevel@tonic-gate 	}
39077c478bd9Sstevel@tonic-gate 
39087c478bd9Sstevel@tonic-gate 	if (cp->cache_maxcolor >= vmp->vm_quantum)
39097c478bd9Sstevel@tonic-gate 		cp->cache_maxcolor = vmp->vm_quantum - 1;
39107c478bd9Sstevel@tonic-gate 
39117c478bd9Sstevel@tonic-gate 	cp->cache_color = cp->cache_mincolor;
39127c478bd9Sstevel@tonic-gate 
39137c478bd9Sstevel@tonic-gate 	/*
39147c478bd9Sstevel@tonic-gate 	 * Initialize the rest of the slab layer.
39157c478bd9Sstevel@tonic-gate 	 */
39167c478bd9Sstevel@tonic-gate 	mutex_init(&cp->cache_lock, NULL, MUTEX_DEFAULT, NULL);
39177c478bd9Sstevel@tonic-gate 
3918b5fca8f8Stomee 	avl_create(&cp->cache_partial_slabs, kmem_partial_slab_cmp,
3919b5fca8f8Stomee 	    sizeof (kmem_slab_t), offsetof(kmem_slab_t, slab_link));
3920b5fca8f8Stomee 	/* LINTED: E_TRUE_LOGICAL_EXPR */
3921b5fca8f8Stomee 	ASSERT(sizeof (list_node_t) <= sizeof (avl_node_t));
3922b5fca8f8Stomee 	/* reuse partial slab AVL linkage for complete slab list linkage */
3923b5fca8f8Stomee 	list_create(&cp->cache_complete_slabs,
3924b5fca8f8Stomee 	    sizeof (kmem_slab_t), offsetof(kmem_slab_t, slab_link));
39257c478bd9Sstevel@tonic-gate 
39267c478bd9Sstevel@tonic-gate 	if (cp->cache_flags & KMF_HASH) {
39277c478bd9Sstevel@tonic-gate 		cp->cache_hash_table = vmem_alloc(kmem_hash_arena,
39287c478bd9Sstevel@tonic-gate 		    KMEM_HASH_INITIAL * sizeof (void *), VM_SLEEP);
39297c478bd9Sstevel@tonic-gate 		bzero(cp->cache_hash_table,
39307c478bd9Sstevel@tonic-gate 		    KMEM_HASH_INITIAL * sizeof (void *));
39317c478bd9Sstevel@tonic-gate 		cp->cache_hash_mask = KMEM_HASH_INITIAL - 1;
39327c478bd9Sstevel@tonic-gate 		cp->cache_hash_shift = highbit((ulong_t)chunksize) - 1;
39337c478bd9Sstevel@tonic-gate 	}
39347c478bd9Sstevel@tonic-gate 
39357c478bd9Sstevel@tonic-gate 	/*
39367c478bd9Sstevel@tonic-gate 	 * Initialize the depot.
39377c478bd9Sstevel@tonic-gate 	 */
39387c478bd9Sstevel@tonic-gate 	mutex_init(&cp->cache_depot_lock, NULL, MUTEX_DEFAULT, NULL);
39397c478bd9Sstevel@tonic-gate 
39407c478bd9Sstevel@tonic-gate 	for (mtp = kmem_magtype; chunksize <= mtp->mt_minbuf; mtp++)
39417c478bd9Sstevel@tonic-gate 		continue;
39427c478bd9Sstevel@tonic-gate 
39437c478bd9Sstevel@tonic-gate 	cp->cache_magtype = mtp;
39447c478bd9Sstevel@tonic-gate 
39457c478bd9Sstevel@tonic-gate 	/*
39467c478bd9Sstevel@tonic-gate 	 * Initialize the CPU layer.
39477c478bd9Sstevel@tonic-gate 	 */
39487c478bd9Sstevel@tonic-gate 	for (cpu_seqid = 0; cpu_seqid < max_ncpus; cpu_seqid++) {
39497c478bd9Sstevel@tonic-gate 		kmem_cpu_cache_t *ccp = &cp->cache_cpu[cpu_seqid];
39507c478bd9Sstevel@tonic-gate 		mutex_init(&ccp->cc_lock, NULL, MUTEX_DEFAULT, NULL);
39517c478bd9Sstevel@tonic-gate 		ccp->cc_flags = cp->cache_flags;
39527c478bd9Sstevel@tonic-gate 		ccp->cc_rounds = -1;
39537c478bd9Sstevel@tonic-gate 		ccp->cc_prounds = -1;
39547c478bd9Sstevel@tonic-gate 	}
39557c478bd9Sstevel@tonic-gate 
39567c478bd9Sstevel@tonic-gate 	/*
39577c478bd9Sstevel@tonic-gate 	 * Create the cache's kstats.
39587c478bd9Sstevel@tonic-gate 	 */
39597c478bd9Sstevel@tonic-gate 	if ((cp->cache_kstat = kstat_create("unix", 0, cp->cache_name,
39607c478bd9Sstevel@tonic-gate 	    "kmem_cache", KSTAT_TYPE_NAMED,
39617c478bd9Sstevel@tonic-gate 	    sizeof (kmem_cache_kstat) / sizeof (kstat_named_t),
39627c478bd9Sstevel@tonic-gate 	    KSTAT_FLAG_VIRTUAL)) != NULL) {
39637c478bd9Sstevel@tonic-gate 		cp->cache_kstat->ks_data = &kmem_cache_kstat;
39647c478bd9Sstevel@tonic-gate 		cp->cache_kstat->ks_update = kmem_cache_kstat_update;
39657c478bd9Sstevel@tonic-gate 		cp->cache_kstat->ks_private = cp;
39667c478bd9Sstevel@tonic-gate 		cp->cache_kstat->ks_lock = &kmem_cache_kstat_lock;
39677c478bd9Sstevel@tonic-gate 		kstat_install(cp->cache_kstat);
39687c478bd9Sstevel@tonic-gate 	}
39697c478bd9Sstevel@tonic-gate 
39707c478bd9Sstevel@tonic-gate 	/*
39717c478bd9Sstevel@tonic-gate 	 * Add the cache to the global list.  This makes it visible
39727c478bd9Sstevel@tonic-gate 	 * to kmem_update(), so the cache must be ready for business.
39737c478bd9Sstevel@tonic-gate 	 */
39747c478bd9Sstevel@tonic-gate 	mutex_enter(&kmem_cache_lock);
3975b5fca8f8Stomee 	list_insert_tail(&kmem_caches, cp);
39767c478bd9Sstevel@tonic-gate 	mutex_exit(&kmem_cache_lock);
39777c478bd9Sstevel@tonic-gate 
39787c478bd9Sstevel@tonic-gate 	if (kmem_ready)
39797c478bd9Sstevel@tonic-gate 		kmem_cache_magazine_enable(cp);
39807c478bd9Sstevel@tonic-gate 
39817c478bd9Sstevel@tonic-gate 	return (cp);
39827c478bd9Sstevel@tonic-gate }
39837c478bd9Sstevel@tonic-gate 
3984b5fca8f8Stomee static int
3985b5fca8f8Stomee kmem_move_cmp(const void *buf, const void *p)
3986b5fca8f8Stomee {
3987b5fca8f8Stomee 	const kmem_move_t *kmm = p;
3988b5fca8f8Stomee 	uintptr_t v1 = (uintptr_t)buf;
3989b5fca8f8Stomee 	uintptr_t v2 = (uintptr_t)kmm->kmm_from_buf;
3990b5fca8f8Stomee 	return (v1 < v2 ? -1 : (v1 > v2 ? 1 : 0));
3991b5fca8f8Stomee }
3992b5fca8f8Stomee 
3993b5fca8f8Stomee static void
3994b5fca8f8Stomee kmem_reset_reclaim_threshold(kmem_defrag_t *kmd)
3995b5fca8f8Stomee {
3996b5fca8f8Stomee 	kmd->kmd_reclaim_numer = 1;
3997b5fca8f8Stomee }
3998b5fca8f8Stomee 
3999b5fca8f8Stomee /*
4000b5fca8f8Stomee  * Initially, when choosing candidate slabs for buffers to move, we want to be
4001b5fca8f8Stomee  * very selective and take only slabs that are less than
4002b5fca8f8Stomee  * (1 / KMEM_VOID_FRACTION) allocated. If we have difficulty finding candidate
4003b5fca8f8Stomee  * slabs, then we raise the allocation ceiling incrementally. The reclaim
4004b5fca8f8Stomee  * threshold is reset to (1 / KMEM_VOID_FRACTION) as soon as the cache is no
4005b5fca8f8Stomee  * longer fragmented.
4006b5fca8f8Stomee  */
4007b5fca8f8Stomee static void
4008b5fca8f8Stomee kmem_adjust_reclaim_threshold(kmem_defrag_t *kmd, int direction)
4009b5fca8f8Stomee {
4010b5fca8f8Stomee 	if (direction > 0) {
4011b5fca8f8Stomee 		/* make it easier to find a candidate slab */
4012b5fca8f8Stomee 		if (kmd->kmd_reclaim_numer < (KMEM_VOID_FRACTION - 1)) {
4013b5fca8f8Stomee 			kmd->kmd_reclaim_numer++;
4014b5fca8f8Stomee 		}
4015b5fca8f8Stomee 	} else {
4016b5fca8f8Stomee 		/* be more selective */
4017b5fca8f8Stomee 		if (kmd->kmd_reclaim_numer > 1) {
4018b5fca8f8Stomee 			kmd->kmd_reclaim_numer--;
4019b5fca8f8Stomee 		}
4020b5fca8f8Stomee 	}
4021b5fca8f8Stomee }
4022b5fca8f8Stomee 
4023b5fca8f8Stomee void
4024b5fca8f8Stomee kmem_cache_set_move(kmem_cache_t *cp,
4025b5fca8f8Stomee     kmem_cbrc_t (*move)(void *, void *, size_t, void *))
4026b5fca8f8Stomee {
4027b5fca8f8Stomee 	kmem_defrag_t *defrag;
4028b5fca8f8Stomee 
4029b5fca8f8Stomee 	ASSERT(move != NULL);
4030b5fca8f8Stomee 	/*
4031b5fca8f8Stomee 	 * The consolidator does not support NOTOUCH caches because kmem cannot
4032b5fca8f8Stomee 	 * initialize their slabs with the 0xbaddcafe memory pattern, which sets
4033b5fca8f8Stomee 	 * a low order bit usable by clients to distinguish uninitialized memory
4034b5fca8f8Stomee 	 * from known objects (see kmem_slab_create).
4035b5fca8f8Stomee 	 */
4036b5fca8f8Stomee 	ASSERT(!(cp->cache_cflags & KMC_NOTOUCH));
4037b5fca8f8Stomee 	ASSERT(!(cp->cache_cflags & KMC_IDENTIFIER));
4038b5fca8f8Stomee 
4039b5fca8f8Stomee 	/*
4040b5fca8f8Stomee 	 * We should not be holding anyone's cache lock when calling
4041b5fca8f8Stomee 	 * kmem_cache_alloc(), so allocate in all cases before acquiring the
4042b5fca8f8Stomee 	 * lock.
4043b5fca8f8Stomee 	 */
4044b5fca8f8Stomee 	defrag = kmem_cache_alloc(kmem_defrag_cache, KM_SLEEP);
4045b5fca8f8Stomee 
4046b5fca8f8Stomee 	mutex_enter(&cp->cache_lock);
4047b5fca8f8Stomee 
4048b5fca8f8Stomee 	if (KMEM_IS_MOVABLE(cp)) {
4049b5fca8f8Stomee 		if (cp->cache_move == NULL) {
40504d4c4c43STom Erickson 			ASSERT(cp->cache_slab_alloc == 0);
4051b5fca8f8Stomee 
4052b5fca8f8Stomee 			cp->cache_defrag = defrag;
4053b5fca8f8Stomee 			defrag = NULL; /* nothing to free */
4054b5fca8f8Stomee 			bzero(cp->cache_defrag, sizeof (kmem_defrag_t));
4055b5fca8f8Stomee 			avl_create(&cp->cache_defrag->kmd_moves_pending,
4056b5fca8f8Stomee 			    kmem_move_cmp, sizeof (kmem_move_t),
4057b5fca8f8Stomee 			    offsetof(kmem_move_t, kmm_entry));
4058b5fca8f8Stomee 			/* LINTED: E_TRUE_LOGICAL_EXPR */
4059b5fca8f8Stomee 			ASSERT(sizeof (list_node_t) <= sizeof (avl_node_t));
4060b5fca8f8Stomee 			/* reuse the slab's AVL linkage for deadlist linkage */
4061b5fca8f8Stomee 			list_create(&cp->cache_defrag->kmd_deadlist,
4062b5fca8f8Stomee 			    sizeof (kmem_slab_t),
4063b5fca8f8Stomee 			    offsetof(kmem_slab_t, slab_link));
4064b5fca8f8Stomee 			kmem_reset_reclaim_threshold(cp->cache_defrag);
4065b5fca8f8Stomee 		}
4066b5fca8f8Stomee 		cp->cache_move = move;
4067b5fca8f8Stomee 	}
4068b5fca8f8Stomee 
4069b5fca8f8Stomee 	mutex_exit(&cp->cache_lock);
4070b5fca8f8Stomee 
4071b5fca8f8Stomee 	if (defrag != NULL) {
4072b5fca8f8Stomee 		kmem_cache_free(kmem_defrag_cache, defrag); /* unused */
4073b5fca8f8Stomee 	}
4074b5fca8f8Stomee }
4075b5fca8f8Stomee 
40767c478bd9Sstevel@tonic-gate void
40777c478bd9Sstevel@tonic-gate kmem_cache_destroy(kmem_cache_t *cp)
40787c478bd9Sstevel@tonic-gate {
40797c478bd9Sstevel@tonic-gate 	int cpu_seqid;
40807c478bd9Sstevel@tonic-gate 
40817c478bd9Sstevel@tonic-gate 	/*
40827c478bd9Sstevel@tonic-gate 	 * Remove the cache from the global cache list so that no one else
40837c478bd9Sstevel@tonic-gate 	 * can schedule tasks on its behalf, wait for any pending tasks to
40847c478bd9Sstevel@tonic-gate 	 * complete, purge the cache, and then destroy it.
40857c478bd9Sstevel@tonic-gate 	 */
40867c478bd9Sstevel@tonic-gate 	mutex_enter(&kmem_cache_lock);
4087b5fca8f8Stomee 	list_remove(&kmem_caches, cp);
40887c478bd9Sstevel@tonic-gate 	mutex_exit(&kmem_cache_lock);
40897c478bd9Sstevel@tonic-gate 
40907c478bd9Sstevel@tonic-gate 	if (kmem_taskq != NULL)
40917c478bd9Sstevel@tonic-gate 		taskq_wait(kmem_taskq);
4092b5fca8f8Stomee 	if (kmem_move_taskq != NULL)
4093b5fca8f8Stomee 		taskq_wait(kmem_move_taskq);
40947c478bd9Sstevel@tonic-gate 
40957c478bd9Sstevel@tonic-gate 	kmem_cache_magazine_purge(cp);
40967c478bd9Sstevel@tonic-gate 
40977c478bd9Sstevel@tonic-gate 	mutex_enter(&cp->cache_lock);
40987c478bd9Sstevel@tonic-gate 	if (cp->cache_buftotal != 0)
40997c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "kmem_cache_destroy: '%s' (%p) not empty",
41007c478bd9Sstevel@tonic-gate 		    cp->cache_name, (void *)cp);
4101b5fca8f8Stomee 	if (cp->cache_defrag != NULL) {
4102b5fca8f8Stomee 		avl_destroy(&cp->cache_defrag->kmd_moves_pending);
4103b5fca8f8Stomee 		list_destroy(&cp->cache_defrag->kmd_deadlist);
4104b5fca8f8Stomee 		kmem_cache_free(kmem_defrag_cache, cp->cache_defrag);
4105b5fca8f8Stomee 		cp->cache_defrag = NULL;
4106b5fca8f8Stomee 	}
41077c478bd9Sstevel@tonic-gate 	/*
4108b5fca8f8Stomee 	 * The cache is now dead.  There should be no further activity.  We
4109b5fca8f8Stomee 	 * enforce this by setting land mines in the constructor, destructor,
4110b5fca8f8Stomee 	 * reclaim, and move routines that induce a kernel text fault if
4111b5fca8f8Stomee 	 * invoked.
41127c478bd9Sstevel@tonic-gate 	 */
41137c478bd9Sstevel@tonic-gate 	cp->cache_constructor = (int (*)(void *, void *, int))1;
41147c478bd9Sstevel@tonic-gate 	cp->cache_destructor = (void (*)(void *, void *))2;
4115b5fca8f8Stomee 	cp->cache_reclaim = (void (*)(void *))3;
4116b5fca8f8Stomee 	cp->cache_move = (kmem_cbrc_t (*)(void *, void *, size_t, void *))4;
41177c478bd9Sstevel@tonic-gate 	mutex_exit(&cp->cache_lock);
41187c478bd9Sstevel@tonic-gate 
41197c478bd9Sstevel@tonic-gate 	kstat_delete(cp->cache_kstat);
41207c478bd9Sstevel@tonic-gate 
41217c478bd9Sstevel@tonic-gate 	if (cp->cache_hash_table != NULL)
41227c478bd9Sstevel@tonic-gate 		vmem_free(kmem_hash_arena, cp->cache_hash_table,
41237c478bd9Sstevel@tonic-gate 		    (cp->cache_hash_mask + 1) * sizeof (void *));
41247c478bd9Sstevel@tonic-gate 
41257c478bd9Sstevel@tonic-gate 	for (cpu_seqid = 0; cpu_seqid < max_ncpus; cpu_seqid++)
41267c478bd9Sstevel@tonic-gate 		mutex_destroy(&cp->cache_cpu[cpu_seqid].cc_lock);
41277c478bd9Sstevel@tonic-gate 
41287c478bd9Sstevel@tonic-gate 	mutex_destroy(&cp->cache_depot_lock);
41297c478bd9Sstevel@tonic-gate 	mutex_destroy(&cp->cache_lock);
41307c478bd9Sstevel@tonic-gate 
41317c478bd9Sstevel@tonic-gate 	vmem_free(kmem_cache_arena, cp, KMEM_CACHE_SIZE(max_ncpus));
41327c478bd9Sstevel@tonic-gate }
41337c478bd9Sstevel@tonic-gate 
41347c478bd9Sstevel@tonic-gate /*ARGSUSED*/
41357c478bd9Sstevel@tonic-gate static int
41367c478bd9Sstevel@tonic-gate kmem_cpu_setup(cpu_setup_t what, int id, void *arg)
41377c478bd9Sstevel@tonic-gate {
41387c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&cpu_lock));
41397c478bd9Sstevel@tonic-gate 	if (what == CPU_UNCONFIG) {
41407c478bd9Sstevel@tonic-gate 		kmem_cache_applyall(kmem_cache_magazine_purge,
41417c478bd9Sstevel@tonic-gate 		    kmem_taskq, TQ_SLEEP);
41427c478bd9Sstevel@tonic-gate 		kmem_cache_applyall(kmem_cache_magazine_enable,
41437c478bd9Sstevel@tonic-gate 		    kmem_taskq, TQ_SLEEP);
41447c478bd9Sstevel@tonic-gate 	}
41457c478bd9Sstevel@tonic-gate 	return (0);
41467c478bd9Sstevel@tonic-gate }
41477c478bd9Sstevel@tonic-gate 
41487c478bd9Sstevel@tonic-gate static void
4149dce01e3fSJonathan W Adams kmem_alloc_caches_create(const int *array, size_t count,
4150dce01e3fSJonathan W Adams     kmem_cache_t **alloc_table, size_t maxbuf, uint_t shift)
4151dce01e3fSJonathan W Adams {
4152dce01e3fSJonathan W Adams 	char name[KMEM_CACHE_NAMELEN + 1];
4153dce01e3fSJonathan W Adams 	size_t table_unit = (1 << shift); /* range of one alloc_table entry */
4154dce01e3fSJonathan W Adams 	size_t size = table_unit;
4155dce01e3fSJonathan W Adams 	int i;
4156dce01e3fSJonathan W Adams 
4157dce01e3fSJonathan W Adams 	for (i = 0; i < count; i++) {
4158dce01e3fSJonathan W Adams 		size_t cache_size = array[i];
4159dce01e3fSJonathan W Adams 		size_t align = KMEM_ALIGN;
4160dce01e3fSJonathan W Adams 		kmem_cache_t *cp;
4161dce01e3fSJonathan W Adams 
4162dce01e3fSJonathan W Adams 		/* if the table has an entry for maxbuf, we're done */
4163dce01e3fSJonathan W Adams 		if (size > maxbuf)
4164dce01e3fSJonathan W Adams 			break;
4165dce01e3fSJonathan W Adams 
4166dce01e3fSJonathan W Adams 		/* cache size must be a multiple of the table unit */
4167dce01e3fSJonathan W Adams 		ASSERT(P2PHASE(cache_size, table_unit) == 0);
4168dce01e3fSJonathan W Adams 
4169dce01e3fSJonathan W Adams 		/*
4170dce01e3fSJonathan W Adams 		 * If they allocate a multiple of the coherency granularity,
4171dce01e3fSJonathan W Adams 		 * they get a coherency-granularity-aligned address.
4172dce01e3fSJonathan W Adams 		 */
4173dce01e3fSJonathan W Adams 		if (IS_P2ALIGNED(cache_size, 64))
4174dce01e3fSJonathan W Adams 			align = 64;
4175dce01e3fSJonathan W Adams 		if (IS_P2ALIGNED(cache_size, PAGESIZE))
4176dce01e3fSJonathan W Adams 			align = PAGESIZE;
4177dce01e3fSJonathan W Adams 		(void) snprintf(name, sizeof (name),
4178dce01e3fSJonathan W Adams 		    "kmem_alloc_%lu", cache_size);
4179dce01e3fSJonathan W Adams 		cp = kmem_cache_create(name, cache_size, align,
4180dce01e3fSJonathan W Adams 		    NULL, NULL, NULL, NULL, NULL, KMC_KMEM_ALLOC);
4181dce01e3fSJonathan W Adams 
4182dce01e3fSJonathan W Adams 		while (size <= cache_size) {
4183dce01e3fSJonathan W Adams 			alloc_table[(size - 1) >> shift] = cp;
4184dce01e3fSJonathan W Adams 			size += table_unit;
4185dce01e3fSJonathan W Adams 		}
4186dce01e3fSJonathan W Adams 	}
4187dce01e3fSJonathan W Adams 
4188dce01e3fSJonathan W Adams 	ASSERT(size > maxbuf);		/* i.e. maxbuf <= max(cache_size) */
4189dce01e3fSJonathan W Adams }
4190dce01e3fSJonathan W Adams 
4191dce01e3fSJonathan W Adams static void
41927c478bd9Sstevel@tonic-gate kmem_cache_init(int pass, int use_large_pages)
41937c478bd9Sstevel@tonic-gate {
41947c478bd9Sstevel@tonic-gate 	int i;
4195dce01e3fSJonathan W Adams 	size_t maxbuf;
41967c478bd9Sstevel@tonic-gate 	kmem_magtype_t *mtp;
41977c478bd9Sstevel@tonic-gate 
41987c478bd9Sstevel@tonic-gate 	for (i = 0; i < sizeof (kmem_magtype) / sizeof (*mtp); i++) {
4199dce01e3fSJonathan W Adams 		char name[KMEM_CACHE_NAMELEN + 1];
4200dce01e3fSJonathan W Adams 
42017c478bd9Sstevel@tonic-gate 		mtp = &kmem_magtype[i];
42027c478bd9Sstevel@tonic-gate 		(void) sprintf(name, "kmem_magazine_%d", mtp->mt_magsize);
42037c478bd9Sstevel@tonic-gate 		mtp->mt_cache = kmem_cache_create(name,
42047c478bd9Sstevel@tonic-gate 		    (mtp->mt_magsize + 1) * sizeof (void *),
42057c478bd9Sstevel@tonic-gate 		    mtp->mt_align, NULL, NULL, NULL, NULL,
42067c478bd9Sstevel@tonic-gate 		    kmem_msb_arena, KMC_NOHASH);
42077c478bd9Sstevel@tonic-gate 	}
42087c478bd9Sstevel@tonic-gate 
42097c478bd9Sstevel@tonic-gate 	kmem_slab_cache = kmem_cache_create("kmem_slab_cache",
42107c478bd9Sstevel@tonic-gate 	    sizeof (kmem_slab_t), 0, NULL, NULL, NULL, NULL,
42117c478bd9Sstevel@tonic-gate 	    kmem_msb_arena, KMC_NOHASH);
42127c478bd9Sstevel@tonic-gate 
42137c478bd9Sstevel@tonic-gate 	kmem_bufctl_cache = kmem_cache_create("kmem_bufctl_cache",
42147c478bd9Sstevel@tonic-gate 	    sizeof (kmem_bufctl_t), 0, NULL, NULL, NULL, NULL,
42157c478bd9Sstevel@tonic-gate 	    kmem_msb_arena, KMC_NOHASH);
42167c478bd9Sstevel@tonic-gate 
42177c478bd9Sstevel@tonic-gate 	kmem_bufctl_audit_cache = kmem_cache_create("kmem_bufctl_audit_cache",
42187c478bd9Sstevel@tonic-gate 	    sizeof (kmem_bufctl_audit_t), 0, NULL, NULL, NULL, NULL,
42197c478bd9Sstevel@tonic-gate 	    kmem_msb_arena, KMC_NOHASH);
42207c478bd9Sstevel@tonic-gate 
42217c478bd9Sstevel@tonic-gate 	if (pass == 2) {
42227c478bd9Sstevel@tonic-gate 		kmem_va_arena = vmem_create("kmem_va",
42237c478bd9Sstevel@tonic-gate 		    NULL, 0, PAGESIZE,
42247c478bd9Sstevel@tonic-gate 		    vmem_alloc, vmem_free, heap_arena,
42257c478bd9Sstevel@tonic-gate 		    8 * PAGESIZE, VM_SLEEP);
42267c478bd9Sstevel@tonic-gate 
42277c478bd9Sstevel@tonic-gate 		if (use_large_pages) {
42287c478bd9Sstevel@tonic-gate 			kmem_default_arena = vmem_xcreate("kmem_default",
42297c478bd9Sstevel@tonic-gate 			    NULL, 0, PAGESIZE,
42307c478bd9Sstevel@tonic-gate 			    segkmem_alloc_lp, segkmem_free_lp, kmem_va_arena,
42319dd77bc8SDave Plauger 			    0, VMC_DUMPSAFE | VM_SLEEP);
42327c478bd9Sstevel@tonic-gate 		} else {
42337c478bd9Sstevel@tonic-gate 			kmem_default_arena = vmem_create("kmem_default",
42347c478bd9Sstevel@tonic-gate 			    NULL, 0, PAGESIZE,
42357c478bd9Sstevel@tonic-gate 			    segkmem_alloc, segkmem_free, kmem_va_arena,
42369dd77bc8SDave Plauger 			    0, VMC_DUMPSAFE | VM_SLEEP);
42377c478bd9Sstevel@tonic-gate 		}
4238dce01e3fSJonathan W Adams 
4239dce01e3fSJonathan W Adams 		/* Figure out what our maximum cache size is */
4240dce01e3fSJonathan W Adams 		maxbuf = kmem_max_cached;
4241dce01e3fSJonathan W Adams 		if (maxbuf <= KMEM_MAXBUF) {
4242dce01e3fSJonathan W Adams 			maxbuf = 0;
4243dce01e3fSJonathan W Adams 			kmem_max_cached = KMEM_MAXBUF;
4244dce01e3fSJonathan W Adams 		} else {
4245dce01e3fSJonathan W Adams 			size_t size = 0;
4246dce01e3fSJonathan W Adams 			size_t max =
4247dce01e3fSJonathan W Adams 			    sizeof (kmem_big_alloc_sizes) / sizeof (int);
4248dce01e3fSJonathan W Adams 			/*
4249dce01e3fSJonathan W Adams 			 * Round maxbuf up to an existing cache size.  If maxbuf
4250dce01e3fSJonathan W Adams 			 * is larger than the largest cache, we truncate it to
4251dce01e3fSJonathan W Adams 			 * the largest cache's size.
4252dce01e3fSJonathan W Adams 			 */
4253dce01e3fSJonathan W Adams 			for (i = 0; i < max; i++) {
4254dce01e3fSJonathan W Adams 				size = kmem_big_alloc_sizes[i];
4255dce01e3fSJonathan W Adams 				if (maxbuf <= size)
4256dce01e3fSJonathan W Adams 					break;
4257dce01e3fSJonathan W Adams 			}
4258dce01e3fSJonathan W Adams 			kmem_max_cached = maxbuf = size;
4259dce01e3fSJonathan W Adams 		}
4260dce01e3fSJonathan W Adams 
4261dce01e3fSJonathan W Adams 		/*
4262dce01e3fSJonathan W Adams 		 * The big alloc table may not be completely overwritten, so
4263dce01e3fSJonathan W Adams 		 * we clear out any stale cache pointers from the first pass.
4264dce01e3fSJonathan W Adams 		 */
4265dce01e3fSJonathan W Adams 		bzero(kmem_big_alloc_table, sizeof (kmem_big_alloc_table));
42667c478bd9Sstevel@tonic-gate 	} else {
42677c478bd9Sstevel@tonic-gate 		/*
42687c478bd9Sstevel@tonic-gate 		 * During the first pass, the kmem_alloc_* caches
42697c478bd9Sstevel@tonic-gate 		 * are treated as metadata.
42707c478bd9Sstevel@tonic-gate 		 */
42717c478bd9Sstevel@tonic-gate 		kmem_default_arena = kmem_msb_arena;
4272dce01e3fSJonathan W Adams 		maxbuf = KMEM_BIG_MAXBUF_32BIT;
42737c478bd9Sstevel@tonic-gate 	}
42747c478bd9Sstevel@tonic-gate 
42757c478bd9Sstevel@tonic-gate 	/*
42767c478bd9Sstevel@tonic-gate 	 * Set up the default caches to back kmem_alloc()
42777c478bd9Sstevel@tonic-gate 	 */
4278dce01e3fSJonathan W Adams 	kmem_alloc_caches_create(
4279dce01e3fSJonathan W Adams 	    kmem_alloc_sizes, sizeof (kmem_alloc_sizes) / sizeof (int),
4280dce01e3fSJonathan W Adams 	    kmem_alloc_table, KMEM_MAXBUF, KMEM_ALIGN_SHIFT);
4281dce01e3fSJonathan W Adams 
4282dce01e3fSJonathan W Adams 	kmem_alloc_caches_create(
4283dce01e3fSJonathan W Adams 	    kmem_big_alloc_sizes, sizeof (kmem_big_alloc_sizes) / sizeof (int),
4284dce01e3fSJonathan W Adams 	    kmem_big_alloc_table, maxbuf, KMEM_BIG_SHIFT);
4285dce01e3fSJonathan W Adams 
4286dce01e3fSJonathan W Adams 	kmem_big_alloc_table_max = maxbuf >> KMEM_BIG_SHIFT;
42877c478bd9Sstevel@tonic-gate }
42887c478bd9Sstevel@tonic-gate 
42897c478bd9Sstevel@tonic-gate void
42907c478bd9Sstevel@tonic-gate kmem_init(void)
42917c478bd9Sstevel@tonic-gate {
42927c478bd9Sstevel@tonic-gate 	kmem_cache_t *cp;
42937c478bd9Sstevel@tonic-gate 	int old_kmem_flags = kmem_flags;
42947c478bd9Sstevel@tonic-gate 	int use_large_pages = 0;
42957c478bd9Sstevel@tonic-gate 	size_t maxverify, minfirewall;
42967c478bd9Sstevel@tonic-gate 
42977c478bd9Sstevel@tonic-gate 	kstat_init();
42987c478bd9Sstevel@tonic-gate 
42997c478bd9Sstevel@tonic-gate 	/*
43007c478bd9Sstevel@tonic-gate 	 * Don't do firewalled allocations if the heap is less than 1TB
43017c478bd9Sstevel@tonic-gate 	 * (i.e. on a 32-bit kernel)
43027c478bd9Sstevel@tonic-gate 	 * The resulting VM_NEXTFIT allocations would create too much
43037c478bd9Sstevel@tonic-gate 	 * fragmentation in a small heap.
43047c478bd9Sstevel@tonic-gate 	 */
43057c478bd9Sstevel@tonic-gate #if defined(_LP64)
43067c478bd9Sstevel@tonic-gate 	maxverify = minfirewall = PAGESIZE / 2;
43077c478bd9Sstevel@tonic-gate #else
43087c478bd9Sstevel@tonic-gate 	maxverify = minfirewall = ULONG_MAX;
43097c478bd9Sstevel@tonic-gate #endif
43107c478bd9Sstevel@tonic-gate 
43117c478bd9Sstevel@tonic-gate 	/* LINTED */
43127c478bd9Sstevel@tonic-gate 	ASSERT(sizeof (kmem_cpu_cache_t) == KMEM_CPU_CACHE_SIZE);
43137c478bd9Sstevel@tonic-gate 
4314b5fca8f8Stomee 	list_create(&kmem_caches, sizeof (kmem_cache_t),
4315b5fca8f8Stomee 	    offsetof(kmem_cache_t, cache_link));
43167c478bd9Sstevel@tonic-gate 
43177c478bd9Sstevel@tonic-gate 	kmem_metadata_arena = vmem_create("kmem_metadata", NULL, 0, PAGESIZE,
43187c478bd9Sstevel@tonic-gate 	    vmem_alloc, vmem_free, heap_arena, 8 * PAGESIZE,
43197c478bd9Sstevel@tonic-gate 	    VM_SLEEP | VMC_NO_QCACHE);
43207c478bd9Sstevel@tonic-gate 
43217c478bd9Sstevel@tonic-gate 	kmem_msb_arena = vmem_create("kmem_msb", NULL, 0,
43227c478bd9Sstevel@tonic-gate 	    PAGESIZE, segkmem_alloc, segkmem_free, kmem_metadata_arena, 0,
43239dd77bc8SDave Plauger 	    VMC_DUMPSAFE | VM_SLEEP);
43247c478bd9Sstevel@tonic-gate 
43257c478bd9Sstevel@tonic-gate 	kmem_cache_arena = vmem_create("kmem_cache", NULL, 0, KMEM_ALIGN,
43267c478bd9Sstevel@tonic-gate 	    segkmem_alloc, segkmem_free, kmem_metadata_arena, 0, VM_SLEEP);
43277c478bd9Sstevel@tonic-gate 
43287c478bd9Sstevel@tonic-gate 	kmem_hash_arena = vmem_create("kmem_hash", NULL, 0, KMEM_ALIGN,
43297c478bd9Sstevel@tonic-gate 	    segkmem_alloc, segkmem_free, kmem_metadata_arena, 0, VM_SLEEP);
43307c478bd9Sstevel@tonic-gate 
43317c478bd9Sstevel@tonic-gate 	kmem_log_arena = vmem_create("kmem_log", NULL, 0, KMEM_ALIGN,
43327c478bd9Sstevel@tonic-gate 	    segkmem_alloc, segkmem_free, heap_arena, 0, VM_SLEEP);
43337c478bd9Sstevel@tonic-gate 
43347c478bd9Sstevel@tonic-gate 	kmem_firewall_va_arena = vmem_create("kmem_firewall_va",
43357c478bd9Sstevel@tonic-gate 	    NULL, 0, PAGESIZE,
43367c478bd9Sstevel@tonic-gate 	    kmem_firewall_va_alloc, kmem_firewall_va_free, heap_arena,
43377c478bd9Sstevel@tonic-gate 	    0, VM_SLEEP);
43387c478bd9Sstevel@tonic-gate 
43397c478bd9Sstevel@tonic-gate 	kmem_firewall_arena = vmem_create("kmem_firewall", NULL, 0, PAGESIZE,
43409dd77bc8SDave Plauger 	    segkmem_alloc, segkmem_free, kmem_firewall_va_arena, 0,
43419dd77bc8SDave Plauger 	    VMC_DUMPSAFE | VM_SLEEP);
43427c478bd9Sstevel@tonic-gate 
43437c478bd9Sstevel@tonic-gate 	/* temporary oversize arena for mod_read_system_file */
43447c478bd9Sstevel@tonic-gate 	kmem_oversize_arena = vmem_create("kmem_oversize", NULL, 0, PAGESIZE,
43457c478bd9Sstevel@tonic-gate 	    segkmem_alloc, segkmem_free, heap_arena, 0, VM_SLEEP);
43467c478bd9Sstevel@tonic-gate 
43477c478bd9Sstevel@tonic-gate 	kmem_reap_interval = 15 * hz;
43487c478bd9Sstevel@tonic-gate 
43497c478bd9Sstevel@tonic-gate 	/*
43507c478bd9Sstevel@tonic-gate 	 * Read /etc/system.  This is a chicken-and-egg problem because
43517c478bd9Sstevel@tonic-gate 	 * kmem_flags may be set in /etc/system, but mod_read_system_file()
43527c478bd9Sstevel@tonic-gate 	 * needs to use the allocator.  The simplest solution is to create
43537c478bd9Sstevel@tonic-gate 	 * all the standard kmem caches, read /etc/system, destroy all the
43547c478bd9Sstevel@tonic-gate 	 * caches we just created, and then create them all again in light
43557c478bd9Sstevel@tonic-gate 	 * of the (possibly) new kmem_flags and other kmem tunables.
43567c478bd9Sstevel@tonic-gate 	 */
43577c478bd9Sstevel@tonic-gate 	kmem_cache_init(1, 0);
43587c478bd9Sstevel@tonic-gate 
43597c478bd9Sstevel@tonic-gate 	mod_read_system_file(boothowto & RB_ASKNAME);
43607c478bd9Sstevel@tonic-gate 
4361b5fca8f8Stomee 	while ((cp = list_tail(&kmem_caches)) != NULL)
43627c478bd9Sstevel@tonic-gate 		kmem_cache_destroy(cp);
43637c478bd9Sstevel@tonic-gate 
43647c478bd9Sstevel@tonic-gate 	vmem_destroy(kmem_oversize_arena);
43657c478bd9Sstevel@tonic-gate 
43667c478bd9Sstevel@tonic-gate 	if (old_kmem_flags & KMF_STICKY)
43677c478bd9Sstevel@tonic-gate 		kmem_flags = old_kmem_flags;
43687c478bd9Sstevel@tonic-gate 
43697c478bd9Sstevel@tonic-gate 	if (!(kmem_flags & KMF_AUDIT))
43707c478bd9Sstevel@tonic-gate 		vmem_seg_size = offsetof(vmem_seg_t, vs_thread);
43717c478bd9Sstevel@tonic-gate 
43727c478bd9Sstevel@tonic-gate 	if (kmem_maxverify == 0)
43737c478bd9Sstevel@tonic-gate 		kmem_maxverify = maxverify;
43747c478bd9Sstevel@tonic-gate 
43757c478bd9Sstevel@tonic-gate 	if (kmem_minfirewall == 0)
43767c478bd9Sstevel@tonic-gate 		kmem_minfirewall = minfirewall;
43777c478bd9Sstevel@tonic-gate 
43787c478bd9Sstevel@tonic-gate 	/*
43797c478bd9Sstevel@tonic-gate 	 * give segkmem a chance to figure out if we are using large pages
43807c478bd9Sstevel@tonic-gate 	 * for the kernel heap
43817c478bd9Sstevel@tonic-gate 	 */
43827c478bd9Sstevel@tonic-gate 	use_large_pages = segkmem_lpsetup();
43837c478bd9Sstevel@tonic-gate 
43847c478bd9Sstevel@tonic-gate 	/*
43857c478bd9Sstevel@tonic-gate 	 * To protect against corruption, we keep the actual number of callers
43867c478bd9Sstevel@tonic-gate 	 * KMF_LITE records seperate from the tunable.  We arbitrarily clamp
43877c478bd9Sstevel@tonic-gate 	 * to 16, since the overhead for small buffers quickly gets out of
43887c478bd9Sstevel@tonic-gate 	 * hand.
43897c478bd9Sstevel@tonic-gate 	 *
43907c478bd9Sstevel@tonic-gate 	 * The real limit would depend on the needs of the largest KMC_NOHASH
43917c478bd9Sstevel@tonic-gate 	 * cache.
43927c478bd9Sstevel@tonic-gate 	 */
43937c478bd9Sstevel@tonic-gate 	kmem_lite_count = MIN(MAX(0, kmem_lite_pcs), 16);
43947c478bd9Sstevel@tonic-gate 	kmem_lite_pcs = kmem_lite_count;
43957c478bd9Sstevel@tonic-gate 
43967c478bd9Sstevel@tonic-gate 	/*
43977c478bd9Sstevel@tonic-gate 	 * Normally, we firewall oversized allocations when possible, but
43987c478bd9Sstevel@tonic-gate 	 * if we are using large pages for kernel memory, and we don't have
43997c478bd9Sstevel@tonic-gate 	 * any non-LITE debugging flags set, we want to allocate oversized
44007c478bd9Sstevel@tonic-gate 	 * buffers from large pages, and so skip the firewalling.
44017c478bd9Sstevel@tonic-gate 	 */
44027c478bd9Sstevel@tonic-gate 	if (use_large_pages &&
44037c478bd9Sstevel@tonic-gate 	    ((kmem_flags & KMF_LITE) || !(kmem_flags & KMF_DEBUG))) {
44047c478bd9Sstevel@tonic-gate 		kmem_oversize_arena = vmem_xcreate("kmem_oversize", NULL, 0,
44057c478bd9Sstevel@tonic-gate 		    PAGESIZE, segkmem_alloc_lp, segkmem_free_lp, heap_arena,
44069dd77bc8SDave Plauger 		    0, VMC_DUMPSAFE | VM_SLEEP);
44077c478bd9Sstevel@tonic-gate 	} else {
44087c478bd9Sstevel@tonic-gate 		kmem_oversize_arena = vmem_create("kmem_oversize",
44097c478bd9Sstevel@tonic-gate 		    NULL, 0, PAGESIZE,
44107c478bd9Sstevel@tonic-gate 		    segkmem_alloc, segkmem_free, kmem_minfirewall < ULONG_MAX?
44119dd77bc8SDave Plauger 		    kmem_firewall_va_arena : heap_arena, 0, VMC_DUMPSAFE |
44129dd77bc8SDave Plauger 		    VM_SLEEP);
44137c478bd9Sstevel@tonic-gate 	}
44147c478bd9Sstevel@tonic-gate 
44157c478bd9Sstevel@tonic-gate 	kmem_cache_init(2, use_large_pages);
44167c478bd9Sstevel@tonic-gate 
44177c478bd9Sstevel@tonic-gate 	if (kmem_flags & (KMF_AUDIT | KMF_RANDOMIZE)) {
44187c478bd9Sstevel@tonic-gate 		if (kmem_transaction_log_size == 0)
44197c478bd9Sstevel@tonic-gate 			kmem_transaction_log_size = kmem_maxavail() / 50;
44207c478bd9Sstevel@tonic-gate 		kmem_transaction_log = kmem_log_init(kmem_transaction_log_size);
44217c478bd9Sstevel@tonic-gate 	}
44227c478bd9Sstevel@tonic-gate 
44237c478bd9Sstevel@tonic-gate 	if (kmem_flags & (KMF_CONTENTS | KMF_RANDOMIZE)) {
44247c478bd9Sstevel@tonic-gate 		if (kmem_content_log_size == 0)
44257c478bd9Sstevel@tonic-gate 			kmem_content_log_size = kmem_maxavail() / 50;
44267c478bd9Sstevel@tonic-gate 		kmem_content_log = kmem_log_init(kmem_content_log_size);
44277c478bd9Sstevel@tonic-gate 	}
44287c478bd9Sstevel@tonic-gate 
44297c478bd9Sstevel@tonic-gate 	kmem_failure_log = kmem_log_init(kmem_failure_log_size);
44307c478bd9Sstevel@tonic-gate 
44317c478bd9Sstevel@tonic-gate 	kmem_slab_log = kmem_log_init(kmem_slab_log_size);
44327c478bd9Sstevel@tonic-gate 
44337c478bd9Sstevel@tonic-gate 	/*
44347c478bd9Sstevel@tonic-gate 	 * Initialize STREAMS message caches so allocb() is available.
44357c478bd9Sstevel@tonic-gate 	 * This allows us to initialize the logging framework (cmn_err(9F),
44367c478bd9Sstevel@tonic-gate 	 * strlog(9F), etc) so we can start recording messages.
44377c478bd9Sstevel@tonic-gate 	 */
44387c478bd9Sstevel@tonic-gate 	streams_msg_init();
44397d692464Sdp201428 
44407c478bd9Sstevel@tonic-gate 	/*
44417c478bd9Sstevel@tonic-gate 	 * Initialize the ZSD framework in Zones so modules loaded henceforth
44427c478bd9Sstevel@tonic-gate 	 * can register their callbacks.
44437c478bd9Sstevel@tonic-gate 	 */
44447c478bd9Sstevel@tonic-gate 	zone_zsd_init();
4445f4b3ec61Sdh155122 
44467c478bd9Sstevel@tonic-gate 	log_init();
44477c478bd9Sstevel@tonic-gate 	taskq_init();
44487c478bd9Sstevel@tonic-gate 
44497d692464Sdp201428 	/*
44507d692464Sdp201428 	 * Warn about invalid or dangerous values of kmem_flags.
44517d692464Sdp201428 	 * Always warn about unsupported values.
44527d692464Sdp201428 	 */
44537d692464Sdp201428 	if (((kmem_flags & ~(KMF_AUDIT | KMF_DEADBEEF | KMF_REDZONE |
44547d692464Sdp201428 	    KMF_CONTENTS | KMF_LITE)) != 0) ||
44557d692464Sdp201428 	    ((kmem_flags & KMF_LITE) && kmem_flags != KMF_LITE))
44567d692464Sdp201428 		cmn_err(CE_WARN, "kmem_flags set to unsupported value 0x%x. "
44577d692464Sdp201428 		    "See the Solaris Tunable Parameters Reference Manual.",
44587d692464Sdp201428 		    kmem_flags);
44597d692464Sdp201428 
44607d692464Sdp201428 #ifdef DEBUG
44617d692464Sdp201428 	if ((kmem_flags & KMF_DEBUG) == 0)
44627d692464Sdp201428 		cmn_err(CE_NOTE, "kmem debugging disabled.");
44637d692464Sdp201428 #else
44647d692464Sdp201428 	/*
44657d692464Sdp201428 	 * For non-debug kernels, the only "normal" flags are 0, KMF_LITE,
44667d692464Sdp201428 	 * KMF_REDZONE, and KMF_CONTENTS (the last because it is only enabled
44677d692464Sdp201428 	 * if KMF_AUDIT is set). We should warn the user about the performance
44687d692464Sdp201428 	 * penalty of KMF_AUDIT or KMF_DEADBEEF if they are set and KMF_LITE
44697d692464Sdp201428 	 * isn't set (since that disables AUDIT).
44707d692464Sdp201428 	 */
44717d692464Sdp201428 	if (!(kmem_flags & KMF_LITE) &&
44727d692464Sdp201428 	    (kmem_flags & (KMF_AUDIT | KMF_DEADBEEF)) != 0)
44737d692464Sdp201428 		cmn_err(CE_WARN, "High-overhead kmem debugging features "
44747d692464Sdp201428 		    "enabled (kmem_flags = 0x%x).  Performance degradation "
44757d692464Sdp201428 		    "and large memory overhead possible. See the Solaris "
44767d692464Sdp201428 		    "Tunable Parameters Reference Manual.", kmem_flags);
44777d692464Sdp201428 #endif /* not DEBUG */
44787d692464Sdp201428 
44797c478bd9Sstevel@tonic-gate 	kmem_cache_applyall(kmem_cache_magazine_enable, NULL, TQ_SLEEP);
44807c478bd9Sstevel@tonic-gate 
44817c478bd9Sstevel@tonic-gate 	kmem_ready = 1;
44827c478bd9Sstevel@tonic-gate 
44837c478bd9Sstevel@tonic-gate 	/*
44847c478bd9Sstevel@tonic-gate 	 * Initialize the platform-specific aligned/DMA memory allocator.
44857c478bd9Sstevel@tonic-gate 	 */
44867c478bd9Sstevel@tonic-gate 	ka_init();
44877c478bd9Sstevel@tonic-gate 
44887c478bd9Sstevel@tonic-gate 	/*
44897c478bd9Sstevel@tonic-gate 	 * Initialize 32-bit ID cache.
44907c478bd9Sstevel@tonic-gate 	 */
44917c478bd9Sstevel@tonic-gate 	id32_init();
4492f4b3ec61Sdh155122 
4493f4b3ec61Sdh155122 	/*
4494f4b3ec61Sdh155122 	 * Initialize the networking stack so modules loaded can
4495f4b3ec61Sdh155122 	 * register their callbacks.
4496f4b3ec61Sdh155122 	 */
4497f4b3ec61Sdh155122 	netstack_init();
44987c478bd9Sstevel@tonic-gate }
44997c478bd9Sstevel@tonic-gate 
4500b5fca8f8Stomee static void
4501b5fca8f8Stomee kmem_move_init(void)
4502b5fca8f8Stomee {
4503b5fca8f8Stomee 	kmem_defrag_cache = kmem_cache_create("kmem_defrag_cache",
4504b5fca8f8Stomee 	    sizeof (kmem_defrag_t), 0, NULL, NULL, NULL, NULL,
4505b5fca8f8Stomee 	    kmem_msb_arena, KMC_NOHASH);
4506b5fca8f8Stomee 	kmem_move_cache = kmem_cache_create("kmem_move_cache",
4507b5fca8f8Stomee 	    sizeof (kmem_move_t), 0, NULL, NULL, NULL, NULL,
4508b5fca8f8Stomee 	    kmem_msb_arena, KMC_NOHASH);
4509b5fca8f8Stomee 
4510b5fca8f8Stomee 	/*
4511b5fca8f8Stomee 	 * kmem guarantees that move callbacks are sequential and that even
4512b5fca8f8Stomee 	 * across multiple caches no two moves ever execute simultaneously.
4513b5fca8f8Stomee 	 * Move callbacks are processed on a separate taskq so that client code
4514b5fca8f8Stomee 	 * does not interfere with internal maintenance tasks.
4515b5fca8f8Stomee 	 */
4516b5fca8f8Stomee 	kmem_move_taskq = taskq_create_instance("kmem_move_taskq", 0, 1,
4517b5fca8f8Stomee 	    minclsyspri, 100, INT_MAX, TASKQ_PREPOPULATE);
4518b5fca8f8Stomee }
4519b5fca8f8Stomee 
45207c478bd9Sstevel@tonic-gate void
45217c478bd9Sstevel@tonic-gate kmem_thread_init(void)
45227c478bd9Sstevel@tonic-gate {
4523b5fca8f8Stomee 	kmem_move_init();
45247c478bd9Sstevel@tonic-gate 	kmem_taskq = taskq_create_instance("kmem_taskq", 0, 1, minclsyspri,
45257c478bd9Sstevel@tonic-gate 	    300, INT_MAX, TASKQ_PREPOPULATE);
45267c478bd9Sstevel@tonic-gate }
45277c478bd9Sstevel@tonic-gate 
45287c478bd9Sstevel@tonic-gate void
45297c478bd9Sstevel@tonic-gate kmem_mp_init(void)
45307c478bd9Sstevel@tonic-gate {
45317c478bd9Sstevel@tonic-gate 	mutex_enter(&cpu_lock);
45327c478bd9Sstevel@tonic-gate 	register_cpu_setup_func(kmem_cpu_setup, NULL);
45337c478bd9Sstevel@tonic-gate 	mutex_exit(&cpu_lock);
45347c478bd9Sstevel@tonic-gate 
45357c478bd9Sstevel@tonic-gate 	kmem_update_timeout(NULL);
45362e0c549eSJonathan Adams 
45372e0c549eSJonathan Adams 	taskq_mp_init();
45387c478bd9Sstevel@tonic-gate }
4539b5fca8f8Stomee 
4540b5fca8f8Stomee /*
4541b5fca8f8Stomee  * Return the slab of the allocated buffer, or NULL if the buffer is not
4542b5fca8f8Stomee  * allocated. This function may be called with a known slab address to determine
4543b5fca8f8Stomee  * whether or not the buffer is allocated, or with a NULL slab address to obtain
4544b5fca8f8Stomee  * an allocated buffer's slab.
4545b5fca8f8Stomee  */
4546b5fca8f8Stomee static kmem_slab_t *
4547b5fca8f8Stomee kmem_slab_allocated(kmem_cache_t *cp, kmem_slab_t *sp, void *buf)
4548b5fca8f8Stomee {
4549b5fca8f8Stomee 	kmem_bufctl_t *bcp, *bufbcp;
4550b5fca8f8Stomee 
4551b5fca8f8Stomee 	ASSERT(MUTEX_HELD(&cp->cache_lock));
4552b5fca8f8Stomee 	ASSERT(sp == NULL || KMEM_SLAB_MEMBER(sp, buf));
4553b5fca8f8Stomee 
4554b5fca8f8Stomee 	if (cp->cache_flags & KMF_HASH) {
4555b5fca8f8Stomee 		for (bcp = *KMEM_HASH(cp, buf);
4556b5fca8f8Stomee 		    (bcp != NULL) && (bcp->bc_addr != buf);
4557b5fca8f8Stomee 		    bcp = bcp->bc_next) {
4558b5fca8f8Stomee 			continue;
4559b5fca8f8Stomee 		}
4560b5fca8f8Stomee 		ASSERT(sp != NULL && bcp != NULL ? sp == bcp->bc_slab : 1);
4561b5fca8f8Stomee 		return (bcp == NULL ? NULL : bcp->bc_slab);
4562b5fca8f8Stomee 	}
4563b5fca8f8Stomee 
4564b5fca8f8Stomee 	if (sp == NULL) {
4565b5fca8f8Stomee 		sp = KMEM_SLAB(cp, buf);
4566b5fca8f8Stomee 	}
4567b5fca8f8Stomee 	bufbcp = KMEM_BUFCTL(cp, buf);
4568b5fca8f8Stomee 	for (bcp = sp->slab_head;
4569b5fca8f8Stomee 	    (bcp != NULL) && (bcp != bufbcp);
4570b5fca8f8Stomee 	    bcp = bcp->bc_next) {
4571b5fca8f8Stomee 		continue;
4572b5fca8f8Stomee 	}
4573b5fca8f8Stomee 	return (bcp == NULL ? sp : NULL);
4574b5fca8f8Stomee }
4575b5fca8f8Stomee 
4576b5fca8f8Stomee static boolean_t
4577b5fca8f8Stomee kmem_slab_is_reclaimable(kmem_cache_t *cp, kmem_slab_t *sp, int flags)
4578b5fca8f8Stomee {
4579686031edSTom Erickson 	long refcnt = sp->slab_refcnt;
4580b5fca8f8Stomee 
4581b5fca8f8Stomee 	ASSERT(cp->cache_defrag != NULL);
4582b5fca8f8Stomee 
4583686031edSTom Erickson 	/*
4584686031edSTom Erickson 	 * For code coverage we want to be able to move an object within the
4585686031edSTom Erickson 	 * same slab (the only partial slab) even if allocating the destination
4586686031edSTom Erickson 	 * buffer resulted in a completely allocated slab.
4587686031edSTom Erickson 	 */
4588686031edSTom Erickson 	if (flags & KMM_DEBUG) {
4589686031edSTom Erickson 		return ((flags & KMM_DESPERATE) ||
4590686031edSTom Erickson 		    ((sp->slab_flags & KMEM_SLAB_NOMOVE) == 0));
4591686031edSTom Erickson 	}
4592686031edSTom Erickson 
4593b5fca8f8Stomee 	/* If we're desperate, we don't care if the client said NO. */
4594b5fca8f8Stomee 	if (flags & KMM_DESPERATE) {
4595b5fca8f8Stomee 		return (refcnt < sp->slab_chunks); /* any partial */
4596b5fca8f8Stomee 	}
4597b5fca8f8Stomee 
4598b5fca8f8Stomee 	if (sp->slab_flags & KMEM_SLAB_NOMOVE) {
4599b5fca8f8Stomee 		return (B_FALSE);
4600b5fca8f8Stomee 	}
4601b5fca8f8Stomee 
4602686031edSTom Erickson 	if ((refcnt == 1) || kmem_move_any_partial) {
4603b5fca8f8Stomee 		return (refcnt < sp->slab_chunks);
4604b5fca8f8Stomee 	}
4605b5fca8f8Stomee 
4606b5fca8f8Stomee 	/*
4607b5fca8f8Stomee 	 * The reclaim threshold is adjusted at each kmem_cache_scan() so that
4608b5fca8f8Stomee 	 * slabs with a progressively higher percentage of used buffers can be
4609b5fca8f8Stomee 	 * reclaimed until the cache as a whole is no longer fragmented.
4610b5fca8f8Stomee 	 *
4611b5fca8f8Stomee 	 *	sp->slab_refcnt   kmd_reclaim_numer
4612b5fca8f8Stomee 	 *	--------------- < ------------------
4613b5fca8f8Stomee 	 *	sp->slab_chunks   KMEM_VOID_FRACTION
4614b5fca8f8Stomee 	 */
4615b5fca8f8Stomee 	return ((refcnt * KMEM_VOID_FRACTION) <
4616b5fca8f8Stomee 	    (sp->slab_chunks * cp->cache_defrag->kmd_reclaim_numer));
4617b5fca8f8Stomee }
4618b5fca8f8Stomee 
4619b5fca8f8Stomee static void *
4620b5fca8f8Stomee kmem_hunt_mag(kmem_cache_t *cp, kmem_magazine_t *m, int n, void *buf,
4621b5fca8f8Stomee     void *tbuf)
4622b5fca8f8Stomee {
4623b5fca8f8Stomee 	int i;		/* magazine round index */
4624b5fca8f8Stomee 
4625b5fca8f8Stomee 	for (i = 0; i < n; i++) {
4626b5fca8f8Stomee 		if (buf == m->mag_round[i]) {
4627b5fca8f8Stomee 			if (cp->cache_flags & KMF_BUFTAG) {
4628b5fca8f8Stomee 				(void) kmem_cache_free_debug(cp, tbuf,
4629b5fca8f8Stomee 				    caller());
4630b5fca8f8Stomee 			}
4631b5fca8f8Stomee 			m->mag_round[i] = tbuf;
4632b5fca8f8Stomee 			return (buf);
4633b5fca8f8Stomee 		}
4634b5fca8f8Stomee 	}
4635b5fca8f8Stomee 
4636b5fca8f8Stomee 	return (NULL);
4637b5fca8f8Stomee }
4638b5fca8f8Stomee 
4639b5fca8f8Stomee /*
4640b5fca8f8Stomee  * Hunt the magazine layer for the given buffer. If found, the buffer is
4641b5fca8f8Stomee  * removed from the magazine layer and returned, otherwise NULL is returned.
4642b5fca8f8Stomee  * The state of the returned buffer is freed and constructed.
4643b5fca8f8Stomee  */
4644b5fca8f8Stomee static void *
4645b5fca8f8Stomee kmem_hunt_mags(kmem_cache_t *cp, void *buf)
4646b5fca8f8Stomee {
4647b5fca8f8Stomee 	kmem_cpu_cache_t *ccp;
4648b5fca8f8Stomee 	kmem_magazine_t	*m;
4649b5fca8f8Stomee 	int cpu_seqid;
4650b5fca8f8Stomee 	int n;		/* magazine rounds */
4651b5fca8f8Stomee 	void *tbuf;	/* temporary swap buffer */
4652b5fca8f8Stomee 
4653b5fca8f8Stomee 	ASSERT(MUTEX_NOT_HELD(&cp->cache_lock));
4654b5fca8f8Stomee 
4655b5fca8f8Stomee 	/*
4656b5fca8f8Stomee 	 * Allocated a buffer to swap with the one we hope to pull out of a
4657b5fca8f8Stomee 	 * magazine when found.
4658b5fca8f8Stomee 	 */
4659b5fca8f8Stomee 	tbuf = kmem_cache_alloc(cp, KM_NOSLEEP);
4660b5fca8f8Stomee 	if (tbuf == NULL) {
4661b5fca8f8Stomee 		KMEM_STAT_ADD(kmem_move_stats.kms_hunt_alloc_fail);
4662b5fca8f8Stomee 		return (NULL);
4663b5fca8f8Stomee 	}
4664b5fca8f8Stomee 	if (tbuf == buf) {
4665b5fca8f8Stomee 		KMEM_STAT_ADD(kmem_move_stats.kms_hunt_lucky);
4666b5fca8f8Stomee 		if (cp->cache_flags & KMF_BUFTAG) {
4667b5fca8f8Stomee 			(void) kmem_cache_free_debug(cp, buf, caller());
4668b5fca8f8Stomee 		}
4669b5fca8f8Stomee 		return (buf);
4670b5fca8f8Stomee 	}
4671b5fca8f8Stomee 
4672b5fca8f8Stomee 	/* Hunt the depot. */
4673b5fca8f8Stomee 	mutex_enter(&cp->cache_depot_lock);
4674b5fca8f8Stomee 	n = cp->cache_magtype->mt_magsize;
4675b5fca8f8Stomee 	for (m = cp->cache_full.ml_list; m != NULL; m = m->mag_next) {
4676b5fca8f8Stomee 		if (kmem_hunt_mag(cp, m, n, buf, tbuf) != NULL) {
4677b5fca8f8Stomee 			mutex_exit(&cp->cache_depot_lock);
4678b5fca8f8Stomee 			return (buf);
4679b5fca8f8Stomee 		}
4680b5fca8f8Stomee 	}
4681b5fca8f8Stomee 	mutex_exit(&cp->cache_depot_lock);
4682b5fca8f8Stomee 
4683b5fca8f8Stomee 	/* Hunt the per-CPU magazines. */
4684b5fca8f8Stomee 	for (cpu_seqid = 0; cpu_seqid < max_ncpus; cpu_seqid++) {
4685b5fca8f8Stomee 		ccp = &cp->cache_cpu[cpu_seqid];
4686b5fca8f8Stomee 
4687b5fca8f8Stomee 		mutex_enter(&ccp->cc_lock);
4688b5fca8f8Stomee 		m = ccp->cc_loaded;
4689b5fca8f8Stomee 		n = ccp->cc_rounds;
4690b5fca8f8Stomee 		if (kmem_hunt_mag(cp, m, n, buf, tbuf) != NULL) {
4691b5fca8f8Stomee 			mutex_exit(&ccp->cc_lock);
4692b5fca8f8Stomee 			return (buf);
4693b5fca8f8Stomee 		}
4694b5fca8f8Stomee 		m = ccp->cc_ploaded;
4695b5fca8f8Stomee 		n = ccp->cc_prounds;
4696b5fca8f8Stomee 		if (kmem_hunt_mag(cp, m, n, buf, tbuf) != NULL) {
4697b5fca8f8Stomee 			mutex_exit(&ccp->cc_lock);
4698b5fca8f8Stomee 			return (buf);
4699b5fca8f8Stomee 		}
4700b5fca8f8Stomee 		mutex_exit(&ccp->cc_lock);
4701b5fca8f8Stomee 	}
4702b5fca8f8Stomee 
4703b5fca8f8Stomee 	kmem_cache_free(cp, tbuf);
4704b5fca8f8Stomee 	return (NULL);
4705b5fca8f8Stomee }
4706b5fca8f8Stomee 
4707b5fca8f8Stomee /*
4708b5fca8f8Stomee  * May be called from the kmem_move_taskq, from kmem_cache_move_notify_task(),
4709b5fca8f8Stomee  * or when the buffer is freed.
4710b5fca8f8Stomee  */
4711b5fca8f8Stomee static void
4712b5fca8f8Stomee kmem_slab_move_yes(kmem_cache_t *cp, kmem_slab_t *sp, void *from_buf)
4713b5fca8f8Stomee {
4714b5fca8f8Stomee 	ASSERT(MUTEX_HELD(&cp->cache_lock));
4715b5fca8f8Stomee 	ASSERT(KMEM_SLAB_MEMBER(sp, from_buf));
4716b5fca8f8Stomee 
4717b5fca8f8Stomee 	if (!KMEM_SLAB_IS_PARTIAL(sp)) {
4718b5fca8f8Stomee 		return;
4719b5fca8f8Stomee 	}
4720b5fca8f8Stomee 
4721b5fca8f8Stomee 	if (sp->slab_flags & KMEM_SLAB_NOMOVE) {
4722b5fca8f8Stomee 		if (KMEM_SLAB_OFFSET(sp, from_buf) == sp->slab_stuck_offset) {
4723b5fca8f8Stomee 			avl_remove(&cp->cache_partial_slabs, sp);
4724b5fca8f8Stomee 			sp->slab_flags &= ~KMEM_SLAB_NOMOVE;
4725b5fca8f8Stomee 			sp->slab_stuck_offset = (uint32_t)-1;
4726b5fca8f8Stomee 			avl_add(&cp->cache_partial_slabs, sp);
4727b5fca8f8Stomee 		}
4728b5fca8f8Stomee 	} else {
4729b5fca8f8Stomee 		sp->slab_later_count = 0;
4730b5fca8f8Stomee 		sp->slab_stuck_offset = (uint32_t)-1;
4731b5fca8f8Stomee 	}
4732b5fca8f8Stomee }
4733b5fca8f8Stomee 
4734b5fca8f8Stomee static void
4735b5fca8f8Stomee kmem_slab_move_no(kmem_cache_t *cp, kmem_slab_t *sp, void *from_buf)
4736b5fca8f8Stomee {
4737b5fca8f8Stomee 	ASSERT(taskq_member(kmem_move_taskq, curthread));
4738b5fca8f8Stomee 	ASSERT(MUTEX_HELD(&cp->cache_lock));
4739b5fca8f8Stomee 	ASSERT(KMEM_SLAB_MEMBER(sp, from_buf));
4740b5fca8f8Stomee 
4741b5fca8f8Stomee 	if (!KMEM_SLAB_IS_PARTIAL(sp)) {
4742b5fca8f8Stomee 		return;
4743b5fca8f8Stomee 	}
4744b5fca8f8Stomee 
4745b5fca8f8Stomee 	avl_remove(&cp->cache_partial_slabs, sp);
4746b5fca8f8Stomee 	sp->slab_later_count = 0;
4747b5fca8f8Stomee 	sp->slab_flags |= KMEM_SLAB_NOMOVE;
4748b5fca8f8Stomee 	sp->slab_stuck_offset = KMEM_SLAB_OFFSET(sp, from_buf);
4749b5fca8f8Stomee 	avl_add(&cp->cache_partial_slabs, sp);
4750b5fca8f8Stomee }
4751b5fca8f8Stomee 
4752b5fca8f8Stomee static void kmem_move_end(kmem_cache_t *, kmem_move_t *);
4753b5fca8f8Stomee 
4754b5fca8f8Stomee /*
4755b5fca8f8Stomee  * The move callback takes two buffer addresses, the buffer to be moved, and a
4756b5fca8f8Stomee  * newly allocated and constructed buffer selected by kmem as the destination.
4757b5fca8f8Stomee  * It also takes the size of the buffer and an optional user argument specified
4758b5fca8f8Stomee  * at cache creation time. kmem guarantees that the buffer to be moved has not
4759b5fca8f8Stomee  * been unmapped by the virtual memory subsystem. Beyond that, it cannot
4760b5fca8f8Stomee  * guarantee the present whereabouts of the buffer to be moved, so it is up to
4761b5fca8f8Stomee  * the client to safely determine whether or not it is still using the buffer.
4762b5fca8f8Stomee  * The client must not free either of the buffers passed to the move callback,
4763b5fca8f8Stomee  * since kmem wants to free them directly to the slab layer. The client response
4764b5fca8f8Stomee  * tells kmem which of the two buffers to free:
4765b5fca8f8Stomee  *
4766b5fca8f8Stomee  * YES		kmem frees the old buffer (the move was successful)
4767b5fca8f8Stomee  * NO		kmem frees the new buffer, marks the slab of the old buffer
4768b5fca8f8Stomee  *              non-reclaimable to avoid bothering the client again
4769b5fca8f8Stomee  * LATER	kmem frees the new buffer, increments slab_later_count
4770b5fca8f8Stomee  * DONT_KNOW	kmem frees the new buffer, searches mags for the old buffer
4771b5fca8f8Stomee  * DONT_NEED	kmem frees both the old buffer and the new buffer
4772b5fca8f8Stomee  *
4773b5fca8f8Stomee  * The pending callback argument now being processed contains both of the
4774b5fca8f8Stomee  * buffers (old and new) passed to the move callback function, the slab of the
4775b5fca8f8Stomee  * old buffer, and flags related to the move request, such as whether or not the
4776b5fca8f8Stomee  * system was desperate for memory.
4777686031edSTom Erickson  *
4778686031edSTom Erickson  * Slabs are not freed while there is a pending callback, but instead are kept
4779686031edSTom Erickson  * on a deadlist, which is drained after the last callback completes. This means
4780686031edSTom Erickson  * that slabs are safe to access until kmem_move_end(), no matter how many of
4781686031edSTom Erickson  * their buffers have been freed. Once slab_refcnt reaches zero, it stays at
4782686031edSTom Erickson  * zero for as long as the slab remains on the deadlist and until the slab is
4783686031edSTom Erickson  * freed.
4784b5fca8f8Stomee  */
4785b5fca8f8Stomee static void
4786b5fca8f8Stomee kmem_move_buffer(kmem_move_t *callback)
4787b5fca8f8Stomee {
4788b5fca8f8Stomee 	kmem_cbrc_t response;
4789b5fca8f8Stomee 	kmem_slab_t *sp = callback->kmm_from_slab;
4790b5fca8f8Stomee 	kmem_cache_t *cp = sp->slab_cache;
4791b5fca8f8Stomee 	boolean_t free_on_slab;
4792b5fca8f8Stomee 
4793b5fca8f8Stomee 	ASSERT(taskq_member(kmem_move_taskq, curthread));
4794b5fca8f8Stomee 	ASSERT(MUTEX_NOT_HELD(&cp->cache_lock));
4795b5fca8f8Stomee 	ASSERT(KMEM_SLAB_MEMBER(sp, callback->kmm_from_buf));
4796b5fca8f8Stomee 
4797b5fca8f8Stomee 	/*
4798b5fca8f8Stomee 	 * The number of allocated buffers on the slab may have changed since we
4799b5fca8f8Stomee 	 * last checked the slab's reclaimability (when the pending move was
4800b5fca8f8Stomee 	 * enqueued), or the client may have responded NO when asked to move
4801b5fca8f8Stomee 	 * another buffer on the same slab.
4802b5fca8f8Stomee 	 */
4803b5fca8f8Stomee 	if (!kmem_slab_is_reclaimable(cp, sp, callback->kmm_flags)) {
4804b5fca8f8Stomee 		KMEM_STAT_ADD(kmem_move_stats.kms_no_longer_reclaimable);
4805b5fca8f8Stomee 		KMEM_STAT_COND_ADD((callback->kmm_flags & KMM_NOTIFY),
4806b5fca8f8Stomee 		    kmem_move_stats.kms_notify_no_longer_reclaimable);
4807b5fca8f8Stomee 		kmem_slab_free(cp, callback->kmm_to_buf);
4808b5fca8f8Stomee 		kmem_move_end(cp, callback);
4809b5fca8f8Stomee 		return;
4810b5fca8f8Stomee 	}
4811b5fca8f8Stomee 
4812b5fca8f8Stomee 	/*
4813b5fca8f8Stomee 	 * Hunting magazines is expensive, so we'll wait to do that until the
4814b5fca8f8Stomee 	 * client responds KMEM_CBRC_DONT_KNOW. However, checking the slab layer
4815b5fca8f8Stomee 	 * is cheap, so we might as well do that here in case we can avoid
4816b5fca8f8Stomee 	 * bothering the client.
4817b5fca8f8Stomee 	 */
4818b5fca8f8Stomee 	mutex_enter(&cp->cache_lock);
4819b5fca8f8Stomee 	free_on_slab = (kmem_slab_allocated(cp, sp,
4820b5fca8f8Stomee 	    callback->kmm_from_buf) == NULL);
4821b5fca8f8Stomee 	mutex_exit(&cp->cache_lock);
4822b5fca8f8Stomee 
4823b5fca8f8Stomee 	if (free_on_slab) {
4824b5fca8f8Stomee 		KMEM_STAT_ADD(kmem_move_stats.kms_hunt_found_slab);
4825b5fca8f8Stomee 		kmem_slab_free(cp, callback->kmm_to_buf);
4826b5fca8f8Stomee 		kmem_move_end(cp, callback);
4827b5fca8f8Stomee 		return;
4828b5fca8f8Stomee 	}
4829b5fca8f8Stomee 
4830b5fca8f8Stomee 	if (cp->cache_flags & KMF_BUFTAG) {
4831b5fca8f8Stomee 		/*
4832b5fca8f8Stomee 		 * Make kmem_cache_alloc_debug() apply the constructor for us.
4833b5fca8f8Stomee 		 */
4834b5fca8f8Stomee 		if (kmem_cache_alloc_debug(cp, callback->kmm_to_buf,
4835b5fca8f8Stomee 		    KM_NOSLEEP, 1, caller()) != 0) {
4836b5fca8f8Stomee 			KMEM_STAT_ADD(kmem_move_stats.kms_alloc_fail);
4837b5fca8f8Stomee 			kmem_move_end(cp, callback);
4838b5fca8f8Stomee 			return;
4839b5fca8f8Stomee 		}
4840b5fca8f8Stomee 	} else if (cp->cache_constructor != NULL &&
4841b5fca8f8Stomee 	    cp->cache_constructor(callback->kmm_to_buf, cp->cache_private,
4842b5fca8f8Stomee 	    KM_NOSLEEP) != 0) {
48431a5e258fSJosef 'Jeff' Sipek 		atomic_inc_64(&cp->cache_alloc_fail);
4844b5fca8f8Stomee 		KMEM_STAT_ADD(kmem_move_stats.kms_constructor_fail);
4845b5fca8f8Stomee 		kmem_slab_free(cp, callback->kmm_to_buf);
4846b5fca8f8Stomee 		kmem_move_end(cp, callback);
4847b5fca8f8Stomee 		return;
4848b5fca8f8Stomee 	}
4849b5fca8f8Stomee 
4850b5fca8f8Stomee 	KMEM_STAT_ADD(kmem_move_stats.kms_callbacks);
4851b5fca8f8Stomee 	KMEM_STAT_COND_ADD((callback->kmm_flags & KMM_NOTIFY),
4852b5fca8f8Stomee 	    kmem_move_stats.kms_notify_callbacks);
4853b5fca8f8Stomee 	cp->cache_defrag->kmd_callbacks++;
4854b5fca8f8Stomee 	cp->cache_defrag->kmd_thread = curthread;
4855b5fca8f8Stomee 	cp->cache_defrag->kmd_from_buf = callback->kmm_from_buf;
4856b5fca8f8Stomee 	cp->cache_defrag->kmd_to_buf = callback->kmm_to_buf;
4857b5fca8f8Stomee 	DTRACE_PROBE2(kmem__move__start, kmem_cache_t *, cp, kmem_move_t *,
4858b5fca8f8Stomee 	    callback);
4859b5fca8f8Stomee 
4860b5fca8f8Stomee 	response = cp->cache_move(callback->kmm_from_buf,
4861b5fca8f8Stomee 	    callback->kmm_to_buf, cp->cache_bufsize, cp->cache_private);
4862b5fca8f8Stomee 
4863b5fca8f8Stomee 	DTRACE_PROBE3(kmem__move__end, kmem_cache_t *, cp, kmem_move_t *,
4864b5fca8f8Stomee 	    callback, kmem_cbrc_t, response);
4865b5fca8f8Stomee 	cp->cache_defrag->kmd_thread = NULL;
4866b5fca8f8Stomee 	cp->cache_defrag->kmd_from_buf = NULL;
4867b5fca8f8Stomee 	cp->cache_defrag->kmd_to_buf = NULL;
4868b5fca8f8Stomee 
4869b5fca8f8Stomee 	if (response == KMEM_CBRC_YES) {
4870b5fca8f8Stomee 		KMEM_STAT_ADD(kmem_move_stats.kms_yes);
4871b5fca8f8Stomee 		cp->cache_defrag->kmd_yes++;
4872b5fca8f8Stomee 		kmem_slab_free_constructed(cp, callback->kmm_from_buf, B_FALSE);
4873686031edSTom Erickson 		/* slab safe to access until kmem_move_end() */
4874686031edSTom Erickson 		if (sp->slab_refcnt == 0)
4875686031edSTom Erickson 			cp->cache_defrag->kmd_slabs_freed++;
4876b5fca8f8Stomee 		mutex_enter(&cp->cache_lock);
4877b5fca8f8Stomee 		kmem_slab_move_yes(cp, sp, callback->kmm_from_buf);
4878b5fca8f8Stomee 		mutex_exit(&cp->cache_lock);
4879b5fca8f8Stomee 		kmem_move_end(cp, callback);
4880b5fca8f8Stomee 		return;
4881b5fca8f8Stomee 	}
4882b5fca8f8Stomee 
4883b5fca8f8Stomee 	switch (response) {
4884b5fca8f8Stomee 	case KMEM_CBRC_NO:
4885b5fca8f8Stomee 		KMEM_STAT_ADD(kmem_move_stats.kms_no);
4886b5fca8f8Stomee 		cp->cache_defrag->kmd_no++;
4887b5fca8f8Stomee 		mutex_enter(&cp->cache_lock);
4888b5fca8f8Stomee 		kmem_slab_move_no(cp, sp, callback->kmm_from_buf);
4889b5fca8f8Stomee 		mutex_exit(&cp->cache_lock);
4890b5fca8f8Stomee 		break;
4891b5fca8f8Stomee 	case KMEM_CBRC_LATER:
4892b5fca8f8Stomee 		KMEM_STAT_ADD(kmem_move_stats.kms_later);
4893b5fca8f8Stomee 		cp->cache_defrag->kmd_later++;
4894b5fca8f8Stomee 		mutex_enter(&cp->cache_lock);
4895b5fca8f8Stomee 		if (!KMEM_SLAB_IS_PARTIAL(sp)) {
4896b5fca8f8Stomee 			mutex_exit(&cp->cache_lock);
4897b5fca8f8Stomee 			break;
4898b5fca8f8Stomee 		}
4899b5fca8f8Stomee 
4900b5fca8f8Stomee 		if (++sp->slab_later_count >= KMEM_DISBELIEF) {
4901b5fca8f8Stomee 			KMEM_STAT_ADD(kmem_move_stats.kms_disbelief);
4902b5fca8f8Stomee 			kmem_slab_move_no(cp, sp, callback->kmm_from_buf);
4903b5fca8f8Stomee 		} else if (!(sp->slab_flags & KMEM_SLAB_NOMOVE)) {
4904b5fca8f8Stomee 			sp->slab_stuck_offset = KMEM_SLAB_OFFSET(sp,
4905b5fca8f8Stomee 			    callback->kmm_from_buf);
4906b5fca8f8Stomee 		}
4907b5fca8f8Stomee 		mutex_exit(&cp->cache_lock);
4908b5fca8f8Stomee 		break;
4909b5fca8f8Stomee 	case KMEM_CBRC_DONT_NEED:
4910b5fca8f8Stomee 		KMEM_STAT_ADD(kmem_move_stats.kms_dont_need);
4911b5fca8f8Stomee 		cp->cache_defrag->kmd_dont_need++;
4912b5fca8f8Stomee 		kmem_slab_free_constructed(cp, callback->kmm_from_buf, B_FALSE);
4913686031edSTom Erickson 		if (sp->slab_refcnt == 0)
4914686031edSTom Erickson 			cp->cache_defrag->kmd_slabs_freed++;
4915b5fca8f8Stomee 		mutex_enter(&cp->cache_lock);
4916b5fca8f8Stomee 		kmem_slab_move_yes(cp, sp, callback->kmm_from_buf);
4917b5fca8f8Stomee 		mutex_exit(&cp->cache_lock);
4918b5fca8f8Stomee 		break;
4919b5fca8f8Stomee 	case KMEM_CBRC_DONT_KNOW:
4920b5fca8f8Stomee 		KMEM_STAT_ADD(kmem_move_stats.kms_dont_know);
4921b5fca8f8Stomee 		cp->cache_defrag->kmd_dont_know++;
4922b5fca8f8Stomee 		if (kmem_hunt_mags(cp, callback->kmm_from_buf) != NULL) {
4923b5fca8f8Stomee 			KMEM_STAT_ADD(kmem_move_stats.kms_hunt_found_mag);
4924b5fca8f8Stomee 			cp->cache_defrag->kmd_hunt_found++;
4925b5fca8f8Stomee 			kmem_slab_free_constructed(cp, callback->kmm_from_buf,
4926b5fca8f8Stomee 			    B_TRUE);
4927686031edSTom Erickson 			if (sp->slab_refcnt == 0)
4928686031edSTom Erickson 				cp->cache_defrag->kmd_slabs_freed++;
4929b5fca8f8Stomee 			mutex_enter(&cp->cache_lock);
4930b5fca8f8Stomee 			kmem_slab_move_yes(cp, sp, callback->kmm_from_buf);
4931b5fca8f8Stomee 			mutex_exit(&cp->cache_lock);
4932b5fca8f8Stomee 		}
4933b5fca8f8Stomee 		break;
4934b5fca8f8Stomee 	default:
4935b5fca8f8Stomee 		panic("'%s' (%p) unexpected move callback response %d\n",
4936b5fca8f8Stomee 		    cp->cache_name, (void *)cp, response);
4937b5fca8f8Stomee 	}
4938b5fca8f8Stomee 
4939b5fca8f8Stomee 	kmem_slab_free_constructed(cp, callback->kmm_to_buf, B_FALSE);
4940b5fca8f8Stomee 	kmem_move_end(cp, callback);
4941b5fca8f8Stomee }
4942b5fca8f8Stomee 
4943b5fca8f8Stomee /* Return B_FALSE if there is insufficient memory for the move request. */
4944b5fca8f8Stomee static boolean_t
4945b5fca8f8Stomee kmem_move_begin(kmem_cache_t *cp, kmem_slab_t *sp, void *buf, int flags)
4946b5fca8f8Stomee {
4947b5fca8f8Stomee 	void *to_buf;
4948b5fca8f8Stomee 	avl_index_t index;
4949b5fca8f8Stomee 	kmem_move_t *callback, *pending;
4950686031edSTom Erickson 	ulong_t n;
4951b5fca8f8Stomee 
4952b5fca8f8Stomee 	ASSERT(taskq_member(kmem_taskq, curthread));
4953b5fca8f8Stomee 	ASSERT(MUTEX_NOT_HELD(&cp->cache_lock));
4954b5fca8f8Stomee 	ASSERT(sp->slab_flags & KMEM_SLAB_MOVE_PENDING);
4955b5fca8f8Stomee 
4956b5fca8f8Stomee 	callback = kmem_cache_alloc(kmem_move_cache, KM_NOSLEEP);
4957b5fca8f8Stomee 	if (callback == NULL) {
4958b5fca8f8Stomee 		KMEM_STAT_ADD(kmem_move_stats.kms_callback_alloc_fail);
4959b5fca8f8Stomee 		return (B_FALSE);
4960b5fca8f8Stomee 	}
4961b5fca8f8Stomee 
4962b5fca8f8Stomee 	callback->kmm_from_slab = sp;
4963b5fca8f8Stomee 	callback->kmm_from_buf = buf;
4964b5fca8f8Stomee 	callback->kmm_flags = flags;
4965b5fca8f8Stomee 
4966b5fca8f8Stomee 	mutex_enter(&cp->cache_lock);
4967b5fca8f8Stomee 
4968686031edSTom Erickson 	n = avl_numnodes(&cp->cache_partial_slabs);
4969686031edSTom Erickson 	if ((n == 0) || ((n == 1) && !(flags & KMM_DEBUG))) {
4970b5fca8f8Stomee 		mutex_exit(&cp->cache_lock);
4971b5fca8f8Stomee 		kmem_cache_free(kmem_move_cache, callback);
4972b5fca8f8Stomee 		return (B_TRUE); /* there is no need for the move request */
4973b5fca8f8Stomee 	}
4974b5fca8f8Stomee 
4975b5fca8f8Stomee 	pending = avl_find(&cp->cache_defrag->kmd_moves_pending, buf, &index);
4976b5fca8f8Stomee 	if (pending != NULL) {
4977b5fca8f8Stomee 		/*
4978b5fca8f8Stomee 		 * If the move is already pending and we're desperate now,
4979b5fca8f8Stomee 		 * update the move flags.
4980b5fca8f8Stomee 		 */
4981b5fca8f8Stomee 		if (flags & KMM_DESPERATE) {
4982b5fca8f8Stomee 			pending->kmm_flags |= KMM_DESPERATE;
4983b5fca8f8Stomee 		}
4984b5fca8f8Stomee 		mutex_exit(&cp->cache_lock);
4985b5fca8f8Stomee 		KMEM_STAT_ADD(kmem_move_stats.kms_already_pending);
4986b5fca8f8Stomee 		kmem_cache_free(kmem_move_cache, callback);
4987b5fca8f8Stomee 		return (B_TRUE);
4988b5fca8f8Stomee 	}
4989b5fca8f8Stomee 
4990b942e89bSDavid Valin 	to_buf = kmem_slab_alloc_impl(cp, avl_first(&cp->cache_partial_slabs),
4991b942e89bSDavid Valin 	    B_FALSE);
4992b5fca8f8Stomee 	callback->kmm_to_buf = to_buf;
4993b5fca8f8Stomee 	avl_insert(&cp->cache_defrag->kmd_moves_pending, callback, index);
4994b5fca8f8Stomee 
4995b5fca8f8Stomee 	mutex_exit(&cp->cache_lock);
4996b5fca8f8Stomee 
4997b5fca8f8Stomee 	if (!taskq_dispatch(kmem_move_taskq, (task_func_t *)kmem_move_buffer,
4998b5fca8f8Stomee 	    callback, TQ_NOSLEEP)) {
499925e2c9cfStomee 		KMEM_STAT_ADD(kmem_move_stats.kms_callback_taskq_fail);
5000b5fca8f8Stomee 		mutex_enter(&cp->cache_lock);
5001b5fca8f8Stomee 		avl_remove(&cp->cache_defrag->kmd_moves_pending, callback);
5002b5fca8f8Stomee 		mutex_exit(&cp->cache_lock);
500325e2c9cfStomee 		kmem_slab_free(cp, to_buf);
5004b5fca8f8Stomee 		kmem_cache_free(kmem_move_cache, callback);
5005b5fca8f8Stomee 		return (B_FALSE);
5006b5fca8f8Stomee 	}
5007b5fca8f8Stomee 
5008b5fca8f8Stomee 	return (B_TRUE);
5009b5fca8f8Stomee }
5010b5fca8f8Stomee 
5011b5fca8f8Stomee static void
5012b5fca8f8Stomee kmem_move_end(kmem_cache_t *cp, kmem_move_t *callback)
5013b5fca8f8Stomee {
5014b5fca8f8Stomee 	avl_index_t index;
5015b5fca8f8Stomee 
5016b5fca8f8Stomee 	ASSERT(cp->cache_defrag != NULL);
5017b5fca8f8Stomee 	ASSERT(taskq_member(kmem_move_taskq, curthread));
5018b5fca8f8Stomee 	ASSERT(MUTEX_NOT_HELD(&cp->cache_lock));
5019b5fca8f8Stomee 
5020b5fca8f8Stomee 	mutex_enter(&cp->cache_lock);
5021b5fca8f8Stomee 	VERIFY(avl_find(&cp->cache_defrag->kmd_moves_pending,
5022b5fca8f8Stomee 	    callback->kmm_from_buf, &index) != NULL);
5023b5fca8f8Stomee 	avl_remove(&cp->cache_defrag->kmd_moves_pending, callback);
5024b5fca8f8Stomee 	if (avl_is_empty(&cp->cache_defrag->kmd_moves_pending)) {
5025b5fca8f8Stomee 		list_t *deadlist = &cp->cache_defrag->kmd_deadlist;
5026b5fca8f8Stomee 		kmem_slab_t *sp;
5027b5fca8f8Stomee 
5028b5fca8f8Stomee 		/*
5029b5fca8f8Stomee 		 * The last pending move completed. Release all slabs from the
5030b5fca8f8Stomee 		 * front of the dead list except for any slab at the tail that
5031b5fca8f8Stomee 		 * needs to be released from the context of kmem_move_buffers().
5032b5fca8f8Stomee 		 * kmem deferred unmapping the buffers on these slabs in order
5033b5fca8f8Stomee 		 * to guarantee that buffers passed to the move callback have
5034b5fca8f8Stomee 		 * been touched only by kmem or by the client itself.
5035b5fca8f8Stomee 		 */
5036b5fca8f8Stomee 		while ((sp = list_remove_head(deadlist)) != NULL) {
5037b5fca8f8Stomee 			if (sp->slab_flags & KMEM_SLAB_MOVE_PENDING) {
5038b5fca8f8Stomee 				list_insert_tail(deadlist, sp);
5039b5fca8f8Stomee 				break;
5040b5fca8f8Stomee 			}
5041b5fca8f8Stomee 			cp->cache_defrag->kmd_deadcount--;
5042b5fca8f8Stomee 			cp->cache_slab_destroy++;
5043b5fca8f8Stomee 			mutex_exit(&cp->cache_lock);
5044b5fca8f8Stomee 			kmem_slab_destroy(cp, sp);
5045b5fca8f8Stomee 			KMEM_STAT_ADD(kmem_move_stats.kms_dead_slabs_freed);
5046b5fca8f8Stomee 			mutex_enter(&cp->cache_lock);
5047b5fca8f8Stomee 		}
5048b5fca8f8Stomee 	}
5049b5fca8f8Stomee 	mutex_exit(&cp->cache_lock);
5050b5fca8f8Stomee 	kmem_cache_free(kmem_move_cache, callback);
5051b5fca8f8Stomee }
5052b5fca8f8Stomee 
5053b5fca8f8Stomee /*
5054b5fca8f8Stomee  * Move buffers from least used slabs first by scanning backwards from the end
5055b5fca8f8Stomee  * of the partial slab list. Scan at most max_scan candidate slabs and move
5056b5fca8f8Stomee  * buffers from at most max_slabs slabs (0 for all partial slabs in both cases).
5057b5fca8f8Stomee  * If desperate to reclaim memory, move buffers from any partial slab, otherwise
5058b5fca8f8Stomee  * skip slabs with a ratio of allocated buffers at or above the current
5059b5fca8f8Stomee  * threshold. Return the number of unskipped slabs (at most max_slabs, -1 if the
5060b5fca8f8Stomee  * scan is aborted) so that the caller can adjust the reclaimability threshold
5061b5fca8f8Stomee  * depending on how many reclaimable slabs it finds.
5062b5fca8f8Stomee  *
5063b5fca8f8Stomee  * kmem_move_buffers() drops and reacquires cache_lock every time it issues a
5064b5fca8f8Stomee  * move request, since it is not valid for kmem_move_begin() to call
5065b5fca8f8Stomee  * kmem_cache_alloc() or taskq_dispatch() with cache_lock held.
5066b5fca8f8Stomee  */
5067b5fca8f8Stomee static int
5068b5fca8f8Stomee kmem_move_buffers(kmem_cache_t *cp, size_t max_scan, size_t max_slabs,
5069b5fca8f8Stomee     int flags)
5070b5fca8f8Stomee {
5071b5fca8f8Stomee 	kmem_slab_t *sp;
5072b5fca8f8Stomee 	void *buf;
5073b5fca8f8Stomee 	int i, j; /* slab index, buffer index */
5074b5fca8f8Stomee 	int s; /* reclaimable slabs */
5075b5fca8f8Stomee 	int b; /* allocated (movable) buffers on reclaimable slab */
5076b5fca8f8Stomee 	boolean_t success;
5077b5fca8f8Stomee 	int refcnt;
5078b5fca8f8Stomee 	int nomove;
5079b5fca8f8Stomee 
5080b5fca8f8Stomee 	ASSERT(taskq_member(kmem_taskq, curthread));
5081b5fca8f8Stomee 	ASSERT(MUTEX_HELD(&cp->cache_lock));
5082b5fca8f8Stomee 	ASSERT(kmem_move_cache != NULL);
5083b5fca8f8Stomee 	ASSERT(cp->cache_move != NULL && cp->cache_defrag != NULL);
5084686031edSTom Erickson 	ASSERT((flags & KMM_DEBUG) ? !avl_is_empty(&cp->cache_partial_slabs) :
5085686031edSTom Erickson 	    avl_numnodes(&cp->cache_partial_slabs) > 1);
5086b5fca8f8Stomee 
5087b5fca8f8Stomee 	if (kmem_move_blocked) {
5088b5fca8f8Stomee 		return (0);
5089b5fca8f8Stomee 	}
5090b5fca8f8Stomee 
5091b5fca8f8Stomee 	if (kmem_move_fulltilt) {
5092b5fca8f8Stomee 		flags |= KMM_DESPERATE;
5093b5fca8f8Stomee 	}
5094b5fca8f8Stomee 
5095b5fca8f8Stomee 	if (max_scan == 0 || (flags & KMM_DESPERATE)) {
5096b5fca8f8Stomee 		/*
5097b5fca8f8Stomee 		 * Scan as many slabs as needed to find the desired number of
5098b5fca8f8Stomee 		 * candidate slabs.
5099b5fca8f8Stomee 		 */
5100b5fca8f8Stomee 		max_scan = (size_t)-1;
5101b5fca8f8Stomee 	}
5102b5fca8f8Stomee 
5103b5fca8f8Stomee 	if (max_slabs == 0 || (flags & KMM_DESPERATE)) {
5104b5fca8f8Stomee 		/* Find as many candidate slabs as possible. */
5105b5fca8f8Stomee 		max_slabs = (size_t)-1;
5106b5fca8f8Stomee 	}
5107b5fca8f8Stomee 
5108b5fca8f8Stomee 	sp = avl_last(&cp->cache_partial_slabs);
5109686031edSTom Erickson 	ASSERT(KMEM_SLAB_IS_PARTIAL(sp));
5110686031edSTom Erickson 	for (i = 0, s = 0; (i < max_scan) && (s < max_slabs) && (sp != NULL) &&
5111686031edSTom Erickson 	    ((sp != avl_first(&cp->cache_partial_slabs)) ||
5112686031edSTom Erickson 	    (flags & KMM_DEBUG));
5113b5fca8f8Stomee 	    sp = AVL_PREV(&cp->cache_partial_slabs, sp), i++) {
5114b5fca8f8Stomee 
5115b5fca8f8Stomee 		if (!kmem_slab_is_reclaimable(cp, sp, flags)) {
5116b5fca8f8Stomee 			continue;
5117b5fca8f8Stomee 		}
5118b5fca8f8Stomee 		s++;
5119b5fca8f8Stomee 
5120b5fca8f8Stomee 		/* Look for allocated buffers to move. */
5121b5fca8f8Stomee 		for (j = 0, b = 0, buf = sp->slab_base;
5122b5fca8f8Stomee 		    (j < sp->slab_chunks) && (b < sp->slab_refcnt);
5123b5fca8f8Stomee 		    buf = (((char *)buf) + cp->cache_chunksize), j++) {
5124b5fca8f8Stomee 
5125b5fca8f8Stomee 			if (kmem_slab_allocated(cp, sp, buf) == NULL) {
5126b5fca8f8Stomee 				continue;
5127b5fca8f8Stomee 			}
5128b5fca8f8Stomee 
5129b5fca8f8Stomee 			b++;
5130b5fca8f8Stomee 
5131b5fca8f8Stomee 			/*
5132b5fca8f8Stomee 			 * Prevent the slab from being destroyed while we drop
5133b5fca8f8Stomee 			 * cache_lock and while the pending move is not yet
5134b5fca8f8Stomee 			 * registered. Flag the pending move while
5135b5fca8f8Stomee 			 * kmd_moves_pending may still be empty, since we can't
5136b5fca8f8Stomee 			 * yet rely on a non-zero pending move count to prevent
5137b5fca8f8Stomee 			 * the slab from being destroyed.
5138b5fca8f8Stomee 			 */
5139b5fca8f8Stomee 			ASSERT(!(sp->slab_flags & KMEM_SLAB_MOVE_PENDING));
5140b5fca8f8Stomee 			sp->slab_flags |= KMEM_SLAB_MOVE_PENDING;
5141b5fca8f8Stomee 			/*
5142b5fca8f8Stomee 			 * Recheck refcnt and nomove after reacquiring the lock,
5143b5fca8f8Stomee 			 * since these control the order of partial slabs, and
5144b5fca8f8Stomee 			 * we want to know if we can pick up the scan where we
5145b5fca8f8Stomee 			 * left off.
5146b5fca8f8Stomee 			 */
5147b5fca8f8Stomee 			refcnt = sp->slab_refcnt;
5148b5fca8f8Stomee 			nomove = (sp->slab_flags & KMEM_SLAB_NOMOVE);
5149b5fca8f8Stomee 			mutex_exit(&cp->cache_lock);
5150b5fca8f8Stomee 
5151b5fca8f8Stomee 			success = kmem_move_begin(cp, sp, buf, flags);
5152b5fca8f8Stomee 
5153b5fca8f8Stomee 			/*
5154b5fca8f8Stomee 			 * Now, before the lock is reacquired, kmem could
5155b5fca8f8Stomee 			 * process all pending move requests and purge the
5156b5fca8f8Stomee 			 * deadlist, so that upon reacquiring the lock, sp has
5157686031edSTom Erickson 			 * been remapped. Or, the client may free all the
5158686031edSTom Erickson 			 * objects on the slab while the pending moves are still
5159686031edSTom Erickson 			 * on the taskq. Therefore, the KMEM_SLAB_MOVE_PENDING
5160b5fca8f8Stomee 			 * flag causes the slab to be put at the end of the
5161686031edSTom Erickson 			 * deadlist and prevents it from being destroyed, since
5162686031edSTom Erickson 			 * we plan to destroy it here after reacquiring the
5163686031edSTom Erickson 			 * lock.
5164b5fca8f8Stomee 			 */
5165b5fca8f8Stomee 			mutex_enter(&cp->cache_lock);
5166b5fca8f8Stomee 			ASSERT(sp->slab_flags & KMEM_SLAB_MOVE_PENDING);
5167b5fca8f8Stomee 			sp->slab_flags &= ~KMEM_SLAB_MOVE_PENDING;
5168b5fca8f8Stomee 
5169b5fca8f8Stomee 			if (sp->slab_refcnt == 0) {
5170b5fca8f8Stomee 				list_t *deadlist =
5171b5fca8f8Stomee 				    &cp->cache_defrag->kmd_deadlist;
5172b5fca8f8Stomee 				list_remove(deadlist, sp);
5173686031edSTom Erickson 
5174686031edSTom Erickson 				if (!avl_is_empty(
5175686031edSTom Erickson 				    &cp->cache_defrag->kmd_moves_pending)) {
5176686031edSTom Erickson 					/*
5177686031edSTom Erickson 					 * A pending move makes it unsafe to
5178686031edSTom Erickson 					 * destroy the slab, because even though
5179686031edSTom Erickson 					 * the move is no longer needed, the
5180686031edSTom Erickson 					 * context where that is determined
5181686031edSTom Erickson 					 * requires the slab to exist.
5182686031edSTom Erickson 					 * Fortunately, a pending move also
5183686031edSTom Erickson 					 * means we don't need to destroy the
5184686031edSTom Erickson 					 * slab here, since it will get
5185686031edSTom Erickson 					 * destroyed along with any other slabs
5186686031edSTom Erickson 					 * on the deadlist after the last
5187686031edSTom Erickson 					 * pending move completes.
5188686031edSTom Erickson 					 */
5189686031edSTom Erickson 					list_insert_head(deadlist, sp);
5190686031edSTom Erickson 					KMEM_STAT_ADD(kmem_move_stats.
5191686031edSTom Erickson 					    kms_endscan_slab_dead);
5192686031edSTom Erickson 					return (-1);
5193686031edSTom Erickson 				}
5194686031edSTom Erickson 
5195686031edSTom Erickson 				/*
5196686031edSTom Erickson 				 * Destroy the slab now if it was completely
5197686031edSTom Erickson 				 * freed while we dropped cache_lock and there
5198686031edSTom Erickson 				 * are no pending moves. Since slab_refcnt
5199686031edSTom Erickson 				 * cannot change once it reaches zero, no new
5200686031edSTom Erickson 				 * pending moves from that slab are possible.
5201686031edSTom Erickson 				 */
5202b5fca8f8Stomee 				cp->cache_defrag->kmd_deadcount--;
5203b5fca8f8Stomee 				cp->cache_slab_destroy++;
5204b5fca8f8Stomee 				mutex_exit(&cp->cache_lock);
5205b5fca8f8Stomee 				kmem_slab_destroy(cp, sp);
5206b5fca8f8Stomee 				KMEM_STAT_ADD(kmem_move_stats.
5207b5fca8f8Stomee 				    kms_dead_slabs_freed);
5208b5fca8f8Stomee 				KMEM_STAT_ADD(kmem_move_stats.
5209b5fca8f8Stomee 				    kms_endscan_slab_destroyed);
5210b5fca8f8Stomee 				mutex_enter(&cp->cache_lock);
5211b5fca8f8Stomee 				/*
5212b5fca8f8Stomee 				 * Since we can't pick up the scan where we left
5213b5fca8f8Stomee 				 * off, abort the scan and say nothing about the
5214b5fca8f8Stomee 				 * number of reclaimable slabs.
5215b5fca8f8Stomee 				 */
5216b5fca8f8Stomee 				return (-1);
5217b5fca8f8Stomee 			}
5218b5fca8f8Stomee 
5219b5fca8f8Stomee 			if (!success) {
5220b5fca8f8Stomee 				/*
5221b5fca8f8Stomee 				 * Abort the scan if there is not enough memory
5222b5fca8f8Stomee 				 * for the request and say nothing about the
5223b5fca8f8Stomee 				 * number of reclaimable slabs.
5224b5fca8f8Stomee 				 */
5225686031edSTom Erickson 				KMEM_STAT_COND_ADD(s < max_slabs,
5226b5fca8f8Stomee 				    kmem_move_stats.kms_endscan_nomem);
5227b5fca8f8Stomee 				return (-1);
5228b5fca8f8Stomee 			}
5229b5fca8f8Stomee 
5230b5fca8f8Stomee 			/*
5231b5fca8f8Stomee 			 * The slab's position changed while the lock was
5232b5fca8f8Stomee 			 * dropped, so we don't know where we are in the
5233b5fca8f8Stomee 			 * sequence any more.
5234b5fca8f8Stomee 			 */
5235b5fca8f8Stomee 			if (sp->slab_refcnt != refcnt) {
5236686031edSTom Erickson 				/*
5237686031edSTom Erickson 				 * If this is a KMM_DEBUG move, the slab_refcnt
5238686031edSTom Erickson 				 * may have changed because we allocated a
5239686031edSTom Erickson 				 * destination buffer on the same slab. In that
5240686031edSTom Erickson 				 * case, we're not interested in counting it.
5241686031edSTom Erickson 				 */
5242686031edSTom Erickson 				KMEM_STAT_COND_ADD(!(flags & KMM_DEBUG) &&
5243686031edSTom Erickson 				    (s < max_slabs),
5244b5fca8f8Stomee 				    kmem_move_stats.kms_endscan_refcnt_changed);
5245b5fca8f8Stomee 				return (-1);
5246b5fca8f8Stomee 			}
5247b5fca8f8Stomee 			if ((sp->slab_flags & KMEM_SLAB_NOMOVE) != nomove) {
5248686031edSTom Erickson 				KMEM_STAT_COND_ADD(s < max_slabs,
5249b5fca8f8Stomee 				    kmem_move_stats.kms_endscan_nomove_changed);
5250b5fca8f8Stomee 				return (-1);
5251b5fca8f8Stomee 			}
5252b5fca8f8Stomee 
5253b5fca8f8Stomee 			/*
5254b5fca8f8Stomee 			 * Generating a move request allocates a destination
5255686031edSTom Erickson 			 * buffer from the slab layer, bumping the first partial
5256686031edSTom Erickson 			 * slab if it is completely allocated. If the current
5257686031edSTom Erickson 			 * slab becomes the first partial slab as a result, we
5258686031edSTom Erickson 			 * can't continue to scan backwards.
5259686031edSTom Erickson 			 *
5260686031edSTom Erickson 			 * If this is a KMM_DEBUG move and we allocated the
5261686031edSTom Erickson 			 * destination buffer from the last partial slab, then
5262686031edSTom Erickson 			 * the buffer we're moving is on the same slab and our
5263686031edSTom Erickson 			 * slab_refcnt has changed, causing us to return before
5264686031edSTom Erickson 			 * reaching here if there are no partial slabs left.
5265b5fca8f8Stomee 			 */
5266b5fca8f8Stomee 			ASSERT(!avl_is_empty(&cp->cache_partial_slabs));
5267b5fca8f8Stomee 			if (sp == avl_first(&cp->cache_partial_slabs)) {
5268686031edSTom Erickson 				/*
5269686031edSTom Erickson 				 * We're not interested in a second KMM_DEBUG
5270686031edSTom Erickson 				 * move.
5271686031edSTom Erickson 				 */
5272b5fca8f8Stomee 				goto end_scan;
5273b5fca8f8Stomee 			}
5274b5fca8f8Stomee 		}
5275b5fca8f8Stomee 	}
5276b5fca8f8Stomee end_scan:
5277b5fca8f8Stomee 
5278686031edSTom Erickson 	KMEM_STAT_COND_ADD(!(flags & KMM_DEBUG) &&
5279686031edSTom Erickson 	    (s < max_slabs) &&
5280686031edSTom Erickson 	    (sp == avl_first(&cp->cache_partial_slabs)),
5281b5fca8f8Stomee 	    kmem_move_stats.kms_endscan_freelist);
5282b5fca8f8Stomee 
5283b5fca8f8Stomee 	return (s);
5284b5fca8f8Stomee }
5285b5fca8f8Stomee 
5286b5fca8f8Stomee typedef struct kmem_move_notify_args {
5287b5fca8f8Stomee 	kmem_cache_t *kmna_cache;
5288b5fca8f8Stomee 	void *kmna_buf;
5289b5fca8f8Stomee } kmem_move_notify_args_t;
5290b5fca8f8Stomee 
5291b5fca8f8Stomee static void
5292b5fca8f8Stomee kmem_cache_move_notify_task(void *arg)
5293b5fca8f8Stomee {
5294b5fca8f8Stomee 	kmem_move_notify_args_t *args = arg;
5295b5fca8f8Stomee 	kmem_cache_t *cp = args->kmna_cache;
5296b5fca8f8Stomee 	void *buf = args->kmna_buf;
5297b5fca8f8Stomee 	kmem_slab_t *sp;
5298b5fca8f8Stomee 
5299b5fca8f8Stomee 	ASSERT(taskq_member(kmem_taskq, curthread));
5300b5fca8f8Stomee 	ASSERT(list_link_active(&cp->cache_link));
5301b5fca8f8Stomee 
5302b5fca8f8Stomee 	kmem_free(args, sizeof (kmem_move_notify_args_t));
5303b5fca8f8Stomee 	mutex_enter(&cp->cache_lock);
5304b5fca8f8Stomee 	sp = kmem_slab_allocated(cp, NULL, buf);
5305b5fca8f8Stomee 
5306b5fca8f8Stomee 	/* Ignore the notification if the buffer is no longer allocated. */
5307b5fca8f8Stomee 	if (sp == NULL) {
5308b5fca8f8Stomee 		mutex_exit(&cp->cache_lock);
5309b5fca8f8Stomee 		return;
5310b5fca8f8Stomee 	}
5311b5fca8f8Stomee 
5312b5fca8f8Stomee 	/* Ignore the notification if there's no reason to move the buffer. */
5313b5fca8f8Stomee 	if (avl_numnodes(&cp->cache_partial_slabs) > 1) {
5314b5fca8f8Stomee 		/*
5315b5fca8f8Stomee 		 * So far the notification is not ignored. Ignore the
5316b5fca8f8Stomee 		 * notification if the slab is not marked by an earlier refusal
5317b5fca8f8Stomee 		 * to move a buffer.
5318b5fca8f8Stomee 		 */
5319b5fca8f8Stomee 		if (!(sp->slab_flags & KMEM_SLAB_NOMOVE) &&
5320b5fca8f8Stomee 		    (sp->slab_later_count == 0)) {
5321b5fca8f8Stomee 			mutex_exit(&cp->cache_lock);
5322b5fca8f8Stomee 			return;
5323b5fca8f8Stomee 		}
5324b5fca8f8Stomee 
5325b5fca8f8Stomee 		kmem_slab_move_yes(cp, sp, buf);
5326b5fca8f8Stomee 		ASSERT(!(sp->slab_flags & KMEM_SLAB_MOVE_PENDING));
5327b5fca8f8Stomee 		sp->slab_flags |= KMEM_SLAB_MOVE_PENDING;
5328b5fca8f8Stomee 		mutex_exit(&cp->cache_lock);
5329b5fca8f8Stomee 		/* see kmem_move_buffers() about dropping the lock */
5330b5fca8f8Stomee 		(void) kmem_move_begin(cp, sp, buf, KMM_NOTIFY);
5331b5fca8f8Stomee 		mutex_enter(&cp->cache_lock);
5332b5fca8f8Stomee 		ASSERT(sp->slab_flags & KMEM_SLAB_MOVE_PENDING);
5333b5fca8f8Stomee 		sp->slab_flags &= ~KMEM_SLAB_MOVE_PENDING;
5334b5fca8f8Stomee 		if (sp->slab_refcnt == 0) {
5335b5fca8f8Stomee 			list_t *deadlist = &cp->cache_defrag->kmd_deadlist;
5336b5fca8f8Stomee 			list_remove(deadlist, sp);
5337686031edSTom Erickson 
5338686031edSTom Erickson 			if (!avl_is_empty(
5339686031edSTom Erickson 			    &cp->cache_defrag->kmd_moves_pending)) {
5340686031edSTom Erickson 				list_insert_head(deadlist, sp);
5341686031edSTom Erickson 				mutex_exit(&cp->cache_lock);
5342686031edSTom Erickson 				KMEM_STAT_ADD(kmem_move_stats.
5343686031edSTom Erickson 				    kms_notify_slab_dead);
5344686031edSTom Erickson 				return;
5345686031edSTom Erickson 			}
5346686031edSTom Erickson 
5347b5fca8f8Stomee 			cp->cache_defrag->kmd_deadcount--;
5348b5fca8f8Stomee 			cp->cache_slab_destroy++;
5349b5fca8f8Stomee 			mutex_exit(&cp->cache_lock);
5350b5fca8f8Stomee 			kmem_slab_destroy(cp, sp);
5351b5fca8f8Stomee 			KMEM_STAT_ADD(kmem_move_stats.kms_dead_slabs_freed);
5352686031edSTom Erickson 			KMEM_STAT_ADD(kmem_move_stats.
5353686031edSTom Erickson 			    kms_notify_slab_destroyed);
5354b5fca8f8Stomee 			return;
5355b5fca8f8Stomee 		}
5356b5fca8f8Stomee 	} else {
5357b5fca8f8Stomee 		kmem_slab_move_yes(cp, sp, buf);
5358b5fca8f8Stomee 	}
5359b5fca8f8Stomee 	mutex_exit(&cp->cache_lock);
5360b5fca8f8Stomee }
5361b5fca8f8Stomee 
5362b5fca8f8Stomee void
5363b5fca8f8Stomee kmem_cache_move_notify(kmem_cache_t *cp, void *buf)
5364b5fca8f8Stomee {
5365b5fca8f8Stomee 	kmem_move_notify_args_t *args;
5366b5fca8f8Stomee 
5367b5fca8f8Stomee 	KMEM_STAT_ADD(kmem_move_stats.kms_notify);
5368b5fca8f8Stomee 	args = kmem_alloc(sizeof (kmem_move_notify_args_t), KM_NOSLEEP);
5369b5fca8f8Stomee 	if (args != NULL) {
5370b5fca8f8Stomee 		args->kmna_cache = cp;
5371b5fca8f8Stomee 		args->kmna_buf = buf;
5372eb697d4eStomee 		if (!taskq_dispatch(kmem_taskq,
5373b5fca8f8Stomee 		    (task_func_t *)kmem_cache_move_notify_task, args,
5374eb697d4eStomee 		    TQ_NOSLEEP))
5375eb697d4eStomee 			kmem_free(args, sizeof (kmem_move_notify_args_t));
5376b5fca8f8Stomee 	}
5377b5fca8f8Stomee }
5378b5fca8f8Stomee 
5379b5fca8f8Stomee static void
5380b5fca8f8Stomee kmem_cache_defrag(kmem_cache_t *cp)
5381b5fca8f8Stomee {
5382b5fca8f8Stomee 	size_t n;
5383b5fca8f8Stomee 
5384b5fca8f8Stomee 	ASSERT(cp->cache_defrag != NULL);
5385b5fca8f8Stomee 
5386b5fca8f8Stomee 	mutex_enter(&cp->cache_lock);
5387b5fca8f8Stomee 	n = avl_numnodes(&cp->cache_partial_slabs);
5388b5fca8f8Stomee 	if (n > 1) {
5389b5fca8f8Stomee 		/* kmem_move_buffers() drops and reacquires cache_lock */
5390b5fca8f8Stomee 		KMEM_STAT_ADD(kmem_move_stats.kms_defrags);
5391686031edSTom Erickson 		cp->cache_defrag->kmd_defrags++;
5392686031edSTom Erickson 		(void) kmem_move_buffers(cp, n, 0, KMM_DESPERATE);
5393b5fca8f8Stomee 	}
5394b5fca8f8Stomee 	mutex_exit(&cp->cache_lock);
5395b5fca8f8Stomee }
5396b5fca8f8Stomee 
5397b5fca8f8Stomee /* Is this cache above the fragmentation threshold? */
5398b5fca8f8Stomee static boolean_t
5399b5fca8f8Stomee kmem_cache_frag_threshold(kmem_cache_t *cp, uint64_t nfree)
5400b5fca8f8Stomee {
5401b5fca8f8Stomee 	/*
5402b5fca8f8Stomee 	 *	nfree		kmem_frag_numer
5403b5fca8f8Stomee 	 * ------------------ > ---------------
5404b5fca8f8Stomee 	 * cp->cache_buftotal	kmem_frag_denom
5405b5fca8f8Stomee 	 */
5406b5fca8f8Stomee 	return ((nfree * kmem_frag_denom) >
5407b5fca8f8Stomee 	    (cp->cache_buftotal * kmem_frag_numer));
5408b5fca8f8Stomee }
5409b5fca8f8Stomee 
5410b5fca8f8Stomee static boolean_t
5411b5fca8f8Stomee kmem_cache_is_fragmented(kmem_cache_t *cp, boolean_t *doreap)
5412b5fca8f8Stomee {
5413b5fca8f8Stomee 	boolean_t fragmented;
5414b5fca8f8Stomee 	uint64_t nfree;
5415b5fca8f8Stomee 
5416b5fca8f8Stomee 	ASSERT(MUTEX_HELD(&cp->cache_lock));
5417b5fca8f8Stomee 	*doreap = B_FALSE;
5418b5fca8f8Stomee 
5419686031edSTom Erickson 	if (kmem_move_fulltilt) {
5420686031edSTom Erickson 		if (avl_numnodes(&cp->cache_partial_slabs) > 1) {
5421686031edSTom Erickson 			return (B_TRUE);
5422686031edSTom Erickson 		}
5423686031edSTom Erickson 	} else {
5424686031edSTom Erickson 		if ((cp->cache_complete_slab_count + avl_numnodes(
5425686031edSTom Erickson 		    &cp->cache_partial_slabs)) < kmem_frag_minslabs) {
5426b5fca8f8Stomee 			return (B_FALSE);
5427686031edSTom Erickson 		}
5428686031edSTom Erickson 	}
5429b5fca8f8Stomee 
5430b5fca8f8Stomee 	nfree = cp->cache_bufslab;
5431686031edSTom Erickson 	fragmented = ((avl_numnodes(&cp->cache_partial_slabs) > 1) &&
5432686031edSTom Erickson 	    kmem_cache_frag_threshold(cp, nfree));
5433686031edSTom Erickson 
5434b5fca8f8Stomee 	/*
5435b5fca8f8Stomee 	 * Free buffers in the magazine layer appear allocated from the point of
5436b5fca8f8Stomee 	 * view of the slab layer. We want to know if the slab layer would
5437b5fca8f8Stomee 	 * appear fragmented if we included free buffers from magazines that
5438b5fca8f8Stomee 	 * have fallen out of the working set.
5439b5fca8f8Stomee 	 */
5440b5fca8f8Stomee 	if (!fragmented) {
5441b5fca8f8Stomee 		long reap;
5442b5fca8f8Stomee 
5443b5fca8f8Stomee 		mutex_enter(&cp->cache_depot_lock);
5444b5fca8f8Stomee 		reap = MIN(cp->cache_full.ml_reaplimit, cp->cache_full.ml_min);
5445b5fca8f8Stomee 		reap = MIN(reap, cp->cache_full.ml_total);
5446b5fca8f8Stomee 		mutex_exit(&cp->cache_depot_lock);
5447b5fca8f8Stomee 
5448b5fca8f8Stomee 		nfree += ((uint64_t)reap * cp->cache_magtype->mt_magsize);
5449b5fca8f8Stomee 		if (kmem_cache_frag_threshold(cp, nfree)) {
5450b5fca8f8Stomee 			*doreap = B_TRUE;
5451b5fca8f8Stomee 		}
5452b5fca8f8Stomee 	}
5453b5fca8f8Stomee 
5454b5fca8f8Stomee 	return (fragmented);
5455b5fca8f8Stomee }
5456b5fca8f8Stomee 
5457b5fca8f8Stomee /* Called periodically from kmem_taskq */
5458b5fca8f8Stomee static void
5459b5fca8f8Stomee kmem_cache_scan(kmem_cache_t *cp)
5460b5fca8f8Stomee {
5461b5fca8f8Stomee 	boolean_t reap = B_FALSE;
5462686031edSTom Erickson 	kmem_defrag_t *kmd;
5463b5fca8f8Stomee 
5464b5fca8f8Stomee 	ASSERT(taskq_member(kmem_taskq, curthread));
5465b5fca8f8Stomee 
5466b5fca8f8Stomee 	mutex_enter(&cp->cache_lock);
5467b5fca8f8Stomee 
5468686031edSTom Erickson 	kmd = cp->cache_defrag;
5469686031edSTom Erickson 	if (kmd->kmd_consolidate > 0) {
5470686031edSTom Erickson 		kmd->kmd_consolidate--;
5471686031edSTom Erickson 		mutex_exit(&cp->cache_lock);
5472686031edSTom Erickson 		kmem_cache_reap(cp);
5473686031edSTom Erickson 		return;
5474686031edSTom Erickson 	}
5475686031edSTom Erickson 
5476b5fca8f8Stomee 	if (kmem_cache_is_fragmented(cp, &reap)) {
5477b5fca8f8Stomee 		size_t slabs_found;
5478b5fca8f8Stomee 
5479b5fca8f8Stomee 		/*
5480b5fca8f8Stomee 		 * Consolidate reclaimable slabs from the end of the partial
5481b5fca8f8Stomee 		 * slab list (scan at most kmem_reclaim_scan_range slabs to find
5482b5fca8f8Stomee 		 * reclaimable slabs). Keep track of how many candidate slabs we
5483b5fca8f8Stomee 		 * looked for and how many we actually found so we can adjust
5484b5fca8f8Stomee 		 * the definition of a candidate slab if we're having trouble
5485b5fca8f8Stomee 		 * finding them.
5486b5fca8f8Stomee 		 *
5487b5fca8f8Stomee 		 * kmem_move_buffers() drops and reacquires cache_lock.
5488b5fca8f8Stomee 		 */
5489686031edSTom Erickson 		KMEM_STAT_ADD(kmem_move_stats.kms_scans);
5490686031edSTom Erickson 		kmd->kmd_scans++;
5491b5fca8f8Stomee 		slabs_found = kmem_move_buffers(cp, kmem_reclaim_scan_range,
5492b5fca8f8Stomee 		    kmem_reclaim_max_slabs, 0);
5493b5fca8f8Stomee 		if (slabs_found >= 0) {
5494b5fca8f8Stomee 			kmd->kmd_slabs_sought += kmem_reclaim_max_slabs;
5495b5fca8f8Stomee 			kmd->kmd_slabs_found += slabs_found;
5496b5fca8f8Stomee 		}
5497b5fca8f8Stomee 
5498686031edSTom Erickson 		if (++kmd->kmd_tries >= kmem_reclaim_scan_range) {
5499686031edSTom Erickson 			kmd->kmd_tries = 0;
5500b5fca8f8Stomee 
5501b5fca8f8Stomee 			/*
5502b5fca8f8Stomee 			 * If we had difficulty finding candidate slabs in
5503b5fca8f8Stomee 			 * previous scans, adjust the threshold so that
5504b5fca8f8Stomee 			 * candidates are easier to find.
5505b5fca8f8Stomee 			 */
5506b5fca8f8Stomee 			if (kmd->kmd_slabs_found == kmd->kmd_slabs_sought) {
5507b5fca8f8Stomee 				kmem_adjust_reclaim_threshold(kmd, -1);
5508b5fca8f8Stomee 			} else if ((kmd->kmd_slabs_found * 2) <
5509b5fca8f8Stomee 			    kmd->kmd_slabs_sought) {
5510b5fca8f8Stomee 				kmem_adjust_reclaim_threshold(kmd, 1);
5511b5fca8f8Stomee 			}
5512b5fca8f8Stomee 			kmd->kmd_slabs_sought = 0;
5513b5fca8f8Stomee 			kmd->kmd_slabs_found = 0;
5514b5fca8f8Stomee 		}
5515b5fca8f8Stomee 	} else {
5516b5fca8f8Stomee 		kmem_reset_reclaim_threshold(cp->cache_defrag);
5517b5fca8f8Stomee #ifdef	DEBUG
5518686031edSTom Erickson 		if (!avl_is_empty(&cp->cache_partial_slabs)) {
5519b5fca8f8Stomee 			/*
5520b5fca8f8Stomee 			 * In a debug kernel we want the consolidator to
5521b5fca8f8Stomee 			 * run occasionally even when there is plenty of
5522b5fca8f8Stomee 			 * memory.
5523b5fca8f8Stomee 			 */
5524686031edSTom Erickson 			uint16_t debug_rand;
5525b5fca8f8Stomee 
5526686031edSTom Erickson 			(void) random_get_bytes((uint8_t *)&debug_rand, 2);
5527b5fca8f8Stomee 			if (!kmem_move_noreap &&
5528b5fca8f8Stomee 			    ((debug_rand % kmem_mtb_reap) == 0)) {
5529b5fca8f8Stomee 				mutex_exit(&cp->cache_lock);
5530b5fca8f8Stomee 				KMEM_STAT_ADD(kmem_move_stats.kms_debug_reaps);
5531686031edSTom Erickson 				kmem_cache_reap(cp);
5532b5fca8f8Stomee 				return;
5533b5fca8f8Stomee 			} else if ((debug_rand % kmem_mtb_move) == 0) {
5534686031edSTom Erickson 				KMEM_STAT_ADD(kmem_move_stats.kms_scans);
5535686031edSTom Erickson 				KMEM_STAT_ADD(kmem_move_stats.kms_debug_scans);
5536686031edSTom Erickson 				kmd->kmd_scans++;
5537b5fca8f8Stomee 				(void) kmem_move_buffers(cp,
5538686031edSTom Erickson 				    kmem_reclaim_scan_range, 1, KMM_DEBUG);
5539b5fca8f8Stomee 			}
5540b5fca8f8Stomee 		}
5541b5fca8f8Stomee #endif	/* DEBUG */
5542b5fca8f8Stomee 	}
5543b5fca8f8Stomee 
5544b5fca8f8Stomee 	mutex_exit(&cp->cache_lock);
5545b5fca8f8Stomee 
5546b5fca8f8Stomee 	if (reap) {
5547b5fca8f8Stomee 		KMEM_STAT_ADD(kmem_move_stats.kms_scan_depot_ws_reaps);
5548b5fca8f8Stomee 		kmem_depot_ws_reap(cp);
5549b5fca8f8Stomee 	}
5550b5fca8f8Stomee }
5551