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

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


  • To: Edwin Torok <edvin.torok@xxxxxxxxxx>, "xen-devel@xxxxxxxxxxxxxxxxxxxx" <xen-devel@xxxxxxxxxxxxxxxxxxxx>
  • From: Andrew Cooper <Andrew.Cooper3@xxxxxxxxxx>
  • Date: Fri, 5 Aug 2022 18:06:37 +0000
  • Accept-language: en-GB, en-US
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=citrix.com; dmarc=pass action=none header.from=citrix.com; dkim=pass header.d=citrix.com; arc=none
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=N0/54p4No/wTFPdg0dZSU4Qe/D5i6arfL0fpOb6VaCg=; b=UE7YVB12vXAI8LP14bYRVH8SCrDWyPuU5US9OKZQjfHqhLF2LLgw4ZphHeo2EoRAo0Dqu9QTdVEne+nLRQ16Esp+wyibmWKmr7eVlQjRoqEsLX/A9WXPUNrNXYhJyKWW4+IG666Zj69bYqTuIiee1zuxRQiR61YcGnBMssse8Tn1mjiEa/ea2OJvz6w57sNUWAv7xJ5oWxevFxkzx0T3/n/Dl0Pxcl/dAHqDn1snyA2H4Y6mJUtpQtgYz+IXCmrR4UNdWdC2zjCIZUUdC+gWxSUCfH+LjnOGY7I3DWe+5Gt4eLlZxjiKi274pX/4VtkeQmsPNDdHdJhc3KjdceNjXA==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Qy9cYPBGn5vhY1U8clll7VozmaAmt4lP15MNEf+212qQxTN57WvOJDbVpUOvYKRaGVcYA0ticMgdB5SotG079avafwsSWlgjTtY/rnehTd4ieWlxDs0pM/Yw31AjgGLa8iK0sT5NIAF87sYqy86KRlt76aGuzc+45WSUqOqn6TFo52TzC8iOIrCYMIMlJ/fKCs2eUOqLFmfg/KL1V8uDsmTEIwFFpb5z/9XXauW+Mc+TbTGnmMzcGUpz63kPo4WlWAE/j4ibsxodOnYpdJ9XEgn4uMxj1oBFo1Y09Zh5hN/SpR4vbU10yxzH8ISZnY6BmcEF548Ae2VofRhYc4aZag==
  • Authentication-results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=citrix.com;
  • Cc: Christian Lindig <christian.lindig@xxxxxxxxxx>, David Scott <dave@xxxxxxxxxx>, Wei Liu <wl@xxxxxxx>, Anthony Perard <anthony.perard@xxxxxxxxxx>
  • Delivery-date: Fri, 05 Aug 2022 18:07:09 +0000
  • Ironport-data: A9a23:4s/J9KBkbtuyrhVW/zfiw5YqxClBgxIJ4kV8jS/XYbTApD4l0jxWn GIfXmiAPanbMGSgeNwlaI+x/EpSuMKHn9dmQQY4rX1jcSlH+JHPbTi7wuYcHM8wwunrFh8PA xA2M4GYRCwMZiaA4E3ratANlFEkvYmQXL3wFeXYDS54QA5gWU8JhAlq3uU0meaEu/Dga++2k Y608pa31GONgWYuaDpEs/7b83uDgdyp0N8mlg1mDRx0lAe2e0k9VPo3Oay3Jn3kdYhYdsbSq zHrlezREsvxpn/BO/v9+lrJWhRiro36ZGBivkF+Sam66iWukwRpukoN2FjwXm8M49mBt4gZJ NygLvVcQy9xVkHHsLx1vxW1j0iSlECJkVPKCSHXjCCd86HJW2Xq2M00CBwnAbFbwLdRAj9K7 6Q+Fj9YO3hvh8ruqF66Ys9Fo517aePNY8YYsHwmyizFB/E7R5yFW7/N+dJTwDY3gIZJAOraY M0aLzFoaXwsYTUWYgtRVM14w7/u3yGvG9FbgAv9Sa4fym7f1gFulpPqN8LYYIeiTsRJhEeI4 GnB+gwVBzlFa43GlWXYqhpAgMf/vD7nRI8YTYG/zaF1hVm1wGBDJxcvAA7TTf6RzxTWt8hkA 1wZ/G8ioLY/8GSvT8LhRFuorXicpBkeVtFMVeog52mlxqPK7i6DC2MDTzoHb8Yp3OcpQRQ62 1nPmMnmbQGDq5WQQHOZs72S8jW7PHFNKXdYPHdUCwwY/9PkvYc/yArVScpuG7K0iduzHizsx zeNr241gLB7YdM36phXNGvv21qEzqUlhCZutm07gkrNAttFWbOY
  • Ironport-hdrordr: A9a23:tKyQMqnSnnakZONjPg3hajRvrkXpDfOPimdD5ihNYBxZY6Wkfp +V8cjzhCWftN9OYhodcIi7SdK9qXO1z+8X3WGIVY3SETUOy1HYVr2KirGSjwEIeheOvNK1sJ 0NT0EQMqyWMbEXt6fHCUyDYq4dKbq8ge+VbIXlvhFQpGhRAskOgTuRSDzra3GeLzM2Z6bRYa Dsgvav0ADQHEj/AP7aOlA1G8z44/HbnpPvZhALQzQ97hOVsD+u4LnmVzCFwxY3SVp0sPYf2F mAtza8yrSosvm9xBOZ/XTU9Y5qlNzozcYGLNCQi/ISNi7nhm+TFcRcsvy5zXMISdOUmRMXee r30lMd1gNImjTsl1SO0FnQMs/boXATAjHZuAalaDDY0LHErXoBerZ8bMRiA1XkAgMbza9BOO gg5RPni7NHSRzHhyjz/N7OSlVjkVe1u2MrlaoJg2VYSpZ2Us4ZkWUzxjIjLH47JlON1Kk3VO 11SM3M7vdfdl2XK3jfo2l02dSpGnA+BA2PTEQOstGcl2E+pgEz82IIgMgE2nsQ/pM0TJdJo+ zCL6RzjblLCssbd7h0CusNSda+TmbNXRXPOmSPJkmPLtBOB1vd75rspLkl7uCjf5IFiJM0hZ TaSVtd8XU/fkr/YPf+qKGjMiq9NVlVcQ6duv22vaIJy4EUbICbQhGrWRQpj9aqpekZD4nSR+ uzUagmccPeEQ==
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>
  • Thread-index: AQHYo3Q7g2T4T7O1X0OIdhve1Wb7Ta2gpUyA
  • Thread-topic: [PATCH v1 7/7] tools/ocaml/libs/eventchn: do not leak event channels and OCaml 5.0 compat

On 29/07/2022 18:53, Edwin Török wrote:
> 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>

So while this looks like a good improvement, don't we need to do it for
all libraries, not just evtchn?

It doesn't look as if Ocaml 5.0 is very far away.

> ---
>  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 */

This wants to gain a trailing comma.

> +};
>  
>  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);

The memory allocated for xce is tiny (48 bytes) and invariant for the
lifetime of the evtchn object, which we've already established tends to
operate as a singleton anyway.

Don't we want to use the recommended 0 and 1 here?

~Andrew

 


Rackspace

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