From patchwork Sat Mar 18 11:13:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Stoakes X-Patchwork-Id: 13179746 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 52FCEC7618A for ; Sat, 18 Mar 2023 11:15:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 35A77900002; Sat, 18 Mar 2023 07:15:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2DF5E280001; Sat, 18 Mar 2023 07:15:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1A573900006; Sat, 18 Mar 2023 07:15:35 -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 0ACCD900002 for ; Sat, 18 Mar 2023 07:15:35 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id A6723AB599 for ; Sat, 18 Mar 2023 11:15:34 +0000 (UTC) X-FDA: 80581763388.14.210AD03 Received: from mail-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) by imf23.hostedemail.com (Postfix) with ESMTP id BE7F4140007 for ; Sat, 18 Mar 2023 11:15:32 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=YMQqEVZi; spf=pass (imf23.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.128.52 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=1679138132; 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:in-reply-to:references:references:dkim-signature; bh=YNIdJ6Vt6KcFvn7hpLjquLcVPqIZMiCegIYHYyxVYMQ=; b=DQrQYVw47k7k9b+8LkVLQxJaNrZOBW9QoVRKFDH5XZeM++pgRNbdB0Ur/x7037+RBMavZR N/WrH2zMfblnRJwPU0omMD2OerxuIj51axxs7qvnNtv822EXgvB83JUmu2cFqEItNYCpHR +UVSWyjUGARakwBmknbNaFbaoTmgt4I= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=YMQqEVZi; spf=pass (imf23.hostedemail.com: domain of lstoakes@gmail.com designates 209.85.128.52 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=1679138132; a=rsa-sha256; cv=none; b=u05dQkfFlG3rPcPhIkgyumI0PPuQk0Bggcw4Wl9u2B9Q7AseNN9ZATb7jgLWtEZ09arcTS tcnsi+sIhQMj1PHP6ZpsH8EpNlhhwGttiIq4DdBpUTZ00AC4B+Rf34OQdLsUb1agGmsSzS A/IKBz8Cs/ZUIbPOD2j2fKZe1TklXwE= Received: by mail-wm1-f52.google.com with SMTP id p13-20020a05600c358d00b003ed346d4522so4779840wmq.2 for ; Sat, 18 Mar 2023 04:15:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1679138131; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=YNIdJ6Vt6KcFvn7hpLjquLcVPqIZMiCegIYHYyxVYMQ=; b=YMQqEVZicnvPGmZNhsvLOxCWnnQ4bjhFqhJ82RSQUi0l0UVsKNSk3G7LWahwyS905J swwajsI070tAoW5Lm6cSP7fXTIC4aTibf8W/tNdetrlBGyPiIq+HIZ7DLScmCKjLOTby nttJ+XmxMdfwYloG/jxJxQR/KmIjTQxvdQOFDnI7HKBupIH9vOALsYdb4cVJnJxRJwuo Onw484Ikw64e8ppcyEHnXW7y2q1jTAKZWcB9r0n0onaKB7xG7uUr1ZvhzjE892amjNLs 4i/8D/ciyaHtYYQF0XluT6STyjKEr6PUmgcjpPhiJLBjKSEvzfheFEii0uC8cUJfoDx7 ALIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679138131; 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=YNIdJ6Vt6KcFvn7hpLjquLcVPqIZMiCegIYHYyxVYMQ=; b=H+8d+EcgN2IoM/rPdF/MrWGv98moqNvN1Lc6dUrCoIZOamapBdLR1JMJwduyMWyPwO 4LzyhNpcFiHwmUdv4IeEZkztuDatXMMiJeql8vk+cSpz3GS3M4tIhUuDlUwbWvOxdcPW Aqeb7U2Bo/MwEIra8bNPCxbpZ3Ee1qinQbFGSPdCsAJyU6dPRkd4hjuM/mZu3LEaz/dn NRR85ie357w8KZ36Ym3BtmB4T3JCMjGA1Whdbo146mk2MlhmEVzsmYSUQwhBr6etljY6 jgwotzNTihs2jIkdLKDAa0k3AdR7EvvjfdKE3AWMPjm05ahIWYMHEt+cbWCKH9wCm8Y1 mpIQ== X-Gm-Message-State: AO0yUKXfDcl5AV+YQcMIBIWYLKgo53l8deyVkHleN5LSoVGBQx2N87aj XmwErqTtRBwnu6hsiyrNPgYIlXRfWbo= X-Google-Smtp-Source: AK7set94PT/Q7iAywBf0Cb7oTqcOQNbpnm2Aipfhodna7X7JLeaBkzyN1dtmmLsIFyBtm6OTuR62Qw== X-Received: by 2002:a05:600c:46d2:b0:3ed:46e2:85fb with SMTP id q18-20020a05600c46d200b003ed46e285fbmr7951903wmo.33.1679138131106; Sat, 18 Mar 2023 04:15:31 -0700 (PDT) Received: from lucifer.home (host86-146-209-214.range86-146.btcentralplus.com. [86.146.209.214]) by smtp.googlemail.com with ESMTPSA id n23-20020a1c7217000000b003eb68bb61c8sm4849965wmc.3.2023.03.18.04.15.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Mar 2023 04:15:30 -0700 (PDT) From: Lorenzo Stoakes To: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrew Morton Cc: David Hildenbrand , Matthew Wilcox , Vlastimil Babka , "Liam R . Howlett" , maple-tree@lists.infradead.org, Lorenzo Stoakes Subject: [PATCH 3/4] mm/mmap/vma_merge: extend invariants, avoid invalid res, vma Date: Sat, 18 Mar 2023 11:13:20 +0000 Message-Id: <0b7d3e6c770a2bdc2c307ddde268561135ef3395.1679137163.git.lstoakes@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: References: MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: BE7F4140007 X-Stat-Signature: 6ec4zr1uodwnnkiu8rg3j5my5kwsxqnc X-HE-Tag: 1679138132-621686 X-HE-Meta: U2FsdGVkX1/fWnP/XkW+hrl1C7rGkK+TxmJz4+Yrw6SgPc2/fUD388F6cQl/U9F6oCqu//nK4e/bU+nXQKy5oB7+2HKuZ1+EHwWpH5HuaFFZp/sZXXrW5yx+SOYl6LHenv1xyEVr/pfXbuqXG23mCy9ansvGrY+Y5ihiAJcQHkBibJj8QtPkc8FZKvr+6zxdBQGSVJY/ka4icKH5qLuYq0GTzfAaXpkkz9HYpECDYbDgz8VEVGD/gwwv9P4SuNEwc3sPDkPC6FU3gEQRFRV47RGquQDg5LFwH5Ito3uZ1MKygVEU4IQmwtwwE96W/Fher3UrVeejks4TO1gBBfXEu3trYpjCQL1agLsjEzWdTxefdL8cUUkjw08ptFGQ+B8Bg9uBgBiT5Qw3t/Rc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uTnjVGaWZsgOamcFhRI5K5BYBjr/L5Pxle2tc 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: Previously, vma was an uninitialised variable which was only definitely assigned as a result of the logic covering all possible input cases - for it to have remained uninitialised, prev would have to be NULL, and next would _have_ to be mergeable. We now reuse vma to assign curr and next, so to be absolutely explicit, ensure this variable is _always_ assigned, and while we're at it remove the redundant assignment of both res and vma (if prev is NULL then we simply assign to NULL). In addition, we absolutely do rely on addr == curr->vm_start should curr exist, so assert as much. Signed-off-by: Lorenzo Stoakes --- mm/mmap.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/mm/mmap.c b/mm/mmap.c index 66893fc72e03..e60c637f4e49 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -911,7 +911,7 @@ struct vm_area_struct *vma_merge(struct vma_iterator *vmi, struct mm_struct *mm, { pgoff_t pglen = (end - addr) >> PAGE_SHIFT; pgoff_t vma_pgoff; - struct vm_area_struct *curr, *next, *res = NULL; + struct vm_area_struct *curr, *next, *res; struct vm_area_struct *vma, *adjust, *remove, *remove2; int err = -1; bool merge_prev = false; @@ -978,14 +978,18 @@ struct vm_area_struct *vma_merge(struct vma_iterator *vmi, struct mm_struct *mm, else next = NULL; - /* verify some invariant that must be enforced by the caller */ + /* + * By default, we return prev. Cases 3, 4, 8 will instead return next + * and cases 3, 8 will also update vma to point at next. + */ + res = vma = prev; + + /* Verify some invariant that must be enforced by the caller. */ VM_WARN_ON(prev && addr <= prev->vm_start); - VM_WARN_ON(curr && end > curr->vm_end); + VM_WARN_ON(curr && (addr != curr->vm_start || end > curr->vm_end)); VM_WARN_ON(addr >= end); if (prev) { - res = prev; - vma = prev; vma_start = prev->vm_start; vma_pgoff = prev->vm_pgoff; /* Can we merge the predecessor? */ @@ -996,6 +1000,7 @@ struct vm_area_struct *vma_merge(struct vma_iterator *vmi, struct mm_struct *mm, vma_prev(vmi); } } + /* Can we merge the successor? */ if (next && mpol_equal(policy, vma_policy(next)) && can_vma_merge_before(next, vm_flags, @@ -1036,6 +1041,10 @@ struct vm_area_struct *vma_merge(struct vma_iterator *vmi, struct mm_struct *mm, adj_start = -(prev->vm_end - addr); err = dup_anon_vma(next, prev); } else { + /* + * Note that cases 3 and 8 are the ONLY ones where prev + * is permitted to be (but is not necessarily) NULL. + */ vma = next; /* case 3 */ vma_start = addr; vma_end = next->vm_end;