From patchwork Tue Jun 20 23:57:24 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13286504 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 7F231EB64D7 for ; Tue, 20 Jun 2023 23:57:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E73328D0002; Tue, 20 Jun 2023 19:57:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DFBDA8D0001; Tue, 20 Jun 2023 19:57:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C9C548D0002; Tue, 20 Jun 2023 19:57:32 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id B4CE18D0001 for ; Tue, 20 Jun 2023 19:57:32 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 7B3D11A0646 for ; Tue, 20 Jun 2023 23:57:32 +0000 (UTC) X-FDA: 80924790744.10.3998AF0 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf14.hostedemail.com (Postfix) with ESMTP id C4B22100003 for ; Tue, 20 Jun 2023 23:57:30 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=CSSYpE6o; spf=pass (imf14.hostedemail.com: domain of 36TySZAYKCNYKMJ6F38GG8D6.4GEDAFMP-EECN24C.GJ8@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=36TySZAYKCNYKMJ6F38GG8D6.4GEDAFMP-EECN24C.GJ8@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1687305450; 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: references:dkim-signature; bh=6My5zaxRwCubM/znAlFWMrzsQs0o4/hMLfIyqDwIdCM=; b=xJM0V7l30klqqa16EN7/muq+3JMdY/geNgq7umzhYMMTC0G2Uvi+i+GoA2cO+FhCSLq9Ep 2uG652WyShgg66ZUJiVirnApzthln000/z4jTjnycbzKZg8M2TUyrO79Q08Gx3daRjsT99 DZ3D9/o4SqDYfC3vXoOKkJs/4DBTHrw= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1687305450; a=rsa-sha256; cv=none; b=unfHfWmcrh5Qzd5H9KAzptRC4Dlx+kVw4MS2UH36ikwEKV1dy7RdX8c+t8Y+O+ZVnBe59n RTIrpaXYrbQY+4KDhZ5lKy06SoDBekqHpOE+2sbw+t0FIwbYlYQodNB48m8vjILfbxiBon FET+4GOjyJ9CS+Z8qBR8hMqNkei5x/A= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=CSSYpE6o; spf=pass (imf14.hostedemail.com: domain of 36TySZAYKCNYKMJ6F38GG8D6.4GEDAFMP-EECN24C.GJ8@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=36TySZAYKCNYKMJ6F38GG8D6.4GEDAFMP-EECN24C.GJ8@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-ba81b37d9d2so5587934276.3 for ; Tue, 20 Jun 2023 16:57:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1687305450; x=1689897450; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=6My5zaxRwCubM/znAlFWMrzsQs0o4/hMLfIyqDwIdCM=; b=CSSYpE6o2vAYB+EczjNxHQoW9ZZEIQkPp00UpPzxbzxvXBVI4La9GGnHYy8QOAimev sfzfDqWb0009TEU/RKdrkDPXynJU93p0tuZD9SmZGHcLexKdiOKqZWJHOqcUGvtktcrY 6STtqvnsGmXhMto4nbyL6fX0OZ5Mm8HfxnS/LYv9XTquxzNy8LHbClyPaVrhoICYyZ/2 g7WkUN6m7pcrbGPcMrAUVmt45VALmV9NtE5bNAkafpbx7TM/rk6YC/Ke8TslHhaYfECz 8sS3IXfjPcEJnj01RFYOknIlwJz0+ih21+BBteNcARxJv49L9w0p5ZwII+bz34K/JCZ/ JX1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687305450; x=1689897450; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=6My5zaxRwCubM/znAlFWMrzsQs0o4/hMLfIyqDwIdCM=; b=SCobmXJWNBF0/vF/XQesM6AgB4kp7DGg7UAB6ZiwqjXCUPS/o22QJeiDvgp6/pDsgt Be5RmTR2hw1s0v8C4tpk8GjmU9Vy9ocdYrTC1Cv6TSIs2Dg5Ky7uaSt2TE1fiFFvtP9r WrPcBCK447JMPBE+hdYF57wLTPde7NHLIrtxdoLxOgv3CJ2YhvJJMXEU0nQpyGM0myYW i/XBY5YlZfDTVtGtUA/kCdiEDR8J15ZHJB/0n7zVVZB0+4d7yXAMLRIg6ch8uCP6YdAT FHXzeQp+qr9nBzeRgtwx066Qx/iN75rF6L58XtdXkBuFqjyMi8vLEPlaVKxSry9I1Cyy OFSg== X-Gm-Message-State: AC+VfDxB85zWii0RWBbaMNneTIYpRXzusRC3PajU10EM+gTHtSKT7dK+ cLvjeuvjX6U/8xeyKaGlbj9j1kcfDGk= X-Google-Smtp-Source: ACHHUZ7lmHtfjY/K80Z1E48r7yvyTtsMdx0YbswhxFGrQ9sy28TJt2rAbO44vsYSrvgTnZ1JPXOfT1sdcAc= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:3f57:9854:e19:8906]) (user=surenb job=sendgmr) by 2002:a05:6902:91:b0:ba8:6dc0:cacf with SMTP id h17-20020a056902009100b00ba86dc0cacfmr1731864ybs.12.1687305449890; Tue, 20 Jun 2023 16:57:29 -0700 (PDT) Date: Tue, 20 Jun 2023 16:57:24 -0700 Mime-Version: 1.0 X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog Message-ID: <20230620235726.3873043-1-surenb@google.com> Subject: [PATCH 1/3] mm: change vma_start_read to fail if VMA got detached from under it From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: willy@infradead.org, torvalds@linuxfoundation.org, vegard.nossum@oracle.com, mpe@ellerman.id.au, Liam.Howlett@oracle.com, lrh2000@pku.edu.cn, mgorman@suse.de, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@android.com, surenb@google.com X-Rspamd-Queue-Id: C4B22100003 X-Rspam-User: X-Stat-Signature: b69x7ek6imy6e4zezbo16rqt165xwsgm X-Rspamd-Server: rspam03 X-HE-Tag: 1687305450-89827 X-HE-Meta: U2FsdGVkX182quzMrGta/MEPbcE3G7g6ezzt+avT4V6alW5Z6GAar9tVS4USppYK2J9V88v9E3xbek5aPFS+nEOAuCOWay4qB5C0IBprjC7Pnf9Jk0osaQdjMaz+VamIyHgaoOqTuOVUEbPWCtySBuoti3QhUPxB2b8V1MavjBgyaHETY/TSmZn1R1cMngp6Umn/WsZ26BCVHCiiHUMFuH6NNLIjPNo4g+wyHstDfWSFi8UbJN+ycfedoxWWP5ipgU6pabFIxvKrDzKNAbdZq1CrdNlw6EGYCmcYhNF7+NORyQ5bcCrF9hVZKfizzWcq4LUt4KRHYxTMPScl53fyJMc7Gkcggpa+gOPQzNrV1MAYyKFrHMlpp9ua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q9btI/PyuFteddwcZ0ZntyN3NncQCrmOsgCj1yuGnZKw8C3fBOwq3LFEslXAbgAxxQQCwTAtBwwfbRawQGhNziDOKMpqYmPhmOi5GGhF7SKh3vYlcKgfWxQyZ+OIk7ecrfcBxzp8byBFd7zmDiSKx/FTlw6MCdZ5gEpC9TQhhg9uKWAbo7Og2YKhJ1G6RETQrItxaH7aT9nc6oUpPy1Y2gKENjG3GsB3UKqk1sTPCwtEmjBPE0Hd+4OQ7pzown3hdahhPEvKfkQ== 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: Current implementation of vma_start_read() checks VMA for being locked before taking vma->vm_lock and then checks that again. This mechanism fails to detect a case when the VMA gets write-locked, modified and unlocked after the first check but before the vma->vm_lock is obtained. While this is not strictly a problem (vma_start_read would not produce a false unlocked result), this allows it to successfully lock a VMA which got detached from the VMA tree while vma_start_read was locking it. New condition checks for any change in vma->vm_lock_seq after we obtain vma->vm_lock and will cause vma_start_read() to fail if the above race occurs. Fixes: 5e31275cc997 ("mm: add per-VMA lock and helper functions to control it") Suggested-by: Matthew Wilcox Signed-off-by: Suren Baghdasaryan --- include/linux/mm.h | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 27ce77080c79..8410da79c570 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -639,23 +639,24 @@ static inline void vma_numab_state_free(struct vm_area_struct *vma) {} */ static inline bool vma_start_read(struct vm_area_struct *vma) { - /* Check before locking. A race might cause false locked result. */ - if (vma->vm_lock_seq == READ_ONCE(vma->vm_mm->mm_lock_seq)) + int vm_lock_seq = READ_ONCE(vma->vm_lock_seq); + + /* + * Check if VMA is locked before taking vma->vm_lock. A race or + * mm_lock_seq overflow might cause false locked result. + */ + if (vm_lock_seq == READ_ONCE(vma->vm_mm->mm_lock_seq)) return false; if (unlikely(down_read_trylock(&vma->vm_lock->lock) == 0)) return false; - /* - * Overflow might produce false locked result. - * False unlocked result is impossible because we modify and check - * vma->vm_lock_seq under vma->vm_lock protection and mm->mm_lock_seq - * modification invalidates all existing locks. - */ - if (unlikely(vma->vm_lock_seq == READ_ONCE(vma->vm_mm->mm_lock_seq))) { + /* Fail if VMA was write-locked after we checked it earlier */ + if (unlikely(vm_lock_seq != READ_ONCE(vma->vm_lock_seq))) { up_read(&vma->vm_lock->lock); return false; } + return true; }