From mboxrd@z Thu Jan 1 00:00:00 1970 From: Ian Campbell Subject: [PATCH XEN v7 25/29] tools/libs/call: linux: touch newly allocated pages after madvise lockdown Date: Wed, 16 Dec 2015 12:31:32 +0000 Message-ID: <1450269096-27598-26-git-send-email-ian.campbell@citrix.com> References: <1450269007.4053.48.camel@citrix.com> <1450269096-27598-1-git-send-email-ian.campbell@citrix.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: <1450269096-27598-1-git-send-email-ian.campbell@citrix.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: ian.jackson@eu.citrix.com, wei.liu2@citrix.com, xen-devel@lists.xen.org Cc: Ian Campbell List-Id: xen-devel@lists.xenproject.org This avoids a potential issue with a fork after allocation but before madvise. Signed-off-by: Ian Campbell --- v7: New, replacing "tools/libs/call: linux: avoid forking between mmap and madvise". --- tools/libs/call/linux.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/tools/libs/call/linux.c b/tools/libs/call/linux.c index 3641e41..651f380 100644 --- a/tools/libs/call/linux.c +++ b/tools/libs/call/linux.c @@ -88,7 +88,7 @@ void *osdep_alloc_pages(xencall_handle *xcall, size_t npages) { size_t size = npages * PAGE_SIZE; void *p; - int rc, saved_errno; + int rc, i, saved_errno; /* Address returned by mmap is page aligned. */ p = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_LOCKED, -1, 0); @@ -107,6 +107,18 @@ void *osdep_alloc_pages(xencall_handle *xcall, size_t npages) goto out; } + /* + * Touch each page in turn to force them to be un-CoWed, in case a + * fork happened in another thread at an inopportune moment + * above. The madvise() will prevent any subsequent fork calls from + * causing the same problem. + */ + for ( i = 0; i < npages ; i++ ) + { + char *c = (char *)p + (i*PAGE_SIZE); + *c = 0; + } + return p; out: -- 2.1.4