From patchwork Mon Jul 11 16:59:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nadav Amit X-Patchwork-Id: 12914407 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 3F02BC43334 for ; Tue, 12 Jul 2022 00:33:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BCD0594002D; Mon, 11 Jul 2022 20:33:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B7ACE940010; Mon, 11 Jul 2022 20:33:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A1E3F94002D; Mon, 11 Jul 2022 20:33:46 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 8F4D6940010 for ; Mon, 11 Jul 2022 20:33:46 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay11.hostedemail.com (Postfix) with ESMTP id 3E6D580440 for ; Tue, 12 Jul 2022 00:33:46 +0000 (UTC) X-FDA: 79676574852.24.19ED945 Received: from relay3.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by imf27.hostedemail.com (Postfix) with ESMTP id DBB184006C for ; Tue, 12 Jul 2022 00:33:45 +0000 (UTC) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay13.hostedemail.com (Postfix) with ESMTP id 8A4926013A for ; Tue, 12 Jul 2022 00:33:45 +0000 (UTC) X-FDA: 79676574810.27.69767DA Received: from mail-pf1-f172.google.com (mail-pf1-f172.google.com [209.85.210.172]) by imf29.hostedemail.com (Postfix) with ESMTP id 334B712006E for ; Tue, 12 Jul 2022 00:33:45 +0000 (UTC) Received: by mail-pf1-f172.google.com with SMTP id b9so6120019pfp.10 for ; Mon, 11 Jul 2022 17:33:44 -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=Ti0C5xab/5kDqDKYjHrnv6fD7vOYV0VSIifA6sunqqM=; b=XORqPXdxn8L1WpRMLVZeoGz+I3la1Lzf/Ogve9kYHbmy+kM/Pr8ry1/D7p5jN/BrQN oYGgElFHZjnGR6neB7LhDwVbxCbNSacPtkNWbJzacIdhVxanV4cCpqZW8NGRtDpuCc5M fIDDKVJdOtsmskgmTMoxnJsQDuOz86qIcUaANSciwpk+Bs1f3yCZkWuFGbYlIeDNiXtW kCupp0WUX2MC0I2KDchdSS3S5BEOhxfO3YbzVr6PuRoqNypwsQ2av44cK4EnpoTkVmHI S95UTC/j8QFjUw8vt9DE11hf5vafBcZx6JpSEXVYg8adgtIkocg1UNsL3rUY1xK9eb9H CzIQ== 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=Ti0C5xab/5kDqDKYjHrnv6fD7vOYV0VSIifA6sunqqM=; b=t3qtoCo5v1VZB/Pa0VAr6CHDx6HjL6rs2tdwGm8sqGEAM9jygCy5tv77xUaZpeZDdL 5EZ08x3ldiVrJcqpDygIdkvycrVA0GjGt4MjW9g1V0gKZurcePHNeRvPr1RKQC0uHjaE y7VivKQl3hSojhTaVRPgh1y2sbG/fCBQj37GViOiE74Jk9rCf7tfQcVcuq42/bVZb6V6 WlGbJh/9IqCEyWHRE41ejcsaHciTZHjMUZaGg61KkI/iufefGGsuw3tHR4yI3vHTXQwT fTi6R15NKAvAZ2B0zXEN2/fa9JlWGF+F5pfcqok00h3yn5Q+J8+k9rgbEdtnb2S/qthh lT1w== X-Gm-Message-State: AJIora+NW79/ZcwLY/B8KvYldM9Qxbf85feBFzw4/y0NpZLh8Lq6lqoQ QynpjIaUTGVQtYJISk7oZTY= X-Google-Smtp-Source: AGRyM1u8mq01hXRym4iEcmpnDSmqpJXy216tb9zXA+hUsAi2nNnAklahyKOPDSahKlwEDJGgKhy+/w== X-Received: by 2002:a05:6a00:889:b0:510:91e6:6463 with SMTP id q9-20020a056a00088900b0051091e66463mr20757829pfj.58.1657586023850; Mon, 11 Jul 2022 17:33:43 -0700 (PDT) Received: from sc2-haas01-esx0118.eng.vmware.com ([66.170.99.1]) by smtp.gmail.com with ESMTPSA id p2-20020a170902e74200b0016be596c8afsm5383138plf.282.2022.07.11.17.33.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Jul 2022 17:33:43 -0700 (PDT) From: Nadav Amit X-Google-Original-From: Nadav Amit To: Andrew Morton Cc: linux-mm@kvack.org, Nadav Amit , James Houghton , Peter Xu , David Hildenbrand , Mike Rapoport , Jan Kara , Andrea Arcangeli , stable@vger.kernel.org Subject: [PATCH] userfaultfd: provide properly masked address for huge-pages Date: Mon, 11 Jul 2022 09:59:06 -0700 Message-Id: <20220711165906.2682-1-namit@vmware.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=XORqPXdx; dmarc=pass (policy=none) header.from=gmail.com; spf=none (imf27.hostedemail.com: domain of MAILER-DAEMON@hostedemail.com has no SPF policy when checking 216.40.44.13) smtp.mailfrom=MAILER-DAEMON@hostedemail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1657586025; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=Ti0C5xab/5kDqDKYjHrnv6fD7vOYV0VSIifA6sunqqM=; b=4JdpsyC5HWn2PDls+TPK/5Kwf5r56YbeCYu8ZpwzFUkfByEcTuBI8Hs0WyKIlhgZdF2MlI Xj9E9DOAPli8tv2UQ2LOZmg56+mVXS5EAXDmMSwupgUJQRcctBIivYvGaZicP47ENj3UK2 9C9ezn8J2OtNBQ1N9owC5NWG3suEykk= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1657586025; a=rsa-sha256; cv=none; b=sv/5g5pEicq0NQjPVcXwp2z0QM7nEISCNJ+SREJVuw6dM4pWhcf6zGy1r51lXSuCAlVaX2 +I2pF+ktxJuRa5BXsHpOv5xM/Ep4pQHeD6x+IxBwe+n3UrItf33xnG1zqwIxtCbjpdy1GT pV9h75ZVqCeyzZ1VPZAtBbRwyVRMbk8= X-HE-Tag-Orig: 1657586025-153458 Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=XORqPXdx; dmarc=pass (policy=none) header.from=gmail.com; spf=none (imf27.hostedemail.com: domain of MAILER-DAEMON@hostedemail.com has no SPF policy when checking 216.40.44.13) smtp.mailfrom=MAILER-DAEMON@hostedemail.com X-Rspam-User: X-Rspamd-Server: rspam08 X-Stat-Signature: zmkxp8g9bonxjbcoxg33464szhdrpsq9 X-Rspamd-Queue-Id: DBB184006C X-HE-Tag: 1657586025-634843 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 Commit 824ddc601adc ("userfaultfd: provide unmasked address on page-fault") was introduced to fix an old bug, in which the offset in the address of a page-fault was masked. Concerns were raised - although were never backed by actual code - that some userspace code might break because the bug has been around for quite a while. To address these concerns a new flag was introduced, and only when this flag is set by the user, userfaultfd provides the exact address of the page-fault. The commit however had a bug, and if the flag is unset, the offset was always masked based on a base-page granularity. Yet, for huge-pages, the behavior prior to the commit was that the address is masked to the huge-page granulrity. While there are no reports on real breakage, fix this issue. If the flag is unset, use the address with the masking that was done before. Fixes: 824ddc601adc ("userfaultfd: provide unmasked address on page-fault") Reported-by: James Houghton Cc: Peter Xu Cc: David Hildenbrand Cc: Mike Rapoport Cc: Jan Kara Cc: Andrea Arcangeli Cc: Signed-off-by: Nadav Amit Reviewed-by: Mike Rapoport Reviewed-by: Peter Xu Reviewed-by: James Houghton --- fs/userfaultfd.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index e943370107d0..de86f5b2859f 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -192,17 +192,19 @@ static inline void msg_init(struct uffd_msg *msg) } static inline struct uffd_msg userfault_msg(unsigned long address, + unsigned long real_address, unsigned int flags, unsigned long reason, unsigned int features) { 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; + msg.arg.pagefault.address = (features & UFFD_FEATURE_EXACT_ADDRESS) ? + real_address : address; + /* * These flags indicate why the userfault occurred: * - UFFD_PAGEFAULT_FLAG_WP indicates a write protect fault. @@ -488,8 +490,8 @@ 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->real_address, vmf->flags, reason, - ctx->features); + uwq.msg = userfault_msg(vmf->address, vmf->real_address, vmf->flags, + reason, ctx->features); uwq.ctx = ctx; uwq.waken = false;