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 3A7E5C433EF for ; Sat, 26 Feb 2022 07:37:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AD48D8D0002; Sat, 26 Feb 2022 02:37:42 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A84198D0001; Sat, 26 Feb 2022 02:37:42 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 94BC58D0002; Sat, 26 Feb 2022 02:37:42 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.28]) by kanga.kvack.org (Postfix) with ESMTP id 88C9A8D0001 for ; Sat, 26 Feb 2022 02:37:42 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 49C6D20558 for ; Sat, 26 Feb 2022 07:37:42 +0000 (UTC) X-FDA: 79184126364.14.86E4DB7 Received: from mx0b-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) by imf12.hostedemail.com (Postfix) with ESMTP id AC1E940005 for ; Sat, 26 Feb 2022 07:37:41 +0000 (UTC) Received: from pps.filterd (m0098417.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 21Q5cpL7026106; Sat, 26 Feb 2022 07:37:40 GMT 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 : in-reply-to; s=pp1; bh=5a5V4DlGyuRKVEZe/sMhS3bMfANO1QpLfbhNzU788lo=; b=G+uoLmvHCMINNV0dtRZkNfYxMw1rgEV+xEil4NFGaKpGOXSS9VuqqkQlCHHbB3SDdWWq hrQAwcFKkRzt0OxCDw+d8cZ7STGSJ8PjihvfmorZ3NDQfBZQlsjAwanRl/0sOi2WCr7a mvNm1xesz5I6XzwZgW0+VyC/8l8RfL3YB1SUciur7Tl8/kynhoe8YxONpWfno/UNYTeY HbhmOnrHJEu24b1c0+9aIRAK5Ch6IweFUqFVzjUfe5FbaBiI20GX/YSiplARSaGlKutn JFHfLKMoJBYjH4MbPqZjn9LrNvrHUQCScauEvNatvKeFdDwS020XYLB7muTVXOAEptYw PA== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com with ESMTP id 3efcjeu9r4-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 26 Feb 2022 07:37:40 +0000 Received: from m0098417.ppops.net (m0098417.ppops.net [127.0.0.1]) by pps.reinject (8.16.0.43/8.16.0.43) with SMTP id 21Q7Xdl1010808; Sat, 26 Feb 2022 07:37:40 GMT Received: from ppma06fra.de.ibm.com (48.49.7a9f.ip4.static.sl-reverse.com [159.122.73.72]) by mx0a-001b2d01.pphosted.com with ESMTP id 3efcjeu9q6-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 26 Feb 2022 07:37:39 +0000 Received: from pps.filterd (ppma06fra.de.ibm.com [127.0.0.1]) by ppma06fra.de.ibm.com (8.16.1.2/8.16.1.2) with SMTP id 21Q7XJR8018569; Sat, 26 Feb 2022 07:37:38 GMT Received: from b06cxnps4074.portsmouth.uk.ibm.com (d06relay11.portsmouth.uk.ibm.com [9.149.109.196]) by ppma06fra.de.ibm.com with ESMTP id 3efbfhrs8j-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Sat, 26 Feb 2022 07:37:38 +0000 Received: from b06wcsmtp001.portsmouth.uk.ibm.com (b06wcsmtp001.portsmouth.uk.ibm.com [9.149.105.160]) by b06cxnps4074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 21Q7bZax48038374 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Sat, 26 Feb 2022 07:37:35 GMT Received: from b06wcsmtp001.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id A6D4DA4060; Sat, 26 Feb 2022 07:37:35 +0000 (GMT) Received: from b06wcsmtp001.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id B547BA4054; Sat, 26 Feb 2022 07:37:34 +0000 (GMT) Received: from linux.ibm.com (unknown [9.145.147.106]) by b06wcsmtp001.portsmouth.uk.ibm.com (Postfix) with ESMTPS; Sat, 26 Feb 2022 07:37:34 +0000 (GMT) Date: Sat, 26 Feb 2022 09:37:32 +0200 From: Mike Rapoport To: Nadav Amit Cc: linux-mm@kvack.org, Nadav Amit , Peter Xu , David Hildenbrand , Andrea Arcangeli , Mike Rapoport , Jan Kara Subject: Re: [PATCH v3] userfaultfd: provide unmasked address on page-fault Message-ID: References: <20220226022655.350562-1-namit@vmware.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220226022655.350562-1-namit@vmware.com> X-TM-AS-GCONF: 00 X-Proofpoint-GUID: LHcMCoSNb-SVEK8XLThi-IHXnSlwrqyC X-Proofpoint-ORIG-GUID: 4xFVGp78ZLvJQibUKKXndB293c62zoVA X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.816,Hydra:6.0.425,FMLib:17.11.64.514 definitions=2022-02-25_11,2022-02-25_01,2022-02-23_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 bulkscore=0 mlxlogscore=999 suspectscore=0 spamscore=0 clxscore=1015 adultscore=0 mlxscore=0 phishscore=0 malwarescore=0 lowpriorityscore=0 impostorscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2201110000 definitions=main-2202260052 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: AC1E940005 X-Stat-Signature: k6augg4y7qhmwtofi7wqupdbow8ek4se Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=ibm.com header.s=pp1 header.b=G+uoLmvH; dmarc=pass (policy=none) header.from=ibm.com; spf=pass (imf12.hostedemail.com: domain of rppt@linux.ibm.com designates 148.163.158.5 as permitted sender) smtp.mailfrom=rppt@linux.ibm.com X-HE-Tag: 1645861061-838269 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: On Sat, Feb 26, 2022 at 02:26:55AM +0000, Nadav Amit wrote: > From: Nadav Amit > > Userfaultfd is supposed to provide the full address (i.e., unmasked) of > the faulting access back to userspace. However, that is not the case for > quite some time. > > Even running "userfaultfd_demo" from the userfaultfd man page provides > the wrong output (and contradicts the man page). Notice that > "UFFD_EVENT_PAGEFAULT event" shows the masked address (7fc5e30b3000) > and not the first read address (0x7fc5e30b300f). > > Address returned by mmap() = 0x7fc5e30b3000 > > fault_handler_thread(): > poll() returns: nready = 1; POLLIN = 1; POLLERR = 0 > UFFD_EVENT_PAGEFAULT event: flags = 0; address = 7fc5e30b3000 > (uffdio_copy.copy returned 4096) > Read address 0x7fc5e30b300f in main(): A > Read address 0x7fc5e30b340f in main(): A > Read address 0x7fc5e30b380f in main(): A > Read address 0x7fc5e30b3c0f in main(): A > > The exact address is useful for various reasons and specifically for > prefetching decisions. If it is known that the memory is populated by > certain objects whose size is not page-aligned, then based on the > faulting address, the uffd-monitor can decide whether to prefetch and > prefault the adjacent page. > > This bug has been for quite some time in the kernel: since commit > 1a29d85eb0f1 ("mm: use vmf->address instead of of vmf->virtual_address") > vmf->virtual_address"), which dates back to 2016. A concern has been > raised that existing userspace application might rely on the old/wrong > behavior in which the address is masked. Therefore, it was suggested to > provide the masked address unless the user explicitly asks for the exact > address. > > Add a new userfaultfd feature UFFD_FEATURE_EXACT_ADDRESS to direct > userfaultfd to provide the exact address. Add a new "real_address" field > to vmf to hold the unmasked address. Provide the address to userspace > accordingly. > > Initialize real_address in various code-paths to be consistent with > address, even when it is not used, to be on the safe side. > > Acked-by: Peter Xu > Reviewed-by: David Hildenbrand > Cc: Andrea Arcangeli > Cc: Mike Rapoport > Cc: Jan Kara > Signed-off-by: Nadav Amit Acked-by: Mike Rapoport > > --- > > v2->v3: > * Initialize real_address on all code paths [Jan] > > v1->v2: > * Add uffd feature to selectively enable [David, Andrea] > --- > fs/userfaultfd.c | 5 ++++- > include/linux/mm.h | 3 ++- > include/uapi/linux/userfaultfd.h | 8 +++++++- > mm/hugetlb.c | 6 ++++-- > mm/memory.c | 1 + > mm/swapfile.c | 1 + > 6 files changed, 19 insertions(+), 5 deletions(-) > > diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c > index e26b10132d47..826927026fe7 100644 > --- a/fs/userfaultfd.c > +++ b/fs/userfaultfd.c > @@ -198,6 +198,9 @@ static inline struct uffd_msg userfault_msg(unsigned long address, > struct uffd_msg msg; > msg_init(&msg); > msg.event = UFFD_EVENT_PAGEFAULT; > + > + if (!(features & UFFD_FEATURE_EXACT_ADDRESS)) > + address &= PAGE_MASK; > msg.arg.pagefault.address = address; > /* > * These flags indicate why the userfault occurred: > @@ -482,7 +485,7 @@ vm_fault_t handle_userfault(struct vm_fault *vmf, unsigned long reason) > > init_waitqueue_func_entry(&uwq.wq, userfaultfd_wake_function); > uwq.wq.private = current; > - uwq.msg = userfault_msg(vmf->address, vmf->flags, reason, > + uwq.msg = userfault_msg(vmf->real_address, vmf->flags, reason, > ctx->features); > uwq.ctx = ctx; > uwq.waken = false; > diff --git a/include/linux/mm.h b/include/linux/mm.h > index 213cc569b192..27df0ca0a36a 100644 > --- a/include/linux/mm.h > +++ b/include/linux/mm.h > @@ -478,7 +478,8 @@ struct vm_fault { > struct vm_area_struct *vma; /* Target VMA */ > gfp_t gfp_mask; /* gfp mask to be used for allocations */ > pgoff_t pgoff; /* Logical page offset based on vma */ > - unsigned long address; /* Faulting virtual address */ > + unsigned long address; /* Faulting virtual address - masked */ > + unsigned long real_address; /* Faulting virtual address - unmaked */ > }; > enum fault_flag flags; /* FAULT_FLAG_xxx flags > * XXX: should really be 'const' */ > diff --git a/include/uapi/linux/userfaultfd.h b/include/uapi/linux/userfaultfd.h > index 05b31d60acf6..ef739054cb1c 100644 > --- a/include/uapi/linux/userfaultfd.h > +++ b/include/uapi/linux/userfaultfd.h > @@ -32,7 +32,8 @@ > UFFD_FEATURE_SIGBUS | \ > UFFD_FEATURE_THREAD_ID | \ > UFFD_FEATURE_MINOR_HUGETLBFS | \ > - UFFD_FEATURE_MINOR_SHMEM) > + UFFD_FEATURE_MINOR_SHMEM | \ > + UFFD_FEATURE_EXACT_ADDRESS) > #define UFFD_API_IOCTLS \ > ((__u64)1 << _UFFDIO_REGISTER | \ > (__u64)1 << _UFFDIO_UNREGISTER | \ > @@ -189,6 +190,10 @@ struct uffdio_api { > * > * UFFD_FEATURE_MINOR_SHMEM indicates the same support as > * UFFD_FEATURE_MINOR_HUGETLBFS, but for shmem-backed pages instead. > + * > + * UFFD_FEATURE_EXACT_ADDRESS indicates that the exact address of page > + * faults would be provided and the offset within the page would not be > + * masked. > */ > #define UFFD_FEATURE_PAGEFAULT_FLAG_WP (1<<0) > #define UFFD_FEATURE_EVENT_FORK (1<<1) > @@ -201,6 +206,7 @@ struct uffdio_api { > #define UFFD_FEATURE_THREAD_ID (1<<8) > #define UFFD_FEATURE_MINOR_HUGETLBFS (1<<9) > #define UFFD_FEATURE_MINOR_SHMEM (1<<10) > +#define UFFD_FEATURE_EXACT_ADDRESS (1<<11) > __u64 features; > > __u64 ioctls; > diff --git a/mm/hugetlb.c b/mm/hugetlb.c > index 61895cc01d09..16017f90568b 100644 > --- a/mm/hugetlb.c > +++ b/mm/hugetlb.c > @@ -5342,6 +5342,7 @@ static inline vm_fault_t hugetlb_handle_userfault(struct vm_area_struct *vma, > pgoff_t idx, > unsigned int flags, > unsigned long haddr, > + unsigned long addr, > unsigned long reason) > { > vm_fault_t ret; > @@ -5349,6 +5350,7 @@ static inline vm_fault_t hugetlb_handle_userfault(struct vm_area_struct *vma, > struct vm_fault vmf = { > .vma = vma, > .address = haddr, > + .real_address = addr, > .flags = flags, > > /* > @@ -5417,7 +5419,7 @@ static vm_fault_t hugetlb_no_page(struct mm_struct *mm, > /* Check for page in userfault range */ > if (userfaultfd_missing(vma)) { > ret = hugetlb_handle_userfault(vma, mapping, idx, > - flags, haddr, > + flags, haddr, address, > VM_UFFD_MISSING); > goto out; > } > @@ -5481,7 +5483,7 @@ static vm_fault_t hugetlb_no_page(struct mm_struct *mm, > unlock_page(page); > put_page(page); > ret = hugetlb_handle_userfault(vma, mapping, idx, > - flags, haddr, > + flags, haddr, address, > VM_UFFD_MINOR); > goto out; > } > diff --git a/mm/memory.c b/mm/memory.c > index c125c4969913..aae53fde13d9 100644 > --- a/mm/memory.c > +++ b/mm/memory.c > @@ -4622,6 +4622,7 @@ static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma, > struct vm_fault vmf = { > .vma = vma, > .address = address & PAGE_MASK, > + .real_address = address, > .flags = flags, > .pgoff = linear_page_index(vma, address), > .gfp_mask = __get_fault_gfp_mask(vma), > diff --git a/mm/swapfile.c b/mm/swapfile.c > index bf0df7aa7158..33c7abb16610 100644 > --- a/mm/swapfile.c > +++ b/mm/swapfile.c > @@ -1951,6 +1951,7 @@ static int unuse_pte_range(struct vm_area_struct *vma, pmd_t *pmd, > struct vm_fault vmf = { > .vma = vma, > .address = addr, > + .real_address = addr, > .pmd = pmd, > }; > > -- > 2.25.1 > -- Sincerely yours, Mike.