createfilemapping returns 1450

jay rhoades 6 Reputation points
2021-04-09T15:27:22.827+00:00

using the msdn online example (c++) code
found here: https://learn.microsoft.com/en-us/windows/win32/memory/creating-a-file-mapping-using-large-pages

on a windows 10 version 1909 box with 48GB of memory, the createfilemapping() for a size of 0x200000 returns error 1450 as shown below

the privilege "SeLockMemoryPrivilege" is successfully enabled in the process token.
any suggestions/experience with such would be appreciated;
thanks you;

86325-image.png

Windows development | Windows API - Win32
{count} votes

5 answers

Sort by: Most helpful
  1. jay rhoades 6 Reputation points
    2021-04-13T20:22:43.627+00:00

    here is attached txt (cpp) mainline that when put into a visual studio c++ console app will represent this ongoing discussion;
    this in lieu of posting a zip of the console app that with intellisense browse db is just way too big....
    if anyone has any insights into why using map_use_large_pages on specify size of 6 * GETLARGEPAGEMINIMUM results in 1450
    on windows 10 version 1909 box with 48GB of memory, baffles me;
    kind regards; @David Lowndes
    jsr;

    87453-filemaplargepages.txt


  2. Gary Nebbett 6,216 Reputation points
    2021-04-15T18:39:15.2+00:00

    Hello @jay rhoades ,

    I guess that you have access to more than one 64-bit Windows system; is this behaviour widespread (e.g. what percentage of your 64-bit Windows systems exhibit this behaviour)?

    Because memory management is such a fundamental OS (Operating System) service, there is not much more that we can do to troubleshoot this problem remotely. You may have to try local kernel debugging if you really want to pursue the cause of this problem...

    One my system, I can allocate 6000 × GetLargePageMinimum() without error but 7000 × GetLargePageMinimum() fails with error 1450, so ERROR_NO_SYSTEM_RESOURCES/STATUS_INSUFFICIENT_RESOURCES seems sometimes to be an accurate description of the problem...

    Gary

    0 comments No comments

  3. Gary Nebbett 6,216 Reputation points
    2021-04-14T19:44:52.7+00:00

    Hello @jay rhoades ,

    Can you try compiling and running this program and then reporting what it shows? Success would look something like this:

    0x200000
    0xD8, 0
    0x1B000000, 0

    I checked that large page(s) were actually used and it seems that they are:

    0: kd> !vad 1B000000
    VAD Level Start End Commit
    ffffce8206e94b60 6 1b000 1b1ff 0 Mapped LargePag READWRITE Pagefile section, shared commit 0x200

    The program can be compiled by just invoking the command "%SystemRoot%\Microsoft.NET\Framework64\v4.0.30319\csc" with the name of the file containing the saved program text. You can add the "-platform:x64" option to the compilation if you want but the default (anycpu) should be OK too if there is nothing unusual about your environment.

    Gary

    using System;  
    using System.Reflection;  
    using System.Runtime.InteropServices;  
      
    class A  
    {  
        static int Main(string[] args)  
        {  
     try  
     {  
         Privilege lockmem = new Privilege("SeLockMemoryPrivilege");  
      
         lockmem.Enable();  
      
         X();  
      
         lockmem.Revert();  
     }  
     catch (Exception ex)  
     {  
         Console.Error.WriteLine(ex);  
     }  
      
     return 0;  
        }  
      
        static void X()  
        {  
     UIntPtr n = GetLargePageMinimum();  
      
     Console.WriteLine("0x{0:X}", n.ToUInt64());  
      
     IntPtr h = CreateFileMapping(INVALID_HANDLE_VALUE, null, PAGE_READWRITE | SEC_COMMIT | SEC_LARGE_PAGES, 0, n.ToUInt32(), "Gary");  
      
     Console.WriteLine("0x{0:X}, {1}", h.ToInt64(), Marshal.GetLastWin32Error());  
      
     IntPtr p = MapViewOfFile(h, FILE_MAP_ALL_ACCESS | FILE_MAP_LARGE_PAGES, 0, 0, n);  
      
     Console.WriteLine("0x{0:X}, {1}", p.ToInt64(), Marshal.GetLastWin32Error());  
      
     UnmapViewOfFile(p);  
     CloseHandle(h);  
        }  
      
        static IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);  
      
        const uint SEC_COMMIT = 0x08000000;  
        const uint SEC_LARGE_PAGES = 0x80000000;  
        const uint FILE_MAP_ALL_ACCESS = 0x000F001F;  
        const uint FILE_MAP_LARGE_PAGES = 0x20000000;  
        const uint PAGE_READWRITE = 0x04;  
      
        [DllImport("kernel32.dll", SetLastError = true)]  
        static extern bool CloseHandle(IntPtr handle);  
      
        [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]  
        static extern IntPtr CreateFileMapping(IntPtr file, object sa, uint protect, uint n2, uint n, string name);  
      
        [DllImport("kernel32.dll", SetLastError = true)]  
        static extern IntPtr MapViewOfFile(IntPtr mapping, uint access, uint m2, uint m, UIntPtr n);  
      
        [DllImport("kernel32.dll", SetLastError = true)]  
        static extern bool UnmapViewOfFile(IntPtr p);  
      
        [DllImport("kernel32.dll")]  
        static extern UIntPtr GetLargePageMinimum();  
    }  
      
    class Privilege  
    {  
        Type t = Type.GetType("System.Security.AccessControl.Privilege");  
      
        object privilege;  
      
        public Privilege(string name)  
        {  
     privilege = t.GetConstructor(new Type[] {typeof(string)}).Invoke(new object[] {name});  
        }  
      
        public void Enable()  
        {  
     t.GetMethod("Enable", BindingFlags.Public | BindingFlags.Instance).Invoke(privilege, null);  
        }  
      
        public void Revert()  
        {  
     t.GetMethod("Revert", BindingFlags.Public | BindingFlags.Instance).Invoke(privilege, null);  
        }  
    }  
    
    
    
      
    

  4. Gary Nebbett 6,216 Reputation points
    2021-04-15T15:42:20.923+00:00

    Hello @jay rhoades ,

    As a first step, let's just try a VirtualAlloc with MEM_LARGE_PAGES and check whether that works (code below).

    Gary

    using System;  
    using System.Reflection;  
    using System.Runtime.InteropServices;  
          
    class A  
    {  
        static int Main(string[] args)  
        {  
    	try  
    	{  
    	    Privilege lockmem = new Privilege("SeLockMemoryPrivilege");  
          
    	    lockmem.Enable();  
          
    	    X();  
          
    	    lockmem.Revert();  
    	}  
    	catch (Exception ex)  
    	{  
    	    Console.Error.WriteLine(ex);  
    	}  
          
    	return 0;  
        }  
          
        static void X()  
        {  
    	UIntPtr n = GetLargePageMinimum();  
          
    	Console.WriteLine("0x{0:X}", n.ToUInt64());  
      
    	IntPtr p0 = VirtualAlloc(IntPtr.Zero, n, MEM_RESERVE | MEM_COMMIT | MEM_LARGE_PAGES, PAGE_READWRITE);  
          
    	Console.WriteLine("0x{0:X}, {1}", p0.ToInt64(), Marshal.GetLastWin32Error());  
      
    	IntPtr h = CreateFileMapping(INVALID_HANDLE_VALUE, null, PAGE_READWRITE | SEC_COMMIT | SEC_LARGE_PAGES, 0, n.ToUInt32(), "Gary");  
          
    	Console.WriteLine("0x{0:X}, {1}", h.ToInt64(), Marshal.GetLastWin32Error());  
          
    	IntPtr p = MapViewOfFile(h, FILE_MAP_ALL_ACCESS | FILE_MAP_LARGE_PAGES, 0, 0, n);  
          
    	Console.WriteLine("0x{0:X}, {1}", p.ToInt64(), Marshal.GetLastWin32Error());  
          
    	UnmapViewOfFile(p);  
    	CloseHandle(h);  
        }  
          
        static IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);  
          
        const uint MEM_COMMIT = 0x00001000;  
        const uint MEM_RESERVE = 0x00002000;  
        const uint MEM_LARGE_PAGES = 0x20000000;  
        const uint SEC_COMMIT = 0x08000000;  
        const uint SEC_LARGE_PAGES = 0x80000000;  
        const uint FILE_MAP_ALL_ACCESS = 0x000F001F;  
        const uint FILE_MAP_LARGE_PAGES = 0x20000000;  
        const uint PAGE_READWRITE = 0x04;  
          
        [DllImport("kernel32.dll", SetLastError = true)]  
        static extern bool CloseHandle(IntPtr handle);  
          
        [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]  
        static extern IntPtr CreateFileMapping(IntPtr file, object sa, uint protect, uint n2, uint n, string name);  
          
        [DllImport("kernel32.dll", SetLastError = true)]  
        static extern IntPtr MapViewOfFile(IntPtr mapping, uint access, uint m2, uint m, UIntPtr n);  
          
        [DllImport("kernel32.dll", SetLastError = true)]  
        static extern bool UnmapViewOfFile(IntPtr p);  
          
        [DllImport("kernel32.dll")]  
        static extern UIntPtr GetLargePageMinimum();  
      
        [DllImport("kernel32.dll", SetLastError = true)]  
        static extern IntPtr VirtualAlloc(IntPtr p, UIntPtr n, uint type, uint protect);  
    }  
          
    class Privilege  
    {  
        Type t = Type.GetType("System.Security.AccessControl.Privilege");  
          
        object privilege;  
          
        public Privilege(string name)  
        {  
    	privilege = t.GetConstructor(new Type[] {typeof(string)}).Invoke(new object[] {name});  
        }  
          
        public void Enable()  
        {  
    	t.GetMethod("Enable", BindingFlags.Public | BindingFlags.Instance).Invoke(privilege, null);  
        }  
          
        public void Revert()  
        {  
    	t.GetMethod("Revert", BindingFlags.Public | BindingFlags.Instance).Invoke(privilege, null);  
        }  
    }  
      
    

  5. Gary Nebbett 6,216 Reputation points
    2021-04-15T19:56:43.11+00:00

    Hello @jay rhoades ,

    Here are some more suggestions.

    If you have an existing crash dump file for the system, analyze it with a debugger. Find the base of the kernel image (e.g. with "lm m nt") and then check the Page Table Entry (PTE) for that page (using the "!pte" command). The kernel should be mapped with large pages and the !pte output should include the text "LARGE PAGE".

    0: kd> lm m nt
    start end module name
    fffff8033ec00000 fffff8033fc46000 nt (pdb symbols) c:\windows\symbols\ntkrnlmp.pdb\769C521E4833ECF72E21F02BF33691A51\ntkrnlmp.pdb
    0: kd> !pte fffff803`3ec00000
    VA fffff8033ec00000
    PXE at FFFF804020100F80 PPE at FFFF8040201F0060 PDE at FFFF80403E00CFB0 PTE at FFFF807C019F6000
    contains 000000007B009063 contains 000000007B00A063 contains 8A000000030000A1 contains 0000000000000000
    pfn 7b009 ---DA--KWEV pfn 7b00a ---DA--KWEV pfn 3000 --L-A--KR-V LARGE PAGE pfn 3000

    Another command that can be issued when analyzing the crash dump is "rM aa". This should produce output like the following:

    0: kd> rM aa
    rax=0000000000000000 rbx=ffffce81f6010000 rcx=ffffbe00605bf440
    rdx=0000000000000001 rsi=0000000000000000 rdi=ffffce8202f7b010
    rip=fffff8033f5a82c4 rsp=ffff9782f9edf3e0 rbp=0000000000000000
    r8=8000000000000000 r9=0000000000000000 r10=0000000000000000
    r11=0000000000000000 r12=0000000000000002 r13=0000000000000000
    r14=fffff8033c713180 r15=ffffce820ec7c080
    iopl=0 nv up di pl zr na po nc
    cs=0010 ss=0018 ds=002b es=002b fs=0053 gs=002b efl=00040046
    cr0=0000000080050033 cr2=00007ffd9c0ab1e0 cr3=00000002c2ce4002
    cr8=0000000000000002
    dr0=0000000000000000 dr1=0000000000000000 dr2=0000000000000000
    dr3=0000000000000000 dr6=00000000ffff0ff0 dr7=0000000000000400 cr4=0000000000370678
    kdr0=0000000000000000 kdr1=0000000000000000 kdr2=0000000000000000
    kdr3=0000000000000000 kdr6=00000000ffff0ff0 kdr7=0000000000000400
    nt!IopLiveDumpEndMirroringCallback+0xb4:
    fffff803`3f5a82c4 498d8e00010000 lea rcx,[r14+100h]

    If you post this output, then we will be able to see what has been configured in the processor's control registers.

    Try rebooting the system and then running the test programs when the system comes back up - this will maximize the chances of success if there really is a resource shortage.

    Gary


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.