From patchwork Wed May 17 19:09:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13245568 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 0F8D9C77B75 for ; Wed, 17 May 2023 19:09:30 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B122B280001; Wed, 17 May 2023 15:09:29 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9B4B5900009; Wed, 17 May 2023 15:09:29 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 653F9280001; Wed, 17 May 2023 15:09:29 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 46362900009 for ; Wed, 17 May 2023 15:09:29 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id DD8D41A06DA for ; Wed, 17 May 2023 19:09:28 +0000 (UTC) X-FDA: 80800685616.02.4AC022D Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf17.hostedemail.com (Postfix) with ESMTP id 9A7D040012 for ; Wed, 17 May 2023 19:09:26 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=W98rDRnr; spf=pass (imf17.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1684350566; a=rsa-sha256; cv=none; b=L9DCzK+NOyA2W+WM62LVPDx6r5fb5rcwF7QfdyyjmX57ON8wyouhTEjWRrmRd8t/ALOclh vVTAlnqrgnPth6ksV65vr4yh81woaP5xuB3FCSVk5cqgKreMAtW1p0jIjUbCPZerZGIznn ltY1Kg+tKP9T3N+aCPTa4CqHhDrZiBA= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=W98rDRnr; spf=pass (imf17.hostedemail.com: domain of peterx@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=peterx@redhat.com; dmarc=pass (policy=none) header.from=redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1684350566; 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:in-reply-to:references:references:dkim-signature; bh=iN+jv8msY1qYvEsq3XmStz9lSIVf2EzEiCXgZiX6HJw=; b=zfUNl+nLV3uANNE4NNRrF06DjyV4ypW1DsWVe+93INju3e4idk75koqTFbwQcFOUlNp6JW YSLmcVKxX9bVf92CObedcxFDE1TrNRdnSS/kLwEPlgpGQ6H6U2fADxX05Ih1zHiXYYzov1 veD7Td9PhMG3OkFTT507Y1GTyyB6xXg= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1684350565; h=from:from: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:in-reply-to:references:references; bh=iN+jv8msY1qYvEsq3XmStz9lSIVf2EzEiCXgZiX6HJw=; b=W98rDRnr89/PtgAOaq+HlMjbX4jkMcMl9UytYI1wj9ZPmJUANf1JjL/FKYN990Oj/f6zXY bOUSsKeWmRfK/8fchO7kCULAaRT96C3Usk1ORwRJjj9Wt182mqbD8lIatK+xoeRsnB2pl4 cRkJr3vKrZrw87m+Yz6f0Z+d8wtkl7M= Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-9-Xp7ljQ1bMya5c0BfFNr5aQ-1; Wed, 17 May 2023 15:09:24 -0400 X-MC-Unique: Xp7ljQ1bMya5c0BfFNr5aQ-1 Received: by mail-qv1-f69.google.com with SMTP id 6a1803df08f44-62387ccd3bdso1582206d6.1 for ; Wed, 17 May 2023 12:09:24 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684350564; x=1686942564; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=iN+jv8msY1qYvEsq3XmStz9lSIVf2EzEiCXgZiX6HJw=; b=T6aPEiT9eXlcpxW03fv7y3XMJK8gl2pm6D0kJrpnE8VYiWUydMwszaBOcvRBES45IS UuSbKyUPJcE4OrG3Ks2wRgNBz4NjwWKTfYvZu7IGBWFnnD5oPL8736MgPXoa6yl050P+ TCJHKhRA+BMVEclTs2N/0FgRTvHduD9xXKXQvkK+KEFjIfuG0zXjrDIgVCmG98FUDJtT F2r6PiSOYmjFfiEejr/m5JLerGDcjiuN8ZVnp4q8aXF6HH+VRg41CbEkABbbE5YQM6I8 TDWujPzj/JaMbF4P6c3NVqXb7wzdycOxLIK2MvWzG8Sc4y2DDC6+Zt7UpsseeGfbKyjq Bp4w== X-Gm-Message-State: AC+VfDzgg/f99KbjTth8rleRpEdyKpF7p8OUEaCJl9bkkNUDEgSpIx3C GOb4lfEYMOdMDglQXywZLHf0jHP+yolsNDIhegyVFhKZxxqmQ3sS2bDGzus+6Sj7QM9Pev+sTBJ UZISv0UU8ReA= X-Received: by 2002:a05:6214:c2d:b0:61b:7115:55a9 with SMTP id a13-20020a0562140c2d00b0061b711555a9mr7053384qvd.0.1684350564320; Wed, 17 May 2023 12:09:24 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5P+JGEIsxWMcDTyGW5MOqVSCkIOibsvYLwKde5Ar0gJAeQXW0Wox1vONWp1UZpJXyF/cU+Xg== X-Received: by 2002:a05:6214:c2d:b0:61b:7115:55a9 with SMTP id a13-20020a0562140c2d00b0061b711555a9mr7053354qvd.0.1684350564106; Wed, 17 May 2023 12:09:24 -0700 (PDT) Received: from x1n.. (bras-base-aurron9127w-grc-62-70-24-86-62.dsl.bell.ca. [70.24.86.62]) by smtp.gmail.com with ESMTPSA id k21-20020a05620a143500b0075954005b46sm833464qkj.48.2023.05.17.12.09.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 May 2023 12:09:23 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Mike Rapoport , peterx@redhat.com, Alexander Viro , Andrew Morton , "Liam R . Howlett" , Andrea Arcangeli , Mark Rutland , Lorenzo Stoakes , linux-stable Subject: [PATCH v2 2/2] mm/uffd: Allow vma to merge as much as possible Date: Wed, 17 May 2023 15:09:16 -0400 Message-Id: <20230517190916.3429499-3-peterx@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230517190916.3429499-1-peterx@redhat.com> References: <20230517190916.3429499-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 9A7D040012 X-Stat-Signature: m7difdgq44i78waxdhcjwk91rhozhxss X-HE-Tag: 1684350566-727189 X-HE-Meta: U2FsdGVkX1+BCOcP8JVIIXF7lGfDRTAhjwMMe2hpEHaKxpPyso6oxwyCHUSy5MNwEssApWlaO5zTJ95kwukg5JsLll1SWb4uXrHLS/nWKtQWfcFVeDOWHWsxTcX2u95tSm84QMwUSH6OFSgO5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lfHXmaZJ3b0lrdbwpF5gn6VvT4SxhtVFe0F9p4xVEeZwUYmHpclu8nVGw9tnRqDaI2hTBSKu7qlAMyMLkXUynV2vMxd9VOlhkl22qp3Eya0YTTOwZbQT5zNfIKAhWOy459E+cMWo+OSdtICxMQvkbcwXG9RnEED6/FZUvZPvUCmJcrLWTTu/O4AED6bxkfxL3TPC5+NUBeEwHMP/jyr6MUPsOsFtoufkDJ9RDaNNkJInCxCHQ2AraDEZlWc4qQ3Z87ffsO1M2c/bUnge0/VN7EPE79WqOCopHF6i75SHvUtivWczhjMd04zplRWm0GMyYjfo= 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: We used to not pass in the pgoff correctly when register/unregister uffd regions, it caused incorrect behavior on vma merging and can cause mergeable vmas being separate after ioctls return. For example, when we have: vma1(range 0-9, with uffd), vma2(range 10-19, no uffd) Then someone unregisters uffd on range (5-9), it should logically become: vma1(range 0-4, with uffd), vma2(range 5-19, no uffd) But with current code we'll have: vma1(range 0-4, with uffd), vma3(range 5-9, no uffd), vma2(range 10-19, no uffd) This patch allows such merge to happen correctly before ioctl returns. This behavior seems to have existed since the 1st day of uffd. Since pgoff for vma_merge() is only used to identify the possibility of vma merging, meanwhile here what we did was always passing in a pgoff smaller than what we should, so there should have no other side effect besides not merging it. Let's still tentatively copy stable for this, even though I don't see anything will go wrong besides vma being split (which is mostly not user visible). Cc: Andrea Arcangeli Cc: Mike Rapoport (IBM) Cc: linux-stable Reported-by: Lorenzo Stoakes Acked-by: Lorenzo Stoakes Reviewed-by: Liam R. Howlett Fixes: 86039bd3b4e6 ("userfaultfd: add new syscall to provide memory externalization") Signed-off-by: Peter Xu --- fs/userfaultfd.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index 17c8c345dac4..4e800bb7d2ab 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -1332,6 +1332,7 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx, bool basic_ioctls; unsigned long start, end, vma_end; struct vma_iterator vmi; + pgoff_t pgoff; user_uffdio_register = (struct uffdio_register __user *) arg; @@ -1484,8 +1485,9 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx, vma_end = min(end, vma->vm_end); new_flags = (vma->vm_flags & ~__VM_UFFD_FLAGS) | vm_flags; + pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT); prev = vma_merge(&vmi, mm, prev, start, vma_end, new_flags, - vma->anon_vma, vma->vm_file, vma->vm_pgoff, + vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma), ((struct vm_userfaultfd_ctx){ ctx }), anon_vma_name(vma)); @@ -1565,6 +1567,7 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx, unsigned long start, end, vma_end; const void __user *buf = (void __user *)arg; struct vma_iterator vmi; + pgoff_t pgoff; ret = -EFAULT; if (copy_from_user(&uffdio_unregister, buf, sizeof(uffdio_unregister))) @@ -1667,8 +1670,9 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx, uffd_wp_range(vma, start, vma_end - start, false); new_flags = vma->vm_flags & ~__VM_UFFD_FLAGS; + pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT); prev = vma_merge(&vmi, mm, prev, start, vma_end, new_flags, - vma->anon_vma, vma->vm_file, vma->vm_pgoff, + vma->anon_vma, vma->vm_file, pgoff, vma_policy(vma), NULL_VM_UFFD_CTX, anon_vma_name(vma)); if (prev) {