<div dir="ltr">I sent the previous mail too early!<div><br></div><div>I got it to work for root user by setting &quot;<span style="color:rgb(0,0,0);white-space:pre-wrap">/proc/sys/vm/nr_hugepages&quot; to 20 as suggested in </span><span style="font-size:13px;font-family:arial,sans-serif">Documentation/vm/hugetlbpage.</span><span style="font-size:13px;font-family:arial,sans-serif">txt. Now I&#39;m able to mmap it and it reflects the same in /proc/meminfo as well! :)</span></div>
<div><span style="font-size:13px;font-family:arial,sans-serif"><br></span></div><div><span style="font-size:13px;font-family:arial,sans-serif">HTH,</span></div><div><span style="font-size:13px;font-family:arial,sans-serif">-mandeep</span></div>
<div><span style="font-size:13px;font-family:arial,sans-serif"><br></span></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Aug 18, 2014 at 1:23 PM, Mandeep Sandhu <span dir="ltr">&lt;<a href="mailto:mandeepsandhu.chd@gmail.com" target="_blank">mandeepsandhu.chd@gmail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">I tried your program and got the same result, i.e &quot;Resource temporarily unavailable&quot; for an ordinary user and &quot;Cannot allocate memory&quot; if run as root.<div>
<br></div><div>However, reading the Huge TLB pages doc (Documentation/vm/hugetlbpage.txt), I saw the following para which might explain the error:</div>
<div><span style="font-family:arial,helvetica,sans-serif;color:rgb(0,0,0);white-space:pre-wrap">...</span></div><div><span style="font-family:arial,helvetica,sans-serif;color:rgb(0,0,0);white-space:pre-wrap">Once a number of huge pages have been pre-allocated to the kernel huge page</span><br>

</div><div><span style="font-family:arial,helvetica,sans-serif;color:rgb(0,0,0);white-space:pre-wrap">pool, a user with appropriate privilege can use either the mmap system call</span><br></div><div><span style="font-family:arial,helvetica,sans-serif;color:rgb(0,0,0);white-space:pre-wrap">or shared memory system calls to use the huge pages.  See the discussion of</span><br>

</div><div><span style="font-family:arial,helvetica,sans-serif;color:rgb(0,0,0);white-space:pre-wrap">Using Huge Pages, below.</span><br></div><div><span style="font-family:arial,helvetica,sans-serif;color:rgb(0,0,0);white-space:pre-wrap"><br>

</span></div><div><span style="font-family:arial,helvetica,sans-serif;color:rgb(0,0,0);white-space:pre-wrap">The administrator can allocate persistent huge pages on the kernel boot</span><br></div><div><span style="font-family:arial,helvetica,sans-serif;color:rgb(0,0,0);white-space:pre-wrap">command line by specifying the &quot;hugepages=N&quot; parameter, where &#39;N&#39; = the</span><br>

</div><div><span style="font-family:arial,helvetica,sans-serif;color:rgb(0,0,0);white-space:pre-wrap">number of huge pages requested.  This is the most reliable method of</span><br></div><div><span style="font-family:arial,helvetica,sans-serif;color:rgb(0,0,0);white-space:pre-wrap">allocating huge pages as memory has not yet become fragmented.</span><br>

</div><div><span style="font-family:arial,helvetica,sans-serif;color:rgb(0,0,0);white-space:pre-wrap">...</span></div><div><span style="font-family:arial,helvetica,sans-serif;color:rgb(0,0,0);white-space:pre-wrap">...</span></div>

<div><pre style="word-wrap:break-word"><font face="arial, helvetica, sans-serif" style="color:rgb(0,0,0);white-space:pre-wrap">I don&#39;t have my kernel booted with it, so that might explain why mmap fails for </font><font color="#000000" face="arial, helvetica, sans-serif"><span style="white-space:pre-wrap">MAP_HUGETLB</span></font><span style="color:rgb(0,0,0);white-space:pre-wrap;font-family:arial,helvetica,sans-serif">.</span></pre>

<pre style="word-wrap:break-word"><span style="color:rgb(0,0,0);white-space:pre-wrap;font-family:arial,helvetica,sans-serif">I can&#39;t restart my machine with the new flag right now, but maybe you can try.</span></pre>
<pre style="word-wrap:break-word"><font color="#000000" face="arial, helvetica, sans-serif"><span style="white-space:pre-wrap">HTH,</span></font></pre><pre style="word-wrap:break-word"><span style="color:rgb(0,0,0);white-space:pre-wrap;font-family:arial,helvetica,sans-serif">-mandeep</span></pre>

