Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp1617019pxu; Sun, 6 Dec 2020 01:41:11 -0800 (PST) X-Google-Smtp-Source: ABdhPJy8AeY4HC3hZXaPF7+xjnRz9200ql+ujHIo957CJx5VW/iM1wMDE9RZuU7sZ1E9ZvsRMMMK X-Received: by 2002:a17:906:7687:: with SMTP id o7mr14683852ejm.209.1607247671457; Sun, 06 Dec 2020 01:41:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1607247671; cv=none; d=google.com; s=arc-20160816; b=tyT58gFNPCvqn4nJgdkgrmLEg+O2CaJOmA0lwKOG4ByGKNl1PYp1SUxtdHZpKi7Nzz zeQJ4FsO+eqNRCW9xbz7+iAtoTpEd3zUAkYGSd4sHVOuLQqyLGtqXhk1H/1BZQtUrLUK 6fH1r49XAr6dxZhMb2huG9cjEAbj0nrDG8iNeZ7qdOEqL1wmAgkpgWfwR5SdbLmVcwXT qIWp92LWNlau8y+gpK/Z+76w6H3lHI6SoFFnUMZI9HWeCqXCb9eFflA4Chz572A418+j itlunxjfluxSQd2RpGw4ywKvIue31KVfVpeGcYcG0XL6CRl3tI+E+Wx74+qZfnBJaQb9 hroA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=XmcTBFq44vi836zyAohHF6X1GupydpiV7SbX5aQ9n68=; b=Zk/esinUOR/FlzjiUFFYzabx8xRF4aSYq4XLxYcjVoL7qttZU+a2CJJ3yXG8/06DvE i3fe3Tl0+hn8oOkgyBj9P6/vvJe6Y6LF5ularsksEFAWUwY0mOFztWFVjP5ayrEg33mg hoC8rMqm5lwTPpyaHjnWM1aADEGm87JvRx9E+b/5nlAHZs2sj0CU+J2Jub6H+rA841z7 /Ih1SmifbdwF5HBCoOzrsHn77aLI++oNSdzxM7jtt8RI7u3fTfh87Scg6ngJizJO6Tpg iRsRR4vi2kaKjmM7Wjzh9BNoYpybQJI4dYh8+S3r2JgvR1jnohztow6exObgi5iUPr6d uP9A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=n3Q1zxJ0; 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=ibm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l11si2841148ejx.27.2020.12.06.01.40.48; Sun, 06 Dec 2020 01:41:11 -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=@ibm.com header.s=pp1 header.b=n3Q1zxJ0; 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=ibm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726209AbgLFJiC (ORCPT + 99 others); Sun, 6 Dec 2020 04:38:02 -0500 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:54776 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725794AbgLFJiB (ORCPT ); Sun, 6 Dec 2020 04:38:01 -0500 Received: from pps.filterd (m0098414.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.42/8.16.0.42) with SMTP id 0B69WEiS159503; Sun, 6 Dec 2020 04:37:11 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=date : from : to : cc : subject : message-id : references : mime-version : content-type : content-transfer-encoding : in-reply-to; s=pp1; bh=XmcTBFq44vi836zyAohHF6X1GupydpiV7SbX5aQ9n68=; b=n3Q1zxJ0QVMlJFa+ffz/mjTyDCNcVipIbrNM3klFKxE7UJxiG0YGFc0WL/m2GFkHjsij ykr8WU8suLKLQjrJ3O2y77eBRTHiL8ehddJtSneRzBl++nafJpgUxq5HPOcEtk/ynst9 SkEzC7luLaBhbl7zlQZBCYU6l8gW2ihqTp7Ee7VOVJT8ZGhy0ZxKEE2fC95dYU3ATAA2 cvqhUWakUHb4E72CIQ4lVxMK+vW6CDH+VTXxxFDXc01GpXZoYJAVv7kJzRgAeoM0Bdnh JE2905nJOL2ipe75QQXbG2uiYA0xMvr0UOKikcc6SLMc0EXVzPHSsSRbBR5kmF8VdnSq Cg== Received: from pps.reinject (localhost [127.0.0.1]) by mx0b-001b2d01.pphosted.com with ESMTP id 358rmxm2h2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sun, 06 Dec 2020 04:37:11 -0500 Received: from m0098414.ppops.net (m0098414.ppops.net [127.0.0.1]) by pps.reinject (8.16.0.36/8.16.0.36) with SMTP id 0B69WH43159873; Sun, 6 Dec 2020 04:37:10 -0500 Received: from ppma04ams.nl.ibm.com (63.31.33a9.ip4.static.sl-reverse.com [169.51.49.99]) by mx0b-001b2d01.pphosted.com with ESMTP id 358rmxm2fh-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sun, 06 Dec 2020 04:37:10 -0500 Received: from pps.filterd (ppma04ams.nl.ibm.com [127.0.0.1]) by ppma04ams.nl.ibm.com (8.16.0.42/8.16.0.42) with SMTP id 0B69Wol5021109; Sun, 6 Dec 2020 09:37:08 GMT Received: from b06avi18626390.portsmouth.uk.ibm.com (b06avi18626390.portsmouth.uk.ibm.com [9.149.26.192]) by ppma04ams.nl.ibm.com with ESMTP id 3583svgxdj-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sun, 06 Dec 2020 09:37:08 +0000 Received: from d06av24.portsmouth.uk.ibm.com (d06av24.portsmouth.uk.ibm.com [9.149.105.60]) by b06avi18626390.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 0B69b69x58327394 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sun, 6 Dec 2020 09:37:06 GMT Received: from d06av24.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 9B0EB4203F; Sun, 6 Dec 2020 09:37:06 +0000 (GMT) Received: from d06av24.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 849EB4204B; Sun, 6 Dec 2020 09:37:05 +0000 (GMT) Received: from linux.ibm.com (unknown [9.145.50.18]) by d06av24.portsmouth.uk.ibm.com (Postfix) with ESMTPS; Sun, 6 Dec 2020 09:37:05 +0000 (GMT) Date: Sun, 6 Dec 2020 11:37:03 +0200 From: Mike Rapoport To: Nadav Amit Cc: Mike Rapoport , Andrew Morton , linux-mm , lkml , Andrea Arcangeli , Mike Kravetz , Pavel Emelyanov , Andrei Vagin Subject: Re: [PATCH] userfaultfd: prevent non-cooperative events vs mcopy_atomic races Message-ID: <20201206093703.GY123287@linux.ibm.com> References: <1527061324-19949-1-git-send-email-rppt@linux.vnet.ibm.com> <31DA12CC-E9CC-497D-A2EE-B83549D95CE8@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <31DA12CC-E9CC-497D-A2EE-B83549D95CE8@gmail.com> X-TM-AS-GCONF: 00 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.312,18.0.737 definitions=2020-12-06_04:2020-12-04,2020-12-06 signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxscore=0 suspectscore=0 impostorscore=0 bulkscore=0 lowpriorityscore=0 phishscore=0 spamscore=0 mlxlogscore=999 malwarescore=0 adultscore=0 priorityscore=1501 clxscore=1011 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2012060058 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hello Nadav, On Thu, Dec 03, 2020 at 11:57:46AM -0800, Nadav Amit wrote: > Hello Mike, > > Regarding your (old) patch: > > > On May 23, 2018, at 12:42 AM, Mike Rapoport wrote: > > > > If a process monitored with userfaultfd changes it's memory mappings or > > forks() at the same time as uffd monitor fills the process memory with > > UFFDIO_COPY, the actual creation of page table entries and copying of the > > data in mcopy_atomic may happen either before of after the memory mapping > > modifications and there is no way for the uffd monitor to maintain > > consistent view of the process memory layout. > > > > For instance, let's consider fork() running in parallel with > > userfaultfd_copy(): > > > > process | uffd monitor > > ---------------------------------+------------------------------ > > fork() | userfaultfd_copy() > > ... | ... > > dup_mmap() | down_read(mmap_sem) > > down_write(mmap_sem) | /* create PTEs, copy data */ > > dup_uffd() | up_read(mmap_sem) > > copy_page_range() | > > up_write(mmap_sem) | > > dup_uffd_complete() | > > /* notify monitor */ | > > > > If the userfaultfd_copy() takes the mmap_sem first, the new page(s) will be > > present by the time copy_page_range() is called and they will appear in the > > child's memory mappings. However, if the fork() is the first to take the > > mmap_sem, the new pages won't be mapped in the child's address space. > > > > Since userfaultfd monitor has no way to determine what was the order, let's > > disallow userfaultfd_copy in parallel with the non-cooperative events. In > > such case we return -EAGAIN and the uffd monitor can understand that > > userfaultfd_copy() clashed with a non-cooperative event and take an > > appropriate action. > > I am struggling to understand this patch and would appreciate your > assistance. The tl;dr version is that without this commit we had failing fork tests in CRIU [1] :) > Specifically, I have two questions: > > 1. How can memory corruption occur? If the page is already mapped and the > handler “mistakenly" calls userfaultfd_copy(), wouldn't mcopy_atomic_pte() > return -EEXIST once it sees the PTE already exists? In such case, I would > presume that the handler should be able to recover gracefully by waking the > faulting thread. The issue we had was when fork() in a monitored process happened concurrently with "background copy" of pages into the process address space during a post-copy process migration. The userspace has no way to tell who won the race for mmap_lock and depending on that we can have two different cases: * fork() took the mmap_lock, pages in the parent are still empty, so they will be empty in the child * userfaultfd_copy() won the lock, there is data in the parent and the child's inherits these mappings The uffd monotor should *know* what is the state of child's memory and without this patch it could only guess. > 2. How is memory ordering supposed to work here? IIUC, mmap_changing is not > protected by any lock and there are no memory barriers that are associated > with the assignment. Indeed, the code calls WRITE_ONCE()/READ_ONCE(), but > AFAIK this does not guarantee ordering with non-volatile reads/writes. There is also mmap_lock involved, so I don't see how copy can start in parallel with fork processing. Fork sets mmap_chaning to true while holding mmap_lock, so copy cannot start in parallel. When mmap_lock is realeased, mmap_chaning remains true until fork event is pushed to userspace and when this is done there is no issue with userfaultfd_copy. Maybe I am missing something... [1] https://github.com/checkpoint-restore/criu/blob/criu-dev/test/zdtm/transition/fork.c > Thanks, > Nadav -- Sincerely yours, Mike.