diff --git a/21301-svm-lmsl.patch b/21301-svm-lmsl.patch
index a3c59f3..2fb8966 100644
--- a/21301-svm-lmsl.patch
+++ b/21301-svm-lmsl.patch
@@ -56,7 +56,7 @@ Index: xen-4.0.2-testing/xen/arch/x86/hvm/svm/svm.c
  #define set_segment_register(name, value)  \
      asm volatile ( "movw %%ax ,%%" STR(name) "" : : "a" (value) )
  
-@@ -871,6 +874,29 @@ static int svm_cpu_up(struct cpuinfo_x86
+@@ -864,6 +867,29 @@ static int svm_cpu_up(struct cpuinfo_x86
      /* Initialize core's ASID handling. */
      svm_asid_init(c);
  
diff --git a/22526-ept-get-entry-no-lock.patch b/22526-ept-get-entry-no-lock.patch
deleted file mode 100644
index 0ccc6fd..0000000
--- a/22526-ept-get-entry-no-lock.patch
+++ /dev/null
@@ -1,49 +0,0 @@
-# HG changeset patch
-# User Keir Fraser <keir@xen.org>
-# Date 1292410025 0
-# Node ID 7a5ee380041707177ca9c78e800095d1f5f3d373
-# Parent  01f3b350902385627d1fa9e8cd1c231953e7610c
-ept: Remove lock in ept_get_entry, replace with access-once semantics.
-
-This mirrors the RVI/shadow situation, where p2m read access is
-lockless because it's done in the hardware (linear map of the p2m
-table).
-
-This fixes the original bug (call it bug A) without introducing bug B
-(a deadlock).
-
-Bug A was caused by a race when updating p2m entries: between testing
-if it's valid, and testing if it's populate-on-demand, it may have
-been changed from populate-on-demand to valid.
-
-My original patch simply introduced a lock into ept_get_entry, but
-that caused bug B, caused by circular locking order: p2m_change_type
-[grabs p2m lock] -> set_p2m_entry -> ept_set_entry ->
-ept_set_middle_level -> p2m_alloc [grabs hap lock] write cr4 ->
-hap_update_paging_modes [grabes hap lock] -> hap_update_cr3 ->
-gfn_to_mfn -> ept_get_entry -> [grabs p2m lock]
-
-Signed-off-by: George Dunlap <george.dunlap@eu.citrix.com>
-
---- a/xen/arch/x86/mm/hap/p2m-ept.c
-+++ b/xen/arch/x86/mm/hap/p2m-ept.c
-@@ -395,10 +395,6 @@ static mfn_t ept_get_entry(struct domain
-     int i;
-     int ret = 0;
-     mfn_t mfn = _mfn(INVALID_MFN);
--    int do_locking = !p2m_locked_by_me(d->arch.p2m);
--
--    if ( do_locking )
--        p2m_lock(d->arch.p2m);
- 
-     *t = p2m_mmio_dm;
- 
-@@ -476,8 +472,6 @@ static mfn_t ept_get_entry(struct domain
-     }
- 
- out:
--    if ( do_locking )
--        p2m_unlock(d->arch.p2m);
-     unmap_domain_page(table);
-     return mfn;
- }
diff --git a/22790-svm-resume-migrate-pirqs.patch b/22790-svm-resume-migrate-pirqs.patch
index 9964be3..9c44d0f 100644
--- a/22790-svm-resume-migrate-pirqs.patch
+++ b/22790-svm-resume-migrate-pirqs.patch
@@ -13,7 +13,7 @@ Index: xen-4.0.2-testing/xen/arch/x86/hvm/svm/svm.c
 ===================================================================
 --- xen-4.0.2-testing.orig/xen/arch/x86/hvm/svm/svm.c
 +++ xen-4.0.2-testing/xen/arch/x86/hvm/svm/svm.c
-@@ -704,7 +704,7 @@ static void svm_do_resume(struct vcpu *v
+@@ -697,7 +697,7 @@ static void svm_do_resume(struct vcpu *v
      {
          v->arch.hvm_svm.launch_core = smp_processor_id();
          hvm_migrate_timers(v);
diff --git a/vif-bridge.mtu.patch b/vif-bridge.mtu.patch
new file mode 100644
index 0000000..fe07eee
--- /dev/null
+++ b/vif-bridge.mtu.patch
@@ -0,0 +1,33 @@
+Subject: set mtu from bridge also on vif interface
+References: bug#625394
+
+Apply mtu size from bridge interface also in vif interface.
+This depends on a kernel change which allows arbitrary mtu sizes until
+the frontend driver has connected to the backend driver. Without this
+kernel change, the vif mtu size will be limited to 1500 with this change
+to the vif-bridge script.
+
+(xen-unstable changeset:   22885:005722613063)
+
+Signed-off-by: Olaf Hering <olaf@aepfle.de>
+
+---
+ tools/hotplug/Linux/vif-bridge |    5 +++++
+ 1 file changed, 5 insertions(+)
+
+Index: xen-4.0.2-testing/tools/hotplug/Linux/vif-bridge
+===================================================================
+--- xen-4.0.2-testing.orig/tools/hotplug/Linux/vif-bridge
++++ xen-4.0.2-testing/tools/hotplug/Linux/vif-bridge
+@@ -82,6 +82,11 @@ fi
+ case "$command" in
+     online)
+ 	setup_bridge_port "$vif"
++	mtu="`ip link show $bridge | awk '/mtu/ { print $5 }'`"
++	if [ -n "$mtu" ] && [ "$mtu" -gt 0 ]
++	then
++		ip link set $vif mtu $mtu || :
++	fi
+ 	add_to_bridge "$bridge" "$vif"
+         ;;
+ 
diff --git a/xen-4.0.2-testing-src.tar.bz2 b/xen-4.0.2-testing-src.tar.bz2
index 7e2298e..79bdb05 100644
--- a/xen-4.0.2-testing-src.tar.bz2
+++ b/xen-4.0.2-testing-src.tar.bz2
@@ -1,3 +1,3 @@
 version https://git-lfs.github.com/spec/v1
-oid sha256:b404e22364957ac0dd036cb48c96bcb7b14d6a2db4756622e876eb88ca2d5fda
-size 23223429
+oid sha256:0549d79069b4a53915d2f7c12edd7c386c9cf07ba3f2cfeaef3e79c3c7c67a87
+size 23224207
diff --git a/xen-config.diff b/xen-config.diff
index f44d48b..63b8349 100644
--- a/xen-config.diff
+++ b/xen-config.diff
@@ -9,7 +9,7 @@ Index: xen-4.0.2-testing/Config.mk
 -CONFIG_QEMU ?= $(QEMU_REMOTE)
 +CONFIG_QEMU ?= ioemu-qemu-xen
  
- QEMU_TAG := xen-4.0.2-rc1
+ QEMU_TAG := xen-4.0.2-rc2
  #QEMU_TAG ?= 6d5b7ee3acfe8cc10681d2583a38398f7470ec2a
 @@ -181,9 +181,9 @@ CONFIG_OCAML_XENSTORED ?= n
  # Optional components
@@ -27,17 +27,13 @@ Index: xen-4.0.2-testing/tools/libxc/Makefile
 ===================================================================
 --- xen-4.0.2-testing.orig/tools/libxc/Makefile
 +++ xen-4.0.2-testing/tools/libxc/Makefile
-@@ -169,10 +169,10 @@ zlib-options = $(shell					\
- 	 fi) | grep $(1))
- endif
+@@ -174,7 +174,8 @@ xc_dom_bzimageloader.opic: CFLAGS += $(c
  
--xc_dom_bzimageloader.o: CFLAGS += $(call zlib-options,D)
--xc_dom_bzimageloader.opic: CFLAGS += $(call zlib-options,D)
-+#xc_dom_bzimageloader.o: CFLAGS += $(call zlib-options,D)
-+#xc_dom_bzimageloader.opic: CFLAGS += $(call zlib-options,D)
- 
--libxenguest.so.$(MAJOR).$(MINOR): LDFLAGS += $(call zlib-options,l)
-+#libxenguest.so.$(MAJOR).$(MINOR): LDFLAGS += $(call zlib-options,l)
+ libxenguest.so.$(MAJOR).$(MINOR): LDFLAGS += $(call zlib-options,l)
  libxenguest.so.$(MAJOR).$(MINOR): $(GUEST_PIC_OBJS) libxenctrl.so
- 	$(CC) $(CFLAGS) $(LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenguest.so.$(MAJOR) $(SHLIB_CFLAGS) -o $@ $(GUEST_PIC_OBJS) -lz -lxenctrl $(PTHREAD_LIBS)
+-	$(CC) $(CFLAGS) $(LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenguest.so.$(MAJOR) $(SHLIB_CFLAGS) -o $@ $(GUEST_PIC_OBJS) -lz -lxenctrl $(PTHREAD_LIBS)
++	$(CC) $(CFLAGS) $(LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenguest.so.$(MAJOR) $(SHLIB_CFLAGS) -o $@ $(GUEST_PIC_OBJS) -lz -lxenctrl $(PTHREAD_LIBS) $(call zlib-options,l)
++
+ 
+ -include $(DEPS)
  
diff --git a/xen.changes b/xen.changes
index 58fcacb..3483526 100644
--- a/xen.changes
+++ b/xen.changes
@@ -1,3 +1,51 @@
+-------------------------------------------------------------------
+Fri Feb 11 16:25:51 MST 2011 - carnold@novell.com
+
+- Update to Xen 4.0.2 rc2, changeset 21452 
+
+-------------------------------------------------------------------
+Fri Feb 11 11:50:20 MST 2011 - carnold@novell.com
+
+- Enable support for kernel decompression for gzip, bzip2, and LZMA 
+  so that kernels compressed with any of these methods can be 
+  launched. 
+
+-------------------------------------------------------------------
+Mon Feb  7 13:25:04 CET 2011 - ohering@suse.de
+
+- update xenalyze, more 64bit fixes
+
+-------------------------------------------------------------------
+Mon Feb  7 13:17:12 CET 2011 - ohering@suse.de
+
+- allocate xentrace buffer metadata based on requested tbuf_size
+  xentrace.dynamic_sized_tbuf.patch
+
+-------------------------------------------------------------------
+Mon Feb  7 13:07:43 CET 2011 - ohering@suse.de
+
+- fate#310510 - fix xenpaging
+  xenpaging.runtime_mru_size.patch
+  - specify policy mru size at runtime
+  xenpaging.no_domain_id.patch
+  - reduce memory usage in pager
+
+-------------------------------------------------------------------
+Mon Feb  7 13:01:03 CET 2011 - ohering@suse.de
+
+- bnc#625394 - set vif mtu from bridge mtu if kernel supports it
+  vif-bridge.mtu.patch
+
+-------------------------------------------------------------------
+Sun Feb  6 15:46:43 CET 2011 - ohering@suse.de
+
+- fate#310510 - fix xenpaging
+  xenpaging.autostart_delay.patch
+  - decouple create/destroycreateXenPaging from _create/_removeDevices
+  - change xenpaging variable from int to str
+  - init xenpaging variable to 0 if xenpaging is not in config file
+    to avoid string None coming from sxp file
+
 -------------------------------------------------------------------
 Tue Feb  1 16:01:45 MST 2011 - carnold@novell.com
 
diff --git a/xen.spec b/xen.spec
index b36dfde..dff9558 100644
--- a/xen.spec
+++ b/xen.spec
@@ -1,5 +1,5 @@
 #
-# spec file for package xen
+# spec file for package xen (Version 4.0.2_02)
 #
 # Copyright (c) 2011 SUSE LINUX Products GmbH, Nuernberg, Germany.
 #
@@ -22,39 +22,41 @@ Name:           xen
 ExclusiveArch:  %ix86 x86_64
 %define xvers 4.0
 %define xvermaj 4
-%define changeset 21443
+%define changeset 21452
 %define xen_build_dir xen-4.0.2-testing
 %define with_kmp 1
 %define with_stubdom 1
-BuildRequires:  LibVNCServer-devel
-BuildRequires:  SDL-devel
-BuildRequires:  automake
-BuildRequires:  bin86
-BuildRequires:  curl-devel
-BuildRequires:  dev86
-BuildRequires:  graphviz
-BuildRequires:  latex2html
-BuildRequires:  libjpeg-devel
-BuildRequires:  libxml2-devel
-BuildRequires:  ncurses-devel
-BuildRequires:  openssl
-BuildRequires:  openssl-devel
-BuildRequires:  pciutils-devel
-BuildRequires:  python-devel
-BuildRequires:  texinfo
-BuildRequires:  transfig
+BuildRequires: LibVNCServer-devel
+BuildRequires: SDL-devel
+BuildRequires: automake
+BuildRequires: bin86
+BuildRequires: curl-devel
+BuildRequires: dev86
+BuildRequires: graphviz
+BuildRequires: latex2html
+BuildRequires: libjpeg-devel
+BuildRequires: libxml2-devel
+BuildRequires: ncurses-devel
+BuildRequires: openssl
+BuildRequires: openssl-devel
+BuildRequires: pciutils-devel
+BuildRequires: python-devel
+BuildRequires: texinfo
+BuildRequires: transfig
+BuildRequires: libbz2-devel
+BuildRequires: xz-devel
 %if %suse_version <= 1110
-BuildRequires:  pmtools
+BuildRequires: pmtools
 %else
-BuildRequires:  acpica
+BuildRequires: acpica
 %endif
 %if %suse_version >= 1030
-BuildRequires:  texlive
-BuildRequires:  texlive-latex
+BuildRequires: texlive
+BuildRequires: texlive-latex
 %else
-BuildRequires:  te_ams
-BuildRequires:  te_latex
-BuildRequires:  tetex
+BuildRequires: te_ams 
+BuildRequires: te_latex 
+BuildRequires: tetex
 %endif
 %ifarch x86_64
 BuildRequires:  glibc-32bit glibc-devel-32bit
@@ -70,7 +72,7 @@ BuildRequires:  glibc-devel
 %if %{?with_kmp}0
 BuildRequires:  kernel-source kernel-syms module-init-tools xorg-x11
 %endif
-Version:        4.0.2_01
+Version:        4.0.2_02
 Release:        1
 License:        GPLv2+
 Group:          System/Kernel
@@ -141,21 +143,20 @@ Patch27:        22431-p2m-remove-bug-check.patch
 Patch28:        22475-x2apic-cleanup.patch
 Patch29:        22484-vlapic-tmcct-periodic.patch
 Patch30:        22504-iommu-dom0-holes.patch
-Patch31:        22526-ept-get-entry-no-lock.patch
-Patch32:        22533-x86-32bit-apicid.patch
-Patch33:        22534-x86-max-local-apic.patch
-Patch34:        22535-x2apic-preenabled.patch
-Patch35:        22645-amd-flush-filter.patch
-Patch36:        22693-fam10-mmio-conf-base-protect.patch
-Patch37:        22694-x86_64-no-weak.patch
-Patch38:        22707-x2apic-preenabled-check.patch
-Patch39:        22708-xenctx-misc.patch
-Patch40:        22744-ept-pod-locking.patch
-Patch41:        22749-vtd-workarounds.patch
-Patch42:        22777-vtd-ats-fixes.patch
-Patch43:        22781-pod-hap-logdirty.patch
-Patch44:        22789-i386-no-x2apic.patch
-Patch45:        22790-svm-resume-migrate-pirqs.patch
+Patch31:        22533-x86-32bit-apicid.patch
+Patch32:        22534-x86-max-local-apic.patch
+Patch33:        22535-x2apic-preenabled.patch
+Patch34:        22645-amd-flush-filter.patch
+Patch35:        22693-fam10-mmio-conf-base-protect.patch
+Patch36:        22694-x86_64-no-weak.patch
+Patch37:        22707-x2apic-preenabled-check.patch
+Patch38:        22708-xenctx-misc.patch
+Patch39:        22744-ept-pod-locking.patch
+Patch40:        22749-vtd-workarounds.patch
+Patch41:        22777-vtd-ats-fixes.patch
+Patch42:        22781-pod-hap-logdirty.patch
+Patch43:        22789-i386-no-x2apic.patch
+Patch44:        22790-svm-resume-migrate-pirqs.patch
 # Upstream qemu patches
 Patch200:       7410-qemu-alt-gr.patch
 Patch201:       7426-xenfb-depth.patch
@@ -223,7 +224,7 @@ Patch373:       usb-list.patch
 Patch374:       xend-devid-or-name.patch
 Patch375:       22326-cpu-pools-numa-placement.patch
 Patch376:       20158-revert.patch
-Patch377:       suspend_evtchn_lock.patch
+Patch377:	suspend_evtchn_lock.patch
 # Patches for snapshot support
 Patch400:       snapshot-ioemu-save.patch
 Patch401:       snapshot-ioemu-restore.patch
@@ -259,6 +260,8 @@ Patch439:       minios-fixups.patch
 Patch440:       bdrv_default_rwflag.patch
 Patch441:       blktap2.patch
 Patch442:       xen-minimum-restart-time.patch
+Patch443:       vif-bridge.mtu.patch
+Patch444:       xentrace.dynamic_sized_tbuf.patch
 # Jim's domain lock patch
 Patch450:       xend-domain-lock.patch
 # Hypervisor and PV driver Patches
@@ -282,35 +285,35 @@ Patch700:       hv_extid_compatibility.patch
 # Build patch
 Patch999:       tmp_build.patch
 # FATE 310510
-Patch902:       hotplug-block-losetup-a.patch
-Patch10001:     xenpaging.tools_xenpaging_cleanup.patch
-Patch10002:     xenpaging.pageout_policy.patch
-Patch10003:     xenpaging.get_paged_frame.patch
-Patch10004:     xenpaging.makefile.patch
-Patch10010:     xenpaging.policy_linear.patch
-Patch10011:     xenpaging.pagefile.patch
-Patch10012:     xenpaging.xenpaging_init.patch
-Patch10013:     xenpaging.mem_paging_tool_qemu_flush_cache.patch
-Patch10014:     xenpaging.machine_to_phys_mapping.patch
-Patch10015:     xenpaging.populate_only_if_paged.patch
-Patch10017:     xenpaging.autostart.patch
-Patch10018:     xenpaging.signal_handling.patch
-Patch10019:     xenpaging.MRU_SIZE.patch
-Patch10020:     xenpaging.guest_remove_page.patch
-Patch10021:     xenpaging.mem_event_check_ring-free_requests.patch
-Patch10022:     xenpaging.blacklist.patch
-Patch10023:     xenpaging.autostart_delay.patch
-Patch10024:     xenpaging.page_already_populated.patch
-Patch10025:     xenpaging.notify_policy_only_once.patch
-Patch10026:     xenpaging.num_pages_equal_max_pages.patch
-Patch10027:     xenpaging.p2m_mem_paging_populate_if_p2m_ram_paged.patch
-Patch10028:     xenpaging.HVMCOPY_gfn_paged_out.patch
-Patch10029:     xenpaging.optimize_p2m_mem_paging_populate.patch
-Patch10030:     xenpaging.paging_prep_enomem.patch
-Patch10031:     xenpaging.print-arguments.patch
-Patch10040:     xenpaging.doc.patch
-# xenalyze
-Patch20000:     xenalyze.64bit.patch
+Patch902: hotplug-block-losetup-a.patch
+Patch10001: xenpaging.tools_xenpaging_cleanup.patch
+Patch10002: xenpaging.pageout_policy.patch
+Patch10003: xenpaging.get_paged_frame.patch
+Patch10004: xenpaging.makefile.patch
+Patch10010: xenpaging.policy_linear.patch
+Patch10011: xenpaging.pagefile.patch
+Patch10012: xenpaging.xenpaging_init.patch
+Patch10013: xenpaging.mem_paging_tool_qemu_flush_cache.patch
+Patch10014: xenpaging.machine_to_phys_mapping.patch
+Patch10015: xenpaging.populate_only_if_paged.patch
+Patch10017: xenpaging.autostart.patch
+Patch10018: xenpaging.signal_handling.patch
+Patch10019: xenpaging.MRU_SIZE.patch
+Patch10020: xenpaging.guest_remove_page.patch
+Patch10021: xenpaging.mem_event_check_ring-free_requests.patch
+Patch10022: xenpaging.blacklist.patch
+Patch10023: xenpaging.autostart_delay.patch
+Patch10024: xenpaging.page_already_populated.patch
+Patch10025: xenpaging.notify_policy_only_once.patch
+Patch10026: xenpaging.num_pages_equal_max_pages.patch
+Patch10027: xenpaging.p2m_mem_paging_populate_if_p2m_ram_paged.patch
+Patch10028: xenpaging.HVMCOPY_gfn_paged_out.patch
+Patch10029: xenpaging.optimize_p2m_mem_paging_populate.patch
+Patch10030: xenpaging.paging_prep_enomem.patch
+Patch10031: xenpaging.print-arguments.patch
+Patch10032: xenpaging.no_domain_id.patch
+Patch10033: xenpaging.runtime_mru_size.patch
+Patch10040: xenpaging.doc.patch
 Url:            http://www.cl.cam.ac.uk/Research/SRG/netos/xen/
 BuildRoot:      %{_tmppath}/%{name}-%{version}-build
 #%define pysite %(python -c "import distutils.sysconfig; print distutils.sysconfig.get_python_lib()")
@@ -689,7 +692,6 @@ tar xfj %{SOURCE2} -C $RPM_BUILD_DIR/%{xen_build_dir}/tools
 %patch42 -p1
 %patch43 -p1
 %patch44 -p1
-%patch45 -p1
 %patch200 -p1
 %patch201 -p1
 %patch202 -p1
@@ -787,6 +789,8 @@ tar xfj %{SOURCE2} -C $RPM_BUILD_DIR/%{xen_build_dir}/tools
 %patch440 -p1
 %patch441 -p1
 %patch442 -p1
+%patch443 -p1
+%patch444 -p1
 %patch450 -p1
 %patch500 -p1
 %patch501 -p1
@@ -832,9 +836,10 @@ tar xfj %{SOURCE2} -C $RPM_BUILD_DIR/%{xen_build_dir}/tools
 %patch10029 -p1
 %patch10030 -p1
 %patch10031 -p1
+%patch10032 -p1
+%patch10033 -p1
 %patch10040 -p1
-#
-%patch20000 -p1
+
 
 %build
 XEN_EXTRAVERSION=%version-%release
diff --git a/xenalyze.64bit.patch b/xenalyze.64bit.patch
deleted file mode 100644
index 094ea11..0000000
--- a/xenalyze.64bit.patch
+++ /dev/null
@@ -1,30 +0,0 @@
-Are these unsigned long lines really unsigned int, u32?
-
-grep -En 'unsigned[[:blank:]]+long' xenalyze.hg/xenalyze.c | \
-grep -Ev 'unsigned[[:blank:]]+long[[:blank:]]+long'
-
-Signed-off-by: Olaf Hering <olaf@aepfle.de>
-
----
- xenalyze.hg/xenalyze.c |    4 ++--
- 1 file changed, 2 insertions(+), 2 deletions(-)
-
-Index: xen-4.0.2-testing/xenalyze.hg/xenalyze.c
-===================================================================
---- xen-4.0.2-testing.orig/xenalyze.hg/xenalyze.c
-+++ xen-4.0.2-testing/xenalyze.hg/xenalyze.c
-@@ -3588,12 +3588,12 @@ void hvm_mmio_assist_process(struct reco
-     if(mevt.x64) {
-         e->gpa = r->x64.gpa;
-         e->data = r->x64.data;
--        if(ri->extra_words*(sizeof(unsigned long))==sizeof(r->x64))
-+        if(ri->extra_words*(sizeof(unsigned))==sizeof(r->x64))
-             e->mmio_data_valid=1;
-     } else {
-         e->gpa = r->x32.gpa;
-         e->data = r->x32.data;
--        if(ri->extra_words*(sizeof(unsigned long))==sizeof(r->x32))
-+        if(ri->extra_words*(sizeof(unsigned))==sizeof(r->x32))
-             e->mmio_data_valid=1;
-     }
- 
diff --git a/xenalyze.hg.tar.bz2 b/xenalyze.hg.tar.bz2
index a8e8ca9..cc5f911 100644
--- a/xenalyze.hg.tar.bz2
+++ b/xenalyze.hg.tar.bz2
@@ -1,3 +1,3 @@
 version https://git-lfs.github.com/spec/v1
-oid sha256:a3afdfed61f88250bf84f0fe89576cfb54110fa2c4a78652a1aa7a2964b10273
-size 117804
+oid sha256:870743ab79d34e066a9b090e6555e9fe626165e9d7de28b6a6af136a2b8c4095
+size 118364
diff --git a/xenpaging.autostart.patch b/xenpaging.autostart.patch
index 8039177..8f5cff3 100644
--- a/xenpaging.autostart.patch
+++ b/xenpaging.autostart.patch
@@ -9,6 +9,11 @@ TODO: parse config values like 42K, 42M, 42G, 42%
 Signed-off-by: Olaf Hering <olaf@aepfle.de>
 
 ---
+v3:
+  decouple create/destroycreateXenPaging from _create/_removeDevices
+  init xenpaging variable to 0 if xenpaging is not in config file to
+  avoid string None coming from sxp file
+
 v2:
   unlink logfile instead of truncating it.
   allows hardlinking for further inspection
@@ -17,11 +22,11 @@ v2:
  tools/python/README.XendConfig          |    1 
  tools/python/README.sxpcfg              |    1 
  tools/python/xen/xend/XendConfig.py     |    3 +
- tools/python/xen/xend/XendDomainInfo.py |    6 ++
- tools/python/xen/xend/image.py          |   91 ++++++++++++++++++++++++++++++++
+ tools/python/xen/xend/XendDomainInfo.py |   12 ++++
+ tools/python/xen/xend/image.py          |   84 ++++++++++++++++++++++++++++++++
  tools/python/xen/xm/create.py           |    5 +
  tools/python/xen/xm/xenapi_create.py    |    1 
- 8 files changed, 111 insertions(+)
+ 8 files changed, 110 insertions(+)
 
 Index: xen-4.0.2-testing/tools/examples/xmexample.hvm
 ===================================================================
@@ -69,7 +74,7 @@ Index: xen-4.0.2-testing/tools/python/xen/xend/XendConfig.py
      'apic': int,
      'boot': str,
      'device_model': str,
-+    'xenpaging': int,
++    'xenpaging': str,
      'loader': str,
      'display' : str,
      'fda': str,
@@ -78,7 +83,7 @@ Index: xen-4.0.2-testing/tools/python/xen/xend/XendConfig.py
  
          if self.is_hvm():
 +            if 'xenpaging' not in self['platform']:
-+                self['platform']['xenpaging'] = None
++                self['platform']['xenpaging'] = "0"
              if 'timer_mode' not in self['platform']:
                  self['platform']['timer_mode'] = 1
              if 'extid' in self['platform'] and int(self['platform']['extid']) == 1:
@@ -86,26 +91,60 @@ Index: xen-4.0.2-testing/tools/python/xen/xend/XendDomainInfo.py
 ===================================================================
 --- xen-4.0.2-testing.orig/tools/python/xen/xend/XendDomainInfo.py
 +++ xen-4.0.2-testing/tools/python/xen/xend/XendDomainInfo.py
-@@ -2441,6 +2441,7 @@ class XendDomainInfo:
+@@ -2293,6 +2293,8 @@ class XendDomainInfo:
+                  self.info['name_label'], self.domid, self.info['uuid'],
+                  new_name, new_uuid)
+         self._unwatchVm()
++        if self.image:
++            self.image.destroyXenPaging()
+         self._releaseDevices()
+         # Remove existing vm node in xenstore
+         self._removeVm()
+@@ -2954,6 +2956,9 @@ class XendDomainInfo:
  
-         if self.image:
-             self.image.createDeviceModel(resume)
-+            self.image.createXenPaging()
+             self._createDevices()
  
-         #if have pass-through devs, need the virtual pci slots info from qemu
-         self.pci_device_configure_boot()
-@@ -2453,6 +2454,11 @@ class XendDomainInfo:
-                 self.image.destroyDeviceModel()
-             except Exception, e:
-                 log.exception("Device model destroy failed %s" % str(e))
-+            try:
-+                log.debug("stopping xenpaging")
++            if self.image:
++                self.image.createXenPaging()
++
+             self.image.cleanupTmpImages()
+ 
+             self.info['start_time'] = time.time()
+@@ -2978,6 +2983,8 @@ class XendDomainInfo:
+         self.refresh_shutdown_lock.acquire()
+         try:
+             self.unwatchShutdown()
++            if self.image:
 +                self.image.destroyXenPaging()
-+            except Exception, e:
-+                log.exception("stopping xenpaging failed %s" % str(e))
-         else:
-             log.debug("No device model")
+             self._releaseDevices()
+             bootloader_tidy(self)
  
+@@ -3062,6 +3069,7 @@ class XendDomainInfo:
+         self.image = image.create(self, self.info)
+         if self.image:
+             self._createDevices(True)
++            self.image.createXenPaging()
+         self._storeDomDetails()
+         self._registerWatches()
+         self.refreshShutdown()
+@@ -3202,6 +3210,8 @@ class XendDomainInfo:
+             # could also fetch a parsed note from xenstore
+             fast = self.info.get_notes().get('SUSPEND_CANCEL') and 1 or 0
+             if not fast:
++                if self.image:
++                    self.image.destroyXenPaging()
+                 self._releaseDevices()
+                 self.testDeviceComplete()
+                 self.testvifsComplete()
+@@ -3217,6 +3227,8 @@ class XendDomainInfo:
+                 self._storeDomDetails()
+ 
+                 self._createDevices()
++                if self.image:
++                    self.image.createXenPaging()
+                 log.debug("XendDomainInfo.resumeDomain: devices created")
+ 
+             xc.domain_resume(self.domid, fast)
 Index: xen-4.0.2-testing/tools/python/xen/xend/image.py
 ===================================================================
 --- xen-4.0.2-testing.orig/tools/python/xen/xend/image.py
@@ -125,14 +164,12 @@ Index: xen-4.0.2-testing/tools/python/xen/xend/image.py
          rtc_timeoffset = int(vmConfig['platform'].get('rtc_timeoffset', 0))
          if int(vmConfig['platform'].get('localtime', 0)):
              if time.localtime(time.time())[8]:
-@@ -392,6 +394,95 @@ class ImageHandler:
+@@ -392,6 +394,88 @@ class ImageHandler:
          sentinel_fifos_inuse[sentinel_path_fifo] = 1
          self.sentinel_path_fifo = sentinel_path_fifo
  
 +    def createXenPaging(self):
-+        if self.xenpaging is None:
-+            return
-+        if self.xenpaging == 0:
++        if self.xenpaging == "0":
 +            return
 +        if self.xenpaging_pid:
 +            return
@@ -160,11 +197,6 @@ Index: xen-4.0.2-testing/tools/python/xen/xend/image.py
 +                os.dup2(logfd, 1)
 +                os.dup2(logfd, 2)
 +                try:
-+                    os.mkdir(xenpaging_dir)
-+                except:
-+                    log.info("mkdir %s failed" % xenpaging_dir)
-+                    pass
-+                try:
 +                    os.chdir(xenpaging_dir)
 +                except:
 +                    log.warn("chdir %s failed" % xenpaging_dir)
@@ -186,7 +218,7 @@ Index: xen-4.0.2-testing/tools/python/xen/xend/image.py
 +            os.close(logfd)
 +
 +    def destroyXenPaging(self):
-+        if self.xenpaging is None:
++        if self.xenpaging == "0":
 +            return
 +        if self.xenpaging_pid:
 +            try:
@@ -230,7 +262,7 @@ Index: xen-4.0.2-testing/tools/python/xen/xm/create.py
            use="Set the path of the root NFS directory.")
  
 +gopts.var('xenpaging', val='NUM',
-+          fn=set_int, default=None,
++          fn=set_value, default='0',
 +          use="Number of pages to swap.")
 +
  gopts.var('device_model', val='FILE',
diff --git a/xenpaging.autostart_delay.patch b/xenpaging.autostart_delay.patch
index 88ff850..81ca91b 100644
--- a/xenpaging.autostart_delay.patch
+++ b/xenpaging.autostart_delay.patch
@@ -22,7 +22,7 @@ Index: xen-4.0.2-testing/tools/python/xen/xend/image.py
  
          self.device_model = vmConfig['platform'].get('device_model')
          self.xenpaging = vmConfig['platform'].get('xenpaging')
-+        if self.xenpaging:
++        if self.xenpaging is not "0":
 +            self.xenpaging_delay = xstransact.Read("/local/domain/0/xenpaging/%s/xenpaging_delay" % self.vm.info['name_label'])
 +            if self.xenpaging_delay == None:
 +                log.warn("XXX creating /local/domain/0/xenpaging/%s" % self.vm.info['name_label'])
@@ -38,7 +38,7 @@ Index: xen-4.0.2-testing/tools/python/xen/xend/image.py
  
          self.display = vmConfig['platform'].get('display')
          self.xauthority = vmConfig['platform'].get('xauthority')
-@@ -401,6 +414,17 @@ class ImageHandler:
+@@ -399,6 +412,17 @@ class ImageHandler:
              return
          if self.xenpaging_pid:
              return
@@ -56,7 +56,7 @@ Index: xen-4.0.2-testing/tools/python/xen/xend/image.py
          xenpaging_bin = auxbin.pathTo("xenpaging")
          args = [xenpaging_bin]
          args = args + ([ "%d" % self.vm.getDomid()])
-@@ -434,6 +458,9 @@ class ImageHandler:
+@@ -427,6 +451,9 @@ class ImageHandler:
                  except:
                      log.warn("chdir %s failed" % xenpaging_dir)
                  try:
@@ -66,7 +66,7 @@ Index: xen-4.0.2-testing/tools/python/xen/xend/image.py
                      log.info("starting %s" % args)
                      os.execve(xenpaging_bin, args, env)
                  except Exception, e:
-@@ -449,10 +476,16 @@ class ImageHandler:
+@@ -442,10 +469,16 @@ class ImageHandler:
              self.xenpaging_pid = xenpaging_pid
              os.close(null)
              os.close(logfd)
@@ -76,7 +76,7 @@ Index: xen-4.0.2-testing/tools/python/xen/xend/image.py
 +                log.warn("waiting for xenpaging done.")
  
      def destroyXenPaging(self):
-         if self.xenpaging is None:
+         if self.xenpaging == "0":
              return
 +        # FIXME find correct place for guest shutdown or crash
 +        #xstransact.Remove("/local/domain/0/xenpaging/%s" % self.vm.info['name_label'])
diff --git a/xenpaging.doc.patch b/xenpaging.doc.patch
index 51cd6d2..dd3a287 100644
--- a/xenpaging.doc.patch
+++ b/xenpaging.doc.patch
@@ -57,7 +57,7 @@ Index: xen-4.0.2-testing/docs/misc/xenpaging.txt
 +
 +Todo:
 +- implement stopping of xenpaging
-+- implement/test live migration
++- implement live migration
 +
 +
 +# vim: tw=72
diff --git a/xenpaging.guest_remove_page.patch b/xenpaging.guest_remove_page.patch
index 896c341..e4b00d6 100644
--- a/xenpaging.guest_remove_page.patch
+++ b/xenpaging.guest_remove_page.patch
@@ -3,6 +3,8 @@ Subject: xenpaging: drop paged pages in guest_remove_page
 Simply drop paged-pages in guest_remove_page(), and notify xenpaging to
 drop reference to the gfn.
 
+(xen-unstable changeset:   22705:c5b42971234a)
+
 Signed-off-by: Olaf Hering <olaf@aepfle.de>
 ---
 v2:
diff --git a/xenpaging.no_domain_id.patch b/xenpaging.no_domain_id.patch
new file mode 100644
index 0000000..9fa9034
--- /dev/null
+++ b/xenpaging.no_domain_id.patch
@@ -0,0 +1,184 @@
+Subject: xenpaging: remove domain_id and mfn from struct xenpaging_victim
+
+Remove unused mfn member from struct xenpaging_victim.
+Remove domain_id from struct xenpaging_victim and use the one from
+paging->mem_event where needed. Its not used in the policy.
+This saves 4MB runtime data with a 1GB pagefile.
+
+(xen-unstable changeset:   22699:f84ae053b7da)
+
+Signed-off-by: Olaf Hering <olaf@aepfle.de>
+
+---
+ tools/xenpaging/policy.h         |    7 +++----
+ tools/xenpaging/policy_default.c |   10 +++-------
+ tools/xenpaging/xenpaging.c      |   21 ++++++++++-----------
+ tools/xenpaging/xenpaging.h      |    4 ----
+ 4 files changed, 16 insertions(+), 26 deletions(-)
+
+Index: xen-4.0.2-testing/tools/xenpaging/policy.h
+===================================================================
+--- xen-4.0.2-testing.orig/tools/xenpaging/policy.h
++++ xen-4.0.2-testing/tools/xenpaging/policy.h
+@@ -29,10 +29,9 @@
+ 
+ 
+ int policy_init(xenpaging_t *paging);
+-int policy_choose_victim(xenpaging_t *paging, domid_t domain_id,
+-                         xenpaging_victim_t *victim);
+-void policy_notify_paged_out(domid_t domain_id, unsigned long gfn);
+-void policy_notify_paged_in(domid_t domain_id, unsigned long gfn);
++int policy_choose_victim(xenpaging_t *paging, xenpaging_victim_t *victim);
++void policy_notify_paged_out(unsigned long gfn);
++void policy_notify_paged_in(unsigned long gfn);
+ 
+ #endif // __XEN_PAGING_POLICY_H__
+ 
+Index: xen-4.0.2-testing/tools/xenpaging/policy_default.c
+===================================================================
+--- xen-4.0.2-testing.orig/tools/xenpaging/policy_default.c
++++ xen-4.0.2-testing/tools/xenpaging/policy_default.c
+@@ -68,15 +68,11 @@ int policy_init(xenpaging_t *paging)
+     return rc;
+ }
+ 
+-int policy_choose_victim(xenpaging_t *paging, domid_t domain_id,
+-                         xenpaging_victim_t *victim)
++int policy_choose_victim(xenpaging_t *paging, xenpaging_victim_t *victim)
+ {
+     unsigned long wrap = current_gfn;
+     ASSERT(victim != NULL);
+ 
+-    /* Domain to pick on */
+-    victim->domain_id = domain_id;
+-
+     do
+     {
+         current_gfn++;
+@@ -96,13 +92,13 @@ int policy_choose_victim(xenpaging_t *pa
+     return 0;
+ }
+ 
+-void policy_notify_paged_out(domid_t domain_id, unsigned long gfn)
++void policy_notify_paged_out(unsigned long gfn)
+ {
+     set_bit(gfn, bitmap);
+     clear_bit(gfn, unconsumed);
+ }
+ 
+-void policy_notify_paged_in(domid_t domain_id, unsigned long gfn)
++void policy_notify_paged_in(unsigned long gfn)
+ {
+     unsigned long old_gfn = mru[i_mru & (MRU_SIZE - 1)];
+ 
+Index: xen-4.0.2-testing/tools/xenpaging/xenpaging.c
+===================================================================
+--- xen-4.0.2-testing.orig/tools/xenpaging/xenpaging.c
++++ xen-4.0.2-testing/tools/xenpaging/xenpaging.c
+@@ -344,7 +344,7 @@ int xenpaging_evict_page(xenpaging_t *pa
+     /* Map page */
+     gfn = victim->gfn;
+     ret = -EFAULT;
+-    page = xc_map_foreign_pages(paging->xc_handle, victim->domain_id,
++    page = xc_map_foreign_pages(paging->xc_handle, paging->mem_event.domain_id,
+                                 PROT_READ | PROT_WRITE, &gfn, 1);
+     if ( page == NULL )
+     {
+@@ -376,7 +376,7 @@ int xenpaging_evict_page(xenpaging_t *pa
+     }
+ 
+     /* Notify policy of page being paged out */
+-    policy_notify_paged_out(paging->mem_event.domain_id, victim->gfn);
++    policy_notify_paged_out(victim->gfn);
+ 
+  out:
+     return ret;
+@@ -393,7 +393,7 @@ static int xenpaging_resume_page(xenpagi
+ 
+     /* Notify policy of page being paged in */
+     if ( notify_policy )
+-        policy_notify_paged_in(paging->mem_event.domain_id, rsp->gfn);
++        policy_notify_paged_in(rsp->gfn);
+ 
+     /* Tell Xen page is ready */
+     ret = xc_mem_paging_resume(paging->xc_handle, paging->mem_event.domain_id,
+@@ -459,7 +459,7 @@ static int xenpaging_populate_page(
+     return ret;
+ }
+ 
+-static int evict_victim(xenpaging_t *paging, domid_t domain_id,
++static int evict_victim(xenpaging_t *paging,
+                         xenpaging_victim_t *victim, int fd, int i)
+ {
+     int j = 0;
+@@ -467,7 +467,7 @@ static int evict_victim(xenpaging_t *pag
+ 
+     do
+     {
+-        ret = policy_choose_victim(paging, domain_id, victim);
++        ret = policy_choose_victim(paging, victim);
+         if ( ret != 0 )
+         {
+             if ( ret != -ENOSPC )
+@@ -487,7 +487,7 @@ static int evict_victim(xenpaging_t *pag
+         else
+         {
+             if ( j++ % 1000 == 0 )
+-                if ( xc_mem_paging_flush_ioemu_cache(domain_id) )
++                if ( xc_mem_paging_flush_ioemu_cache(paging->mem_event.domain_id) )
+                     ERROR("Error flushing ioemu cache");
+         }
+     }
+@@ -570,7 +570,7 @@ int main(int argc, char *argv[])
+     memset(victims, 0, sizeof(xenpaging_victim_t) * num_pages);
+     for ( i = 0; i < num_pages; i++ )
+     {
+-        rc = evict_victim(paging, domain_id, &victims[i], fd, i);
++        rc = evict_victim(paging, &victims[i], fd, i);
+         if ( rc == -ENOSPC )
+             break;
+         if ( rc == -EINTR )
+@@ -611,8 +611,7 @@ int main(int argc, char *argv[])
+                 /* Find where in the paging file to read from */
+                 for ( i = 0; i < num_pages; i++ )
+                 {
+-                    if ( (victims[i].domain_id == paging->mem_event.domain_id) &&
+-                         (victims[i].gfn == req.gfn) )
++                    if ( victims[i].gfn == req.gfn )
+                         break;
+                 }
+     
+@@ -625,6 +624,7 @@ int main(int argc, char *argv[])
+                 if ( req.flags & MEM_EVENT_FLAG_DROP_PAGE )
+                 {
+                     DPRINTF("Dropping page %"PRIx64" p2mt %x\n", req.gfn, req.p2mt);
++                    policy_notify_paged_out(req.gfn);
+                 }
+                 else
+                 {
+@@ -651,7 +651,7 @@ int main(int argc, char *argv[])
+                 }
+ 
+                 /* Evict a new page to replace the one we just paged in */
+-                evict_victim(paging, domain_id, &victims[i], fd, i);
++                evict_victim(paging, &victims[i], fd, i);
+             }
+             else
+             {
+Index: xen-4.0.2-testing/tools/xenpaging/xenpaging.h
+===================================================================
+--- xen-4.0.2-testing.orig/tools/xenpaging/xenpaging.h
++++ xen-4.0.2-testing/tools/xenpaging/xenpaging.h
+@@ -49,12 +49,8 @@ typedef struct xenpaging {
+ 
+ 
+ typedef struct xenpaging_victim {
+-    /* the domain to evict a page from */
+-    domid_t domain_id;
+     /* the gfn of the page to evict */
+     unsigned long gfn;
+-    /* the mfn of evicted page */
+-    unsigned long mfn;
+ } xenpaging_victim_t;
+ 
+ 
diff --git a/xenpaging.runtime_mru_size.patch b/xenpaging.runtime_mru_size.patch
new file mode 100644
index 0000000..7f08ea3
--- /dev/null
+++ b/xenpaging.runtime_mru_size.patch
@@ -0,0 +1,107 @@
+Subject: xenpaging: specify policy mru_size at runtime
+
+The environment variable XENPAGING_POLICY_MRU_SIZE will change the mru_size in
+the policy at runtime.
+Specifying the mru_size at runtime allows the admin to keep more pages in
+memory so guests can make more progress.  Its also good for development to
+reduce the value to put more pressure on the paging related code paths.
+
+(xen-unstable changeset:   22700:f87b1c194eb8)
+
+Signed-off-by: Olaf Hering <olaf@aepfle.de>
+
+---
+ tools/xenpaging/policy_default.c |   21 +++++++++++++++++----
+ tools/xenpaging/xenpaging.c      |    8 ++++++++
+ tools/xenpaging/xenpaging.h      |    1 +
+ 3 files changed, 26 insertions(+), 4 deletions(-)
+
+Index: xen-4.0.2-testing/tools/xenpaging/policy_default.c
+===================================================================
+--- xen-4.0.2-testing.orig/tools/xenpaging/policy_default.c
++++ xen-4.0.2-testing/tools/xenpaging/policy_default.c
+@@ -29,7 +29,8 @@
+ #define MRU_SIZE (1024 * 16)
+ 
+ 
+-static unsigned long mru[MRU_SIZE];
++static unsigned long *mru;
++static unsigned int mru_size;
+ static unsigned int i_mru;
+ static unsigned long *bitmap;
+ static unsigned long *unconsumed;
+@@ -56,8 +57,20 @@ int policy_init(xenpaging_t *paging)
+     bitmap_size = paging->bitmap_size;
+     max_pages = paging->domain_info->max_pages;
+ 
++    if ( paging->policy_mru_size > 0 )
++        mru_size = paging->policy_mru_size;
++    else
++        mru_size = MRU_SIZE;
++
++    mru = malloc(sizeof(*mru) * mru_size);
++    if ( mru == NULL )
++    {
++        rc = -ENOMEM;
++        goto out;
++    }
++
+     /* Initialise MRU list of paged in pages */
+-    for ( i = 0; i < MRU_SIZE; i++ )
++    for ( i = 0; i < mru_size; i++ )
+         mru[i] = INVALID_MFN;
+ 
+     /* Don't page out first 16MB */
+@@ -100,12 +113,12 @@ void policy_notify_paged_out(unsigned lo
+ 
+ void policy_notify_paged_in(unsigned long gfn)
+ {
+-    unsigned long old_gfn = mru[i_mru & (MRU_SIZE - 1)];
++    unsigned long old_gfn = mru[i_mru & (mru_size - 1)];
+ 
+     if ( old_gfn != INVALID_MFN )
+         clear_bit(old_gfn, bitmap);
+     
+-    mru[i_mru & (MRU_SIZE - 1)] = gfn;
++    mru[i_mru & (mru_size - 1)] = gfn;
+     i_mru++;
+ }
+ 
+Index: xen-4.0.2-testing/tools/xenpaging/xenpaging.c
+===================================================================
+--- xen-4.0.2-testing.orig/tools/xenpaging/xenpaging.c
++++ xen-4.0.2-testing/tools/xenpaging/xenpaging.c
+@@ -80,6 +80,7 @@ static void *init_page(void)
+ xenpaging_t *xenpaging_init(domid_t domain_id)
+ {
+     xenpaging_t *paging;
++    char *p;
+     int rc;
+ 
+     DPRINTF("xenpaging init\n");
+@@ -96,6 +97,13 @@ xenpaging_t *xenpaging_init(domid_t doma
+         goto err;
+     }
+ 
++    p = getenv("XENPAGING_POLICY_MRU_SIZE");
++    if ( p && *p )
++    {
++        paging->policy_mru_size = atoi(p);
++        DPRINTF("Setting policy mru_size to %d\n", paging->policy_mru_size);
++    }
++
+     /* Set domain id */
+     paging->mem_event.domain_id = domain_id;
+ 
+Index: xen-4.0.2-testing/tools/xenpaging/xenpaging.h
+===================================================================
+--- xen-4.0.2-testing.orig/tools/xenpaging/xenpaging.h
++++ xen-4.0.2-testing/tools/xenpaging/xenpaging.h
+@@ -37,6 +37,7 @@
+ 
+ typedef struct xenpaging {
+     int xc_handle;
++    int policy_mru_size;
+ 
+     xc_platform_info_t *platform_info;
+     xc_domaininfo_t    *domain_info;
diff --git a/xentrace.dynamic_sized_tbuf.patch b/xentrace.dynamic_sized_tbuf.patch
new file mode 100644
index 0000000..2b4bd7f
--- /dev/null
+++ b/xentrace.dynamic_sized_tbuf.patch
@@ -0,0 +1,340 @@
+---
+ xen/common/trace.c |  215 ++++++++++++++++++++---------------------------------
+ 1 file changed, 83 insertions(+), 132 deletions(-)
+
+Index: xen-4.0.2-testing/xen/common/trace.c
+===================================================================
+--- xen-4.0.2-testing.orig/xen/common/trace.c
++++ xen-4.0.2-testing/xen/common/trace.c
+@@ -41,14 +41,14 @@ CHECK_t_buf;
+ #define compat_t_rec t_rec
+ #endif
+ 
+-/* opt_tbuf_size: trace buffer size (in pages) */
+-static unsigned int opt_tbuf_size = 0;
++/* opt_tbuf_size: trace buffer size (in pages) for each cpu */
++static unsigned int opt_tbuf_size;
+ integer_param("tbuf_size", opt_tbuf_size);
+ 
+ /* Pointers to the meta-data objects for all system trace buffers */
+ static struct t_info *t_info;
+-#define T_INFO_PAGES 2  /* Size fixed at 2 pages for now. */
+-#define T_INFO_SIZE ((T_INFO_PAGES)*(PAGE_SIZE))
++static unsigned int t_info_pages;
++
+ static DEFINE_PER_CPU_READ_MOSTLY(struct t_buf *, t_bufs);
+ static DEFINE_PER_CPU_READ_MOSTLY(unsigned char *, t_data);
+ static DEFINE_PER_CPU_READ_MOSTLY(spinlock_t, t_lock);
+@@ -84,20 +84,30 @@ static void calc_tinfo_first_offset(void
+ }
+ 
+ /**
+- * check_tbuf_size - check to make sure that the proposed size will fit
++ * calculate_tbuf_size - check to make sure that the proposed size will fit
+  * in the currently sized struct t_info and allows prod and cons to
+  * reach double the value without overflow.
+  */
+-static int check_tbuf_size(u32 pages)
++static int calculate_tbuf_size(unsigned int pages)
+ {
+     struct t_buf dummy;
+-    typeof(dummy.prod) size;
+-    
+-    size = ((typeof(dummy.prod))pages)  * PAGE_SIZE;
+-    
+-    return (size / PAGE_SIZE != pages)
+-           || (size + size < size)
+-           || (num_online_cpus() * pages + t_info_first_offset > T_INFO_SIZE / sizeof(uint32_t));
++    typeof(dummy.prod) size = -1;
++
++    /* max size holds up to n pages */
++    size /= PAGE_SIZE;
++    if ( pages > size )
++    {
++        gdprintk(XENLOG_INFO, "%s: requested number of %u pages reduced to %u\n",
++               __func__, pages, (unsigned int)size);
++        pages = size;
++    }
++
++    t_info_pages = num_online_cpus() * pages + t_info_first_offset;
++    t_info_pages *= sizeof(uint32_t);
++    t_info_pages /= PAGE_SIZE;
++    if ( t_info_pages % PAGE_SIZE )
++        t_info_pages++;
++    return pages;
+ }
+ 
+ /**
+@@ -110,47 +120,28 @@ static int check_tbuf_size(u32 pages)
+  * This function may also be called later when enabling trace buffers 
+  * via the SET_SIZE hypercall.
+  */
+-static int alloc_trace_bufs(void)
++static int alloc_trace_bufs(unsigned int pages)
+ {
+-    int           i, cpu, order;
+-    unsigned long nr_pages;
++    int i, cpu, order;
+     /* Start after a fixed-size array of NR_CPUS */
+     uint32_t *t_info_mfn_list;
+     int offset;
+ 
+-    if ( opt_tbuf_size == 0 )
+-        return -EINVAL;
++    if ( t_info )
++        return -EBUSY;
+ 
+-    if ( check_tbuf_size(opt_tbuf_size) )
+-    {
+-        printk("Xen trace buffers: tb size %d too large. "
+-               "Tracing disabled.\n",
+-               opt_tbuf_size);
++    if ( pages == 0 )
+         return -EINVAL;
+-    }
+-
+-    /* t_info size is fixed for now. Currently this works great, so there
+-     * seems to be no need to make it dynamic. */
+-    t_info = alloc_xenheap_pages(get_order_from_pages(T_INFO_PAGES), 0);
+-    if ( t_info == NULL )
+-    {
+-        printk("Xen trace buffers: t_info allocation failed! "
+-               "Tracing disabled.\n");
+-        return -ENOMEM;
+-    }
+-
+-    for ( i = 0; i < T_INFO_PAGES; i++ )
+-        share_xen_page_with_privileged_guests(
+-            virt_to_page(t_info) + i, XENSHARE_readonly);
+ 
+-    t_info_mfn_list = (uint32_t *)t_info;
+-    offset = t_info_first_offset;
++    /* Calculate offset in u32 of first mfn */
++    calc_tinfo_first_offset();
+ 
+-    t_info->tbuf_size = opt_tbuf_size;
+-    printk(XENLOG_INFO "tbuf_size %d\n", t_info->tbuf_size);
++    pages = calculate_tbuf_size(pages);
++    order = get_order_from_pages(pages);
+ 
+-    nr_pages = opt_tbuf_size;
+-    order = get_order_from_pages(nr_pages);
++    t_info = alloc_xenheap_pages(get_order_from_pages(t_info_pages), 0);
++    if ( t_info == NULL )
++        goto out_dealloc;
+ 
+     /*
+      * First, allocate buffers for all of the cpus.  If any
+@@ -158,27 +149,29 @@ static int alloc_trace_bufs(void)
+      */
+     for_each_online_cpu(cpu)
+     {
+-        int flags;
+-        char         *rawbuf;
++        void *rawbuf;
+         struct t_buf *buf;
+ 
+         if ( (rawbuf = alloc_xenheap_pages(
+                 order, MEMF_bits(32 + PAGE_SHIFT))) == NULL )
+         {
+-            printk("Xen trace buffers: memory allocation failed\n");
+-            opt_tbuf_size = 0;
++            printk("Xen trace buffers: memory allocation failed on cpu %d\n", cpu);
+             goto out_dealloc;
+         }
+ 
+-        spin_lock_irqsave(&per_cpu(t_lock, cpu), flags);
+-
+-        per_cpu(t_bufs, cpu) = buf = (struct t_buf *)rawbuf;
++        per_cpu(t_bufs, cpu) = buf = rawbuf;
+         buf->cons = buf->prod = 0;
+         per_cpu(t_data, cpu) = (unsigned char *)(buf + 1);
++    }
+ 
+-        spin_unlock_irqrestore(&per_cpu(t_lock, cpu), flags);
++    offset = t_info_first_offset;
++    t_info_mfn_list = (uint32_t *)t_info;
+ 
+-    }
++    for(i = 0; i < t_info_pages; i++)
++        share_xen_page_with_privileged_guests(
++            virt_to_page(t_info) + i, XENSHARE_readonly);
++
++    t_info->tbuf_size = pages;
+ 
+     /*
+      * Now share the pages to xentrace can map them, and write them in
+@@ -187,91 +180,72 @@ static int alloc_trace_bufs(void)
+     for_each_online_cpu(cpu)
+     {
+         /* Share pages so that xentrace can map them. */
+-        char         *rawbuf;
++        void *rawbuf = per_cpu(t_bufs, cpu);
++        struct page_info *p = virt_to_page(rawbuf);
++        uint32_t mfn = virt_to_mfn(rawbuf);
+ 
+-        if ( (rawbuf = (char *)per_cpu(t_bufs, cpu)) )
++        for ( i = 0; i < pages; i++ )
+         {
+-            struct page_info *p = virt_to_page(rawbuf);
+-            uint32_t mfn = virt_to_mfn(rawbuf);
++            share_xen_page_with_privileged_guests(p + i, XENSHARE_writable);
+ 
+-            for ( i = 0; i < nr_pages; i++ )
+-            {
+-                share_xen_page_with_privileged_guests(
+-                    p + i, XENSHARE_writable);
+-            
+-                t_info_mfn_list[offset + i]=mfn + i;
+-            }
+-            /* Write list first, then write per-cpu offset. */
+-            wmb();
+-            t_info->mfn_offset[cpu]=offset;
+-            printk(XENLOG_INFO "p%d mfn %"PRIx32" offset %d\n",
+-                   cpu, mfn, offset);
+-            offset+=i;
++            t_info_mfn_list[offset + i]=mfn + i;
+         }
++        t_info->mfn_offset[cpu]=offset;
++        printk(XENLOG_INFO "p%d mfn %"PRIx32" offset %d\n",
++               cpu, mfn, offset);
++        offset+=i;
++
++        spin_lock_init(&per_cpu(t_lock, cpu));
+     }
+ 
+-    data_size  = (opt_tbuf_size * PAGE_SIZE - sizeof(struct t_buf));
++    data_size  = (pages * PAGE_SIZE - sizeof(struct t_buf));
+     t_buf_highwater = data_size >> 1; /* 50% high water */
++    opt_tbuf_size = pages;
++
++    printk("Xen trace buffers: initialised\n");
++    wmb(); /* above must be visible before tb_init_done flag set */
++    tb_init_done = 1;
+ 
+     return 0;
++
+ out_dealloc:
+     for_each_online_cpu(cpu)
+     {
+-        int flags;
+-        char * rawbuf;
+-
+-        spin_lock_irqsave(&per_cpu(t_lock, cpu), flags);
+-        if ( (rawbuf = (char *)per_cpu(t_bufs, cpu)) )
++        void *rawbuf = per_cpu(t_bufs, cpu);
++        per_cpu(t_bufs, cpu) = NULL;
++        if ( rawbuf )
+         {
+-            per_cpu(t_bufs, cpu) = NULL;
+             ASSERT(!(virt_to_page(rawbuf)->count_info & PGC_allocated));
+             free_xenheap_pages(rawbuf, order);
+         }
+-        spin_unlock_irqrestore(&per_cpu(t_lock, cpu), flags);
+     }
+-    
++    free_xenheap_pages(t_info, get_order_from_pages(t_info_pages));
++    t_info = NULL;
++    printk("Xen trace buffers: allocation failed! Tracing disabled.\n");
+     return -ENOMEM;
+ }
+ 
+ 
+ /**
+- * tb_set_size - handle the logic involved with dynamically
+- * allocating and deallocating tbufs
++ * tb_set_size - handle the logic involved with dynamically allocating tbufs
+  *
+  * This function is called when the SET_SIZE hypercall is done.
+  */
+-static int tb_set_size(int size)
++static int tb_set_size(unsigned int pages)
+ {
+     /*
+      * Setting size is a one-shot operation. It can be done either at
+      * boot time or via control tools, but not by both. Once buffers
+      * are created they cannot be destroyed.
+      */
+-    int ret = 0;
+-
+-
+-
+-    if ( opt_tbuf_size != 0 )
++    if ( opt_tbuf_size && pages != opt_tbuf_size )
+     {
+-        if ( size != opt_tbuf_size )
+-            gdprintk(XENLOG_INFO, "tb_set_size from %d to %d not implemented\n",
+-                     opt_tbuf_size, size);
++        gdprintk(XENLOG_INFO, "tb_set_size from %d to %d not implemented\n",
++                     opt_tbuf_size, pages);
+         return -EINVAL;
+     }
+ 
+-    if ( size <= 0 )
+-        return -EINVAL;
+-
+-    opt_tbuf_size = size;
+-
+-    if ( (ret = alloc_trace_bufs()) != 0 )
+-    {
+-        opt_tbuf_size = 0;
+-        return ret;
+-    }
+-
+-    printk("Xen trace buffers: initialized\n");
+-    return 0;
++    return alloc_trace_bufs(pages);
+ }
+ 
+ int trace_will_trace_event(u32 event)
+@@ -309,36 +283,13 @@ int trace_will_trace_event(u32 event)
+  */
+ void __init init_trace_bufs(void)
+ {
+-    int i;
+-
+-    /* Calculate offset in u32 of first mfn */
+-    calc_tinfo_first_offset();
+-
+-    /* Per-cpu t_lock initialisation. */
+-    for ( i = 0; i < NR_CPUS; i++ )
+-        spin_lock_init(&per_cpu(t_lock, i));
+-
+-    if ( opt_tbuf_size == 0 )
++    if ( opt_tbuf_size && alloc_trace_bufs(opt_tbuf_size) )
+     {
+-        printk("Xen trace buffers: disabled\n");
+-        goto fail;
+-    }
+-
+-    if ( alloc_trace_bufs() != 0 )
+-    {
+-        dprintk(XENLOG_INFO, "Xen trace buffers: "
+-                "allocation size %d failed, disabling\n",
+-                opt_tbuf_size);
+-        goto fail;
++        gdprintk(XENLOG_INFO, "Xen trace buffers: "
++                 "allocation size %d failed, disabling\n",
++                 opt_tbuf_size);
++        opt_tbuf_size = 0;
+     }
+-
+-    printk("Xen trace buffers: initialised\n");
+-    wmb(); /* above must be visible before tb_init_done flag set */
+-    tb_init_done = 1;
+-    return;
+-
+- fail:
+-    opt_tbuf_size = 0;
+ }
+ 
+ /**
+@@ -357,7 +308,7 @@ int tb_control(xen_sysctl_tbuf_op_t *tbc
+     case XEN_SYSCTL_TBUFOP_get_info:
+         tbc->evt_mask   = tb_event_mask;
+         tbc->buffer_mfn = t_info ? virt_to_mfn(t_info) : 0;
+-        tbc->size = T_INFO_PAGES * PAGE_SIZE;
++        tbc->size = t_info_pages * PAGE_SIZE;
+         break;
+     case XEN_SYSCTL_TBUFOP_set_cpu_mask:
+         rc = xenctl_cpumap_to_cpumask(&tb_cpu_mask, &tbc->cpu_mask);