Path: blob/main/sys/contrib/openzfs/module/os/linux/zfs/arc_os.c
48774 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* Common Development and Distribution License (the "License").6* You may not use this file except in compliance with the License.7*8* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE9* or https://opensource.org/licenses/CDDL-1.0.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/21/*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.27*/2829#include <sys/spa.h>30#include <sys/zio.h>31#include <sys/spa_impl.h>32#include <sys/zio_compress.h>33#include <sys/zio_checksum.h>34#include <sys/zfs_context.h>35#include <sys/arc.h>36#include <sys/zfs_refcount.h>37#include <sys/vdev.h>38#include <sys/vdev_trim.h>39#include <sys/vdev_impl.h>40#include <sys/dsl_pool.h>41#include <sys/multilist.h>42#include <sys/abd.h>43#include <sys/zil.h>44#include <sys/fm/fs/zfs.h>45#include <sys/shrinker.h>46#include <sys/vmsystm.h>47#include <sys/zpl.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>54#include <sys/zthr.h>55#include <zfs_fletcher.h>56#include <sys/arc_impl.h>57#include <sys/trace_zfs.h>58#include <sys/aggsum.h>5960/*61* This is a limit on how many pages the ARC shrinker makes available for62* eviction in response to one page allocation attempt. Note that in63* practice, the kernel's shrinker can ask us to evict up to about 4x this64* for one allocation attempt.65*66* For example a value of 10,000 (in practice, 160MB per allocation attempt67* with 4K pages) limits the amount of time spent attempting to reclaim ARC68* memory to less than 100ms per allocation attempt, even with a small69* average compressed block size of ~8KB.70*71* See also the comment in arc_shrinker_count().72* Set to 0 to disable limit.73*/74static int zfs_arc_shrinker_limit = 0;7576/*77* Relative cost of ARC eviction, AKA number of seeks needed to restore evicted78* page. Bigger values make ARC more precious and evictions smaller comparing79* 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.81*/82static int zfs_arc_shrinker_seeks = DEFAULT_SEEKS;8384#ifdef CONFIG_MEMORY_HOTPLUG85static struct notifier_block arc_hotplug_callback_mem_nb;86#endif8788/*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.91*/92uint64_t93arc_default_max(uint64_t min, uint64_t allmem)94{95uint64_t size;9697if (allmem >= 1 << 30)98size = allmem - (1 << 30);99else100size = min;101return (MAX(allmem * 5 / 8, size));102}103104/*105* Return maximum amount of memory that we could possibly use. Reduced106* to half of all memory in user space which is primarily used for testing.107*/108uint64_t109arc_all_memory(void)110{111#ifdef CONFIG_HIGHMEM112return (ptob(zfs_totalram_pages - zfs_totalhigh_pages));113#else114return (ptob(zfs_totalram_pages));115#endif /* CONFIG_HIGHMEM */116}117118/*119* Return the amount of memory that is considered free. In user space120* which is primarily used for testing we pretend that free memory ranges121* from 0-20% of all memory.122*/123uint64_t124arc_free_memory(void)125{126#ifdef CONFIG_HIGHMEM127struct sysinfo si;128si_meminfo(&si);129return (ptob(si.freeram - si.freehigh));130#else131return (ptob(nr_free_pages() +132nr_inactive_file_pages()));133#endif /* CONFIG_HIGHMEM */134}135136/*137* Return the amount of memory that can be consumed before reclaim will be138* needed. Positive if there is sufficient free memory, negative indicates139* the amount of memory that needs to be freed up.140*/141int64_t142arc_available_memory(void)143{144return (arc_free_memory() - arc_sys_free);145}146147static uint64_t148arc_evictable_memory(void)149{150int64_t asize = aggsum_value(&arc_sums.arcstat_size);151uint64_t arc_clean =152zfs_refcount_count(&arc_mru->arcs_esize[ARC_BUFC_DATA]) +153zfs_refcount_count(&arc_mru->arcs_esize[ARC_BUFC_METADATA]) +154zfs_refcount_count(&arc_mfu->arcs_esize[ARC_BUFC_DATA]) +155zfs_refcount_count(&arc_mfu->arcs_esize[ARC_BUFC_METADATA]);156uint64_t arc_dirty = MAX((int64_t)asize - (int64_t)arc_clean, 0);157158/*159* Scale reported evictable memory in proportion to page cache, cap160* at specified min/max.161*/162uint64_t min = (ptob(nr_file_pages()) / 100) * zfs_arc_pc_percent;163min = MAX(arc_c_min, MIN(arc_c_max, min));164165if (arc_dirty >= min)166return (arc_clean);167168return (MAX((int64_t)asize - (int64_t)min, 0));169}170171/*172* The _count() function returns the number of free-able objects.173* The _scan() function returns the number of objects that were freed.174*/175static unsigned long176arc_shrinker_count(struct shrinker *shrink, struct shrink_control *sc)177{178/*179* The kernel's shrinker code may not understand how many pages the180* ARC's callback actually frees, so it may ask the ARC to shrink a181* lot for one page allocation. This is problematic because it may182* take a long time, thus delaying the page allocation, and because183* it may force the ARC to unnecessarily shrink very small.184*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 for187* one page allocation attempt.188*189* In practice, we may be asked to shrink 4x the limit to satisfy one190* 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 pages192* that we freed before invoking the OOM killer. This happens in193* __alloc_pages_slowpath(), which retries and finds the pages we194* freed when it calls get_page_from_freelist().195*196* See also the comment above zfs_arc_shrinker_limit.197*/198int64_t can_free = btop(arc_evictable_memory());199if (current_is_kswapd() && zfs_arc_shrinker_limit)200can_free = MIN(can_free, zfs_arc_shrinker_limit);201return (can_free);202}203204static unsigned long205arc_shrinker_scan(struct shrinker *shrink, struct shrink_control *sc)206{207/* The arc is considered warm once reclaim has occurred */208if (unlikely(arc_warm == B_FALSE))209arc_warm = B_TRUE;210211/*212* We are experiencing memory pressure which the arc_evict_zthr was213* unable to keep up with. Set arc_no_grow to briefly pause ARC214* growth to avoid compounding the memory pressure.215*/216arc_no_grow = B_TRUE;217218/*219* Evict the requested number of pages by reducing arc_c and waiting220* for the requested amount of data to be evicted. To avoid deadlock221* do not wait for eviction if we may be called from ZFS itself (see222* kmem_flags_convert() removing __GFP_FS). It may cause excessive223* eviction later if many evictions are accumulated, but just skipping224* the eviction is not good either if most of memory is used by ARC.225*/226uint64_t to_free = arc_reduce_target_size(ptob(sc->nr_to_scan));227if (sc->gfp_mask & __GFP_FS)228arc_wait_for_eviction(to_free, B_FALSE, B_FALSE);229if (current->reclaim_state != NULL)230#ifdef HAVE_RECLAIM_STATE_RECLAIMED231current->reclaim_state->reclaimed += btop(to_free);232#else233current->reclaim_state->reclaimed_slab += btop(to_free);234#endif235236/*237* When direct reclaim is observed it usually indicates a rapid238* increase in memory pressure. This occurs because the kswapd239* threads were unable to asynchronously keep enough free memory240* available.241*/242if (current_is_kswapd()) {243ARCSTAT_BUMP(arcstat_memory_indirect_count);244} else {245ARCSTAT_BUMP(arcstat_memory_direct_count);246}247248return (btop(to_free));249}250251static struct shrinker *arc_shrinker = NULL;252253int254arc_memory_throttle(spa_t *spa, uint64_t reserve, uint64_t txg)255{256uint64_t free_memory = arc_free_memory();257258if (free_memory > arc_all_memory() * arc_lotsfree_percent / 100)259return (0);260261if (txg > spa->spa_lowmem_last_txg) {262spa->spa_lowmem_last_txg = txg;263spa->spa_lowmem_page_load = 0;264}265/*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 to268* continue to let page writes occur as quickly as possible.269*/270if (current_is_kswapd()) {271if (spa->spa_lowmem_page_load >272MAX(arc_sys_free / 4, free_memory) / 4) {273DMU_TX_STAT_BUMP(dmu_tx_memory_reclaim);274return (SET_ERROR(ERESTART));275}276/* Note: reserve is inflated, so we deflate */277atomic_add_64(&spa->spa_lowmem_page_load, reserve / 8);278return (0);279} else if (spa->spa_lowmem_page_load > 0 && arc_reclaim_needed()) {280/* memory is low, delay before restarting */281ARCSTAT_INCR(arcstat_memory_throttle_count, 1);282DMU_TX_STAT_BUMP(dmu_tx_memory_reclaim);283return (SET_ERROR(EAGAIN));284}285spa->spa_lowmem_page_load = 0;286return (0);287}288289static void290arc_set_sys_free(uint64_t allmem)291{292/*293* The ARC tries to keep at least this much memory available for the294* system. This gives the ARC time to shrink in response to memory295* pressure, before running completely out of memory and invoking the296* direct-reclaim ARC shrinker.297*298* This should be more than twice high_wmark_pages(), so that299* arc_wait_for_eviction() will wait until at least the300* high_wmark_pages() are free (see arc_evict_state_impl()).301*302* Note: If concurrent allocations consume these pages, there may303* still be insufficient free pages, and the OOM killer takes action.304*305* By setting arc_sys_free large enough, and having306* arc_wait_for_eviction() wait until there is at least arc_sys_free/2307* free memory, it is much less likely that concurrent allocations can308* consume all the memory that was evicted before checking for309* OOM.310*311* It's hard to iterate the zones from a linux kernel module, which312* makes it difficult to determine the watermark dynamically. Instead313* we compute the maximum high watermark for this system, based314* on the amount of memory, using the same method as the kernel uses315* to calculate its internal `min_free_kbytes` variable. See316* torvalds/linux@ee8eb9a5fe86 for the change in the upper clamp value317* from 64M to 256M.318*/319320/*321* Base wmark_low is 4 * the square root of Kbytes of RAM.322*/323long wmark = int_sqrt(allmem / 1024 * 16) * 1024;324325/*326* Clamp to between 128K and 256/64MB.327*/328wmark = MAX(wmark, 128 * 1024);329#if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 7, 0)330wmark = MIN(wmark, 256 * 1024 * 1024);331#else332wmark = MIN(wmark, 64 * 1024 * 1024);333#endif334335/*336* watermark_boost can increase the wmark by up to 150%.337*/338wmark += wmark * 150 / 100;339340/*341* arc_sys_free needs to be more than 2x the watermark, because342* arc_wait_for_eviction() waits for half of arc_sys_free. Bump this up343* to 3x to ensure we're above it.344*/345arc_sys_free = wmark * 3 + allmem / 32;346}347348void349arc_lowmem_init(void)350{351uint64_t allmem = arc_all_memory();352353/*354* Register a shrinker to support synchronous (direct) memory355* reclaim from the arc. This is done to prevent kswapd from356* swapping out pages when it is preferable to shrink the arc.357*/358arc_shrinker = spl_register_shrinker("zfs-arc-shrinker",359arc_shrinker_count, arc_shrinker_scan, zfs_arc_shrinker_seeks);360VERIFY(arc_shrinker);361362arc_set_sys_free(allmem);363}364365void366arc_lowmem_fini(void)367{368spl_unregister_shrinker(arc_shrinker);369arc_shrinker = NULL;370}371372int373param_set_arc_u64(const char *buf, zfs_kernel_param_t *kp)374{375int error;376377error = spl_param_set_u64(buf, kp);378if (error < 0)379return (SET_ERROR(error));380381arc_tuning_update(B_TRUE);382383return (0);384}385386int387param_set_arc_min(const char *buf, zfs_kernel_param_t *kp)388{389return (param_set_arc_u64(buf, kp));390}391392int393param_set_arc_max(const char *buf, zfs_kernel_param_t *kp)394{395return (param_set_arc_u64(buf, kp));396}397398int399param_set_arc_int(const char *buf, zfs_kernel_param_t *kp)400{401int error;402403error = param_set_int(buf, kp);404if (error < 0)405return (SET_ERROR(error));406407arc_tuning_update(B_TRUE);408409return (0);410}411412#ifdef CONFIG_MEMORY_HOTPLUG413static int414arc_hotplug_callback(struct notifier_block *self, unsigned long action,415void *arg)416{417(void) self, (void) arg;418uint64_t allmem = arc_all_memory();419if (action != MEM_ONLINE)420return (NOTIFY_OK);421422arc_set_limits(allmem);423424#ifdef __LP64__425if (zfs_dirty_data_max_max == 0)426zfs_dirty_data_max_max = MIN(4ULL * 1024 * 1024 * 1024,427allmem * zfs_dirty_data_max_max_percent / 100);428#else429if (zfs_dirty_data_max_max == 0)430zfs_dirty_data_max_max = MIN(1ULL * 1024 * 1024 * 1024,431allmem * zfs_dirty_data_max_max_percent / 100);432#endif433434arc_set_sys_free(allmem);435return (NOTIFY_OK);436}437#endif438439void440arc_register_hotplug(void)441{442#ifdef CONFIG_MEMORY_HOTPLUG443arc_hotplug_callback_mem_nb.notifier_call = arc_hotplug_callback;444/* There is no significance to the value 100 */445arc_hotplug_callback_mem_nb.priority = 100;446register_memory_notifier(&arc_hotplug_callback_mem_nb);447#endif448}449450void451arc_unregister_hotplug(void)452{453#ifdef CONFIG_MEMORY_HOTPLUG454unregister_memory_notifier(&arc_hotplug_callback_mem_nb);455#endif456}457458ZFS_MODULE_PARAM(zfs_arc, zfs_arc_, shrinker_limit, INT, ZMOD_RW,459"Limit on number of pages that ARC shrinker can reclaim at once");460ZFS_MODULE_PARAM(zfs_arc, zfs_arc_, shrinker_seeks, INT, ZMOD_RD,461"Relative cost of ARC eviction vs other kernel subsystems");462463464