18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (c) 2023 Huawei Technologies Co., Ltd. 48c2ecf20Sopenharmony_ci */ 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ci#include <linux/fs.h> 78c2ecf20Sopenharmony_ci#include <linux/proc_fs.h> 88c2ecf20Sopenharmony_ci#include <linux/seq_file.h> 98c2ecf20Sopenharmony_ci#include <linux/fdtable.h> 108c2ecf20Sopenharmony_ci#include <linux/sched/task.h> 118c2ecf20Sopenharmony_ci#include <linux/sched/signal.h> 128c2ecf20Sopenharmony_ci#include "../drivers/staging/android/ashmem.h" 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci#define PURGEABLE_ASHMEM_SHRINKALL_ARG 0 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_cistruct purgeable_ashmem_trigger_args { 178c2ecf20Sopenharmony_ci struct seq_file *seq; 188c2ecf20Sopenharmony_ci struct task_struct *tsk; 198c2ecf20Sopenharmony_ci}; 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_cistatic int purgeable_ashmem_trigger_cb(const void *data, 228c2ecf20Sopenharmony_ci struct file *f, unsigned int fd) 238c2ecf20Sopenharmony_ci{ 248c2ecf20Sopenharmony_ci const struct purgeable_ashmem_trigger_args *args = data; 258c2ecf20Sopenharmony_ci struct task_struct *tsk = args->tsk; 268c2ecf20Sopenharmony_ci struct purgeable_ashmem_metadata pmdata; 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci if (!is_ashmem_file(f)) 298c2ecf20Sopenharmony_ci return 0; 308c2ecf20Sopenharmony_ci if (!get_purgeable_ashmem_metadata(f, &pmdata)) 318c2ecf20Sopenharmony_ci return 0; 328c2ecf20Sopenharmony_ci if (pmdata.is_purgeable) { 338c2ecf20Sopenharmony_ci pmdata.name = pmdata.name == NULL ? "" : pmdata.name; 348c2ecf20Sopenharmony_ci seq_printf(args->seq, 358c2ecf20Sopenharmony_ci "%s,%u,%u,%ld,%s,%zu,%u,%u,%d,%d\n", 368c2ecf20Sopenharmony_ci tsk->comm, tsk->pid, fd, (long)tsk->signal->oom_score_adj, 378c2ecf20Sopenharmony_ci pmdata.name, pmdata.size, pmdata.id, pmdata.create_time, 388c2ecf20Sopenharmony_ci pmdata.refc, pmdata.purged); 398c2ecf20Sopenharmony_ci } 408c2ecf20Sopenharmony_ci return 0; 418c2ecf20Sopenharmony_ci} 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_cistatic ssize_t purgeable_ashmem_trigger_write(struct file *file, 448c2ecf20Sopenharmony_ci const char __user *buffer, size_t count, loff_t *ppos) 458c2ecf20Sopenharmony_ci{ 468c2ecf20Sopenharmony_ci char *buf; 478c2ecf20Sopenharmony_ci unsigned int ashmem_id = 0; 488c2ecf20Sopenharmony_ci unsigned int create_time = 0; 498c2ecf20Sopenharmony_ci const unsigned int params_num = 2; 508c2ecf20Sopenharmony_ci const struct cred *cred = current_cred(); 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci if (!cred) 538c2ecf20Sopenharmony_ci return 0; 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci if (!uid_eq(cred->euid, GLOBAL_MEMMGR_UID) && 568c2ecf20Sopenharmony_ci !uid_eq(cred->euid, GLOBAL_ROOT_UID)) { 578c2ecf20Sopenharmony_ci pr_err("no permission to shrink purgeable ashmem!\n"); 588c2ecf20Sopenharmony_ci return 0; 598c2ecf20Sopenharmony_ci } 608c2ecf20Sopenharmony_ci buf = memdup_user_nul(buffer, count); 618c2ecf20Sopenharmony_ci buf = strstrip(buf); 628c2ecf20Sopenharmony_ci if (sscanf(buf, "%u %u", &ashmem_id, &create_time) != params_num) 638c2ecf20Sopenharmony_ci return -EINVAL; 648c2ecf20Sopenharmony_ci if (ashmem_id == PURGEABLE_ASHMEM_SHRINKALL_ARG && 658c2ecf20Sopenharmony_ci create_time == PURGEABLE_ASHMEM_SHRINKALL_ARG) 668c2ecf20Sopenharmony_ci ashmem_shrinkall(); 678c2ecf20Sopenharmony_ci else 688c2ecf20Sopenharmony_ci ashmem_shrink_by_id(ashmem_id, create_time); 698c2ecf20Sopenharmony_ci return count; 708c2ecf20Sopenharmony_ci} 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_cistatic int purgeable_ashmem_trigger_show(struct seq_file *s, void *d) 738c2ecf20Sopenharmony_ci{ 748c2ecf20Sopenharmony_ci struct task_struct *tsk = NULL; 758c2ecf20Sopenharmony_ci struct purgeable_ashmem_trigger_args cb_args; 768c2ecf20Sopenharmony_ci const struct cred *cred = current_cred(); 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci if (!cred) 798c2ecf20Sopenharmony_ci return -EINVAL; 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci if (!uid_eq(cred->euid, GLOBAL_MEMMGR_UID) && 828c2ecf20Sopenharmony_ci !uid_eq(cred->euid, GLOBAL_ROOT_UID)) { 838c2ecf20Sopenharmony_ci pr_err("no permission to shrink purgeable ashmem!\n"); 848c2ecf20Sopenharmony_ci return -EINVAL; 858c2ecf20Sopenharmony_ci } 868c2ecf20Sopenharmony_ci seq_puts(s, "Process purgeable ashmem detail info:\n"); 878c2ecf20Sopenharmony_ci seq_puts(s, "----------------------------------------------------\n"); 888c2ecf20Sopenharmony_ci seq_printf(s, "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n", 898c2ecf20Sopenharmony_ci "process_name", "pid", "adj", "fd", 908c2ecf20Sopenharmony_ci "ashmem_name", "size", "id", "time", "ref_count", "purged"); 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci ashmem_mutex_lock(); 938c2ecf20Sopenharmony_ci rcu_read_lock(); 948c2ecf20Sopenharmony_ci for_each_process(tsk) { 958c2ecf20Sopenharmony_ci if (tsk->flags & PF_KTHREAD) 968c2ecf20Sopenharmony_ci continue; 978c2ecf20Sopenharmony_ci cb_args.seq = s; 988c2ecf20Sopenharmony_ci cb_args.tsk = tsk; 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ci task_lock(tsk); 1018c2ecf20Sopenharmony_ci iterate_fd(tsk->files, 0, 1028c2ecf20Sopenharmony_ci purgeable_ashmem_trigger_cb, (void *)&cb_args); 1038c2ecf20Sopenharmony_ci task_unlock(tsk); 1048c2ecf20Sopenharmony_ci } 1058c2ecf20Sopenharmony_ci rcu_read_unlock(); 1068c2ecf20Sopenharmony_ci ashmem_mutex_unlock(); 1078c2ecf20Sopenharmony_ci seq_puts(s, "----------------------------------------------------\n"); 1088c2ecf20Sopenharmony_ci return 0; 1098c2ecf20Sopenharmony_ci} 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_cistatic int purgeable_ashmem_trigger_open(struct inode *inode, 1128c2ecf20Sopenharmony_ci struct file *file) 1138c2ecf20Sopenharmony_ci{ 1148c2ecf20Sopenharmony_ci return single_open(file, purgeable_ashmem_trigger_show, 1158c2ecf20Sopenharmony_ci inode->i_private); 1168c2ecf20Sopenharmony_ci} 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_cistatic const struct proc_ops purgeable_ashmem_trigger_fops = { 1198c2ecf20Sopenharmony_ci .proc_open = purgeable_ashmem_trigger_open, 1208c2ecf20Sopenharmony_ci .proc_write = purgeable_ashmem_trigger_write, 1218c2ecf20Sopenharmony_ci .proc_read = seq_read, 1228c2ecf20Sopenharmony_ci .proc_lseek = seq_lseek, 1238c2ecf20Sopenharmony_ci .proc_release = single_release, 1248c2ecf20Sopenharmony_ci}; 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_civoid init_purgeable_ashmem_trigger(void) 1278c2ecf20Sopenharmony_ci{ 1288c2ecf20Sopenharmony_ci struct proc_dir_entry *entry = NULL; 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ci entry = proc_create_data("purgeable_ashmem_trigger", 0666, 1318c2ecf20Sopenharmony_ci NULL, &purgeable_ashmem_trigger_fops, NULL); 1328c2ecf20Sopenharmony_ci if (!entry) 1338c2ecf20Sopenharmony_ci pr_err("Failed to create purgeable ashmem trigger\n"); 1348c2ecf20Sopenharmony_ci} 135