Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp1647543ybt; Mon, 15 Jun 2020 06:04:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxqjw0HV8Lb5J9Hsx+nzh656rojkjulvyxgDHp0l4baE1laxfbf7YkyOB2zthtJUhwlAaQD X-Received: by 2002:a17:906:ce31:: with SMTP id sd17mr25360495ejb.255.1592226257965; Mon, 15 Jun 2020 06:04:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592226257; cv=none; d=google.com; s=arc-20160816; b=z8mHh4WgTZyQgjJ5EhQgVXi9dZpeEhZMj5jFBZbsNYziahn6kR8a3cirKtDFiVBvLO ttFZz9pNf/c+DmLFPLS4GunhhifqqJwZ3K5iXb9fIfkB53cZjMMgHk1HX9VQ2flMhV1C LfyXQJeiKGM6DIBx7Vb1TtlMXdSCmNv18BaIHFqy7iAzffmbgW8fJqq4GypHaEW60sBO BNWwHqXOhiTXZcnGbRcT2M0QyzJYhOsT5hCzsjrjtdZwKqe8+kmlVsuC/6ZPAWe2PTHO NrFaCbwV1qSkkGnzfznQKBGTKquVHno1zJSQEwjjaC4LrI5zSxY2APZ68JHnJvEYZ8a4 jbhA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=T94kCvjMg3POOYY9QZ5B+bKnsinmf9rCUNlS0Naq4B0=; b=fPpHIKGLG5R1mf6PRizvKCYpwInijRu9Tu1RD4v8BLQiNFTaCJW8JYHPcMAXXAAi5W x07Tcnj9MVuVAjSPeN1fvQ6579lUjtaA8ijaGLFFztvpdaRzv61eu81wmWS6P6XvoVec d0Xs9hTijbB0JRAlzfz6sF5U59WYactujoNcfWnNjsNNP3v1di+r0o6l9idx2xvYQDf8 vJsC7eh7I17UxKGj1KHuR9ySdnnCwtzGe8enTz7k7Av0EKXofL3OMmzDcTn/ruTiQ9+w UFmpvgEfWvL/7/k7KIRmVSd9JY3+nnvE5eJwDtUAHTsTZJE9YPYoA8xmkP2hxwMs4dn+ GUZw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=Qo5IGkkz; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id du1si12870604ejc.616.2020.06.15.06.03.53; Mon, 15 Jun 2020 06:04:17 -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=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=Qo5IGkkz; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730404AbgFONBD (ORCPT + 99 others); Mon, 15 Jun 2020 09:01:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52414 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730251AbgFONA6 (ORCPT ); Mon, 15 Jun 2020 09:00:58 -0400 Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:e::133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A280DC061A0E; Mon, 15 Jun 2020 06:00:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender :Reply-To:Content-Type:Content-ID:Content-Description; bh=T94kCvjMg3POOYY9QZ5B+bKnsinmf9rCUNlS0Naq4B0=; b=Qo5IGkkztcUZUo+cF8SPeORX9i w8KjM/VC9s4RMbj4LN1fKVtGCejc53i0Zu2X7+dZHUKqbl+/UWNGou0bWdNaQSGZGFBALkNZyyX7G 7WPcNWmdxBXgCdcQmbNo4w+COAtvJ909XyC6k/F9jHQD/7PTa4oq2W1vPCOQ0q/U7pcIct/SwXOFK ttpKACNil9TDb9+ef+c4u10g1xvHGoDSCuYvcugulrM6h5bvWrzi4gpMUu3jqFiIt+7jLRAhrE1wo AYbI592N/mkwbu7osPGGfCrVJQGOe+sP14GCKFKsGMmY+XEQIT5qRRum69TNwcvDRsf4IHCkpK76+ SpOrojPQ==; Received: from 195-192-102-148.dyn.cablelink.at ([195.192.102.148] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.92.3 #3 (Red Hat Linux)) id 1jkojK-00081b-G3; Mon, 15 Jun 2020 13:00:51 +0000 From: Christoph Hellwig To: Al Viro Cc: Arnd Bergmann , Luis Chamberlain , linux-arm-kernel@lists.infradead.org, x86@kernel.org, linux-mips@vger.kernel.org, linux-parisc@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-s390@vger.kernel.org, sparclinux@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 6/6] kernel: add a kernel_wait helper Date: Mon, 15 Jun 2020 15:00:32 +0200 Message-Id: <20200615130032.931285-7-hch@lst.de> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20200615130032.931285-1-hch@lst.de> References: <20200615130032.931285-1-hch@lst.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add a helper that waits for a pid and stores the status in the passed in kernel pointer. Use it to fix the usage of kernel_wait4 in call_usermodehelper_exec_sync that only happens to work due to the implicit set_fs(KERNEL_DS) for kernel threads. Signed-off-by: Christoph Hellwig --- include/linux/sched/task.h | 1 + kernel/exit.c | 16 ++++++++++++++++ kernel/umh.c | 29 ++++------------------------- 3 files changed, 21 insertions(+), 25 deletions(-) diff --git a/include/linux/sched/task.h b/include/linux/sched/task.h index 38359071236ad7..a80007df396e95 100644 --- a/include/linux/sched/task.h +++ b/include/linux/sched/task.h @@ -102,6 +102,7 @@ struct task_struct *fork_idle(int); struct mm_struct *copy_init_mm(void); extern pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags); extern long kernel_wait4(pid_t, int __user *, int, struct rusage *); +int kernel_wait(pid_t pid, int *stat); extern void free_task(struct task_struct *tsk); diff --git a/kernel/exit.c b/kernel/exit.c index 727150f2810338..fd598846df0b17 100644 --- a/kernel/exit.c +++ b/kernel/exit.c @@ -1626,6 +1626,22 @@ long kernel_wait4(pid_t upid, int __user *stat_addr, int options, return ret; } +int kernel_wait(pid_t pid, int *stat) +{ + struct wait_opts wo = { + .wo_type = PIDTYPE_PID, + .wo_pid = find_get_pid(pid), + .wo_flags = WEXITED, + }; + int ret; + + ret = do_wait(&wo); + if (ret > 0 && wo.wo_stat) + *stat = wo.wo_stat; + put_pid(wo.wo_pid); + return ret; +} + SYSCALL_DEFINE4(wait4, pid_t, upid, int __user *, stat_addr, int, options, struct rusage __user *, ru) { diff --git a/kernel/umh.c b/kernel/umh.c index 1284823dbad338..6fd948e478bec4 100644 --- a/kernel/umh.c +++ b/kernel/umh.c @@ -126,37 +126,16 @@ static void call_usermodehelper_exec_sync(struct subprocess_info *sub_info) { pid_t pid; - /* If SIGCLD is ignored kernel_wait4 won't populate the status. */ + /* If SIGCLD is ignored do_wait won't populate the status. */ kernel_sigaction(SIGCHLD, SIG_DFL); pid = kernel_thread(call_usermodehelper_exec_async, sub_info, SIGCHLD); - if (pid < 0) { + if (pid < 0) sub_info->retval = pid; - } else { - int ret = -ECHILD; - /* - * Normally it is bogus to call wait4() from in-kernel because - * wait4() wants to write the exit code to a userspace address. - * But call_usermodehelper_exec_sync() always runs as kernel - * thread (workqueue) and put_user() to a kernel address works - * OK for kernel threads, due to their having an mm_segment_t - * which spans the entire address space. - * - * Thus the __user pointer cast is valid here. - */ - kernel_wait4(pid, (int __user *)&ret, 0, NULL); - - /* - * If ret is 0, either call_usermodehelper_exec_async failed and - * the real error code is already in sub_info->retval or - * sub_info->retval is 0 anyway, so don't mess with it then. - */ - if (ret) - sub_info->retval = ret; - } + else + kernel_wait(pid, &sub_info->retval); /* Restore default kernel sig handler */ kernel_sigaction(SIGCHLD, SIG_IGN); - umh_complete(sub_info); } -- 2.26.2