Received: by 2002:a05:7412:5112:b0:fa:6e18:a558 with SMTP id fm18csp697158rdb; Tue, 23 Jan 2024 11:55:17 -0800 (PST) X-Google-Smtp-Source: AGHT+IEOIvbwwyD4DNECaDCLrzZ+al+ty4lfyOAuEA4Zvy0Fp82Ig2qJPCCvU6aPApLpndFZG/Bg X-Received: by 2002:a17:907:a642:b0:a31:4ff:f324 with SMTP id vu2-20020a170907a64200b00a3104fff324mr226915ejc.86.1706039717267; Tue, 23 Jan 2024 11:55:17 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706039717; cv=pass; d=google.com; s=arc-20160816; b=hhCCs+EshpDiy3m3QYu28wkkMagTSd+I7qDq17+TdBs0c0c1V2VJ+fiMBTVRJ4B4Gw tGyggn2R0do76ky+6cITdUTTEloAqafQ4pr5c3b723m8Y6Xe9QlDEHulo7k4Nw8aCCTJ wQsGrJcR5D8lUSLBd/yfUcFXcWshRRS/VXCPkCGytiHwS9azIPHHPr4qnqB3B/XOSW0b 9/BXo5w2yTfgDGfiUUR5CiT/BD5Ar8oLV/gbrNEwEdHI1ABf0A5LQWwb3OME5eyIIPLW wBQq100TH7tGEXcYtQ9CDCDjAMIGQuPfeEBgN0c2Gb3fw4JBhFELcBqzjRTA0eENUDOX YMiA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-unsubscribe:list-subscribe:list-id:precedence:message-id:date :subject:cc:to:from:dkim-signature; bh=4cvj7c2w+z41DgBXLGIzhGP2KC9m8gbIV2h/6Xm2E7c=; fh=z2lHFCem0SFICYkcDgRpW7miq+wojjaskGnH7cmfo7E=; b=wkvljKeieOY0QK6UyGj6dBkQ4Ag7fyBqCEMglye2IbZh1E/CGv4T1OersUyhiQy7Po 4R0Gj2l0tvWPssYsPoQEVbBy6erTnVotbR7DY6mCRWo+lLAaq/bIjj8K45ArKTntXb1Q +IFZLUB1pSeOTd2CFcLwJLnMFNSdahyhu2SMKB2LcOOhyEvMDbwxNfIR147/ID/d3/7t 2Y/4lzA63/RfRemYzlO3gfROcsxX8b3qdeQDBjB5r01P6WdALMNbbKtc66bE2F378WXv EZd9E0RL9OH/H0SBx1gocYXb+MRkVYR6mkAPoEROCzbEcWC847nYsuJ9/HUjIaPuE5A5 mfIQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2023-11-20 header.b="I11P/kwN"; arc=pass (i=1 spf=pass spfdomain=oracle.com dkim=pass dkdomain=oracle.com dmarc=pass fromdomain=oracle.com); spf=pass (google.com: domain of linux-kernel+bounces-35997-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-35997-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id u20-20020a170906b11400b00a26ac400698si11562893ejy.17.2024.01.23.11.55.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 23 Jan 2024 11:55:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-35997-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@oracle.com header.s=corp-2023-11-20 header.b="I11P/kwN"; arc=pass (i=1 spf=pass spfdomain=oracle.com dkim=pass dkdomain=oracle.com dmarc=pass fromdomain=oracle.com); spf=pass (google.com: domain of linux-kernel+bounces-35997-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-35997-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oracle.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 0E4CA1F22257 for ; Tue, 23 Jan 2024 19:55:17 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B277A85C4C; Tue, 23 Jan 2024 19:54:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="I11P/kwN" Received: from mx0b-00069f02.pphosted.com (mx0b-00069f02.pphosted.com [205.220.177.32]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 86EFC82D6E for ; Tue, 23 Jan 2024 19:54:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.177.32 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706039697; cv=none; b=XOOWWdCYYu5f7R17jlBfSknGT6DXmOhrkTU5r+jvflnTIcJjDu3wimbPtj3qbhn9rhArPG6BAkMi3uN/E6iXGQy1rqEimS0oW4GOl+XnXoRwo3aXfn+UoWhXiS8qMEv3WAoqoJzCEzFR5vR4Y9kzDKyc1HdAzIaFqBdboq07J4Y= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706039697; c=relaxed/simple; bh=9IEGG6PLhGqtk7d3upqqFRh5vj6GY8bnCuD2LwJUNsk=; h=From:To:Cc:Subject:Date:Message-Id; b=GrfmDIq0X9SiWiutcfCxP9qfoYjsD03D6DRoEY7MJGmFp5MyGh3gjItFgUxbWBWU+6jC7NqV7STss6yOwm7qmO1RNH3e3XtSxtJgC1PdsXUv7/+mUpdvtsMe4rZoj60rGszQ4Dz3F3MnNK8e3kozWwwRDoSZ5CiUjnDK8Fui6Vc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=oracle.com; spf=pass smtp.mailfrom=oracle.com; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b=I11P/kwN; arc=none smtp.client-ip=205.220.177.32 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=oracle.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oracle.com Received: from pps.filterd (m0246630.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 40NGRDpM010751; Tue, 23 Jan 2024 19:54:48 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id; s=corp-2023-11-20; bh=4cvj7c2w+z41DgBXLGIzhGP2KC9m8gbIV2h/6Xm2E7c=; b=I11P/kwN3xlIMVsze4r4AjQR7rnVlK2rgaOneJe0CJFtIjNLw3D7mX24cwJKAQL1xqJw Yi3fXY7s8tRbr2HZAoL0NfpoaP2oNjL8VRHykKlPRSyi2L7RzyaV6hhU61JmytGcyPci /K2Ay+8DExnSAxe5zBtCgJW8WRe+nbLnUi+esvwPYiJg/+qTAF2yoHkKQtRsxIMm2o3o Q75g34T/gIdb8qIDZP1MtBJfuMW5JdxQheFA3SvLqXg7Im2AQH8JboxAszGsKESi4qbp lllT7Fbei66C9trjJOYD4FgnAeu5j8Mvkjyk41dUftM0IeCVFZjkSq2bdA73RQ4w86Bk Uw== Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.appoci.oracle.com [130.35.103.27]) by mx0b-00069f02.pphosted.com (PPS) with ESMTPS id 3vr7cxy5t9-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 23 Jan 2024 19:54:48 +0000 Received: from pps.filterd (iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (8.17.1.19/8.17.1.19) with ESMTP id 40NJGoJm001953; Tue, 23 Jan 2024 19:54:48 GMT Received: from pps.reinject (localhost [127.0.0.1]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3vs322uy92-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA256 bits=128 verify=NO); Tue, 23 Jan 2024 19:54:48 +0000 Received: from iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 40NJsliO012746; Tue, 23 Jan 2024 19:54:47 GMT Received: from pp-thinkcentre-m82.us.oracle.com (dhcp-10-132-95-245.usdhcp.oraclecorp.com [10.132.95.245]) by iadpaimrmta03.imrmtpd1.prodappiadaev1.oraclevcn.com (PPS) with ESMTPS id 3vs322uy3m-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA256 bits=128 verify=NO); Tue, 23 Jan 2024 19:54:47 +0000 From: Prakash Sangappa To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: muchun.song@linux.dev, mike.kravetz@oracle.com, akpm@linux-foundation.org Subject: [PATCH v3] Hugetlb pages should not be reserved by shmat() if SHM_NORESERVE Date: Tue, 23 Jan 2024 12:04:42 -0800 Message-Id: <1706040282-12388-1-git-send-email-prakash.sangappa@oracle.com> X-Mailer: git-send-email 2.7.4 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1011,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2024-01-23_11,2024-01-23_02,2023-05-22_02 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 adultscore=0 suspectscore=0 phishscore=0 malwarescore=0 mlxscore=0 mlxlogscore=999 spamscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2311290000 definitions=main-2401230148 X-Proofpoint-ORIG-GUID: PbxVhyfXH_cXUs6-8xQQ0Hu3MOa1QFGZ X-Proofpoint-GUID: PbxVhyfXH_cXUs6-8xQQ0Hu3MOa1QFGZ Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: For shared memory of type SHM_HUGETLB, hugetlb pages are reserved in shmget() call. If SHM_NORESERVE flags is specified then the hugetlb pages are not reserved. However when the shared memory is attached with the shmat() call the hugetlb pages are getting reserved incorrectly for SHM_HUGETLB shared memory created with SHM_NORESERVE which is a bug. ------------------------------- Following test shows the issue. $cat shmhtb.c int main() { int shmflags = 0660 | IPC_CREAT | SHM_HUGETLB | SHM_NORESERVE; int shmid; shmid = shmget(SKEY, SHMSZ, shmflags); if (shmid < 0) { printf("shmat: shmget() failed, %d\n", errno); return 1; } printf("After shmget()\n"); system("cat /proc/meminfo | grep -i hugepages_"); shmat(shmid, NULL, 0); printf("\nAfter shmat()\n"); system("cat /proc/meminfo | grep -i hugepages_"); shmctl(shmid, IPC_RMID, NULL); return 0; } #sysctl -w vm.nr_hugepages=20 #./shmhtb After shmget() HugePages_Total: 20 HugePages_Free: 20 HugePages_Rsvd: 0 HugePages_Surp: 0 After shmat() HugePages_Total: 20 HugePages_Free: 20 HugePages_Rsvd: 5 <-- HugePages_Surp: 0 -------------------------------- Fix is to ensure that hugetlb pages are not reserved for SHM_HUGETLB shared memory in the shmat() call. Signed-off-by: Prakash Sangappa --- v2: Modifed fix to call hugetlb_reserve_pages() with VM_NORESERVE instead as per vma lock is allocated in hugetlb_reserve_pages(). v3: Updated change log to describe user visible effect of the bug with a test case, as suggested by Andrew Morton. fs/hugetlbfs/inode.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/fs/hugetlbfs/inode.c b/fs/hugetlbfs/inode.c index f757d4f..40b12b0 100644 --- a/fs/hugetlbfs/inode.c +++ b/fs/hugetlbfs/inode.c @@ -100,6 +100,7 @@ static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma) loff_t len, vma_len; int ret; struct hstate *h = hstate_file(file); + vm_flags_t vm_flags; /* * vma address alignment (but not the pgoff alignment) has @@ -141,10 +142,20 @@ static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma) file_accessed(file); ret = -ENOMEM; + + vm_flags = vma->vm_flags; + /* + * for SHM_HUGETLB, the pages are reserved in the shmget() call so skip + * reserving here. Note: only for SHM hugetlbfs file, the inode + * flag S_PRIVATE is set. + */ + if (inode->i_flags & S_PRIVATE) + vm_flags |= VM_NORESERVE; + if (!hugetlb_reserve_pages(inode, vma->vm_pgoff >> huge_page_order(h), len >> huge_page_shift(h), vma, - vma->vm_flags)) + vm_flags)) goto out; ret = 0; -- 2.7.4