From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.5 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CACEDC433E7 for ; Fri, 16 Oct 2020 10:02:36 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 8527820866 for ; Fri, 16 Oct 2020 10:02:35 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="key not found in DNS" (0-bit key) header.d=szeredi.hu header.i=@szeredi.hu header.b="HxuXGdW5" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8527820866 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=szeredi.hu Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id D40A7940008; Fri, 16 Oct 2020 06:02:34 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CF24C900002; Fri, 16 Oct 2020 06:02:34 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C2EE6940008; Fri, 16 Oct 2020 06:02:34 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0221.hostedemail.com [216.40.44.221]) by kanga.kvack.org (Postfix) with ESMTP id AC0D0900002 for ; Fri, 16 Oct 2020 06:02:34 -0400 (EDT) Received: from smtpin21.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 656B91EE6 for ; Fri, 16 Oct 2020 10:02:34 +0000 (UTC) X-FDA: 77377349028.21.fall79_5d15b3d2721c Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin21.hostedemail.com (Postfix) with ESMTP id 42EC4180442C0 for ; Fri, 16 Oct 2020 10:02:34 +0000 (UTC) X-HE-Tag: fall79_5d15b3d2721c X-Filterd-Recvd-Size: 6324 Received: from mail-vk1-f193.google.com (mail-vk1-f193.google.com [209.85.221.193]) by imf25.hostedemail.com (Postfix) with ESMTP for ; Fri, 16 Oct 2020 10:02:33 +0000 (UTC) Received: by mail-vk1-f193.google.com with SMTP id m3so479580vki.12 for ; Fri, 16 Oct 2020 03:02:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=szeredi.hu; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=q2UAmLb0Qij04dFY8btDQwDS1sHsBxgRtlIjW8zX0hU=; b=HxuXGdW5l4w+ihhtKYRt3hD/bRT+8E5pe3iSfQPyrw6D67FeV6PB/zpzWqDX1xfdd8 4K6TneSSeRdgIYXCWGY8595WYwWcd46mNtnaUcSiwffKjIyI3j13jgudt36kpzzwWlop JG0WJgcGmXY6SXquDOu/toyDrsl3gtpsxJRu8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=q2UAmLb0Qij04dFY8btDQwDS1sHsBxgRtlIjW8zX0hU=; b=UfIzxnqMSIJuz55jr3HtOJL0T+njeX6Jzeaj0PRQl6N2ycpqxsut9ipO3Mqa5ZLmdK ReKb1g52bMahg99KPtJIYd+dSv7dUfYN/r4KSjoIkvMTUe2GnAVoxwIj33ageMzQgJSi QYLC2sBYAk5hpoe8fG1Yp7i3pNWxwWJGO2Xkzz5AgpcHSNvgtRjxcZ/72ptImQwDNnKJ D+VTb9dN32XiBmPxfowc7VP35PSvZac9JffnmBJWfihdwPeVGqZxYupLpOpyLeMBuNNa T5bSy9YPB6eJjTNyVtzbtekzxOa8E90zGyQ00sUge/k4XZLJ7M2C5uhs1cJ3DdmmLjmw IVaw== X-Gm-Message-State: AOAM532ujFUu0u+fzKOPWQs4+DoGVntuOHWgAz3ADdSVPY8Rt/fXdIJ4 mkqFXb2LearYYL6CBDkMcb0iezMqZvAQzYp+IZnSgA== X-Google-Smtp-Source: ABdhPJwi3kZwYWwdrcHl+IhxKB+EmYRh7nUgJOgGUsnavnybsSB+2XY2LVZ4yAEPcqe8eMB4+xdF+3zdjIQ6i1w+tBs= X-Received: by 2002:a1f:3144:: with SMTP id x65mr1594215vkx.3.1602842552307; Fri, 16 Oct 2020 03:02:32 -0700 (PDT) MIME-Version: 1.0 References: <4794a3fa3742a5e84fb0f934944204b55730829b.camel@lca.pw> <20201015151606.GA226448@redhat.com> <20201015195526.GC226448@redhat.com> In-Reply-To: From: Miklos Szeredi Date: Fri, 16 Oct 2020 12:02:21 +0200 Message-ID: Subject: Re: Possible deadlock in fuse write path (Was: Re: [PATCH 0/4] Some more lock_page work..) To: Linus Torvalds Cc: Vivek Goyal , Qian Cai , Hugh Dickins , Matthew Wilcox , "Kirill A . Shutemov" , Linux-MM , Andrew Morton , linux-fsdevel , Amir Goldstein Content-Type: text/plain; charset="UTF-8" X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Thu, Oct 15, 2020 at 11:22 PM Linus Torvalds wrote: > > On Thu, Oct 15, 2020 at 12:55 PM Vivek Goyal wrote: > > > > I am wondering how should I fix this issue. Is it enough that I drop > > the page lock (but keep the reference) inside the loop. And once copying > > from user space is done, acquire page locks for all pages (Attached > > a patch below). > > What is the page lock supposed to protect? > > Because whatever it protects, dropping the lock drops, and you'd need > to re-check whatever the page lock was there for. > > > Or dropping page lock means that there are no guarantees that this > > page did not get written back and removed from address space and > > a new page has been placed at same offset. Does that mean I should > > instead be looking up page cache again after copying from user > > space is done. > > I don't know why fuse does multiple pages to begin with. Why can't it > do whatever it does just one page at a time? > > But yes, you probably should look the page up again whenever you've > unlocked it, because it might have been truncated or whatever. > > Not that this is purely about unlocking the page, not about "after > copying from user space". The iov_iter_copy_from_user_atomic() part is > safe - if that takes a page fault, it will just do a partial copy, it > won't deadlock. > > So you can potentially do multiple pages, and keep them all locked, > but only as long as the copies are all done with that > "from_user_atomic()" case. Which normally works fine, since normal > users will write stuff that they just generated, so it will all be > there. > > It's only when that returns zero, and you do the fallback to pre-fault > in any data with iov_iter_fault_in_readable() that you need to unlock > _all_ pages (and once you do that, I don't see what possible advantage > the multi-page array can have). > > Of course, the way that code is written, it always does the > iov_iter_fault_in_readable() for each page - it's not written like > some kind of "special case fallback thing". This was added by commit ea9b9907b82a ("fuse: implement perform_write") in v2.6.26 and remains essentially unchanged, AFAICS. So this is an old bug indeed. So what is the page lock protecting? I think not truncation, because inode_lock should be sufficient protection. What it does after sending a synchronous WRITE and before unlocking the pages is set the PG_uptodate flag, but only if the complete page was really written, which is what the uptodate flag really says: this page is in sync with the underlying fs. So I think the page lock here is trying to protect against concurrent reads/faults on not uptodate pages. I.e. until the WRITE request completes it is unknown whether the page was really written or not, so any reads must block until this state becomes known. This logic falls down on already cached pages, since they start out uptodate and the write does not clear this flag. So keeping the pages locked has dubious value: short writes don't seem to work correctly anyway. Which means that we can probably just set the page uptodate right after filling it from the user buffer, and unlock the page immediately. Am I missing something? Thanks, Miklos