Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp2610565pxa; Mon, 17 Aug 2020 14:17:16 -0700 (PDT) X-Google-Smtp-Source: ABdhPJygvwYCaz9x48g3ia6XK9sDM1Lyavlc7X/3ENff1g/IP1d4UyQcG2P5R3YfRFWwoIveS8do X-Received: by 2002:a17:906:25c1:: with SMTP id n1mr12081948ejb.463.1597699036377; Mon, 17 Aug 2020 14:17:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597699036; cv=none; d=google.com; s=arc-20160816; b=n+cBFsl5WqLsgP9ePGvcja1vD/7/GU4nzZvJnrzAHB1CNEuEcPta17m9brYS0g/147 JvHF2jrUyG/skpQbgNZAwh3NLSDNYYn1C1VP8PFwSOB7XOaRXWDbFCy1DHpblSo3Bwjm YZermvjDJdbRQgLRVMe3kdvTpeU6QPLtk9obkzfD6YIlN9WGitP2abmZzmgKb/2qki1P V7YZO0P9ueXGTA7AEFEJJhe/c7kQbvMVPWNJQuIHYJ0q4FSXuSWl1EEpAgE6U81YwPzQ DMWFqKjFSN6ZtBpLgBdamfZCTCvGMTcZZeg/AkCiGV5wRoCUm9Bt9+1MYJscwMYzQScn hEVw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=7rpL6/EB1+EwHbaZQMk8wUHt9s1YSLWhla0Ecu2Wfro=; b=a5cJBVt/G1gTTeMNiE30jcQpjTU8VzyGlJEOqQ+mA0gqITG3zdkcrxHmavuWKEVHOd 6BxT9fMyRb5b4DGB164q8xzQhqsNl9zSWCIlQ9lsn90ccuB73N6CsRGHEmI1ImGaTtyR LYmNMYUN2FJOH6R6IeyLjRz1eveJ/d6RliX9IrS1gRFhgdJLJdAlOw7rI0A44YWl6a8S fJiyIzBZibR4IHNvhbdzQuLkM0b7gqcsrLOehxdXDVgL01YlRWJpF4MMRwQrcry68GOu 8Q9wayOfzcJjXlrvZ1PYUY5LJ4CH0ZHt68LNNqib4xH2CkzQcCihY5iw+krO56D54sWm VfQA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=iPSrUHrf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a20si15614072edj.133.2020.08.17.14.16.52; Mon, 17 Aug 2020 14:17:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=iPSrUHrf; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728987AbgHQOKX (ORCPT + 99 others); Mon, 17 Aug 2020 10:10:23 -0400 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:41501 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1728924AbgHQOKB (ORCPT ); Mon, 17 Aug 2020 10:10:01 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1597673399; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:in-reply-to:in-reply-to:references:references; bh=7rpL6/EB1+EwHbaZQMk8wUHt9s1YSLWhla0Ecu2Wfro=; b=iPSrUHrfCMJwLSA4wHU5w1GlxNkpBt/F7SmDlE3YpZU78Os7KDQ3r3BP2VgqHDPSRcKGJc +FVc7kb0WutGCSsf6ZT9v6pfhcJbCZh3G66Ct3UXndHozmN94GJtSZr6kvUpqNZou/sXK2 YDsirTB7ZLFsq5jkbbNMsE/ObkBS+4g= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-179-7Pue4gy4ORSIsEQu0cavkw-1; Mon, 17 Aug 2020 10:09:57 -0400 X-MC-Unique: 7Pue4gy4ORSIsEQu0cavkw-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 8C45A425D4; Mon, 17 Aug 2020 14:09:55 +0000 (UTC) Received: from llong.com (ovpn-118-35.rdu2.redhat.com [10.10.118.35]) by smtp.corp.redhat.com (Postfix) with ESMTP id C792421E90; Mon, 17 Aug 2020 14:09:53 +0000 (UTC) From: Waiman Long To: Andrew Morton , Johannes Weiner , Michal Hocko , Vladimir Davydov , Jonathan Corbet , Alexey Dobriyan , Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot Cc: linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-fsdevel@vger.kernel.org, cgroups@vger.kernel.org, linux-mm@kvack.org, Waiman Long Subject: [RFC PATCH 4/8] fs/proc: Support a new procfs memctl file Date: Mon, 17 Aug 2020 10:08:27 -0400 Message-Id: <20200817140831.30260-5-longman@redhat.com> In-Reply-To: <20200817140831.30260-1-longman@redhat.com> References: <20200817140831.30260-1-longman@redhat.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org To allow system administrators to view and modify the over-high action settings of a running application, a new /proc//memctl file is now added to show the over-high action parameters as well as allowing their modification. Signed-off-by: Waiman Long --- fs/proc/base.c | 109 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) diff --git a/fs/proc/base.c b/fs/proc/base.c index 617db4e0faa0..3c9349ad1e37 100644 --- a/fs/proc/base.c +++ b/fs/proc/base.c @@ -88,6 +88,8 @@ #include #include #include +#include +#include #include #include #include @@ -3145,6 +3147,107 @@ static int proc_stack_depth(struct seq_file *m, struct pid_namespace *ns, } #endif /* CONFIG_STACKLEAK_METRICS */ +#ifdef CONFIG_MEMCG +/* + * Memory cgroup control parameters + * + */ +static ssize_t proc_memctl_read(struct file *file, char __user *buf, + size_t count, loff_t *ppos) +{ + struct task_struct *task = get_proc_task(file_inode(file)); + unsigned long action, limit1, limit2; + char buffer[80]; + ssize_t len; + + if (!task) + return -ESRCH; + + action = task->memcg_over_high_action | + (task->memcg_over_high_signal << PR_MEMACT_SIG_SHIFT) | + (task->memcg_over_high_flags << PR_MEMACT_FLG_SHIFT); + limit1 = (unsigned long)task->memcg_over_high_climit * PAGE_SIZE; + limit2 = (unsigned long)task->memcg_over_high_plimit * PAGE_SIZE; + + put_task_struct(task); + len = snprintf(buffer, sizeof(buffer), "%ld %ld %ld\n", + action, limit1, limit2); + return simple_read_from_buffer(buf, count, ppos, buffer, len); +} + +static ssize_t proc_memctl_write(struct file *file, const char __user *buf, + size_t count, loff_t *offs) +{ + struct task_struct *task = get_proc_task(file_inode(file)); + unsigned long vals[3]; + char buffer[80]; + char *ptr, *next; + int i, err; + unsigned int action, signal, flags; + + if (!task) + return -ESRCH; + if (count > sizeof(buffer) - 1) + count = sizeof(buffer) - 1; + if (copy_from_user(buffer, buf, count)) { + err = -EFAULT; + goto out; + } + buffer[count] = '\0'; + next = buffer; + + /* + * Expect to find 3 numbers + */ + for (i = 0, ptr = buffer; i < 3; i++) { + ptr = skip_spaces(next); + if (!*ptr) { + err = -EINVAL; + goto out; + } + + /* Skip non-space characters for next */ + for (next = ptr; *next && !isspace(*next); next++) + ; + if (isspace(*next)) + *next++ = '\0'; + + err = kstrtoul(ptr, 0, &vals[i]); + if (err) + break; + } + action = vals[0] & PR_MEMACT_MASK; + signal = (vals[0] >> PR_MEMACT_SIG_SHIFT) & PR_MEMACT_MASK; + flags = vals[0] >> PR_MEMACT_FLG_SHIFT; + + /* Round up limits to number of pages */ + vals[1] = DIV_ROUND_UP(vals[1], PAGE_SIZE); + vals[2] = DIV_ROUND_UP(vals[2], PAGE_SIZE); + + /* Check input values */ + if ((action > PR_MEMACT_MAX) || (signal >= _NSIG) || + (flags & ~PR_MEMFLAG_MASK)) { + err = -EINVAL; + goto out; + } + + WRITE_ONCE(task->memcg_over_high_action, action); + WRITE_ONCE(task->memcg_over_high_signal, signal); + WRITE_ONCE(task->memcg_over_high_flags, flags); + WRITE_ONCE(task->memcg_over_high_climit, vals[1]); + WRITE_ONCE(task->memcg_over_high_plimit, vals[2]); +out: + put_task_struct(task); + return err < 0 ? err : count; +} + +const struct file_operations proc_memctl_operations = { + .read = proc_memctl_read, + .write = proc_memctl_write, + .llseek = generic_file_llseek, +}; +#endif /* CONFIG_MEMCG */ + /* * Thread groups */ @@ -3258,6 +3361,9 @@ static const struct pid_entry tgid_base_stuff[] = { #ifdef CONFIG_PROC_PID_ARCH_STATUS ONE("arch_status", S_IRUGO, proc_pid_arch_status), #endif +#ifdef CONFIG_MEMCG + REG("memctl", 0644, proc_memctl_operations), +#endif }; static int proc_tgid_base_readdir(struct file *file, struct dir_context *ctx) @@ -3587,6 +3693,9 @@ static const struct pid_entry tid_base_stuff[] = { #ifdef CONFIG_PROC_PID_ARCH_STATUS ONE("arch_status", S_IRUGO, proc_pid_arch_status), #endif +#ifdef CONFIG_MEMCG + REG("memctl", 0644, proc_memctl_operations), +#endif }; static int proc_tid_base_readdir(struct file *file, struct dir_context *ctx) -- 2.18.1