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=-18.3 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham 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 2C80BC433DB for ; Wed, 23 Dec 2020 22:29:59 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id AB86622288 for ; Wed, 23 Dec 2020 22:29:58 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org AB86622288 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id BEC918D0054; Wed, 23 Dec 2020 17:29:57 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B9CD68D0026; Wed, 23 Dec 2020 17:29:57 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A8C118D0054; Wed, 23 Dec 2020 17:29:57 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0139.hostedemail.com [216.40.44.139]) by kanga.kvack.org (Postfix) with ESMTP id 9303B8D0026 for ; Wed, 23 Dec 2020 17:29:57 -0500 (EST) Received: from smtpin05.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 4489D181AEF23 for ; Wed, 23 Dec 2020 22:29:57 +0000 (UTC) X-FDA: 77625990834.05.care66_5f0a1d92746c Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin05.hostedemail.com (Postfix) with ESMTP id 2546C18014A02 for ; Wed, 23 Dec 2020 22:29:57 +0000 (UTC) X-HE-Tag: care66_5f0a1d92746c X-Filterd-Recvd-Size: 6365 Received: from mail-io1-f54.google.com (mail-io1-f54.google.com [209.85.166.54]) by imf50.hostedemail.com (Postfix) with ESMTP for ; Wed, 23 Dec 2020 22:29:56 +0000 (UTC) Received: by mail-io1-f54.google.com with SMTP id p187so584863iod.4 for ; Wed, 23 Dec 2020 14:29:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=61D1avpBNAQTrzPUC+V3vNlrIkz1Ho8e0u54ev6p94E=; b=v83tm17hJbXD1YD85KCQF9w+wyVDxiP+lP8+ApqT44Fv6bBx7iFtr7ccCmK5kdUYlu D7WN2A8OYjBztmOn9jtKbAxkcrgq0tNqr+noMi0sW22XxNftmBIsXzGsC75BGQRPvTCK U91xjpkSQwk77RUF6kEW0aivXAz7T0mkqiRkCJ95cjnPw9pUp/gzQGGVS4aOa6IhFJYJ g/J0dCArwhHApHEaH1eCDjxKlxcLtj8AjJlYpaDqNXaN8JydilT8fiySksFbOFgQNtZf o7I5TeJu3G/RHrlSc0G0F4BCYPmTrV+hSKGU2bc1egvMk//xb23kfiqa8pc/l0vCf3Z8 rBCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=61D1avpBNAQTrzPUC+V3vNlrIkz1Ho8e0u54ev6p94E=; b=Rnn5Q2Xub0dpJW/KAe4JdYhJqEVIuBwOslyL9UEMO0wdjfX8M68e93IghVSIQtRqRM MDvO7/amJmaRCbYOgOjhySRdQohMMMNKAjr17DD8Yhuy9zbaL6JLb9csglHwc2yBhgeX AZC4sP4OINOpilFLfRrVYT49eJN9FpirZPCl07gwNkdTBnakfkin4jvvpaPf0hTJ+zbJ i52JUhZ1Obzl2WuLTL/r9O2SBYGfjh4IaxpGdcYrnt1xWBF2Dk+K7+zC0Jmdjq1Wb1U2 hepI0IFT4uloh2bPebAVQQFsM8rFQNqAyePkXC6dbYqTXQCr0TkfCd61JyFy8rqUjxEC gBIg== X-Gm-Message-State: AOAM531PbRUqpi6taavCG0qOplm8AX4IyyZXDg38oDpMEWh8rpBWUKCU 17NlLaNsRuzs3njzLY0J2mbNxA== X-Google-Smtp-Source: ABdhPJxOgHitNDbwpl4RFoLQWRW+gwIhTHV1P5RAby/BaeyOl3GQispO1B3AccbMuwC1XhAY+dS3gg== X-Received: by 2002:a02:7a50:: with SMTP id z16mr24430480jad.87.1608762595879; Wed, 23 Dec 2020 14:29:55 -0800 (PST) Received: from google.com ([2620:15c:183:200:7220:84ff:fe09:2d90]) by smtp.gmail.com with ESMTPSA id w3sm18872116ilk.17.2020.12.23.14.29.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Dec 2020 14:29:55 -0800 (PST) Date: Wed, 23 Dec 2020 15:29:51 -0700 From: Yu Zhao To: Andrea Arcangeli Cc: Andy Lutomirski , Linus Torvalds , Peter Xu , Nadav Amit , linux-mm , lkml , Pavel Emelyanov , Mike Kravetz , Mike Rapoport , stable , Minchan Kim , Will Deacon , Peter Zijlstra Subject: Re: [PATCH] mm/userfaultfd: fix memory corruption due to writeprotect Message-ID: References: <20201221223041.GL6640@xz-x1> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: 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 Wed, Dec 23, 2020 at 04:39:00PM -0500, Andrea Arcangeli wrote: > On Tue, Dec 22, 2020 at 08:36:04PM -0700, Yu Zhao wrote: > > Thanks for the details. > > I hope we can find a way put the page_mapcount back where there's a > page_count right now. > > If you're so worried about having to maintain a all defined well > documented (or to be documented even better if you ACK it) > marker/catcher for userfaultfd_writeprotect, I can't see how you could > consider to maintain the page fault safe against any random code > leaving too permissive TLB entries out of sync of the more restrictive > pte permissions as it was happening with clear_refs_write, which > worked by luck until page_mapcount was changed to page_count. > > page_count is far from optimal, but it is a feature it finally allowed > us to notice that various code (clear_refs_write included apparently > even after the fix) leaves stale too permissive TLB entries when it > shouldn't. > > The question is only which way you prefer to fix clear_refs_write and > I don't think we can deviate from those 3 methods that already exist > today. So clear_refs_write will have to pick one of those and > currently it's not falling in the same category with mprotect even > after the fix. > > I think if clear_refs_write starts to take the mmap_write_lock and > really start to operate like mprotect, only then we can consider to > make userfaultfd_writeprotect also operate like mprotect. > > Even then I'd hope we can at least be allowed to make it operate like > KSM write_protect_page for len <= HPAGE_PMD_SIZE, something that > clear_refs_write cannot do since it works in O(N) and tends to scan > everything at once, so there would be no point to optimize not to > defer the flush, for a process with a tiny amount of virtual memory > mapped. > > vm86 also should be fixed to fall in the same category with mprotect, > since performance there is irrelevant. I was hesitant to suggest the following because it isn't that straight forward. But since you seem to be less concerned with the complexity, I'll just bring it on the table -- it would take care of both ufd and clear_refs_write, wouldn't it? diff --git a/mm/memory.c b/mm/memory.c index 5e9ca612d7d7..af38c5ee327e 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -4403,8 +4403,11 @@ static vm_fault_t handle_pte_fault(struct vm_fault *vmf) goto unlock; } if (vmf->flags & FAULT_FLAG_WRITE) { - if (!pte_write(entry)) + if (!pte_write(entry)) { + if (mm_tlb_flush_pending(vmf->vma->vm_mm)) + flush_tlb_page(vmf->vma, vmf->address); return do_wp_page(vmf); + } entry = pte_mkdirty(entry); } entry = pte_mkyoung(entry);