Received: by 2002:ab2:710b:0:b0:1ef:a325:1205 with SMTP id z11csp1888256lql; Wed, 13 Mar 2024 10:54:09 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWi/EfzcpEzARLXaDxzNJ58YdWbTo0As32WA8v0psNgYv/CKU8cu5ctXroyShg+M88DN1DOg70jeA9BD7UcTj8TuGzRGXfnY/GF8HcLMg== X-Google-Smtp-Source: AGHT+IG4asMUZERvX2Br0FhTmvSikBq4t2H9HrGMnhqsaK7XrW/q3PPBpFOXJhBBwA2Xp/BYahHq X-Received: by 2002:a05:6358:7419:b0:17e:69d4:ddbb with SMTP id s25-20020a056358741900b0017e69d4ddbbmr885087rwg.14.1710352448843; Wed, 13 Mar 2024 10:54:08 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710352448; cv=pass; d=google.com; s=arc-20160816; b=bwASk7hk16DAdDz1P6Qr1zL11wnZJRrieJx/n3f9rkNB5mnlKefCY9KHQEg190M7Yv pKyLsAr29LqOGM5OeYZInWErANZ4E/Dzv8/AlZEUbFBfIhej56+fBM521VzhN59oUFbe 21iG22U+c7M4ivD/r7tRD5SsHR53supWwuCA6RtOS9G8STpeNpKAA4Sp1JNXYDbiTdCs 4qTBc5K9/4YT8O/Ex3ugkHJd2bLmIVIgdVniDngn0fX1fpSWIuHQZd3k/c1InP+7MJEB oC05djo3OugG/EawY3OZDawSFBtzru0vYv2B5VR0PGuNgF8EiZm+02i6JmLktoieK7ci V+WA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=1+nlWvapj8DoSG0RMEkMd4DmhakMDwHD7ZouVVfdEN8=; fh=yIGCPFmJJnUak5wW421lkB19Xd1EC2cZ2/BsQPiB2NU=; b=s/JFSYadiflJgnaeE2GnwmXZcdahtkG577gDh+9+XaEBzSet/9GGMIB9zUxHN6iGPq PLW7Us2KStwjBuCoHDtDdYIdwlGerUoJ7eNgKJfq3fI3OJCz5eg0olJBb1s1NJroBU5x Dew0fCLI7RboXlUslWvCWoyaOiC45KNN6Ek+o2wXed0zVkHKeVrPpoFhGgXGmW0DQDfN I7KyEbfHe79hbnB/lPjBvd/v2GiQYculyq+8/OksuLkpt+ubjRBCrQQC0dH2IrlqYOzn jrMBwH/CQdz5kxYXxTMt4wUwa6Nz+SBuPdxcwEQK3M6zLmI6uyC7uD7BrHBEJreiZjsp eGMQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=kUM9xHkq; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-102085-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-102085-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id w11-20020a63160b000000b005cdd60a42f5si9498269pgl.838.2024.03.13.10.54.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Mar 2024 10:54:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-102085-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=kUM9xHkq; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-102085-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-102085-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org 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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 8212B282020 for ; Wed, 13 Mar 2024 17:54:08 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D4D286FE19; Wed, 13 Mar 2024 16:47:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="kUM9xHkq" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 EC7626FE02; Wed, 13 Mar 2024 16:47:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710348469; cv=none; b=E+Z6l2+gJWW3sZTjQdv7X0bS55Fsee/1pJWjdelGvYusEF9f8RlUHV2tgpeUuCk98lMbzC6Je2R2uQMggQCVQkCDdknchKyEvlPiEL6ZqBm1KotDKFH9npFo1ziF2tC+JUkoIE5EbMw6BP+I3fR9xzLd8sYlLrAY0qVR8H+CVcs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710348469; c=relaxed/simple; bh=GVU3b2m2MSnTEGBcFRcMvG/E+T4QoRdGny2PkMtzVi4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=BL82fa+Vo3mWkeY64mtepiTb0a+CEvzJRmXnOidIsJ++qSAx6OLk8NOM60DLGFVbvIelA0bd9faRjtZxmbrB60QKSMpH4PyZPLhhYkoNdq4us47DEkXvvt2UixGvTUmSTrCNK0Qi1qF8VYIKs7gYHuXWQ8iSr3G35ZamLkpyR1U= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=kUM9xHkq; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0BC69C43390; Wed, 13 Mar 2024 16:47:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1710348468; bh=GVU3b2m2MSnTEGBcFRcMvG/E+T4QoRdGny2PkMtzVi4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kUM9xHkqB4IP5CpjKrHkPvt3n/cIZHMg+7zLq2WGaWTbHlq1Y9hv+2QmJ/dLzbiyr /m+tQSU0Zn4xD+k527SbW68u3q6AtygGKJIvhg2O7a4rsvsKWtznk8yLHdxN6yj0Cd KSJRF39ZbX9dMUFHn1CwGUNsyj32TLUU/CPFIf4necF4IZfY42jdSQElDyGMmc0lqi +apZeCUhzSWTE4yCBcsLrIlGz8wEJSDkYZtAkTacHpENJTAWd45kJPdYQZTlUT5Sse F6RKByG2fo8BivD11bx7cBlLRxb/ZsLZiYcrfn9kJ3HNNMdV/4TrAdjIOYhBzcR5R8 sZ2oMg1MAZOLw== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Prakash Sangappa , Muchun Song , Andrew Morton , Sasha Levin Subject: [PATCH 5.10 59/73] mm: hugetlb pages should not be reserved by shmat() if SHM_NORESERVE Date: Wed, 13 Mar 2024 12:46:26 -0400 Message-ID: <20240313164640.616049-60-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240313164640.616049-1-sashal@kernel.org> References: <20240313164640.616049-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-KernelTest-Patch: http://kernel.org/pub/linux/kernel/v5.x/stable-review/patch-5.10.213-rc1.gz X-KernelTest-Tree: git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable-rc.git X-KernelTest-Branch: linux-5.10.y X-KernelTest-Patches: git://git.kernel.org/pub/scm/linux/kernel/git/stable/stable-queue.git X-KernelTest-Version: 5.10.213-rc1 X-KernelTest-Deadline: 2024-03-15T16:46+00:00 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit From: Prakash Sangappa [ Upstream commit e656c7a9e59607d1672d85ffa9a89031876ffe67 ] 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. Link: https://lkml.kernel.org/r/1706040282-12388-1-git-send-email-prakash.sangappa@oracle.com Signed-off-by: Prakash Sangappa Acked-by: Muchun Song Cc: Signed-off-by: Andrew Morton Signed-off-by: Sasha Levin --- 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 c3e9fa7ce75f7..bf3cda4989623 100644 --- a/fs/hugetlbfs/inode.c +++ b/fs/hugetlbfs/inode.c @@ -135,6 +135,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 @@ -176,10 +177,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.43.0