<pre style="word-wrap:break-word"><span style="color:rgb(0,0,0);white-space:pre-wrap;font-family:arial,helvetica,sans-serif"><br></span></pre></div></div><div class="gmail_extra"><br><br><div class="gmail_quote"><div><div class="h5">
On Mon, Aug 18, 2014 at 11:30 AM, Kevin O&#39;Gorman <span dir="ltr">&lt;<a href="mailto:kogorman@gmail.com" target="_blank">kogorman@gmail.com</a>&gt;</span> wrote:<br>
</div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5"><div dir="ltr"><div><div>Now I&#39;m befuddled.  I modified the source so that it was easy to try different combinations.  If I sudo it, I can map a 1GiB (1&lt;&lt;30) private anonymous region if I sudo the program.  I cannot find any combination with MAP_HUGETLB that works at all.<br>



<br></div>Here it is in  working form, with an anonymous map and no backing file.  The mmap request is for a single byte, so it works for ordinary users  Try uncommenting the MAP_HUGETLB line, and I&#39;d love to hear about a version that works, even if it&#39;s only for root.<br>



<br></div><div>Since I&#39;m aiming for a huge memory region, I figured HUGETLB would make sense to limit the overhead.  <br></div><div><br></div>============================ CUT HERE ================<br>/**<br> * @file<br>



 * &lt;pre&gt;&quot;Find out the limits on locked memory&quot;<br> * Last Modified: Mon Aug 18 11:21:23 PDT 2014<br> * @author Kevin O&#39;Gorman<br> */<br><br>#define _GNU_SOURCE /* enable some of the mmap flags */<br>#include &lt;unistd.h&gt;<br>



#include &lt;stdlib.h&gt;<br>#include &lt;stdio.h&gt;<br>#include &lt;string.h&gt;<br>#include &lt;sys/mman.h&gt;<br><br>/* for getrlimit(2) */<br>#include &lt;sys/time.h&gt;<br>#include &lt;sys/resource.h&gt;<br><br>/* for open(2) */<br>



#include &lt;sys/types.h&gt;<br>#include &lt;sys/stat.h&gt;<br>#include &lt;sys/fcntl.h&gt;<br><br>int<br>main(int argc, char *argv[])<br>{<br>  void *where;<br>  size_t length = 1;<br>  struct rlimit rlim;<br>  int flags = 0;<br>



  int fd = -1;<br><br>//  fd = open(&quot;solvedtaq&quot;, O_RDWR);  if (fd == -1) perror(&quot;open&quot;);<br>  printf(&quot;fd is %d\n&quot;, fd);<br><br>//  flags |= MAP_HUGETLB;<br>  flags |= MAP_PRIVATE;<br>  flags |= MAP_LOCKED;<br>



  flags |= MAP_ANONYMOUS;<br><br>  if (getrlimit(RLIMIT_MEMLOCK, &amp;rlim) == 0) {<br>    printf(&quot;RLIMIT_MEMLOCK: hard: %lld, soft: %lld\n&quot;, (long long)rlim.rlim_max, (long long)rlim.rlim_cur);<br>  } else {<br>



    perror(&quot;getrlimit failed&quot;);<br>  }<br><br>  where = mmap(NULL, length, PROT_READ | PROT_WRITE, flags, fd, 0);<br>  if (where != MAP_FAILED) {<br>    printf(&quot;Mapped at %p\n&quot;, where);<br>  } else {<br>



    perror(&quot;Mapping failed&quot;);<br>  }<br>  <br>  return EXIT_SUCCESS;<br>}<br>============================ CUT HERE ================<br><br></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Aug 18, 2014 at 10:24 AM, Kevin O&#39;Gorman <span dir="ltr">&lt;<a href="mailto:kogorman@gmail.com" target="_blank">kogorman@gmail.com</a>&gt;</span> wrote:<br>


<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><br><div class="gmail_quote"><div>On Mon, Aug 18, 2014 at 9:46 AM,  <span dir="ltr">&lt;<a href="mailto:Valdis.Kletnieks@vt.edu" target="_blank">Valdis.Kletnieks@vt.edu</a>&gt;</span> wrote:<br>



<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>On Mon, 18 Aug 2014 08:15:12 -0700, &quot;Kevin O&#39;Gorman&quot; said:<br>
<br>
&gt; maybe someone here can help me figure out how to map a big (really big)<br>
&gt; work area and lock it in memory.<br>
<br>
</div>What problem are you trying to solve by doing this?  It usually doesn&#39;t make<br>
sense to lock more than a small chunk in memory - that&#39;s mostly for crypto<br>
programs like PGP that are paranoid about keys being written to swap spaces,<br>
and a *very* few highly performance oriented programs.  99.8% of the time,<br>
the kernel will do a reasonable job of making sure the right pages are in<br>
memory.<br>
<br>
Plus, programmers almost always over-estimate what they *really* need to lock<br>
down.  The end result is that performance ends up being *worse*, because they<br>
lock out more than needed, leaving all the rest of the processes on the system<br>
fighting over a too-small pool of pages.  Really sucks when you actually have<br>
to read  from disk every I/O because you no longer have an in-core file cache :)<br>
<div><br></div></blockquote></div><div>I&#39;m solving a huge alpha-beta search problem, and want a huge cache table tocheck permutations.  That&#39;s why I increased RAM from 8 to 32 GB, and would<br>be happy to use the extra 24GB all for the cache.  (Cache table is like a hash table but with collisions causing replacement rather than chaining).<br>



