Received: by 2002:a25:c593:0:0:0:0:0 with SMTP id v141csp5774555ybe; Tue, 17 Sep 2019 13:18:14 -0700 (PDT) X-Google-Smtp-Source: APXvYqzui2Wac49Rbc3eegBUOdTheaZt/O4zd8nq72M2qTA6QtY6rHMxjRo/yed8kNZvEgSPY5EE X-Received: by 2002:a05:6402:13c9:: with SMTP id a9mr153210edx.25.1568751494267; Tue, 17 Sep 2019 13:18:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1568751494; cv=none; d=google.com; s=arc-20160816; b=bi0fJo65sFsOdpjgi20Pr9J1E724IdQh4wkspRxhBCxp26ZKlAOPdlC7lmt1rpAYh5 QVcT91izwKlqApjPOrFADn5h87AmtRDpBo/U5UYenuKTKUyXo0+0MdUij2NYBbi341Hl hR0882U4UPpB79wcM98NeFe84DW2sXroQXQFos3GJpbw9Yp5hJ49jAaJtwwPsILXgpI1 EYVhcT9XIW34uQGYxX3Q0J0gvQ+HT83HgySKToymV27qnkVLdV5oOBEOAXdLajBq2G1h invh/jnvZ7f3AVMWk9Mp+LxqzE6vedPhwyN0Y65qq9TCYQV2QpKLYTiwAu08UFVHAHGO ySQg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=0m7/ZumwLSv5t3mUYut6yK4VSYX7KlA2rU925YLpV3s=; b=a6f9ZWjYUcfbo1P+6k+qSGuexvaudEpfmC5VSIKrAWYoTTXxO156UWkYPHtpkk3+df YBBZ+PkrgtD2uopzq5VOIIM7ovi7QOrWs8dVvV5Fnx5tLKAc41jaPlJujT6bR3W01w3h b3gDkJIMvqJLoQydjJ/ZafgD9cFu14Hy09u++r7SPDB5mRGPjZQFtSvAX1KAhJ5x+hMr erIAOkfo4CQoh/s5P63e/SY606CJaSyeUpNgjew1nbQ6c4REgdVs103vjU6SEO4Fq8EQ m/FXKXg8I/OaHu+ZJko99qk7nxbMov8Mg1sVAhXqXXeGgyePwoTZRPQkRuKuM3ffEfS8 uJ0Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e49si2245952edb.87.2019.09.17.13.17.50; Tue, 17 Sep 2019 13:18:14 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727544AbfIQUKZ (ORCPT + 99 others); Tue, 17 Sep 2019 16:10:25 -0400 Received: from mx1.redhat.com ([209.132.183.28]:58606 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726404AbfIQUKY (ORCPT ); Tue, 17 Sep 2019 16:10:24 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C29E28665D; Tue, 17 Sep 2019 20:10:24 +0000 (UTC) Received: from treble (ovpn-120-32.rdu2.redhat.com [10.10.120.32]) by smtp.corp.redhat.com (Postfix) with ESMTPS id DCCB05D6B2; Tue, 17 Sep 2019 20:10:23 +0000 (UTC) Date: Tue, 17 Sep 2019 15:10:21 -0500 From: Josh Poimboeuf To: Borislav Petkov Cc: x86-ml , Andy Lutomirski , Linus Torvalds , lkml Subject: Re: [RFC] Improve memset Message-ID: <20190917201021.evoxxj7vkcb45rpg@treble> References: <20190913072237.GA12381@zn.tnic> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20190913072237.GA12381@zn.tnic> User-Agent: NeoMutt/20180716 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Tue, 17 Sep 2019 20:10:24 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Sep 13, 2019 at 09:22:37AM +0200, Borislav Petkov wrote: > In order to patch on machines which don't set X86_FEATURE_ERMS, I need > to do a "reversed" patching of sorts, i.e., patch when the x86 feature > flag is NOT set. See the below changes in alternative.c which basically > add a flags field to struct alt_instr and thus control the patching > behavior in apply_alternatives(). > > The result is this: > > static __always_inline void *memset(void *dest, int c, size_t n) > { > void *ret, *dummy; > > asm volatile(ALTERNATIVE_2_REVERSE("rep; stosb", > "call memset_rep", X86_FEATURE_ERMS, > "call memset_orig", X86_FEATURE_REP_GOOD) > : "=&D" (ret), "=a" (dummy) > : "0" (dest), "a" (c), "c" (n) > /* clobbers used by memset_orig() and memset_rep_good() */ > : "rsi", "rdx", "r8", "r9", "memory"); > > return dest; > } I think this also needs ASM_CALL_CONSTRAINT. Doesn't this break on older non-ERMS CPUs when the memset() is done early, before alternative patching? Could it instead do this? ALTERNATIVE_2("call memset_orig", "call memset_rep", X86_FEATURE_REP_GOOD, "rep; stosb", X86_FEATURE_ERMS) Then the "reverse alternatives" feature wouldn't be needed anyway. -- Josh