Tarantool development patches archive
 help / color / mirror / Atom feed
From: Sergey Kaplun via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: Sergey Ostanevich <sergos@tarantool.org>
Cc: tarantool-patches@dev.tarantool.org
Subject: Re: [Tarantool-patches] [PATCH luajit 1/2] Add support for full-range 64 bit lightuserdata.
Date: Mon, 2 Aug 2021 17:51:29 +0300	[thread overview]
Message-ID: <YQgGcfW7bJ4j9Iqk@root> (raw)
In-Reply-To: <9C3661B1-0D21-42B7-94F6-C9C14FCEBCCD@tarantool.org>

Hi, Sergos!

Thanks for the review!

On 01.08.21, Sergey Ostanevich wrote:
> Hi! Thanks for the patch!
> 
> Some minor message fixes, one great gag from Mike’s code and a
> test request.
> 
> Regards,
> Sergos
> 
> > 
> > The new commit message is the following:
> > 
> > ===================================================================
> > Add support for full-range 64 bit lightuserdata.
> > 
> > (cherry picked from commit e9af1abec542e6f9851ff2368e7f196b6382a44c)
> > 
> > LuaJIT uses special NaN-tagging technique to store internal type on
> > the Lua stack. In case LJ_GC64 first 13 bits are set in special NaN
> 		^^^^^^^		^
> 		In case of     the

Fixed.

> > type (0xfff8...). FPU generates the only one type. The next 4 bits are
> 				  ^^^^^^^^^^^
> 			Which one and how is it relevant?	

Yep, it can be dropped.

> 
> > used for an internal LuaJIT type of object on stack. The next 47 bits
> > are used for storing this object's content. For userdata, it is its
> > address. In case arm64 the pointer can have more than 47 significant
> 	   ^^^^^
> 	   For

Fixed.

> > bits [1]. In this case the error BADLU error is raised.
> > 
> > For the support of full 64-bit range lightuserdata pointers two new
> > fields in GCState are added:
> > 
> > `lightudseg` - vector of segments of lightuserdata. Each element keeps
> > 32-bit value. 25 MSB equal to MSB of lightuserdata address, the rest are
>                                                     ^
> 						64bit

Fixed.

> > filled with zeros. The length of the vector is power of 2.
> > 
> > `lightudnum` - the length - 1 of aforementioned vector (up to 255).
> > 
> > When lightuserdata is pushed on the stack, if its segment is not stored
> 
> > in vector new value is appended on top of this vector. The maximum
> 				 ^^^^^^^^^ to

Fixed.

> 
> At first I want you to put it as ’not found’ instead of ’not stored’. 
> Then I start thinking over ‘on top’ for a vector and I got a strange
> feeling... 
> 
> 
> Now tell me, every time you put a LUD pointer to stack you have to roll
> over all present segments in this '>>>' plain loop below?

Yep, praying for the "Fast path".

Side note: Mike likes to "teach" people how they *should* write code,
and how they *shouldn't*. Also, he often tells that huge slowdowns and
lags, when the "wrong" code is running are teaching them to avoid bad
code. I suppose, he thinks, that intensive usage of userdata is a bad
pattern in Lua and more important in LuaJIT...

> 
> --- a/src/lj_api.c
> +++ b/src/lj_api.c
> +#if LJ_64
> +static void *lightud_intern(lua_State *L, void *p)
> +{
> +  global_State *g = G(L);
> +  uint64_t u = (uint64_t)p;
> +  uint32_t up = lightudup(u);
> +  uint32_t *segmap = mref(g->gc.lightudseg, uint32_t);
> +  MSize segnum = g->gc.lightudnum;
> +  if (segmap) {
> +    MSize seg;
> >>> +    for (seg = 0; seg <= segnum; seg++)
> >>> +      if (segmap[seg] == up)  /* Fast path. */
> >>> +	return (void *)(((uint64_t)seg << LJ_LIGHTUD_BITS_LO) | lightudlo(u));
> +    segnum++;
> +  }
> +  if (!((segnum-1) & segnum) && segnum != 1) {
> +    if (segnum >= (1 << LJ_LIGHTUD_BITS_SEG)) lj_err_msg(L, LJ_ERR_BADLU);
> +    lj_mem_reallocvec(L, segmap, segnum, segnum ? 2*segnum : 2u, uint32_t);
> +    setmref(g->gc.lightudseg, segmap);
> +  }
> +  g->gc.lightudnum = segnum;
> +  segmap[segnum] = up;
> +  return (void *)(((uint64_t)segnum << LJ_LIGHTUD_BITS_LO) | lightudlo(u));
> +}
> +#endif
> +
> 
> Can’t help to laugh at Mike’s /* Fast path */, brilliant isn’t it?
> Perhaps addition of a new segment is not so often - and is counted to 256 -
> so we can easily sort the array each time to make it log(n) rather (n) for
> each lua_pushlightuserdata()?

