Received: by 2002:ac0:aed5:0:0:0:0:0 with SMTP id t21csp4529512imb; Wed, 6 Mar 2019 16:04:30 -0800 (PST) X-Google-Smtp-Source: APXvYqxj4vyFj64AK813cG/QWh7A4y8IqGSrkpYD0Zok5i7C8aW6jBd/AblUP7M3e1Mvvna4ZZIv X-Received: by 2002:a17:902:5066:: with SMTP id f35mr10032419plh.184.1551917070276; Wed, 06 Mar 2019 16:04:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1551917070; cv=none; d=google.com; s=arc-20160816; b=HjyvKUqsID+rNbXo+ZPVpkKvwTQIz4bAMCWAWfcf3JWCK9AD5LO5MSe3lQuaAxZMd6 wkDyMY7ESOq0+c2dlvzjweMtfdBoN5Ghx68i6BQ9lJF+zu/IO0gc3rbzZ39L49PUhFCA dSVg/kRuxZ1LbeaHkdntNa/5Fgy7Sf8eXCYVBvWC9tp0+5XBkfy0dQTsBlbNeAt19c7o Aizll9SX/8sWJWWaUj6mi4j75NCVhtiXGbjZGMjDE0waPZ6gmEmhGih95kPG4eSgtOzL l3Fo5E9Y+yU+lOxsCqPQf6PR8zdbYHr2U65pzTAislFJhCtwVFPMD2af8gSSx7IYUlhr NZsg== 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; bh=BihykMmBsCETylVTxVfB9oBOxV2TPHQ2VsCcbWnxvFE=; b=fE5x6wTjRtlsXOy3LmvkF9IjdZ67yDQLv9kq67tDr3uXWW5W2VqLAJVRw4XdHkA9RD Hjj7VzOKVWHPWzOYi1ZDGwg5Kwqm4j/4/x0kIavyfG0oy5NpN6wowap1ijMf2c5v9Csf 7kh7QFRzWAzDU8Eu8OPey0hSotoBVl5uY4ORWUdO9aXLE3aKLTpRyvNYcjGYE6ImXA+v xfOi5aB7bF96C3m0rN+tVlNKgWqdMfaHcFKgFCtLFF1BRpgmHNuHxGjODpNFTzppUZb1 mDUt0Lk/qoCDf6WbbPcKCRAi+o+kGDQ/ZFKvYPI/7Ztwat/9OjC3ZuUwHHL2bkvn17Wy F9RQ== ARC-Authentication-Results: i=1; mx.google.com; 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 r124si2930015pfr.252.2019.03.06.16.04.14; Wed, 06 Mar 2019 16:04:30 -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; 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 S1726686AbfCGADa (ORCPT + 99 others); Wed, 6 Mar 2019 19:03:30 -0500 Received: from zeniv.linux.org.uk ([195.92.253.2]:39962 "EHLO ZenIV.linux.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725793AbfCGAD3 (ORCPT ); Wed, 6 Mar 2019 19:03:29 -0500 Received: from viro by ZenIV.linux.org.uk with local (Exim 4.92 #3 (Red Hat Linux)) id 1h1gVJ-00086s-8T; Thu, 07 Mar 2019 00:03:17 +0000 From: Al Viro To: Linus Torvalds Cc: Eric Dumazet , David Miller , Jason Baron , kgraul@linux.ibm.com, ktkhai@virtuozzo.com, kyeongdon.kim@lge.com, Linux List Kernel Mailing , Netdev , pabeni@redhat.com, syzkaller-bugs@googlegroups.com, xiyou.wangcong@gmail.com, Christoph Hellwig , zhengbin , bcrl@kvack.org, linux-fsdevel@vger.kernel.org, linux-aio@kvack.org, houtao1@huawei.com, yi.zhang@huawei.com Subject: [PATCH 3/8] aio_poll(): sanitize the logics after vfs_poll(), get rid of leak on error Date: Thu, 7 Mar 2019 00:03:11 +0000 Message-Id: <20190307000316.31133-3-viro@ZenIV.linux.org.uk> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190307000316.31133-1-viro@ZenIV.linux.org.uk> References: <20190307000316.31133-1-viro@ZenIV.linux.org.uk> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Al Viro We want iocb_put() happening on errors, to balance the extra reference we'd taken. As it is, we end up with a leak. The rules should be * error: iocb_put() to deal with the extra ref, return error, let the caller do another iocb_put(). * async: iocb_put() to deal with the extra ref, return 0. * no error, event present immediately: aio_poll_complete() to report it, iocb_put() to deal with the extra ref, return 0. Signed-off-by: Al Viro --- fs/aio.c | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/fs/aio.c b/fs/aio.c index 3a8b894378e0..22b288997441 100644 --- a/fs/aio.c +++ b/fs/aio.c @@ -1724,6 +1724,7 @@ static ssize_t aio_poll(struct aio_kiocb *aiocb, const struct iocb *iocb) struct kioctx *ctx = aiocb->ki_ctx; struct poll_iocb *req = &aiocb->poll; struct aio_poll_table apt; + bool async = false; __poll_t mask; /* reject any unknown events outside the normal event mask. */ @@ -1760,30 +1761,26 @@ static ssize_t aio_poll(struct aio_kiocb *aiocb, const struct iocb *iocb) spin_lock_irq(&ctx->ctx_lock); spin_lock(&req->head->lock); - if (req->woken) { - /* wake_up context handles the rest */ - mask = 0; + if (req->woken) { /* already taken up by aio_poll_wake() */ + async = true; apt.error = 0; - } else if (mask || apt.error) { - /* if we get an error or a mask we are done */ - WARN_ON_ONCE(list_empty(&req->wait.entry)); - list_del_init(&req->wait.entry); - } else { - /* actually waiting for an event */ + } else if (!mask && !apt.error) { /* actually waiting for an event */ list_add_tail(&aiocb->ki_list, &ctx->active_reqs); aiocb->ki_cancel = aio_poll_cancel; + async = true; + } else { /* if we get an error or a mask we are done */ + WARN_ON_ONCE(list_empty(&req->wait.entry)); + list_del_init(&req->wait.entry); + /* no wakeup in the future either; aiocb is ours to dispose of */ } spin_unlock(&req->head->lock); spin_unlock_irq(&ctx->ctx_lock); out: - if (unlikely(apt.error)) - return apt.error; - - if (mask) + if (async && !apt.error) aio_poll_complete(aiocb, mask); iocb_put(aiocb); - return 0; + return apt.error; } static int __io_submit_one(struct kioctx *ctx, const struct iocb *iocb, -- 2.11.0