Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp5312112pxu; Tue, 22 Dec 2020 13:38:07 -0800 (PST) X-Google-Smtp-Source: ABdhPJyvVvBUSx36ATizLb5yAdhFW3mttdjbcv1+sIYY8Lab1kl0vR0vwFe4QktIQQ9B1Y4wC05c X-Received: by 2002:aa7:c84c:: with SMTP id g12mr21736692edt.193.1608673086930; Tue, 22 Dec 2020 13:38:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1608673086; cv=none; d=google.com; s=arc-20160816; b=jRoPAcSR8Vh9gemaiJYHW8yBo1pqzzAQiBtfg6+6ldS728rXAfNql3Re9zuWXbmYBb 0Pu7jx9GgB06IUBLwlfjDg+sH1Stt5b6tlAH8ydsu/XcMwsm9gag6/QjZFCW+bUNYvxa tI9+RA/yUtmy4NfNohGfvixSgLNEkCul8+CpKw8bDcgx3aXodcxnTKvpmCWZIze0/Mwd b8L2U5+z0/ZAtf50a8zllN4S+tRiqYNAIIyK6GFYnUoWg/1F1Akchz+OopDABK+a3AvP yXvuIIHFRn29VvZTTQIAoUluspjYddAJl2s2hUWajrxjh6gQp6lVRNcZTpf4n6JlbDuU trzw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=yBqsHakSTD7XDsy5mwErSb3uXLotZdoXMrPlsNRLmNc=; b=ufBB54xVUCDe340iwGzrrvSxWxmucZOAL7Ha+eXkdi+p3/Ym99f7MPMNAxU4YPzKb6 n6dgnSSCWlVEq0lufZXdBtMRMCGKYfkiYS2yhnpDcunW3v0MIPbk2yGOK7YO0cMd6fFY uWCRTlCnHNgJTmBq/Zy24ROlx+botSWyE/qj+wtCZTZJ7uKrhVWE6YwkdykGVX9yCVFl 07RRLG66Ui/hXuvljw3ngyRgGGBIlgz5Z5MQfDLC669RWwRpIU1Iggv1AmUepyuml1tp Gq4+iO8WAg3KFukLlBGtM1aEQ1VDHb6ArBnyKvAR079q9uKXYWjVYi5FG244TU75p9zf HQBg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Jz9UJGvP; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id dn3si12132783edb.35.2020.12.22.13.37.44; Tue, 22 Dec 2020 13:38:06 -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=@redhat.com header.s=mimecast20190719 header.b=Jz9UJGvP; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727865AbgLVVfv (ORCPT + 99 others); Tue, 22 Dec 2020 16:35:51 -0500 Received: from us-smtp-delivery-124.mimecast.com ([63.128.21.124]:39532 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726384AbgLVVfu (ORCPT ); Tue, 22 Dec 2020 16:35:50 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1608672864; 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=yBqsHakSTD7XDsy5mwErSb3uXLotZdoXMrPlsNRLmNc=; b=Jz9UJGvPaT8so7nYVhOSqVLBI7jvRszuYMIJR2t0mQdhXB59IFIgMopdYk65qriFJnYBS/ aOL0hEIdqQJsCLXT5xKLw5x210ZMRnivgwem8G0Rjux+cW1c8TY4q7yD7YdcaVzs12Ebe+ ODNE1s2qJdYUrW+qZ1lfTceVbPPd+0I= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-124-yRAbj35rPkKUEfuqOGmmYg-1; Tue, 22 Dec 2020 16:34:20 -0500 X-MC-Unique: yRAbj35rPkKUEfuqOGmmYg-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 6AD9C1005513; Tue, 22 Dec 2020 21:34:18 +0000 (UTC) Received: from mail (ovpn-112-5.rdu2.redhat.com [10.10.112.5]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 43C8E10013C1; Tue, 22 Dec 2020 21:34:15 +0000 (UTC) Date: Tue, 22 Dec 2020 16:34:14 -0500 From: Andrea Arcangeli To: Nadav Amit Cc: Andy Lutomirski , linux-mm , Peter Xu , lkml , Pavel Emelyanov , Mike Kravetz , Mike Rapoport , stable , Minchan Kim , Yu Zhao , Will Deacon , Peter Zijlstra Subject: Re: [PATCH] mm/userfaultfd: fix memory corruption due to writeprotect Message-ID: References: <20201219043006.2206347-1-namit@vmware.com> <719DF2CD-A0BC-4B67-9FBA-A9E0A98AA45E@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <719DF2CD-A0BC-4B67-9FBA-A9E0A98AA45E@gmail.com> User-Agent: Mutt/2.0.3 (2020-12-04) X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Dec 22, 2020 at 12:58:18PM -0800, Nadav Amit wrote: > I had somewhat similar ideas - saving in each page-struct the generation, > which would allow to: (1) extend pte_same() to detect interim changes > that were reverted (RO->RW->RO) and (2) per-PTE pending flushes. What don't you feel safe about, what's the problem with RO->RO->RO, I don't get it. The pte_same is perfectly ok without sequence counter in my view, I never seen anything that would not be ok with pte_same given all the invariant are respected. It's actually a great optimization compared to any unscalable sequence counter. The counter would slowdown everything, having to increase a counter every time you change a pte, no matter if it's a counter per pgtable or per-vma or per-mm, sounds very bad. I'd rather prefer to take mmap_lock_write across the whole userfaultfd ioctl, than having to deal with a new sequence counter increase for every pte modification on a heavily contended cacheline. Also note the counter would have solved nothing for userfaultfd_writeprotect, it's useless to detect stale TLB entries. See how !pte_write check happens after the counter was already increased: CPU0 CPU 1 CPU 2 ------ -------- ------- userfaultfd_wrprotect(mode_wp = true) PT lock atomic set _PAGE_UFFD_WP and clear _PAGE_WRITE false_shared_counter_counter++ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PT unlock do_page_fault FAULT_FLAG_WRITE userfaultfd_wrprotect(mode_wp = false) PT lock ATOMIC clear _PAGE_UFFD_WP <- problem /* _PAGE_WRITE not set */ false_shared_counter_counter++ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PT unlock XXXXXXXXXXXXXX BUG RACE window open here PT lock counter = false_shared_counter_counter ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ FAULT_FLAG_WRITE is set by CPU _PAGE_WRITE is still clear in pte PT unlock wp_page_copy copy_user_page runs with stale TLB pte_same(counter, orig_pte, pte) -> PASS ^^^^^^^ ^^^^ commit the copy to the pte with the lost writes deferred tlb flush <- too late XXXXXXXXXXXXXX BUG RACE window close here ================================================================================