<br></div><div>Even in my small tests, the cache is giving me speedups in the neighborhood of<br></div><div>a million, so I&#39;m going big. Because this is about speed, I do not *ever* want it to<br></div><div>swap.<br>


</div><div>
<div> <br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>
&gt; I wrote a little test program to try this out, and it fails.  As a regular<br>
&gt; user, perror() tells me some &quot;resource is temporarily unavailable&quot;.  As<br>
&gt; root, it says it &quot;cannot allocate memory&quot;.  I&#39;m only asking for 1 byte.<br>
<br>
</div><div>&gt;   where = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_PRIVATE |<br>
&gt; MAP_ANONYMOUS | MAP_HUGETLB | MAP_LOCKED | MAP_POPULATE, -1, 0);<br>
<br>
</div>Remember that no matter what &quot;length&quot; you pass, even if it&#39;s 1 byte, the<br>
kernel has to allocate at least enough integer pages to cover the request.<br>
You ask for 4100 bytes, it has to allocate 2 pages because 4096 isn&#39;t<br>
quite big enough.<br>
<br>
And if you ask for MAP_HUGETLB, you&#39;re going to get at least 1 page.  Which<br>
is probably 2M in size.  And that can be painful if the default max lockable<br>
memory is less than 2M (at least on my Fedora Rawhide box, it&#39;s all of<br>
64K).  There&#39;s also some interaction danger with MAP_POPULATE and memory<br>
fragmentation.  But both POPULATE and HUGETLB have a high chance of<br>
not playing well with LOCKED.<br>
</blockquote></div></div><br></div><div class="gmail_extra">Good catch.  Indeed, removing MAP_HUGETLB allows the program to succeed.  I&#39;ll try increasing the resource limit, but may have to have to sudo it, do the mmap() and then chusr to my real ID.<br>



</div><div class="gmail_extra"><br></div><div class="gmail_extra">The fd of -1 that some folks thought might be a problem is suggested in the man page for use with MAP_ANONYMOUS.  That flag means I&#39;m not using a backing file, after all.<span><font color="#888888"><br clear="all">



</font></span></div><span><font color="#888888"><div><div class="gmail_extra"><br>-- <br><div dir="ltr">Kevin O&#39;Gorman<br><br>programmer, n. an organism that transmutes caffeine into software.<br><span style="line-height:normal;font-variant:normal;font-size:10pt;font-style:normal;font-weight:normal"><span style="line-height:normal;font-variant:normal;font-size:10pt;font-style:normal;font-weight:normal"></span></span><table cellpadding="0" cellspacing="0" border="0" width="448">



<tbody><tr><td width="25"><img src="cid:XVHDKDFDBURW.IMAGE_60.gif" height="21" width="25"></td>
<td width="423"><span style="FONT-FAMILY:Verdana,Geneva,sans-serif;COLOR:rgb(0,153,0);MARGIN-LEFT:5px;FONT-SIZE:10px">Please consider the environment before printing this email.</span></td></tr></tbody></table><br></div>




</div></div></font></span></div><span><font color="#888888">
</font></span></blockquote></div><span><font color="#888888"><br><br clear="all"><br>-- <br><div dir="ltr">Kevin O&#39;Gorman<br><br>programmer, n. an organism that transmutes caffeine into software.<br><span style="line-height:normal;font-variant:normal;font-size:10pt;font-style:normal;font-weight:normal"><span style="line-height:normal;font-variant:normal;font-size:10pt;font-style:normal;font-weight:normal"></span></span><table border="0" cellpadding="0" cellspacing="0" width="448">


<tbody><tr><td width="25"><img src="cid:XVHDKDFDBURW.IMAGE_60.gif" height="21" width="25"></td>
<td width="423"><span style="FONT-FAMILY:Verdana,Geneva,sans-serif;COLOR:rgb(0,153,0);MARGIN-LEFT:5px;FONT-SIZE:10px">Please consider the environment before printing this email.</span></td></tr></tbody></table><br></div>



</font></span></div>
<br></div></div><div class="">_______________________________________________<br>
Kernelnewbies mailing list<br>
<a href="mailto:Kernelnewbies@kernelnewbies.org" target="_blank">Kernelnewbies@kernelnewbies.org</a><br>
<a href="http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies" target="_blank">http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies</a><br>
<br></div></blockquote></div><br></div>
</blockquote></div><br></div>