Message ID | 20231214101931.1155586-1-usama.anjum@collabora.com |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:3b04:b0:fb:cd0c:d3e with SMTP id c4csp8440278dys; Thu, 14 Dec 2023 02:20:04 -0800 (PST) X-Google-Smtp-Source: AGHT+IH52jjQeZcMpxe8vMh58W4SLy/1G5nejZFL/YyHEcK8ny0wavOdtwZ23ac3DR1foHIyrQZW X-Received: by 2002:a05:6a20:d413:b0:18c:8fef:22cc with SMTP id il19-20020a056a20d41300b0018c8fef22ccmr4855918pzb.24.1702549204400; Thu, 14 Dec 2023 02:20:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702549204; cv=none; d=google.com; s=arc-20160816; b=K5oeJz3GVnnZ1DlIe+ttf4VqACFmbvaUazti+yAgJrxFGANWzFd1a9IshIBDrtEwPM /WxjeDjd57N5+otlKVqkxRT0b2TZwlqBC9c9lzXsRZYEWALzCNCiF12IAtDi7KGfDP16 M6XcHO1iAKThk8bF80Wu40alAEWfgwSdtA+pM526ZCMK0+98FD2L4Rll6TSC42VzBT57 FzJOosL5kre4PVEt0tl47cwa4C4piX83/g2F6/n7l1kLOqHjCXSEUh06wrFkOmSYcTwU oHRIJ/03belluhC/nr3Kk8GgSdbkEit48qOhVXepU5OcIeME5OYdERTcaRGwag3vRGNE nybQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=X2vDUFx7i5Dl2rPMDZU06vX/Imo4IhO/38H755HgPRo=; fh=if72FA2J+4nFryHMCVgJxAGR3e/xUADBLOvFTVrJeus=; b=ON7jIH2vDZ4SYY0i2AZf8EdO06rc0Q29uHtjLWCjdR2UN2OY0CORMTaK2iAhXmX+2y oulNOo62Hw27OYeEqsogYzxB8TBJbEzXlDvyWUYVLziV35TDK6cA3jgYFDRKG/NvhVUu IH13RlR9NjXC9KmIzZAA8ZjYCZ03ujHaWEqTyBJNgnuCRa/HUmddMBRBYJP/36qubAbK PBn7qjuCnMfNFyksCMTVbXV7yiGvJA2O4q27quHjkC76OaiM/rNl7NGp3pFeum8a8P+v IzLTAqU++1U6kaTzDstnV9CLVQpcro3gcA1IyX15MDRrhv+LnXNg5q2ySgwH74dBlOQe UyFg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=oP+MrIZc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Received: from morse.vger.email (morse.vger.email. [23.128.96.31]) by mx.google.com with ESMTPS id d4-20020a170902b70400b001cfcf3dd310si10987627pls.505.2023.12.14.02.20.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 02:20:04 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) client-ip=23.128.96.31; Authentication-Results: mx.google.com; dkim=pass header.i=@collabora.com header.s=mail header.b=oP+MrIZc; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.31 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=collabora.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id B76458221105; Thu, 14 Dec 2023 02:19:58 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1443583AbjLNKTu (ORCPT <rfc822;dexuan.linux@gmail.com> + 99 others); Thu, 14 Dec 2023 05:19:50 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54982 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1443555AbjLNKTt (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Thu, 14 Dec 2023 05:19:49 -0500 Received: from madrid.collaboradmins.com (madrid.collaboradmins.com [IPv6:2a00:1098:ed:100::25]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DC788106; Thu, 14 Dec 2023 02:19:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1702549194; bh=pjC6AgncqA69AxQ0TJR0swcPyuKAW1TG7xIWc+P76j8=; h=From:To:Cc:Subject:Date:From; b=oP+MrIZcGjD6oX9TyNQE6+NQ/JFU1CVpxFDvx6nr7YJl7+DRT3PdKJiuepIyJDaUf MbM0byta1XsLrImto2gzclOwmEtBn/k9ODy4gMG5FaP8B/P0h4Peop4B18SkusUFWS 7BZuCmrkhFBwPo1acs4NWLmZbk/fMmlF6Phu8J3W/uxeQ9Wjnxd1UMPnMwZLBFB4/D XEA8yUvcuOU36g5zm3l8G8dCihrtnYUzHMhKA5JmDGsO+U/xX/0URnf7sx0xDLhVsm VPCZ73AYN37QJZdK2p9+LkAnbVz/lgw5b4VmRmsoO5momu+h13m5z0xcannUzrdoeG wLIYBrpwEPVzQ== Received: from localhost.localdomain (cola.collaboradmins.com [195.201.22.229]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: usama.anjum) by madrid.collaboradmins.com (Postfix) with ESMTPSA id E1EE7378000B; Thu, 14 Dec 2023 10:19:49 +0000 (UTC) From: Muhammad Usama Anjum <usama.anjum@collabora.com> To: Andrew Morton <akpm@linux-foundation.org>, Shuah Khan <shuah@kernel.org>, Mike Rapoport <rppt@kernel.org>, James Bottomley <James.Bottomley@HansenPartnership.com> Cc: Muhammad Usama Anjum <usama.anjum@collabora.com>, kernel@collabora.com, "kernelci.org bot" <bot@kernelci.org>, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] selftests: secretmem: Floor the memory size to the multiple of page_size Date: Thu, 14 Dec 2023 15:19:30 +0500 Message-ID: <20231214101931.1155586-1-usama.anjum@collabora.com> X-Mailer: git-send-email 2.42.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Thu, 14 Dec 2023 02:19:59 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785252234295710376 X-GMAIL-MSGID: 1785252234295710376 |
Series |
selftests: secretmem: Floor the memory size to the multiple of page_size
|
|
Commit Message
Muhammad Usama Anjum
Dec. 14, 2023, 10:19 a.m. UTC
The "locked-in-memory size" limit per process can be non-multiple of
page_size. The mmap() fails if we try to allocate locked-in-memory
with same size as the allowed limit if it isn't multiple of the
page_size because mmap() rounds off the memory size to be allocated
to next multiple of page_size.
Fix this by flooring the length to be allocated with mmap() to the
previous multiple of the page_size.
Fixes: 76fe17ef588a ("secretmem: test: add basic selftest for memfd_secret(2)")
Reported-by: "kernelci.org bot" <bot@kernelci.org>
Signed-off-by: Muhammad Usama Anjum <usama.anjum@collabora.com>
---
tools/testing/selftests/mm/memfd_secret.c | 3 +++
1 file changed, 3 insertions(+)
Comments
On Thu, 14 Dec 2023 15:19:30 +0500 Muhammad Usama Anjum <usama.anjum@collabora.com> wrote: > The "locked-in-memory size" limit per process can be non-multiple of > page_size. The mmap() fails if we try to allocate locked-in-memory > with same size as the allowed limit if it isn't multiple of the > page_size because mmap() rounds off the memory size to be allocated > to next multiple of page_size. > > Fix this by flooring the length to be allocated with mmap() to the > previous multiple of the page_size. I'd like to understand how this was noticed, what the ongoing effect might be, etc. To help decide which kernel version(s) need the patch. > Fixes: 76fe17ef588a ("secretmem: test: add basic selftest for memfd_secret(2)") > Reported-by: "kernelci.org bot" <bot@kernelci.org> Which is one of the reasons we're now placing a Closes: tag after a Reported-by:.
Hi Andrew, On 12/15/23 12:40 AM, Andrew Morton wrote: > On Thu, 14 Dec 2023 15:19:30 +0500 Muhammad Usama Anjum <usama.anjum@collabora.com> wrote: > >> The "locked-in-memory size" limit per process can be non-multiple of >> page_size. The mmap() fails if we try to allocate locked-in-memory >> with same size as the allowed limit if it isn't multiple of the >> page_size because mmap() rounds off the memory size to be allocated >> to next multiple of page_size. >> >> Fix this by flooring the length to be allocated with mmap() to the >> previous multiple of the page_size. > > I'd like to understand how this was noticed, what the ongoing effect > might be, etc. To help decide which kernel version(s) need the patch. This was getting triggered on KernelCI regularly because of different ulimit settings which wasn't multiple of the page_size. Find logs here: https://linux.kernelci.org/test/plan/id/657654bd8e81e654fae13532/ The bug in was present from the time test was first added. > >> Fixes: 76fe17ef588a ("secretmem: test: add basic selftest for memfd_secret(2)") >> Reported-by: "kernelci.org bot" <bot@kernelci.org> > > Which is one of the reasons we're now placing a Closes: tag after a > Reported-by:. I was looking for email report from KernelCI. But I didn't find it. Not sure if we can do something like following: Closes: https://linux.kernelci.org/test/plan/id/657654bd8e81e654fae13532/ > >
On Thu, Dec 14, 2023 at 03:19:30PM +0500, Muhammad Usama Anjum wrote: > The "locked-in-memory size" limit per process can be non-multiple of > page_size. The mmap() fails if we try to allocate locked-in-memory > with same size as the allowed limit if it isn't multiple of the > page_size because mmap() rounds off the memory size to be allocated > to next multiple of page_size. > > Fix this by flooring the length to be allocated with mmap() to the > previous multiple of the page_size. > > Fixes: 76fe17ef588a ("secretmem: test: add basic selftest for memfd_secret(2)") > Reported-by: "kernelci.org bot" <bot@kernelci.org> > Signed-off-by: Muhammad Usama Anjum <usama.anjum@collabora.com> > --- > tools/testing/selftests/mm/memfd_secret.c | 3 +++ > 1 file changed, 3 insertions(+) > > diff --git a/tools/testing/selftests/mm/memfd_secret.c b/tools/testing/selftests/mm/memfd_secret.c > index 957b9e18c729..9b298f6a04b3 100644 > --- a/tools/testing/selftests/mm/memfd_secret.c > +++ b/tools/testing/selftests/mm/memfd_secret.c > @@ -62,6 +62,9 @@ static void test_mlock_limit(int fd) > char *mem; > > len = mlock_limit_cur; > + if (len % page_size != 0) > + len = (len/page_size) * page_size; > + With mlock limit smaller than a page we get zero length here and mmap will fail with -EINVAL because of it. In this case I think we can just skip the first mmap and only check that mmaping more than mlock limit fails. > mem = mmap(NULL, len, prot, mode, fd, 0); > if (mem == MAP_FAILED) { > fail("unable to mmap secret memory\n"); > -- > 2.42.0 >
On 12/15/23 3:59 PM, Mike Rapoport wrote: > On Thu, Dec 14, 2023 at 03:19:30PM +0500, Muhammad Usama Anjum wrote: >> The "locked-in-memory size" limit per process can be non-multiple of >> page_size. The mmap() fails if we try to allocate locked-in-memory >> with same size as the allowed limit if it isn't multiple of the >> page_size because mmap() rounds off the memory size to be allocated >> to next multiple of page_size. >> >> Fix this by flooring the length to be allocated with mmap() to the >> previous multiple of the page_size. >> >> Fixes: 76fe17ef588a ("secretmem: test: add basic selftest for memfd_secret(2)") >> Reported-by: "kernelci.org bot" <bot@kernelci.org> >> Signed-off-by: Muhammad Usama Anjum <usama.anjum@collabora.com> >> --- >> tools/testing/selftests/mm/memfd_secret.c | 3 +++ >> 1 file changed, 3 insertions(+) >> >> diff --git a/tools/testing/selftests/mm/memfd_secret.c b/tools/testing/selftests/mm/memfd_secret.c >> index 957b9e18c729..9b298f6a04b3 100644 >> --- a/tools/testing/selftests/mm/memfd_secret.c >> +++ b/tools/testing/selftests/mm/memfd_secret.c >> @@ -62,6 +62,9 @@ static void test_mlock_limit(int fd) >> char *mem; >> >> len = mlock_limit_cur; >> + if (len % page_size != 0) >> + len = (len/page_size) * page_size; >> + > > With mlock limit smaller than a page we get zero length here and mmap will > fail with -EINVAL because of it. This test has a initialization step in prepare() where it increases the limit to at least a page if it is less than a page. Hence we'll never get len = 0 here. > In this case I think we can just skip the first mmap and only check that > mmaping more than mlock limit fails. > >> mem = mmap(NULL, len, prot, mode, fd, 0); >> if (mem == MAP_FAILED) { >> fail("unable to mmap secret memory\n"); >> -- >> 2.42.0 >> >
diff --git a/tools/testing/selftests/mm/memfd_secret.c b/tools/testing/selftests/mm/memfd_secret.c index 957b9e18c729..9b298f6a04b3 100644 --- a/tools/testing/selftests/mm/memfd_secret.c +++ b/tools/testing/selftests/mm/memfd_secret.c @@ -62,6 +62,9 @@ static void test_mlock_limit(int fd) char *mem; len = mlock_limit_cur; + if (len % page_size != 0) + len = (len/page_size) * page_size; + mem = mmap(NULL, len, prot, mode, fd, 0); if (mem == MAP_FAILED) { fail("unable to mmap secret memory\n");