From patchwork Sun Apr 1 16:33:31 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ilya Dryomov X-Patchwork-Id: 10319203 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 92F3D60247 for ; Sun, 1 Apr 2018 16:33:45 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7C56928FBA for ; Sun, 1 Apr 2018 16:33:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7080D28FC6; Sun, 1 Apr 2018 16:33:45 +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_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID, T_TVD_MIME_EPI autolearn=ham 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 3F0FB28FBA for ; Sun, 1 Apr 2018 16:33:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753716AbeDAQde (ORCPT ); Sun, 1 Apr 2018 12:33:34 -0400 Received: from mail-it0-f49.google.com ([209.85.214.49]:53475 "EHLO mail-it0-f49.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753708AbeDAQdc (ORCPT ); Sun, 1 Apr 2018 12:33:32 -0400 Received: by mail-it0-f49.google.com with SMTP id m134-v6so16498128itb.3 for ; Sun, 01 Apr 2018 09:33:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=fKmpYYI7U+ovhfcW+uPEywc1JnzjqVhVXT0sV23qW0w=; b=dKqzkwEwHEzCbfe59y26CaVNbBvOOn4TrOmx/ECwSU+qGA1XUJ05YzG5puOgYlQD6C XVFhmV6WZLp2J6bhIOvh6CFQG6HWLTonMrqY6Vo51q12MCWS6ybWSk/YxCGJFuT29E77 ykfQscZYAAQ/RyHaCdF8xNMJ56I0RM4mCzs83SB1FDeZYxsz+6stR99XMHVdrP+x7lrA nQMSTzlYYOcIT+FSWGOsb3tZuohVqveoEPSsElH1y1zXplOOZsnfSwqF1z68tuWC0AZr 2dD6ZgXyETvjbauyWS564xCUPl/FtiNnkRcdQtyfc181IkemFmuiP6C/howprIU8ZA15 ULkQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=fKmpYYI7U+ovhfcW+uPEywc1JnzjqVhVXT0sV23qW0w=; b=ez8bySfFc1ltSU9NeOQ2k4iS+m2k8I89wop5zfFS16SvmzNHFRG5RpgPDF2cd7N56R lvIZtE02/Km2Ku1giZ7yVt9xpJ2bQmlqf+huOH07p0l+FJq0bTtAuuyPZwJfUuJSSR1a 3FZQ7iW0LspBVMd6FCorzeXaDf09w9CBLdE4NCtMd+IaDk96JAx7gkpNwa969JOUsVPe I/QPY8K1/AJ2Y7YnkUnUQWkzIK5uld8zlkI/8lL+Ea3AJ2GNDLjJ7W0+0vcNgtB+paOa CoV1kNcFdg8fDWbE2Bt42EmEfP5jNFNuQ20hULDCir2QeS73ic4XwF8Z7JQTOzoaMSzY n93g== X-Gm-Message-State: ALQs6tCZk1MDSeq7h7fNJz5PJDE7XvQ1rslm+ajhQsQFz4rkktN6oExN iYX30SOwZ+9aIwQUqgYF6dyv5y1jcXgewsm3rKkATcvM X-Google-Smtp-Source: AIpwx480h9pxh2R8esUTtYPQuGBwhLNNDUJBnhhjYNVKGdCT7KCSJJFGZGcj9RdK9Z1Q2VW6MWoiDuMrCO4SPhKuPUs= X-Received: by 2002:a24:8ec7:: with SMTP id h190-v6mr9557701ite.34.1522600412196; Sun, 01 Apr 2018 09:33:32 -0700 (PDT) MIME-Version: 1.0 Received: by 10.2.180.251 with HTTP; Sun, 1 Apr 2018 09:33:31 -0700 (PDT) In-Reply-To: <1522437252.32547.14.camel@redhat.com> References: <20180316033212.30543-1-zyan@redhat.com> <950C3A6F-D935-4C8B-AA3A-B9E1C8B63802@redhat.com> <87370h7tes.fsf@suse.com> <1522437252.32547.14.camel@redhat.com> From: Ilya Dryomov Date: Sun, 1 Apr 2018 18:33:31 +0200 Message-ID: Subject: Re: [PATCH] ceph: only dirty ITER_IOVEC pages for direct read To: Jeff Layton Cc: Luis Henriques , "Yan, Zheng" , Ceph Development , Al Viro Sender: ceph-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: ceph-devel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP On Fri, Mar 30, 2018 at 9:14 PM, Jeff Layton wrote: > On Fri, 2018-03-30 at 11:24 +0100, Luis Henriques wrote: >> (CC'ing Jeff) >> >> Ilya Dryomov writes: >> >> > On Fri, Mar 30, 2018 at 9:26 AM, Yan, Zheng wrote: >> > > >> > > >> > > > On 30 Mar 2018, at 15:20, Ilya Dryomov wrote: >> > > > >> > > > On Fri, Mar 30, 2018 at 5:21 AM, Yan, Zheng wrote: >> > > > > >> > > > > >> > > > > > On 30 Mar 2018, at 00:49, Ilya Dryomov wrote: >> > > > > > >> > > > > > On Fri, Mar 16, 2018 at 4:32 AM, Yan, Zheng wrote: >> > > > > > >> > > > > > Hi Zheng, >> > > > > > >> > > > > > Can you explain how this fixes the invalid memory access reported in >> > > > > > "KASAN: use-after-free Read in set_page_dirty_lock"? Did you come up >> > > > > > with a different reproducer? >> > > > > > >> > > > > >> > > > > I don\u2019t know why KASAN reports use-after-free (like false alarm) in >> > > > > this case. I compared the code with other filesystem, found recent commits >> > > > > make other filesystems only dirty ITER_IOVEC pages. The change should also >> > > > > make KASAN silent. >> > > > >> > > > Did you manage to reproduce that KASAN report? >> > > > >> > > >> > > no >> > > >> > > > If it is a false positive, the patch shouldn't have been marked for >> > > > stable. >> > > > >> > > >> > > I CCed stable because fuse did the same >> > > >> > > commit 8fba54aebbdf1f999738121922e74bf796ad60ee >> > > Author: Miklos Szeredi >> > > Date: Wed Aug 24 18:17:04 2016 +0200 >> > > >> > > fuse: direct-io: don't dirty ITER_BVEC pages >> > > >> > > When reading from a loop device backed by a fuse file it deadlocks on >> > > lock_page(). >> > > >> > > This is because the page is already locked by the read() operation done on >> > > the loop device. In this case we don't want to either lock the page or >> > > dirty it. >> > > >> > > So do what fs/direct-io.c does: only dirty the page for ITER_IOVEC vectors. >> > >> > OK, so it is a potential deadlock which has nothing to do with that >> > KASAN report. I have rewritten the changelog to reflect that, please >> > take a look: >> > >> > https://github.com/ceph/ceph-client/commit/85784f9395987a422fa04263e7c0fb13da11eb5c >> >> Just as a side note, it's still trivial to get a cephfs-related KASAN >> report by running a fuzzer (trinity in my case) against a mainline >> kernel 4.16.0-rc7 with this fix backported. >> > > I suspect trinity is clobbering the array >> As Jeff mentioned in a different thread, splice syscall is broken on >> cephfs and the fix for it is still tagged as "DO NOT MERGE" in the >> ceph-client testing branch. I still think there's some bug in Jeff's >> fix as I still see a crash occasionally, but I haven't yet finished >> debugging it. Unfortunately, Jeff's fix is probably not appropriate for >> stable kernels (but may I'm wrong). >> >> Cheers, > > (cc'ing Al) > > Yeah, I should have revisited this a while ago. So the deal is that I > posted this patch upstream around a year ago, but Al didn't really like > it: > > https://patchwork.kernel.org/patch/9541829/ > > He wanted to add some iov_iter_for_each_page infrastructure and base > the new function on that. I had thought he had something queued up > along those lines at one point, but I don't see anything in mainline so > far. > > There is a iov_iter_for_each_range, but it doesn't handle userland > iovecs and doesn't seem to care about alignment. I think we'll need > both here. > > I'm not sure however that a per-page callback is really that helpful > for callers like ceph_direct_read_write. We'll just end up having to do > more work in the fs to batch up the pages before we add the op to the > req. > > iov_iter_get_pages_alloc is really sort of what we want in this case, > and we want that function to stitch together longer arrays when it can. > > Al, do you have any thoughts on what we should do here? I think this can be fixed entirely in ceph by walking away from page vectors (barely tested): https://github.com/ceph/ceph-client/commits/wip-splice If folks would like to see something like iov_iter_get_bvecs{,_alloc}() in iov_iter.c, I can work on generalizing my helper (i.e. add maxbvecs and maxsize parameters -- I didn't need maxbvecs and maxsize is just "make a copy of the iterator and truncate it"). Otherwise we will probably merge it into ceph, as this code has remained broken for way too long... Attached is the main patch from wip-splice. Thanks, Ilya From b3f10930acba4bd9a805921dcb98070475a05261 Mon Sep 17 00:00:00 2001 From: Ilya Dryomov Date: Sun, 1 Apr 2018 17:20:31 +0200 Subject: [PATCH 3/3] ceph: fix iov_iter issues in ceph_direct_read_write() dio_get_pagev_size() and dio_get_pages_alloc() introduced in commit b5b98989dc7e ("ceph: combine as many iovec as possile into one OSD request") assume that the passed iov_iter is ITER_IOVEC. This isn't the case with splice where it ends up poking into the guts of ITER_BVEC or ITER_PIPE iterators, causing lockups and crashes. Rather than trying to figure out gap alignment and stuff pages into a page vector, add a helper for going from iov_iter to a bio_vec array and make use of the new CEPH_OSD_DATA_TYPE_BVECS code. Signed-off-by: Ilya Dryomov --- fs/ceph/file.c | 171 ++++++++++++++++++++++++++++++--------------------------- 1 file changed, 91 insertions(+), 80 deletions(-) diff --git a/fs/ceph/file.c b/fs/ceph/file.c index 4a92acba1e9c..66f5e13a0804 100644 --- a/fs/ceph/file.c +++ b/fs/ceph/file.c @@ -69,70 +69,76 @@ static __le32 ceph_flags_sys2wire(u32 flags) * need to wait for MDS acknowledgement. */ -/* - * Calculate the length sum of direct io vectors that can - * be combined into one page vector. - */ -static size_t dio_get_pagev_size(const struct iov_iter *it) +static ssize_t __iter_get_bvecs(struct iov_iter *i, struct bio_vec *bvecs) { - const struct iovec *iov = it->iov; - const struct iovec *iovend = iov + it->nr_segs; - size_t size; - - size = iov->iov_len - it->iov_offset; - /* - * An iov can be page vectored when both the current tail - * and the next base are page aligned. - */ - while (PAGE_ALIGNED((iov->iov_base + iov->iov_len)) && - (++iov < iovend && PAGE_ALIGNED((iov->iov_base)))) { - size += iov->iov_len; - } - dout("dio_get_pagevlen len = %zu\n", size); - return size; + size_t total = 0; + int bvec_idx = 0; + + while (iov_iter_count(i)) { + struct page *pages[16]; + ssize_t bytes; + size_t start; + int idx = 0; + + bytes = iov_iter_get_pages(i, pages, SIZE_MAX, 16, &start); + if (bytes < 0) + return total ?: bytes; + + for ( ; bytes; idx++, bvec_idx++) { + struct bio_vec bv = { + .bv_page = pages[idx], + .bv_len = min_t(int, bytes, PAGE_SIZE - start), + .bv_offset = start, + }; + + bvecs[bvec_idx] = bv; + iov_iter_advance(i, bv.bv_len); + total += bv.bv_len; + bytes -= bv.bv_len; + start = 0; + } + } + + return total; } -/* - * Allocate a page vector based on (@it, @nbytes). - * The return value is the tuple describing a page vector, - * that is (@pages, @page_align, @num_pages). - */ -static struct page ** -dio_get_pages_alloc(const struct iov_iter *it, size_t nbytes, - size_t *page_align, int *num_pages) +static ssize_t iter_get_bvecs_alloc(struct iov_iter *i, size_t maxsize, + struct bio_vec **bvecs, int *num_bvecs) { - struct iov_iter tmp_it = *it; - size_t align; - struct page **pages; - int ret = 0, idx, npages; + struct iov_iter ii = *i; + struct bio_vec *bv; + ssize_t bytes; + int npages; + + iov_iter_truncate(&ii, maxsize); + npages = iov_iter_npages(&ii, INT_MAX); + bv = kcalloc(npages, sizeof(*bv), GFP_KERNEL); + if (!bv) + return -ENOMEM; - align = (unsigned long)(it->iov->iov_base + it->iov_offset) & - (PAGE_SIZE - 1); - npages = calc_pages_for(align, nbytes); - pages = kvmalloc(sizeof(*pages) * npages, GFP_KERNEL); - if (!pages) - return ERR_PTR(-ENOMEM); + bytes = __iter_get_bvecs(&ii, bv); + if (bytes < 0) { + kfree(bv); + return bytes; + } - for (idx = 0; idx < npages; ) { - size_t start; - ret = iov_iter_get_pages(&tmp_it, pages + idx, nbytes, - npages - idx, &start); - if (ret < 0) - goto fail; + *bvecs = bv; + *num_bvecs = npages; + return bytes; +} - iov_iter_advance(&tmp_it, ret); - nbytes -= ret; - idx += (ret + start + PAGE_SIZE - 1) / PAGE_SIZE; - } +static void put_bvecs(struct bio_vec *bvecs, int num_bvecs, bool should_dirty) +{ + int i; - BUG_ON(nbytes != 0); - *num_pages = npages; - *page_align = align; - dout("dio_get_pages_alloc: got %d pages align %zu\n", npages, align); - return pages; -fail: - ceph_put_page_vector(pages, idx, false); - return ERR_PTR(ret); + for (i = 0; i < num_bvecs; i++) { + if (bvecs[i].bv_page) { + if (should_dirty) + set_page_dirty_lock(bvecs[i].bv_page); + put_page(bvecs[i].bv_page); + } + } + kfree(bvecs); } /* @@ -744,11 +750,12 @@ static void ceph_aio_complete_req(struct ceph_osd_request *req) struct inode *inode = req->r_inode; struct ceph_aio_request *aio_req = req->r_priv; struct ceph_osd_data *osd_data = osd_req_op_extent_osd_data(req, 0); - int num_pages = calc_pages_for((u64)osd_data->alignment, - osd_data->length); - dout("ceph_aio_complete_req %p rc %d bytes %llu\n", - inode, rc, osd_data->length); + BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_BVECS); + BUG_ON(!osd_data->num_bvecs); + + dout("ceph_aio_complete_req %p rc %d bytes %u\n", + inode, rc, osd_data->bvec_pos.iter.bi_size); if (rc == -EOLDSNAPC) { struct ceph_aio_work *aio_work; @@ -766,9 +773,10 @@ static void ceph_aio_complete_req(struct ceph_osd_request *req) } else if (!aio_req->write) { if (rc == -ENOENT) rc = 0; - if (rc >= 0 && osd_data->length > rc) { - int zoff = osd_data->alignment + rc; - int zlen = osd_data->length - rc; + if (rc >= 0 && osd_data->bvec_pos.iter.bi_size > rc) { + struct iov_iter i; + int zlen = osd_data->bvec_pos.iter.bi_size - rc; + /* * If read is satisfied by single OSD request, * it can pass EOF. Otherwise read is within @@ -783,13 +791,16 @@ static void ceph_aio_complete_req(struct ceph_osd_request *req) aio_req->total_len = rc + zlen; } - if (zlen > 0) - ceph_zero_page_vector_range(zoff, zlen, - osd_data->pages); + iov_iter_bvec(&i, ITER_BVEC, osd_data->bvec_pos.bvecs, + osd_data->num_bvecs, + osd_data->bvec_pos.iter.bi_size); + iov_iter_advance(&i, rc); + iov_iter_zero(zlen, &i); } } - ceph_put_page_vector(osd_data->pages, num_pages, aio_req->should_dirty); + put_bvecs(osd_data->bvec_pos.bvecs, osd_data->num_bvecs, + aio_req->should_dirty); ceph_osdc_put_request(req); if (rc < 0) @@ -877,7 +888,7 @@ ceph_direct_read_write(struct kiocb *iocb, struct iov_iter *iter, struct ceph_fs_client *fsc = ceph_inode_to_client(inode); struct ceph_vino vino; struct ceph_osd_request *req; - struct page **pages; + struct bio_vec *bvecs; struct ceph_aio_request *aio_req = NULL; int num_pages = 0; int flags; @@ -912,8 +923,7 @@ ceph_direct_read_write(struct kiocb *iocb, struct iov_iter *iter, } while (iov_iter_count(iter) > 0) { - u64 size = dio_get_pagev_size(iter); - size_t start = 0; + u64 size = iov_iter_count(iter); ssize_t len; vino = ceph_vino(inode); @@ -936,11 +946,10 @@ ceph_direct_read_write(struct kiocb *iocb, struct iov_iter *iter, else size = min_t(u64, size, fsc->mount_options->rsize); - len = size; - pages = dio_get_pages_alloc(iter, len, &start, &num_pages); - if (IS_ERR(pages)) { + len = iter_get_bvecs_alloc(iter, size, &bvecs, &num_pages); + if (len < 0) { ceph_osdc_put_request(req); - ret = PTR_ERR(pages); + ret = len; break; } @@ -975,8 +984,7 @@ ceph_direct_read_write(struct kiocb *iocb, struct iov_iter *iter, req->r_mtime = mtime; } - osd_req_op_extent_osd_data_pages(req, 0, pages, len, start, - false, false); + osd_req_op_extent_osd_data_bvecs(req, 0, bvecs, num_pages, len); if (aio_req) { aio_req->total_len += len; @@ -1002,18 +1010,21 @@ ceph_direct_read_write(struct kiocb *iocb, struct iov_iter *iter, if (ret == -ENOENT) ret = 0; if (ret >= 0 && ret < len && pos + ret < size) { + struct iov_iter i; int zlen = min_t(size_t, len - ret, size - pos - ret); - ceph_zero_page_vector_range(start + ret, zlen, - pages); + + iov_iter_bvec(&i, ITER_BVEC, bvecs, num_pages, + len); + iov_iter_advance(&i, ret); + iov_iter_zero(zlen, &i); ret += zlen; } if (ret >= 0) len = ret; } - ceph_put_page_vector(pages, num_pages, should_dirty); - + put_bvecs(bvecs, num_pages, should_dirty); ceph_osdc_put_request(req); if (ret < 0) break; -- 2.4.3