Mike's style... Also, I suggest to avoid sorting optimization for now by
two reasons:

1) We have no goal to beat everyone at ARM __yet__. Just make it
breathing.
2) We have no performance tests to measure such changes (I hope
__yet__, too).

> 
> > <snipped>
> > 
> > See the iterative patch below.
> > 
> > ===================================================================
> > diff --git a/test/tarantool-tests/lj-49-bad-lightuserdata.test.lua b/test/tarantool-tests/lj-49-bad-lightuserdata.test.lua
> 
> This one tests the LUD push/pop to/fro stack. How about those 
> 
> > all internal usage of lightuserdata (for hooks,
> > profilers, built-in package, IR and so on) is changed to special values
> > on Lua Stack.
> 
> Can you add at least _some_ test to verify memprof is fine?

Memprof avoids such extroversions. Do you mean the test for `jit.p`?

The new commit message is the following:

===================================================================
Add support for full-range 64 bit lightuserdata.

(cherry picked from commit e9af1abec542e6f9851ff2368e7f196b6382a44c)

LuaJIT uses special NaN-tagging technique to store internal type on
the Lua stack. In case of LJ_GC64 the first 13 bits are set in special
NaN type (0xfff8...). The next 4 bits are used for an internal LuaJIT
type of object on stack. The next 47 bits are used for storing this
object's content. For userdata, it is its address. For arm64 a pointer
can have more than 47 significant bits [1]. In this case the error BADLU
error is raised.

For the support of full 64-bit range lightuserdata pointers two new
fields in GCState are added:

`lightudseg` - vector of segments of lightuserdata. Each element keeps
32-bit value. 25 MSB equal to MSB of lightuserdata 64-bit address, the
rest are filled with zeros. The lentgh of the vector is power of 2.

`lightudnum` - the length - 1 of aforementioned vector (up to 255).

When lightuserdata is pushed on the stack, if its segment is not stored
in vector new value is appended to of this vector. The maximum amount of
segments is 256. BADLU error is raised in case when user tried to add
userdata with the new 257-th segment, so the whole VA-space isn't
covered by this patch.

Also, in this patch all internal usage of lightuserdata (for hooks,
profilers, built-in package, IR and so on) is changed to special values
on Lua Stack.

Also, conversion of TValue to FFI C type with store is no longer
compiled for lightuserdata.

[1]: https://www.kernel.org/doc/html/latest/arm64/memory.html

Sergey Kaplun:
* added the description and the test for the problem

Resolves tarantool/tarantool#2712
Needed for tarantool/tarantool#6154
===================================================================

Branch is force-pushed.

> 

-- 
Best regards,
Sergey Kaplun

  reply	other threads:[~2021-08-02 14:52 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-06 17:40 [Tarantool-patches] [PATCH luajit 0/2] arm64: fix 48-bit addresses issues Sergey Kaplun via Tarantool-patches
2021-07-06 17:40 ` [Tarantool-patches] [PATCH luajit 1/2] Add support for full-range 64 bit lightuserdata Sergey Kaplun via Tarantool-patches
2021-07-27 13:59   ` Igor Munkin via Tarantool-patches
2021-07-28 12:29     ` Sergey Kaplun via Tarantool-patches
2021-08-01 10:36       ` Igor Munkin via Tarantool-patches
2021-08-02 14:56         ` Sergey Kaplun via Tarantool-patches
2021-08-01 16:25       ` Sergey Ostanevich via Tarantool-patches
2021-08-02 14:51         ` Sergey Kaplun via Tarantool-patches [this message]
2021-08-02 15:42           ` Igor Munkin via Tarantool-patches
2021-08-10 16:46           ` Sergey Ostanevich via Tarantool-patches
2021-08-11  5:54             ` Vitaliia Ioffe via Tarantool-patches
2021-07-06 17:40 ` [Tarantool-patches] [PATCH luajit 2/2] Linux/ARM64: Make mremap() non-moving due to VA space woes Sergey Kaplun via Tarantool-patches
2021-07-27 15:23   ` Igor Munkin via Tarantool-patches
2021-07-28 12:29     ` Sergey Kaplun via Tarantool-patches
2021-08-01 10:36       ` Igor Munkin via Tarantool-patches
2021-08-01 16:59         ` Sergey Ostanevich via Tarantool-patches
2021-08-02 15:08           ` Sergey Kaplun via Tarantool-patches
2021-08-02 15:55             ` Sergey Ostanevich via Tarantool-patches
2021-08-02 15:11         ` Sergey Kaplun via Tarantool-patches
2021-08-11  7:21 ` [Tarantool-patches] [PATCH luajit 0/2] arm64: fix 48-bit addresses issues Igor Munkin via Tarantool-patches

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YQgGcfW7bJ4j9Iqk@root \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=sergos@tarantool.org \
    --cc=skaplun@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH luajit 1/2] Add support for full-range 64 bit lightuserdata.' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox