https://source.android.com/docs/security/bulletin/2024-02-01 * tag 'ASB-2024-02-05_4.19-stable' of https://android.googlesource.com/kernel/common: Reapply "perf: Fix perf_event_validate_size()" UPSTREAM: usb: raw-gadget: properly handle interrupted requests UPSTREAM: mm/cma: use nth_page() in place of direct struct page manipulation UPSTREAM: wireguard: allowedips: expand maximum node depth UPSTREAM: coresight: tmc: Explicit type conversions to prevent integer overflow UPSTREAM: wireguard: netlink: send staged packets when setting initial private key UPSTREAM: wireguard: queueing: use saner cpu selection wrapping UPSTREAM: kheaders: Use array declaration instead of char UPSTREAM: arm64: efi: Make efi_rt_lock a raw_spinlock UPSTREAM: sched/psi: Fix use-after-free in ep_remove_wait_queue() UPSTREAM: usb: musb: mediatek: don't unregister something that wasn't registered UPSTREAM: xfrm/compat: prevent potential spectre v1 gadget in xfrm_xlate32_attr() UPSTREAM: xfrm: compat: change expression for switch in xfrm_xlate64 UPSTREAM: perf/core: Call LSM hook after copying perf_event_attr Linux 4.19.306 crypto: scompress - initialize per-CPU variables on each CPU Revert "NFSD: Fix possible sleep during nfsd4_release_lockowner()" i2c: s3c24xx: fix transferring more than one message in polling mode i2c: s3c24xx: fix read transfers in polling mode kdb: Fix a potential buffer overflow in kdb_local() kdb: Censor attempts to set PROMPT without ENABLE_MEM_READ ipvs: avoid stat macros calls from preemptible context net: dsa: vsc73xx: Add null pointer check to vsc73xx_gpio_probe net: ravb: Fix dma_addr_t truncation in error case net: qualcomm: rmnet: fix global oob in rmnet_policy serial: imx: Correct clock error message in function probe() apparmor: avoid crash when parsed profile name is empty perf genelf: Set ELF program header addresses properly acpi: property: Let args be NULL in __acpi_node_get_property_reference MIPS: Alchemy: Fix an out-of-bound access in db1550_dev_setup() MIPS: Alchemy: Fix an out-of-bound access in db1200_dev_setup() HID: wacom: Correct behavior when processing some confidence == false touches wifi: mwifiex: configure BSSID consistently when starting AP wifi: rtlwifi: Convert LNKCTL change to PCIe cap RMW accessors wifi: rtlwifi: Remove bogus and dangerous ASPM disable/enable code fbdev: flush deferred work in fb_deferred_io_fsync() ALSA: oxygen: Fix right channel of capture volume mixer usb: mon: Fix atomicity violation in mon_bin_vma_fault usb: typec: class: fix typec_altmode_put_partner to put plugs Revert "usb: typec: class: fix typec_altmode_put_partner to put plugs" usb: chipidea: wait controller resume finished for wakeup irq Revert "usb: dwc3: don't reset device side if dwc3 was configured as host-only" Revert "usb: dwc3: Soft reset phy on probe for host" usb: dwc: ep0: Update request status in dwc3_ep0_stall_restart usb: phy: mxs: remove CONFIG_USB_OTG condition for mxs_phy_is_otg_host() tick-sched: Fix idle and iowait sleeptime accounting vs CPU hotplug binder: fix unused alloc->free_async_space binder: fix race between mmput() and do_exit() xen-netback: don't produce zero-size SKB frags Revert "ASoC: atmel: Remove system clock tree configuration for at91sam9g20ek" Input: atkbd - use ab83 as id when skipping the getid command binder: fix async space check for 0-sized buffers of: unittest: Fix of_count_phandle_with_args() expected value message of: Fix double free in of_parse_phandle_with_args_map mmc: sdhci_omap: Fix TI SoC dependencies watchdog: bcm2835_wdt: Fix WDIOC_SETTIMEOUT handling watchdog/hpwdt: Only claim UNKNOWN NMI if from iLO watchdog: set cdev owner before adding gpu/drm/radeon: fix two memleaks in radeon_vm_init drivers/amd/pm: fix a use-after-free in kv_parse_power_table drm/amd/pm: fix a double-free in si_dpm_init drm/amdgpu/debugfs: fix error code when smc register accessors are NULL media: dvbdev: drop refcount on error path in dvb_device_open() media: cx231xx: fix a memleak in cx231xx_init_isoc drm/radeon/trinity_dpm: fix a memleak in trinity_parse_power_table drm/radeon/dpm: fix a memleak in sumo_parse_power_table drm/radeon: check the alloc_workqueue return value in radeon_crtc_init() drm/drv: propagate errors from drm_modeset_register_all() drm/msm/mdp4: flush vblank event on disable ASoC: cs35l34: Fix GPIO name and drop legacy include ASoC: cs35l33: Fix GPIO name and drop legacy include drm/radeon: check return value of radeon_ring_lock() drm/radeon/r100: Fix integer overflow issues in r100_cs_track_check() drm/radeon/r600_cs: Fix possible int overflows in r600_cs_check_reg() f2fs: fix to avoid dirent corruption drm/bridge: Fix typo in post_disable() description media: pvrusb2: fix use after free on context disconnection RDMA/usnic: Silence uninitialized symbol smatch warnings ip6_tunnel: fix NEXTHDR_FRAGMENT handling in ip6_tnl_parse_tlv_enc_lim() Bluetooth: btmtkuart: fix recv_buf() return value Bluetooth: Fix bogus check for re-auth no supported with non-ssp wifi: rtlwifi: rtl8192se: using calculate_bit_shift() wifi: rtlwifi: rtl8192ee: using calculate_bit_shift() wifi: rtlwifi: rtl8192de: using calculate_bit_shift() rtlwifi: rtl8192de: make arrays static const, makes object smaller wifi: rtlwifi: rtl8192ce: using calculate_bit_shift() wifi: rtlwifi: rtl8192cu: using calculate_bit_shift() wifi: rtlwifi: rtl8192c: using calculate_bit_shift() wifi: rtlwifi: rtl8188ee: phy: using calculate_bit_shift() wifi: rtlwifi: add calculate_bit_shift() dma-mapping: clear dev->dma_mem to NULL after freeing it scsi: hisi_sas: Replace with standard error code return value wifi: rtlwifi: rtl8821ae: phy: fix an undefined bitwise shift behavior rtlwifi: Use ffs in <foo>_phy_calculate_bit_shift firmware: ti_sci: Fix an off-by-one in ti_sci_debugfs_create() net/ncsi: Fix netlink major/minor version numbers ncsi: internal.h: Fix a spello ARM: dts: qcom: apq8064: correct XOADC register address wifi: libertas: stop selecting wext bpf, lpm: Fix check prefixlen before walking trie NFSv4.1/pnfs: Ensure we handle the error NFS4ERR_RETURNCONFLICT blocklayoutdriver: Fix reference leak of pnfs_device_node crypto: scomp - fix req->dst buffer overflow crypto: scompress - Use per-CPU struct instead multiple variables crypto: scompress - return proper error code for allocation failure crypto: sahara - do not resize req->src when doing hash operations crypto: sahara - fix processing hash requests with req->nbytes < sg->length crypto: sahara - improve error handling in sahara_sha_process() crypto: sahara - fix wait_for_completion_timeout() error handling crypto: sahara - fix ahash reqsize crypto: virtio - Wait for tasklet to complete on device remove pstore: ram_core: fix possible overflow in persistent_ram_init_ecc() crypto: sahara - fix error handling in sahara_hw_descriptor_create() crypto: sahara - fix processing requests with cryptlen < sg->length crypto: sahara - fix ahash selftest failure crypto: sahara - remove FLAGS_NEW_KEY logic crypto: af_alg - Disallow multiple in-flight AIO requests crypto: ccp - fix memleak in ccp_init_dm_workarea crypto: virtio - Handle dataq logic with tasklet selinux: Fix error priority for bind with AF_UNSPEC on PF_INET6 socket mtd: Fix gluebi NULL pointer dereference caused by ftl notifier calipso: fix memory leak in netlbl_calipso_add_pass() netlabel: remove unused parameter in netlbl_netlink_auditinfo() net: netlabel: Fix kerneldoc warnings ACPI: LPIT: Avoid u32 multiplication overflow ACPI: video: check for error while searching for backlight device parent mtd: rawnand: Increment IFC_TIMEOUT_MSECS for nand controller response powerpc/imc-pmu: Add a null pointer check in update_events_in_group() powerpc/powernv: Add a null pointer check in opal_event_init() selftests/powerpc: Fix error handling in FPU/VMX preemption tests powerpc/pseries/memhp: Fix access beyond end of drmem array powerpc/pseries/memhotplug: Quieten some DLPAR operations powerpc/44x: select I2C for CURRITUCK powerpc: remove redundant 'default n' from Kconfig-s powerpc: add crtsavres.o to always-y instead of extra-y EDAC/thunderx: Fix possible out-of-bounds string access x86/lib: Fix overflow when counting digits coresight: etm4x: Fix width of CCITMIN field uio: Fix use-after-free in uio_open binder: fix comment on binder_alloc_new_buf() return value binder: use EPOLLERR from eventpoll.h drm/crtc: fix uninitialized variable use ARM: sun9i: smp: fix return code check of of_property_match_string Input: xpad - add Razer Wolverine V2 support ARC: fix spare error s390/scm: fix virtual vs physical address confusion Input: i8042 - add nomux quirk for Acer P459-G2-M Input: atkbd - skip ATKBD_CMD_GETID in translated mode reset: hisilicon: hi6220: fix Wvoid-pointer-to-enum-cast warning ring-buffer: Do not record in NMI if the arch does not support cmpxchg in NMI tracing: Add size check when printing trace_marker output tracing: Have large events show up as '[LINE TOO BIG]' instead of nothing drm/crtc: Fix uninit-value bug in drm_mode_setcrtc jbd2: correct the printing of write_flags in jbd2_write_superblock() clk: rockchip: rk3128: Fix HCLK_OTG gate register drm/exynos: fix a potential error pointer dereference ASoC: da7219: Support low DC impedance headset net/tg3: fix race condition in tg3_reset_task() ASoC: rt5650: add mutex to avoid the jack detection failure ASoC: cs43130: Fix incorrect frame delay configuration ASoC: cs43130: Fix the position of const qualifier ASoC: Intel: Skylake: mem leak in skl register function f2fs: explicitly null-terminate the xattr list UPSTREAM: wifi: cfg80211: fix buffer overflow in elem comparison UPSTREAM: gcov: clang: fix the buffer overflow issue BACKPORT: selinux: enable use of both GFP_KERNEL and GFP_ATOMIC in convert_context() UPSTREAM: wifi: cfg80211: avoid nontransmitted BSS list corruption UPSTREAM: wifi: cfg80211: fix BSS refcounting bugs UPSTREAM: wifi: cfg80211: ensure length byte is present before access UPSTREAM: wifi: cfg80211: fix u8 overflow in cfg80211_update_notlisted_nontrans() UPSTREAM: wireguard: netlink: avoid variable-sized memcpy on sockaddr UPSTREAM: wireguard: ratelimiter: disable timings test by default UPSTREAM: crypto: lib - remove unneeded selection of XOR_BLOCKS UPSTREAM: wireguard: allowedips: don't corrupt stack when detecting overflow UPSTREAM: wireguard: ratelimiter: use hrtimer in selftest UPSTREAM: crypto: arm64/poly1305 - fix a read out-of-bound UPSTREAM: wifi: mac80211_hwsim: set virtio device ready in probe() UPSTREAM: crypto: memneq - move into lib/ UPSTREAM: dma-buf: fix use of DMA_BUF_SET_NAME_{A,B} in userspace BACKPORT: usb: typec: mux: Check dev_set_name() return value UPSTREAM: wireguard: device: check for metadata_dst with skb_valid_dst() UPSTREAM: sched/fair: Fix cfs_rq_clock_pelt() for throttled cfs_rq UPSTREAM: cfg80211: hold bss_lock while updating nontrans_list UPSTREAM: wireguard: socket: ignore v6 endpoints when ipv6 is disabled UPSTREAM: wireguard: socket: free skb in send6 when ipv6 is disabled UPSTREAM: wireguard: queueing: use CFI-safe ptr_ring cleanup function UPSTREAM: mm: don't try to NUMA-migrate COW pages that have other uses UPSTREAM: copy_process(): Move fd_install() out of sighand->siglock critical section UPSTREAM: usb: raw-gadget: fix handling of dual-direction-capable endpoints UPSTREAM: psi: Fix "no previous prototype" warnings when CONFIG_CGROUPS=n UPSTREAM: sched/uclamp: Fix rq->uclamp_max not set on first enqueue UPSTREAM: wireguard: selftests: increase default dmesg log size UPSTREAM: wireguard: allowedips: add missing __rcu annotation to satisfy sparse UPSTREAM: sched/uclamp: Fix uclamp_tg_restrict() UPSTREAM: coresight: etm4x: Fix etm4_count race by moving cpuhp callbacks to init UPSTREAM: sched/uclamp: Fix a deadlock when enabling uclamp static key UPSTREAM: mac80211_hwsim: use GFP_ATOMIC under spin lock UPSTREAM: usercopy: Avoid soft lockups in test_check_nonzero_user() UPSTREAM: lib: test_user_copy: style cleanup UPSTREAM: fork: return proper negative error code Revert "ipv6: make ip6_rt_gc_expire an atomic_t" Revert "ipv6: remove max_size check inline with ipv4" Linux 4.19.305 ipv6: remove max_size check inline with ipv4 ipv6: make ip6_rt_gc_expire an atomic_t net/dst: use a smaller percpu_counter batch for dst entries accounting net: add a route cache full diagnostic message PCI: Disable ATS for specific Intel IPU E2000 devices PCI: Extract ATS disabling to a helper function netfilter: nf_tables: Reject tables of unsupported family fuse: nlookup missing decrement in fuse_direntplus_link mmc: core: Cancel delayed work before releasing host mmc: rpmb: fixes pause retune on all RPMB partitions. mm: fix unmap_mapping_range high bits shift bug firewire: ohci: suppress unexpected system reboot in AMD Ryzen machines and ASM108x/VT630x PCIe cards mm/memory-failure: check the mapcount of the precise page bnxt_en: Remove mis-applied code from bnxt_cfg_ntp_filters() asix: Add check for usbnet_get_endpoints net/qla3xxx: fix potential memleak in ql_alloc_buffer_queues net/qla3xxx: switch from 'pci_' to 'dma_' API i40e: Restore VF MSI-X state during PCI reset i40e: fix use-after-free in i40e_aqc_add_filters() net: Save and restore msg_namelen in sock_sendmsg net: bcmgenet: Fix FCS generation for fragmented skbuffs ARM: sun9i: smp: Fix array-index-out-of-bounds read in sunxi_mc_smp_init net: sched: em_text: fix possible memory leak in em_text_destroy() i40e: Fix filter input checks to prevent config with invalid values nfc: llcp_core: Hold a ref to llcp_local->dev when holding a ref to llcp_local UPSTREAM: fsverity: skip PKCS#7 parser when keyring is empty Conflicts: drivers/hwtracing/coresight/coresight-etm4x.c drivers/net/ethernet/qualcomm/rmnet/rmnet_config.c include/linux/psi.h mm/memory-failure.c net/wireless/scan.c Change-Id: I49b769cb8387e5d5f28730d13cbdf4ffd335dc70
1410 lines
38 KiB
C
1410 lines
38 KiB
C
/*
|
|
* Pressure stall information for CPU, memory and IO
|
|
*
|
|
* Copyright (c) 2018 Facebook, Inc.
|
|
* Author: Johannes Weiner <hannes@cmpxchg.org>
|
|
*
|
|
* Polling support by Suren Baghdasaryan <surenb@google.com>
|
|
* Copyright (c) 2018 Google, Inc.
|
|
*
|
|
* When CPU, memory and IO are contended, tasks experience delays that
|
|
* reduce throughput and introduce latencies into the workload. Memory
|
|
* and IO contention, in addition, can cause a full loss of forward
|
|
* progress in which the CPU goes idle.
|
|
*
|
|
* This code aggregates individual task delays into resource pressure
|
|
* metrics that indicate problems with both workload health and
|
|
* resource utilization.
|
|
*
|
|
* Model
|
|
*
|
|
* The time in which a task can execute on a CPU is our baseline for
|
|
* productivity. Pressure expresses the amount of time in which this
|
|
* potential cannot be realized due to resource contention.
|
|
*
|
|
* This concept of productivity has two components: the workload and
|
|
* the CPU. To measure the impact of pressure on both, we define two
|
|
* contention states for a resource: SOME and FULL.
|
|
*
|
|
* In the SOME state of a given resource, one or more tasks are
|
|
* delayed on that resource. This affects the workload's ability to
|
|
* perform work, but the CPU may still be executing other tasks.
|
|
*
|
|
* In the FULL state of a given resource, all non-idle tasks are
|
|
* delayed on that resource such that nobody is advancing and the CPU
|
|
* goes idle. This leaves both workload and CPU unproductive.
|
|
*
|
|
* (Naturally, the FULL state doesn't exist for the CPU resource.)
|
|
*
|
|
* SOME = nr_delayed_tasks != 0
|
|
* FULL = nr_delayed_tasks != 0 && nr_running_tasks == 0
|
|
*
|
|
* The percentage of wallclock time spent in those compound stall
|
|
* states gives pressure numbers between 0 and 100 for each resource,
|
|
* where the SOME percentage indicates workload slowdowns and the FULL
|
|
* percentage indicates reduced CPU utilization:
|
|
*
|
|
* %SOME = time(SOME) / period
|
|
* %FULL = time(FULL) / period
|
|
*
|
|
* Multiple CPUs
|
|
*
|
|
* The more tasks and available CPUs there are, the more work can be
|
|
* performed concurrently. This means that the potential that can go
|
|
* unrealized due to resource contention *also* scales with non-idle
|
|
* tasks and CPUs.
|
|
*
|
|
* Consider a scenario where 257 number crunching tasks are trying to
|
|
* run concurrently on 256 CPUs. If we simply aggregated the task
|
|
* states, we would have to conclude a CPU SOME pressure number of
|
|
* 100%, since *somebody* is waiting on a runqueue at all
|
|
* times. However, that is clearly not the amount of contention the
|
|
* workload is experiencing: only one out of 256 possible exceution
|
|
* threads will be contended at any given time, or about 0.4%.
|
|
*
|
|
* Conversely, consider a scenario of 4 tasks and 4 CPUs where at any
|
|
* given time *one* of the tasks is delayed due to a lack of memory.
|
|
* Again, looking purely at the task state would yield a memory FULL
|
|
* pressure number of 0%, since *somebody* is always making forward
|
|
* progress. But again this wouldn't capture the amount of execution
|
|
* potential lost, which is 1 out of 4 CPUs, or 25%.
|
|
*
|
|
* To calculate wasted potential (pressure) with multiple processors,
|
|
* we have to base our calculation on the number of non-idle tasks in
|
|
* conjunction with the number of available CPUs, which is the number
|
|
* of potential execution threads. SOME becomes then the proportion of
|
|
* delayed tasks to possibe threads, and FULL is the share of possible
|
|
* threads that are unproductive due to delays:
|
|
*
|
|
* threads = min(nr_nonidle_tasks, nr_cpus)
|
|
* SOME = min(nr_delayed_tasks / threads, 1)
|
|
* FULL = (threads - min(nr_running_tasks, threads)) / threads
|
|
*
|
|
* For the 257 number crunchers on 256 CPUs, this yields:
|
|
*
|
|
* threads = min(257, 256)
|
|
* SOME = min(1 / 256, 1) = 0.4%
|
|
* FULL = (256 - min(257, 256)) / 256 = 0%
|
|
*
|
|
* For the 1 out of 4 memory-delayed tasks, this yields:
|
|
*
|
|
* threads = min(4, 4)
|
|
* SOME = min(1 / 4, 1) = 25%
|
|
* FULL = (4 - min(3, 4)) / 4 = 25%
|
|
*
|
|
* [ Substitute nr_cpus with 1, and you can see that it's a natural
|
|
* extension of the single-CPU model. ]
|
|
*
|
|
* Implementation
|
|
*
|
|
* To assess the precise time spent in each such state, we would have
|
|
* to freeze the system on task changes and start/stop the state
|
|
* clocks accordingly. Obviously that doesn't scale in practice.
|
|
*
|
|
* Because the scheduler aims to distribute the compute load evenly
|
|
* among the available CPUs, we can track task state locally to each
|
|
* CPU and, at much lower frequency, extrapolate the global state for
|
|
* the cumulative stall times and the running averages.
|
|
*
|
|
* For each runqueue, we track:
|
|
*
|
|
* tSOME[cpu] = time(nr_delayed_tasks[cpu] != 0)
|
|
* tFULL[cpu] = time(nr_delayed_tasks[cpu] && !nr_running_tasks[cpu])
|
|
* tNONIDLE[cpu] = time(nr_nonidle_tasks[cpu] != 0)
|
|
*
|
|
* and then periodically aggregate:
|
|
*
|
|
* tNONIDLE = sum(tNONIDLE[i])
|
|
*
|
|
* tSOME = sum(tSOME[i] * tNONIDLE[i]) / tNONIDLE
|
|
* tFULL = sum(tFULL[i] * tNONIDLE[i]) / tNONIDLE
|
|
*
|
|
* %SOME = tSOME / period
|
|
* %FULL = tFULL / period
|
|
*
|
|
* This gives us an approximation of pressure that is practical
|
|
* cost-wise, yet way more sensitive and accurate than periodic
|
|
* sampling of the aggregate task states would be.
|
|
*/
|
|
|
|
#include "../workqueue_internal.h"
|
|
#include <linux/sched/loadavg.h>
|
|
#include <linux/seq_file.h>
|
|
#include <linux/proc_fs.h>
|
|
#include <linux/seqlock.h>
|
|
#include <linux/uaccess.h>
|
|
#include <linux/cgroup.h>
|
|
#include <linux/module.h>
|
|
#include <linux/sched.h>
|
|
#include <linux/ctype.h>
|
|
#include <linux/file.h>
|
|
#include <linux/poll.h>
|
|
#include <linux/psi.h>
|
|
#include <linux/oom.h>
|
|
#include "sched.h"
|
|
|
|
#define CREATE_TRACE_POINTS
|
|
#include <trace/events/psi.h>
|
|
|
|
static int psi_bug __read_mostly;
|
|
|
|
DEFINE_STATIC_KEY_FALSE(psi_disabled);
|
|
DEFINE_STATIC_KEY_TRUE(psi_cgroups_enabled);
|
|
|
|
#ifdef CONFIG_PSI_DEFAULT_DISABLED
|
|
static bool psi_enable;
|
|
#else
|
|
static bool psi_enable = true;
|
|
#endif
|
|
static int __init setup_psi(char *str)
|
|
{
|
|
return kstrtobool(str, &psi_enable) == 0;
|
|
}
|
|
__setup("psi=", setup_psi);
|
|
|
|
/* Running averages - we need to be higher-res than loadavg */
|
|
#define PSI_FREQ (2*HZ+1) /* 2 sec intervals */
|
|
#define EXP_10s 1677 /* 1/exp(2s/10s) as fixed-point */
|
|
#define EXP_60s 1981 /* 1/exp(2s/60s) */
|
|
#define EXP_300s 2034 /* 1/exp(2s/300s) */
|
|
|
|
/* PSI trigger definitions */
|
|
#define WINDOW_MIN_US 500000 /* Min window size is 500ms */
|
|
#define WINDOW_MAX_US 10000000 /* Max window size is 10s */
|
|
#define UPDATES_PER_WINDOW 10 /* 10 updates per window */
|
|
|
|
/* Sampling frequency in nanoseconds */
|
|
static u64 psi_period __read_mostly;
|
|
|
|
/* System-level pressure and stall tracking */
|
|
static DEFINE_PER_CPU(struct psi_group_cpu, system_group_pcpu);
|
|
static struct psi_group psi_system = {
|
|
.pcpu = &system_group_pcpu,
|
|
};
|
|
|
|
static void psi_avgs_work(struct work_struct *work);
|
|
|
|
static void group_init(struct psi_group *group)
|
|
{
|
|
int cpu;
|
|
|
|
for_each_possible_cpu(cpu)
|
|
seqcount_init(&per_cpu_ptr(group->pcpu, cpu)->seq);
|
|
group->avg_last_update = sched_clock();
|
|
group->avg_next_update = group->avg_last_update + psi_period;
|
|
INIT_DEFERRABLE_WORK(&group->avgs_work, psi_avgs_work);
|
|
mutex_init(&group->avgs_lock);
|
|
/* Init trigger-related members */
|
|
atomic_set(&group->poll_scheduled, 0);
|
|
mutex_init(&group->trigger_lock);
|
|
INIT_LIST_HEAD(&group->triggers);
|
|
memset(group->nr_triggers, 0, sizeof(group->nr_triggers));
|
|
group->poll_states = 0;
|
|
group->poll_min_period = U32_MAX;
|
|
memset(group->polling_total, 0, sizeof(group->polling_total));
|
|
group->polling_next_update = ULLONG_MAX;
|
|
group->polling_until = 0;
|
|
rcu_assign_pointer(group->poll_kworker, NULL);
|
|
}
|
|
|
|
void __init psi_init(void)
|
|
{
|
|
if (!psi_enable) {
|
|
static_branch_enable(&psi_disabled);
|
|
return;
|
|
}
|
|
|
|
if (!cgroup_psi_enabled())
|
|
static_branch_disable(&psi_cgroups_enabled);
|
|
|
|
psi_period = jiffies_to_nsecs(PSI_FREQ);
|
|
group_init(&psi_system);
|
|
}
|
|
|
|
static bool test_state(unsigned int *tasks, enum psi_states state)
|
|
{
|
|
switch (state) {
|
|
case PSI_IO_SOME:
|
|
return tasks[NR_IOWAIT];
|
|
case PSI_IO_FULL:
|
|
return tasks[NR_IOWAIT] && !tasks[NR_RUNNING];
|
|
case PSI_MEM_SOME:
|
|
return tasks[NR_MEMSTALL];
|
|
case PSI_MEM_FULL:
|
|
return tasks[NR_MEMSTALL] && !tasks[NR_RUNNING];
|
|
case PSI_CPU_SOME:
|
|
return tasks[NR_RUNNING] > 1;
|
|
case PSI_NONIDLE:
|
|
return tasks[NR_IOWAIT] || tasks[NR_MEMSTALL] ||
|
|
tasks[NR_RUNNING];
|
|
default:
|
|
return false;
|
|
}
|
|
}
|
|
|
|
static void get_recent_times(struct psi_group *group, int cpu,
|
|
enum psi_aggregators aggregator, u32 *times,
|
|
u32 *pchanged_states)
|
|
{
|
|
struct psi_group_cpu *groupc = per_cpu_ptr(group->pcpu, cpu);
|
|
u64 now, state_start;
|
|
enum psi_states s;
|
|
unsigned int seq;
|
|
u32 state_mask;
|
|
|
|
*pchanged_states = 0;
|
|
|
|
/* Snapshot a coherent view of the CPU state */
|
|
do {
|
|
seq = read_seqcount_begin(&groupc->seq);
|
|
now = cpu_clock(cpu);
|
|
memcpy(times, groupc->times, sizeof(groupc->times));
|
|
state_mask = groupc->state_mask;
|
|
state_start = groupc->state_start;
|
|
} while (read_seqcount_retry(&groupc->seq, seq));
|
|
|
|
/* Calculate state time deltas against the previous snapshot */
|
|
for (s = 0; s < NR_PSI_STATES; s++) {
|
|
u32 delta;
|
|
/*
|
|
* In addition to already concluded states, we also
|
|
* incorporate currently active states on the CPU,
|
|
* since states may last for many sampling periods.
|
|
*
|
|
* This way we keep our delta sampling buckets small
|
|
* (u32) and our reported pressure close to what's
|
|
* actually happening.
|
|
*/
|
|
if (state_mask & (1 << s))
|
|
times[s] += now - state_start;
|
|
|
|
delta = times[s] - groupc->times_prev[aggregator][s];
|
|
groupc->times_prev[aggregator][s] = times[s];
|
|
|
|
times[s] = delta;
|
|
if (delta)
|
|
*pchanged_states |= (1 << s);
|
|
}
|
|
}
|
|
|
|
static void calc_avgs(unsigned long avg[3], int missed_periods,
|
|
u64 time, u64 period)
|
|
{
|
|
unsigned long pct;
|
|
|
|
/* Fill in zeroes for periods of no activity */
|
|
if (missed_periods) {
|
|
avg[0] = calc_load_n(avg[0], EXP_10s, 0, missed_periods);
|
|
avg[1] = calc_load_n(avg[1], EXP_60s, 0, missed_periods);
|
|
avg[2] = calc_load_n(avg[2], EXP_300s, 0, missed_periods);
|
|
}
|
|
|
|
/* Sample the most recent active period */
|
|
pct = div_u64(time * 100, period);
|
|
pct *= FIXED_1;
|
|
avg[0] = calc_load(avg[0], EXP_10s, pct);
|
|
avg[1] = calc_load(avg[1], EXP_60s, pct);
|
|
avg[2] = calc_load(avg[2], EXP_300s, pct);
|
|
}
|
|
|
|
static void collect_percpu_times(struct psi_group *group,
|
|
enum psi_aggregators aggregator,
|
|
u32 *pchanged_states)
|
|
{
|
|
u64 deltas[NR_PSI_STATES - 1] = { 0, };
|
|
unsigned long nonidle_total = 0;
|
|
u32 changed_states = 0;
|
|
int cpu;
|
|
int s;
|
|
|
|
/*
|
|
* Collect the per-cpu time buckets and average them into a
|
|
* single time sample that is normalized to wallclock time.
|
|
*
|
|
* For averaging, each CPU is weighted by its non-idle time in
|
|
* the sampling period. This eliminates artifacts from uneven
|
|
* loading, or even entirely idle CPUs.
|
|
*/
|
|
for_each_possible_cpu(cpu) {
|
|
u32 times[NR_PSI_STATES];
|
|
u32 nonidle;
|
|
u32 cpu_changed_states;
|
|
|
|
get_recent_times(group, cpu, aggregator, times,
|
|
&cpu_changed_states);
|
|
changed_states |= cpu_changed_states;
|
|
|
|
nonidle = nsecs_to_jiffies(times[PSI_NONIDLE]);
|
|
nonidle_total += nonidle;
|
|
|
|
for (s = 0; s < PSI_NONIDLE; s++)
|
|
deltas[s] += (u64)times[s] * nonidle;
|
|
}
|
|
|
|
/*
|
|
* Integrate the sample into the running statistics that are
|
|
* reported to userspace: the cumulative stall times and the
|
|
* decaying averages.
|
|
*
|
|
* Pressure percentages are sampled at PSI_FREQ. We might be
|
|
* called more often when the user polls more frequently than
|
|
* that; we might be called less often when there is no task
|
|
* activity, thus no data, and clock ticks are sporadic. The
|
|
* below handles both.
|
|
*/
|
|
|
|
/* total= */
|
|
for (s = 0; s < NR_PSI_STATES - 1; s++)
|
|
group->total[aggregator][s] +=
|
|
div_u64(deltas[s], max(nonidle_total, 1UL));
|
|
|
|
if (pchanged_states)
|
|
*pchanged_states = changed_states;
|
|
}
|
|
|
|
static u64 update_averages(struct psi_group *group, u64 now)
|
|
{
|
|
unsigned long missed_periods = 0;
|
|
u64 expires, period;
|
|
u64 avg_next_update;
|
|
int s;
|
|
|
|
/* avgX= */
|
|
expires = group->avg_next_update;
|
|
if (now - expires >= psi_period)
|
|
missed_periods = div_u64(now - expires, psi_period);
|
|
|
|
/*
|
|
* The periodic clock tick can get delayed for various
|
|
* reasons, especially on loaded systems. To avoid clock
|
|
* drift, we schedule the clock in fixed psi_period intervals.
|
|
* But the deltas we sample out of the per-cpu buckets above
|
|
* are based on the actual time elapsing between clock ticks.
|
|
*/
|
|
avg_next_update = expires + ((1 + missed_periods) * psi_period);
|
|
period = now - (group->avg_last_update + (missed_periods * psi_period));
|
|
group->avg_last_update = now;
|
|
|
|
for (s = 0; s < NR_PSI_STATES - 1; s++) {
|
|
u32 sample;
|
|
|
|
sample = group->total[PSI_AVGS][s] - group->avg_total[s];
|
|
/*
|
|
* Due to the lockless sampling of the time buckets,
|
|
* recorded time deltas can slip into the next period,
|
|
* which under full pressure can result in samples in
|
|
* excess of the period length.
|
|
*
|
|
* We don't want to report non-sensical pressures in
|
|
* excess of 100%, nor do we want to drop such events
|
|
* on the floor. Instead we punt any overage into the
|
|
* future until pressure subsides. By doing this we
|
|
* don't underreport the occurring pressure curve, we
|
|
* just report it delayed by one period length.
|
|
*
|
|
* The error isn't cumulative. As soon as another
|
|
* delta slips from a period P to P+1, by definition
|
|
* it frees up its time T in P.
|
|
*/
|
|
if (sample > period)
|
|
sample = period;
|
|
group->avg_total[s] += sample;
|
|
calc_avgs(group->avg[s], missed_periods, sample, period);
|
|
}
|
|
|
|
return avg_next_update;
|
|
}
|
|
|
|
static void psi_avgs_work(struct work_struct *work)
|
|
{
|
|
struct delayed_work *dwork;
|
|
struct psi_group *group;
|
|
u32 changed_states;
|
|
bool nonidle;
|
|
u64 now;
|
|
|
|
dwork = to_delayed_work(work);
|
|
group = container_of(dwork, struct psi_group, avgs_work);
|
|
|
|
mutex_lock(&group->avgs_lock);
|
|
|
|
now = sched_clock();
|
|
|
|
collect_percpu_times(group, PSI_AVGS, &changed_states);
|
|
nonidle = changed_states & (1 << PSI_NONIDLE);
|
|
/*
|
|
* If there is task activity, periodically fold the per-cpu
|
|
* times and feed samples into the running averages. If things
|
|
* are idle and there is no data to process, stop the clock.
|
|
* Once restarted, we'll catch up the running averages in one
|
|
* go - see calc_avgs() and missed_periods.
|
|
*/
|
|
if (now >= group->avg_next_update)
|
|
group->avg_next_update = update_averages(group, now);
|
|
|
|
if (nonidle) {
|
|
schedule_delayed_work(dwork, nsecs_to_jiffies(
|
|
group->avg_next_update - now) + 1);
|
|
}
|
|
|
|
mutex_unlock(&group->avgs_lock);
|
|
}
|
|
|
|
#ifdef CONFIG_PSI_FTRACE
|
|
|
|
#define TOKB(x) ((x) * (PAGE_SIZE / 1024))
|
|
|
|
static void trace_event_helper(struct psi_group *group)
|
|
{
|
|
struct zone *zone;
|
|
unsigned long wmark;
|
|
unsigned long free;
|
|
unsigned long cma;
|
|
unsigned long file;
|
|
|
|
u64 mem_some_delta = group->total[PSI_POLL][PSI_MEM_SOME] -
|
|
group->polling_total[PSI_MEM_SOME];
|
|
u64 mem_full_delta = group->total[PSI_POLL][PSI_MEM_FULL] -
|
|
group->polling_total[PSI_MEM_FULL];
|
|
|
|
for_each_populated_zone(zone) {
|
|
wmark = TOKB(high_wmark_pages(zone));
|
|
free = TOKB(zone_page_state(zone, NR_FREE_PAGES));
|
|
cma = TOKB(zone_page_state(zone, NR_FREE_CMA_PAGES));
|
|
file = TOKB(zone_page_state(zone, NR_ZONE_ACTIVE_FILE) +
|
|
zone_page_state(zone, NR_ZONE_INACTIVE_FILE));
|
|
|
|
trace_psi_window_vmstat(
|
|
mem_some_delta, mem_full_delta, zone->name, wmark,
|
|
free, cma, file);
|
|
}
|
|
}
|
|
#else
|
|
static void trace_event_helper(struct psi_group *group)
|
|
{
|
|
}
|
|
#endif /* CONFIG_PSI_FTRACE */
|
|
|
|
/* Trigger tracking window manupulations */
|
|
static void window_reset(struct psi_window *win, u64 now, u64 value,
|
|
u64 prev_growth)
|
|
{
|
|
win->start_time = now;
|
|
win->start_value = value;
|
|
win->prev_growth = prev_growth;
|
|
}
|
|
|
|
/*
|
|
* PSI growth tracking window update and growth calculation routine.
|
|
*
|
|
* This approximates a sliding tracking window by interpolating
|
|
* partially elapsed windows using historical growth data from the
|
|
* previous intervals. This minimizes memory requirements (by not storing
|
|
* all the intermediate values in the previous window) and simplifies
|
|
* the calculations. It works well because PSI signal changes only in
|
|
* positive direction and over relatively small window sizes the growth
|
|
* is close to linear.
|
|
*/
|
|
static u64 window_update(struct psi_window *win, u64 now, u64 value)
|
|
{
|
|
u64 elapsed;
|
|
u64 growth;
|
|
|
|
elapsed = now - win->start_time;
|
|
growth = value - win->start_value;
|
|
/*
|
|
* After each tracking window passes win->start_value and
|
|
* win->start_time get reset and win->prev_growth stores
|
|
* the average per-window growth of the previous window.
|
|
* win->prev_growth is then used to interpolate additional
|
|
* growth from the previous window assuming it was linear.
|
|
*/
|
|
if (elapsed > win->size)
|
|
window_reset(win, now, value, growth);
|
|
else {
|
|
u32 remaining;
|
|
|
|
remaining = win->size - elapsed;
|
|
growth += div64_u64(win->prev_growth * remaining, win->size);
|
|
}
|
|
|
|
return growth;
|
|
}
|
|
|
|
static void init_triggers(struct psi_group *group, u64 now)
|
|
{
|
|
struct psi_trigger *t;
|
|
|
|
list_for_each_entry(t, &group->triggers, node)
|
|
window_reset(&t->win, now,
|
|
group->total[PSI_POLL][t->state], 0);
|
|
memcpy(group->polling_total, group->total[PSI_POLL],
|
|
sizeof(group->polling_total));
|
|
group->polling_next_update = now + group->poll_min_period;
|
|
}
|
|
|
|
static u64 update_triggers(struct psi_group *group, u64 now)
|
|
{
|
|
struct psi_trigger *t;
|
|
bool new_stall = false;
|
|
u64 *total = group->total[PSI_POLL];
|
|
|
|
/*
|
|
* On subsequent updates, calculate growth deltas and let
|
|
* watchers know when their specified thresholds are exceeded.
|
|
*/
|
|
list_for_each_entry(t, &group->triggers, node) {
|
|
u64 growth;
|
|
|
|
/* Check for stall activity */
|
|
if (group->polling_total[t->state] == total[t->state])
|
|
continue;
|
|
|
|
/*
|
|
* Multiple triggers might be looking at the same state,
|
|
* remember to update group->polling_total[] once we've
|
|
* been through all of them. Also remember to extend the
|
|
* polling time if we see new stall activity.
|
|
*/
|
|
new_stall = true;
|
|
|
|
/* Calculate growth since last update */
|
|
growth = window_update(&t->win, now, total[t->state]);
|
|
if (growth < t->threshold)
|
|
continue;
|
|
|
|
/* Limit event signaling to once per window */
|
|
if (now < t->last_event_time + t->win.size)
|
|
continue;
|
|
|
|
trace_psi_event(t->state, t->threshold);
|
|
|
|
/* Generate an event */
|
|
if (cmpxchg(&t->event, 0, 1) == 0) {
|
|
if (!strcmp(t->comm, ULMK_MAGIC))
|
|
mod_timer(&t->wdog_timer, jiffies +
|
|
nsecs_to_jiffies(2 * t->win.size));
|
|
wake_up_interruptible(&t->event_wait);
|
|
}
|
|
t->last_event_time = now;
|
|
}
|
|
|
|
trace_event_helper(group);
|
|
if (new_stall)
|
|
memcpy(group->polling_total, total,
|
|
sizeof(group->polling_total));
|
|
|
|
return now + group->poll_min_period;
|
|
}
|
|
|
|
/*
|
|
* Allows sending more than one event per window.
|
|
*/
|
|
void psi_emergency_trigger(void)
|
|
{
|
|
struct psi_group *group = &psi_system;
|
|
struct psi_trigger *t;
|
|
u64 now;
|
|
|
|
if (static_branch_likely(&psi_disabled))
|
|
return;
|
|
|
|
/*
|
|
* In unlikely case that OOM was triggered while adding/
|
|
* removing triggers.
|
|
*/
|
|
if (!mutex_trylock(&group->trigger_lock))
|
|
return;
|
|
|
|
now = sched_clock();
|
|
list_for_each_entry(t, &group->triggers, node) {
|
|
if (strcmp(t->comm, ULMK_MAGIC))
|
|
continue;
|
|
trace_psi_event(t->state, t->threshold);
|
|
|
|
/* Generate an event */
|
|
if (cmpxchg(&t->event, 0, 1) == 0) {
|
|
mod_timer(&t->wdog_timer, jiffies +
|
|
nsecs_to_jiffies(2 * t->win.size));
|
|
wake_up_interruptible(&t->event_wait);
|
|
}
|
|
t->last_event_time = now;
|
|
}
|
|
mutex_unlock(&group->trigger_lock);
|
|
}
|
|
|
|
/*
|
|
* Return true if any trigger is active.
|
|
*/
|
|
bool psi_is_trigger_active(void)
|
|
{
|
|
struct psi_group *group = &psi_system;
|
|
struct psi_trigger *t;
|
|
bool trigger_active = false;
|
|
u64 now;
|
|
|
|
if (static_branch_likely(&psi_disabled))
|
|
return false;
|
|
|
|
/*
|
|
* In unlikely case that OOM was triggered while adding/
|
|
* removing triggers.
|
|
*/
|
|
if (!mutex_trylock(&group->trigger_lock))
|
|
return true;
|
|
|
|
now = sched_clock();
|
|
list_for_each_entry(t, &group->triggers, node) {
|
|
if (strcmp(t->comm, ULMK_MAGIC))
|
|
continue;
|
|
|
|
if (now <= t->last_event_time + t->win.size)
|
|
trigger_active = true;
|
|
}
|
|
mutex_unlock(&group->trigger_lock);
|
|
return trigger_active;
|
|
}
|
|
|
|
/*
|
|
* Schedule polling if it's not already scheduled. It's safe to call even from
|
|
* hotpath because even though kthread_queue_delayed_work takes worker->lock
|
|
* spinlock that spinlock is never contended due to poll_scheduled atomic
|
|
* preventing such competition.
|
|
*/
|
|
static void psi_schedule_poll_work(struct psi_group *group, unsigned long delay)
|
|
{
|
|
struct kthread_worker *kworker;
|
|
|
|
/* Do not reschedule if already scheduled */
|
|
if (atomic_cmpxchg(&group->poll_scheduled, 0, 1) != 0)
|
|
return;
|
|
|
|
rcu_read_lock();
|
|
|
|
kworker = rcu_dereference(group->poll_kworker);
|
|
/*
|
|
* kworker might be NULL in case psi_trigger_destroy races with
|
|
* psi_task_change (hotpath) which can't use locks
|
|
*/
|
|
if (likely(kworker))
|
|
kthread_queue_delayed_work(kworker, &group->poll_work, delay);
|
|
else
|
|
atomic_set(&group->poll_scheduled, 0);
|
|
|
|
rcu_read_unlock();
|
|
}
|
|
|
|
static void psi_poll_work(struct kthread_work *work)
|
|
{
|
|
struct kthread_delayed_work *dwork;
|
|
struct psi_group *group;
|
|
u32 changed_states;
|
|
u64 now;
|
|
|
|
dwork = container_of(work, struct kthread_delayed_work, work);
|
|
group = container_of(dwork, struct psi_group, poll_work);
|
|
|
|
atomic_set(&group->poll_scheduled, 0);
|
|
|
|
mutex_lock(&group->trigger_lock);
|
|
|
|
now = sched_clock();
|
|
|
|
collect_percpu_times(group, PSI_POLL, &changed_states);
|
|
|
|
if (changed_states & group->poll_states) {
|
|
/* Initialize trigger windows when entering polling mode */
|
|
if (now > group->polling_until)
|
|
init_triggers(group, now);
|
|
|
|
/*
|
|
* Keep the monitor active for at least the duration of the
|
|
* minimum tracking window as long as monitor states are
|
|
* changing.
|
|
*/
|
|
group->polling_until = now +
|
|
group->poll_min_period * UPDATES_PER_WINDOW;
|
|
}
|
|
|
|
if (now > group->polling_until) {
|
|
group->polling_next_update = ULLONG_MAX;
|
|
goto out;
|
|
}
|
|
|
|
if (now >= group->polling_next_update)
|
|
group->polling_next_update = update_triggers(group, now);
|
|
|
|
psi_schedule_poll_work(group,
|
|
nsecs_to_jiffies(group->polling_next_update - now) + 1);
|
|
|
|
out:
|
|
mutex_unlock(&group->trigger_lock);
|
|
}
|
|
|
|
static void record_times(struct psi_group_cpu *groupc, int cpu,
|
|
bool memstall_tick)
|
|
{
|
|
u32 delta;
|
|
u64 now;
|
|
|
|
now = cpu_clock(cpu);
|
|
delta = now - groupc->state_start;
|
|
groupc->state_start = now;
|
|
|
|
if (groupc->state_mask & (1 << PSI_IO_SOME)) {
|
|
groupc->times[PSI_IO_SOME] += delta;
|
|
if (groupc->state_mask & (1 << PSI_IO_FULL))
|
|
groupc->times[PSI_IO_FULL] += delta;
|
|
}
|
|
|
|
if (groupc->state_mask & (1 << PSI_MEM_SOME)) {
|
|
groupc->times[PSI_MEM_SOME] += delta;
|
|
if (groupc->state_mask & (1 << PSI_MEM_FULL))
|
|
groupc->times[PSI_MEM_FULL] += delta;
|
|
else if (memstall_tick) {
|
|
u32 sample;
|
|
/*
|
|
* Since we care about lost potential, a
|
|
* memstall is FULL when there are no other
|
|
* working tasks, but also when the CPU is
|
|
* actively reclaiming and nothing productive
|
|
* could run even if it were runnable.
|
|
*
|
|
* When the timer tick sees a reclaiming CPU,
|
|
* regardless of runnable tasks, sample a FULL
|
|
* tick (or less if it hasn't been a full tick
|
|
* since the last state change).
|
|
*/
|
|
sample = min(delta, (u32)jiffies_to_nsecs(1));
|
|
groupc->times[PSI_MEM_FULL] += sample;
|
|
}
|
|
}
|
|
|
|
if (groupc->state_mask & (1 << PSI_CPU_SOME))
|
|
groupc->times[PSI_CPU_SOME] += delta;
|
|
|
|
if (groupc->state_mask & (1 << PSI_NONIDLE))
|
|
groupc->times[PSI_NONIDLE] += delta;
|
|
}
|
|
|
|
static u32 psi_group_change(struct psi_group *group, int cpu,
|
|
unsigned int clear, unsigned int set)
|
|
{
|
|
struct psi_group_cpu *groupc;
|
|
unsigned int t, m;
|
|
enum psi_states s;
|
|
u32 state_mask = 0;
|
|
|
|
groupc = per_cpu_ptr(group->pcpu, cpu);
|
|
|
|
/*
|
|
* First we assess the aggregate resource states this CPU's
|
|
* tasks have been in since the last change, and account any
|
|
* SOME and FULL time these may have resulted in.
|
|
*
|
|
* Then we update the task counts according to the state
|
|
* change requested through the @clear and @set bits.
|
|
*/
|
|
write_seqcount_begin(&groupc->seq);
|
|
|
|
record_times(groupc, cpu, false);
|
|
|
|
for (t = 0, m = clear; m; m &= ~(1 << t), t++) {
|
|
if (!(m & (1 << t)))
|
|
continue;
|
|
if (groupc->tasks[t] == 0 && !psi_bug) {
|
|
printk_deferred(KERN_ERR "psi: task underflow! cpu=%d t=%d tasks=[%u %u %u] clear=%x set=%x\n",
|
|
cpu, t, groupc->tasks[0],
|
|
groupc->tasks[1], groupc->tasks[2],
|
|
clear, set);
|
|
psi_bug = 1;
|
|
}
|
|
groupc->tasks[t]--;
|
|
}
|
|
|
|
for (t = 0; set; set &= ~(1 << t), t++)
|
|
if (set & (1 << t))
|
|
groupc->tasks[t]++;
|
|
|
|
/* Calculate state mask representing active states */
|
|
for (s = 0; s < NR_PSI_STATES; s++) {
|
|
if (test_state(groupc->tasks, s))
|
|
state_mask |= (1 << s);
|
|
}
|
|
groupc->state_mask = state_mask;
|
|
|
|
write_seqcount_end(&groupc->seq);
|
|
|
|
return state_mask;
|
|
}
|
|
|
|
static struct psi_group *iterate_groups(struct task_struct *task, void **iter)
|
|
{
|
|
if (*iter == &psi_system)
|
|
return NULL;
|
|
|
|
#ifdef CONFIG_CGROUPS
|
|
if (static_branch_likely(&psi_cgroups_enabled)) {
|
|
struct cgroup *cgroup = NULL;
|
|
|
|
if (!*iter)
|
|
cgroup = task->cgroups->dfl_cgrp;
|
|
else
|
|
cgroup = cgroup_parent(*iter);
|
|
|
|
if (cgroup && cgroup_parent(cgroup)) {
|
|
*iter = cgroup;
|
|
return cgroup_psi(cgroup);
|
|
}
|
|
}
|
|
#endif
|
|
*iter = &psi_system;
|
|
return &psi_system;
|
|
}
|
|
|
|
void psi_task_change(struct task_struct *task, int clear, int set)
|
|
{
|
|
int cpu = task_cpu(task);
|
|
struct psi_group *group;
|
|
bool wake_clock = true;
|
|
void *iter = NULL;
|
|
|
|
if (!task->pid)
|
|
return;
|
|
|
|
if (((task->psi_flags & set) ||
|
|
(task->psi_flags & clear) != clear) &&
|
|
!psi_bug) {
|
|
printk_deferred(KERN_ERR "psi: inconsistent task state! task=%d:%s cpu=%d psi_flags=%x clear=%x set=%x\n",
|
|
task->pid, task->comm, cpu,
|
|
task->psi_flags, clear, set);
|
|
psi_bug = 1;
|
|
}
|
|
|
|
task->psi_flags &= ~clear;
|
|
task->psi_flags |= set;
|
|
|
|
/*
|
|
* Periodic aggregation shuts off if there is a period of no
|
|
* task changes, so we wake it back up if necessary. However,
|
|
* don't do this if the task change is the aggregation worker
|
|
* itself going to sleep, or we'll ping-pong forever.
|
|
*/
|
|
if (unlikely((clear & TSK_RUNNING) &&
|
|
(task->flags & PF_WQ_WORKER) &&
|
|
wq_worker_last_func(task) == psi_avgs_work))
|
|
wake_clock = false;
|
|
|
|
while ((group = iterate_groups(task, &iter))) {
|
|
u32 state_mask = psi_group_change(group, cpu, clear, set);
|
|
|
|
if (state_mask & group->poll_states)
|
|
psi_schedule_poll_work(group, 1);
|
|
|
|
if (wake_clock && !delayed_work_pending(&group->avgs_work))
|
|
schedule_delayed_work(&group->avgs_work, PSI_FREQ);
|
|
}
|
|
}
|
|
|
|
void psi_memstall_tick(struct task_struct *task, int cpu)
|
|
{
|
|
struct psi_group *group;
|
|
void *iter = NULL;
|
|
|
|
while ((group = iterate_groups(task, &iter))) {
|
|
struct psi_group_cpu *groupc;
|
|
|
|
groupc = per_cpu_ptr(group->pcpu, cpu);
|
|
write_seqcount_begin(&groupc->seq);
|
|
record_times(groupc, cpu, true);
|
|
write_seqcount_end(&groupc->seq);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* psi_memstall_enter - mark the beginning of a memory stall section
|
|
* @flags: flags to handle nested sections
|
|
*
|
|
* Marks the calling task as being stalled due to a lack of memory,
|
|
* such as waiting for a refault or performing reclaim.
|
|
*/
|
|
void psi_memstall_enter(unsigned long *flags)
|
|
{
|
|
struct rq_flags rf;
|
|
struct rq *rq;
|
|
|
|
if (static_branch_likely(&psi_disabled))
|
|
return;
|
|
|
|
*flags = current->flags & PF_MEMSTALL;
|
|
if (*flags)
|
|
return;
|
|
/*
|
|
* PF_MEMSTALL setting & accounting needs to be atomic wrt
|
|
* changes to the task's scheduling state, otherwise we can
|
|
* race with CPU migration.
|
|
*/
|
|
rq = this_rq_lock_irq(&rf);
|
|
|
|
current->flags |= PF_MEMSTALL;
|
|
psi_task_change(current, 0, TSK_MEMSTALL);
|
|
|
|
rq_unlock_irq(rq, &rf);
|
|
}
|
|
|
|
/**
|
|
* psi_memstall_leave - mark the end of an memory stall section
|
|
* @flags: flags to handle nested memdelay sections
|
|
*
|
|
* Marks the calling task as no longer stalled due to lack of memory.
|
|
*/
|
|
void psi_memstall_leave(unsigned long *flags)
|
|
{
|
|
struct rq_flags rf;
|
|
struct rq *rq;
|
|
|
|
if (static_branch_likely(&psi_disabled))
|
|
return;
|
|
|
|
if (*flags)
|
|
return;
|
|
/*
|
|
* PF_MEMSTALL clearing & accounting needs to be atomic wrt
|
|
* changes to the task's scheduling state, otherwise we could
|
|
* race with CPU migration.
|
|
*/
|
|
rq = this_rq_lock_irq(&rf);
|
|
|
|
current->flags &= ~PF_MEMSTALL;
|
|
psi_task_change(current, TSK_MEMSTALL, 0);
|
|
|
|
rq_unlock_irq(rq, &rf);
|
|
}
|
|
|
|
#ifdef CONFIG_CGROUPS
|
|
int psi_cgroup_alloc(struct cgroup *cgroup)
|
|
{
|
|
if (static_branch_likely(&psi_disabled))
|
|
return 0;
|
|
|
|
cgroup->psi.pcpu = alloc_percpu(struct psi_group_cpu);
|
|
if (!cgroup->psi.pcpu)
|
|
return -ENOMEM;
|
|
group_init(&cgroup->psi);
|
|
return 0;
|
|
}
|
|
|
|
void psi_cgroup_free(struct cgroup *cgroup)
|
|
{
|
|
if (static_branch_likely(&psi_disabled))
|
|
return;
|
|
|
|
cancel_delayed_work_sync(&cgroup->psi.avgs_work);
|
|
free_percpu(cgroup->psi.pcpu);
|
|
/* All triggers must be removed by now */
|
|
WARN_ONCE(cgroup->psi.poll_states, "psi: trigger leak\n");
|
|
}
|
|
|
|
/**
|
|
* cgroup_move_task - move task to a different cgroup
|
|
* @task: the task
|
|
* @to: the target css_set
|
|
*
|
|
* Move task to a new cgroup and safely migrate its associated stall
|
|
* state between the different groups.
|
|
*
|
|
* This function acquires the task's rq lock to lock out concurrent
|
|
* changes to the task's scheduling state and - in case the task is
|
|
* running - concurrent changes to its stall state.
|
|
*/
|
|
void cgroup_move_task(struct task_struct *task, struct css_set *to)
|
|
{
|
|
unsigned int task_flags = 0;
|
|
struct rq_flags rf;
|
|
struct rq *rq;
|
|
|
|
if (static_branch_likely(&psi_disabled)) {
|
|
/*
|
|
* Lame to do this here, but the scheduler cannot be locked
|
|
* from the outside, so we move cgroups from inside sched/.
|
|
*/
|
|
rcu_assign_pointer(task->cgroups, to);
|
|
return;
|
|
}
|
|
|
|
rq = task_rq_lock(task, &rf);
|
|
|
|
if (task_on_rq_queued(task))
|
|
task_flags = TSK_RUNNING;
|
|
else if (task->in_iowait)
|
|
task_flags = TSK_IOWAIT;
|
|
|
|
if (task->flags & PF_MEMSTALL)
|
|
task_flags |= TSK_MEMSTALL;
|
|
|
|
if (task_flags)
|
|
psi_task_change(task, task_flags, 0);
|
|
|
|
/* See comment above */
|
|
rcu_assign_pointer(task->cgroups, to);
|
|
|
|
if (task_flags)
|
|
psi_task_change(task, 0, task_flags);
|
|
|
|
task_rq_unlock(rq, task, &rf);
|
|
}
|
|
#endif /* CONFIG_CGROUPS */
|
|
|
|
int psi_show(struct seq_file *m, struct psi_group *group, enum psi_res res)
|
|
{
|
|
int full;
|
|
u64 now;
|
|
|
|
if (static_branch_likely(&psi_disabled))
|
|
return -EOPNOTSUPP;
|
|
|
|
/* Update averages before reporting them */
|
|
mutex_lock(&group->avgs_lock);
|
|
now = sched_clock();
|
|
collect_percpu_times(group, PSI_AVGS, NULL);
|
|
if (now >= group->avg_next_update)
|
|
group->avg_next_update = update_averages(group, now);
|
|
mutex_unlock(&group->avgs_lock);
|
|
|
|
for (full = 0; full < 2 - (res == PSI_CPU); full++) {
|
|
unsigned long avg[3];
|
|
u64 total;
|
|
int w;
|
|
|
|
for (w = 0; w < 3; w++)
|
|
avg[w] = group->avg[res * 2 + full][w];
|
|
total = div_u64(group->total[PSI_AVGS][res * 2 + full],
|
|
NSEC_PER_USEC);
|
|
|
|
seq_printf(m, "%s avg10=%lu.%02lu avg60=%lu.%02lu avg300=%lu.%02lu total=%llu\n",
|
|
full ? "full" : "some",
|
|
LOAD_INT(avg[0]), LOAD_FRAC(avg[0]),
|
|
LOAD_INT(avg[1]), LOAD_FRAC(avg[1]),
|
|
LOAD_INT(avg[2]), LOAD_FRAC(avg[2]),
|
|
total);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int psi_io_show(struct seq_file *m, void *v)
|
|
{
|
|
return psi_show(m, &psi_system, PSI_IO);
|
|
}
|
|
|
|
static int psi_memory_show(struct seq_file *m, void *v)
|
|
{
|
|
return psi_show(m, &psi_system, PSI_MEM);
|
|
}
|
|
|
|
static int psi_cpu_show(struct seq_file *m, void *v)
|
|
{
|
|
return psi_show(m, &psi_system, PSI_CPU);
|
|
}
|
|
|
|
static int psi_io_open(struct inode *inode, struct file *file)
|
|
{
|
|
return single_open(file, psi_io_show, NULL);
|
|
}
|
|
|
|
static int psi_memory_open(struct inode *inode, struct file *file)
|
|
{
|
|
return single_open(file, psi_memory_show, NULL);
|
|
}
|
|
|
|
static int psi_cpu_open(struct inode *inode, struct file *file)
|
|
{
|
|
return single_open(file, psi_cpu_show, NULL);
|
|
}
|
|
|
|
struct psi_trigger *psi_trigger_create(struct psi_group *group,
|
|
char *buf, size_t nbytes, enum psi_res res)
|
|
{
|
|
struct psi_trigger *t;
|
|
enum psi_states state;
|
|
u32 threshold_us;
|
|
u32 window_us;
|
|
|
|
if (static_branch_likely(&psi_disabled))
|
|
return ERR_PTR(-EOPNOTSUPP);
|
|
|
|
if (sscanf(buf, "some %u %u", &threshold_us, &window_us) == 2)
|
|
state = PSI_IO_SOME + res * 2;
|
|
else if (sscanf(buf, "full %u %u", &threshold_us, &window_us) == 2)
|
|
state = PSI_IO_FULL + res * 2;
|
|
else
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
if (state >= PSI_NONIDLE)
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
if (window_us < WINDOW_MIN_US ||
|
|
window_us > WINDOW_MAX_US)
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
/* Check threshold */
|
|
if (threshold_us == 0 || threshold_us > window_us)
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
t = kmalloc(sizeof(*t), GFP_KERNEL);
|
|
if (!t)
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
t->group = group;
|
|
t->state = state;
|
|
t->threshold = threshold_us * NSEC_PER_USEC;
|
|
t->win.size = window_us * NSEC_PER_USEC;
|
|
window_reset(&t->win, 0, 0, 0);
|
|
|
|
t->event = 0;
|
|
t->last_event_time = 0;
|
|
init_waitqueue_head(&t->event_wait);
|
|
get_task_comm(t->comm, current);
|
|
timer_setup(&t->wdog_timer, ulmk_watchdog_fn, TIMER_DEFERRABLE);
|
|
|
|
mutex_lock(&group->trigger_lock);
|
|
|
|
if (!rcu_access_pointer(group->poll_kworker)) {
|
|
struct sched_param param = {
|
|
.sched_priority = 1,
|
|
};
|
|
struct kthread_worker *kworker;
|
|
|
|
kworker = kthread_create_worker(0, "psimon");
|
|
if (IS_ERR(kworker)) {
|
|
kfree(t);
|
|
mutex_unlock(&group->trigger_lock);
|
|
return ERR_CAST(kworker);
|
|
}
|
|
sched_setscheduler_nocheck(kworker->task, SCHED_FIFO, ¶m);
|
|
kthread_init_delayed_work(&group->poll_work,
|
|
psi_poll_work);
|
|
rcu_assign_pointer(group->poll_kworker, kworker);
|
|
}
|
|
|
|
list_add(&t->node, &group->triggers);
|
|
group->poll_min_period = min(group->poll_min_period,
|
|
div_u64(t->win.size, UPDATES_PER_WINDOW));
|
|
group->nr_triggers[t->state]++;
|
|
group->poll_states |= (1 << t->state);
|
|
|
|
mutex_unlock(&group->trigger_lock);
|
|
|
|
return t;
|
|
}
|
|
|
|
void psi_trigger_destroy(struct psi_trigger *t)
|
|
{
|
|
struct psi_group *group;
|
|
struct kthread_worker *kworker_to_destroy = NULL;
|
|
|
|
/*
|
|
* We do not check psi_disabled since it might have been disabled after
|
|
* the trigger got created.
|
|
*/
|
|
if (!t)
|
|
return;
|
|
|
|
group = t->group;
|
|
/*
|
|
* Wakeup waiters to stop polling and clear the queue to prevent it from
|
|
* being accessed later. Can happen if cgroup is deleted from under a
|
|
* polling process.
|
|
*/
|
|
wake_up_pollfree(&t->event_wait);
|
|
|
|
mutex_lock(&group->trigger_lock);
|
|
|
|
if (!list_empty(&t->node)) {
|
|
struct psi_trigger *tmp;
|
|
u64 period = ULLONG_MAX;
|
|
|
|
list_del(&t->node);
|
|
group->nr_triggers[t->state]--;
|
|
if (!group->nr_triggers[t->state])
|
|
group->poll_states &= ~(1 << t->state);
|
|
/* reset min update period for the remaining triggers */
|
|
list_for_each_entry(tmp, &group->triggers, node)
|
|
period = min(period, div_u64(tmp->win.size,
|
|
UPDATES_PER_WINDOW));
|
|
group->poll_min_period = period;
|
|
/* Destroy poll_kworker when the last trigger is destroyed */
|
|
if (group->poll_states == 0) {
|
|
group->polling_until = 0;
|
|
kworker_to_destroy = rcu_dereference_protected(
|
|
group->poll_kworker,
|
|
lockdep_is_held(&group->trigger_lock));
|
|
rcu_assign_pointer(group->poll_kworker, NULL);
|
|
}
|
|
}
|
|
|
|
del_timer_sync(&t->wdog_timer);
|
|
mutex_unlock(&group->trigger_lock);
|
|
|
|
/*
|
|
* Wait for psi_schedule_poll_work RCU to complete its read-side
|
|
* critical section before destroying the trigger and optionally the
|
|
* poll_task.
|
|
*/
|
|
synchronize_rcu();
|
|
/*
|
|
* Destroy the kworker after releasing trigger_lock to prevent a
|
|
* deadlock while waiting for psi_poll_work to acquire trigger_lock
|
|
*/
|
|
if (kworker_to_destroy) {
|
|
/*
|
|
* After the RCU grace period has expired, the worker
|
|
* can no longer be found through group->poll_kworker.
|
|
* But it might have been already scheduled before
|
|
* that - deschedule it cleanly before destroying it.
|
|
*/
|
|
kthread_cancel_delayed_work_sync(&group->poll_work);
|
|
atomic_set(&group->poll_scheduled, 0);
|
|
|
|
kthread_destroy_worker(kworker_to_destroy);
|
|
}
|
|
kfree(t);
|
|
}
|
|
|
|
__poll_t psi_trigger_poll(void **trigger_ptr,
|
|
struct file *file, poll_table *wait)
|
|
{
|
|
__poll_t ret = DEFAULT_POLLMASK;
|
|
struct psi_trigger *t;
|
|
|
|
if (static_branch_likely(&psi_disabled))
|
|
return DEFAULT_POLLMASK | EPOLLERR | EPOLLPRI;
|
|
|
|
t = smp_load_acquire(trigger_ptr);
|
|
if (!t)
|
|
return DEFAULT_POLLMASK | EPOLLERR | EPOLLPRI;
|
|
|
|
poll_wait(file, &t->event_wait, wait);
|
|
|
|
if (cmpxchg(&t->event, 1, 0) == 1) {
|
|
ret |= EPOLLPRI;
|
|
if (!strcmp(t->comm, ULMK_MAGIC))
|
|
ulmk_watchdog_pet(&t->wdog_timer);
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
static ssize_t psi_write(struct file *file, const char __user *user_buf,
|
|
size_t nbytes, enum psi_res res)
|
|
{
|
|
char buf[32];
|
|
size_t buf_size;
|
|
struct seq_file *seq;
|
|
struct psi_trigger *new;
|
|
|
|
if (static_branch_likely(&psi_disabled))
|
|
return -EOPNOTSUPP;
|
|
|
|
if (!nbytes)
|
|
return -EINVAL;
|
|
|
|
buf_size = min(nbytes, sizeof(buf));
|
|
if (copy_from_user(buf, user_buf, buf_size))
|
|
return -EFAULT;
|
|
|
|
buf[buf_size - 1] = '\0';
|
|
|
|
seq = file->private_data;
|
|
|
|
/* Take seq->lock to protect seq->private from concurrent writes */
|
|
mutex_lock(&seq->lock);
|
|
|
|
/* Allow only one trigger per file descriptor */
|
|
if (seq->private) {
|
|
mutex_unlock(&seq->lock);
|
|
return -EBUSY;
|
|
}
|
|
|
|
new = psi_trigger_create(&psi_system, buf, nbytes, res);
|
|
if (IS_ERR(new)) {
|
|
mutex_unlock(&seq->lock);
|
|
return PTR_ERR(new);
|
|
}
|
|
|
|
smp_store_release(&seq->private, new);
|
|
mutex_unlock(&seq->lock);
|
|
|
|
return nbytes;
|
|
}
|
|
|
|
static ssize_t psi_io_write(struct file *file, const char __user *user_buf,
|
|
size_t nbytes, loff_t *ppos)
|
|
{
|
|
return psi_write(file, user_buf, nbytes, PSI_IO);
|
|
}
|
|
|
|
static ssize_t psi_memory_write(struct file *file, const char __user *user_buf,
|
|
size_t nbytes, loff_t *ppos)
|
|
{
|
|
return psi_write(file, user_buf, nbytes, PSI_MEM);
|
|
}
|
|
|
|
static ssize_t psi_cpu_write(struct file *file, const char __user *user_buf,
|
|
size_t nbytes, loff_t *ppos)
|
|
{
|
|
return psi_write(file, user_buf, nbytes, PSI_CPU);
|
|
}
|
|
|
|
static __poll_t psi_fop_poll(struct file *file, poll_table *wait)
|
|
{
|
|
struct seq_file *seq = file->private_data;
|
|
|
|
return psi_trigger_poll(&seq->private, file, wait);
|
|
}
|
|
|
|
static int psi_fop_release(struct inode *inode, struct file *file)
|
|
{
|
|
struct seq_file *seq = file->private_data;
|
|
|
|
psi_trigger_destroy(seq->private);
|
|
return single_release(inode, file);
|
|
}
|
|
|
|
static const struct file_operations psi_io_fops = {
|
|
.open = psi_io_open,
|
|
.read = seq_read,
|
|
.llseek = seq_lseek,
|
|
.write = psi_io_write,
|
|
.poll = psi_fop_poll,
|
|
.release = psi_fop_release,
|
|
};
|
|
|
|
static const struct file_operations psi_memory_fops = {
|
|
.open = psi_memory_open,
|
|
.read = seq_read,
|
|
.llseek = seq_lseek,
|
|
.write = psi_memory_write,
|
|
.poll = psi_fop_poll,
|
|
.release = psi_fop_release,
|
|
};
|
|
|
|
static const struct file_operations psi_cpu_fops = {
|
|
.open = psi_cpu_open,
|
|
.read = seq_read,
|
|
.llseek = seq_lseek,
|
|
.write = psi_cpu_write,
|
|
.poll = psi_fop_poll,
|
|
.release = psi_fop_release,
|
|
};
|
|
|
|
static int __init psi_proc_init(void)
|
|
{
|
|
proc_mkdir("pressure", NULL);
|
|
proc_create("pressure/io", 0, NULL, &psi_io_fops);
|
|
proc_create("pressure/memory", 0, NULL, &psi_memory_fops);
|
|
proc_create("pressure/cpu", 0, NULL, &psi_cpu_fops);
|
|
return 0;
|
|
}
|
|
module_init(psi_proc_init);
|