Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp656698pxu; Thu, 7 Jan 2021 14:54:09 -0800 (PST) X-Google-Smtp-Source: ABdhPJwj0hqjsQwIcPCt9j88Y9f3XGk73KjZJolThaoim8F9l7Gj3hjX8xN3NgAco9uJJKZaJXZT X-Received: by 2002:a17:906:e206:: with SMTP id gf6mr765855ejb.342.1610060049130; Thu, 07 Jan 2021 14:54:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610060049; cv=none; d=google.com; s=arc-20160816; b=rvWSy11GGi9UmuX9WjhQx1uVCVgKUDhDz+gxFz7FAcbCnDQPe7AIw3G5jQb+u0CtJg wj8d/Os9Lk3Kxb6IZLsi6fhINzrvs3Zd8sVZDUdal7bBV/7Lw3Gjtr9COsywDaSE1USu 62lY5SwUlG6X3UbKgHAGM/E9qyVWozKLGqcIZ3Z0oZKqf9A7k1r7noNziyoTTuVEq4i3 r8O8ErF9vI7ni13iUEOc+2jKKOlUJsrMgNALkl+bB7v/HFwzrt8+utluqWqiZ3n5Eq2d UBsZV5AJ8ysdEVGVchPjQaheHc9VEniJ5gHSCoqpaHpFunJlm2A9DTPrh0+EON+w6sLl z54A== 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=MpUXPlOpjoaY/t+m2AK2xZLsPyG5FSaY9KMpLdS2dhw=; b=zdnjeSJDeMN8IjJvXoJwEYF6m/SA7tMsnvr0hAVM/9+9wR097b+2SbwQsi/YQr8pde fCFZGJF9YMxXykn5RzMEP7ok2V6eSYdKlDvlW5Ln5L4qiJj9BWNUWvKbxC32eYjMtgYz pFFS1uU9u2HUAza7KJq4D1tstun+q5O82UYKIoNrecXGvhlC5azpjvhmm/BalqiO1x8m aRE19QnWMQinbb3ONc++DPRqEvuZef6wEUMK1AdJTTGJstGhWBtn3qobFGb7zfqEpWhA /Y4aXu+q8RfOcRfZTKulPb7ztOoiZEFRNJ97hhNYNTuwYNISboQzRye0C8YiZ/RMDJxx 4jLw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=google header.b=G4oJ39ti; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id nt21si2677103ejb.739.2021.01.07.14.53.45; Thu, 07 Jan 2021 14:54:09 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=google header.b=G4oJ39ti; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727873AbhAGWwZ (ORCPT + 99 others); Thu, 7 Jan 2021 17:52:25 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50616 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727669AbhAGWwY (ORCPT ); Thu, 7 Jan 2021 17:52:24 -0500 Received: from mail-lf1-x129.google.com (mail-lf1-x129.google.com [IPv6:2a00:1450:4864:20::129]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4ADEEC0612F4 for ; Thu, 7 Jan 2021 14:51:44 -0800 (PST) Received: by mail-lf1-x129.google.com with SMTP id o13so18539789lfr.3 for ; Thu, 07 Jan 2021 14:51:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=google; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=MpUXPlOpjoaY/t+m2AK2xZLsPyG5FSaY9KMpLdS2dhw=; b=G4oJ39ti8dgcRqvI7baXd54HVqR6F0iRx/fnKM96EUoWqgYKtQCN07SCaol2E0qoZA 1/0qyVSNPbPjKiFYtyydwwn63S8AffyVbCftg3OOFVFN20qvxP36tMnvcmtcQcHudZJa XZ2BPp/sEsPoEEDpsVVoQUye8djTn0iPsEm0M= 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=MpUXPlOpjoaY/t+m2AK2xZLsPyG5FSaY9KMpLdS2dhw=; b=mjOyo7oeX0S15nE1IhQ8/wDhunb3IJvQ+BktQI+rvMd83ZXvUryeoB6WAVU/eGR3jv nw08yz8FLEm8PiEkrsiJPleYwnuPochaHWhEH/D1T/eCeiU/gFl4lF9XSnblBmEpfwvM coP/mODeUCAiUAyeQ9BeEhla4ou52EAbGcYAXpVCobaGJBu8CzGpXHnjAZ4u/hjjDa0Z ZABZS2vH2Lo+jZJUU2jXwYWFHq7HpZVhUQWS5wR0sjmm5m27dkOVLTNV4lALyn509YzN ihrqT/WXd4FkLM5UKXI6xPyVVR5DU5n16mx5YocPaOF87fLEIME1rRIAGRobcVMcuGOo SayA== X-Gm-Message-State: AOAM530PBiQJYfUSriogvs8B24G5ecMN1AHgnJGqVswssjtxe5yd7ecB 9OypayXpfDjczucFCfrYeavl+Vk7a4XSgA== X-Received: by 2002:a2e:87cb:: with SMTP id v11mr276920ljj.218.1610059902216; Thu, 07 Jan 2021 14:51:42 -0800 (PST) Received: from mail-lf1-f51.google.com (mail-lf1-f51.google.com. [209.85.167.51]) by smtp.gmail.com with ESMTPSA id s4sm1593726ljp.123.2021.01.07.14.51.40 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 07 Jan 2021 14:51:41 -0800 (PST) Received: by mail-lf1-f51.google.com with SMTP id h205so18497893lfd.5 for ; Thu, 07 Jan 2021 14:51:40 -0800 (PST) X-Received: by 2002:a2e:b4af:: with SMTP id q15mr255547ljm.507.1610059900270; Thu, 07 Jan 2021 14:51:40 -0800 (PST) MIME-Version: 1.0 References: <20210107200402.31095-1-aarcange@redhat.com> <20210107200402.31095-3-aarcange@redhat.com> In-Reply-To: From: Linus Torvalds Date: Thu, 7 Jan 2021 14:51:24 -0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH 2/2] mm: soft_dirty: userfaultfd: introduce wrprotect_tlb_flush_pending To: Andrea Arcangeli Cc: Linux-MM , Linux Kernel Mailing List , Yu Zhao , Andy Lutomirski , Peter Xu , Pavel Emelyanov , Mike Kravetz , Mike Rapoport , Minchan Kim , Will Deacon , Peter Zijlstra , Hugh Dickins , "Kirill A. Shutemov" , Matthew Wilcox , Oleg Nesterov , Jann Horn , Kees Cook , John Hubbard , Leon Romanovsky , Jason Gunthorpe , Jan Kara , Kirill Tkhai Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Jan 7, 2021 at 2:42 PM Linus Torvalds wrote: > > But I thought we agreed earlier that that is a bug. And I thought the > softdirty code already got it for writing. Ho humm. I had obviously not looked very much at that code. I had done a quick git grep, but now that I look closer, it *does* get the mmap_sem for writing, but only for that VM_SOFTDIRTY bit clearing, and then it does a mmap_write_downgrade(). So that's why I had looked more at the UFFD code, because that one was the one I was aware of doing this all with just the read lock. I _thought_ the softdirty code already took the write lock and wouldn't race with page faults. But I had missed that write_downgrade. So yeah, this code has the same issue. Anyway, the fix is - I think - the same I outlined earlier when I was talking about UFFD: take the thing for writing, so that you can't race. The alternate fix remains "make sure we always flush the TLB before releasing the page table lock, and make COW do the copy under the page table lock". But I really would prefer to just have this code follow all the usual rules, and if it does a write protect, then it should take the mmap_sem for writing. Why is that very simple rule so bad? (And see my unrelated but incidental note on it being a good idea to try to minimize latency by making surfe we don't do any IO under the mmap lock - whether held for reading _or_ writing. Because I do think we can improve in that area, if you have some good test-case). Linus