From patchwork Fri Oct 21 23:28:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rik van Riel X-Patchwork-Id: 13015580 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 D456CC433FE for ; Fri, 21 Oct 2022 23:28:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4406D8E0002; Fri, 21 Oct 2022 19:28:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3F0778E0001; Fri, 21 Oct 2022 19:28:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2DFB38E0002; Fri, 21 Oct 2022 19:28:10 -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 1A96A8E0001 for ; Fri, 21 Oct 2022 19:28:10 -0400 (EDT) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id E5D0BC0286 for ; Fri, 21 Oct 2022 23:28:09 +0000 (UTC) X-FDA: 80046547098.02.E758D0B Received: from shelob.surriel.com (shelob.surriel.com [96.67.55.147]) by imf29.hostedemail.com (Postfix) with ESMTP id 763D9120014 for ; Fri, 21 Oct 2022 23:28:08 +0000 (UTC) Received: from [2603:3005:d05:2b00:6e0b:84ff:fee2:98bb] (helo=imladris.surriel.com) by shelob.surriel.com with esmtpsa (TLS1.2) tls TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1om1Qr-0000ST-2D; Fri, 21 Oct 2022 19:28:05 -0400 Date: Fri, 21 Oct 2022 19:28:05 -0400 From: Rik van Riel To: linux-kernel@vger.kernel.org Cc: linux-mm@kvack.org, kernel-team@meta.com, stable@kernel.org, Mike Kravetz , Andrew Morton , David Hildenbrand Subject: [PATCH v2] mm,madvise,hugetlb: fix unexpected data loss with MADV_DONTNEED on hugetlbfs Message-ID: <20221021192805.366ad573@imladris.surriel.com> X-Mailer: Claws Mail 4.1.0 (GTK 3.24.34; x86_64-redhat-linux-gnu) MIME-Version: 1.0 ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=none; spf=none (imf29.hostedemail.com: domain of riel@shelob.surriel.com has no SPF policy when checking 96.67.55.147) smtp.mailfrom=riel@shelob.surriel.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1666394888; a=rsa-sha256; cv=none; b=Oi0KzOJnuXTgL2FqKPQcsbssru8KV+s43W08kcm4e7dkT/PYEWxstSzhLP1RZJSyxMH7OT V5twfvjm8TMoJt7K/9/fe6+PlbRpZK+as/YwvOkf4vppk0KY/lXYMZbo49kyQZUH68wiHl 0pnYJlCr9jBav26xTQ/wyiVRRe2OIbk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1666394888; h=from:from:sender: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: references; bh=NNIjsxsXvHppPN2twb3s9Oh/SYdaL1myuKZJ0aPS8/c=; b=Vekr6kZpK0XxKPs5xgjv4+GFMtCSclKiKRqsgkkHGyBMsFzb7q7Cbxj0rusdKjbUvnxP++ 5+RCTBRzuPJnx2fG8SFySrXQIBLVMJf77bER0Lo2Sjm2p5iNXPhlIqwOkeJZgiEisnYEHW PoqLF6o89pCR6QRJKB0JJBzmHoqlZyU= Authentication-Results: imf29.hostedemail.com; dkim=none; spf=none (imf29.hostedemail.com: domain of riel@shelob.surriel.com has no SPF policy when checking 96.67.55.147) smtp.mailfrom=riel@shelob.surriel.com; dmarc=none X-Rspam-User: X-Rspamd-Server: rspam10 X-Stat-Signature: sdnetx3hfn1jbogxk3ctbr6qz7wa39y9 X-Rspamd-Queue-Id: 763D9120014 X-HE-Tag: 1666394888-224106 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: A common use case for hugetlbfs is for the application to create memory pools backed by huge pages, which then get handed over to some malloc library (eg. jemalloc) for further management. That malloc library may be doing MADV_DONTNEED calls on memory that is no longer needed, expecting those calls to happen on PAGE_SIZE boundaries. However, currently the MADV_DONTNEED code rounds up any such requests to HPAGE_PMD_SIZE boundaries. This leads to undesired outcomes when jemalloc expects a 4kB MADV_DONTNEED, but 2MB of memory get zeroed out, instead. Use of pre-built shared libraries means that user code does not always know the page size of every memory arena in use. Avoid unexpected data loss with MADV_DONTNEED by rounding up only to PAGE_SIZE (in do_madvise), and rounding down to huge page granularity. That way programs will only get as much memory zeroed out as they requested. Cc: Mike Kravetz Cc: David Hildenbrand Cc: Andrew Morton Cc: stable@kernel.org Fixes: 90e7e7f5ef3f ("mm: enable MADV_DONTNEED for hugetlb mappings") Reviewed-by: Mike Kravetz --- v2: split out the most urgent fix for stable backports mm/madvise.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/mm/madvise.c b/mm/madvise.c index 2baa93ca2310..c7105ec6d08c 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -813,7 +813,14 @@ static bool madvise_dontneed_free_valid_vma(struct vm_area_struct *vma, if (start & ~huge_page_mask(hstate_vma(vma))) return false; - *end = ALIGN(*end, huge_page_size(hstate_vma(vma))); + /* + * Madvise callers expect the length to be rounded up to PAGE_SIZE + * boundaries, and may be unaware that this VMA uses huge pages. + * Avoid unexpected data loss by rounding down the number of + * huge pages freed. + */ + *end = ALIGN_DOWN(*end, huge_page_size(hstate_vma(vma))); + return true; } @@ -828,6 +835,9 @@ static long madvise_dontneed_free(struct vm_area_struct *vma, if (!madvise_dontneed_free_valid_vma(vma, start, &end, behavior)) return -EINVAL; + if (start == end) + return 0; + if (!userfaultfd_remove(vma, start, end)) { *prev = NULL; /* mmap_lock has been dropped, prev is stale */