From patchwork Tue Mar 20 08:47:01 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 10296677 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 EADCE6039A for ; Tue, 20 Mar 2018 08:47:09 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D947629575 for ; Tue, 20 Mar 2018 08:47:09 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CDED629576; Tue, 20 Mar 2018 08:47:09 +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=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, 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 7546F29577 for ; Tue, 20 Mar 2018 08:47:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751981AbeCTIrI (ORCPT ); Tue, 20 Mar 2018 04:47:08 -0400 Received: from bombadil.infradead.org ([198.137.202.133]:37750 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750991AbeCTIrH (ORCPT ); Tue, 20 Mar 2018 04:47:07 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=In-Reply-To:Content-Type:MIME-Version :References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: 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=Dr1U8/vuP5ZWSmKlxD45QCnMuXdN3LgZnD80ae/3zS4=; b=mUz5AgA3I5DnjVF0vogDZKFg+ 98Fc8dDqvPi1sETzRTo5yuIJuQLI7Xia7lfzpe0BzOxpvg1xiCi6FzlzY0NdpBprF+ZDgVOnj7/jy zXfNo+kFs2mMXc9aEdwQaby5pPPNwaPPKVaGeocXbSGWu4oeghuUpmfMiAb2TR/seNxdvxMGu48UE XmbHxZxPwWpoq9bcsBDDoGxqlWOGL+iL/2P15UWrNEF6ak1yw5zekBuFNL0T4qlmT7yhs+iWUDOXv B26dyzFCWpvUuljsG5GEFj9r+uzH1RgqavMpos57bWYUtNL1Adx+mj3+R6JSyvjNjt9Dm0Nl74pEl RZiWiWAnA==; Received: from hch by bombadil.infradead.org with local (Exim 4.90_1 #2 (Red Hat Linux)) id 1eyCv8-0002a4-3F; Tue, 20 Mar 2018 08:47:02 +0000 Date: Tue, 20 Mar 2018 01:47:01 -0700 From: Christoph Hellwig To: Andrei Vagin Cc: Christoph Hellwig , Andrei Vagin , "Nicholas A. Bellinger" , linux-scsi@vger.kernel.org, target-devel@vger.kernel.org, shli@fb.com, axboe@kernel.dk Subject: Re: [PATCH] [RFC] target/file: add support of direct and async I/O Message-ID: <20180320084701.GA4241@infradead.org> References: <20180309004259.16052-1-avagin@openvz.org> <20180316075027.GA19192@infradead.org> <20180320075456.GA11151@outlook.office365.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20180320075456.GA11151@outlook.office365.com> User-Agent: Mutt/1.9.2 (2017-12-15) X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org. See http://www.infradead.org/rpr.html Sender: linux-scsi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-scsi@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP On Tue, Mar 20, 2018 at 12:54:58AM -0700, Andrei Vagin wrote: > commit 92d773324b7edbd36bf0c28c1e0157763aeccc92 > Author: Shaohua Li > Date: Fri Sep 1 11:15:17 2017 -0700 > > block/loop: fix use after free > > lo_rw_aio->call_read_iter-> > 1 aops->direct_IO > 2 iov_iter_revert > lo_rw_aio_complete could happen between 1 and 2, the bio and bvec > could > be freed before 2, which accesses bvec. > > Signed-off-by: Shaohua Li > Signed-off-by: Jens Axboe > > This commit looks reasonable, doesn't it? In out case, bvec-s are > freed from the callback too. It looks completely bogus, but it papers over a valid issue. The iovec/bvec passed to ->read_iter/->write_iter is caller allocated and freed, so we should always free it after the calls. The actual I/O completion is separate from that. Below is the real fix for the loop driver: --- From 101f857c5b67492cfd75212d104699813f1bd345 Mon Sep 17 00:00:00 2001 From: Christoph Hellwig Date: Tue, 20 Mar 2018 09:35:55 +0100 Subject: loop: remove loop_cmd refcounting Instead of introducing an unneeded refcount free the bvec after calling into ->read_iter and ->write_iter, which follows the the normal read/write path conventions. Fixes: 92d77332 ("block/loop: fix use after free") Signed-off-by: Christoph Hellwig --- drivers/block/loop.c | 14 ++------------ drivers/block/loop.h | 1 - 2 files changed, 2 insertions(+), 13 deletions(-) diff --git a/drivers/block/loop.c b/drivers/block/loop.c index ee62d2d517bf..2578a39640b8 100644 --- a/drivers/block/loop.c +++ b/drivers/block/loop.c @@ -463,15 +463,6 @@ static void lo_complete_rq(struct request *rq) blk_mq_end_request(rq, cmd->ret < 0 ? BLK_STS_IOERR : BLK_STS_OK); } -static void lo_rw_aio_do_completion(struct loop_cmd *cmd) -{ - if (!atomic_dec_and_test(&cmd->ref)) - return; - kfree(cmd->bvec); - cmd->bvec = NULL; - blk_mq_complete_request(cmd->rq); -} - static void lo_rw_aio_complete(struct kiocb *iocb, long ret, long ret2) { struct loop_cmd *cmd = container_of(iocb, struct loop_cmd, iocb); @@ -479,7 +470,7 @@ static void lo_rw_aio_complete(struct kiocb *iocb, long ret, long ret2) if (cmd->css) css_put(cmd->css); cmd->ret = ret; - lo_rw_aio_do_completion(cmd); + blk_mq_complete_request(cmd->rq); } static int lo_rw_aio(struct loop_device *lo, struct loop_cmd *cmd, @@ -527,7 +518,6 @@ static int lo_rw_aio(struct loop_device *lo, struct loop_cmd *cmd, bvec = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter); segments = bio_segments(bio); } - atomic_set(&cmd->ref, 2); iov_iter_bvec(&iter, ITER_BVEC | rw, bvec, segments, blk_rq_bytes(rq)); @@ -545,7 +535,7 @@ static int lo_rw_aio(struct loop_device *lo, struct loop_cmd *cmd, else ret = call_read_iter(file, &cmd->iocb, &iter); - lo_rw_aio_do_completion(cmd); + kfree(cmd->bvec); kthread_associate_blkcg(NULL); if (ret != -EIOCBQUEUED) diff --git a/drivers/block/loop.h b/drivers/block/loop.h index 0f45416e4fcf..ba65848c7c33 100644 --- a/drivers/block/loop.h +++ b/drivers/block/loop.h @@ -68,7 +68,6 @@ struct loop_cmd { struct kthread_work work; struct request *rq; bool use_aio; /* use AIO interface to handle I/O */ - atomic_t ref; /* only for aio */ long ret; struct kiocb iocb; struct bio_vec *bvec;