From patchwork Fri Oct 21 16:36:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Houghton X-Patchwork-Id: 13015101 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 2DEF5FA373F for ; Fri, 21 Oct 2022 16:37:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BBB458E0023; Fri, 21 Oct 2022 12:37:50 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A7F378E0001; Fri, 21 Oct 2022 12:37:50 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 833C08E0023; Fri, 21 Oct 2022 12:37:50 -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 6BD2C8E0001 for ; Fri, 21 Oct 2022 12:37:50 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 49B7AA1317 for ; Fri, 21 Oct 2022 16:37:50 +0000 (UTC) X-FDA: 80045513100.22.864A082 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf25.hostedemail.com (Postfix) with ESMTP id D82C0A0027 for ; Fri, 21 Oct 2022 16:37:49 +0000 (UTC) Received: by mail-yb1-f201.google.com with SMTP id d8-20020a25bc48000000b00680651cf051so3722669ybk.23 for ; Fri, 21 Oct 2022 09:37:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=u3+VTs02NGQ5oyT2/qztgs968E2VJInsJwZCRXskVX8=; b=B67mZeoRzaRq4630o3McC0EzYYV2BfBTiABJMfKCzqnLycO3omT8Gy4/5jQCJFz8AY Qu+pjgsVsBEQ7iOage5JALUzkvGcVzVAI+BCZzndDO2njS28MHuucgPnHYGjHi0MMXoR Jp5rOJtddtvnQu8CbcPae/HSkg70RTOIctaRcEgcIMgYrmnjlaiPn4cNjpoWw5tTnOGk A3Uul0URZQlgQ2/D4cBlS3e4c0tE/yI2F+txMyFPuIxYVsSXN6ldOmoWU22e+oy+uchS z7av7s9Z3JIen2P4NfWiU5pOIplHTUb/SK5SQWwj0gI2b/bXVCp6bRB3GWynd6s9gcat VN1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=u3+VTs02NGQ5oyT2/qztgs968E2VJInsJwZCRXskVX8=; b=qAjCRFxhUIbxcKIKk/pr/SUiJEpNNc6B6tmZJqrPc4hZzTF/tJBz1mu0hQlPYXMIXW 2qx9NJqYicQ/SzHbjNtOMo1aj1mPJ71AiQX6OLKzj9PofjB21YOziJcCw31/7xr/QL9G T1N6UBYykmVR0BTPbo+wUpI7RScAcPVw2+aBsHAsV1f2YxNsIu16ahlPCwfornXE0q9O Ub0iRGTdU5eMU7OEBfhUJCsvf5U92QTAoGmSrPVBoVhwN7QuRR576FbOYHafZV17cv0v VbacUN8Nu8TJ9hiPs0fVuaIBxWvr0y22Gyq19eCC3evfYcS04cdxS+XLOy2O+avkixnD r2rA== X-Gm-Message-State: ACrzQf21xCtwdOf1EyN6fcww4I5M/+SMWiyLz5qbm28alNo+Hed87krD /hqCWuDhyYax1DOJzf+pM8wcIG3WcjwETIko X-Google-Smtp-Source: AMsMyM5EIJbefFWR9wH2Ga6LCed1ohj9fcbD4MeuIorT1fpq+Z8mg3azIGenpfDISn40nNMhrx3GeTcUaANowwsy X-Received: from jthoughton.c.googlers.com ([fda3:e722:ac3:cc00:14:4d90:c0a8:2a4f]) (user=jthoughton job=sendgmr) by 2002:a25:2187:0:b0:6b0:1abc:2027 with SMTP id h129-20020a252187000000b006b01abc2027mr17110269ybh.348.1666370269187; Fri, 21 Oct 2022 09:37:49 -0700 (PDT) Date: Fri, 21 Oct 2022 16:36:51 +0000 In-Reply-To: <20221021163703.3218176-1-jthoughton@google.com> Mime-Version: 1.0 References: <20221021163703.3218176-1-jthoughton@google.com> X-Mailer: git-send-email 2.38.0.135.g90850a2211-goog Message-ID: <20221021163703.3218176-36-jthoughton@google.com> Subject: [RFC PATCH v2 35/47] userfaultfd: require UFFD_FEATURE_EXACT_ADDRESS when using HugeTLB HGM From: James Houghton To: Mike Kravetz , Muchun Song , Peter Xu Cc: David Hildenbrand , David Rientjes , Axel Rasmussen , Mina Almasry , "Zach O'Keefe" , Manish Mishra , Naoya Horiguchi , "Dr . David Alan Gilbert" , "Matthew Wilcox (Oracle)" , Vlastimil Babka , Baolin Wang , Miaohe Lin , Yang Shi , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, James Houghton ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1666370269; a=rsa-sha256; cv=none; b=vGDB7wYhiEZTRQ4rPjc3T1it7S3zxJl39/ttAgBcV5Nlhe0jN9iOkywlkxbaYIqAOZaln2 k0fu871jc/1j1e+q8uK2Gxqfd8Q+Q399ianTa+mq3q+D6dB0Qhv2u6mAvryDtVBshCjm6k 3cIhqC11IDNsWnWJGEgAHSJsATzZUeE= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=B67mZeoR; spf=pass (imf25.hostedemail.com: domain of 33cpSYwoKCN4JTHOUGHTONGOOGLE.COMLINUX-MMKVACK.ORG@flex--jthoughton.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=33cpSYwoKCN4JTHOUGHTONGOOGLE.COMLINUX-MMKVACK.ORG@flex--jthoughton.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1666370269; 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-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=u3+VTs02NGQ5oyT2/qztgs968E2VJInsJwZCRXskVX8=; b=o76/q97ahNMudtvqE9wZb6kFLzkPfIpW2oZzFr97BII2rxzVFUmQ42vZRNtVR12AocQowx +3WpxuqjzzHiSHqw5ClLDjezHPmaoHOqDygLbbhCSc+rUIlNlzkXIpYme8xQx3uZjCbo9Y ftnv/yBwn3HycvmVXaj1Ma5fbuLqKlk= X-Stat-Signature: dhqd4pgtabdpke9i3tw5qdih83kc9gnu X-Rspamd-Queue-Id: D82C0A0027 X-Rspam-User: X-Rspamd-Server: rspam03 Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=B67mZeoR; spf=pass (imf25.hostedemail.com: domain of 33cpSYwoKCN4JTHOUGHTONGOOGLE.COMLINUX-MMKVACK.ORG@flex--jthoughton.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=33cpSYwoKCN4JTHOUGHTONGOOGLE.COMLINUX-MMKVACK.ORG@flex--jthoughton.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-HE-Tag: 1666370269-920687 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: To avoid bugs in userspace, we require that userspace provide UFFD_FEATURE_EXACT_ADDRESS when using UFFD_FEATURE_MINOR_HUGETLBFS_HGM, otherwise UFFDIO_API will fail with EINVAL. The potential confusion is this: without EXACT_ADDRESS, the address given in the userfaultfd message will be rounded down to the hugepage size. Userspace may think that, because they're using HGM, just UFFDIO_CONTINUE the interval [address, address+PAGE_SIZE), but for faults that didn't occur in the first base page of the hugepage, this won't resolve the fault. The only choice it has in this scenario is to UFFDIO_CONTINUE the interval [address, address+hugepage_size), which negates the purpose of using HGM in the first place. By requiring userspace to provide UFFD_FEATURE_EXACT_ADDRESS, there is no rounding, and userspace now has the information it needs to appropriately resolve the fault. Another potential solution here is to change the behavior when UFFD_FEATURE_EXACT_ADDRESS is not provided: when HGM is enabled, start rounding to PAGE_SIZE instead of to the hugepage size. I think requiring UFFD_FEATURE_EXACT_ADDRESS is cleaner. Signed-off-by: James Houghton --- fs/userfaultfd.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index 0204108e3882..c8f21f53e37d 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -1990,6 +1990,17 @@ static int userfaultfd_api(struct userfaultfd_ctx *ctx, ~(UFFD_FEATURE_MINOR_HUGETLBFS | UFFD_FEATURE_MINOR_SHMEM); #ifndef CONFIG_HUGETLB_HIGH_GRANULARITY_MAPPING uffdio_api.features &= ~UFFD_FEATURE_MINOR_HUGETLBFS_HGM; +#else + + ret = -EINVAL; + if ((uffdio_api.features & UFFD_FEATURE_MINOR_HUGETLBFS_HGM) && + !(uffdio_api.features & UFFD_FEATURE_EXACT_ADDRESS)) + /* + * UFFD_FEATURE_MINOR_HUGETLBFS_HGM is mostly + * useless without UFFD_FEATURE_EXACT_ADDRESS, + * so require userspace to provide both. + */ + goto err_out; #endif /* CONFIG_HUGETLB_HIGH_GRANULARITY_MAPPING */ #endif /* CONFIG_HAVE_ARCH_USERFAULTFD_MINOR */