Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp243503iob; Tue, 3 May 2022 16:17:04 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxOhJm3m+wSgrdU5XtB4IB1pz6eDZ7DemsHVN34La7e1wm5ysCt8coEWWR2CjmsfAeoMLHK X-Received: by 2002:a17:90b:210a:b0:1db:cbc6:ca57 with SMTP id kz10-20020a17090b210a00b001dbcbc6ca57mr7304891pjb.69.1651619823919; Tue, 03 May 2022 16:17:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651619823; cv=none; d=google.com; s=arc-20160816; b=POuAOQnv6B/BeNMR0wOcJT+H7OAFWiefKgCOpts2kYdssx07qTJZa7s/FxZXF5gcqL KrEJaoWdWPQWo3KVg0jGLBUDZI07CTbU2EJh9DcUdaiVf+oGiMJ94wKD7wkJPvq4xKOu mcjI7cFjwSs6zBlXf+lVebKFcmkvcq3PV8jMPQ2VIweUGhYGbAyVGGcj8DR6hLJn+jS/ N7xR0aHtVzxBYF8jRppwYONjm4aDwlfL0CsuMJAQ7a29AL1/WrZNg3RsnSTYk8EuR8NO THRLBJjzPIw43gk/1gGobtrQbeCRoKRMevKBygD2aMJxUgCXAp0qE3/zEMIb+Mp0sgvD 72CA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=fB+GyMb8KaE7uGv5sjOmUbdvZf3IS0zLisgeiZ4V7NY=; b=o+hL2xaW2QwsIuWHP4nJ7gzeTNxqccsXih2C02sKTVqG1+tHM9y+iCfSssIlWtUV+p tODU7ei3PSoK1YUgjJ4IPuTmlV23RgcW5aUaqhTT/ceORjJeowWrIVSKZ6sM+zvvD8FW l9RdqYN3TD8hpLwqBJZeh5N6/VTKwEQkmxQc437evkZyo0zB58PE10fY/BKzGcrgafb5 8pCSkv6K/Xx5qrNsP6oCeM8AlcaE/1bcg4mukd25NdBCAy7PNs9J2oLvGqYMLBlusb9Q NafsyeXE9OI7CavfseMdDtwQI1D6tUJs4hDoMcqbmJWiDaQYx3HCIDpnOcTE2q208Fty 12cQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="KE1jdc//"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t133-20020a63788b000000b003ab2f093e45si18624200pgc.597.2022.05.03.16.16.39; Tue, 03 May 2022 16:17:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="KE1jdc//"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238384AbiECPXY (ORCPT + 99 others); Tue, 3 May 2022 11:23:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51750 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238433AbiECPXQ (ORCPT ); Tue, 3 May 2022 11:23:16 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 1C0C13D1DF for ; Tue, 3 May 2022 08:19:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1651591114; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=fB+GyMb8KaE7uGv5sjOmUbdvZf3IS0zLisgeiZ4V7NY=; b=KE1jdc//PDiDUuYHzUgU7zK8fu0wqfphmXD2DT2Ow/MhCXIeDFHu3HD56vYFX0q6+K3F/w FtlB5UOMukDz1+4jOXqsdjLffGUH+90845DGbWCQK+iLzry4b16NurOVs81GurlcDMjf5q I8t6u3APM2EM8r5Rgq2XmdpuTZGSY2o= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-131-oQEIyqTjMkqFzOBWMnv_aA-1; Tue, 03 May 2022 11:18:07 -0400 X-MC-Unique: oQEIyqTjMkqFzOBWMnv_aA-1 Received: by mail-wr1-f72.google.com with SMTP id s8-20020adf9788000000b0020adb01dc25so6461031wrb.20 for ; Tue, 03 May 2022 08:18:06 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=fB+GyMb8KaE7uGv5sjOmUbdvZf3IS0zLisgeiZ4V7NY=; b=rFRmR7mPNB5TrbwUL1hkl4H6c4dl5Lu+LmZjD2+1+BgR4UpRvoxEBOYQiFAzblyOzD +x7VyhqiX2t8AlnQGCw5JAU5nzB7rav1sQW6e5D1WmoRuwn/eX9MaB/IY26ipKPVAAOv xFTrVEy6RKMgfFYq3yhS0PfoMDVxmLvrrw+UhQ77qmaeCE5J72TX68/WUHkt8TkGazCD ClGbDUet9ysk9Byjow7yKoSz+zpgNRsTcJiSe69+kJT7ot+eeEd3ieMOfqOSDoMR88tW +ja9QIvu59ECFkWRS+gSSVw6K0bQXPaV6av+BxPHDJvLbrLXeR4TFH/VRUeLx8D2rMkD w/AQ== X-Gm-Message-State: AOAM531j55Yj2cJPqFdA4S0/ya5S4blwtBXoSQmSzWBqO+m1T1uDcf7a wTt9O5CoEEFGfw54ClMnEYzWJV3dzc2QHwm6nELytKL4wzgek8mul47uMY55Np7HsPU5Qc3rny8 b8KAP4p39Or4QQGNW+18csvgxtXyLvb+aYHyoJkd8 X-Received: by 2002:a05:600c:9:b0:393:ea67:1c68 with SMTP id g9-20020a05600c000900b00393ea671c68mr3474355wmc.92.1651584637395; Tue, 03 May 2022 06:30:37 -0700 (PDT) X-Received: by 2002:a05:600c:9:b0:393:ea67:1c68 with SMTP id g9-20020a05600c000900b00393ea671c68mr3474343wmc.92.1651584637194; Tue, 03 May 2022 06:30:37 -0700 (PDT) MIME-Version: 1.0 References: <20220426145445.2282274-1-agruenba@redhat.com> In-Reply-To: From: Andreas Gruenbacher Date: Tue, 3 May 2022 15:30:25 +0200 Message-ID: Subject: Re: [GIT PULL] gfs2 fix To: Linus Torvalds Cc: Christoph Hellwig , "Darrick J. Wong" , Dave Chinner , cluster-devel , Linux Kernel Mailing List Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-3.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, May 3, 2022 at 10:56 AM Andreas Gruenbacher wrote: > On Mon, May 2, 2022 at 8:32 PM Linus Torvalds wrote: > > On Thu, Apr 28, 2022 at 10:39 AM Andreas Gruenbacher wrote: > > > > > > Yes, but note that it's gfs2_file_buffered_write() that fails. When > > > the pagefault_disable/enable() around iomap_file_buffered_write() is > > > removed, the corruption goes away. > > > > I looked some more at this on and off, and ended up even more confused. > > > > For some reason, I'd mostly looked at the read case, because I had > > mis-read some of your emails and thought it was the buffered reads > > that caused problems. > > > > Then I went back more carefully, and realized you had always said > > gfs2_file_buffered_write() was where the issues happened, and looked > > at that path more, and that confused me even *MORE*. > > > > Because that case has always done the copy from user space with page > > faults disabled, because of the traditional deadlock with reading from > > user space while holding the page lock on the target page cache page. > > > > So that is not really about the new deadlock with filesystem locks, > > that was fixed by 00bfe02f4796 ("gfs2: Fix mmap + page fault deadlocks > > for buffered I/O"). > > > > So now that I'm looking at the right function (maybe) I'm going "huh", > > because it's none of the complex cases that would seem to fail, it's > > literally just the fault_in_iov_iter_readable() that we've always done > > in iomap_write_iter() that presumably starts failing. > > > > But *that* old code seems bogus too. It's doing > > > > if (unlikely(fault_in_iov_iter_readable(i, bytes) == bytes)) { > > status = -EFAULT; > > break; > > } > > > > which on the face of it is sane: it's saying "if we can't fault in any > > bytes, then stop trying". > > > > And it's good, and correct, but it does leave one case open. > > > > Because what if the result is "we can fault things in _partially_"? > > > > The code blithely goes on and tries to do the whole 'bytes' range _anyway_. > > > > Now, with a bug-free filesystem, this really shouldn't matter, since > > the later copy_page_from_iter_atomic() thing should then DTRT anyway, > > but this does mean that one fundamental thing that that commit > > 00bfe02f4796 changed is that it basically disabled that > > fault_in_iov_iter_readable() that *used* to fault in the whole range, > > and now potentially only faults in a small area. > > > > That, in turn, means that in practice it *used* to do "write_end()" > > with a fully successful range, ie when it did that > > > > status = a_ops->write_end(file, mapping, pos, bytes, copied, > > page, fsdata); > > > > then "bytes" and "copied" were the same. > > > > But now that commit 00bfe02f4796 added the "disable_pagefault()" > > around the whole thing, fault_in_iov_iter_readable() will easily fail > > half-way instead of bringing the next page in, and then that > > ->write_begin() to ->write_end() sequence will see the copy in the > > middle failing half-way too, and you'll have that write_end() > > condition with the write _partially_ succeeding. > > > > Which is the complex case for write_end() that you practically > > speaking never saw before (it *could* happen with a race with swap-out > > or similar, but it was not really something you could trigger in real > > life. > > > > And I suspect this is what bites you with gfs2 > > > > To *test* that hypothesis, how about you try this attached patch? The > > generic_perform_write() function in mm/filemap.c has the same exact > > pattern, but as mentioned, a filesystem really needs to be able to > > handle the partial write_end() case, so it's not a *bug* in that code, > > but it migth be triggering a bug in gfs2. > > > > And gfs2 only uses the iomap_write_iter() case, I think. So that's the > > only case this attached patch changes. > > > > Again - I think the unpatched iomap_write_iter() code is fine, but I > > think it may be what then triggers the real bug in gfs2. So this patch > > is not wrong per se, but this patch is basically a "hide the problem" > > patch, and it would be very interesting to hear if it does indeed fix > > your test-case. > > We still get data corruption with the patch applied. The > WARN_ON_ONCE(!bytes) doesn't trigger. > > As an additional experiment, I've added code to check the iterator > position that iomap_file_buffered_write() returns, and it's all > looking good as well: an iov_iter_advance(orig_from, written) from the > original position always gets us to the same iterator. > > This points at gfs2 getting things wrong after a short write, for > example, marking a page / folio uptodate that isn't. But the uptodate > handling happens at the iomap layer, so this doesn't leave me with an > immediate suspect. > > We're on filesystems with block size == page size, so none of the > struct iomap_page uptodata handling should be involved, either. The rounding around the hole punching in gfs2_iomap_end() looks wrong. I'm trying a fix now. > > Because that would pinpoint exactly what the bug is. > > > > I'm adding Christoph and Darrick as iomap maintainers here to the > > participants (and Dave Chinner in case he's also the temporary > > maintainer because Darrick is doing reviews) not because they > > necessarily care, but just because this test-patch obviously involves > > the iomap code. > > > > NOTE! This patch is entirely untested. I also didn't actually yet go > > look at what gfs2 does when 'bytes' and 'copied' are different. But > > since I finally think I figured out what might be going on, I decided > > I'd send this out sooner rather than later. > > > > Because this is the first thing that makes me go "Aaahh.. This might > > explain it". > > > > Linus > > Thanks, > Andreas