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

[PATCH v1 7/7] tools/ocaml/libs/eventchn: do not leak event channels and OCaml 5.0 compat


  • To: <xen-devel@xxxxxxxxxxxxxxxxxxxx>
  • From: Edwin Török <edvin.torok@xxxxxxxxxx>
  • Date: Fri, 29 Jul 2022 18:53:30 +0100
  • 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, 29 Jul 2022 17:54:15 +0000
  • Ironport-data: A9a23:fIy9p67Lxj81PkSfPwyxlgxR2vbMJaxDgicv29bJVGNS3hzvzP4wXsRDTujRkuZqZDvb7yzGW0cUVF/XAW5zYYN0QI0iEgVdHalNNuE0JwZLfNgEvJfZhRHGoA4rYvCxxwf6MU/QvLN2GFB/zkPdL0Otk0MyZ8oTFLw3Hls8CtaUNQ2PKE6Stmcd7MkJoJLIo/zZhfLCG5ViXUDWnaHGg3ykuwsATahAcdWKHbmZkmUkBzHbD5HkMeAv+fgf3YWQC0bYeJ7K/+0BmEjGvgyvUmCrvGXKMz5YOaEMiQUyAnuQrAZJuLVTDomDnj7lTWD3wB06U9lXJ2WkNZe/gyMO/tQWkac92fYRWHI5nLvf3fRhKVoZjhxI+8pZA018VmCqvbqJOpq75ZBxuW8VCuvVUxo5aFwC1cmz69W9LMk4Tqg/wQeLhbq5uZZKUNtZJ1YcRvE0HX3E+IsVDSzJyLV3lTqXt8r889jziweWKhFl5D2LsccW0gGdAImQVHMrCXFgu9wRuDeS0INK4lIZ5SSzlHiolQI4N0UgV+TQA+N9f8k34d81p7G+vOBCYV6x7JUAKCnsaIFEFzRZyXTZzodvknl1INy1BToPaKqpFZ8Q8uvna5pSoE3aAI9Hp4jqOnOB8X8GncY99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nvnz5YHye6VDnJNcbxvEGwd1CCAs3H80/oyZX5zI60SXef3janVH6Swe35VS19bz7UzYBJAl6ROC/uwlmLkw93tvJTTpHwgf9aQfB+uW5myV0lQrHSszN5rZC0zro6YqUE6xuuRcfrrXPfmumeeWshQROTnspLwvpd1uzua9o46d0K6bzYDn4b3ncssKwNKiOyfdIFZLaXiQlWdh2X5hVqtEvhadRd02YgHeIAy2DzTfic2tt37wM0/4QWfvQZgZGu2zfxq1/VS5jtvG/wrdcb5MnNiMM/I6exdMJU7siOLhDwB2C5I7un/T5CFqDHw4MQD2KIvP1bnffpYAssx2moWqM+YG7gnfgzI1nzrvD9HEz32o17Mdb0WKGfXv/Q4ADYrTbCwJ0F+uFWFxp6Ckhpq5RMDAsgDwLK3pOfmvhLsSU7coS2tctR+ahp2tCDM4vWagaX8y+phT1BPFEDjZTp/8JScugZKdoj407IaAVYX98noa+Hy3AA5ljQxxayw0VfPQ5xHAhChRZbwGAIagMCarxT605eGTYBIMv1esRUfdMHnIhWAlh6FotFMd1MGPK57wHfve2mPHXEHykj/kkiGr+7Z8VZwTAn5NpGRH7d8E/LL2nG9fIXAQKX4YwnwHH/kpAwBdrfi17AHtt3FSMPWzOI/Crnw/FxPZjWwlwAwPTFXaRLgqK3JPprjWBXlQTQDFHk9hqWrg4FxwreYM3HMGiqsY4T36utjlBgDIAUrgLHrEePNAWZDxTgT3yB5MhIvwqWMzgZWxzS85CXAWiRqqj9P4eVYRNw/8ES788AMhITprxWrZXe4+hYTTaAmYPjAB1Su9nkLSGPQA7m0SNdAd9+In73OBwZyc451G5p6wFLaB4oiKvMHzIqytInEqGvMVkKip2K/aK5txyHd4ATXvpJaYw08l46NqzQv0L3qewrMBAIIzFkrCGQ521ZDKl1N/R3dPVnK5298k=
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

Add a finalizer on the event channel value, so that it calls
`xenevtchn_close` when the value would be GCed.

In practice oxenstored seems to be the only user of this,
and it creates a single global event channel only,
but freeing this could still be useful when run with OCAMLRUNPARAM=c

The code was previously casting a C pointer to an OCaml value,
which should be avoided: OCaml 5.0 won't support it.
(all "naked" C pointers must be wrapped inside an OCaml value,
 either an Abstract tag, or Nativeint, see the manual
 https://ocaml.org/manual/intfc.html#ss:c-outside-head)

Signed-off-by: Edwin Török <edvin.torok@xxxxxxxxxx>
---
 tools/ocaml/libs/eventchn/xeneventchn_stubs.c | 29 +++++++++++++++++--
 1 file changed, 27 insertions(+), 2 deletions(-)

diff --git a/tools/ocaml/libs/eventchn/xeneventchn_stubs.c 
b/tools/ocaml/libs/eventchn/xeneventchn_stubs.c
index f889a7a2e4..c0d57e2954 100644
--- a/tools/ocaml/libs/eventchn/xeneventchn_stubs.c
+++ b/tools/ocaml/libs/eventchn/xeneventchn_stubs.c
@@ -33,7 +33,30 @@
 #include <caml/fail.h>
 #include <caml/signals.h>
 
-#define _H(__h) ((xenevtchn_handle *)(__h))
+/* We want to close the event channel when it is no longer in use,
+   which can only be done safely with a finalizer.
+   Event channels are typically long lived, so we don't need tighter control 
over resource deallocation.
+   Use a custom block
+*/
+
+/* Access the xenevtchn_t* part of the OCaml custom block */
+#define _H(__h) (*((xenevtchn_handle**)Data_custom_val(__h)))
+
+static void stub_evtchn_finalize(value v)
+{
+       /* docs say to not use any CAMLparam* macros here */
+       xenevtchn_close(_H(v));
+}
+
+static struct custom_operations xenevtchn_ops = {
+       "xenevtchn",
+       stub_evtchn_finalize,
+       custom_compare_default, /* raises Failure, cannot compare */
+       custom_hash_default, /* ignored */
+       custom_serialize_default, /* raises Failure, can't serialize */
+       custom_deserialize_default, /* raises Failure, can't deserialize */
+       custom_compare_ext_default /* raises Failure */
+};
 
 CAMLprim value stub_eventchn_init(void)
 {
@@ -48,7 +71,9 @@ CAMLprim value stub_eventchn_init(void)
        if (xce == NULL)
                caml_failwith("open failed");
 
-       result = (value)xce;
+       /* contains file descriptors, trigger full GC at least every 128 
allocations */
+       result = caml_alloc_custom(&xenevtchn_ops, sizeof(xce), 1, 128);
+       _H(result) = xce;
        CAMLreturn(result);
 }
 
-- 
2.34.1




 


Rackspace

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