Received: by 2002:a17:90a:9307:0:0:0:0 with SMTP id p7csp3963387pjo; Tue, 3 Mar 2020 10:10:24 -0800 (PST) X-Google-Smtp-Source: ADFU+vtsamkFO5Asflcj50vliwNj2OhaOeU/Lf83XpG4Z1YYjmZLJHLKERe69VbIjo/STbXoRFWy X-Received: by 2002:a05:6808:8ee:: with SMTP id d14mr3134337oic.138.1583259024544; Tue, 03 Mar 2020 10:10:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1583259024; cv=none; d=google.com; s=arc-20160816; b=FtA2tuwrJi0JJIhESZ/I3QyYJUOAfkqck1AfdFM6l+cUlHjI0ae5tUDfM8dXQtBFF6 qjne17gXg4dHXTGd/yP8lQ2485s5XqP20/dZU07d/Gup7kbS32zzXBDTMbHgoVsvZR2x 5XQkez21imq2vTlbBlY3KdXST9f1NDL8yb/DjrTkxeJslRgjXzgmQ3qhTnjkNx+wfGQz XCQEQ4ZLnSE6UQgpMwRbn3psnI+Xi/fBk5169UYvEAA2+hvG0jjI8bhE6/pJVJw66Ey8 X3cQ8KaijGYW2utncAf8MXsCBqFwcCXaRoYZoBUT9+wjFFMw5Qo8GQInA3hWp2IA4QrF gjxQ== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=4bcQOzN6/Pg2Tw8J8zp4gb/1UsKYPQBXz7YePyPqJ+o=; b=H0ZBzgi7EWw6+4VwTL2bAnQu5+0tKWOdkJnPd2SYSBQgHFQoNPk09XVtmyPBVkIhPq HlPUGKTtzq0H4WmURoswoiT1j6siObt5AL7eVA2NY1N1qFDGpmijs53r9u4kZa/hqHVF JmvF/yXWwbG/MJyoRuXcmKZnYlW2xV6lK/9PeFPWggNpxklCqDpGzObyUpKmu6OayhrG Foqa2yWvxlq2HWtdIc1rJCDhvi7SieEyCVNfR993DMOhnAn8Jp1aQZGq92GNxYLQG5zB X0KhXzJfTWscPO2YIVtv9HCBtb+1wxOrsArB2AHQab5E/hPf9dcV6wTSH0PkM9hkCiiC xVhA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=GN3byUCT; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l13si939239oil.161.2020.03.03.10.10.12; Tue, 03 Mar 2020 10:10:24 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=GN3byUCT; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731903AbgCCRwT (ORCPT + 99 others); Tue, 3 Mar 2020 12:52:19 -0500 Received: from mail.kernel.org ([198.145.29.99]:60596 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731876AbgCCRwP (ORCPT ); Tue, 3 Mar 2020 12:52:15 -0500 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id ADD9D2146E; Tue, 3 Mar 2020 17:52:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1583257935; bh=242ju/f1UL0RmgOXdz4af0lNK8ENlCQQx6rAW3hkU6s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GN3byUCTlLsELEx6H2hmA73HRYTHBZcNdnk8ktlPDqRapVagXwynzFSNfkFhyXZZm sLf3tgMlWYHy/U9gOYhfGMDuKrPxdwYNQiYhZtIptW1CsJanlVrOcvq3be4He4a1pi wFtLm16XonqDNEQoNricspapdRmIX1lqp8MEdB4Y= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jens Axboe , Sasha Levin Subject: [PATCH 5.4 001/152] io_uring: grab ->fs as part of async offload Date: Tue, 3 Mar 2020 18:41:39 +0100 Message-Id: <20200303174302.711223909@linuxfoundation.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200303174302.523080016@linuxfoundation.org> References: <20200303174302.523080016@linuxfoundation.org> User-Agent: quilt/0.66 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jens Axboe [ Upstream commits 9392a27d88b9 and ff002b30181d ] Ensure that the async work grabs ->fs from the queueing task if the punted commands needs to do lookups. We don't have these two commits in 5.4-stable: ff002b30181d30cdfbca316dadd099c3ca0d739c 9392a27d88b9707145d713654eb26f0c29789e50 because they don't apply with the rework that was done in how io_uring handles offload. Since there's no io-wq in 5.4, it doesn't make sense to do two patches. I'm attaching my port of the two for 5.4-stable, it's been tested. Please queue it up for the next 5.4-stable, thanks! Signed-off-by: Jens Axboe Signed-off-by: Sasha Levin --- fs/io_uring.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/fs/io_uring.c b/fs/io_uring.c index ed9a551882cf3..f34a8f7eee5d1 100644 --- a/fs/io_uring.c +++ b/fs/io_uring.c @@ -71,6 +71,7 @@ #include #include #include +#include #include @@ -334,6 +335,8 @@ struct io_kiocb { u32 result; u32 sequence; + struct fs_struct *fs; + struct work_struct work; }; @@ -651,6 +654,7 @@ static struct io_kiocb *io_get_req(struct io_ring_ctx *ctx, /* one is dropped after submission, the other at completion */ refcount_set(&req->refs, 2); req->result = 0; + req->fs = NULL; return req; out: percpu_ref_put(&ctx->refs); @@ -1663,6 +1667,16 @@ static int io_send_recvmsg(struct io_kiocb *req, const struct io_uring_sqe *sqe, ret = -EINTR; } + if (req->fs) { + struct fs_struct *fs = req->fs; + + spin_lock(&req->fs->lock); + if (--fs->users) + fs = NULL; + spin_unlock(&req->fs->lock); + if (fs) + free_fs_struct(fs); + } io_cqring_add_event(req->ctx, sqe->user_data, ret); io_put_req(req); return 0; @@ -2159,6 +2173,7 @@ static inline bool io_sqe_needs_user(const struct io_uring_sqe *sqe) static void io_sq_wq_submit_work(struct work_struct *work) { struct io_kiocb *req = container_of(work, struct io_kiocb, work); + struct fs_struct *old_fs_struct = current->fs; struct io_ring_ctx *ctx = req->ctx; struct mm_struct *cur_mm = NULL; struct async_list *async_list; @@ -2178,6 +2193,15 @@ restart: /* Ensure we clear previously set non-block flag */ req->rw.ki_flags &= ~IOCB_NOWAIT; + if (req->fs != current->fs && current->fs != old_fs_struct) { + task_lock(current); + if (req->fs) + current->fs = req->fs; + else + current->fs = old_fs_struct; + task_unlock(current); + } + ret = 0; if (io_sqe_needs_user(sqe) && !cur_mm) { if (!mmget_not_zero(ctx->sqo_mm)) { @@ -2276,6 +2300,11 @@ out: mmput(cur_mm); } revert_creds(old_cred); + if (old_fs_struct) { + task_lock(current); + current->fs = old_fs_struct; + task_unlock(current); + } } /* @@ -2503,6 +2532,23 @@ err: req->user_data = s->sqe->user_data; +#if defined(CONFIG_NET) + switch (READ_ONCE(s->sqe->opcode)) { + case IORING_OP_SENDMSG: + case IORING_OP_RECVMSG: + spin_lock(¤t->fs->lock); + if (!current->fs->in_exec) { + req->fs = current->fs; + req->fs->users++; + } + spin_unlock(¤t->fs->lock); + if (!req->fs) { + ret = -EAGAIN; + goto err_req; + } + } +#endif + /* * If we already have a head request, queue this one for async * submittal once the head completes. If we don't have a head but -- 2.20.1