Received: by 10.223.176.46 with SMTP id f43csp3331059wra; Mon, 22 Jan 2018 12:14:57 -0800 (PST) X-Google-Smtp-Source: AH8x226md0JryosrdWR2R93Vuscw8JBDpGCnpj8Ww3bdjRfK7HLKCKA76Y8rXeTFTk6YA+ExXqv1 X-Received: by 10.107.134.97 with SMTP id i94mr115212iod.210.1516652097280; Mon, 22 Jan 2018 12:14:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516652097; cv=none; d=google.com; s=arc-20160816; b=e5FQ2vJbniVDiiuICmN2z4m2FS0L56/ZsiQgy1jS2XebvlcKZWayubOgnaZGMlifB9 DJ9r5/kPF6vKlT162E7Zu86/itJCLAgvx30dC4/QC8aZvM3U7TTl21pDgIDliqD68pL+ Qh/jVoUrwPupcU+yzXXwVs1dpzMVqurtntl//Ouor0L/r3dV3IiwCn0MXStqhq3F9Cxy j8sFpgXoja6Ea/Px45pAX92p4kw7qtr9/RiNC7mdBXIPNr4/wz8AvJb0V2V4ryFByLZQ gLRbLAvqRff6uCkIdQoBO3HGn4UIfdPBA4mNLmoeiGiW2ntkjdaPiVvnzF7+apnYsMHv CDPw== 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:arc-authentication-results; bh=VpqakcI8sYAELFAySdoxDqEkH5q2dVZz9XV5xzL+7c8=; b=ehbdUXFZRxHVaYpe+wMOB4C7y6aVLcWlVAHzI/mHy5sN1T9fbOI0bE4pYBh5btcJMC Z8I03gtSw1xFz07rjcywbDlTpjVjxD5ECDJuq0V0AheAAATyC2obbIID6vAD2flzohFn 8H/FkWlnR2TTWqrHFdqM7aGpnFWfvCg23ADKNXAZ71wIxI+2YJ6JnvLRgAFx4bnHmFjh abYHh2WM+aMGDcGTX9aV6D77n/kJsTzxvQunkOCwIvEQTeN2clLFmgs/iJ/v3Da5erlD a+KfsF+d+d3QhToheD4F9g0P0od6VD2l/fMOkNl4cCJNL6cEuorStzK2vTIt23d42ILU GymQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=b/ypZCMw; 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 196si6625326itz.9.2018.01.22.12.14.44; Mon, 22 Jan 2018 12:14:57 -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=fail header.i=@infradead.org header.s=bombadil.20170209 header.b=b/ypZCMw; 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 S1751439AbeAVUNP (ORCPT + 99 others); Mon, 22 Jan 2018 15:13:15 -0500 Received: from bombadil.infradead.org ([65.50.211.133]:56399 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750973AbeAVUNL (ORCPT ); Mon, 22 Jan 2018 15:13:11 -0500 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=VpqakcI8sYAELFAySdoxDqEkH5q2dVZz9XV5xzL+7c8=; b=b/ypZCMwuvKx+4ZnJHU9n/MOa qKYpZO/VKdqS90HFcyuN5jrFu5U2D+XGCgPfKmbu1IHzYMXpFjTSkDPFA+ASoqubRzh9LzgK/WTx8 pN82PONoRNGCr3MyTJaraKEj3ZmdLEvfNLM7U0oTe+pIwBmtW6nq+hQPx1Ar562P/JqecXSlr+0VE c0UxbSR57blFWY+vzZ592GeKbP8Jwkl6LiID41V5+acRA5EU1XduYxqGfZm87+wCeNwYDBrlgH5n8 qeIT74YzkjB0wKXSriq8W0vYUXSEhAX/Pq/M3cLqNROsnnUGnBNgtguw5RGsGUKRoR4bjAoNwt1W4 zWDszAKAA==; Received: from 178.114.226.247.wireless.dyn.drei.com ([178.114.226.247] helo=localhost) by bombadil.infradead.org with esmtpsa (Exim 4.89 #1 (Red Hat Linux)) id 1ediSr-0007Y3-Ae; Mon, 22 Jan 2018 20:13:09 +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 07/36] aio: add delayed cancel support Date: Mon, 22 Jan 2018 21:12:14 +0100 Message-Id: <20180122201243.31610-8-hch@lst.de> X-Mailer: git-send-email 2.14.2 In-Reply-To: <20180122201243.31610-1-hch@lst.de> References: <20180122201243.31610-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-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The upcoming aio poll support would like to be able to complete the iocb inline from the cancellation context, but that would cause a lock order reversal. Add support for optionally moving the cancelation outside the context lock to avoid this reversal. Signed-off-by: Christoph Hellwig Acked-by: Jeff Moyer --- fs/aio.c | 49 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/fs/aio.c b/fs/aio.c index 0b6394b4e528..9d7d6e4cde87 100644 --- a/fs/aio.c +++ b/fs/aio.c @@ -170,6 +170,10 @@ struct aio_kiocb { struct list_head ki_list; /* the aio core uses this * for cancellation */ + unsigned int flags; /* protected by ctx->ctx_lock */ +#define AIO_IOCB_DELAYED_CANCEL (1 << 0) +#define AIO_IOCB_CANCELLED (1 << 1) + /* * If the aio_resfd field of the userspace iocb is not zero, * this is the underlying eventfd context to deliver events to. @@ -536,9 +540,9 @@ static int aio_setup_ring(struct kioctx *ctx, unsigned int nr_events) #define AIO_EVENTS_FIRST_PAGE ((PAGE_SIZE - sizeof(struct aio_ring)) / sizeof(struct io_event)) #define AIO_EVENTS_OFFSET (AIO_EVENTS_PER_PAGE - AIO_EVENTS_FIRST_PAGE) -void kiocb_set_cancel_fn(struct kiocb *iocb, kiocb_cancel_fn *cancel) +static void __kiocb_set_cancel_fn(struct aio_kiocb *req, + kiocb_cancel_fn *cancel, unsigned int iocb_flags) { - struct aio_kiocb *req = container_of(iocb, struct aio_kiocb, rw); struct kioctx *ctx = req->ki_ctx; unsigned long flags; @@ -548,8 +552,15 @@ void kiocb_set_cancel_fn(struct kiocb *iocb, kiocb_cancel_fn *cancel) spin_lock_irqsave(&ctx->ctx_lock, flags); list_add_tail(&req->ki_list, &ctx->active_reqs); req->ki_cancel = cancel; + req->flags |= iocb_flags; spin_unlock_irqrestore(&ctx->ctx_lock, flags); } + +void kiocb_set_cancel_fn(struct kiocb *iocb, kiocb_cancel_fn *cancel) +{ + return __kiocb_set_cancel_fn(container_of(iocb, struct aio_kiocb, rw), + cancel, 0); +} EXPORT_SYMBOL(kiocb_set_cancel_fn); /* @@ -603,17 +614,27 @@ static void free_ioctx_users(struct percpu_ref *ref) { struct kioctx *ctx = container_of(ref, struct kioctx, users); struct aio_kiocb *req; + LIST_HEAD(list); spin_lock_irq(&ctx->ctx_lock); - while (!list_empty(&ctx->active_reqs)) { req = list_first_entry(&ctx->active_reqs, struct aio_kiocb, ki_list); - kiocb_cancel(req); - } + if (req->flags & AIO_IOCB_DELAYED_CANCEL) { + req->flags |= AIO_IOCB_CANCELLED; + list_move_tail(&req->ki_list, &list); + } else { + kiocb_cancel(req); + } + } spin_unlock_irq(&ctx->ctx_lock); + while (!list_empty(&list)) { + req = list_first_entry(&list, struct aio_kiocb, ki_list); + kiocb_cancel(req); + } + percpu_ref_kill(&ctx->reqs); percpu_ref_put(&ctx->reqs); } @@ -1785,15 +1806,22 @@ SYSCALL_DEFINE3(io_cancel, aio_context_t, ctx_id, struct iocb __user *, iocb, if (unlikely(!ctx)) return -EINVAL; - spin_lock_irq(&ctx->ctx_lock); + ret = -EINVAL; + spin_lock_irq(&ctx->ctx_lock); kiocb = lookup_kiocb(ctx, iocb, key); + if (kiocb) { + if (kiocb->flags & AIO_IOCB_DELAYED_CANCEL) { + kiocb->flags |= AIO_IOCB_CANCELLED; + } else { + ret = kiocb_cancel(kiocb); + kiocb = NULL; + } + } + spin_unlock_irq(&ctx->ctx_lock); + if (kiocb) ret = kiocb_cancel(kiocb); - else - ret = -EINVAL; - - spin_unlock_irq(&ctx->ctx_lock); if (!ret) { /* @@ -1805,7 +1833,6 @@ SYSCALL_DEFINE3(io_cancel, aio_context_t, ctx_id, struct iocb __user *, iocb, } percpu_ref_put(&ctx->users); - return ret; } -- 2.14.2