Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp362710ybb; Wed, 25 Mar 2020 01:00:26 -0700 (PDT) X-Google-Smtp-Source: ADFU+vvmXxw9J6xKUEkv4+rzu6Xvaer0zIF4v1H6aUzFmBBFwr72UaJcc4XpiM6k8Mdr3ARntk0w X-Received: by 2002:a05:6830:118d:: with SMTP id u13mr1475317otq.41.1585123226589; Wed, 25 Mar 2020 01:00:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1585123226; cv=none; d=google.com; s=arc-20160816; b=kH/5smT0XfW6otT+OE0vqXIKjZbAEE2Bt624btSDCogYKjqwa+dCNdbjG9//rPJcLS wSDDPeBN7KlALbCv8GIGk9br7VoEw23fcQTGuMVzqSTtp3SiSNOtVsIf8is5tFUMxv27 S/yAvZD/06075YPfOvWyikxh29lyiB8+kwnjivS2tWsSPSfUms7zBDdm23XYsYi7up1j SOCVZF2f5pzzfaqBkX8G2prPctV2pbujAWh1aSZFW7Jgo+05oApPeAPNrscTlBesMOHb rBHKbSDMUQ3V24vxXUCaQZQHm6huTsiEtTxCOZSw647TmTUlFoE8tJ2j68fdZemGjQbY ulrA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:to:cc:from:subject:references :mime-version:message-id:in-reply-to:date:dkim-signature; bh=hfFzAgFLiPhJI7PNYPoebZUNl36WTAu03kIuyGPpbI8=; b=LXpUWDhEFmkqlAEe46e0xhE9R2gXzyj6l8+jRUVhCaiY2XDROmPOh2q1XaeElC8GOq 7wM5MGn6GxdkjumNeNkyQgXNeduljTWyM1R2uS7paLrs+N63q/QlprPSg1vfOhD1ipxX Eg0qcIqRqG+cbzjiB24jguXvYrkm0LSin7MQtfIGV7DJ9TrBTTvwVo/EjT1rGh12mKi6 hxJLF3vxWaeq+mUpQrl+v1F5wn0Z6DhSWvcFQsbwbnOK+fhu8iYG8JCM9Oi2i2Vo6+dK R3J1EV10przVMlj59lEG/LcG+yjjDv8UeJQalbpBpSY0cuBj2Cl9zb5fDz3qpIeyA7eq 6w7A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=pxDYmGnK; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c17si9763505otr.109.2020.03.25.01.00.11; Wed, 25 Mar 2020 01:00:26 -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; dkim=pass header.i=@google.com header.s=20161025 header.b=pxDYmGnK; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726276AbgCYH7l (ORCPT + 99 others); Wed, 25 Mar 2020 03:59:41 -0400 Received: from mail-wm1-f73.google.com ([209.85.128.73]:41738 "EHLO mail-wm1-f73.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725903AbgCYH7l (ORCPT ); Wed, 25 Mar 2020 03:59:41 -0400 Received: by mail-wm1-f73.google.com with SMTP id f207so567931wme.6 for ; Wed, 25 Mar 2020 00:59:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:cc; bh=hfFzAgFLiPhJI7PNYPoebZUNl36WTAu03kIuyGPpbI8=; b=pxDYmGnKOS3Ef4pWxZN1Ura0RIq7zlN5bDScyl2R/APyLXHYdrpVd4JasW3YV8ovxc tpv/HetTuguGF2H9tM3EvvcPvihKmJPH0GAI7CyAMdHHbiHlPMjp9wavU26rHP4DHW/W zBVkQ5hFebZocWV/Qqa2rZe6/J0hHu3a+w7KlxBT/8Kw94DRetTSnjwc8Q/bXg9wLEyU oo/upaNvUI8DEAdrH2pO3OGukNsyZu8Ah8ClzpJvH/KSBHHt1Ok1j2sdN44M1BJOdyT3 FgMckxzlWTQdyPx5Zp6Gl6LhzLUmL71QUe5ZoyfuuLdN7t97o03NpOgsXPOxFHZ58pzh KvNw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:cc; bh=hfFzAgFLiPhJI7PNYPoebZUNl36WTAu03kIuyGPpbI8=; b=m3sP7y1DSc+AybqaFn6zIMvykYNkduhzuM9GfZ9NL0pdsi/cgjI8P72783rjuCV0aK NOvdzo7IzSM5tUymvmcxN6A9rqNr2oEukxlpUO/UOtF5HBVxYdbjGFzBBqMJjMSjBmky Ck7v31gQ5ZZ2kmCD8LJBYYM6nqiGJYyggYck/gNGg6bxXxVu9kqj2XaZGw7wZovgoXOs L1Cli1PLjwW37jkprYE+feZhOdrtafutz5Y9+uuC+4M0r3hiB0UHuVGwALkVH9qtZP3m D3JalZ+RqDZ9aUxZwnUEzeT2c4ZkFsdyd9eqUoiJo9ICkEB9syuX9qrIwsRQdfDcbc3Z PP0g== X-Gm-Message-State: ANhLgQ3oSxyAanXl0Ey1VRlI2oygyL7ORCw2Sh8xQe66Zv4bVnB/ZXsM GAtSBq+/+Vy+DPfZDOXURC8RsNAnLdk6 X-Received: by 2002:adf:cf09:: with SMTP id o9mr1950627wrj.74.1585123179453; Wed, 25 Mar 2020 00:59:39 -0700 (PDT) Date: Wed, 25 Mar 2020 08:59:22 +0100 In-Reply-To: <20200323114207.222412-1-courbet@google.com> Message-Id: <20200325075924.109330-1-courbet@google.com> Mime-Version: 1.0 References: <20200323114207.222412-1-courbet@google.com> X-Mailer: git-send-email 2.25.1.696.g5e7596f4ac-goog Subject: [PATCH] x86: Alias memset to __builtin_memset. From: Clement Courbet Cc: Nathan Chancellor , Kees Cook , Nick Desaulniers , Joe Perches , Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , x86@kernel.org, Clement Courbet , linux-kernel@vger.kernel.org, clang-built-linux@googlegroups.com Content-Type: text/plain; charset="UTF-8" To: unlisted-recipients:; (no To-header on input) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org > Not sure we should modify this for someone's downstream tree to work > around an issue they introduced. If you want to file a bug > internally, I'd be happy to comment on it. I don't have a strong opinion on that. I don't know the policy of the linux kernel w.r.t. this. There is an internal bug for this, where kernel maintainers suggested I upstream this patch. > Does __builtin_memset detect support for `rep stosb`, then patch the > kernel to always use it or not? __builtin_memset just allows the compiler to recognize that this has the semantics of a memset, exactly like it happens when -freestanding is not specified. In terms of what compilers do when expanding the memset, it depends on the size. gcc or clang obviously do not generate vector code when -no-sse is specified, as this would break promises. clang inlines stores for small sizes and switches to memset as the size increases: https://godbolt.org/z/_X16xt gcc inlines stores for tiny sizes, then switches to repstos for larger sizes: https://godbolt.org/z/m-G233 This behaviour is additionally configurable through command line flags: https://godbolt.org/z/wsoJpQ > Did you verify what this change does for other compilers? Are there other compilers are used to build the kernel on x86 ? icc does the same as gcc and clang: https://godbolt.org/z/yCju_D > yet it doesn't use vector operations for the general case I'm not sure how vector operations relate to freestanding, or this change. > Adding -ffreestanding to optimize one hot memset in > one function is using a really big hammer to solve the wrong > problem. -ffreestanding was not added to optimize anything. It was already there. If anything -ffreestanding actually pessimizes a lot of the code generation, as it prevents the compiler from recognizing the semantics of common primitives. This is what this change is trying to fix. Removing -ffreestanding from the options is another (easier?) way to fix the problem. This change is a stab at accomodating both those who want freestanding and those who want performance. The hot memset I mentionned was just the hottest one. But as you can imagine there are many constant-size memsets spread across many functions, some of which are also very hot, the others constituting a long tail which is not negligible.