Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp4241106rdb; Thu, 14 Sep 2023 16:56:29 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHkpOI5xTu0b9mEsavhKRBmyI+q3Vgg1HmOfgOvGY2w+Cl3EsAoGUkZGu8qHsVJ1QM4Pl6m X-Received: by 2002:a17:902:d2cd:b0:1c0:a5c9:e05a with SMTP id n13-20020a170902d2cd00b001c0a5c9e05amr119377plc.43.1694735789415; Thu, 14 Sep 2023 16:56:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694735789; cv=none; d=google.com; s=arc-20160816; b=j/vVeAldVIsgwOyF40ESmANMPcNvhYy7OMpQ6VTs9JFbv+Lz+TlrJc9Z2uOBvHFx2v XEePew1H8GR1kFjazWL9RRP27opnWmfeMZ3jio0pJjWQAqui4TurMMfUKyBa5eG2KGax 2Rt9V2Z6W8LxmtIy9H2jg2mjMkVwnSzgN2a5xRlb6ay03zhn+Uy/4eGcDaa32wRdk9db 8L4oZspMzdNHDfsPEJkNjEwZ2MjDf2zzh4SlT6cAMXvJ/fNPi87Kzzf5He/Eg3G1jJak 7c5l7pHptpo9AGqnAYZNYIs18SG/wOs6H3pV03+gyeaFlif05SbBZMu1KjFqxhEvXbPr U2dA== 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:from:content-language:references:cc:to:subject :user-agent:mime-version:date:message-id:dkim-signature; bh=0akcrrxPTvMntGm3gQRHZF2Md0/HBRzawwdcPZ7RPas=; fh=zHn3qBolt4OTLLEhs1IrY/Uwg+SBCcSD4RaAymJ9P6Y=; b=AN0MGIsy9mt/VNzF6Dg8kyGPspUwduwI81G9byeDd7jwDIVRdKIRPS5vGqfHviz12U gH0pF1wtJTaGhXIkiqXZHE96U3qikJ+9q+Qpdolp/PRivXvDq4xcw8BlhejW/l5mvDYF cQGgZNG9r1Z9+nuaG2rXts2Rve1FRSTuqtZp7vZ/lHEyu4WkYwmeenco/sbC9Px8OCm+ ImpywzbVy1td6x6vpYo/nCSNdBiV/yizUjB8aqt7JrawEVqSMYTi9lpeX3BVEeQ2xuc5 xv6MS3WhJGIdcwSKLRr/Z/Cx/o1T8Ea+IAnI2sUIsPGMLsZfBbLhWFaDwZ0Evv4SiwU8 b1Qg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=NnETkKMx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 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 pete.vger.email (pete.vger.email. [23.128.96.36]) by mx.google.com with ESMTPS id f9-20020a170902684900b001bbdd44bbd0si2298168pln.176.2023.09.14.16.56.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Sep 2023 16:56:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 as permitted sender) client-ip=23.128.96.36; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=NnETkKMx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.36 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 pete.vger.email (Postfix) with ESMTP id D59C98028571; Thu, 14 Sep 2023 11:51:11 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at pete.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236376AbjINSvI (ORCPT + 99 others); Thu, 14 Sep 2023 14:51:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58380 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241924AbjINSuu (ORCPT ); Thu, 14 Sep 2023 14:50:50 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 045EBE5826 for ; Thu, 14 Sep 2023 11:43:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1694717022; 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=0akcrrxPTvMntGm3gQRHZF2Md0/HBRzawwdcPZ7RPas=; b=NnETkKMxgRzXYJHyd0e1mbnsKiTeYFpJLRACAag7SlUOVv90GGYffMiAp5gXAuV48uxQl+ c9d8xFj8zqPypLE0rSvqre3K/TyxluJqM8l2NcKw6nChBGpR0Jm1qzT80Yf8bzgC2evr+o PIsKVfCq0pAyvTNMWUJdBwRROX1pdxs= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-669-a_BhvRMPOuer18GtYLU0bw-1; Thu, 14 Sep 2023 14:43:40 -0400 X-MC-Unique: a_BhvRMPOuer18GtYLU0bw-1 Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-31c6c275c83so816412f8f.2 for ; Thu, 14 Sep 2023 11:43:40 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694717019; x=1695321819; h=content-transfer-encoding:in-reply-to:organization:from :content-language:references:cc:to:subject:user-agent:mime-version :date:message-id:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=0akcrrxPTvMntGm3gQRHZF2Md0/HBRzawwdcPZ7RPas=; b=ckH3t1+NsBqWYb7EitUfqhzGtQ2I4+D9738WjejP0u4hhsA3O7iraqOJvuxHBdgQ0e /73Bf84/lhJFdYydgXDd3c8VkdlEFwoQd0Ek0xW/uj6h0xDfjCPLBqIY3tM+r6d4o1gI 1+GlxM8tXjC3z2VktrbBodTlF6EwhCeFf9bWwCqV3UzPDZjeTqZUVqWIJQJcEjg+Cz/D /0L++W0CRz0hRxAtwZBLpbnBJkqc9/Nai9cfRDrYSeqEVePi8XIQhllSrypzqDefEQXF 60rTXuRrB/DE4Zp9tUKHbCx5amvAxhoNhrnckOj7Lefb7buoPfhnH1cFudWZf+8eF1Oo vFDA== X-Gm-Message-State: AOJu0YzCAEtv9ee4oWFP5cyIGBEdXOLmCww4ZhWLPqa2/V64DmJnZI8M 5bAWWG/ujsCb84EuoWnwNBqH/0EEHn+hhnPDJ6Fy0lVFmFhsg8PPFN3vWKD8fNkVqeny+3FQB8z XL3iBxI10cxTYOQJvIssskNMZ X-Received: by 2002:adf:f782:0:b0:31a:d551:c2c9 with SMTP id q2-20020adff782000000b0031ad551c2c9mr5368592wrp.6.1694717019430; Thu, 14 Sep 2023 11:43:39 -0700 (PDT) X-Received: by 2002:adf:f782:0:b0:31a:d551:c2c9 with SMTP id q2-20020adff782000000b0031ad551c2c9mr5368560wrp.6.1694717018868; Thu, 14 Sep 2023 11:43:38 -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 q5-20020a5d6585000000b0031accc7228asm2463536wru.34.2023.09.14.11.43.37 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 14 Sep 2023 11:43:38 -0700 (PDT) Message-ID: Date: Thu, 14 Sep 2023 20:43:36 +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 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> Content-Language: en-US From: David Hildenbrand 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 (pete.vger.email [0.0.0.0]); Thu, 14 Sep 2023 11:51:12 -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 pete.vger.email 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. [...] >> + /* >> + * Pin the page while holding the lock to be sure the >> + * page isn't freed under us >> + */ >> + spin_lock(src_ptl); >> + if (!pte_same(orig_src_pte, *src_pte)) { >> + spin_unlock(src_ptl); >> + err = -EAGAIN; >> + goto out; >> + } >> + >> + folio = vm_normal_folio(src_vma, src_addr, orig_src_pte); >> + if (!folio || !folio_test_anon(folio) || >> + folio_estimated_sharers(folio) != 1) { > > I wonder if we also want to fail if folio_test_large()? While we don't > have large anon folios today, it seems to me that trying to migrate one > of them like this is just not going to work. Yes, refuse any PTE-mapped large folios. -- Cheers, David / dhildenb