Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp4155865rdb; Thu, 14 Sep 2023 13:35:32 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGqxKI93qg913x6vLPeAkGynZWcrfkCUQvU25uWVl/gP1xKdTuJaWkC4+/J9swrvFp2y36j X-Received: by 2002:a17:902:d70a:b0:1bf:2e5c:7367 with SMTP id w10-20020a170902d70a00b001bf2e5c7367mr6350610ply.42.1694723731630; Thu, 14 Sep 2023 13:35:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694723731; cv=none; d=google.com; s=arc-20160816; b=mEEYMS8b344Z1O0Y0XCKjxHfVmLa2bmkCfIGu4Kk5NKpCofUXJGapRzYtDGkRlVHWB LiaBicRzzflvVAh7G3khiqoqOT1YQT4XAwn40J4hOtIgIa9jlp1aMGVWim3Q0kRj86AB zLUsOmOGb66ONVi/xntrFlOIfBf6YitQoeSh+ZgtZP8tVug9HJwWh9Vne35SvJVLsOr1 MOcS5K7tD+UnFy6A7IhtfCwarofngisoYHvlsBVEORqfCO/Di6PUHiZKZ0oq33GrxjDo 3kMePBfjVdmbe0qAqgBGsyKI4kznJ+CzrEcsjkNMZqMW0uMa+9udV6kPfvDdVLPTwVFn QyFw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to :organization:references:cc:to:from:content-language:subject :user-agent:mime-version:date:message-id:dkim-signature; bh=Yvl0oZS06u+Mc5pUy8196yl56j2FK2OsnzUd+32T3ds=; fh=zHn3qBolt4OTLLEhs1IrY/Uwg+SBCcSD4RaAymJ9P6Y=; b=MGcN9wMwI5ul1C4a5GsGs8dbbFap6verVYJxeW3C0QhMw/x5RDWaoCYk/v7YVlN7PH hxXWYwU1W9GcNVyqK3SruwftfYaTIY2VD5Z9iVF0VbPv+8++M7AhfGErkjqPT6mX5Bdn XMYprPxbLBBYvAKe51yBNnDUyoO+42roNkju77F6j/JqYmrDHoAD8MWlEztL9Yd8myA6 2ODIkXcofPFM+and3Ml++4yDjQOUCBCAKEtNjrReuhjPCQyXREinScMLqZrHzl+1m2T2 jiazYLa1YCa16UdNXEuTwgmIVkuh3uFslatq4REWbCgBf5fhteVeckNRseavWY9qvwyt BW4Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=h3GSZFjV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 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 lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id b12-20020a170902650c00b001c320085a7esi2068077plk.645.2023.09.14.13.35.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Sep 2023 13:35:31 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=h3GSZFjV; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 89CC6828E0A5; Thu, 14 Sep 2023 11:52:23 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240803AbjINSwU (ORCPT + 99 others); Thu, 14 Sep 2023 14:52:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48722 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239845AbjINSvq (ORCPT ); Thu, 14 Sep 2023 14:51:46 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id D58604ECB for ; Thu, 14 Sep 2023 11:45:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1694717149; 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=Yvl0oZS06u+Mc5pUy8196yl56j2FK2OsnzUd+32T3ds=; b=h3GSZFjV/bL4Q4zfYfuI02e0Zs4HeXTkmOsD/kPjqzcu4f98VhA5ZcUkMJuEbxcRkFhbew orJ6/RFTULF62dGSPz/2s2Q7Z5r1AA4MWOda3oAhegY6BhwSxdxDIaHRZ/doQ+w909cevZ lrCDLcrHMI+XrTW3jNpTmJzQSuxpFOY= 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-10-9BPaw4yKOuGJpDk54c7j6w-1; Thu, 14 Sep 2023 14:45:47 -0400 X-MC-Unique: 9BPaw4yKOuGJpDk54c7j6w-1 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-402cba95486so10301895e9.0 for ; Thu, 14 Sep 2023 11:45:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694717146; x=1695321946; h=content-transfer-encoding:in-reply-to:organization:references:cc:to :from:content-language:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Yvl0oZS06u+Mc5pUy8196yl56j2FK2OsnzUd+32T3ds=; b=K9P6hWTTum8ByMcbhPyI+X/yCNQhZom1ACqndf3D19YvnQ/Sf/GeTRFRtS4JHkwfnu jjl3FJQQm0tpUGcnfc5nRj2YwtMMBB89qwaxVxAz5/Ycp3IbKnvG82DZdywUM+jBLFuF /GSIZw4pCF7KewcgRSPZdjeynsdq7BvoxOPR9lFoxK+JpN99jD1GGgbQEeKzgCCKlHH+ FV9QBHNOhKhvo1BNd1hT/uXb3KZE2zVigHVaYX+YN7IbbK5oVamXVd82AueW1h/03370 rF+juQYXXJOTOJKg7Pb+wosVBnqX/kNmvOkI/np3MC6TKjzUkzIACBsnREVwIXM9DVhR HHWQ== X-Gm-Message-State: AOJu0Yz9AF2TbXNuyuxQyLAa3TH7hWULPTY2MWxAocTpaAyYeSS5ZWUG refxRp47/byGqis9deinV1/rsx7xpPviRyt/SFshFJ2Op6MO62ZC51IIEey/AuT6fj5iXpCkRmz r70uLIIi4ckugyHzcjKU5qSYn X-Received: by 2002:a5d:6603:0:b0:317:5e5e:60e0 with SMTP id n3-20020a5d6603000000b003175e5e60e0mr5665414wru.28.1694717146326; Thu, 14 Sep 2023 11:45:46 -0700 (PDT) X-Received: by 2002:a5d:6603:0:b0:317:5e5e:60e0 with SMTP id n3-20020a5d6603000000b003175e5e60e0mr5665391wru.28.1694717145824; Thu, 14 Sep 2023 11:45:45 -0700 (PDT) Received: from ?IPV6:2003:cb:c71c:600:4630:4a91:d07:7095? (p200300cbc71c060046304a910d077095.dip0.t-ipconnect.de. [2003:cb:c71c:600:4630:4a91:d07:7095]) by smtp.gmail.com with ESMTPSA id j15-20020a5d604f000000b0031779a6b451sm2408478wrt.83.2023.09.14.11.45.44 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 14 Sep 2023 11:45:45 -0700 (PDT) Message-ID: Date: Thu, 14 Sep 2023 20:45:44 +0200 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.13.0 Subject: Re: [PATCH 2/3] userfaultfd: UFFDIO_REMAP uABI Content-Language: en-US From: David Hildenbrand To: Matthew Wilcox , 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, Liam.Howlett@oracle.com, jannh@google.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: <20230914152620.2743033-1-surenb@google.com> <20230914152620.2743033-3-surenb@google.com> Organization: Red Hat In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Thu, 14 Sep 2023 11:52:23 -0700 (PDT) X-Spam-Status: No, score=-2.3 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email On 14.09.23 20:43, David Hildenbrand wrote: > On 14.09.23 20:11, Matthew Wilcox wrote: >> On Thu, Sep 14, 2023 at 08:26:12AM -0700, Suren Baghdasaryan wrote: >>> +++ b/include/linux/userfaultfd_k.h >>> @@ -93,6 +93,23 @@ extern int mwriteprotect_range(struct mm_struct *dst_mm, >>> extern long uffd_wp_range(struct vm_area_struct *vma, >>> unsigned long start, unsigned long len, bool enable_wp); >>> >>> +/* remap_pages */ >>> +extern void double_pt_lock(spinlock_t *ptl1, spinlock_t *ptl2); >>> +extern void double_pt_unlock(spinlock_t *ptl1, spinlock_t *ptl2); >>> +extern ssize_t remap_pages(struct mm_struct *dst_mm, >>> + struct mm_struct *src_mm, >>> + unsigned long dst_start, >>> + unsigned long src_start, >>> + unsigned long len, __u64 flags); >>> +extern int remap_pages_huge_pmd(struct mm_struct *dst_mm, >>> + struct mm_struct *src_mm, >>> + pmd_t *dst_pmd, pmd_t *src_pmd, >>> + pmd_t dst_pmdval, >>> + struct vm_area_struct *dst_vma, >>> + struct vm_area_struct *src_vma, >>> + unsigned long dst_addr, >>> + unsigned long src_addr); >> >> Drop the 'extern' markers from function declarations. >> >>> +int remap_pages_huge_pmd(struct mm_struct *dst_mm, >>> + struct mm_struct *src_mm, >>> + pmd_t *dst_pmd, pmd_t *src_pmd, >>> + pmd_t dst_pmdval, >>> + struct vm_area_struct *dst_vma, >>> + struct vm_area_struct *src_vma, >>> + unsigned long dst_addr, >>> + unsigned long src_addr) >>> +{ >>> + pmd_t _dst_pmd, src_pmdval; >>> + struct page *src_page; >>> + struct anon_vma *src_anon_vma, *dst_anon_vma; >>> + spinlock_t *src_ptl, *dst_ptl; >>> + pgtable_t pgtable; >>> + struct mmu_notifier_range range; >>> + >>> + src_pmdval = *src_pmd; >>> + src_ptl = pmd_lockptr(src_mm, src_pmd); >>> + >>> + BUG_ON(!pmd_trans_huge(src_pmdval)); >>> + BUG_ON(!pmd_none(dst_pmdval)); >>> + BUG_ON(!spin_is_locked(src_ptl)); >>> + mmap_assert_locked(src_mm); >>> + mmap_assert_locked(dst_mm); >>> + BUG_ON(src_addr & ~HPAGE_PMD_MASK); >>> + BUG_ON(dst_addr & ~HPAGE_PMD_MASK); >>> + >>> + src_page = pmd_page(src_pmdval); >>> + BUG_ON(!PageHead(src_page)); >>> + BUG_ON(!PageAnon(src_page)); >> >> Better to add a src_folio = page_folio(src_page); >> and then folio_test_anon() here. >> >>> + if (unlikely(page_mapcount(src_page) != 1)) { >> >> Brr, this is going to miss PTE mappings of this folio. I think you >> actually want folio_mapcount() instead, although it'd be more efficient >> to look at folio->_entire_mapcount == 1 and _nr_pages_mapped == 0. >> Not wure what a good name for that predicate would be. > > We have > > * It only works on non shared anonymous pages because those can > * be relocated without generating non linear anon_vmas in the rmap > * code. > * > * It provides a zero copy mechanism to handle userspace page faults. > * The source vma pages should have mapcount == 1, which can be > * enforced by using madvise(MADV_DONTFORK) on src vma. > > Use PageAnonExclusive(). As long as KSM is not involved and you don't > use fork(), that flag should be good enough for that use case here. > ... and similarly don't do any of that swapcount stuff and only check if the swap pte is anon exclusive. -- Cheers, David / dhildenb