From patchwork Thu Oct 7 23:50:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nadav Amit X-Patchwork-Id: 12544489 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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 36764C433EF for ; Fri, 8 Oct 2021 07:22:08 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id C478F608FE for ; Fri, 8 Oct 2021 07:22:07 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org C478F608FE Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 63D076B0071; Fri, 8 Oct 2021 03:22:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5ECE0900002; Fri, 8 Oct 2021 03:22:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4DC206B0073; Fri, 8 Oct 2021 03:22:07 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0070.hostedemail.com [216.40.44.70]) by kanga.kvack.org (Postfix) with ESMTP id 3D5C56B0071 for ; Fri, 8 Oct 2021 03:22:07 -0400 (EDT) Received: from smtpin03.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id E9BF4181C98E1 for ; Fri, 8 Oct 2021 07:22:06 +0000 (UTC) X-FDA: 78672426252.03.8694064 Received: from mail-pj1-f44.google.com (mail-pj1-f44.google.com [209.85.216.44]) by imf21.hostedemail.com (Postfix) with ESMTP id B93E9D03B71C for ; Fri, 8 Oct 2021 07:22:06 +0000 (UTC) Received: by mail-pj1-f44.google.com with SMTP id nn3-20020a17090b38c300b001a03bb6c4ebso4666371pjb.1 for ; Fri, 08 Oct 2021 00:22:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=QgZDXufZT6HKl/lBj8C0d09aIeUA5f6RLZG86dMpu7k=; b=M63ZBGUdgFZB/d9TMgxemztWqBUSTyQpUmePUT/8Ns0SQM4XHJkluWs7JRzLIfZRn1 iyMJ/dtNf/Kfq9DmK62cvwSMbbKAKyjEOADLu5AYLuRQfZoZRRFdUUsxWrTjbpvSgWfq c595/TZ3wVyRQ2od4m7qnXMBxkVjm0HQ3gRVZ+eA8O29V6FDDBoDfiBXCgPpyNw0070s vTGgIiLIsBb9cFtSVQXTgE5fkqpwW4N3W+xYvYfdTyFVzHtUlelFQ9BtojGbCtu98MlE RDK8iiVPMJCJl+Aa5wl24BXz+VaRuxlKYNf6qn5pLFvmfnFAtdaJsgEQTfZTkZs/E1nS 1xlg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=QgZDXufZT6HKl/lBj8C0d09aIeUA5f6RLZG86dMpu7k=; b=V/zKrSksFvD1a2y8yKKMoRx/RWcddDTRqd1eQl1zqwnaUIjFvDRtRfqhnjSyKhWzad mCtQKcEEi8wWuZczeIJShnnoZJEAbkh8p7hGSI53P3SMoYPefEljNmvBGoDhgByFLm9B BIDV4FukLehS21WBAIolMuXHfxTNZ12IEshqrOm6FM59VEEZrJA9D+qjUu5Hs2vhAj+r LSJNeLFLhV2j/rFqRy4Ldg9cXk1OPB8DIDuso/WeRBfChm9zNEE/xBfrLJf0U2jabQJ5 B1R+mtmhNEbFux8ZqZOI3xO5oSqaEg2SWYSiqiVPpi4R6xav57AucPMYaBhGsJ2NWBMN TwpQ== X-Gm-Message-State: AOAM531rJF7cqqAPjKmDFF3hbENolOr948A414xD9c0FgPOm9t3F20zB AOpDWaHw93DOv7OsNszSypY= X-Google-Smtp-Source: ABdhPJzGcX/FpZZkyYgmMcL7AezXWg7k6v+xPhh0/XrE/fVqF0dI+yWdSplZlcFoI8NvQgqrTzuHCA== X-Received: by 2002:a17:903:1112:b0:13d:ce49:e275 with SMTP id n18-20020a170903111200b0013dce49e275mr8280504plh.5.1633677725365; Fri, 08 Oct 2021 00:22:05 -0700 (PDT) Received: from sc2-haas01-esx0118.eng.vmware.com ([66.170.99.1]) by smtp.gmail.com with ESMTPSA id f84sm1565319pfa.25.2021.10.08.00.22.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Oct 2021 00:22:04 -0700 (PDT) From: Nadav Amit X-Google-Original-From: Nadav Amit To: Andrew Morton Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, Nadav Amit , Andrea Arcangeli , Mike Rapoport , Peter Xu , Jan Kara , stable@vger.kernel.org Subject: [PATCH] mm/userfaultfd: provide unmasked address on page-fault Date: Thu, 7 Oct 2021 16:50:55 -0700 Message-Id: <20211007235055.469587-1-namit@vmware.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-Rspamd-Queue-Id: B93E9D03B71C X-Stat-Signature: ksywbcjqqko4zpruqoj8m9f69pien5so Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=M63ZBGUd; spf=none (imf21.hostedemail.com: domain of mail-pj1-f44.google.com has no SPF policy when checking 209.85.216.44) smtp.helo=mail-pj1-f44.google.com; dmarc=pass (policy=none) header.from=gmail.com X-Rspamd-Server: rspam06 X-HE-Tag: 1633677726-335302 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: 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. 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 Add a new "real_address" field to vmf to hold the unmasked address. It is possible to keep the unmasked address in the existing address field (and mask whenever necessary) instead, but this is likely to cause backporting problems of this patch. Cc: Andrea Arcangeli Cc: Mike Rapoport Cc: Peter Xu Cc: Jan Kara Cc: stable@vger.kernel.org Fixes: 1a29d85eb0f19 ("mm: use vmf->address instead of of vmf->virtual_address") Signed-off-by: Nadav Amit --- fs/userfaultfd.c | 2 +- include/linux/mm.h | 3 ++- mm/memory.c | 1 + 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index 003f0d31743e..1dfc0fcd83c1 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -481,7 +481,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 00bb2d938df4..f3f324e3f2bf 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -523,7 +523,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/mm/memory.c b/mm/memory.c index 12a7b2094434..3d2d7fdbb7dc 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -4594,6 +4594,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),