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 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8E9E6E810DA for ; Wed, 27 Sep 2023 13:29:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0C55E8D003B; Wed, 27 Sep 2023 09:29:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 04B628D0002; Wed, 27 Sep 2023 09:29:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E07FA8D003B; Wed, 27 Sep 2023 09:29:43 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id CE3FF8D0002 for ; Wed, 27 Sep 2023 09:29:43 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id A71CD1A0576 for ; Wed, 27 Sep 2023 13:29:43 +0000 (UTC) X-FDA: 81282459846.13.9F7F236 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf08.hostedemail.com (Postfix) with ESMTP id 326A9160017 for ; Wed, 27 Sep 2023 13:29:40 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=HuXzarHL; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf08.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1695821381; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=G2hv5CuQPn74NEvKQKxD/ubIeG0HKIdGdn5RcarEG6U=; b=t/diueuTCq80Bbb0wKb7/5gWP4Rb18pc4StGjm6CgF8uvRdj29NU0iH4GWguurFKlLH86m LtHR5bLwOQAKkJAR+Z/QngrOwPvNfoRPhYME/lzsVBXQkYpgqFmjCxrK8j16LUACf9KcdS RGMWk/2aL8aXIdtniVecoHLQEJ4QNZQ= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=HuXzarHL; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf08.hostedemail.com: domain of david@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=david@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695821381; a=rsa-sha256; cv=none; b=qnNOJinnZ7Lw3/dSs33n3l5mmuB8nKotzyyot9USTd2hZvHFuSz5rwSqmfvB4pEmYZKF0t aJmGwDj0YvBOAiobTkDR5I5TmHTOrG88L2pggTg8w8q5wcqWin1TVJte88RG7dHs2/NtIG vKPdH4ejLqR2Scy2z+3+GsCwwaFuhjE= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1695821380; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=G2hv5CuQPn74NEvKQKxD/ubIeG0HKIdGdn5RcarEG6U=; b=HuXzarHLLaxmu+0QeI+x89WfBtKFV5HJIfdWwxTFAciKzgp0yBdMFQwgJ1x0wNVMt3rUpt C8R9HuZ9gx5l2KDovxmd+CHId8020Biep4kWwa7/SiKefI9QSMjNOynxpiXSubNMjkacbs G/jAAxMlcDoGKJANNvF7BrkdAXvRl68= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-663-Iw-LbQxXOnS-2J1HS91tDA-1; Wed, 27 Sep 2023 09:29:39 -0400 X-MC-Unique: Iw-LbQxXOnS-2J1HS91tDA-1 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-40540179bcdso87595495e9.2 for ; Wed, 27 Sep 2023 06:29:38 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695821378; x=1696426178; h=content-transfer-encoding:in-reply-to:subject:organization:from :content-language:references:cc:to:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=G2hv5CuQPn74NEvKQKxD/ubIeG0HKIdGdn5RcarEG6U=; b=chTkP/LODNQktOmdTpizcruqHnsKRhojZ6PTYyChXYc94FGgYnaWE99MyBOxsmvFW6 55txcagRd2wUf2F6rMJaog5uWlFgzAWvU0CTPP2DwEbIhgxzroHu2K+In1SYxUv1wemO 4YlRXsaOoQvInHsiXLT790YpS6uary1P6NKAjT0qt/6tRSCe3edRcVk3LbnLVDAMde9A d7lVVAa4w1tfVtIEXxbmpTIK73eNbxUsjvUDskigTyTlgLyj1jH8Yu+1MqBta/emMS0j kJn3UKrze2vd3AWGFWckBFSPx7elNF5ctz/IAOam7H3M7xcMNVRvW5ZFa7Y9L1jVuA75 2/Eg== X-Gm-Message-State: AOJu0YzjYLwKL5ei0FuIp3j4Namv7iChlb2FJsOCI8alVWEsx2KITKtJ NffLRgXNS7RgTiOGu7o+nTke7X6UPVHmVkgLnH8svYL8rdrLt1b7nBJ3TuKZCpnwhQ3wvvXQ/ov WGoEiKAgW4Xs= X-Received: by 2002:a7b:c8cd:0:b0:405:3a63:6d12 with SMTP id f13-20020a7bc8cd000000b004053a636d12mr1881910wml.17.1695821377608; Wed, 27 Sep 2023 06:29:37 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEmM/Vcbhyu+gFvAqhqp6gDueEI1OOogKEfv68sNWi+zB+gBfd/bHDtz9MAW7eJXY1rkpiOgg== X-Received: by 2002:a7b:c8cd:0:b0:405:3a63:6d12 with SMTP id f13-20020a7bc8cd000000b004053a636d12mr1881872wml.17.1695821377080; Wed, 27 Sep 2023 06:29:37 -0700 (PDT) Received: from ?IPV6:2003:cb:c749:6900:3a06:bd5:2f7b:e6eb? (p200300cbc74969003a060bd52f7be6eb.dip0.t-ipconnect.de. [2003:cb:c749:6900:3a06:bd5:2f7b:e6eb]) by smtp.gmail.com with ESMTPSA id u2-20020a05600c210200b004063d8b43e7sm3920879wml.48.2023.09.27.06.29.35 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 27 Sep 2023 06:29:36 -0700 (PDT) Message-ID: <03f95e90-82bd-6ee2-7c0d-d4dc5d3e15ee@redhat.com> Date: Wed, 27 Sep 2023 15:29:35 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.15.1 To: Jann Horn , Suren Baghdasaryan Cc: akpm@linux-foundation.org, viro@zeniv.linux.org.uk, brauner@kernel.org, shuah@kernel.org, aarcange@redhat.com, lokeshgidra@google.com, peterx@redhat.com, hughd@google.com, mhocko@suse.com, axelrasmussen@google.com, rppt@kernel.org, willy@infradead.org, Liam.Howlett@oracle.com, zhangpeng362@huawei.com, bgeffon@google.com, kaleshsingh@google.com, ngeoffray@google.com, jdduke@google.com, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, kernel-team@android.com References: <20230923013148.1390521-1-surenb@google.com> <20230923013148.1390521-3-surenb@google.com> From: David Hildenbrand Organization: Red Hat Subject: Re: [PATCH v2 2/3] userfaultfd: UFFDIO_REMAP uABI In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Rspamd-Queue-Id: 326A9160017 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 673rwnm3cm53g4sgharus5jj95inbpjd X-HE-Tag: 1695821380-901554 X-HE-Meta: U2FsdGVkX18HN+tQen2uVHT69YXHpUA2Yp058Kxv5Z3+RRxG6d5viHYlWmoGvKAkW3grMzepuP7EcUIcQRzoRA6D8S5DHGQKSJwF/PheDBUwRvE9A/BiP77gbI/UqGBpLUjxyOVX1dFhSZWhk3GJ/PFdn8Mp2XduwPJrsziiM9pePP3F3SYotxWYZIKDo7UYeINKBBtTij0kbAfKdyi3wt4mn2u0Ky+yU9dOQkJO3UvKND2un1KNQyeTCOxdW5atG3i2S5wZCG7uB2tlxpDbufBn2A6jXz7Auqgb+txKApKCknpjKmRXeUYdfzpNl1sGSfPGPB+3rDHbj/KVNpfxGgko8UTlPEs4bnrN9k7o7+py0uXdkMaxdlJbCjSfRz5FO92F0tb2Rxus1IpIazFYKkxJUxJxYLMoL7fffwnSEVNKzYd49oWJEoZSYTJcK3YWaOO/LA+4BkzSIdpbMOF08OcVjOZWKlg+bFXUGSRCzFd0BnMkJLdyHvF6C6pafy6UDUKTo/qybIaq4Hm9CrrtIu5a6lzU3htBhIWOUJ2Gq75OxJu7EW/v4fK4GtAhbIrLwFi4bcM6iHpmQsQQXDqrPVTply+Ei/Z5CcmS3zPkvwgxGB3lialOafhGJNR//Ksw+LsH+Pe4bEgqWE4k+AYp+dBm4kEwEplOGLoQwKSHmujK29DUTlLxuST7VO4fAjylyqqnNEn7xmwE0F9OgGU8FuuZUAHjOa42Vw5iNMjA7Vo8Z3ZEnwLZiZh4Ql1gTuUu5mKznWlfBiMEIO7GrlzkwcuWpoMns0jhuCEYiFg4n7TtMz/WT5LhKoF5FoDoohlolXjbG8/dlQvuw6ABkp2orILo7y86OvGwdcFDiceRum1Duyn+71jiHDsR34EzaMpnl3RNpFfZNrjGMEO/+h6+BsVa6f/iRaGgs5rkDZWMf7MtRzNLsVKA+P1/2CzfMW1uNRQbPeEyyYV06+mrue1 eDhiNb9Q qT5kJZHvcmvh2ESKmyMM5miNiX4XSGslHcUelb9/eWLlL/jccSLKufXx2JSuMKGu9iy8A4KdGJd7RizaGTSVXSdLdNOPSwSk1imCBPFFjk2dHj9R25tAFh/csfd6JaDGZPPobQtLTTBBXXPjNKDCmdcDA/RkUAHsHOQFSv8/Jb3kcYHhC388/9thqkLcfXtwV4JVuT3HwdMvxlOzTrooGrSHqie7VYjplkN90uFCqyh9MUJqCkZBiioche1XEOsvwMQck513iwWPWm0O2DeKHb0NlLL83K5kFahetRtHCAnTZnQedL+X+aqrtfNQEUDknFueARI258FcdwtE31r6B0HWL/SeCzEjKvtdLPw5jD9TYIeNkrs3lV4N2M5gp0TZcmdmqK1bsWJx4uYsWp7CY8HUdYANEznae4dDqDwTV8sCSJ6s= 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: >> +static int remap_anon_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm, >> + struct vm_area_struct *dst_vma, >> + struct vm_area_struct *src_vma, >> + unsigned long dst_addr, unsigned long src_addr, >> + pte_t *dst_pte, pte_t *src_pte, >> + pte_t orig_dst_pte, pte_t orig_src_pte, >> + spinlock_t *dst_ptl, spinlock_t *src_ptl, >> + struct folio *src_folio) >> +{ >> + struct anon_vma *dst_anon_vma; >> + >> + double_pt_lock(dst_ptl, src_ptl); >> + >> + if (!pte_same(*src_pte, orig_src_pte) || >> + !pte_same(*dst_pte, orig_dst_pte) || >> + folio_test_large(src_folio) || >> + folio_estimated_sharers(src_folio) != 1) { ^ here you should check PageAnonExclusive. Please get rid of any implicit explicit/implcit mapcount checks. >> + double_pt_unlock(dst_ptl, src_ptl); >> + return -EAGAIN; >> + } >> + >> + BUG_ON(!folio_test_anon(src_folio)); >> + >> + dst_anon_vma = (void *)dst_vma->anon_vma + PAGE_MAPPING_ANON; >> + WRITE_ONCE(src_folio->mapping, >> + (struct address_space *) dst_anon_vma); I have some cleanups pending for page_move_anon_rmap(), that moves the SetPageAnonExclusive hunk out. Here we should be using page_move_anon_rmap() [or rather, folio_move_anon_rmap() after my cleanups] I'll send them out soonish. >> + WRITE_ONCE(src_folio->index, linear_page_index(dst_vma, >> + dst_addr)); >> + >> + orig_src_pte = ptep_clear_flush(src_vma, src_addr, src_pte); >> + orig_dst_pte = mk_pte(&src_folio->page, dst_vma->vm_page_prot); >> + orig_dst_pte = maybe_mkwrite(pte_mkdirty(orig_dst_pte), >> + dst_vma); > > I think there's still a theoretical issue here that you could fix by > checking for the AnonExclusive flag, similar to the huge page case. > > Consider the following scenario: > > 1. process P1 does a write fault in a private anonymous VMA, creating > and mapping a new anonymous page A1 > 2. process P1 forks and creates two children P2 and P3. afterwards, A1 > is mapped in P1, P2 and P3 as a COW page, with mapcount 3. > 3. process P1 removes its mapping of A1, dropping its mapcount to 2. > 4. process P2 uses vmsplice() to grab a reference to A1 with get_user_pages() > 5. process P2 removes its mapping of A1, dropping its mapcount to 1. > > If at this point P3 does a write fault on its mapping of A1, it will > still trigger copy-on-write thanks to the AnonExclusive mechanism; and > this is necessary to avoid P3 mapping A1 as writable and writing data > into it that will become visible to P2, if P2 and P3 are in different > security contexts. > > But if P3 instead moves its mapping of A1 to another address with > remap_anon_pte() which only does a page mapcount check, the > maybe_mkwrite() will directly make the mapping writable, circumventing > the AnonExclusive mechanism. > Yes, can_change_pte_writable() contains the exact logic when we can turn something easily writable even if it wasn't writable before. which includes that PageAnonExclusive is set. (but with uffd-wp or softdirty tracking, there is more to consider) -- Cheers, David / dhildenb