[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v4 08/11] tools/ocaml/libs/mmap: mark mmap/munmap as blocking and raise Unix_error on failure


  • To: <xen-devel@xxxxxxxxxxxxxxxxxxxx>
  • From: Edwin Török <edvin.torok@xxxxxxxxxx>
  • Date: Fri, 16 Dec 2022 18:25:17 +0000
  • Authentication-results: esa2.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none
  • Cc: Edwin Török <edvin.torok@xxxxxxxxxx>, Christian Lindig <christian.lindig@xxxxxxxxxx>, David Scott <dave@xxxxxxxxxx>, Wei Liu <wl@xxxxxxx>, Anthony PERARD <anthony.perard@xxxxxxxxxx>
  • Delivery-date: Fri, 16 Dec 2022 18:25:51 +0000
  • Ironport-data: A9a23:0bJHEqssU4ZmDNbJzFvwo0LoKOfnVFheMUV32f8akzHdYApBsoF/q tZmKWqDO/+NZ2D3c9kiO4W0oxtT6p/Qzd5gSABl/3hkEyoQ+JbJXdiXEBz9bniYRiHhoOCLz O1FM4Wdc5pkJpP4jk3wWlQ0hSAkjclkfpKlVKiffHg0HVU/IMsYoUoLs/YjhYJ1isSODQqIu Nfjy+XSI1bg0DNvWo4uw/vrRChH4bKj5lv0gnRkPaoR5QWHySFPZH4iDfrZw0XQE9E88tGSH 44v/JnhlkvF8hEkDM+Sk7qTWiXmlZaLYGBiIlIPM0STqkAqSh4ai87XB9JFAatjsB2bnsgZ9 Tl4ncfYpTHFnEH7sL91vxFwS0mSNEDdkVPNCSDXXce7lyUqf5ZwqhnH4Y5f0YAwo45K7W9yG fMwEGtQMindt8ONxZ2nVNNzvsBgLJbIM9ZK0p1g5Wmx4fcORJnCR+PB5MNC3Sd2jcdLdRrcT 5NHM3w1Nk2GOkARfAdMYH49tL7Aan3XfjFGqVTTua0t5Gv7xw1tyrn9dtHSf7RmQO0FzhbA/ j6ZowwVBDkHP4a1kziM20iig77NnTqqcd1JBp+no6sCbFq7mTVIVUx+uUGAiem0jAuyVsxSL 2QQ+zEytu4i+UqzVN7/Uhak5nmesXY0V9NOHsUg5QqKy66S5ByWbkAbShZRZdpgs9U5LQHGz XfQwYmvX2Y29uTIFzTNrd94sA9eJwAtdjJeXHE0bDAAzMX9mp4LilHPEfVsRfvdYsLOJRn8x DWDrS4bjroVjNIW26jTwW0rkw5AtbCSEFdru1y/snaNq1ogOdX7P9DABU3zt64oEWqPcrWWU JHoceC65ftGM5yCnTflrA4lTODwvKbt3NExbDdS83gdG9aFoSbLkWN4umsWyKJV3iEsJ1fUj Lf741852XOqFCLCgVVLS4ywEd826qPrCM7oUPvZBvIXPMcoLVXWpXA+PRLOt4wIrKTLufhvU ap3jO72VSpKYUiZ5GTeqxghPU8DmXllmDK7qWHTxBW7y7uODEN5up9cWGZimtsRtfveyC2Mq oY3Cid/40kHOAEISnWNoNF7wJFjBSRTOK0aXOQLLr/ef1Y7Rj5xYxITqJt4E7FYc21uvr+g1 hmAtoVwkTITWVWvxd22V01e
  • Ironport-hdrordr: A9a23:IopYJq1/R8Ip+0HqsYo6uwqjBIokLtp133Aq2lEZdPRUGvb3qy nIpoVj6faUskd2ZJhOo7C90cW7LU80sKQFhLX5Xo3SOzUO2lHYT72KhLGKq1aLdhEWtNQtsZ uIG5IOcOEYZmIasS+V2maF+q4bsbu6zJw=
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

These functions can potentially take some time,
so allow other OCaml code to proceed meanwhile (if any).

Also raise a Unix_error based on `errno` if `mmap` fails, instead of just
calling failwith (which would lose the error reason).

Signed-off-by: Edwin Török <edvin.torok@xxxxxxxxxx>
---

Changes since v2:
* repost of lost patch from 2020 (posted to ML on 2021
  
https://patchwork.kernel.org/project/xen-devel/patch/294a60be29027d33b0a1d154b7d576237c7dd420.1620755942.git.edvin.torok@xxxxxxxxxx/)
---
 tools/ocaml/libs/mmap/xenmmap_stubs.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/tools/ocaml/libs/mmap/xenmmap_stubs.c 
b/tools/ocaml/libs/mmap/xenmmap_stubs.c
index bf864a7c32..5b8c240ef9 100644
--- a/tools/ocaml/libs/mmap/xenmmap_stubs.c
+++ b/tools/ocaml/libs/mmap/xenmmap_stubs.c
@@ -27,6 +27,8 @@
 #include <caml/custom.h>
 #include <caml/fail.h>
 #include <caml/callback.h>
+#include <caml/unixsupport.h>
+#include <caml/signals.h>
 
 #define Intf_val(a) ((struct mmap_interface *) Data_abstract_val(a))
 
@@ -35,7 +37,9 @@ static int mmap_interface_init(struct mmap_interface *intf,
                                int len, int offset)
 {
     intf->len = len;
+    caml_enter_blocking_section();
     intf->addr = mmap(NULL, len, pflag, mflag, fd, offset);
+    caml_leave_blocking_section();
     return (intf->addr == MAP_FAILED) ? errno : 0;
 }
 
@@ -64,17 +68,22 @@ CAMLprim value stub_mmap_init(value fd, value pflag, value 
mflag,
     if (mmap_interface_init(Intf_val(result), Int_val(fd),
                             c_pflag, c_mflag,
                             Int_val(len), Int_val(offset)))
-        caml_failwith("mmap");
+        uerror("mmap", Nothing);
     CAMLreturn(result);
 }
 
 CAMLprim value stub_mmap_final(value intf)
 {
     CAMLparam1(intf);
+    struct mmap_interface interface = *Intf_val(intf);
 
-    if (Intf_val(intf)->addr != MAP_FAILED)
-        munmap(Intf_val(intf)->addr, Intf_val(intf)->len);
+    /* mark it as freed, in case munmap below fails, so we don't retry it */
     Intf_val(intf)->addr = MAP_FAILED;
+    if (interface.addr != MAP_FAILED) {
+        caml_enter_blocking_section();
+        munmap(interface.addr, interface.len);
+        caml_leave_blocking_section();
+    }
 
     CAMLreturn(Val_unit);
 }
-- 
2.34.1




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.