{"id":4119,"date":"2014-03-30T07:58:09","date_gmt":"2014-03-30T07:58:09","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/how-to-know-whether-a-copy-on-write-page-is-an-actual-copy-collection-of-common-programming-errors\/"},"modified":"2014-03-30T07:58:09","modified_gmt":"2014-03-30T07:58:09","slug":"how-to-know-whether-a-copy-on-write-page-is-an-actual-copy-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/how-to-know-whether-a-copy-on-write-page-is-an-actual-copy-collection-of-common-programming-errors\/","title":{"rendered":"How to know whether a copy-on-write page is an actual copy?-Collection of common programming errors"},"content":{"rendered":"<p>Good, following the advice of MarkR, I gave it a shot to go through the pagemap and kpageflags interface. Below a quick test to check whether a page is in memory &#8216;SWAPBACKED&#8217; as it is called. One problem remains of course, which is the problem that kpageflags is only accessible to the root.<\/p>\n<pre><code>int main(int argc, char* argv[])\n{\n  unsigned long long pagesize=getpagesize();\n  assert(pagesize&gt;0);\n  int pagecount=4;\n  int filesize=pagesize*pagecount;\n  int fd=open(\"test.dat\", O_RDWR);\n  if (fd0);\n  unsigned long long target=((unsigned long)(void*)M)\/pagesize;\n  err=lseek64(mapfd, target*8, SEEK_SET);\n  assert(err==target*8);\n  assert(sizeof(long long)==8);\n<\/code><\/pre>\n<p>Here we read the page frame numbers for each of our virtual pages<\/p>\n<pre><code>  unsigned long long page2pfn[pagecount];\n  err=read(mapfd,page2pfn,sizeof(long long)*pagecount);\n  if (err0);\n  for(int i = 0 ; i &lt; pagecount; i++)\n    {\n      unsigned long long v2a=page2pfn[i];\n      printf(\"Page: %d, flag %llx\\n\",i,page2pfn[i]);\n\n      if(v2a&amp;0x8000000000000000LL) \/\/ Is the virtual page present ?\n        {\n        unsigned long long pfn=v2a&amp;0x3fffffffffffffLL;\n        err=lseek64(pageflags,pfn*8,SEEK_SET);\n        assert(err==pfn*8);\n        unsigned long long pf;\n        err=read(pageflags,&amp;pf,8);\n        assert(err==8);\n        printf(\"pageflags are %llx with SWAPBACKED: %d\\n\",pf,(pf&gt;&gt;14)&amp;1);\n        }\n    }\n}\n<\/code><\/pre>\n<p>All in all, I&#8217;m not particularly happy with this approach since it requires access to a file that we in general can&#8217;t access and it is bloody complicated (how about a simple kernel call to retrieve the pageflags ?).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Good, following the advice of MarkR, I gave it a shot to go through the pagemap and kpageflags interface. Below a quick test to check whether a page is in memory &#8216;SWAPBACKED&#8217; as it is called. One problem remains of course, which is the problem that kpageflags is only accessible to the root. int main(int [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4119","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4119","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4119"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4119\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4119"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4119"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4119"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}