Changes in 5.15.79
thunderbolt: Tear down existing tunnels when resuming from hibernate
thunderbolt: Add DP OUT resource when DP tunnel is discovered
fuse: fix readdir cache race
drm/amdkfd: avoid recursive lock in migrations back to RAM
drm/amdkfd: handle CPU fault on COW mapping
drm/amdkfd: Fix NULL pointer dereference in svm_migrate_to_ram()
hwspinlock: qcom: correct MMIO max register for newer SoCs
phy: stm32: fix an error code in probe
wifi: cfg80211: silence a sparse RCU warning
wifi: cfg80211: fix memory leak in query_regdb_file()
soundwire: qcom: reinit broadcast completion
soundwire: qcom: check for outanding writes before doing a read
bpf, verifier: Fix memory leak in array reallocation for stack state
bpf, sockmap: Fix the sk->sk_forward_alloc warning of sk_stream_kill_queues
wifi: mac80211: Set TWT Information Frame Disabled bit as 1
bpftool: Fix NULL pointer dereference when pin {PROG, MAP, LINK} without FILE
HID: hyperv: fix possible memory leak in mousevsc_probe()
bpf, sockmap: Fix sk->sk_forward_alloc warn_on in sk_stream_kill_queues
bpf: Fix sockmap calling sleepable function in teardown path
bpf, sock_map: Move cancel_work_sync() out of sock lock
bpf: Add helper macro bpf_for_each_reg_in_vstate
bpf: Fix wrong reg type conversion in release_reference()
net: gso: fix panic on frag_list with mixed head alloc types
macsec: delete new rxsc when offload fails
macsec: fix secy->n_rx_sc accounting
macsec: fix detection of RXSCs when toggling offloading
macsec: clear encryption keys from the stack after setting up offload
octeontx2-pf: Use hardware register for CQE count
octeontx2-pf: NIX TX overwrites SQ_CTX_HW_S[SQ_INT]
net: tun: Fix memory leaks of napi_get_frags
bnxt_en: Fix possible crash in bnxt_hwrm_set_coal()
bnxt_en: fix potentially incorrect return value for ndo_rx_flow_steer
net: fman: Unregister ethernet device on removal
capabilities: fix undefined behavior in bit shift for CAP_TO_MASK
phy: ralink: mt7621-pci: add sentinel to quirks table
KVM: s390: pv: don't allow userspace to set the clock under PV
net: lapbether: fix issue of dev reference count leakage in lapbeth_device_event()
hamradio: fix issue of dev reference count leakage in bpq_device_event()
net: wwan: iosm: fix memory leak in ipc_wwan_dellink
net: wwan: mhi: fix memory leak in mhi_mbim_dellink
drm/vc4: Fix missing platform_unregister_drivers() call in vc4_drm_register()
tcp: prohibit TCP_REPAIR_OPTIONS if data was already sent
ipv6: addrlabel: fix infoleak when sending struct ifaddrlblmsg to network
can: af_can: fix NULL pointer dereference in can_rx_register()
net: stmmac: dwmac-meson8b: fix meson8b_devm_clk_prepare_enable()
net: broadcom: Fix BCMGENET Kconfig
tipc: fix the msg->req tlv len check in tipc_nl_compat_name_table_dump_header
dmaengine: pxa_dma: use platform_get_irq_optional
dmaengine: mv_xor_v2: Fix a resource leak in mv_xor_v2_remove()
dmaengine: ti: k3-udma-glue: fix memory leak when register device fail
net: lapbether: fix issue of invalid opcode in lapbeth_open()
drivers: net: xgene: disable napi when register irq failed in xgene_enet_open()
perf stat: Fix printing os->prefix in CSV metrics output
perf tools: Add the include/perf/ directory to .gitignore
netfilter: nfnetlink: fix potential dead lock in nfnetlink_rcv_msg()
netfilter: Cleanup nft_net->module_list from nf_tables_exit_net()
net: marvell: prestera: fix memory leak in prestera_rxtx_switch_init()
net: nixge: disable napi when enable interrupts failed in nixge_open()
net: wwan: iosm: fix memory leak in ipc_pcie_read_bios_cfg
net/mlx5: Bridge, verify LAG state when adding bond to bridge
net/mlx5: Allow async trigger completion execution on single CPU systems
net/mlx5e: E-Switch, Fix comparing termination table instance
net: cpsw: disable napi in cpsw_ndo_open()
net: cxgb3_main: disable napi when bind qsets failed in cxgb_up()
stmmac: intel: Enable 2.5Gbps for Intel AlderLake-S
stmmac: intel: Update PCH PTP clock rate from 200MHz to 204.8MHz
mctp: Fix an error handling path in mctp_init()
cxgb4vf: shut down the adapter when t4vf_update_port_info() failed in cxgb4vf_open()
stmmac: dwmac-loongson: fix missing pci_disable_msi() while module exiting
stmmac: dwmac-loongson: fix missing pci_disable_device() in loongson_dwmac_probe()
stmmac: dwmac-loongson: fix missing of_node_put() while module exiting
net: phy: mscc: macsec: clear encryption keys when freeing a flow
net: atlantic: macsec: clear encryption keys from the stack
ethernet: s2io: disable napi when start nic failed in s2io_card_up()
net: mv643xx_eth: disable napi when init rxq or txq failed in mv643xx_eth_open()
ethernet: tundra: free irq when alloc ring failed in tsi108_open()
net: macvlan: fix memory leaks of macvlan_common_newlink
riscv: process: fix kernel info leakage
riscv: vdso: fix build with llvm
riscv: fix reserved memory setup
arm64: efi: Fix handling of misaligned runtime regions and drop warning
MIPS: jump_label: Fix compat branch range check
mmc: cqhci: Provide helper for resetting both SDHCI and CQHCI
mmc: sdhci-of-arasan: Fix SDHCI_RESET_ALL for CQHCI
mmc: sdhci_am654: Fix SDHCI_RESET_ALL for CQHCI
mmc: sdhci-tegra: Fix SDHCI_RESET_ALL for CQHCI
mmc: sdhci-esdhc-imx: use the correct host caps for MMC_CAP_8_BIT_DATA
ALSA: hda/hdmi - enable runtime pm for more AMD display audio
ALSA: hda/ca0132: add quirk for EVGA Z390 DARK
ALSA: hda: fix potential memleak in 'add_widget_node'
ALSA: hda/realtek: Add Positivo C6300 model quirk
ALSA: usb-audio: Yet more regression for for the delayed card registration
ALSA: usb-audio: Add quirk entry for M-Audio Micro
ALSA: usb-audio: Add DSD support for Accuphase DAC-60
vmlinux.lds.h: Fix placement of '.data..decrypted' section
ata: libata-scsi: fix SYNCHRONIZE CACHE (16) command failure
nilfs2: fix deadlock in nilfs_count_free_blocks()
nilfs2: fix use-after-free bug of ns_writer on remount
drm/i915/dmabuf: fix sg_table handling in map_dma_buf
drm/amdgpu: disable BACO on special BEIGE_GOBY card
platform/x86: hp_wmi: Fix rfkill causing soft blocked wifi
wifi: ath11k: avoid deadlock during regulatory update in ath11k_regd_update()
btrfs: fix match incorrectly in dev_args_match_device
btrfs: selftests: fix wrong error check in btrfs_free_dummy_root()
btrfs: zoned: initialize device's zone info for seeding
mms: sdhci-esdhc-imx: Fix SDHCI_RESET_ALL for CQHCI
udf: Fix a slab-out-of-bounds write bug in udf_find_entry()
mm/damon/dbgfs: check if rm_contexts input is for a real context
mm/memremap.c: map FS_DAX device memory as decrypted
mm/shmem: use page_mapping() to detect page cache for uffd continue
can: j1939: j1939_send_one(): fix missing CAN header initialization
cert host tools: Stop complaining about deprecated OpenSSL functions
dmaengine: at_hdmac: Fix at_lli struct definition
dmaengine: at_hdmac: Don't start transactions at tx_submit level
dmaengine: at_hdmac: Start transfer for cyclic channels in issue_pending
dmaengine: at_hdmac: Fix premature completion of desc in issue_pending
dmaengine: at_hdmac: Do not call the complete callback on device_terminate_all
dmaengine: at_hdmac: Protect atchan->status with the channel lock
dmaengine: at_hdmac: Fix concurrency problems by removing atc_complete_all()
dmaengine: at_hdmac: Fix concurrency over descriptor
dmaengine: at_hdmac: Free the memset buf without holding the chan lock
dmaengine: at_hdmac: Fix concurrency over the active list
dmaengine: at_hdmac: Fix descriptor handling when issuing it to hardware
dmaengine: at_hdmac: Fix completion of unissued descriptor in case of errors
dmaengine: at_hdmac: Don't allow CPU to reorder channel enable
dmaengine: at_hdmac: Fix impossible condition
dmaengine: at_hdmac: Check return code of dma_async_device_register
marvell: octeontx2: build error: unknown type name 'u64'
drm/amdkfd: Migrate in CPU page fault use current mm
net: tun: call napi_schedule_prep() to ensure we own a napi
x86/cpu: Restore AMD's DE_CFG MSR after resume
Linux 5.15.79
Change-Id: I6f77aa724b7aa43abcef3444af951c7c62d46303
Signed-off-by: Greg Kroah-Hartman <gregkh@google.com>
1013 lines
21 KiB
C
1013 lines
21 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
/*
|
|
* DAMON Debugfs Interface
|
|
*
|
|
* Author: SeongJae Park <sjpark@amazon.de>
|
|
*/
|
|
|
|
#define pr_fmt(fmt) "damon-dbgfs: " fmt
|
|
|
|
#include <linux/damon.h>
|
|
#include <linux/debugfs.h>
|
|
#include <linux/file.h>
|
|
#include <linux/mm.h>
|
|
#include <linux/module.h>
|
|
#include <linux/page_idle.h>
|
|
#include <linux/slab.h>
|
|
|
|
static struct damon_ctx **dbgfs_ctxs;
|
|
static int dbgfs_nr_ctxs;
|
|
static struct dentry **dbgfs_dirs;
|
|
static DEFINE_MUTEX(damon_dbgfs_lock);
|
|
|
|
/*
|
|
* Returns non-empty string on success, negative error code otherwise.
|
|
*/
|
|
static char *user_input_str(const char __user *buf, size_t count, loff_t *ppos)
|
|
{
|
|
char *kbuf;
|
|
ssize_t ret;
|
|
|
|
/* We do not accept continuous write */
|
|
if (*ppos)
|
|
return ERR_PTR(-EINVAL);
|
|
|
|
kbuf = kmalloc(count + 1, GFP_KERNEL | __GFP_NOWARN);
|
|
if (!kbuf)
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
ret = simple_write_to_buffer(kbuf, count + 1, ppos, buf, count);
|
|
if (ret != count) {
|
|
kfree(kbuf);
|
|
return ERR_PTR(-EIO);
|
|
}
|
|
kbuf[ret] = '\0';
|
|
|
|
return kbuf;
|
|
}
|
|
|
|
static ssize_t dbgfs_attrs_read(struct file *file,
|
|
char __user *buf, size_t count, loff_t *ppos)
|
|
{
|
|
struct damon_ctx *ctx = file->private_data;
|
|
char kbuf[128];
|
|
int ret;
|
|
|
|
mutex_lock(&ctx->kdamond_lock);
|
|
ret = scnprintf(kbuf, ARRAY_SIZE(kbuf), "%lu %lu %lu %lu %lu\n",
|
|
ctx->sample_interval, ctx->aggr_interval,
|
|
ctx->primitive_update_interval, ctx->min_nr_regions,
|
|
ctx->max_nr_regions);
|
|
mutex_unlock(&ctx->kdamond_lock);
|
|
|
|
return simple_read_from_buffer(buf, count, ppos, kbuf, ret);
|
|
}
|
|
|
|
static ssize_t dbgfs_attrs_write(struct file *file,
|
|
const char __user *buf, size_t count, loff_t *ppos)
|
|
{
|
|
struct damon_ctx *ctx = file->private_data;
|
|
unsigned long s, a, r, minr, maxr;
|
|
char *kbuf;
|
|
ssize_t ret;
|
|
|
|
kbuf = user_input_str(buf, count, ppos);
|
|
if (IS_ERR(kbuf))
|
|
return PTR_ERR(kbuf);
|
|
|
|
if (sscanf(kbuf, "%lu %lu %lu %lu %lu",
|
|
&s, &a, &r, &minr, &maxr) != 5) {
|
|
ret = -EINVAL;
|
|
goto out;
|
|
}
|
|
|
|
mutex_lock(&ctx->kdamond_lock);
|
|
if (ctx->kdamond) {
|
|
ret = -EBUSY;
|
|
goto unlock_out;
|
|
}
|
|
|
|
ret = damon_set_attrs(ctx, s, a, r, minr, maxr);
|
|
if (!ret)
|
|
ret = count;
|
|
unlock_out:
|
|
mutex_unlock(&ctx->kdamond_lock);
|
|
out:
|
|
kfree(kbuf);
|
|
return ret;
|
|
}
|
|
|
|
static ssize_t sprint_schemes(struct damon_ctx *c, char *buf, ssize_t len)
|
|
{
|
|
struct damos *s;
|
|
int written = 0;
|
|
int rc;
|
|
|
|
damon_for_each_scheme(s, c) {
|
|
rc = scnprintf(&buf[written], len - written,
|
|
"%lu %lu %u %u %u %u %d %lu %lu %lu %u %u %u %d %lu %lu %lu %lu %lu %lu %lu %lu %lu\n",
|
|
s->min_sz_region, s->max_sz_region,
|
|
s->min_nr_accesses, s->max_nr_accesses,
|
|
s->min_age_region, s->max_age_region,
|
|
s->action,
|
|
s->quota.ms, s->quota.sz,
|
|
s->quota.reset_interval,
|
|
s->quota.weight_sz,
|
|
s->quota.weight_nr_accesses,
|
|
s->quota.weight_age,
|
|
s->wmarks.metric, s->wmarks.interval,
|
|
s->wmarks.high, s->wmarks.mid, s->wmarks.low,
|
|
s->stat.nr_tried, s->stat.sz_tried,
|
|
s->stat.nr_applied, s->stat.sz_applied,
|
|
s->stat.qt_exceeds);
|
|
if (!rc)
|
|
return -ENOMEM;
|
|
|
|
written += rc;
|
|
}
|
|
return written;
|
|
}
|
|
|
|
static ssize_t dbgfs_schemes_read(struct file *file, char __user *buf,
|
|
size_t count, loff_t *ppos)
|
|
{
|
|
struct damon_ctx *ctx = file->private_data;
|
|
char *kbuf;
|
|
ssize_t len;
|
|
|
|
kbuf = kmalloc(count, GFP_KERNEL);
|
|
if (!kbuf)
|
|
return -ENOMEM;
|
|
|
|
mutex_lock(&ctx->kdamond_lock);
|
|
len = sprint_schemes(ctx, kbuf, count);
|
|
mutex_unlock(&ctx->kdamond_lock);
|
|
if (len < 0)
|
|
goto out;
|
|
len = simple_read_from_buffer(buf, count, ppos, kbuf, len);
|
|
|
|
out:
|
|
kfree(kbuf);
|
|
return len;
|
|
}
|
|
|
|
static void free_schemes_arr(struct damos **schemes, ssize_t nr_schemes)
|
|
{
|
|
ssize_t i;
|
|
|
|
for (i = 0; i < nr_schemes; i++)
|
|
kfree(schemes[i]);
|
|
kfree(schemes);
|
|
}
|
|
|
|
static bool damos_action_valid(int action)
|
|
{
|
|
switch (action) {
|
|
case DAMOS_WILLNEED:
|
|
case DAMOS_COLD:
|
|
case DAMOS_PAGEOUT:
|
|
case DAMOS_HUGEPAGE:
|
|
case DAMOS_NOHUGEPAGE:
|
|
case DAMOS_STAT:
|
|
return true;
|
|
default:
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/*
|
|
* Converts a string into an array of struct damos pointers
|
|
*
|
|
* Returns an array of struct damos pointers that converted if the conversion
|
|
* success, or NULL otherwise.
|
|
*/
|
|
static struct damos **str_to_schemes(const char *str, ssize_t len,
|
|
ssize_t *nr_schemes)
|
|
{
|
|
struct damos *scheme, **schemes;
|
|
const int max_nr_schemes = 256;
|
|
int pos = 0, parsed, ret;
|
|
unsigned long min_sz, max_sz;
|
|
unsigned int min_nr_a, max_nr_a, min_age, max_age;
|
|
unsigned int action;
|
|
|
|
schemes = kmalloc_array(max_nr_schemes, sizeof(scheme),
|
|
GFP_KERNEL);
|
|
if (!schemes)
|
|
return NULL;
|
|
|
|
*nr_schemes = 0;
|
|
while (pos < len && *nr_schemes < max_nr_schemes) {
|
|
struct damos_quota quota = {};
|
|
struct damos_watermarks wmarks;
|
|
|
|
ret = sscanf(&str[pos],
|
|
"%lu %lu %u %u %u %u %u %lu %lu %lu %u %u %u %u %lu %lu %lu %lu%n",
|
|
&min_sz, &max_sz, &min_nr_a, &max_nr_a,
|
|
&min_age, &max_age, &action, "a.ms,
|
|
"a.sz, "a.reset_interval,
|
|
"a.weight_sz, "a.weight_nr_accesses,
|
|
"a.weight_age, &wmarks.metric,
|
|
&wmarks.interval, &wmarks.high, &wmarks.mid,
|
|
&wmarks.low, &parsed);
|
|
if (ret != 18)
|
|
break;
|
|
if (!damos_action_valid(action))
|
|
goto fail;
|
|
|
|
if (min_sz > max_sz || min_nr_a > max_nr_a || min_age > max_age)
|
|
goto fail;
|
|
|
|
if (wmarks.high < wmarks.mid || wmarks.high < wmarks.low ||
|
|
wmarks.mid < wmarks.low)
|
|
goto fail;
|
|
|
|
pos += parsed;
|
|
scheme = damon_new_scheme(min_sz, max_sz, min_nr_a, max_nr_a,
|
|
min_age, max_age, action, "a, &wmarks);
|
|
if (!scheme)
|
|
goto fail;
|
|
|
|
schemes[*nr_schemes] = scheme;
|
|
*nr_schemes += 1;
|
|
}
|
|
return schemes;
|
|
fail:
|
|
free_schemes_arr(schemes, *nr_schemes);
|
|
return NULL;
|
|
}
|
|
|
|
static ssize_t dbgfs_schemes_write(struct file *file, const char __user *buf,
|
|
size_t count, loff_t *ppos)
|
|
{
|
|
struct damon_ctx *ctx = file->private_data;
|
|
char *kbuf;
|
|
struct damos **schemes;
|
|
ssize_t nr_schemes = 0, ret;
|
|
|
|
kbuf = user_input_str(buf, count, ppos);
|
|
if (IS_ERR(kbuf))
|
|
return PTR_ERR(kbuf);
|
|
|
|
schemes = str_to_schemes(kbuf, count, &nr_schemes);
|
|
if (!schemes) {
|
|
ret = -EINVAL;
|
|
goto out;
|
|
}
|
|
|
|
mutex_lock(&ctx->kdamond_lock);
|
|
if (ctx->kdamond) {
|
|
ret = -EBUSY;
|
|
goto unlock_out;
|
|
}
|
|
|
|
ret = damon_set_schemes(ctx, schemes, nr_schemes);
|
|
if (!ret) {
|
|
ret = count;
|
|
nr_schemes = 0;
|
|
}
|
|
|
|
unlock_out:
|
|
mutex_unlock(&ctx->kdamond_lock);
|
|
free_schemes_arr(schemes, nr_schemes);
|
|
out:
|
|
kfree(kbuf);
|
|
return ret;
|
|
}
|
|
|
|
static inline bool targetid_is_pid(const struct damon_ctx *ctx)
|
|
{
|
|
return ctx->primitive.target_valid == damon_va_target_valid;
|
|
}
|
|
|
|
static ssize_t sprint_target_ids(struct damon_ctx *ctx, char *buf, ssize_t len)
|
|
{
|
|
struct damon_target *t;
|
|
unsigned long id;
|
|
int written = 0;
|
|
int rc;
|
|
|
|
damon_for_each_target(t, ctx) {
|
|
id = t->id;
|
|
if (targetid_is_pid(ctx))
|
|
/* Show pid numbers to debugfs users */
|
|
id = (unsigned long)pid_vnr((struct pid *)id);
|
|
|
|
rc = scnprintf(&buf[written], len - written, "%lu ", id);
|
|
if (!rc)
|
|
return -ENOMEM;
|
|
written += rc;
|
|
}
|
|
if (written)
|
|
written -= 1;
|
|
written += scnprintf(&buf[written], len - written, "\n");
|
|
return written;
|
|
}
|
|
|
|
static ssize_t dbgfs_target_ids_read(struct file *file,
|
|
char __user *buf, size_t count, loff_t *ppos)
|
|
{
|
|
struct damon_ctx *ctx = file->private_data;
|
|
ssize_t len;
|
|
char ids_buf[320];
|
|
|
|
mutex_lock(&ctx->kdamond_lock);
|
|
len = sprint_target_ids(ctx, ids_buf, 320);
|
|
mutex_unlock(&ctx->kdamond_lock);
|
|
if (len < 0)
|
|
return len;
|
|
|
|
return simple_read_from_buffer(buf, count, ppos, ids_buf, len);
|
|
}
|
|
|
|
/*
|
|
* Converts a string into an array of unsigned long integers
|
|
*
|
|
* Returns an array of unsigned long integers if the conversion success, or
|
|
* NULL otherwise.
|
|
*/
|
|
static unsigned long *str_to_target_ids(const char *str, ssize_t len,
|
|
ssize_t *nr_ids)
|
|
{
|
|
unsigned long *ids;
|
|
const int max_nr_ids = 32;
|
|
unsigned long id;
|
|
int pos = 0, parsed, ret;
|
|
|
|
*nr_ids = 0;
|
|
ids = kmalloc_array(max_nr_ids, sizeof(id), GFP_KERNEL);
|
|
if (!ids)
|
|
return NULL;
|
|
while (*nr_ids < max_nr_ids && pos < len) {
|
|
ret = sscanf(&str[pos], "%lu%n", &id, &parsed);
|
|
pos += parsed;
|
|
if (ret != 1)
|
|
break;
|
|
ids[*nr_ids] = id;
|
|
*nr_ids += 1;
|
|
}
|
|
|
|
return ids;
|
|
}
|
|
|
|
static void dbgfs_put_pids(unsigned long *ids, int nr_ids)
|
|
{
|
|
int i;
|
|
|
|
for (i = 0; i < nr_ids; i++)
|
|
put_pid((struct pid *)ids[i]);
|
|
}
|
|
|
|
static ssize_t dbgfs_target_ids_write(struct file *file,
|
|
const char __user *buf, size_t count, loff_t *ppos)
|
|
{
|
|
struct damon_ctx *ctx = file->private_data;
|
|
struct damon_target *t, *next_t;
|
|
bool id_is_pid = true;
|
|
char *kbuf;
|
|
unsigned long *targets;
|
|
ssize_t nr_targets;
|
|
ssize_t ret;
|
|
int i;
|
|
|
|
kbuf = user_input_str(buf, count, ppos);
|
|
if (IS_ERR(kbuf))
|
|
return PTR_ERR(kbuf);
|
|
|
|
if (!strncmp(kbuf, "paddr\n", count)) {
|
|
id_is_pid = false;
|
|
/* target id is meaningless here, but we set it just for fun */
|
|
scnprintf(kbuf, count, "42 ");
|
|
}
|
|
|
|
targets = str_to_target_ids(kbuf, count, &nr_targets);
|
|
if (!targets) {
|
|
ret = -ENOMEM;
|
|
goto out;
|
|
}
|
|
|
|
if (id_is_pid) {
|
|
for (i = 0; i < nr_targets; i++) {
|
|
targets[i] = (unsigned long)find_get_pid(
|
|
(int)targets[i]);
|
|
if (!targets[i]) {
|
|
dbgfs_put_pids(targets, i);
|
|
ret = -EINVAL;
|
|
goto free_targets_out;
|
|
}
|
|
}
|
|
}
|
|
|
|
mutex_lock(&ctx->kdamond_lock);
|
|
if (ctx->kdamond) {
|
|
if (id_is_pid)
|
|
dbgfs_put_pids(targets, nr_targets);
|
|
ret = -EBUSY;
|
|
goto unlock_out;
|
|
}
|
|
|
|
/* remove previously set targets */
|
|
damon_for_each_target_safe(t, next_t, ctx) {
|
|
if (targetid_is_pid(ctx))
|
|
put_pid((struct pid *)t->id);
|
|
damon_destroy_target(t);
|
|
}
|
|
|
|
/* remove targets with previously-set primitive */
|
|
damon_set_targets(ctx, NULL, 0);
|
|
|
|
/* Configure the context for the address space type */
|
|
if (id_is_pid)
|
|
damon_va_set_primitives(ctx);
|
|
else
|
|
damon_pa_set_primitives(ctx);
|
|
|
|
ret = damon_set_targets(ctx, targets, nr_targets);
|
|
if (ret) {
|
|
if (id_is_pid)
|
|
dbgfs_put_pids(targets, nr_targets);
|
|
} else {
|
|
ret = count;
|
|
}
|
|
|
|
unlock_out:
|
|
mutex_unlock(&ctx->kdamond_lock);
|
|
free_targets_out:
|
|
kfree(targets);
|
|
out:
|
|
kfree(kbuf);
|
|
return ret;
|
|
}
|
|
|
|
static ssize_t sprint_init_regions(struct damon_ctx *c, char *buf, ssize_t len)
|
|
{
|
|
struct damon_target *t;
|
|
struct damon_region *r;
|
|
int written = 0;
|
|
int rc;
|
|
|
|
damon_for_each_target(t, c) {
|
|
damon_for_each_region(r, t) {
|
|
rc = scnprintf(&buf[written], len - written,
|
|
"%lu %lu %lu\n",
|
|
t->id, r->ar.start, r->ar.end);
|
|
if (!rc)
|
|
return -ENOMEM;
|
|
written += rc;
|
|
}
|
|
}
|
|
return written;
|
|
}
|
|
|
|
static ssize_t dbgfs_init_regions_read(struct file *file, char __user *buf,
|
|
size_t count, loff_t *ppos)
|
|
{
|
|
struct damon_ctx *ctx = file->private_data;
|
|
char *kbuf;
|
|
ssize_t len;
|
|
|
|
kbuf = kmalloc(count, GFP_KERNEL);
|
|
if (!kbuf)
|
|
return -ENOMEM;
|
|
|
|
mutex_lock(&ctx->kdamond_lock);
|
|
if (ctx->kdamond) {
|
|
mutex_unlock(&ctx->kdamond_lock);
|
|
len = -EBUSY;
|
|
goto out;
|
|
}
|
|
|
|
len = sprint_init_regions(ctx, kbuf, count);
|
|
mutex_unlock(&ctx->kdamond_lock);
|
|
if (len < 0)
|
|
goto out;
|
|
len = simple_read_from_buffer(buf, count, ppos, kbuf, len);
|
|
|
|
out:
|
|
kfree(kbuf);
|
|
return len;
|
|
}
|
|
|
|
static int add_init_region(struct damon_ctx *c,
|
|
unsigned long target_id, struct damon_addr_range *ar)
|
|
{
|
|
struct damon_target *t;
|
|
struct damon_region *r, *prev;
|
|
unsigned long id;
|
|
int rc = -EINVAL;
|
|
|
|
if (ar->start >= ar->end)
|
|
return -EINVAL;
|
|
|
|
damon_for_each_target(t, c) {
|
|
id = t->id;
|
|
if (targetid_is_pid(c))
|
|
id = (unsigned long)pid_vnr((struct pid *)id);
|
|
if (id == target_id) {
|
|
r = damon_new_region(ar->start, ar->end);
|
|
if (!r)
|
|
return -ENOMEM;
|
|
damon_add_region(r, t);
|
|
if (damon_nr_regions(t) > 1) {
|
|
prev = damon_prev_region(r);
|
|
if (prev->ar.end > r->ar.start) {
|
|
damon_destroy_region(r, t);
|
|
return -EINVAL;
|
|
}
|
|
}
|
|
rc = 0;
|
|
}
|
|
}
|
|
return rc;
|
|
}
|
|
|
|
static int set_init_regions(struct damon_ctx *c, const char *str, ssize_t len)
|
|
{
|
|
struct damon_target *t;
|
|
struct damon_region *r, *next;
|
|
int pos = 0, parsed, ret;
|
|
unsigned long target_id;
|
|
struct damon_addr_range ar;
|
|
int err;
|
|
|
|
damon_for_each_target(t, c) {
|
|
damon_for_each_region_safe(r, next, t)
|
|
damon_destroy_region(r, t);
|
|
}
|
|
|
|
while (pos < len) {
|
|
ret = sscanf(&str[pos], "%lu %lu %lu%n",
|
|
&target_id, &ar.start, &ar.end, &parsed);
|
|
if (ret != 3)
|
|
break;
|
|
err = add_init_region(c, target_id, &ar);
|
|
if (err)
|
|
goto fail;
|
|
pos += parsed;
|
|
}
|
|
|
|
return 0;
|
|
|
|
fail:
|
|
damon_for_each_target(t, c) {
|
|
damon_for_each_region_safe(r, next, t)
|
|
damon_destroy_region(r, t);
|
|
}
|
|
return err;
|
|
}
|
|
|
|
static ssize_t dbgfs_init_regions_write(struct file *file,
|
|
const char __user *buf, size_t count,
|
|
loff_t *ppos)
|
|
{
|
|
struct damon_ctx *ctx = file->private_data;
|
|
char *kbuf;
|
|
ssize_t ret = count;
|
|
int err;
|
|
|
|
kbuf = user_input_str(buf, count, ppos);
|
|
if (IS_ERR(kbuf))
|
|
return PTR_ERR(kbuf);
|
|
|
|
mutex_lock(&ctx->kdamond_lock);
|
|
if (ctx->kdamond) {
|
|
ret = -EBUSY;
|
|
goto unlock_out;
|
|
}
|
|
|
|
err = set_init_regions(ctx, kbuf, ret);
|
|
if (err)
|
|
ret = err;
|
|
|
|
unlock_out:
|
|
mutex_unlock(&ctx->kdamond_lock);
|
|
kfree(kbuf);
|
|
return ret;
|
|
}
|
|
|
|
static ssize_t dbgfs_kdamond_pid_read(struct file *file,
|
|
char __user *buf, size_t count, loff_t *ppos)
|
|
{
|
|
struct damon_ctx *ctx = file->private_data;
|
|
char *kbuf;
|
|
ssize_t len;
|
|
|
|
kbuf = kmalloc(count, GFP_KERNEL | __GFP_NOWARN);
|
|
if (!kbuf)
|
|
return -ENOMEM;
|
|
|
|
mutex_lock(&ctx->kdamond_lock);
|
|
if (ctx->kdamond)
|
|
len = scnprintf(kbuf, count, "%d\n", ctx->kdamond->pid);
|
|
else
|
|
len = scnprintf(kbuf, count, "none\n");
|
|
mutex_unlock(&ctx->kdamond_lock);
|
|
if (!len)
|
|
goto out;
|
|
len = simple_read_from_buffer(buf, count, ppos, kbuf, len);
|
|
|
|
out:
|
|
kfree(kbuf);
|
|
return len;
|
|
}
|
|
|
|
static int damon_dbgfs_open(struct inode *inode, struct file *file)
|
|
{
|
|
file->private_data = inode->i_private;
|
|
|
|
return nonseekable_open(inode, file);
|
|
}
|
|
|
|
static const struct file_operations attrs_fops = {
|
|
.open = damon_dbgfs_open,
|
|
.read = dbgfs_attrs_read,
|
|
.write = dbgfs_attrs_write,
|
|
};
|
|
|
|
static const struct file_operations schemes_fops = {
|
|
.open = damon_dbgfs_open,
|
|
.read = dbgfs_schemes_read,
|
|
.write = dbgfs_schemes_write,
|
|
};
|
|
|
|
static const struct file_operations target_ids_fops = {
|
|
.open = damon_dbgfs_open,
|
|
.read = dbgfs_target_ids_read,
|
|
.write = dbgfs_target_ids_write,
|
|
};
|
|
|
|
static const struct file_operations init_regions_fops = {
|
|
.open = damon_dbgfs_open,
|
|
.read = dbgfs_init_regions_read,
|
|
.write = dbgfs_init_regions_write,
|
|
};
|
|
|
|
static const struct file_operations kdamond_pid_fops = {
|
|
.open = damon_dbgfs_open,
|
|
.read = dbgfs_kdamond_pid_read,
|
|
};
|
|
|
|
static void dbgfs_fill_ctx_dir(struct dentry *dir, struct damon_ctx *ctx)
|
|
{
|
|
const char * const file_names[] = {"attrs", "schemes", "target_ids",
|
|
"init_regions", "kdamond_pid"};
|
|
const struct file_operations *fops[] = {&attrs_fops, &schemes_fops,
|
|
&target_ids_fops, &init_regions_fops, &kdamond_pid_fops};
|
|
int i;
|
|
|
|
for (i = 0; i < ARRAY_SIZE(file_names); i++)
|
|
debugfs_create_file(file_names[i], 0600, dir, ctx, fops[i]);
|
|
}
|
|
|
|
static void dbgfs_before_terminate(struct damon_ctx *ctx)
|
|
{
|
|
struct damon_target *t, *next;
|
|
|
|
if (!targetid_is_pid(ctx))
|
|
return;
|
|
|
|
mutex_lock(&ctx->kdamond_lock);
|
|
damon_for_each_target_safe(t, next, ctx) {
|
|
put_pid((struct pid *)t->id);
|
|
damon_destroy_target(t);
|
|
}
|
|
mutex_unlock(&ctx->kdamond_lock);
|
|
}
|
|
|
|
static struct damon_ctx *dbgfs_new_ctx(void)
|
|
{
|
|
struct damon_ctx *ctx;
|
|
|
|
ctx = damon_new_ctx();
|
|
if (!ctx)
|
|
return NULL;
|
|
|
|
damon_va_set_primitives(ctx);
|
|
ctx->callback.before_terminate = dbgfs_before_terminate;
|
|
return ctx;
|
|
}
|
|
|
|
static void dbgfs_destroy_ctx(struct damon_ctx *ctx)
|
|
{
|
|
damon_destroy_ctx(ctx);
|
|
}
|
|
|
|
/*
|
|
* Make a context of @name and create a debugfs directory for it.
|
|
*
|
|
* This function should be called while holding damon_dbgfs_lock.
|
|
*
|
|
* Returns 0 on success, negative error code otherwise.
|
|
*/
|
|
static int dbgfs_mk_context(char *name)
|
|
{
|
|
struct dentry *root, **new_dirs, *new_dir;
|
|
struct damon_ctx **new_ctxs, *new_ctx;
|
|
|
|
if (damon_nr_running_ctxs())
|
|
return -EBUSY;
|
|
|
|
new_ctxs = krealloc(dbgfs_ctxs, sizeof(*dbgfs_ctxs) *
|
|
(dbgfs_nr_ctxs + 1), GFP_KERNEL);
|
|
if (!new_ctxs)
|
|
return -ENOMEM;
|
|
dbgfs_ctxs = new_ctxs;
|
|
|
|
new_dirs = krealloc(dbgfs_dirs, sizeof(*dbgfs_dirs) *
|
|
(dbgfs_nr_ctxs + 1), GFP_KERNEL);
|
|
if (!new_dirs)
|
|
return -ENOMEM;
|
|
dbgfs_dirs = new_dirs;
|
|
|
|
root = dbgfs_dirs[0];
|
|
if (!root)
|
|
return -ENOENT;
|
|
|
|
new_dir = debugfs_create_dir(name, root);
|
|
/* Below check is required for a potential duplicated name case */
|
|
if (IS_ERR(new_dir))
|
|
return PTR_ERR(new_dir);
|
|
dbgfs_dirs[dbgfs_nr_ctxs] = new_dir;
|
|
|
|
new_ctx = dbgfs_new_ctx();
|
|
if (!new_ctx) {
|
|
debugfs_remove(new_dir);
|
|
dbgfs_dirs[dbgfs_nr_ctxs] = NULL;
|
|
return -ENOMEM;
|
|
}
|
|
|
|
dbgfs_ctxs[dbgfs_nr_ctxs] = new_ctx;
|
|
dbgfs_fill_ctx_dir(dbgfs_dirs[dbgfs_nr_ctxs],
|
|
dbgfs_ctxs[dbgfs_nr_ctxs]);
|
|
dbgfs_nr_ctxs++;
|
|
|
|
return 0;
|
|
}
|
|
|
|
static ssize_t dbgfs_mk_context_write(struct file *file,
|
|
const char __user *buf, size_t count, loff_t *ppos)
|
|
{
|
|
char *kbuf;
|
|
char *ctx_name;
|
|
ssize_t ret;
|
|
|
|
kbuf = user_input_str(buf, count, ppos);
|
|
if (IS_ERR(kbuf))
|
|
return PTR_ERR(kbuf);
|
|
ctx_name = kmalloc(count + 1, GFP_KERNEL);
|
|
if (!ctx_name) {
|
|
kfree(kbuf);
|
|
return -ENOMEM;
|
|
}
|
|
|
|
/* Trim white space */
|
|
if (sscanf(kbuf, "%s", ctx_name) != 1) {
|
|
ret = -EINVAL;
|
|
goto out;
|
|
}
|
|
|
|
mutex_lock(&damon_dbgfs_lock);
|
|
ret = dbgfs_mk_context(ctx_name);
|
|
if (!ret)
|
|
ret = count;
|
|
mutex_unlock(&damon_dbgfs_lock);
|
|
|
|
out:
|
|
kfree(kbuf);
|
|
kfree(ctx_name);
|
|
return ret;
|
|
}
|
|
|
|
/*
|
|
* Remove a context of @name and its debugfs directory.
|
|
*
|
|
* This function should be called while holding damon_dbgfs_lock.
|
|
*
|
|
* Return 0 on success, negative error code otherwise.
|
|
*/
|
|
static int dbgfs_rm_context(char *name)
|
|
{
|
|
struct dentry *root, *dir, **new_dirs;
|
|
struct inode *inode;
|
|
struct damon_ctx **new_ctxs;
|
|
int i, j;
|
|
int ret = 0;
|
|
|
|
if (damon_nr_running_ctxs())
|
|
return -EBUSY;
|
|
|
|
root = dbgfs_dirs[0];
|
|
if (!root)
|
|
return -ENOENT;
|
|
|
|
dir = debugfs_lookup(name, root);
|
|
if (!dir)
|
|
return -ENOENT;
|
|
|
|
inode = d_inode(dir);
|
|
if (!S_ISDIR(inode->i_mode)) {
|
|
ret = -EINVAL;
|
|
goto out_dput;
|
|
}
|
|
|
|
new_dirs = kmalloc_array(dbgfs_nr_ctxs - 1, sizeof(*dbgfs_dirs),
|
|
GFP_KERNEL);
|
|
if (!new_dirs) {
|
|
ret = -ENOMEM;
|
|
goto out_dput;
|
|
}
|
|
|
|
new_ctxs = kmalloc_array(dbgfs_nr_ctxs - 1, sizeof(*dbgfs_ctxs),
|
|
GFP_KERNEL);
|
|
if (!new_ctxs) {
|
|
ret = -ENOMEM;
|
|
goto out_new_dirs;
|
|
}
|
|
|
|
for (i = 0, j = 0; i < dbgfs_nr_ctxs; i++) {
|
|
if (dbgfs_dirs[i] == dir) {
|
|
debugfs_remove(dbgfs_dirs[i]);
|
|
dbgfs_destroy_ctx(dbgfs_ctxs[i]);
|
|
continue;
|
|
}
|
|
new_dirs[j] = dbgfs_dirs[i];
|
|
new_ctxs[j++] = dbgfs_ctxs[i];
|
|
}
|
|
|
|
kfree(dbgfs_dirs);
|
|
kfree(dbgfs_ctxs);
|
|
|
|
dbgfs_dirs = new_dirs;
|
|
dbgfs_ctxs = new_ctxs;
|
|
dbgfs_nr_ctxs--;
|
|
|
|
goto out_dput;
|
|
|
|
out_new_dirs:
|
|
kfree(new_dirs);
|
|
out_dput:
|
|
dput(dir);
|
|
return ret;
|
|
}
|
|
|
|
static ssize_t dbgfs_rm_context_write(struct file *file,
|
|
const char __user *buf, size_t count, loff_t *ppos)
|
|
{
|
|
char *kbuf;
|
|
ssize_t ret;
|
|
char *ctx_name;
|
|
|
|
kbuf = user_input_str(buf, count, ppos);
|
|
if (IS_ERR(kbuf))
|
|
return PTR_ERR(kbuf);
|
|
ctx_name = kmalloc(count + 1, GFP_KERNEL);
|
|
if (!ctx_name) {
|
|
kfree(kbuf);
|
|
return -ENOMEM;
|
|
}
|
|
|
|
/* Trim white space */
|
|
if (sscanf(kbuf, "%s", ctx_name) != 1) {
|
|
ret = -EINVAL;
|
|
goto out;
|
|
}
|
|
|
|
mutex_lock(&damon_dbgfs_lock);
|
|
ret = dbgfs_rm_context(ctx_name);
|
|
if (!ret)
|
|
ret = count;
|
|
mutex_unlock(&damon_dbgfs_lock);
|
|
|
|
out:
|
|
kfree(kbuf);
|
|
kfree(ctx_name);
|
|
return ret;
|
|
}
|
|
|
|
static ssize_t dbgfs_monitor_on_read(struct file *file,
|
|
char __user *buf, size_t count, loff_t *ppos)
|
|
{
|
|
char monitor_on_buf[5];
|
|
bool monitor_on = damon_nr_running_ctxs() != 0;
|
|
int len;
|
|
|
|
len = scnprintf(monitor_on_buf, 5, monitor_on ? "on\n" : "off\n");
|
|
|
|
return simple_read_from_buffer(buf, count, ppos, monitor_on_buf, len);
|
|
}
|
|
|
|
static ssize_t dbgfs_monitor_on_write(struct file *file,
|
|
const char __user *buf, size_t count, loff_t *ppos)
|
|
{
|
|
ssize_t ret;
|
|
char *kbuf;
|
|
|
|
kbuf = user_input_str(buf, count, ppos);
|
|
if (IS_ERR(kbuf))
|
|
return PTR_ERR(kbuf);
|
|
|
|
/* Remove white space */
|
|
if (sscanf(kbuf, "%s", kbuf) != 1) {
|
|
kfree(kbuf);
|
|
return -EINVAL;
|
|
}
|
|
|
|
mutex_lock(&damon_dbgfs_lock);
|
|
if (!strncmp(kbuf, "on", count)) {
|
|
int i;
|
|
|
|
for (i = 0; i < dbgfs_nr_ctxs; i++) {
|
|
if (damon_targets_empty(dbgfs_ctxs[i])) {
|
|
kfree(kbuf);
|
|
mutex_unlock(&damon_dbgfs_lock);
|
|
return -EINVAL;
|
|
}
|
|
}
|
|
ret = damon_start(dbgfs_ctxs, dbgfs_nr_ctxs);
|
|
} else if (!strncmp(kbuf, "off", count)) {
|
|
ret = damon_stop(dbgfs_ctxs, dbgfs_nr_ctxs);
|
|
} else {
|
|
ret = -EINVAL;
|
|
}
|
|
mutex_unlock(&damon_dbgfs_lock);
|
|
|
|
if (!ret)
|
|
ret = count;
|
|
kfree(kbuf);
|
|
return ret;
|
|
}
|
|
|
|
static const struct file_operations mk_contexts_fops = {
|
|
.write = dbgfs_mk_context_write,
|
|
};
|
|
|
|
static const struct file_operations rm_contexts_fops = {
|
|
.write = dbgfs_rm_context_write,
|
|
};
|
|
|
|
static const struct file_operations monitor_on_fops = {
|
|
.read = dbgfs_monitor_on_read,
|
|
.write = dbgfs_monitor_on_write,
|
|
};
|
|
|
|
static int __init __damon_dbgfs_init(void)
|
|
{
|
|
struct dentry *dbgfs_root;
|
|
const char * const file_names[] = {"mk_contexts", "rm_contexts",
|
|
"monitor_on"};
|
|
const struct file_operations *fops[] = {&mk_contexts_fops,
|
|
&rm_contexts_fops, &monitor_on_fops};
|
|
int i;
|
|
|
|
dbgfs_root = debugfs_create_dir("damon", NULL);
|
|
|
|
for (i = 0; i < ARRAY_SIZE(file_names); i++)
|
|
debugfs_create_file(file_names[i], 0600, dbgfs_root, NULL,
|
|
fops[i]);
|
|
dbgfs_fill_ctx_dir(dbgfs_root, dbgfs_ctxs[0]);
|
|
|
|
dbgfs_dirs = kmalloc_array(1, sizeof(dbgfs_root), GFP_KERNEL);
|
|
if (!dbgfs_dirs) {
|
|
debugfs_remove(dbgfs_root);
|
|
return -ENOMEM;
|
|
}
|
|
dbgfs_dirs[0] = dbgfs_root;
|
|
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* Functions for the initialization
|
|
*/
|
|
|
|
static int __init damon_dbgfs_init(void)
|
|
{
|
|
int rc = -ENOMEM;
|
|
|
|
mutex_lock(&damon_dbgfs_lock);
|
|
dbgfs_ctxs = kmalloc(sizeof(*dbgfs_ctxs), GFP_KERNEL);
|
|
if (!dbgfs_ctxs)
|
|
goto out;
|
|
dbgfs_ctxs[0] = dbgfs_new_ctx();
|
|
if (!dbgfs_ctxs[0]) {
|
|
kfree(dbgfs_ctxs);
|
|
goto out;
|
|
}
|
|
dbgfs_nr_ctxs = 1;
|
|
|
|
rc = __damon_dbgfs_init();
|
|
if (rc) {
|
|
kfree(dbgfs_ctxs[0]);
|
|
kfree(dbgfs_ctxs);
|
|
pr_err("%s: dbgfs init failed\n", __func__);
|
|
}
|
|
|
|
out:
|
|
mutex_unlock(&damon_dbgfs_lock);
|
|
return rc;
|
|
}
|
|
|
|
module_init(damon_dbgfs_init);
|
|
|
|
#include "dbgfs-test.h"
|