From patchwork Tue Jul 9 09:21:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Donet Tom X-Patchwork-Id: 13727527 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 8DDD8C2BD09 for ; Tue, 9 Jul 2024 09:21:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 14A0D6B0099; Tue, 9 Jul 2024 05:21:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0FBCC6B009A; Tue, 9 Jul 2024 05:21:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F03F46B009B; Tue, 9 Jul 2024 05:21:54 -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 D37876B0099 for ; Tue, 9 Jul 2024 05:21:54 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 78FA11217F6 for ; Tue, 9 Jul 2024 09:21:54 +0000 (UTC) X-FDA: 82319672148.18.2A1A662 Received: from mx0b-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) by imf06.hostedemail.com (Postfix) with ESMTP id 50CB0180008 for ; Tue, 9 Jul 2024 09:21:51 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=ibm.com header.s=pp1 header.b=ADJ77naX; spf=pass (imf06.hostedemail.com: domain of donettom@linux.ibm.com designates 148.163.158.5 as permitted sender) smtp.mailfrom=donettom@linux.ibm.com; dmarc=pass (policy=none) header.from=ibm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1720516882; 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:references:dkim-signature; bh=VAu8nuSyKpLacLsQUVBecgg5k9Mqzo3eKlp3op6poZg=; b=JFruqlMTgREGxcXpIaAHc4G/51uTLgjsy7CUG2foeWckOEKOnJjYFhoCkU8oh1bG6OLAP1 cSpYfiwZ5SBrwNPw1Iq0o/1asAXtAlUzhGBkt4wVYaWgobr8T2ahny8d5/X5BmLKGj0b50 BkxqyBb1VSpoIoGtPrhKEsvIhY44DN8= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1720516882; a=rsa-sha256; cv=none; b=tTbjk90gH+QSqvW4GPpY4a38jBNrEJH9GwuVWTdViuDgC9l+J/+AMw2lvTOK5bLRtQ/zUN iJkHx7PqvKpq08px79iylusa34UM8nrHCchwZLUePwIXznOZJOYflOvROrr29sIdd42SZ0 7l57ndK6bAfOYYuBvDPpTpaG3ufRAdE= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=ibm.com header.s=pp1 header.b=ADJ77naX; spf=pass (imf06.hostedemail.com: domain of donettom@linux.ibm.com designates 148.163.158.5 as permitted sender) smtp.mailfrom=donettom@linux.ibm.com; dmarc=pass (policy=none) header.from=ibm.com Received: from pps.filterd (m0356516.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 4697vMpK024493; Tue, 9 Jul 2024 09:21:34 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from :to:cc:subject:date:message-id:content-transfer-encoding :mime-version; s=pp1; bh=VAu8nuSyKpLacLsQUVBecgg5k9Mqzo3eKlp3op6 poZg=; b=ADJ77naXvcUzRAu4i3KnasFfUhTEG55x5/gya9kq66qEiILR7fumpH9 tDncklx40bX5br0/JBziddJ9zEX/PoMXrB9oTydbne3ZS1r4nxq73Zew7HgBVHUv WpLkPUuowIHxJ85mktEOVDNmxmhIZn8P6KpnQ+45wJgHsdorKxBYGS0qFQ7yGITk oaOFaTl/kefxK2ewto34hFTecYq+S+OSM/G6u7omrwFnvFfYggE+E5LEwhUUN8X5 6Lxn4bIdQXx/UR2e0+hoGFCMtj0d1rgqRvxs4Giv2orufQg472o5xBLbRU/+j2vC AvjEpOCscRYRa67agDhNkNgQmQY1lYw== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 408ws38p9j-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 09 Jul 2024 09:21:34 +0000 (GMT) Received: from m0356516.ppops.net (m0356516.ppops.net [127.0.0.1]) by pps.reinject (8.18.0.8/8.18.0.8) with ESMTP id 4699LXEl020805; Tue, 9 Jul 2024 09:21:33 GMT Received: from ppma23.wdc07v.mail.ibm.com (5d.69.3da9.ip4.static.sl-reverse.com [169.61.105.93]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 408ws38p9f-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 09 Jul 2024 09:21:33 +0000 (GMT) Received: from pps.filterd (ppma23.wdc07v.mail.ibm.com [127.0.0.1]) by ppma23.wdc07v.mail.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 4698tVce024583; Tue, 9 Jul 2024 09:21:32 GMT Received: from smtprelay02.fra02v.mail.ibm.com ([9.218.2.226]) by ppma23.wdc07v.mail.ibm.com (PPS) with ESMTPS id 407hrmkjpx-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 09 Jul 2024 09:21:32 +0000 Received: from smtpav01.fra02v.mail.ibm.com (smtpav01.fra02v.mail.ibm.com [10.20.54.100]) by smtprelay02.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 4699LS0r42140092 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 9 Jul 2024 09:21:30 GMT Received: from smtpav01.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id AAC3220040; Tue, 9 Jul 2024 09:21:28 +0000 (GMT) Received: from smtpav01.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 800A020043; Tue, 9 Jul 2024 09:21:26 +0000 (GMT) Received: from ltczz402-lp1.aus.stglabs.ibm.com (unknown [9.40.194.31]) by smtpav01.fra02v.mail.ibm.com (Postfix) with ESMTP; Tue, 9 Jul 2024 09:21:26 +0000 (GMT) From: Donet Tom To: Andrew Morton , Muchun Song , "Kirill A . Shutemov" Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, Ritesh Harjani , Mike Rapoport , David Hildenbrand , Matthew Wilcox , Tony Battersby , "Aneesh Kumar K . V" , Nicholas Piggin , Alexei Starovoitov , Andy Lutomirski , Donet Tom Subject: [PATCH v2] fs/hugetlbfs/inode.c: Ensure generic_hugetlb_get_unmapped_area() returns higher address than mmap_min_addr Date: Tue, 9 Jul 2024 04:21:22 -0500 Message-ID: <20240709092122.41232-1-donettom@linux.ibm.com> X-Mailer: git-send-email 2.43.5 X-TM-AS-GCONF: 00 X-Proofpoint-GUID: oaUkanhYWHG3dfuPoU_w4oO8JCSI7bbG X-Proofpoint-ORIG-GUID: LNKnn6UQNhxNm9BSsiI72LSIydZX6VL0 X-Proofpoint-UnRewURL: 0 URL was un-rewritten MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.680,FMLib:17.12.28.16 definitions=2024-07-08_15,2024-07-08_01,2024-05-17_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 malwarescore=0 mlxlogscore=999 clxscore=1015 impostorscore=0 lowpriorityscore=0 spamscore=0 priorityscore=1501 bulkscore=0 mlxscore=0 suspectscore=0 phishscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2406140001 definitions=main-2407090059 X-Rspamd-Queue-Id: 50CB0180008 X-Stat-Signature: 5jifkjmpuqfe6tkbgr8uxuuzkrqj9jrr X-Rspamd-Server: rspam09 X-Rspam-User: X-HE-Tag: 1720516911-528155 X-HE-Meta: U2FsdGVkX1+CPVet1XbinzyIXZ3f7mCjeWRCwSjL3vaxK3AUiSTE44LNtUhpwALOF9k+TeImcGqP6T6bESga6xvlhhF9zNmpFCja+gGnzEMlgDHGoihN6OwYPV4PEWA3znjSw71LC32brDKTLju0ZPNjH8GGfIol+Z0dkX24YfPIcfGQINtOoluqN8GGcIJELE0uy28e+i6MhkMV+TMPLjEDEW1O5WG+Ybiu0Z1SnCMJL/EV0QKDanZabQ8C4obaPinxfHQIDC/B2mnQ3BiUr3HzUx2Z2vppP0bqyUneLd5eja8fKOTMPWqZaiUvmJJNojb7SpSCLMrC7E+iO2RchLXu6AXAQq7RvYqORfzD8eODE0VxuZ022H6omtjEZ7mHN/eVHMzaNbZhztsaPUBnJ5neXrba47/wdfe9JfzSjsTEzOa4JJ/Lipboaj7EfyMGsDPG/FZ1TYZoARcvS3CgDcr9JdhHVh3my9b0Cr4XuMZD8Cmk/TYWaRljA9CEYZ5LRvDqx5rfKsmrKr8FwjiutQ2rGSivaw2EF9Bqonb8Juoa53piu86d2v8mW3fbE2F9ZFvbO6QyHbS4MGZHZDkQOPlk4Mfhw62dtX7LOA3agOGtlrf+6COlqN60wF/EAgQaANhCtpZM47TxSL6kRuML4ZHErtfdDnpUmSkFg8TrBu047GqP561TusAStMM1C3zEDlwhPHC6lSbYEYSVGZeT+VoS487A7eDbnfAk/1IZstNrZKUfGt6FWKgR9vtul+j1EGbLG/XKqoqGEsMp/LZYFwdEl0BWoAgjVyWK9EvWevxmDjCHvgWmU0n5O0nVq21dPAbmxFkfP+18atpi7ian4HXPB9N/IHydL6+BV1zcwqY4GiQlCRihZaj5Puoi9trjFZwmQLYR86XJ7lDzZy49Kgmj0Ej+wGO3cU+H4M14Q3kDPMCIlyMiHQYJGb/FLwFwJmMxiklzOzzHHuFr/rW mH/Z6nsT mtxsySUrg49WTvko4WRzjNY8KuHDn0YHyg92c2+dA1w80QRiWQ74tgJ0RibuhTHqWLhn+2HAfpxFQjEMVenOgOJhE5UpXEj4Xk9u6moSJ+eH9nW7+A0JyJLu3rQa7pdX4gcbZoOZs1yeF2g9UjZZZiOxyIE63cqWZAiN2W76ulVDJp6Ra0884VLqZ9MtPGQYHX/1bbPM4Fey12h2g5MrOcyUhMYNG+wm5+7jOKcF4GcsQXzhruA5zWbzufKfCzQwSebwxVCu50Ib9No60S8ZgRP06gl8O4Ce55jAq0EwtRdbpIT+5TjHw/EB/WFDTpsPj6aOYlQ7x4XPCARJmbW+3zEp8HPB3NJZxrDnFxCQLJCC7FRE= 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: List-Subscribe: List-Unsubscribe: generic_hugetlb_get_unmapped_area() was returning an address less than mmap_min_addr if the mmap argument addr, after alignment, was less than mmap_min_addr, causing mmap to fail. This is because current generic_hugetlb_get_unmapped_area() code does not take into account mmap_min_addr. This patch ensures that generic_hugetlb_get_unmapped_area() always returns an address that is greater than mmap_min_addr. Additionally, similar to generic_get_unmapped_area(), vm_end_gap() checks are included to ensure that the address is within the limit. How to reproduce ================ #include #include #include #include #define HUGEPAGE_SIZE (16 * 1024 * 1024) int main() { void *addr = mmap((void *)-1, HUGEPAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS | MAP_HUGETLB, -1, 0); if (addr == MAP_FAILED) { perror("mmap"); exit(EXIT_FAILURE); } snprintf((char *)addr, HUGEPAGE_SIZE, "Hello, Huge Pages!"); printf("%s\n", (char *)addr); if (munmap(addr, HUGEPAGE_SIZE) == -1) { perror("munmap"); exit(EXIT_FAILURE); } return 0; } Result without fix ================== # cat /proc/meminfo |grep -i HugePages_Free HugePages_Free: 20 # ./test mmap: Permission denied # Result with fix =============== # cat /proc/meminfo |grep -i HugePages_Free HugePages_Free: 20 # ./test Hello, Huge Pages! # V2: Added vm_end_gap() check. V1: https://lore.kernel.org/all/20240705071150.84972-1-donettom@linux.ibm.com/ Reported-by Pavithra Prakash Signed-off-by: Donet Tom Acked-by: Kirill A. Shutemov --- fs/hugetlbfs/inode.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c index 412f295acebe..cdd8e53ddd19 100644 --- a/fs/hugetlbfs/inode.c +++ b/fs/hugetlbfs/inode.c @@ -222,13 +222,13 @@ generic_hugetlb_get_unmapped_area(struct file *file, unsigned long addr, unsigned long flags) { struct mm_struct *mm = current->mm; - struct vm_area_struct *vma; + struct vm_area_struct *vma, *prev; struct hstate *h = hstate_file(file); const unsigned long mmap_end = arch_get_mmap_end(addr, len, flags); if (len & ~huge_page_mask(h)) return -EINVAL; - if (len > TASK_SIZE) + if (len > mmap_end - mmap_min_addr) return -ENOMEM; if (flags & MAP_FIXED) { @@ -239,9 +239,10 @@ generic_hugetlb_get_unmapped_area(struct file *file, unsigned long addr, if (addr) { addr = ALIGN(addr, huge_page_size(h)); - vma = find_vma(mm, addr); - if (mmap_end - len >= addr && - (!vma || addr + len <= vm_start_gap(vma))) + vma = find_vma_prev(mm, addr, &prev); + if (mmap_end - len >= addr && addr >= mmap_min_addr && + (!vma || addr + len <= vm_start_gap(vma)) && + (!prev || addr >= vm_end_gap(prev))) return addr; }