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 4F4B5C7EE22 for ; Fri, 12 May 2023 01:22:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A7EE36B007B; Thu, 11 May 2023 21:22:19 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A07C06B007D; Thu, 11 May 2023 21:22:19 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7E54D6B007E; Thu, 11 May 2023 21:22:19 -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 676BC6B007B for ; Thu, 11 May 2023 21:22:19 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 3387F140DD0 for ; Fri, 12 May 2023 01:22:19 +0000 (UTC) X-FDA: 80779852398.01.0BE518A Received: from mail-qt1-f176.google.com (mail-qt1-f176.google.com [209.85.160.176]) by imf27.hostedemail.com (Postfix) with ESMTP id 64F5640002 for ; Fri, 12 May 2023 01:22:16 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=Cb9+L6FO; spf=pass (imf27.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.160.176 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=1683854536; 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=C0EOL6LTB8cUfvNIYifV3ZbFHay8GGDSwBe2dif3Kp8=; b=28urs3wDIfMAvXfuLucWy62WcrNIPe3p2+uZaTYock156Im/fyqmCIT/jZv/AJ1qer4mo9 TcMNNzRKOFlCB+KwpSgbNvuwUZDWAESDSr7FHXeSl1DFs2U/dFCqFnlXTwFHyOSlLTV+GI TIQB2T0WoT/LH2p1/FvCwxtI41fwowI= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1683854536; a=rsa-sha256; cv=none; b=dTcXo64vpZR86ZOjWQhjKtrtolVxJUI1Ig5qx0XIaQ5ZlTklHkpAVCY3SOi0SCk4dZfaZk 7hfpK0G+hFHQLQrKhxzw9g/Tr/DiD1nOoXih5ZuUHwYCdX91/sBc3IS6okY3rA0r5f4LbF cMgOkRGBO1mp0tyUSA97BuwWthnBBAg= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=gmail.com header.s=20221208 header.b=Cb9+L6FO; spf=pass (imf27.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.160.176 as permitted sender) smtp.mailfrom=lstoakes@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-qt1-f176.google.com with SMTP id d75a77b69052e-3f4ef2880fcso11088941cf.3 for ; Thu, 11 May 2023 18:22:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1683854535; x=1686446535; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=C0EOL6LTB8cUfvNIYifV3ZbFHay8GGDSwBe2dif3Kp8=; b=Cb9+L6FOD7IB1lj2riMlPrBVLYqEGE9xd8ZQK2J+0Sj0EtCsqCzfoO+5gYhIhokByc Nnzlxaf8YFuQwHPlXu/efqHJOy3GI6cvI8mPB8t5AqlSjjQ79q6uQR4D2zryQfyfdDxq F6T9V0d0xPynniezKDpylx9PSenV3XT+FGhJRjvrvjEsbtoWImDTeepJBRvXQh+VnD7m sOvTb83dASoHf9hABqjntHOoKk0TDTME/UH9yBHCIdlo4C2oIdEllQQtGO16gYQttU/H tQw4wrmRKoBoJ4oxgorndvbSjR9IwTc0z4lMHdwIDs/NmqDE4sB51VIe8QCMx7031P6y xJHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683854535; x=1686446535; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=C0EOL6LTB8cUfvNIYifV3ZbFHay8GGDSwBe2dif3Kp8=; b=lHGxy6JuPz1s1HtX9w14hFCpPEjvE9XA/MNZJJYtvnaxKX1C6QBpZ2HjxvBdxg6NFW vf+fxcIHC3VqzQHgIw3gPUkxuQIDjctCjm7WGmAv2L6ft24VbURaYYOT4u/LWist6lMl vN5BCinpnTrLD7J38IYFxjMxSaMDeDMJQcebeyive81MP8NAzHfGb5kVLsg5HYtcV0Y0 luctvDLuboVMNJwvnCfnwShvNbRfMk7rBou520fem1lVEvMPMsgYReVlQMpuuMiVBU22 bUrEix3ymtUeJdpcuxb8aKfG/IZNj4ETpfSIdYKX4oZFcfYwfXZ/MVhlLqIVUvg/odTF EYsA== X-Gm-Message-State: AC+VfDxIPduuPxwzc3W7MFm6Yy+yQKBEvUZ1siNi7DDKKMK68d9NJu51 FUZvxwyToIPjnk7X4qBul8Q= X-Google-Smtp-Source: ACHHUZ7L4brVemAOQ+tS5peAIkSwaiZOQtK3Cfu4k9VlCRnFDLKYz/z5Q1PqAYrUFZleLNZqh6m0gw== X-Received: by 2002:a05:622a:1011:b0:3f4:427:dbb8 with SMTP id d17-20020a05622a101100b003f40427dbb8mr13492655qte.2.1683854535350; Thu, 11 May 2023 18:22:15 -0700 (PDT) Received: from localhost ([207.164.22.35]) by smtp.gmail.com with ESMTPSA id ci14-20020a05622a260e00b003e693d92781sm2665866qtb.70.2023.05.11.18.22.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 11 May 2023 18:22:13 -0700 (PDT) Date: Thu, 11 May 2023 18:22:11 -0700 From: Lorenzo Stoakes To: Mike Rapoport Cc: Mark Rutland , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrew Morton , "Liam R . Howlett" , Vlastimil Babka , Peter Xu Subject: Re: [PATCH] mm/mmap/vma_merge: always check invariants Message-ID: References: <20230511180841.GE4135@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230511180841.GE4135@kernel.org> X-Rspamd-Queue-Id: 64F5640002 X-Rspam-User: X-Rspamd-Server: rspam06 X-Stat-Signature: ta7iju6gzswknz14kypnm89wyt8mgu46 X-HE-Tag: 1683854536-622752 X-HE-Meta: U2FsdGVkX1+cMFCGmNuuSEdVBO0ot8hGEpWkItmBwTYycXfRHr+CsVHckb6T9OlbPGa20fL8BI8CzrtsMh3UeWhmtsJagrrXLgnjOGg9lsiCAIeFPpzh8MSuSdb6/6EU7NJ/q4G/Q41I42HRj+mkocOqymRoP4zSpHtqKRxYH1Cf9l4gwyfntYfa10XgDh5LY3FOG+KPNY7jUcjE/wCUkbVUfbduQYtJtKCDkOWHCqIn+jJaC7rzo/nGlziVZ4f5xLbIwKJerxIqL1K5AlYkIy/vLPqvLMJxeM7G0IOg/Bihy4H2mB4/omMMjI967ev0cQ2vTdrwa5jv+tZkc7AdTTVR91Oxgk/poqCJ5sfFXQVUqvx+8W+TwJ1im9khqTSYEohk/Tvsz3Si9/wqdKyKWJGIxL4YfIvfjDgwDw0uGU22mAp+Kgkh16neOAx9oKH3WDm3lokwxO0zzJuRzOvRRDYRA80B0AmFjArICF8NC6Jdb8CTpKsvH/MLy77a/jb5JYuqmtjGu3mJs9VlouMeO7Ru/97MuPNxQQgdMs/OEWt75lv/R8h+A97TFcV4HZ2SfsAK1/JhgajVB52aK1d7ExPstpjdmSEzzKc4zqmya1rMKTcbYecsHzgX/83KFvcg3wOX9YE1AR/PDnzGhDNZ4wU+bTPGpursaLSAGpEA7w9XExfSM/ivVwiKZMFlePqp6VQfagSiX86KIq/pjRUKAIpX/7a2VOYbmUYANMTohmR0PFNM9j905mr9hdRnlfG9xyrCObUPzFkMZAcRYGkKcR7WVQTEiMoDUy5QRegvmy4ykj7Vx2zH8gUlJLXDJi9q7PwXxhn/4i3IEFDIF8Sc9JDrDePAorJAleV5iNjPLU536fZu/Pe9VLoYcwryzD124CPpPIJJhMMjLnOP7BnBydcrmXjGF5yZy2WJZqaUk5zlij0RoDuJuj/Jrb4F7gXT3NXZaZOgER4tg0YMO5W UOfs9vC9 q2+T2G49y5XzIs1rqJ7BR5AB7TiSbMjEJfQ0I8lJyr3BwLCUWkpTny5poARBv9/Gb1et6LOwaVnDChZCaOrHEGmNBoAjtxtM1wqdY3uh1cBh1mSB3H8nSKO4AZ8Mllxvey96jJjrTe999y9gyvG+UNmHt67h5NWJv8SdWlfo258QKiPDP9ZNHCN9MxnQquFmYaDC/iAhv6+5PbkFD3iogxy33c9INAoOrNYA2Jaj54mjN55ymiO4XEf6KYfJiMnPua9Skbi35u+YvMC8louFMoz3jLGp4x7Kd0UYX1WrWV4DuqkyLiNWTdFb3Dy3KYChXQdZwCxlpQTg4POXobg/46gzUrhOf/Zh/ivF0SSgYGdNQO2JB/EVF2tMQAivQqswUxMKSv55YAVJ/xWtDDhpprv/77CyKJaSOX7gI/oqZ7BhB/Ax1zZFeQm4RedQ+DLtlwVQJMmegEE2cdd+5M7+BnwfGmAP6cFkTlKKXQDWOJonU+9fj0nsrApns6YLdDawUOQ0RlM89NrwSIXkGoUIZsWwGKv5fVGHCm/qd9EzsMrXA0PtpL9MvXYx68Q== 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: On Thu, May 11, 2023 at 11:08:41AM -0700, Mike Rapoport wrote: > (adding Peter) > > On Wed, May 10, 2023 at 09:26:10AM -0700, Lorenzo Stoakes wrote: > > On Wed, May 10, 2023 at 05:17:49PM +0100, Mark Rutland wrote: > > > On Wed, May 10, 2023 at 09:04:44AM -0700, Lorenzo Stoakes wrote: > > > > On Wed, May 10, 2023 at 03:15:51PM +0100, Mark Rutland wrote: > > > > > Hi, > > > > > > > > > > On Sun, Apr 30, 2023 at 09:19:17PM +0100, Lorenzo Stoakes wrote: > > > > > > We may still have inconsistent input parameters even if we choose not to > > > > > > merge and the vma_merge() invariant checks are useful for checking this > > > > > > with no production runtime cost (these are only relevant when > > > > > > CONFIG_DEBUG_VM is specified). > > > > > > > > > > > > Therefore, perform these checks regardless of whether we merge. > > > > > > > > > > > > This is relevant, as a recent issue (addressed in commit "mm/mempolicy: > > > > > > Correctly update prev when policy is equal on mbind") in the mbind logic > > > > > > was only picked up in the 6.2.y stable branch where these assertions are > > > > > > performed prior to determining mergeability. > > > > > > > > > > > > Had this remained the same in mainline this issue may have been picked up > > > > > > faster, so moving forward let's always check them. > > > > > > > > > > > > Signed-off-by: Lorenzo Stoakes > > > > > > --- > > > > > > mm/mmap.c | 10 +++++----- > > > > > > 1 file changed, 5 insertions(+), 5 deletions(-) > > > > > > > > > > > > diff --git a/mm/mmap.c b/mm/mmap.c > > > > > > index 5522130ae606..13678edaa22c 100644 > > > > > > --- a/mm/mmap.c > > > > > > +++ b/mm/mmap.c > > > > > > @@ -960,17 +960,17 @@ struct vm_area_struct *vma_merge(struct vma_iterator *vmi, struct mm_struct *mm, > > > > > > merge_next = true; > > > > > > } > > > > > > > > > > > > + /* Verify some invariant that must be enforced by the caller. */ > > > > > > + VM_WARN_ON(prev && addr <= prev->vm_start); > > > > > > + VM_WARN_ON(curr && (addr != curr->vm_start || end > curr->vm_end)); > > > > > > + VM_WARN_ON(addr >= end); > > > > > > + > > > > > > > > > > I'm seeing this fire a lot when fuzzing v6.4-rc1 on arm64 using Syzkaller. > > > > > > > > > > > > > Thanks, from the line I suspect addr != curr->vm_start, but need to look > > > > into the repro, at lsf/mm so a bit time lagged :) > > > > > > No problem; FWIW I can confirm your theory, the reproducer is causing: > > > > > > addr > curr->vm_start > > > > > > ... confirmed the the following hack, log below. > > > > Awesome thanks for that! Just been firing up qemu to do this. > > > > Cases 5-8 should really have addr == curr->vm_start, I wonder if it's > > another case but curr is being set incorrectly, it should in theory not be > > the case. > > AFAIU, it's a case of "adjust vma, but don't merge, because prev is not > compatible". Looks like uffd first attempts to merge compatible the newly > registered range with adjacent vmas relying on that there won't be no merge > when addr != curr->vm_start and only after the merge attempt it splits the > edges. > > I think that moving the split in fs/userfaultfd.c:1495 (as of v6.4-rc1) > before vma_merge() will be the right fix. > Ack this was my strong suspicion, just want to get back to the UK and de-lagged before I dig in properly. > > (See [1] for a visualisation of merge cases as a handy reference) > > > > Of course userfaultfd might be the offender here and might be relying on no > > merge case arising but passing dodgy parameters. > > > > [1]:https://ljs.io/merge_cases.png > > You really should put it into Documentation/mm ;-) Well... will reply to your lsf/mm docs thread on this topic ;) > > > > > > > | diff --git a/mm/mmap.c b/mm/mmap.c > > > | index 13678edaa22c..2cdebba15719 100644 > > > | --- a/mm/mmap.c > > > | +++ b/mm/mmap.c > > > | @@ -961,9 +961,21 @@ struct vm_area_struct *vma_merge(struct vma_iterator *vmi, struct mm_struct *mm, > > > | } > > > | > > > | /* Verify some invariant that must be enforced by the caller. */ > > > | - VM_WARN_ON(prev && addr <= prev->vm_start); > > > | - VM_WARN_ON(curr && (addr != curr->vm_start || end > curr->vm_end)); > > > | - VM_WARN_ON(addr >= end); > > > | + VM_WARN(prev && addr <= prev->vm_start, > > > | + "addr = 0x%016lx, prev->vm_start = 0x%016lx\n", > > > | + addr, prev->vm_start); > > > | + > > > | + VM_WARN(curr && addr != curr->vm_start, > > > | + "addr = 0x%016lx, curr->vm_start = 0x%016lx\n", > > > | + addr, curr->vm_start); > > > | + > > > | + VM_WARN(curr && addr > curr->vm_end, > > > | + "addr = 0x%016lx, curr->vm_end = 0x%016lx\n", > > > | + addr, curr->vm_end); > > > | + > > > | + VM_WARN(addr >= end, > > > | + "addr = 0x%016lx, end = 0x%016lx\n", > > > | + addr, end); > > > | > > > | if (!merge_prev && !merge_next) > > > | return NULL; /* Not mergeable. */ > > > > > > ... with that applied, running the reproducer results in: > > > > > > | addr = 0x0000ffff99dc2000, curr->vm_start = 0x0000ffff99db2000 > > > | WARNING: CPU: 0 PID: 163 at mm/mmap.c:968 vma_merge+0x3d4/0x1260 > > > > > > ... i.e. addr > curr->vm_start > > > > > > Thanks, > > > Mark. > >