From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A489A7E792 for ; Thu, 8 May 2025 15:37:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746718631; cv=none; b=ijuOhIG6dAPWgt/qa7rrFX6YiRCzVc3E+jkBrIqgyCSNGdcB16twGFisQFyV/75xY8Y2rXyo1ZYMBHNNOV3CPYM3d5nN80e8EAUgYbjMlav1uTmSTFqDrFtJNoBzgjc1uvoRpj5gFUjpy3GC0fmdzf3h2cEGAn721igQOdetLfQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746718631; c=relaxed/simple; bh=fHcEnxFvE8betaIrmNhqEpzMpXukhInU56a88lzWZAA=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=aTnbvNAp0pXDNDo/nNzng3JD3p+y5us4+Zi8gD1zsAksspohU0hZGlb4qIjgbkrh5d8OqQYzWV9Ob/aFVolCVSi7uHmylVbJvD+gdFkmRmlMb4JcFaBhokY8Y1OnFGQ9HODXCcun2jmB0ipyZ3cgcvWRZTdFxPsrtWolx82qKJE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=FZ3sbPvI; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="FZ3sbPvI" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1746718628; 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:autocrypt:autocrypt; bh=WqIleyRWbBE6fAeA2+kL12j5GghozmhjBuwTBWLQVFQ=; b=FZ3sbPvIlcGvtFKB7gxpd95E1d/X2B3AgA4Kw+320o5vnO9h0hpnhmrRnsF2xsIAL2x8J/ 1gpUnq78eedxdK43PkzI5yjgBN08hdxpy6HAAafQAbOqEqg2N3IwAbXFdOUh4jYgzADEci veZwcwZnOHavTfy2ivrRBzRfw1rzef8= 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-261-hnFblSKeNpG4r1p0l5SkTQ-1; Thu, 08 May 2025 11:37:07 -0400 X-MC-Unique: hnFblSKeNpG4r1p0l5SkTQ-1 X-Mimecast-MFC-AGG-ID: hnFblSKeNpG4r1p0l5SkTQ_1746718626 Received: by mail-wm1-f69.google.com with SMTP id 5b1f17b1804b1-442ccf0eb4eso8687565e9.1 for ; Thu, 08 May 2025 08:37:07 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746718626; x=1747323426; h=content-transfer-encoding:in-reply-to:organization:autocrypt :content-language:from: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=WqIleyRWbBE6fAeA2+kL12j5GghozmhjBuwTBWLQVFQ=; b=pnm06BfXpv8ZxhedgA+77n9p4ECWCoHwu1bFVLmq54VCsYT0qF/UwK25kq30Hdcbf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:300f:fa4a with SMTP id 5b1f17b1804b1-441d44c4f9cmr77168695e9.12.1746718626064; Thu, 08 May 2025 08:37:06 -0700 (PDT) X-Google-Smtp-Source: AGHT+IG8+Z0hrydt/sNvRj0RjbhS2hsfzsEiHQI1YRNtaN1N6XVBPHA0SaOTi3RMMG6pXTHWtjJ1RA== X-Received: by 2002:a05:600c:1e1c:b0:43d:300f:fa4a with SMTP id 5b1f17b1804b1-441d44c4f9cmr77168355e9.12.1746718625656; Thu, 08 May 2025 08:37:05 -0700 (PDT) Received: from ?IPV6:2003:d8:2f3e:5900:27aa:5f4a:b65c:3d3c? (p200300d82f3e590027aa5f4ab65c3d3c.dip0.t-ipconnect.de. [2003:d8:2f3e:5900:27aa:5f4a:b65c:3d3c]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-442cd32b0c8sm41504345e9.3.2025.05.08.08.37.04 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 08 May 2025 08:37:05 -0700 (PDT) Message-ID: Date: Thu, 8 May 2025 17:37:04 +0200 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 1/1] Fix zero copy I/O on __get_user_pages allocated pages To: Pantelis Antoniou Cc: Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Artem Krupotkin , Charles Briere , Wade Farnsworth , Peter Xu References: <20250507154105.763088-1-p.antoniou@partner.samsung.com> <20250507154105.763088-2-p.antoniou@partner.samsung.com> <99ed92b7-c1b2-4e12-a7ee-776a7f890b47@redhat.com> <20250508182356.45dbfd40@sarc.samsung.com> From: David Hildenbrand Content-Language: en-US Autocrypt: addr=david@redhat.com; keydata= xsFNBFXLn5EBEAC+zYvAFJxCBY9Tr1xZgcESmxVNI/0ffzE/ZQOiHJl6mGkmA1R7/uUpiCjJ dBrn+lhhOYjjNefFQou6478faXE6o2AhmebqT4Ki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6bezi+rugUI+w6DABEBAAHCwXwEGAEIACYCGwwWIQQb2cqtc1xMOkYN/MpN3hD3 AP+DWgUCXw7HsgUJEqkpoQAKCRBN3hD3AP+DWrrpD/4qS3dyVRxDcDHIlmguXjC1Q5tZTwNB boaBTPHSy/Nksu0eY7x6HfQJ3xajVH32Ms6t1trDQmPx2iP5+7iDsb7OKAb5eOS8h+BEBDeq 3ecsQDv0fFJOA9ag5O3LLNk+3x3q7e0uo06XMaY7UHS341ozXUUI7wC7iKfoUTv03iO9El5f XpNMx/YrIMduZ2+nd9Di7o5+KIwlb2mAB9sTNHdMrXesX8eBL6T9b+MZJk+mZuPxKNVfEQMQ a5SxUEADIPQTPNvBewdeI80yeOCrN+Zzwy/Mrx9EPeu59Y5vSJOx/z6OUImD/GhX7Xvkt3kq Er5KTrJz3++B6SH9pum9PuoE/k+nntJkNMmQpR4MCBaV/J9gIOPGodDKnjdng+mXliF3Ptu6 3oxc2RCyGzTlxyMwuc2U5Q7KtUNTdDe8T0uE+9b8BLMVQDDfJjqY0VVqSUwImzTDLX9S4g/8 kC4HRcclk8hpyhY2jKGluZO0awwTIMgVEzmTyBphDg/Gx7dZU1Xf8HFuE+UZ5UDHDTnwgv7E th6RC9+WrhDNspZ9fJjKWRbveQgUFCpe1sa77LAw+XFrKmBHXp9ZVIe90RMe2tRL06BGiRZr jPrnvUsUUsjRoRNJjKKA/REq+sAnhkNPPZ/NNMjaZ5b8Tovi8C0tmxiCHaQYqj7G2rgnT0kt WNyWQQ== Organization: Red Hat In-Reply-To: <20250508182356.45dbfd40@sarc.samsung.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 08.05.25 17:23, Pantelis Antoniou wrote: > On Thu, 8 May 2025 17:03:46 +0200 > David Hildenbrand wrote: > > Hi there, > >> On 07. 05. 25 17: 41, Pantelis Antoniou wrote: Hi, > Recent updates >> to net filesystems enabled zero copy operations, > which require >> getting a user space page pinned. > > This does not work for pages >> that were allocated via __get_user_pages >> On 07.05.25 17:41, Pantelis Antoniou wrote: >> >> Hi, >> >>> Recent updates to net filesystems enabled zero copy operations, >>> which require getting a user space page pinned. >>> >>> This does not work for pages that were allocated via >>> __get_user_pages and then mapped to user-space via remap_pfn_rage. >> >> Right. Because the struct page of a VM_PFNMAP *must not be touched*. >> It has to be treated like it doesn't exist. >> > > Well, that's not exactly the case. For pages mapped to user space via > remap_pfn_range() the VM_PFNMAP bit is set even though the pages do > have a struct page. Yes. And VM_PFNMAP is the big flag that these pages shall not be touched. Even if it exists. Even if you say please. :) See the comment above vm_normal_page(): "Special" mappings do not wish to be associated with a "struct page" (either it doesn't exist, or it exists but they don't want to touch it) VM_MIXEDMAP could maybe be used for that purpose: possibly GUP also has to be updated to make use of that. (I was hoping we can get rid of VM_MIXEDMAP at some point) > > The details of how it happens are at the cover page of this patch but > let me paste the relevant bits here. > > "In our emulation environment we have noticed failing writes when > performing I/O from a userspace mapped DRM GEM buffer object. > The platform does not use VRAM, all graphics memory is regular DRAM > memory, allocated via __get_free_pages > > The same write was successful from a heap allocated bounce buffer. > > The sequence of events is as follows. > > 1. A BO (Buffer Object) is created, and it's backing memory is allocated via > __get_user_pages() __get_user_pages() only allocates memory via page faults. Are you sure you meant __get_user_pages() here? > > 2. Userspace mmaps a BO (Buffer Object) via a mmap call on the opened > file handle of a DRM driver. The mapping is done via the > drm_gem_mmap_obj() call. > > 3. Userspace issues a write to a file copying the contents of the BO. > > 3a. If the file is located on regular filesystem (like ext4), the write > completes successfully. > > 3b. If the file is located on a network filesystem, like 9p the write fails. > > The write fails because v9fs_file_write_iter() will call > netfs_unbuffered_write_iter(), netfs_unbuffered_write_iter_locked() which will > call netfs_extract_user_iter() > > netfs_extract_user_iter() will in turn call iov_iter_extract_pages() which for > a user backed iterator will call iov_iter_extract_user_pages which will call > pin_user_pages_fast() which finally will call __gup_longterm_locked(). > > __gup_longterm_locked() will call __get_user_pages_locked() which will fail > because the VMA is marked with the VM_IO and VM_PFNMAP flags." So, drm_gem_mmap_obj() ends up using remap_pfn_rage()? I can spot that drm_gem_mmap_obj() has a path where it explicitly sets vm_flags_set(vma, VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP); Which is a clear sign to core-MM (incl. GUP) to never mess with the mapped pages. > >>> >>> remap_pfn_range_internal() will turn on VM_IO | VM_PFNMAP vma bits. >>> VM_PFNMAP in particular mark the pages as not having struct_page >>> associated with them, which is not the case for __get_user_pages() >>> >>> This in turn makes any attempt to lock a page fail, and breaking >>> I/O from that address range. >>> >>> This patch address it by special casing pages in those VMAs and not >>> calling vm_normal_page() for them. >>> >>> Signed-off-by: Pantelis Antoniou >>> --- >>> mm/gup.c | 22 ++++++++++++++++++---- >>> 1 file changed, 18 insertions(+), 4 deletions(-) >>> >>> diff --git a/mm/gup.c b/mm/gup.c >>> index 84461d384ae2..e185c18c0c81 100644 >>> --- a/mm/gup.c >>> +++ b/mm/gup.c >>> @@ -833,6 +833,20 @@ static inline bool can_follow_write_pte(pte_t >>> pte, struct page *page, return !userfaultfd_pte_wp(vma, pte); >>> } >>> >>> +static struct page *gup_normal_page(struct vm_area_struct *vma, >>> + unsigned long address, pte_t pte) >>> +{ >>> + unsigned long pfn; >>> + >>> + if (vma->vm_flags & (VM_MIXEDMAP | VM_PFNMAP)) { >>> + pfn = pte_pfn(pte); >>> + if (!pfn_valid(pfn) || is_zero_pfn(pfn) || pfn > >>> highest_memmap_pfn) >>> + return NULL; >>> + return pfn_to_page(pfn); >>> + } >>> + return vm_normal_page(vma, address, pte); >> >> I enjoy seeing vm_normal_page() checks in GUP code. >> >> I don't enjoy seeing what you added before that :) >> >> If vm_normal_page() tells you "this is not a normal", then we should >> not touch it. There is one exception: the shared zeropage. >> >> >> So, unfortunately, this is wrong. >> > > Well, lets talk about a proper fix then for the previously mentioned > user-space regression. You really have to find out the responsible commit. GUP has been behaving like that forever I'm afraid. And even the VM_PFNMAP was in drm_gem_mmap_obj() already at least in 2012 if I am not wrong. -- Cheers, David / dhildenb