From patchwork Tue May 22 11:30:46 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 10418213 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 6CA1A600CC for ; Tue, 22 May 2018 11:41:41 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5397C28C69 for ; Tue, 22 May 2018 11:41:41 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4A2F228C56; Tue, 22 May 2018 11:41:41 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 22AFD28C69 for ; Tue, 22 May 2018 11:41:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751713AbeEVLkx (ORCPT ); Tue, 22 May 2018 07:40:53 -0400 Received: from bombadil.infradead.org ([198.137.202.133]:36648 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751872AbeEVLb5 (ORCPT ); Tue, 22 May 2018 07:31:57 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=References:In-Reply-To:Message-Id: Date:Subject:Cc:To:From:Sender:Reply-To:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=2fD3Az792bf7w7cTa3jEmIunGDI4QkDlvTZ142wreRY=; b=IY09dpxYBtTk7NdN/paH5/jVM LAvKtUUrlLL6x8xUApz9kDTRcjMKrZgM4RONRWMS4xT/8vF4yCssgN8VtrhfxKx4uhGPQ6CQ7fPmr dHk6H5MjDAEAtF5A82ZVzs6+l/4GVRqPNEcLcdf1UUF1R9HY6uZn+DPJ06OVnV7PZ6/yW2bUpblXo M+2JNGQW6UFP1gwhANxgKFbw2ehGEnpLu8JIOqH0BjZ9OCvO9B+jv9jAGBpJ5byWyWUhxaCFvSdB1 Jr9PIi9LHJ3f9IUrEuOWr09+YOls1Wc3Cqt3NwEZR4KBKStGLGHN4ZpPlDJPZOJu6heXigWsPGAgg W02T9g2lQ==; Received: from 80-109-164-210.cable.dynamic.surfer.at ([80.109.164.210] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.90_1 #2 (Red Hat Linux)) id 1fL5WG-0008Mm-0m; Tue, 22 May 2018 11:31:56 +0000 From: Christoph Hellwig To: viro@zeniv.linux.org.uk Cc: Avi Kivity , linux-aio@kvack.org, linux-fsdevel@vger.kernel.org, netdev@vger.kernel.org, linux-api@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 09/31] aio: try to complete poll iocbs without context switch Date: Tue, 22 May 2018 13:30:46 +0200 Message-Id: <20180522113108.25713-10-hch@lst.de> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180522113108.25713-1-hch@lst.de> References: <20180522113108.25713-1-hch@lst.de> X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP If we can acquire ctx_lock without spinning we can just remove our iocb from the active_reqs list, and thus complete the iocbs from the wakeup context. Signed-off-by: Christoph Hellwig --- fs/aio.c | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/fs/aio.c b/fs/aio.c index 4d1eabce6659..85d9102431db 100644 --- a/fs/aio.c +++ b/fs/aio.c @@ -1632,6 +1632,7 @@ static int aio_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, void *key) { struct poll_iocb *req = container_of(wait, struct poll_iocb, wait); + struct aio_kiocb *iocb = container_of(req, struct aio_kiocb, poll); struct file *file = req->file; __poll_t mask = key_to_poll(key); @@ -1647,9 +1648,22 @@ static int aio_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, __aio_poll_remove(req); - req->events = mask; - INIT_WORK(&req->work, aio_poll_work); - schedule_work(&req->work); + /* + * Try completing without a context switch if we can acquire ctx_lock + * without spinning. Otherwise we need to defer to a workqueue to + * avoid a deadlock due to the lock order. + */ + if (spin_trylock(&iocb->ki_ctx->ctx_lock)) { + list_del_init(&iocb->ki_list); + spin_unlock(&iocb->ki_ctx->ctx_lock); + + __aio_poll_complete(req, mask); + } else { + req->events = mask; + INIT_WORK(&req->work, aio_poll_work); + schedule_work(&req->work); + } + return 1; }