public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Evan Green <evan@rivosinc.com>
To: enh <enh@google.com>
Cc: Richard Henderson <richard.henderson@linaro.org>,
	Florian Weimer <fweimer@redhat.com>,
	 libc-alpha@sourceware.org, slewis@rivosinc.com,
	palmer@rivosinc.com,  vineetg@rivosinc.com
Subject: Re: [PATCH v6 5/5] riscv: Add and use alignment-ignorant memcpy
Date: Mon, 7 Aug 2023 17:01:07 -0700	[thread overview]
Message-ID: <CALs-HstqZSQkSfdfZ=+q6TLYFO7e3PnVFoEfWk2GF+TH9Q9H0A@mail.gmail.com> (raw)
In-Reply-To: <CAJgzZorf_25VC1Ssbrz4dFy7YpeHj8habFMQaOzg78nafP3WMA@mail.gmail.com>

On Mon, Aug 7, 2023 at 3:48 PM enh <enh@google.com> wrote:
>
> On Mon, Aug 7, 2023 at 3:11 PM Evan Green <evan@rivosinc.com> wrote:
> >
> > On Thu, Aug 3, 2023 at 3:30 PM Richard Henderson
> > <richard.henderson@linaro.org> wrote:
> > >
> > > On 8/3/23 11:42, Evan Green wrote:
> > > > On Thu, Aug 3, 2023 at 10:50 AM Richard Henderson
> > > > <richard.henderson@linaro.org> wrote:
> > > >> Outside libc something is required.
> > > >>
> > > >> An extra parameter to ifunc is surprising though, and clearly not ideal per the extra
> > > >> hoops above.  I would hope for something with hidden visibility in libc_nonshared.a that
> > > >> could always be called directly.
> > > >
> > > > My previous spin took that approach, defining a
> > > > __riscv_hwprobe_early() in libc_nonshared that could route to the real
> > > > function if available, or make the syscall directly if not. But that
> > > > approach had the drawback that ifunc users couldn't take advantage of
> > > > the vDSO, and then all users had to comprehend the difference between
> > > > __riscv_hwprobe() and __riscv_hwprobe_early().
> > >
> > > I would define __riscv_hwprobe such that it could take advantage of the vDSO once
> > > initialization reaches a certain point, but cope with being run earlier than that point by
> > > falling back to the syscall.
> > >
> > > That constrains the implementation, I guess, in that it can't set errno, but just
> > > returning the negative errno from the syscall seems fine.
> > >
> > > It might be tricky to get a reference to GLRO(dl_vdso_riscv_hwprobe) very early, but I
> > > would hope that some application of __attribute__((weak)) might correctly get you a NULL
> > > prior to full relocations being complete.
> >
> > Right, this is what we had in the previous iteration of this series,
> > and it did work ok. But it wasn't as good since it meant ifunc
> > selectors always got stuck in the null/fallback case and were forced
> > to make the syscall. With this mechanism they get to take advantage of
> > the vDSO.
> >
> > >
> > >
> > > > In contrast, IMO this approach is much nicer. Ifunc writers are
> > > > already used to getting hwcap info via a parameter. Adding this second
> > > > parameter, which also provides hwcap-like things, seems like a natural
> > > > extension. I didn't quite follow what you meant by the "extra hoops
> > > > above".
> > >
> > > The check for null function pointer, for sure.  But also consider how __riscv_hwprobe is
> > > going to be used.
> > >
> > > It might be worth defining some helper functions for probing a single key or a single
> > > field.  E.g.
> > >
> > > uint64_t __riscv_hwprobe_one_key(int64_t key, unsigned int flags)
> > > {
> > >    struct riscv_hwprobe pair = { .key = key };
> > >    int err = __riscv_hwprobe(&pair, 1, 0, NULL, flags);
> > >    if (err)
> > >      return err;
> > >    if (pair.key == -1)
> > >      return -ENOENT;
> > >    return pair.value;
> > > }
> > >
> > > This implementation requires that no future hwprobe key define a value which as a valid
> > > value in the errno range (or better, bit 63 unused).  Alternately, or additionally:
> > >
> > > bool __riscv_hwprobe_one_mask(int64_t key, uint64_t mask, uint64_t val, int flags)
> > > {
> > >    struct riscv_hwprobe pair = { .key = key };
> > >    return (__riscv_hwprobe(&pair, 1, 0, NULL, flags) == 0
> > >            && pair.key != -1
> > >            && (pair.value & mask) == val);
> > > }
> > >
> > > These yield either
> > >
> > >      int64_t v = __riscv_hwprobe_one_key(CPUPERF_0, 0);
> > >      if (v >= 0 && (v & MISALIGNED_MASK) == MISALIGNED_FAST)
> > >        return __memcpy_noalignment;
> > >      return __memcpy_generic;
> > >
> > > or
> > >
> > >      if (__riscv_hwprobe_one_mask(CPUPERF_0, MISALIGNED_MASK, MISALIGNED_FAST, 0))
> > >        return __memcpy_noalignment;
> > >      return __memcpy_generic;
> > >
> > > which to my mind looks much better for a pattern you'll be replicating so very many times
> > > across all of the ifunc implementations in the system.
> >
> > Ah, I see. I could make a static inline function in the header that
> > looks something like this (mangled by gmail, sorry):
> >
> > /* Helper function usable from ifunc selectors that probes a single key. */
> > static inline int __riscv_hwprobe_one(__riscv_hwprobe_t hwprobe_func,
> > signed long long int key,
> > unsigned long long int *value)
> > {
> > struct riscv_hwprobe pair;
> > int rc;
> >
> > if (!hwprobe_func)
> > return -ENOSYS;
> >
> > pair.key = key;
> > rc = hwprobe_func(&pair, 1, 0, NULL, 0);
> > if (rc) {
> > return rc;
> > }
> >
> > if (pair.key < 0) {
> > return -ENOENT;
> > }
> >
> > *value = pair.value;
> > return 0;
> > }
> >
> > The ifunc selector would then be significantly cleaned up, looking
> > something like:
> >
> > if (__riscv_hwprobe_one(hwprobe_func, RISCV_HWPROBE_KEY_CPUPERF_0, &value))
> > return __memcpy_generic;
> >
> > if (value & RISCV_HWPROBE_MISALIGNED_MASK) == RISCV_HWPROBE_MISALIGNED_FAST)
> > return __memcpy_noalignment;
>
> (Android's libc maintainer here, having joined the list just to talk
> about risc-v ifuncs :-) )
>
> has anyone thought about calling ifunc resolvers more like this...
>
> --same part of the dynamic loader that caches the two getauxval()s for arm64--
> static struct riscv_hwprobe probes[] = {
>  {.value = RISCV_HWPROBE_KEY_MVENDORID},
>  {.value = RISCV_HWPROBE_KEY_MARCHID},
>  {.value = RISCV_HWPROBE_KEY_MIMPID},
>  {.value = RISCV_HWPROBE_KEY_BASE_BEHAVIOR},
>  {.value = RISCV_HWPROBE_KEY_IMA_EXT},
>  {.value = RISCV_HWPROBE_KEY_CPUPERF_0},
> ... // every time a new key is added to the kernel, we add it here
> };
> __riscv_hwprobe(...); // called once
>
> --part of the dynamic loader that calls ifunc resolvers--
> (*ifunc_resolver)(sizeof(probes)/sizeof(probes[0]), probes);
>
> this is similar to what we already have for arm64 (where there's a
> getauxval(AT_HWCAP) and a pointer to a struct for AT_HWCAP2 and
> potentially others), but more uniform, and avoiding the source
> (in)compatibility issues of adding new fields to a struct [even if it
> does have a size_t to "version" it like the arm64 ifunc struct].
>
> yes, it means everyone pays to get all the hwprobes, but that gets
> amortized. and lookup in the ifunc resolver is simple and quick. if we
> know that the keys will be kept dense, we can even have code in ifunc
> resolvers like
>
> if (probes[RISCV_HWPROBE_BASE_BEHAVIOR_IMA].value & RISCV_HWPROBE_IMA_V) ...
>
> though personally for the "big ticket items" that get a letter to
> themselves like V, i'd be tempted to pass `(getauxval(AT_HWCAP),
> probe_count, probes_ptr)` to the resolver, but i hear that's
> controversial :-)

Hello, welcome to the fun! :)

What you're describing here is almost exactly what we did inside the
vDSO function. The vDSO function acts as a front for a handful of
probe values that we've already completed and cached in userspace. We
opted to make it a function, rather than exposing the data itself via
vDSO, so that we had future flexibility in what elements we cached in
userspace and their storage format. We can update the kernel as needed
to cache the hottest things in userspace, even if that means
rearranging the data format, passing through some extra information,
or adding an extra snip of code. My hope is callers can directly
interact with the vDSO function (though maybe as Richard suggested
maybe with the help of a tidy inline helper), rather than trying to
add a second layer of userspace caching.

-Evan

  reply	other threads:[~2023-08-08  0:01 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-02 15:58 [PATCH v6 0/5] RISC-V: ifunced memcpy using new kernel hwprobe interface Evan Green
2023-08-02 15:58 ` [PATCH v6 1/5] riscv: Add Linux hwprobe syscall support Evan Green
2023-08-02 16:52   ` Joseph Myers
2023-08-03  7:24   ` Florian Weimer
2023-08-02 15:59 ` [PATCH v6 2/5] riscv: Add hwprobe vdso call support Evan Green
2023-08-02 15:59 ` [PATCH v6 3/5] riscv: Add __riscv_hwprobe pointer to ifunc calls Evan Green
2023-08-02 15:59 ` [PATCH v6 4/5] riscv: Enable multi-arg ifunc resolvers Evan Green
2023-08-02 15:59 ` [PATCH v6 5/5] riscv: Add and use alignment-ignorant memcpy Evan Green
2023-08-03  7:25   ` Florian Weimer
2023-08-03 17:50     ` Richard Henderson
2023-08-03 18:42       ` Evan Green
2023-08-03 22:30         ` Richard Henderson
2023-08-07 22:10           ` Evan Green
2023-08-07 22:21             ` Florian Weimer
2023-08-07 22:30               ` Evan Green
2023-08-07 22:48             ` enh
2023-08-08  0:01               ` Evan Green [this message]
2023-08-12  0:01                 ` enh
2023-08-15 16:40                   ` Evan Green
2023-08-15 21:53                     ` enh
2023-08-15 23:01                       ` Evan Green
2023-08-16 23:18                         ` enh
2023-08-17 16:27                           ` Evan Green
2023-08-17 16:37                             ` enh
2023-08-17 17:40                               ` Evan Green
2023-08-22 15:06                                 ` enh
2023-08-02 16:03 ` [PATCH v6 0/5] RISC-V: ifunced memcpy using new kernel hwprobe interface Evan Green

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='CALs-HstqZSQkSfdfZ=+q6TLYFO7e3PnVFoEfWk2GF+TH9Q9H0A@mail.gmail.com' \
    --to=evan@rivosinc.com \
    --cc=enh@google.com \
    --cc=fweimer@redhat.com \
    --cc=libc-alpha@sourceware.org \
    --cc=palmer@rivosinc.com \
    --cc=richard.henderson@linaro.org \
    --cc=slewis@rivosinc.com \
    --cc=vineetg@rivosinc.com \
    /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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).