Received: by 2002:ac0:950c:0:0:0:0:0 with SMTP id f12csp1402897imc; Mon, 11 Mar 2019 13:01:10 -0700 (PDT) X-Google-Smtp-Source: APXvYqwYzUbpoRtcbhJOHty4pPrUFhvKBo5C1sLstN3D//DRIOBPrJGxzfcGTVLCH8O++bsKEzSM X-Received: by 2002:a65:4581:: with SMTP id o1mr31788797pgq.159.1552334470776; Mon, 11 Mar 2019 13:01:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1552334470; cv=none; d=google.com; s=arc-20160816; b=a9ruIYqDataDVF46y0ai1yvgbDrF2ZeFLsgtI+vAcFoWxlGnWDUili7c5af03Dje+w MaBd3iYbMs/Tw5oGYW0li6cHzag8FZFmp/16wPcyLDTING+ZwO0XPE5rmRcLiYUPTq7f CRyuw2TTj3ZSUSo31CGGTyi6l0AuvtUNRR6q2Zm11SzdNdLsQhJZuSFQQuEGna3Jej8w QPkQv2vjk7JlqBZlK0FphEQXX7DsT3M1Li/YQRojNrdj8z+Pu+Sitorr7XfgiaTyf2Wy ScyPJLQwGz0nJNDs0FnhbRzROjrlcXSuUFHBkk7YM/ZRZOW66Zwr80z0Mg5Smf12jEYp LRcw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=lsLVOt/FvF2B/qXY5XUzWaB5EqfHVJ3XNpFC1o80EJA=; b=DmQfNh31J8TnRZQgEWvbUoPMRkgZhGLDfJU7MR/vDucAjdGn3sCQDjZ5vwiLNGI8Ub wYWmb4HaYPQLAV8X0fKE8EcdPsb1osd1duQ1xaDSlP7GlWY0MVWeBfUDADjlJ4eyl4o1 vuzip9JbbLgQGSeKfN28e0aX1gZRC6Hg44PtAcDK4Vm5BconYKe2+dN30ks2J+faTK4f u4LKZ5c97jyZ4zDTDlsfVbWIqUfDhJjS7BiabuioKVFcxsgafPj/Bl/JaZ4TWpR0xVAL XA3sjRZerbVnHKiziaahIHOFBphEsQnVbPZbU6pOU2wXfIO4krBlWM5MjWpnQdQYJMOM uiBg== 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 66si4873854plc.88.2019.03.11.13.00.55; Mon, 11 Mar 2019 13:01:10 -0700 (PDT) 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 S1729486AbfCKT6y (ORCPT + 99 others); Mon, 11 Mar 2019 15:58:54 -0400 Received: from verein.lst.de ([213.95.11.211]:43316 "EHLO newverein.lst.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728881AbfCKT6j (ORCPT ); Mon, 11 Mar 2019 15:58:39 -0400 Received: by newverein.lst.de (Postfix, from userid 2407) id E268968C7B; Mon, 11 Mar 2019 20:58:31 +0100 (CET) Date: Mon, 11 Mar 2019 20:58:31 +0100 From: Christoph Hellwig To: Al Viro Cc: Linus Torvalds , 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: Re: [PATCH 4/8] Fix aio_poll() races Message-ID: <20190311195831.GA12807@lst.de> References: <20190310070606.GA10138@ZenIV.linux.org.uk> <20190310070822.11564-1-viro@ZenIV.linux.org.uk> <20190310070822.11564-4-viro@ZenIV.linux.org.uk> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190310070822.11564-4-viro@ZenIV.linux.org.uk> User-Agent: Mutt/1.5.17 (2007-11-01) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Where do we put the second iocb reference in case we return from vfs_poll without ever being woken? Also it seems like the complete code would still benefit from a little helper, something like: diff --git a/fs/aio.c b/fs/aio.c index b2a5c7b3a1fe..8415e5e484ce 100644 --- a/fs/aio.c +++ b/fs/aio.c @@ -1611,6 +1611,13 @@ static int aio_fsync(struct fsync_iocb *req, const struct iocb *iocb, return 0; } +static void aio_poll_finish(struct aio_kiocb *iocb, __poll_t mask) +{ + list_del_init(&iocb->ki_list); + iocb->ki_res.res = mangle_poll(mask); + iocb->poll.done = true; +} + static void aio_poll_complete_work(struct work_struct *work) { struct poll_iocb *req = container_of(work, struct poll_iocb, work); @@ -1635,9 +1642,7 @@ static void aio_poll_complete_work(struct work_struct *work) spin_unlock_irq(&ctx->ctx_lock); return; } - list_del_init(&iocb->ki_list); - iocb->ki_res.res = mangle_poll(mask); - req->done = true; + aio_poll_finish(iocb, mask); spin_unlock_irq(&ctx->ctx_lock); iocb_put(iocb); @@ -1674,24 +1679,20 @@ static int aio_poll_wake(struct wait_queue_entry *wait, unsigned mode, int sync, list_del_init(&req->wait.entry); - if (mask) { + if (mask && spin_trylock_irqsave(&iocb->ki_ctx->ctx_lock, flags)) { /* * Try to complete the iocb inline if we can. Use * irqsave/irqrestore because not all filesystems (e.g. fuse) * call this function with IRQs disabled and because IRQs * have to be disabled before ctx_lock is obtained. */ - if (spin_trylock_irqsave(&iocb->ki_ctx->ctx_lock, flags)) { - list_del(&iocb->ki_list); - iocb->ki_res.res = mangle_poll(mask); - req->done = true; - spin_unlock_irqrestore(&iocb->ki_ctx->ctx_lock, flags); - iocb_put(iocb); - return 1; - } + aio_poll_finish(iocb, mask); + spin_unlock_irqrestore(&iocb->ki_ctx->ctx_lock, flags); + iocb_put(iocb); + } else { + schedule_work(&req->work); } - schedule_work(&req->work); return 1; }