From patchwork Sat Dec 24 08:20:34 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zach O'Keefe X-Patchwork-Id: 13081311 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 AB415C4332F for ; Sat, 24 Dec 2022 08:20:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1301294000B; Sat, 24 Dec 2022 03:20:41 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0E03F94000A; Sat, 24 Dec 2022 03:20:41 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EE97594000B; Sat, 24 Dec 2022 03:20:40 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id DF73B94000A for ; Sat, 24 Dec 2022 03:20:40 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id B1A67802A9 for ; Sat, 24 Dec 2022 08:20:40 +0000 (UTC) X-FDA: 80276503440.21.4A76A07 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.202]) by imf27.hostedemail.com (Postfix) with ESMTP id 2E76F40006 for ; Sat, 24 Dec 2022 08:20:38 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=O2Rgjytv; spf=pass (imf27.hostedemail.com: domain of 3VbamYwcKCIA3soiijiksskpi.gsqpmry1-qqozego.svk@flex--zokeefe.bounces.google.com designates 209.85.215.202 as permitted sender) smtp.mailfrom=3VbamYwcKCIA3soiijiksskpi.gsqpmry1-qqozego.svk@flex--zokeefe.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=1671870039; 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=ihTDH6jZHdlVGdscYLo+1J1fOC8tjCOiHkEMZL35An0=; b=y/FytUjuot4Vw/K9uPSJxrztDCeRJSdc1E2WOelQjXypDaMDDU1Wh64hxnFIvPkaocWrgF asQ0l5bIx+7FRF0X4LE5YrjC15266E7oZSXcA0vZFAov2XP1s+BAfMylneOp4+PmfbwVKQ X4wYk9iFQNT958Nw8zBanN0KU/xSuBY= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=O2Rgjytv; spf=pass (imf27.hostedemail.com: domain of 3VbamYwcKCIA3soiijiksskpi.gsqpmry1-qqozego.svk@flex--zokeefe.bounces.google.com designates 209.85.215.202 as permitted sender) smtp.mailfrom=3VbamYwcKCIA3soiijiksskpi.gsqpmry1-qqozego.svk@flex--zokeefe.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1671870039; a=rsa-sha256; cv=none; b=DX5/IWaR7Nl5Kjpf/JGhNNJ7Ggd0E5A3nRbkDNzAOPkKlnJkNuqxKqYoPh5iGUh4HLnDk4 8wqQ/hbiU0cSqV4xpB00ErszC2n0KRAKE2d/QEii5yqhQGutvLYBBSNPUfQhUQss4A66LM HVZB69YaLD9B4OzKb6IYMjI8BrUuMBs= Received: by mail-pg1-f202.google.com with SMTP id s16-20020a632c10000000b0047084b16f23so3486236pgs.7 for ; Sat, 24 Dec 2022 00:20:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=ihTDH6jZHdlVGdscYLo+1J1fOC8tjCOiHkEMZL35An0=; b=O2RgjytvDPz+6g9CFcczLL248lQ0CKGMUUPXSrAXNh49DWEMFH6BB2Lu/8S/m1Kd7R Bbsz01ZiRT223Syp1Eh8kai1+ObucV/135HjfhH/GAAJY7X6m0MNIIp+V8920RBGR8RY ft42nde+5+lSZI87XCDPY+KcgYaQE4nB4d1mZWFmGojxerVBeU6Wi9EA+5lLrc0hezdj rTwNYEsX9FdvJGxM1T5juuxMYPYcEZ4eDajArurOQ2qOcm79cw1AWS9I0uiItkwStEHd aYL4JlQ+/BpySpoV/dNjsTPOdw2ifprcV9CU3S2243eAiX/YA4qE6bWLcOQhevfRonxY BV3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=ihTDH6jZHdlVGdscYLo+1J1fOC8tjCOiHkEMZL35An0=; b=ZZXQAP5t5YJH1erWoKUirp/paGxVFeqzUIBygSGGbsCEYyfNkgu9RsJacYoFNDptX6 zWYaDLUiQaZA8InOmOKDvP0l5QyBEtFGcNU8itETdoca8/z5NU460oeRUeuVnDZo0kMD ZhaKf8HYyhhSibjxlNTiItb1kk9IvPuYeQE7DxMkNQtMsLPSsPnijKqjLUjVGgUb1Hg9 8nKKHRf8rLTLjBxe/bp+LcHE7hfHpyaNJ1K/fwNNpE9FqE/cksmC0hYUylLLEQJ4XO5q PpUqQKq/dUuyAKpD77culDnsF8219fAukgQQ9pa1P/QOYUX7GcTfEOuRLDp4++AiExF9 c/1g== X-Gm-Message-State: AFqh2koD30VcCOnAqeJOq0ifeehIqq5O5c6PWkkwSaOSwLnqYQ5SGroU o6eK3qgu4bMwtvQhr+0KnZvN5rW+VMen39updsYM9xlINvE0zd6jnDR0DT3KYv9HdPpLe+T3Num ys3XaOD2sjn+a+TivHrNf2cYLz07Q8GqaH16yxLWgUfvcml+jA1S29+gEGOQ= X-Google-Smtp-Source: AMrXdXvdFmFXW5Qb97N0QrfYVlOEyfoUEa8iHxd87AV61GPshlomzetW6kZfirXu9H1n5bGTnaw2lWu62OEO X-Received: from zokeefe3.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:1b6]) (user=zokeefe job=sendgmr) by 2002:a17:90b:3015:b0:219:c8d5:27d7 with SMTP id hg21-20020a17090b301500b00219c8d527d7mr1200309pjb.141.1671870037941; Sat, 24 Dec 2022 00:20:37 -0800 (PST) Date: Sat, 24 Dec 2022 00:20:34 -0800 Mime-Version: 1.0 X-Mailer: git-send-email 2.39.0.314.g84b9a713c41-goog Message-ID: <20221224082035.3197140-1-zokeefe@google.com> Subject: [PATCH v3 1/2] mm/MADV_COLLAPSE: don't expand collapse when vm_end is past requested end From: "Zach O'Keefe" To: linux-mm@kvack.org Cc: stable@vger.kernel.org, linux-kernel@vger.kernel.org, Andrew Morton , Hugh Dickins , Yang Shi , "Zach O'Keefe" X-Stat-Signature: sdfj745ot9kjks5dfqtdmppk6opuj1su X-Rspam-User: X-Rspamd-Queue-Id: 2E76F40006 X-Rspamd-Server: rspam06 X-HE-Tag: 1671870038-32848 X-HE-Meta: U2FsdGVkX181wjQoxhycyBnWlqb4EUAT7mqRYRF3qdjLshFG6EbWK/SsrQ0ETEfsz7BiXbdOAKYsmmLLFYpQxBg/MHjE5o47LFigE6TFwjQfkwyDOPWjPkTDifu3pmnnQEoM5nxPctauJtdsJLivu3WCYdLEzy13Kjc1zeylw0gjJRFL3apjFmEV4uN1LfSEFxtFStiy0Mk3ZDOBYpXfeJZtaXE+lehlErxfQuEHmNbilN4Zh9YxtnqhSPrnD52IfZSeGMc3j/6AGcl/Blutiqz2LrWO0fYa45PMF+mfVqvQdso/FEvMBtffOYLnr+y7XatVhkMdrpdfpuFi/Hu8MOJsedrj/ahWeuE4ZuBubNxfAV9L8U3TqczzOyZCu9HgNlsz8a72W9G5gQKTdZdNWMk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ence: bulk X-Loop: owner-majordomo@kvack.org List-ID: MADV_COLLAPSE acts on one hugepage-aligned/sized region at a time, until it has collapsed all eligible memory contained within the bounds supplied by the user. At the top of each hugepage iteration we (re)lock mmap_lock and (re)validate the VMA for eligibility and update variables that might have changed while mmap_lock was dropped. One thing that might occur, is that the VMA could be resized, and as such, we refetch vma->vm_end to make sure we don't collapse past the end of the VMA's new end. However, it's possible that when refetching vma>vm_end that we expand the region acted on by MADV_COLLAPSE if vma->vm_end is greater than size+len supplied by the user. The consequence here is that we may attempt to collapse more memory than requested, possibly yielding either "too much success" or "false failure" user-visible results. An example of the former is if we MADV_COLLAPSE the first 4MiB of a 2TiB mmap()'d file, the incorrect refetch would cause the operation to block for much longer than anticipated as we attempt to collapse the entire TiB region. An example of the latter is that applying MADV_COLLPSE to a 4MiB file mapped to the start of a 6MiB VMA will successfully collapse the first 4MiB, then incorrectly attempt to collapse the last hugepage-aligned/sized region -- fail (since readahead/page cache lookup will fail) -- and report a failure to the user. Don't expand the acted-on region when refetching vma->vm_end. Fixes: 4d24de9425f7 ("mm: MADV_COLLAPSE: refetch vm_end after reacquiring mmap_lock") Reported-by: Hugh Dickins Signed-off-by: Zach O'Keefe Cc: Yang Shi Cc: stable@vger.kernel.org --- v2->v3: Add 'Cc: stable@vger.kernel.org' as per stable-kernel-rules. v1->v2: Updated changelog to make clear what user-visible issues this patch addresses, as well makes the case for backporting (Andrew Morton). While there aren't any stability risks, without this patch there exist trivial examples where MADV_COLLAPSE won't work; as such, this should be backported to stable 6.1.X to make MADV_COLLAPSE dependable in such cases. v1: https://lore.kernel.org/linux-mm/CAAa6QmRx_b2UCJWE2XZ3=3c3-_N3R4cDGX6Wm4OT7qhFC6U_SQ@mail.gmail.com/T/#m6c91da3cdbd9b1d1ebb29d415962deb158a2c658 --- mm/khugepaged.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 5cb401aa2b9d..b4d2ec0a94ed 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -2649,7 +2649,7 @@ int madvise_collapse(struct vm_area_struct *vma, struct vm_area_struct **prev, goto out_nolock; } - hend = vma->vm_end & HPAGE_PMD_MASK; + hend = min(hend, vma->vm_end & HPAGE_PMD_MASK); } mmap_assert_locked(mm); memset(cc->node_load, 0, sizeof(cc->node_load));