From patchwork Sat Aug 6 12:23:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Helge Deller X-Patchwork-Id: 12938013 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 8D499C19F2D for ; Sat, 6 Aug 2022 12:25:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:To:From:Reply-To:Cc:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=PJECBi1g+lvztwxVKF1BCyPo+0SBHxqTgbkfPt9TsXY=; b=A7GU8n2Hi+iUqv Y268xiMyUFSLdylbE5iMdTKVRCoxGxRaljNREfZA2t8b84YfIOltShs9ErhLSweO6T0ZlLgn+Ql65 U6C/FJ6cPdepw/meDaBWM1Zws+ENfYzHbWFydrC96JgkVoMMS47QY3jSujlm6JmBWfO87r/O4c8h5 zP67dG3K+AKZbg70osqxPcJ4KexhcG9qKbhsqsmmOMrMX0ZGPrXP/0ovhT4F3TaM6v9uLnO+Tllx3 umdw/OlAHWYqBaBAUDYGCgcEU4glcT0CJyIABudhmk0H2kW7ofRmRyxXeOZ/doVtWSJ43NAo6JmRH XA2FCNLuxQnEHJk1dokA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oKIqi-00AuQ1-Bx; Sat, 06 Aug 2022 12:24:12 +0000 Received: from mout.gmx.net ([212.227.15.19]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1oKIqV-00AteZ-Kb for linux-arm-kernel@lists.infradead.org; Sat, 06 Aug 2022 12:24:02 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1659788630; bh=EmdUhqjuBaZoiK7nIBlB85Ojty7fJP5lc36LTjVWwVI=; h=X-UI-Sender-Class:From:To:Subject:Date:In-Reply-To:References; b=PPFrNoZ3mW1wYAYBzxqTIHuPp7ljFxHiGSxS6WoYP3mu1RFO4RUsDrm3XmC9Dvs8F t86wmDY5iBi+l/Te5FcTUsGhRwdYfsQsqtlE8Kkmf+N7qqpQ9Gi/jyOp9pUzyb5JrE +iwLmbo+qNpj5y9IbkbnE1dACSlBXJUabP53vok8= X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from p100.fritz.box ([92.116.170.46]) by mail.gmx.net (mrgmx004 [212.227.17.190]) with ESMTPSA (Nemesis) id 1MFsZ3-1oCKlN2yp5-00HPpY; Sat, 06 Aug 2022 14:23:50 +0200 From: Helge Deller To: linux-s390@vger.kernel.org, linux-parisc@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org, x86@kernel.org, linux-arm-kernel@lists.infradead.org, Josh Triplett , linux-fsdevel@vger.kernel.org Subject: [PATCH v2 1/3] proc: Add get_task_cmdline_kernel() function Date: Sat, 6 Aug 2022 14:23:46 +0200 Message-Id: <20220806122348.82584-2-deller@gmx.de> X-Mailer: git-send-email 2.37.1 In-Reply-To: <20220806122348.82584-1-deller@gmx.de> References: <20220806122348.82584-1-deller@gmx.de> MIME-Version: 1.0 X-Provags-ID: V03:K1:KkbEYe0kghWlFwQzjNuZb5/phQrAfDs2/UGiW7nK7hjXneYtmzA Tcmc4lEn26oFet83nGaNCsUOemp/lO74ocP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GrWT/HyxbkBl5+nCcjt8O+wFeU5Q/fBRjdQUXmpxXFB2Rn672uDoxTifP/mS6F4pUXq 5vmVbbs3Kt0UoxGCn5a3LgP0cvfZGA37hG3ZIefnlVRVKPuqeaSwWc6OeMMgmPR2Kgym8rzHg y4voZYaAYS9OURChdJsr3/B/qtlS7usEICg5My2dYfFDahdZ1V6RTLpXRxpzv7/RmZaFpB8rP ihSIfoKxSxnHFUZyZ9kxtDYWQIROhmUgNbMNtfo7lTWQh9UixCfuapEQQ== X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220806_052400_008947_3F18480B X-CRM114-Status: GOOD ( 23.67 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Add a new function get_task_cmdline_kernel() which reads the command line of a process into a kernel buffer. This command line can then be dumped by arch code to give additional debug info via the parameters with which a faulting process was started. The new function re-uses the existing code which provides the cmdline for the procfs. For that the existing functions were modified so that the buffer page is allocated outside of get_mm_proctitle() and get_mm_cmdline() and instead provided as parameter. Signed-off-by: Helge Deller --- fs/proc/base.c | 68 +++++++++++++++++++++++++++-------------- include/linux/proc_fs.h | 5 +++ 2 files changed, 50 insertions(+), 23 deletions(-) -- 2.37.1 diff --git a/fs/proc/base.c b/fs/proc/base.c index 8dfa36a99c74..4da9a8b3c7d1 100644 --- a/fs/proc/base.c +++ b/fs/proc/base.c @@ -217,20 +217,17 @@ static int proc_root_link(struct dentry *dentry, struct path *path) */ static ssize_t get_mm_proctitle(struct mm_struct *mm, char __user *buf, size_t count, unsigned long pos, - unsigned long arg_start) + unsigned long arg_start, char *page) { - char *page; int ret, got; + size_t size; - if (pos >= PAGE_SIZE) + size = min_t(size_t, PAGE_SIZE, count); + if (pos >= size) return 0; - page = (char *)__get_free_page(GFP_KERNEL); - if (!page) - return -ENOMEM; - ret = 0; - got = access_remote_vm(mm, arg_start, page, PAGE_SIZE, FOLL_ANON); + got = access_remote_vm(mm, arg_start, page, size, FOLL_ANON); if (got > 0) { int len = strnlen(page, got); @@ -238,7 +235,9 @@ static ssize_t get_mm_proctitle(struct mm_struct *mm, char __user *buf, if (len < got) len++; - if (len > pos) { + if (!buf) + ret = len; + else if (len > pos) { len -= pos; if (len > count) len = count; @@ -248,16 +247,15 @@ static ssize_t get_mm_proctitle(struct mm_struct *mm, char __user *buf, ret = len; } } - free_page((unsigned long)page); return ret; } static ssize_t get_mm_cmdline(struct mm_struct *mm, char __user *buf, - size_t count, loff_t *ppos) + size_t count, loff_t *ppos, char *page) { unsigned long arg_start, arg_end, env_start, env_end; unsigned long pos, len; - char *page, c; + char c; /* Check if process spawned far enough to have cmdline. */ if (!mm->env_end) @@ -283,7 +281,7 @@ static ssize_t get_mm_cmdline(struct mm_struct *mm, char __user *buf, len = env_end - arg_start; /* We're not going to care if "*ppos" has high bits set */ - pos = *ppos; + pos = ppos ? *ppos : 0; if (pos >= len) return 0; if (count > len - pos) @@ -299,7 +297,7 @@ static ssize_t get_mm_cmdline(struct mm_struct *mm, char __user *buf, * pos is 0, and set a flag in the 'struct file'. */ if (access_remote_vm(mm, arg_end-1, &c, 1, FOLL_ANON) == 1 && c) - return get_mm_proctitle(mm, buf, count, pos, arg_start); + return get_mm_proctitle(mm, buf, count, pos, arg_start, page); /* * For the non-setproctitle() case we limit things strictly @@ -311,10 +309,6 @@ static ssize_t get_mm_cmdline(struct mm_struct *mm, char __user *buf, if (count > arg_end - pos) count = arg_end - pos; - page = (char *)__get_free_page(GFP_KERNEL); - if (!page) - return -ENOMEM; - len = 0; while (count) { int got; @@ -323,7 +317,8 @@ static ssize_t get_mm_cmdline(struct mm_struct *mm, char __user *buf, got = access_remote_vm(mm, pos, page, size, FOLL_ANON); if (got <= 0) break; - got -= copy_to_user(buf, page, got); + if (buf) + got -= copy_to_user(buf, page, got); if (unlikely(!got)) { if (!len) len = -EFAULT; @@ -335,12 +330,11 @@ static ssize_t get_mm_cmdline(struct mm_struct *mm, char __user *buf, count -= got; } - free_page((unsigned long)page); return len; } static ssize_t get_task_cmdline(struct task_struct *tsk, char __user *buf, - size_t count, loff_t *pos) + size_t count, loff_t *pos, char *page) { struct mm_struct *mm; ssize_t ret; @@ -349,23 +343,51 @@ static ssize_t get_task_cmdline(struct task_struct *tsk, char __user *buf, if (!mm) return 0; - ret = get_mm_cmdline(mm, buf, count, pos); + ret = get_mm_cmdline(mm, buf, count, pos, page); mmput(mm); return ret; } +/* + * Place up to maxcount chars of the command line of the process into the + * cmdline buffer. + */ +void get_task_cmdline_kernel(struct task_struct *tsk, + char *cmdline, size_t maxcount) +{ + int i; + + memset(cmdline, 0, maxcount); + get_task_cmdline(tsk, NULL, maxcount - 1, NULL, cmdline); + + /* remove NULs between parameters */ + for (i = 0; i < maxcount - 2; i++) { + if (cmdline[i]) + continue; + if (cmdline[i+1] == 0) + break; + cmdline[i] = ' '; + } +} + static ssize_t proc_pid_cmdline_read(struct file *file, char __user *buf, size_t count, loff_t *pos) { struct task_struct *tsk; ssize_t ret; + char *page; BUG_ON(*pos < 0); tsk = get_proc_task(file_inode(file)); if (!tsk) return -ESRCH; - ret = get_task_cmdline(tsk, buf, count, pos); + page = (char *)__get_free_page(GFP_KERNEL); + if (page) { + ret = get_task_cmdline(tsk, buf, count, pos, page); + free_page((unsigned long)page); + } else + ret = -ENOMEM; put_task_struct(tsk); if (ret > 0) *pos += ret; diff --git a/include/linux/proc_fs.h b/include/linux/proc_fs.h index 81d6e4ec2294..9a256e86205c 100644 --- a/include/linux/proc_fs.h +++ b/include/linux/proc_fs.h @@ -158,6 +158,9 @@ int proc_pid_arch_status(struct seq_file *m, struct pid_namespace *ns, struct pid *pid, struct task_struct *task); #endif /* CONFIG_PROC_PID_ARCH_STATUS */ +void get_task_cmdline_kernel(struct task_struct *tsk, + char *cmdline, size_t maxcount); + #else /* CONFIG_PROC_FS */ static inline void proc_root_init(void) @@ -216,6 +219,8 @@ static inline struct pid *tgid_pidfd_to_pid(const struct file *file) return ERR_PTR(-EBADF); } +static inline void get_task_cmdline_kernel(struct task_struct *, char *, size_t) { } + #endif /* CONFIG_PROC_FS */ struct net;