Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nZTAU-00081Q-Gk for pgsql-bugs@arkaria.postgresql.org; Wed, 30 Mar 2022 07:55:03 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nZTAT-0000pr-Ag for pgsql-bugs@arkaria.postgresql.org; Wed, 30 Mar 2022 07:55:01 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nZTAS-0000pU-HB for pgsql-bugs@lists.postgresql.org; Wed, 30 Mar 2022 07:55:01 +0000 Received: from out2-smtp.messagingengine.com ([66.111.4.26]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nZTAP-0005mh-S0 for pgsql-bugs@lists.postgresql.org; Wed, 30 Mar 2022 07:54:59 +0000 Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id DDF0C5C0219; Wed, 30 Mar 2022 03:54:56 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute1.internal (MEProxy); Wed, 30 Mar 2022 03:54:56 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=paquier.xyz; h= cc:cc:content-type:date:date:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:sender:subject :subject:to:to; s=fm1; bh=Twc+tnocxfzJO1VaKT6HwlKM/VI++3jwhJ1MmH v76Lw=; b=NUg43y1abu45sML5p80pFvmGPGqENO1Sc9Tim3u6L/t9x1BK++ADx+ zH/xSt66HCqHukkyGZJcL9k4M3pI+X8z1vIWw3DeG1Ho7mvnrCzY3cwTotzjyaBo N19yW7mypN/GKYJHd/I3QQ3KhF6lIAOgNJM8uKWCMRLL0S5OdawOhlJb83xT+QwP MgB2CbaaturnHpxTNzeTkmIONmN4XXJIMEQiS6Qygg9JNB24nGxmQxJo/NmqCVAp 3rcKBpt+O9QFxaq0VfHTi7nJARYeGLCBX2+kqyPkaWWm9ag2z/mAVoNLHB1CoBnb HiDLZTt2CGQG41zJ3lONYmtS3aR3uwJQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm3; bh=Twc+tnocxfzJO1VaK T6HwlKM/VI++3jwhJ1MmHv76Lw=; b=YgX9Y3hyhFERuXYFLHmtAjbtySpgdzzpA R4OY0xbJsbEtmUlo8nUPm7omJgiZboSL1TzOWQ5IbB4NZuLgSI/UodwCSZ5+pNuq 5ZJGTbsaI4XwPX3hXpUezYPp7yWJM2xCPATzzaZ/VOV9mJNKUSmKHEujQwh8MbGp qplmof8AWrhfat14ax5IgPhU5yCdJYdatSk3Ty1jV0jNBtJ8GUnrWatJVqibyDro HnrFrgl/gaBfYbJtcSlvG+nJijfB2F5bgFJo1C+VRVsH5YWfvvkkmRzKhPTTPUlz SlJl2otBPYuXEBgvDa4frdIqG5knRUoxT6PO9lwo93JT0RSckTE6w== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvvddrudeiuddguddvhecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enfghrlhcuvffnffculdejtddmnecujfgurhepfffhvffukfhfgggtuggjsehgtderredt tddvnecuhfhrohhmpefoihgthhgrvghlucfrrghquhhivghruceomhhitghhrggvlhesph grqhhuihgvrhdrgiihiieqnecuggftrfgrthhtvghrnhepvdegudeuhfdtueeltedtveej heehieevueeigeelteegleejleeiueeiheegvefhnecuvehluhhsthgvrhfuihiivgeptd enucfrrghrrghmpehmrghilhhfrhhomhepmhhitghhrggvlhesphgrqhhuihgvrhdrgiih ii X-ME-Proxy: Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 30 Mar 2022 03:54:54 -0400 (EDT) Date: Wed, 30 Mar 2022 16:54:50 +0900 From: Michael Paquier To: Thomas Munro Cc: Julien Rouhaud , okano.naoki@jp.fujitsu.com, PostgreSQL mailing lists Subject: Re: BUG #17448: In Windows 10, version 1703 and later, huge_pages doesn't work. Message-ID: References: <17448-0a96583a67edb1f7@postgresql.org> <20220326082408.moh55zuecxjmewnm@jrouhaud> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="LD3pfApFPl6CapiV" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --LD3pfApFPl6CapiV Content-Type: multipart/mixed; boundary="8jnowtt6rp7hcNn9" Content-Disposition: inline --8jnowtt6rp7hcNn9 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Sun, Mar 27, 2022 at 12:07:57AM +1300, Thomas Munro wrote: > There are traces of method to the madness: It's basically YYMM, but > then after 2004 they switched to H1 and H2 (first/second half of the > year) instead of MM, perhaps to avoid confusion with YYYY format year. > Note also that Windows 10 has a 21H2 and Windows 11 has a 21H2. > > Some question I have: is FILE_MAP_LARGE PAGES a macro? We claim to > support all those ancient zombie OSes like Windows 7, or maybe it's > even XP for 11, and this has to be back-patched to 11, so we might > need to make it conditional. But conditional on what? For example, > does something like the attached work (untested)? What happens if a < > 1703 kernel sees this flag, does it reject it or ignore it? I don't have an answer about how much Windows gets angry if we pass down to MapViewOfFileEx() the flag FILE_MAP_LARGE_PAGES when running the code on a version of Windows that does not support it. Anyway, I think that we could just play it safe. See for example the attached, where I use PG_FILE_MAP_LARGE_PAGES at compile time to find if the value is set. Then, at run-time, I am just relying on=20 IsWindowsVersionOrGreater() to do the job, something useful when huge_pages=3Don as I guess we should fail hard if we did not know about FILE_MAP_LARGE_PAGES at compile-time, but try to use huge pages at run time with version >=3D 10.0.1703. Perhaps there is a better thing to do? -- Michael --8jnowtt6rp7hcNn9 Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="win32-hugepages-michael.patch" Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/port/win32_shmem.c b/src/backend/port/win32_shmem.c index 6cf69411db..49b9fa0322 100644 --- a/src/backend/port/win32_shmem.c +++ b/src/backend/port/win32_shmem.c @@ -12,6 +12,8 @@ */ #include "postgres.h" =20 +#include + #include "miscadmin.h" #include "storage/dsm.h" #include "storage/ipc.h" @@ -39,6 +41,17 @@ #define PROTECTIVE_REGION_SIZE (10 * WIN32_STACK_RLIMIT) void *ShmemProtectiveRegion =3D NULL; =20 +/* + * FILE_MAP_LARGE_PAGES is needed for the support of huge pages, and + * it is only available since Windows 10.0.1703. PG_FILE_MAP_LARGE_PAGES + * saves its value at compile-time. + */ +#ifdef FILE_MAP_LARGE_PAGES +#define PG_FILE_MAP_LARGE_PAGES FILE_MAP_LARGE_PAGES +#else +#define PG_FILE_MAP_LARGE_PAGES 0 +#endif + HANDLE UsedShmemSegID =3D INVALID_HANDLE_VALUE; void *UsedShmemSegAddr =3D NULL; static Size UsedShmemSegSize =3D 0; @@ -216,6 +229,7 @@ PGSharedMemoryCreate(Size size, SIZE_T largePageSize =3D 0; Size orig_size =3D size; DWORD flProtect =3D PAGE_READWRITE; + DWORD desiredAccess; =20 ShmemProtectiveRegion =3D VirtualAlloc(NULL, PROTECTIVE_REGION_SIZE, MEM_RESERVE, PAGE_NOACCESS); @@ -258,6 +272,22 @@ PGSharedMemoryCreate(Size size, } } =20 + /* + * If this binary has been compiled without FILE_MAP_LARGE_PAGES + * available and huge pages are wanted, there is nothing do do + * if running on a version older than 10.0.1703 as this flag is a + * requirement to allow the use of huge pages. + */ + if (huge_pages =3D=3D HUGE_PAGES_ON && + PG_FILE_MAP_LARGE_PAGES =3D=3D 0 && + IsWindowsVersionOrGreater(10, 0, 1703)) + { + ereport(FATAL, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("the processor does not support large pages"), + errdetail("The code has been compiled without huge page support for t= his version of Windows."))); + } + retry: #ifdef _WIN64 size_high =3D size >> 32; @@ -353,12 +383,21 @@ retry: if (!CloseHandle(hmap)) elog(LOG, "could not close handle to shared memory: error code %lu", Get= LastError()); =20 + desiredAccess =3D FILE_MAP_WRITE | FILE_MAP_READ; + + /* + * Set large pages if wanted. FILE_MAP_LARGE_PAGES is needed when + * running at least Windows 10.0.1703. + */ + if ((flProtect & SEC_LARGE_PAGES) !=3D 0 && + IsWindowsVersionOrGreater(10, 0, 1703)) + desiredAccess |=3D PG_FILE_MAP_LARGE_PAGES; =20 /* * Get a pointer to the new shared memory segment. Map the whole segment * at once, and let the system decide on the initial address. */ - memAddress =3D MapViewOfFileEx(hmap2, FILE_MAP_WRITE | FILE_MAP_READ, 0, = 0, 0, NULL); + memAddress =3D MapViewOfFileEx(hmap2, desiredAccess, 0, 0, 0, NULL); if (!memAddress) ereport(FATAL, (errmsg("could not create shared memory segment: error code %lu", GetL= astError()), --8jnowtt6rp7hcNn9-- --LD3pfApFPl6CapiV Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEG72nH6vTowiyblFKnvQgOdbyQH0FAmJEDMoACgkQnvQgOdby QH1mwBAApu/wKMUtdXG6pQlzzpQUlbJH4U17pU8Pd8YsuRr5HAzkaJW0DFOrFdTA 9AFlg9mjD9QWLWMYe5dJ80myBZqNjyIXN7CwpSFzqwPYEMSqzpG/FF96Ed7yUtp3 QfEicqZZCjnFPRbAJTpunSQj+Ry8zQhRF/ZUGZuIWTZAw1MeorHcDsgGAsKAnf3E SbyK37ZJVNLgE+jAauGtX8tNsDUl0HNZno8EG5uEg7v2P/EUIh4nf4Gl/e/hiEGl 5JfBVhZXaO7o3moFU2HzCfduB3ZtAiZfxN3JRsbW4Z1O85/kkgjIOoqjYdj3yPsw NhStW0aQffGwcI6kp5DiKs173wzfqUzKlkr7AwPp0roTGm9Nl9erGgARbuR/XxYy NnbW9IovhCDKvHOBxbCU6GmHj82VsENfqv8MmcX+YLd6pP8kYRyUGfBKccg7HA+0 ri15jiaEXTVW/ZkAKJxlHr1MYLdDDm59kZmyyaDhWYHQclYgtifl3Ih5W4wVAQnG 4Sf1Gmi3Nlu1Bd3VgSQIPavvSZ9w/06GetCF4RYliN1GdCPHihbpDmrIqd9Aavcd F/w6nfAAmvnoDmUxy6ixku7HNHsmJaKWkIZp9FlGZtdTCOUdb/pgMCU3Hky6vuMx EEbk7E1j/txwvV96OHpL1zYNJOuI4RaK3ZC1uiwx/PL7lYbjnCM= =IF2N -----END PGP SIGNATURE----- --LD3pfApFPl6CapiV--