Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp1802498rwb; Thu, 29 Sep 2022 02:23:57 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7gC9twxbUXon+ryH4CJ8tw7OPaNujZ+ajN6wdtK3CgaNSfvvGIoSIV6Uf9YQSCwg8tB4CN X-Received: by 2002:a17:907:7ba1:b0:783:198e:7df2 with SMTP id ne33-20020a1709077ba100b00783198e7df2mr1944108ejc.126.1664443437365; Thu, 29 Sep 2022 02:23:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664443437; cv=none; d=google.com; s=arc-20160816; b=Lq4UKMoJoZUGwZkNGEeDgCOl9jjM9eLkiiA8O1NDFLrJQdOlfnYrw3VTtuYfz+nxIV IephYJd0Y4JfoXsE3bWEK4h6EK+Jy7TAuOxQl+shKl1Mk1NHV0BksG6TL4yI9MhQHzpE TEjfE3Kc0x2s9KTrp+Csydip0IpZTL8uRdkQX3HF8knMNgwGTPXR+y6T21c2eZYlP3Dy E7t5l2+MQk1x4iS5TTKVJUg/elZbxgymxAKhPRqIOXPa4UG4CsYt6KFU0pN2lvrCwt3P +vujSgTwkdpXerDFu+6F/2kHE4GhvK6VZeDpFi19ExwCfTCthwfF4J5X05fRo8x0FyBD jhXw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:dkim-signature:date; bh=zM5eWlusI4fZeTPCTlJzvY2vl0b4z7Eo9YuNDNRRBfg=; b=j4BsjG4I82IiEUelRpRBrrmcvky9hpmL2DIqMejt5biP0+2rDU0GE8PFBTlAuBaOgk HntjoLK8oQVPzvDEQRoniiz6pSROb/fxX3llix+N97dalNKREyQH6NI0wIu9vucGZEuY VD3Aw89Ws8D3q+irn5QcjqwFBRqYsLGK7jEjB2ZbKkD9pUg9FG3srkjK81oLj0nUBE08 E5HHmiA7Z/La12F1sLoDIPeuGYB3Hzi0AUs2gN6BFzS2uITJuY4mb+hufAWR9Pt2hVs3 c1aWZ4sSQsz8GOgHEndsXrzigxoVACFw2VWs67wPR9J6ujg1vKOmSpUHvFLAzegZYD8B vE6g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=w8KL9UJa; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t23-20020a170906609700b00781599eb7d9si7098138ejj.542.2022.09.29.02.23.20; Thu, 29 Sep 2022 02:23:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=w8KL9UJa; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235409AbiI2ItI (ORCPT + 99 others); Thu, 29 Sep 2022 04:49:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53878 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235183AbiI2ItH (ORCPT ); Thu, 29 Sep 2022 04:49:07 -0400 Received: from out1.migadu.com (out1.migadu.com [IPv6:2001:41d0:2:863f::]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E25BC133CBD; Thu, 29 Sep 2022 01:49:05 -0700 (PDT) Date: Thu, 29 Sep 2022 10:48:55 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1664441343; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=zM5eWlusI4fZeTPCTlJzvY2vl0b4z7Eo9YuNDNRRBfg=; b=w8KL9UJaiGZ6rWz/TPpHN7naUgaciFFyEvmIwoltT/LPc4XLtYjE/MblKXB0gTKuZ/kegP FZSLeSczifEN3rrwRG7tjjJqCldwsBokBeJru5d+KO0EayDeNbz69prIp4xan1MZSRDUx4 738oa4Rn1gipk1Mu49eLl0kpAKi0uOg= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Andrew Jones To: Sean Christopherson Cc: Paolo Bonzini , Nathan Chancellor , Nick Desaulniers , Tom Rix , kvm@vger.kernel.org, llvm@lists.linux.dev, linux-kernel@vger.kernel.org, Anup Patel , Atish Patra , Christian Borntraeger , Janosch Frank , Claudio Imbrenda , Oliver Upton , Jim Mattson Subject: Re: [PATCH v2 1/7] KVM: selftests: Implement memcmp(), memcpy(), and memset() for guest use Message-ID: <20220929084855.26t6r6aaurm2caum@kamzik> References: <20220928233652.783504-1-seanjc@google.com> <20220928233652.783504-2-seanjc@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220928233652.783504-2-seanjc@google.com> X-Migadu-Flow: FLOW_OUT X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW,SPF_HELO_PASS, SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Sep 28, 2022 at 11:36:46PM +0000, Sean Christopherson wrote: > Implement memcmp(), memcpy(), and memset() to override the compiler's > built-in versions in order to guarantee that the compiler won't generate > out-of-line calls to external functions via the PLT. This allows the > helpers to be safely used in guest code, as KVM selftests don't support > dynamic loading of guest code. > > Steal the implementations from the kernel's generic versions, sans the > optimizations in memcmp() for unaligned accesses. > > Put the utilities in a separate compilation unit and build with > -ffreestanding to fudge around a gcc "feature" where it will optimize > memset(), memcpy(), etc... by generating a recursive call. I.e. the > compiler optimizes itself into infinite recursion. Alternatively, the > individual functions could be tagged with > optimize("no-tree-loop-distribute-patterns"), but using "optimize" for > anything but debug is discouraged, and Linus NAK'd the use of the flag > in the kernel proper[*]. > > https://lore.kernel.org/lkml/CAHk-=wik-oXnUpfZ6Hw37uLykc-_P0Apyn2XuX-odh-3Nzop8w@mail.gmail.com > > Cc: Andrew Jones > Cc: Anup Patel > Cc: Atish Patra > Cc: Christian Borntraeger > Cc: Janosch Frank > Cc: Claudio Imbrenda > Signed-off-by: Sean Christopherson > --- > tools/testing/selftests/kvm/Makefile | 11 +++++- > .../selftests/kvm/lib/string_override.c | 39 +++++++++++++++++++ > 2 files changed, 49 insertions(+), 1 deletion(-) > create mode 100644 tools/testing/selftests/kvm/lib/string_override.c > > diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile > index 8b1b32628ac8..681816df69cc 100644 > --- a/tools/testing/selftests/kvm/Makefile > +++ b/tools/testing/selftests/kvm/Makefile > @@ -48,6 +48,8 @@ LIBKVM += lib/rbtree.c > LIBKVM += lib/sparsebit.c > LIBKVM += lib/test_util.c > > +LIBKVM_STRING += lib/string_override.c > + > LIBKVM_x86_64 += lib/x86_64/apic.c > LIBKVM_x86_64 += lib/x86_64/handlers.S > LIBKVM_x86_64 += lib/x86_64/perf_test_util.c > @@ -221,7 +223,8 @@ LIBKVM_C := $(filter %.c,$(LIBKVM)) > LIBKVM_S := $(filter %.S,$(LIBKVM)) > LIBKVM_C_OBJ := $(patsubst %.c, $(OUTPUT)/%.o, $(LIBKVM_C)) > LIBKVM_S_OBJ := $(patsubst %.S, $(OUTPUT)/%.o, $(LIBKVM_S)) > -LIBKVM_OBJS = $(LIBKVM_C_OBJ) $(LIBKVM_S_OBJ) > +LIBKVM_STRING_OBJ := $(patsubst %.c, $(OUTPUT)/%.o, $(LIBKVM_STRING)) > +LIBKVM_OBJS = $(LIBKVM_C_OBJ) $(LIBKVM_S_OBJ) $(LIBKVM_STRING_OBJ) > > EXTRA_CLEAN += $(LIBKVM_OBJS) cscope.* > > @@ -232,6 +235,12 @@ $(LIBKVM_C_OBJ): $(OUTPUT)/%.o: %.c > $(LIBKVM_S_OBJ): $(OUTPUT)/%.o: %.S > $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c $< -o $@ > > +# Compile the string overrides as freestanding to prevent the compiler from > +# generating self-referential code, e.g. with "freestanding" the compiler may ^ without > +# "optimize" memcmp() by invoking memcmp(), thus causing infinite recursion. > +$(LIBKVM_STRING_OBJ): $(OUTPUT)/%.o: %.c > + $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c -ffreestanding $< -o $@ > + > x := $(shell mkdir -p $(sort $(dir $(TEST_GEN_PROGS)))) > $(TEST_GEN_PROGS): $(LIBKVM_OBJS) > $(TEST_GEN_PROGS_EXTENDED): $(LIBKVM_OBJS) > diff --git a/tools/testing/selftests/kvm/lib/string_override.c b/tools/testing/selftests/kvm/lib/string_override.c > new file mode 100644 > index 000000000000..632398adc229 > --- /dev/null > +++ b/tools/testing/selftests/kvm/lib/string_override.c > @@ -0,0 +1,39 @@ > +// SPDX-License-Identifier: GPL-2.0-only > +#include > + > +/* > + * Override the "basic" built-in string helpers so that they can be used in > + * guest code. KVM selftests don't support dynamic loading in guest code and > + * will jump into the weeds if the compiler decides to insert an out-of-line > + * call via the PLT. > + */ > +int memcmp(const void *cs, const void *ct, size_t count) > +{ > + const unsigned char *su1, *su2; > + int res = 0; > + > + for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--) { > + if ((res = *su1 - *su2) != 0) > + break; > + } > + return res; > +} > + > +void *memcpy(void *dest, const void *src, size_t count) > +{ > + char *tmp = dest; > + const char *s = src; > + > + while (count--) > + *tmp++ = *s++; > + return dest; > +} > + > +void *memset(void *s, int c, size_t count) > +{ > + char *xs = s; > + > + while (count--) > + *xs++ = c; > + return s; > +} > -- > 2.37.3.998.g577e59143f-goog > Otherwise Reviewed-by: Andrew Jones