From nobody Sat Feb 7 19:41:05 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AFDF3EB64DD for ; Fri, 21 Jul 2023 22:33:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230339AbjGUWd6 (ORCPT ); Fri, 21 Jul 2023 18:33:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40128 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229846AbjGUWd4 (ORCPT ); Fri, 21 Jul 2023 18:33:56 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 943C43580 for ; Fri, 21 Jul 2023 15:33:55 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5704995f964so25571157b3.2 for ; Fri, 21 Jul 2023 15:33:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1689978835; x=1690583635; h=cc:to:from:subject:message-id:mime-version:date:reply-to:from:to:cc :subject:date:message-id:reply-to; bh=USyIrgY7glI2I6Y1FEnoLFsj21yaEAeGNlnCwa16vxU=; b=ck1pTWUbkvhYUxYB5e3viKVyP78tg8VHF5sR2juNPhLSnFq0EFHX1xxLtMdZ+DlJHv 53/ZCwbpK6IoS+/Gxs3zFHg/n/JJ+ZBUR/hLZDr7H+g4ZbMdK2oYX44p9KWLXISfiszC D2Ctel3HzQBxkhJy0RmGB4dvaF40UH2HQq3B6wEV3oddZVylMso+Y87Zf4PFRd891SKy 3KNFLw1jukABNrr0YMUU09sAsKYBtlDBZKlWrVXDI9KK6tbdrmTpH0vQPklpryYRKLLY 2i6keBlqyy/wOYi6IYGOQ65X03CutWtBYcMPgafqDrRpI3xA7Jni8BLbJDCOC5Tn/DGj B05w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689978835; x=1690583635; h=cc:to:from:subject:message-id:mime-version:date:reply-to :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=USyIrgY7glI2I6Y1FEnoLFsj21yaEAeGNlnCwa16vxU=; b=hx+bxl0BR3yTLbJXQj73W9c42tOkA/S9QKWZ5DmwEKtZRmPgavhVB8xZt1XbhF4wMz N1pBagmE6b9DyO68VNGVi3QEl0znIsiXWhaL7h70WMOXZdg4gbzWsg6o6Euzu/UydlYK JDqf512G1CXCSPo91bjV+iANZ5F1k3QBDMK/Zyl6h0fdgYVqEZptVwMRpl9MdMiVSq/X GhSpGRjDtsQssTa/XJqJj47MYTf1S1diLZiULZ61GDBGKIliNxIPCr3lzkAnw7io3hP3 W6xbup5Ux3qReSX82OY1lMSgGQsJdBHfDfkj7V/tDkpoDWQNkzSRWj4GTkNaUflkEhhS MAdg== X-Gm-Message-State: ABy/qLYfFvPci5paQ4V0uHS916UrlUqiYp0q1WyaN/Rb2ZfadLfaGH2q uZ7CNaiW8ZaIOahhaZEi1Do3akrvZ6k= X-Google-Smtp-Source: APBJJlH3r7Bida35VU4uL5e0ibtdttucr6EEWOFLP+th9pzb1aucpDZ9X30Dx96SzQEYp4ro5KvPm1YaqH8= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:24a:b0:cea:fb07:7629 with SMTP id k10-20020a056902024a00b00ceafb077629mr22897ybs.10.1689978834851; Fri, 21 Jul 2023 15:33:54 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 21 Jul 2023 15:33:52 -0700 Mime-Version: 1.0 X-Mailer: git-send-email 2.41.0.487.g6d72f3e995-goog Message-ID: <20230721223352.2333911-1-seanjc@google.com> Subject: [PATCH] selftests/rseq: Play nice with binaries statically linked against glibc 2.35+ From: Sean Christopherson To: Mathieu Desnoyers , Peter Zijlstra , "Paul E. McKenney" , Boqun Feng Cc: linux-kernel@vger.kernel.org, Aaron Lewis , kvm@vger.kernel.org, Sean Christopherson Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" To allow running rseq and KVM's rseq selftests as statically linked binaries, initialize the various "trampoline" pointers to point directly at the expect glibc symbols, and skip the dlysm() lookups if the rseq size is non-zero, i.e. the binary is statically linked *and* the libc registered its own rseq. Define weak versions of the symbols so as not to break linking against libc versions that don't support rseq in any capacity. The KVM selftests in particular are often statically linked so that they can be run on targets with very limited runtime environments, i.e. test machines. Fixes: 233e667e1ae3 ("selftests/rseq: Uplift rseq selftests for compatibili= ty with glibc-2.35") Cc: Aaron Lewis Cc: kvm@vger.kernel.org Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson Reviewed-by: Mathieu Desnoyers --- Note, this is very much the result of throwing noodles until something stuck, it seems like there's gotta be a less awful way to handle this :-( I Cc'd stable@ because I know I'm not the only person that runs statically linked KVM selftests, and figuring all this out was quite painful. tools/testing/selftests/rseq/rseq.c | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/tools/testing/selftests/rseq/rseq.c b/tools/testing/selftests/= rseq/rseq.c index 4e4aa006004c..a723da253244 100644 --- a/tools/testing/selftests/rseq/rseq.c +++ b/tools/testing/selftests/rseq/rseq.c @@ -34,9 +34,17 @@ #include "../kselftest.h" #include "rseq.h" =20 -static const ptrdiff_t *libc_rseq_offset_p; -static const unsigned int *libc_rseq_size_p; -static const unsigned int *libc_rseq_flags_p; +/* + * Define weak versions to play nice with binaries that are statically lin= ked + * against a libc that doesn't support registering its own rseq. + */ +__weak ptrdiff_t __rseq_offset; +__weak unsigned int __rseq_size; +__weak unsigned int __rseq_flags; + +static const ptrdiff_t *libc_rseq_offset_p =3D &__rseq_offset; +static const unsigned int *libc_rseq_size_p =3D &__rseq_size; +static const unsigned int *libc_rseq_flags_p =3D &__rseq_flags; =20 /* Offset from the thread pointer to the rseq area. */ ptrdiff_t rseq_offset; @@ -155,9 +163,17 @@ unsigned int get_rseq_feature_size(void) static __attribute__((constructor)) void rseq_init(void) { - libc_rseq_offset_p =3D dlsym(RTLD_NEXT, "__rseq_offset"); - libc_rseq_size_p =3D dlsym(RTLD_NEXT, "__rseq_size"); - libc_rseq_flags_p =3D dlsym(RTLD_NEXT, "__rseq_flags"); + /* + * If the libc's registered rseq size isn't already valid, it may be + * because the binary is dynamically linked and not necessarily due to + * libc not having registered a restartable sequence. Try to find the + * symbols if that's the case. + */ + if (!*libc_rseq_size_p) { + libc_rseq_offset_p =3D dlsym(RTLD_NEXT, "__rseq_offset"); + libc_rseq_size_p =3D dlsym(RTLD_NEXT, "__rseq_size"); + libc_rseq_flags_p =3D dlsym(RTLD_NEXT, "__rseq_flags"); + } if (libc_rseq_size_p && libc_rseq_offset_p && libc_rseq_flags_p && *libc_rseq_size_p !=3D 0) { /* rseq registration owned by glibc */ base-commit: 88bb466c9dec4f70d682cf38c685324e7b1b3d60 --=20 2.41.0.487.g6d72f3e995-goog