From patchwork Wed May 17 15:04:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 13245167 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 8DA11C77B7D for ; Wed, 17 May 2023 15:05:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 309B4900009; Wed, 17 May 2023 11:05:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2B872900003; Wed, 17 May 2023 11:05:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 17FF5900009; Wed, 17 May 2023 11:05:33 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 0660B900003 for ; Wed, 17 May 2023 11:05:33 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 8EF2116060E for ; Wed, 17 May 2023 15:05:32 +0000 (UTC) X-FDA: 80800070904.20.F0CD285 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf24.hostedemail.com (Postfix) with ESMTP id 8C68B1800D6 for ; Wed, 17 May 2023 15:04:23 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=hcAHLwoO; spf=pass (imf24.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.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=1684335863; 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=Ms+Yw6xLDbHeWgltH2GeZ/mYx1EXxuQy0fD+zKwpp0k=; b=5dzopzAGwudgZVhj8o+XIMgnmpj9+7R41wKa6J8wBUOl7H0RlIINHg6JxaRvwxVTiE6pqr y8h7V4Wc5RqLo68X8DCzb2X/B9K01JMfiQOb6c5g6YNnCepdDvPb1iavf6JUG/srjAmgin VK5r6HIC9WRHnFEBL+70Jh4fM8aEQIc= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=hcAHLwoO; spf=pass (imf24.hostedemail.com: domain of peterx@redhat.com designates 170.10.133.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=1684335863; a=rsa-sha256; cv=none; b=S8nnknMfEBzgA735PZ6lfWjCRxo+tapOvm5wy7EdJuCnotgeAtyEJoFPRmJhcO9J4SkcCA X+ei51RLizVHPaOsm2j0Smr/o/l4O7wCYnrUlK0+8gHrBkktYw+xfK4Mbf4GMIghDWxUK6 WCKAjWrwzzGoVkCaedc0StrPBTei22I= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1684335862; 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=Ms+Yw6xLDbHeWgltH2GeZ/mYx1EXxuQy0fD+zKwpp0k=; b=hcAHLwoOFO6EzS8IBQtau3vvAe1fid/mg5nL8tonFbgkTF/kykh751lUsEYy2tg637ATMw fduivUAEcskJzKUhVT1f/WGMmk9A9R5JVAih5liZDweircY69iPFhYI0o0JcTJz2ENuHDO 2tirxQqk18anO/shw8y2fC5qwsBc+9Y= Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-326-6gJYAoOfMkqG52QZJZRuGw-1; Wed, 17 May 2023 11:04:18 -0400 X-MC-Unique: 6gJYAoOfMkqG52QZJZRuGw-1 Received: by mail-qt1-f199.google.com with SMTP id d75a77b69052e-3f387d3b41eso1457711cf.1 for ; Wed, 17 May 2023 08:04:15 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684335855; x=1686927855; 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=Ms+Yw6xLDbHeWgltH2GeZ/mYx1EXxuQy0fD+zKwpp0k=; b=HiIwIpEJGMqpklWVprFV3kMPZBjJur8Gr4mdK4YGW+6/t6j73hYxY0Gs9oB4IAchrE rqNIG2FfqpZGhmr0NhaZWFQSK1Zvx1yqvIqJ8SBW9oW0rEyFvs3qK5VHmoHQVyScIW75 VrzAt0wYNA7o8ecwTkpibecy22y+rOtrkzfejllzVcpF2lUK7Uy3SZKj365Kn/GH5+QN w3+enYaSwF56xZzNwrReNCENT2vARfPmSDISa0DoFuwpZB/D1N5awZK8yDvunAuv0uFO fzO3rbpTDZ0HkXsAOH2XUXuWOKVBhmf3YP33MUrU2D0cuzDxUwIu1QoedtVbDY6PAsNE Th9Q== X-Gm-Message-State: AC+VfDw0EQJghtpWHDWhFIBv/mwZOPBjInLX3gYxHG395AZxgxOxsXSY lT7V8FhRIxbTgXmzlxVsbX+czuOjIGpok5U91BUkkzE4d4ZcH39LcXWkT6zhzrvExi9hD2cXW6c VpYofVOrnGRc= X-Received: by 2002:a05:622a:1a9e:b0:3f4:e9bc:22e with SMTP id s30-20020a05622a1a9e00b003f4e9bc022emr5674336qtc.6.1684335855173; Wed, 17 May 2023 08:04:15 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6hB1PyGgcmxyQUgGB/Eq0H7OvCFFhm3wB8MI1WHZGGvhbCG9ZQuuvRPeeeoIz0SVcyOGZnxg== X-Received: by 2002:a05:622a:1a9e:b0:3f4:e9bc:22e with SMTP id s30-20020a05622a1a9e00b003f4e9bc022emr5674272qtc.6.1684335854592; Wed, 17 May 2023 08:04:14 -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 u10-20020a05620a120a00b0074d4cf8f9fcsm661141qkj.107.2023.05.17.08.04.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 May 2023 08:04:13 -0700 (PDT) From: Peter Xu To: linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Lorenzo Stoakes , Andrew Morton , "Liam R . Howlett" , Mark Rutland , Andrea Arcangeli , Mike Rapoport , peterx@redhat.com, Alexander Viro , linux-stable Subject: [PATCH 2/2] mm/uffd: Allow vma to merge as much as possible Date: Wed, 17 May 2023 11:04:08 -0400 Message-Id: <20230517150408.3411044-3-peterx@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230517150408.3411044-1-peterx@redhat.com> References: <20230517150408.3411044-1-peterx@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Rspam-User: X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 8C68B1800D6 X-Stat-Signature: rsrw668pm9wt67ukcge5tmuyb199pu9o X-HE-Tag: 1684335863-346962 X-HE-Meta: U2FsdGVkX1+dsvBW0nLK3W96NbLud+v7r+QAXXe3IikWKKDJOORd/R5pYB3k9BNVgohJjwVHqfttijx6ua2XIo+FxhMNTDurTMuwe8i0L/aiQYJZwsP7P/i9RZdB5a6cZsy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ruNN53tlNJ3APlxzfXDoZYY1BVRxu03APfBpH+XBQI0x10wo38mMwZ89KDrx6UDFNfhUOe3A0dnRuQVTtcgzduc6dQhqdOnJW2L1Krq5GBYeZXqS40sAMuZ80daByVaQZ3OmJwdGfvn5h629tEAlfnz8IGETCkytDrX30YomyGdnXYGNgkS11neCjR+yVoZQZfnOxjbZ6/LHm06xjm2Z2NaZK4Y94NTqkC+GjA++9/kBGJMLiQHL3HZCqyjOBwyWOaIfs2xYQHYiSybzNiqb7uqKRVm9qYE2+spP 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 Fixes: 86039bd3b4e6 ("userfaultfd: add new syscall to provide memory externalization") Signed-off-by: Peter Xu Acked-by: Lorenzo Stoakes Reviewed-by: Liam R. Howlett --- 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) {