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 45502C7619A for ; Sat, 15 Apr 2023 09:06:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 437986B0072; Sat, 15 Apr 2023 05:06:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3E8166B0075; Sat, 15 Apr 2023 05:06:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2AFF66B0078; Sat, 15 Apr 2023 05:06:27 -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 197A46B0072 for ; Sat, 15 Apr 2023 05:06:27 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id D3373C01E1 for ; Sat, 15 Apr 2023 09:06:26 +0000 (UTC) X-FDA: 80683044372.08.6DE16A0 Received: from mail-wm1-f44.google.com (mail-wm1-f44.google.com [209.85.128.44]) by imf12.hostedemail.com (Postfix) with ESMTP id 09EAE40012 for ; Sat, 15 Apr 2023 09:06:24 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=WoN9ksEF; spf=pass (imf12.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.128.44 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=1681549585; 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=PL6bHkr+M7EoVmkxPtD7r576pSBguHFh3HHTNrHOXq4=; b=Ncskmqu9FSZb2pCD3bg3zbLpUmXpvIxdHx+g27CXInRuGRoQ890bSNee9OiYsGRs8n2pZY X+BBmCGNnBoOYiQ9aZNsouCeMCn/pyhX9pQlyvoEaOOeqXnK3GDyGqzxzyChMpb7hJGha/ q5yEQLTDlCtmm5rCWVAoVgAE/kInhKw= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=WoN9ksEF; spf=pass (imf12.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.128.44 as permitted sender) smtp.mailfrom=lstoakes@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1681549585; a=rsa-sha256; cv=none; b=GqXGWwbo5EkyPBH+++pVVs2Lvz2qLGCpablxEfNEcXBmKJcy3ur/+Uo/A5XZO0n364L7HL 29Pz11Eo0qP8fVOdRx59aLEXRJb83cL1+Ago+nsxEzlcWvrhHfO6zjQZyAw+zv3pUccEBE TFiXpPTUlTZ+8Fd6l/P5e8PxmBOqx6w= Received: by mail-wm1-f44.google.com with SMTP id l10-20020a05600c1d0a00b003f04bd3691eso21790358wms.5 for ; Sat, 15 Apr 2023 02:06:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1681549583; x=1684141583; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=PL6bHkr+M7EoVmkxPtD7r576pSBguHFh3HHTNrHOXq4=; b=WoN9ksEFuQ8QUQAyH4cmjMVVsA13VRy7nhVB36wWA09YtDQfJ5t7eDQw7S9MA2vZ4A UFQCR732185pXcGy4gqzxWXfYVKT/NlP0kdst5qvUkewrLsodz2Yj52epRqRwQ/ECPHq T2jWzaOcoKDqQgMIrLi/ue9YHqBCNW6bbKaKJb6LnzpelBdR1dHCVhxhv7bDDoAF2/Gs KUawhvSFuyYoquODp9d0i52jkFh8KEUrOkXVD5G9sfD/jaqvwopkU6oYg2YA7hEnLxg4 MAkfxOb1PWSTIN8LTaLdWqrpxz3B+I2nRyhpPb+oaqRtJCs6rzI5cHnbjBzFB+F815sE 9XDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681549583; x=1684141583; 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=PL6bHkr+M7EoVmkxPtD7r576pSBguHFh3HHTNrHOXq4=; b=STcjtU69siOXKWQ2RdQ7Ohd+TtS+7NWVHYGqNOxqToIs8tGVkHxoHT0GqUWyw9o4m6 5MTNvloSZkH1tfjNFIRiGNk2ie/SopmOozc2n4GIdMg97FZ+g2HDMRE3Es7tp9r1COKk qU1/ISfWmDyYvJPVg2Vv2Wr6B92/AXXimDXsECpHwVl9p0czaFe3irFzhB04uyuZpJzk AUEIrSbFBMeNPZe8craRcc0dhYfGrOFHYa2UN8juUC+Uzt/fRT2GQoKFFaqpsvj+a24S Y3MuJ2veY6Ly/3lr4g4gj1fKQpRaBERgXyXu38xLf5CcXlphrSMDr4adPfOHS78L4qpw PJoA== X-Gm-Message-State: AAQBX9es8pFbTXKpWUegZTTG0m0Ei4e+R0xbtocSZEfyng3e0FSP71Nt dK6sZnZ+Cggd1G5hk/Q+XNwSBR8IGHx48w== X-Google-Smtp-Source: AKy350bI3XBjE2GChzSj/DnCLie4wBOn0bHEI1PubigwHNZvTnjy6f8yWDoTlQeIbdBcUYW4963O8Q== X-Received: by 2002:a05:600c:221a:b0:3f0:a798:2757 with SMTP id z26-20020a05600c221a00b003f0a7982757mr6248546wml.25.1681549582966; Sat, 15 Apr 2023 02:06:22 -0700 (PDT) Received: from lucifer.home ([2a00:23c5:dc8c:8701:1663:9a35:5a7b:1d76]) by smtp.googlemail.com with ESMTPSA id t15-20020a05600c198f00b003f0a007b802sm8888533wmq.12.2023.04.15.02.06.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 15 Apr 2023 02:06:22 -0700 (PDT) From: Lorenzo Stoakes To: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrew Morton Cc: Matthew Wilcox , David Hildenbrand , Lorenzo Stoakes Subject: [PATCH v2 0/7] remove the vmas parameter from GUP APIs Date: Sat, 15 Apr 2023 10:06:06 +0100 Message-Id: X-Mailer: git-send-email 2.40.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: qyog47noikz48jckniae4qoae5i5kjc7 X-Rspamd-Queue-Id: 09EAE40012 X-HE-Tag: 1681549584-531707 X-HE-Meta: U2FsdGVkX1+CtI+KRzpAJZfGLX5yzUKJMObryJe2bGe/BfKhNxQkpc3pOt7V8XhM80VzqntvhPQSmEFuZablqdAE6Dviebjow2YjLQMPIffGokzAdpzw2N4zdJV89PzhYLxxXXkzoZKIfznLTtzIgC54kcUfHlJnIfLzdpqzbIKsmcyZG0IrkjLpIlfRBBCbavP1uRa2fTZ/T/wwhOK6tn0GXeZVvHf4/eZK7yHEAyBVTO96pvuSbaOQdTg1UvItU72c60IQTlwwCq+gL2UzcOC87bCa7qqbWUVDwSRC6i+Wliz7ud4IcnPhm+SdwFAJocB598DYw/6ZiIKW7X/LiJRkUw9IT1GrzwbsGzuM3a3v6WfJ1xl3KI2oJBHTikxHeUF0Iqg8XHWEKzUYqZ85HRfOExaHANJC7sb0+byrd2gUiXF6nueQ7aN/j5V3RN+rqEQb3+JpObFGmw+RnZt9ECHFw+MhcxWEGtbmumrOKSax/oAe1w4lHgqaIFRpehzohUS43GTR7Ez30xPdeYdu0nWjikrYFr09c29VN+1xRk34308Af+HtFzzR359JILdY2zTED3RiUsu/iEnKwAHBsO7pW+mgapDqXx+oTwkM3Th4A7fAHL/cBYlsu5+TIteHVLPR/B++/oh6eVvjsyCzBmvKtfZXpHnPu+Xo8UT+BJ3Xesn1XqQiwQwBHD3eK9UoMCs4LpnMW9qthUaubbwLzkkWrM5s55VCXeOK4NqF+/BqzoeMO7Bq+nCxi76xogtcuDmIj57UahuyroyezR6RKilOSgLWUnqhrTOASSp/0F8mlxC6Ne8QH5TYE+wrPSJNdNXOGnpM934Ivrsy7NxueRCQ4QuG3mB1/om6PP3mFHuvYoNlWsZJyaTMX9WDZwIC3TQgWaebEx+NaHGxQ02me7ElVsIMtivhlWwZkXwci2oeHRz4VUhDnaYxZh8FZI2JM6xVECjriK6cJSMdmp1 mQA6PSfM ygNjcbwSrkG46W0JuZnWg5UwrfKIT/1ZEpFd9qHWyHoyCAhuyHtsxDklK8VnjMOATsrM1fNnkFKSSBWFCQxfqInHXfb4k9ZSzEn/2fj40+Q1unpq3geaz1ufyZea6iGizhUh5y9MHaQLqwqU29/snV/Ibjgyf4dwtLq5w/6B3EW0LUBafCJLBMuu+Nv9Gkg8oc2eH7iaPG/zMAFIeipVsCL48roRhwpA6YdBmbgN9Zx01XTjgtm3GoRbCYSuXDNfngxpLSgOdvwwFSqHb8eYlL07XA8ESH0me1h/qOXKjTdL88MVu6kWce41WkKp/Hek2lSm1fytZQPcvrcd/8xkUQAUnvsoG41+hs5syrMzGdla8+PUnie8PTNPiqp+vB3wLW2PMnRAE40nATZWYWOI7P6CFIKjZnW1buDmxa9ElxhQd+Mp3qQ68ZO6ml5uJVl10MGqa7jePa9XfNEe6/dWzBuVAKry0CmuYyVApyAfddmwtrPi2aYFvemz9/A== 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 have also to only be accessed under the mmap_lock, 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 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. The special case is io_uring, where io_pin_pages() specifically needs to assert that all the VMAs possess the same vm->vm_file (possibly NULL) and they are either anonymous or hugetlb pages. To continue to provide this functionality, we introduce the FOLL_SAME_PAGE flag which asserts that the vma->vm_file remains the same throughout, erroring out if this is not the case. We can then replace the io_uring case by passing FOLL_SAME_FILE and looking up the first VMA manually and performing the required checks on this alone. The combination of the two amount to the same checks being performed (and avoids an allocation). Eliminating this parameter eliminates an entire class of errors - the vmas array used to become a set of dangling pointers if access after release of mmap_lock was attempted, this is simply no longer possible. In addition the API is simplified and now clearly expresses what it is 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 the gup_test and a simple io_uring program which exercises the use of FOLL_SAME_PAGE with no issues. Thanks to Matthew Wilcox for suggesting this refactoring! 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 v1: https://lore.kernel.org/linux-mm/cover.1681508038.git.lstoakes@gmail.com/ Lorenzo Stoakes (7): 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() mm/gup: introduce the FOLL_SAME_FILE GUP flag io_uring: rsrc: use FOLL_SAME_FILE on pin_user_pages() mm/gup: remove vmas parameter from pin_user_pages() mm/gup: remove vmas array from internal GUP functions arch/arm64/kernel/mte.c | 7 +- 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 | 10 +- include/linux/mm_types.h | 2 + io_uring/rsrc.c | 39 +++---- kernel/events/uprobes.c | 12 +- mm/gup.c | 121 ++++++++------------- mm/gup_test.c | 14 +-- mm/hugetlb.c | 24 ++-- mm/memory.c | 9 +- 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, 127 insertions(+), 164 deletions(-) -- 2.40.0