From patchwork Wed May 17 19:25:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13245598 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 2A8CDC77B7A for ; Wed, 17 May 2023 19:25:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9F8EC900005; Wed, 17 May 2023 15:25:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9B27B900003; Wed, 17 May 2023 15:25:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8703B900005; Wed, 17 May 2023 15:25:37 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 72B53900003 for ; Wed, 17 May 2023 15:25:37 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 3CDEBADFFE for ; Wed, 17 May 2023 19:25:37 +0000 (UTC) X-FDA: 80800726272.15.7D75D87 Received: from mail-wm1-f43.google.com (mail-wm1-f43.google.com [209.85.128.43]) by imf01.hostedemail.com (Postfix) with ESMTP id 59B8F4000D for ; Wed, 17 May 2023 19:25:35 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b="lZ/tsD0x"; spf=pass (imf01.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.128.43 as permitted sender) smtp.mailfrom=lstoakes@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1684351535; 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:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=krdZpOePDdbE0nF/pHwU45mYku/jPnhACH422xo4dBQ=; b=fPxoXs/cFvVOp9BWHxz6wl1BP9AJM7JQeNdO4InJW1gMQINjpHZjmCkIDXN3IojXA5H8Kx +blN2RJjykc2D1qeSM3Ta3BsHSlgAgKPlui/TV4hItg41dlZjDmcHASzNwwjlXVwMzG0OD jIOKwEaDYZZWBs9YTZbqTBlABXjGFNA= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1684351535; a=rsa-sha256; cv=none; b=ZXjQtCaoV+5s85Yigjh/kXzftxwnj2G52zYa+RkKyiN3gCK5sGT9luPpQO2vtNocX+cSpE txaX4jblr4dUTgQ3t9g0nQTTBfRQ2GW7YtG9Q4ezaQh1+Ci8zQ6KSFNxWKeAsekUv1tHH0 i674bsGh8dwPDlZdEP7vfvHrgMOgt7Y= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b="lZ/tsD0x"; spf=pass (imf01.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.128.43 as permitted sender) smtp.mailfrom=lstoakes@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-wm1-f43.google.com with SMTP id 5b1f17b1804b1-3f42bcf5df1so11647255e9.3 for ; Wed, 17 May 2023 12:25:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1684351533; x=1686943533; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=krdZpOePDdbE0nF/pHwU45mYku/jPnhACH422xo4dBQ=; b=lZ/tsD0xcExcp1uVfmzwDMzud4MC6A3GmJK9RyCtoRDYOK9zzUrgfb80Ydgb0g2l4f 6kfqKReWuFzfexwjThTbGd9Tl7wxG2QuUneGYiVZ2hjk9QqArpW5yrqtGsVik1c6tZ7h +dvLDtSUhNbOg8uXGNdS33IEyEn9JA6r8eAkhyjl/whJAHlVXrSIeg5p+Wzvs9ZfHL97 M76l+0eoGhcjPEA4DT5FILL8SXhQQ8xarlxaDe2LrgdcbRgRTFc3BY7jgdLGeDfqL9r6 o1cu0RRnx/o7XvuqTdv8CO5uaynv3ajaFAFb6cCTVY9l83tKPrGqWfL3GistfGYrXE93 W+0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684351533; x=1686943533; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=krdZpOePDdbE0nF/pHwU45mYku/jPnhACH422xo4dBQ=; b=ei5h9JmWXhFU7v4rR144cIZykMtkGchj82iAYPMDowWu3j5FUm6aAHd3Zgu2DCaVrg lAIU73n92+/tK5AdqQNQ593/XmIzF9ZN0QJezKMDWuqSo/TE5VkjiiuQU0CDzcW2DuyZ ln1XDOtdf1JMY53xJZHSvlJYIpnq4JpMUl5c5lv3XfqcoAxDL8fEewMev86VQ/4zqKIh vQcd9u7FSTLUAkFhk9PRNfqOxlkwyELQwDWd8Km8hbXKAuxkP+5FyzO8ub1P+z0L2DLW I5q3pz69UX26io8o/G7sydkLXimFDJfHNDr7wIW4As7/PS9L/DEJvt7yEBXt/W0price gHTA== X-Gm-Message-State: AC+VfDxvpKbJUP8OpablaeCGVZkvUUaXO4XDVLbUeD1Ot8DERsT3o09D 2MDvgvTfbg8CcmuHiVaDksoASziJhWOjkw== X-Google-Smtp-Source: ACHHUZ6RCKgAiLRLWeHOHRHN4W3m2S7jL+0mVpg24O1dxpEHSUO6TGAtwZ1PB7VA7bW47TOcMTavyA== X-Received: by 2002:a05:600c:2146:b0:3f5:60b:31 with SMTP id v6-20020a05600c214600b003f5060b0031mr9091578wml.27.1684351533046; Wed, 17 May 2023 12:25:33 -0700 (PDT) Received: from lucifer.home (host86-156-84-164.range86-156.btcentralplus.com. [86.156.84.164]) by smtp.googlemail.com with ESMTPSA id 5-20020a05600c020500b003f43f82001asm3092398wmi.31.2023.05.17.12.25.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 May 2023 12:25:31 -0700 (PDT) From: Lorenzo Stoakes To: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrew Morton Cc: Matthew Wilcox , David Hildenbrand , Jens Axboe , Pavel Begunkov , Jason Gunthorpe , John Hubbard , Dan Carpenter , Lorenzo Stoakes Subject: [PATCH v6 0/6] remove the vmas parameter from GUP APIs Date: Wed, 17 May 2023 20:25:29 +0100 Message-Id: X-Mailer: git-send-email 2.40.1 MIME-Version: 1.0 X-Rspamd-Queue-Id: 59B8F4000D X-Rspam-User: X-Rspamd-Server: rspam06 X-Stat-Signature: fnbrmss7kugsfxk3wb6ji59454c4af4w X-HE-Tag: 1684351535-558678 X-HE-Meta: U2FsdGVkX1+sfcMl49QzxJcOjiOs1rKfZ2LmWDqhyu/fzdwffSm56U7yeYWyQUs/g8PDXJR9KyFKDbBtaj1tr6fB6aFHaFkCOV5ma5v/4IB5oDJEpxlWpp/peV/UA+NaVt6yoB7QSrH4j8n9s0DWKCjyNOvoaOnTVDlC1hQ1/PeXdcLp/fzozKhQM/aa18rHdwLErXDANUzR3zGqiYY62KHfRirlCROxV3kNgA30CUE8begzPAji/mXpCW3E+Fd2mS4pdkrPIY65UNhxlM9fEK3AJYaffQfK7RQKE1TCgO4/La0SRMY002ATtucsIVL2D1RJD1OvncFdubKEhnbgxDntHuz556tlu2vFkAncf05NHNndXCAGbIAbcW3BfE7Pe/Gl+IQahjTS//NBK1IqX+QZtFlfTKZN2YY0xRjPKcrVBRMFf5GzHibxGAvge2hC0axaMKld97866BIOAs3DZN3filKPC1WLYlRr1xzL7aCtkLMovHas0C9Is1WdtU8xyyVuZ4NUAlc4A3/Jyfmgsd2DAvn1M1oQwprD4jMYhCjyGRmmtYfWj5yrJutUd8nDnOJPAHzcjsVwpASzv70qgUPH+WY2O1KCLCMxModWCGDFJ3CTe/GDNqVIzce/6Z9zrh9w1eOFaLbr7bMGxuhk+486rSJW92zjwY2VW3AnCkpGzAFfjipF/VLv4cOOOAU3sMtlfptcpJJrUiBtATDQJPyP2CyoUD8vRJR4i4WZaLK2nc2H6vGXZMw/dfJjcLzuzglxMPyq1D4VyznJAPJ1OopWZ56MdV4KsGAeen9XmHN37oXC3Ogu5IorlcaBaPZq6tUVcLKOoFSqDhIT8qLMAAIU5kzcu1txujLj/PtSIz3zIDZ/61Rum8hFAehxCbCPdUnwkkM/PisJzFqAJ0qDsFYl2XybsPW0C0++ix/R8kzgROpvioTtETzdjY0vVbMMRR7D5JBqyLV+Wl+vKvw XcfCKms3 6Kuic2RT05G04P0e3kDWiGKFIj33nkBjLIx9OjWBwy7VndiwqQU8pmo1rZ/i/6YB8itVY/nuKX464761btRwcSoM6lGCuOlrwzqS0dCsmCAw8vP2CaQvEmCpWBMMjxNOo6KXi06KX7MOCabV6gIwONsXAwai4agkq6HvsVJvf0rWgO0fo6zsdynIB0dmjrcTnOZU9P+g/iVjlMn27hN/zcjhIm4Ps+q8lzNBMSWrpV2vk653DLNcGkRq/TjeRI7fcz0jqjSd+f2a09BxCU3IxXMTSQlPzZJOEuFwgm3u/JdSKUfymelwDRuYf+nXtmRMwu3CC3C0g1ukGfyutTEbMnDOFjkdo8WkTa6ugq6IzxbR/grhZSRwJG7xyeFet9gdqwqcCglsEZ4nCv2kJfU5tuuDEAVP3bWO60/16vE01yPkq4/5Y29R5JK70qugapXleUAXcIOTDz4sFqspN2vKsLrOEsPceH8f1j/98zzKLAym5A/gZ0G7a2l9JbYhiXQZkJI+6d5JoL5UvnKwnmLEgGMixVvnLSgGKAfRRHROQ/dxj/PI= 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: (pin_/get)_user_pages[_remote]() each provide an optional output parameter for an array of VMA objects associated with each page in the input range. These provide the means for VMAs to be returned, as long as mm->mmap_lock is never released during the GUP operation (i.e. the internal flag FOLL_UNLOCKABLE is not specified). In addition, these VMAs can only be accessed with the mmap_lock held and become invalidated the moment it is released. The vast majority of invocations do not use this functionality and of those that do, all but one case retrieve a single VMA to perform checks upon. It is not egregious in the single VMA cases to simply replace the operation with a vma_lookup(). In these cases we duplicate the (fast) lookup on a slow path already under the mmap_lock, abstracted to a new get_user_page_vma_remote() inline helper function which also performs error checking and reference count maintenance. The special case is io_uring, where io_pin_pages() specifically needs to assert that the VMAs underlying the range do not result in broken long-term GUP file-backed mappings. As GUP now internally asserts that FOLL_LONGTERM mappings are not file-backed in a broken fashion (i.e. requiring dirty tracking) - as implemented in "mm/gup: disallow FOLL_LONGTERM GUP-nonfast writing to file-backed mappings" - this logic is no longer required and so we can simply remove it altogether from io_uring. Eliminating the vmas parameter eliminates an entire class of danging pointer errors that might have occured should the lock have been incorrectly released. In addition, the API is simplified and now clearly expresses what it is intended for - applying the specified GUP flags and (if pinning) returning pinned pages. This change additionally opens the door to further potential improvements in GUP and the possible marrying of disparate code paths. I have run this series against gup_test with no issues. This patch series is rebased on mm-unstable as of 12th May. Thanks to Matthew Wilcox for suggesting this refactoring! v6: - Fixed up unused variable warning under !CONFIG_HAVE_IOREMAP_PROT reported by Anders Roxell. - Fixed up use of uninitialised vma parameter on error/no page found reported by Dan Carpenter (caused by reuse of code with helper that sets vma under different circumstances). - Fixed up the missed kernel/trace/trace_events_user.c caller. v5: - Remove the io_uring open-coded VMA file-backed check, as this is now explicitly disallowed by GUP. - Updated the subsequent patch to eliminate the vmas parameter accordingly. https://lore.kernel/org/linux-mm/cover.1684097001.git.lstoakes@gmail.com/ v4: - Drop FOLL_SAME_FILE as the complexity costs exceed the benefit of having it for a single case. - Update io_pin_pages() to perform VMA lookup directly. - Add get_user_page_vma_remote() to perform the single page/VMA lookup with error checks performed correctly. https://lore.kernel.org/linux-mm/cover.1681831798.git.lstoakes@gmail.com/ v3: - Always explicitly handle !vma cases, feeding back an error to the user if appropriate, indicating the operation did not completely succeed if not and always with a warning since these conditions should be impossible. https://lore.kernel.org/linux-mm/cover.1681558407.git.lstoakes@gmail.com/ v2: - Only lookup the VMA if the pin succeeded (other than __access_remote_vm() which has different semantics) - Be pedantically careful about ensuring that under no circumstances can we fail to unpin a page https://lore.kernel.org/linux-mm/cover.1681547405.git.lstoakes@gmail.com/ v1: https://lore.kernel.org/linux-mm/cover.1681508038.git.lstoakes@gmail.com/ Lorenzo Stoakes (6): mm/gup: remove unused vmas parameter from get_user_pages() mm/gup: remove unused vmas parameter from pin_user_pages_remote() mm/gup: remove vmas parameter from get_user_pages_remote() io_uring: rsrc: delegate VMA file-backed check to GUP mm/gup: remove vmas parameter from pin_user_pages() mm/gup: remove vmas array from internal GUP functions arch/arm64/kernel/mte.c | 17 ++-- arch/powerpc/mm/book3s64/iommu_api.c | 2 +- arch/s390/kvm/interrupt.c | 2 +- arch/x86/kernel/cpu/sgx/ioctl.c | 2 +- drivers/gpu/drm/radeon/radeon_ttm.c | 2 +- drivers/infiniband/hw/qib/qib_user_pages.c | 2 +- drivers/infiniband/hw/usnic/usnic_uiom.c | 2 +- drivers/infiniband/sw/siw/siw_mem.c | 2 +- drivers/iommu/iommufd/pages.c | 4 +- drivers/media/v4l2-core/videobuf-dma-sg.c | 2 +- drivers/misc/sgi-gru/grufault.c | 2 +- drivers/vdpa/vdpa_user/vduse_dev.c | 2 +- drivers/vfio/vfio_iommu_type1.c | 2 +- drivers/vhost/vdpa.c | 2 +- fs/exec.c | 2 +- include/linux/hugetlb.h | 10 +- include/linux/mm.h | 42 +++++++-- io_uring/rsrc.c | 34 ++----- kernel/events/uprobes.c | 13 +-- kernel/trace/trace_events_user.c | 2 +- mm/gup.c | 105 +++++++-------------- mm/gup_test.c | 14 ++- mm/hugetlb.c | 24 ++--- mm/memory.c | 20 ++-- mm/process_vm_access.c | 2 +- mm/rmap.c | 2 +- net/xdp/xdp_umem.c | 2 +- security/tomoyo/domain.c | 2 +- virt/kvm/async_pf.c | 3 +- virt/kvm/kvm_main.c | 2 +- 30 files changed, 142 insertions(+), 182 deletions(-) --- 2.40.1