4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2018, Joyent, Inc.
24 * Copyright (c) 2011, 2019 by Delphix. All rights reserved.
25 * Copyright (c) 2014 by Saso Kiselkov. All rights reserved.
26 * Copyright 2017 Nexenta Systems, Inc. All rights reserved.
31 #include <sys/spa_impl.h>
32 #include <sys/zio_compress.h>
33 #include <sys/zio_checksum.h>
34 #include <sys/zfs_context.h>
36 #include <sys/zfs_refcount.h>
38 #include <sys/vdev_trim.h>
39 #include <sys/vdev_impl.h>
40 #include <sys/dsl_pool.h>
41 #include <sys/multilist.h>
44 #include <sys/fm/fs/zfs.h>
45 #include <sys/shrinker.h>
46 #include <sys/vmsystm.h>
48 #include <linux/page_compat.h>
49 #include <linux/notifier.h>
50 #include <linux/memory.h>
51 #include <linux/version.h>
52 #include <sys/callb.h>
53 #include <sys/kstat.h>
55 #include <zfs_fletcher.h>
56 #include <sys/arc_impl.h>
57 #include <sys/trace_zfs.h>
58 #include <sys/aggsum.h>
61 * This is a limit on how many pages the ARC shrinker makes available for
62 * eviction in response to one page allocation attempt. Note that in
63 * practice, the kernel's shrinker can ask us to evict up to about 4x this
64 * for one allocation attempt.
66 * The default limit of 10,000 (in practice, 160MB per allocation attempt
67 * with 4K pages) limits the amount of time spent attempting to reclaim ARC
68 * memory to less than 100ms per allocation attempt, even with a small
69 * average compressed block size of ~8KB.
71 * See also the comment in arc_shrinker_count().
72 * Set to 0 to disable limit.
74 static int zfs_arc_shrinker_limit
= 10000;
77 * Relative cost of ARC eviction, AKA number of seeks needed to restore evicted
78 * page. Bigger values make ARC more precious and evictions smaller comparing
79 * to other kernel subsystems. Value of 4 means parity with page cache,
80 * according to my reading of kernel's do_shrink_slab() and other code.
82 static int zfs_arc_shrinker_seeks
= DEFAULT_SEEKS
;
84 #ifdef CONFIG_MEMORY_HOTPLUG
85 static struct notifier_block arc_hotplug_callback_mem_nb
;
89 * Return a default max arc size based on the amount of physical memory.
90 * This may be overridden by tuning the zfs_arc_max module parameter.
93 arc_default_max(uint64_t min
, uint64_t allmem
)
97 if (allmem
>= 1 << 30)
98 size
= allmem
- (1 << 30);
101 return (MAX(allmem
* 5 / 8, size
));
105 * Return maximum amount of memory that we could possibly use. Reduced
106 * to half of all memory in user space which is primarily used for testing.
111 #ifdef CONFIG_HIGHMEM
112 return (ptob(zfs_totalram_pages
- zfs_totalhigh_pages
));
114 return (ptob(zfs_totalram_pages
));
115 #endif /* CONFIG_HIGHMEM */
119 * Return the amount of memory that is considered free. In user space
120 * which is primarily used for testing we pretend that free memory ranges
121 * from 0-20% of all memory.
124 arc_free_memory(void)
126 #ifdef CONFIG_HIGHMEM
129 return (ptob(si
.freeram
- si
.freehigh
));
131 return (ptob(nr_free_pages() +
132 nr_inactive_file_pages()));
133 #endif /* CONFIG_HIGHMEM */
137 * Return the amount of memory that can be consumed before reclaim will be
138 * needed. Positive if there is sufficient free memory, negative indicates
139 * the amount of memory that needs to be freed up.
142 arc_available_memory(void)
144 return (arc_free_memory() - arc_sys_free
);
148 arc_evictable_memory(void)
150 int64_t asize
= aggsum_value(&arc_sums
.arcstat_size
);
152 zfs_refcount_count(&arc_mru
->arcs_esize
[ARC_BUFC_DATA
]) +
153 zfs_refcount_count(&arc_mru
->arcs_esize
[ARC_BUFC_METADATA
]) +
154 zfs_refcount_count(&arc_mfu
->arcs_esize
[ARC_BUFC_DATA
]) +
155 zfs_refcount_count(&arc_mfu
->arcs_esize
[ARC_BUFC_METADATA
]);
156 uint64_t arc_dirty
= MAX((int64_t)asize
- (int64_t)arc_clean
, 0);
159 * Scale reported evictable memory in proportion to page cache, cap
160 * at specified min/max.
162 uint64_t min
= (ptob(nr_file_pages()) / 100) * zfs_arc_pc_percent
;
163 min
= MAX(arc_c_min
, MIN(arc_c_max
, min
));
165 if (arc_dirty
>= min
)
168 return (MAX((int64_t)asize
- (int64_t)min
, 0));
172 * The _count() function returns the number of free-able objects.
173 * The _scan() function returns the number of objects that were freed.
176 arc_shrinker_count(struct shrinker
*shrink
, struct shrink_control
*sc
)
179 * The kernel's shrinker code may not understand how many pages the
180 * ARC's callback actually frees, so it may ask the ARC to shrink a
181 * lot for one page allocation. This is problematic because it may
182 * take a long time, thus delaying the page allocation, and because
183 * it may force the ARC to unnecessarily shrink very small.
185 * Therefore, we limit the amount of data that we say is evictable,
186 * which limits the amount that the shrinker will ask us to evict for
187 * one page allocation attempt.
189 * In practice, we may be asked to shrink 4x the limit to satisfy one
190 * page allocation, before the kernel's shrinker code gives up on us.
191 * When that happens, we rely on the kernel code to find the pages
192 * that we freed before invoking the OOM killer. This happens in
193 * __alloc_pages_slowpath(), which retries and finds the pages we
194 * freed when it calls get_page_from_freelist().
196 * See also the comment above zfs_arc_shrinker_limit.
198 int64_t can_free
= btop(arc_evictable_memory());
199 if (current_is_kswapd() && zfs_arc_shrinker_limit
)
200 can_free
= MIN(can_free
, zfs_arc_shrinker_limit
);
205 arc_shrinker_scan(struct shrinker
*shrink
, struct shrink_control
*sc
)
207 /* The arc is considered warm once reclaim has occurred */
208 if (unlikely(arc_warm
== B_FALSE
))
212 * We are experiencing memory pressure which the arc_evict_zthr was
213 * unable to keep up with. Set arc_no_grow to briefly pause ARC
214 * growth to avoid compounding the memory pressure.
216 arc_no_grow
= B_TRUE
;
219 * Evict the requested number of pages by reducing arc_c and waiting
220 * for the requested amount of data to be evicted. To avoid deadlock
221 * do not wait for eviction if we may be called from ZFS itself (see
222 * kmem_flags_convert() removing __GFP_FS). It may cause excessive
223 * eviction later if many evictions are accumulated, but just skipping
224 * the eviction is not good either if most of memory is used by ARC.
226 uint64_t to_free
= arc_reduce_target_size(ptob(sc
->nr_to_scan
));
227 if (sc
->gfp_mask
& __GFP_FS
)
228 arc_wait_for_eviction(to_free
, B_FALSE
, B_FALSE
);
229 if (current
->reclaim_state
!= NULL
)
230 #ifdef HAVE_RECLAIM_STATE_RECLAIMED
231 current
->reclaim_state
->reclaimed
+= btop(to_free
);
233 current
->reclaim_state
->reclaimed_slab
+= btop(to_free
);
237 * When direct reclaim is observed it usually indicates a rapid
238 * increase in memory pressure. This occurs because the kswapd
239 * threads were unable to asynchronously keep enough free memory
242 if (current_is_kswapd()) {
243 ARCSTAT_BUMP(arcstat_memory_indirect_count
);
245 ARCSTAT_BUMP(arcstat_memory_direct_count
);
248 return (btop(to_free
));
251 static struct shrinker
*arc_shrinker
= NULL
;
254 arc_memory_throttle(spa_t
*spa
, uint64_t reserve
, uint64_t txg
)
256 uint64_t free_memory
= arc_free_memory();
258 if (free_memory
> arc_all_memory() * arc_lotsfree_percent
/ 100)
261 if (txg
> spa
->spa_lowmem_last_txg
) {
262 spa
->spa_lowmem_last_txg
= txg
;
263 spa
->spa_lowmem_page_load
= 0;
266 * If we are in pageout, we know that memory is already tight,
267 * the arc is already going to be evicting, so we just want to
268 * continue to let page writes occur as quickly as possible.
270 if (current_is_kswapd()) {
271 if (spa
->spa_lowmem_page_load
>
272 MAX(arc_sys_free
/ 4, free_memory
) / 4) {
273 DMU_TX_STAT_BUMP(dmu_tx_memory_reclaim
);
274 return (SET_ERROR(ERESTART
));
276 /* Note: reserve is inflated, so we deflate */
277 atomic_add_64(&spa
->spa_lowmem_page_load
, reserve
/ 8);
279 } else if (spa
->spa_lowmem_page_load
> 0 && arc_reclaim_needed()) {
280 /* memory is low, delay before restarting */
281 ARCSTAT_INCR(arcstat_memory_throttle_count
, 1);
282 DMU_TX_STAT_BUMP(dmu_tx_memory_reclaim
);
283 return (SET_ERROR(EAGAIN
));
285 spa
->spa_lowmem_page_load
= 0;
290 arc_set_sys_free(uint64_t allmem
)
293 * The ARC tries to keep at least this much memory available for the
294 * system. This gives the ARC time to shrink in response to memory
295 * pressure, before running completely out of memory and invoking the
296 * direct-reclaim ARC shrinker.
298 * This should be more than twice high_wmark_pages(), so that
299 * arc_wait_for_eviction() will wait until at least the
300 * high_wmark_pages() are free (see arc_evict_state_impl()).
302 * Note: If concurrent allocations consume these pages, there may
303 * still be insufficient free pages, and the OOM killer takes action.
305 * By setting arc_sys_free large enough, and having
306 * arc_wait_for_eviction() wait until there is at least arc_sys_free/2
307 * free memory, it is much less likely that concurrent allocations can
308 * consume all the memory that was evicted before checking for
311 * It's hard to iterate the zones from a linux kernel module, which
312 * makes it difficult to determine the watermark dynamically. Instead
313 * we compute the maximum high watermark for this system, based
314 * on the amount of memory, using the same method as the kernel uses
315 * to calculate its internal `min_free_kbytes` variable. See
316 * torvalds/linux@ee8eb9a5fe86 for the change in the upper clamp value
321 * Base wmark_low is 4 * the square root of Kbytes of RAM.
323 long wmark
= int_sqrt(allmem
/ 1024 * 16) * 1024;
326 * Clamp to between 128K and 256/64MB.
328 wmark
= MAX(wmark
, 128 * 1024);
329 #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 7, 0)
330 wmark
= MIN(wmark
, 256 * 1024 * 1024);
332 wmark
= MIN(wmark
, 64 * 1024 * 1024);
336 * watermark_boost can increase the wmark by up to 150%.
338 wmark
+= wmark
* 150 / 100;
341 * arc_sys_free needs to be more than 2x the watermark, because
342 * arc_wait_for_eviction() waits for half of arc_sys_free. Bump this up
343 * to 3x to ensure we're above it.
345 arc_sys_free
= wmark
* 3 + allmem
/ 32;
349 arc_lowmem_init(void)
351 uint64_t allmem
= arc_all_memory();
354 * Register a shrinker to support synchronous (direct) memory
355 * reclaim from the arc. This is done to prevent kswapd from
356 * swapping out pages when it is preferable to shrink the arc.
358 arc_shrinker
= spl_register_shrinker("zfs-arc-shrinker",
359 arc_shrinker_count
, arc_shrinker_scan
, zfs_arc_shrinker_seeks
);
360 VERIFY(arc_shrinker
);
362 arc_set_sys_free(allmem
);
366 arc_lowmem_fini(void)
368 spl_unregister_shrinker(arc_shrinker
);
373 param_set_arc_u64(const char *buf
, zfs_kernel_param_t
*kp
)
377 error
= spl_param_set_u64(buf
, kp
);
379 return (SET_ERROR(error
));
381 arc_tuning_update(B_TRUE
);
387 param_set_arc_min(const char *buf
, zfs_kernel_param_t
*kp
)
389 return (param_set_arc_u64(buf
, kp
));
393 param_set_arc_max(const char *buf
, zfs_kernel_param_t
*kp
)
395 return (param_set_arc_u64(buf
, kp
));
399 param_set_arc_int(const char *buf
, zfs_kernel_param_t
*kp
)
403 error
= param_set_int(buf
, kp
);
405 return (SET_ERROR(error
));
407 arc_tuning_update(B_TRUE
);
412 #ifdef CONFIG_MEMORY_HOTPLUG
414 arc_hotplug_callback(struct notifier_block
*self
, unsigned long action
,
417 (void) self
, (void) arg
;
418 uint64_t allmem
= arc_all_memory();
419 if (action
!= MEM_ONLINE
)
422 arc_set_limits(allmem
);
425 if (zfs_dirty_data_max_max
== 0)
426 zfs_dirty_data_max_max
= MIN(4ULL * 1024 * 1024 * 1024,
427 allmem
* zfs_dirty_data_max_max_percent
/ 100);
429 if (zfs_dirty_data_max_max
== 0)
430 zfs_dirty_data_max_max
= MIN(1ULL * 1024 * 1024 * 1024,
431 allmem
* zfs_dirty_data_max_max_percent
/ 100);
434 arc_set_sys_free(allmem
);
440 arc_register_hotplug(void)
442 #ifdef CONFIG_MEMORY_HOTPLUG
443 arc_hotplug_callback_mem_nb
.notifier_call
= arc_hotplug_callback
;
444 /* There is no significance to the value 100 */
445 arc_hotplug_callback_mem_nb
.priority
= 100;
446 register_memory_notifier(&arc_hotplug_callback_mem_nb
);
451 arc_unregister_hotplug(void)
453 #ifdef CONFIG_MEMORY_HOTPLUG
454 unregister_memory_notifier(&arc_hotplug_callback_mem_nb
);
458 ZFS_MODULE_PARAM(zfs_arc
, zfs_arc_
, shrinker_limit
, INT
, ZMOD_RW
,
459 "Limit on number of pages that ARC shrinker can reclaim at once");
460 ZFS_MODULE_PARAM(zfs_arc
, zfs_arc_
, shrinker_seeks
, INT
, ZMOD_RD
,
461 "Relative cost of ARC eviction vs other kernel subsystems");