Received: by 10.223.176.5 with SMTP id f5csp884176wra; Wed, 7 Feb 2018 09:03:11 -0800 (PST) X-Google-Smtp-Source: AH8x226EXoUb+MYX+nOFg8IH7MggZJh4ZoBi7xoz4q/wI22YsqzndLvf09/0Tnp19VnrLuIuv373 X-Received: by 10.101.80.69 with SMTP id k5mr5455646pgo.443.1518022990981; Wed, 07 Feb 2018 09:03:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518022990; cv=none; d=google.com; s=arc-20160816; b=Q6dOToKv4uSI1VtckVDISFvO4QYSvRdaSum20opyp21FjdXlOR1c1ASmfT1nZKL/i1 bpOJa7+Qwwwxs0iBiKbHU8YOcfT2mCVzQ7JQZIsp4nvXHmU1TobxGDJul4cEvLPYKTjm BgARu1ySV1LTZReuokGKpzY+AwaEiwqn1RBfALtozk1LmA4APLZ0cYLkLARiXNfdG2Wc 8ARsPZhzUFOFKswGcrEdPikOPwuKMzH/GqIvoFPkyto0XsANeITmuYYgku8xdLoiHPsg FJTLlVi4MlbB+eascxQzRSoVilAOZMdwO2cuKip7e4bhTBoKMfaXQttstPJgtsDrkYkP GrdQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:cc:to:subject :message-id:date:from:references:in-reply-to:mime-version :dkim-signature:arc-authentication-results; bh=/O/l0Lp3em6+Z77OcFLhY0OOYCGwW/Wam37OXYIirvY=; b=DtPACD76U6TY3az0ZDPjf8fmXWoACOnqwGm8DoPdPRFWNCWIdEYcaY+vKmhN6QrRdR ASN/Gu4qbZ5zSNsEwzhcclr4bLqvToDu92fWYjWeAsJMTtIpTys4ZcotF9LGjbBTAsCi N/MoFCmq1PoHo5xr/ReHakxrNgtvYMNPdYMWUAGUdvo3G5hFWJH2wSx4C7coSCfPER54 CTEeCLiH98pFHk60EZ2eAQKvZjmNS3GHWtT4elzUR74YcFgmh9LKE7LuzNdBqOMQImvo pmzzibTBN8QEIznCqpR2JcIIERO8q7IlQlqmEOrG/+aLk+Uf0DCgzKkT1rmH8b+8hudV XDVg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=M0XVPg1R; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a92-v6si1312078pla.543.2018.02.07.09.02.55; Wed, 07 Feb 2018 09:03:10 -0800 (PST) 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=fail header.i=@gmail.com header.s=20161025 header.b=M0XVPg1R; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754694AbeBGRBp (ORCPT + 99 others); Wed, 7 Feb 2018 12:01:45 -0500 Received: from mail-it0-f68.google.com ([209.85.214.68]:36323 "EHLO mail-it0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754392AbeBGRBn (ORCPT ); Wed, 7 Feb 2018 12:01:43 -0500 Received: by mail-it0-f68.google.com with SMTP id n206so3041138itg.1 for ; Wed, 07 Feb 2018 09:01:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc:content-transfer-encoding; bh=/O/l0Lp3em6+Z77OcFLhY0OOYCGwW/Wam37OXYIirvY=; b=M0XVPg1RMT+nB3WwWwdmtfPXcv3CmvjUg0L4V5ZO8tdP3WlJYMMuskjbPU1WrDpQF5 hzA4QihTpyJAD4naWUWrNiH49uafb9nQblCHQNKw9jg25atW9uGhycxM0NZKi1sUihBo MR0SkijTlYe0QKz1Nqp/Mfn19wIifRuuDw9ikAzoBnk+3VAU6Gjaoqfre5U4rKbQpIMl kWQUIGOGgr0UmPUAKnsnXHlbQjj9ikkmVJZuYPurQWmgx1893r4003cbw5LhWQz6RI1V 86SudSKhGePxCdiYiA5StTcmNuSftI9si3nyN9Pba8ArJgDL0/T+z3obvlLYfottWpJz gfcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:sender:in-reply-to:references:from :date:message-id:subject:to:cc:content-transfer-encoding; bh=/O/l0Lp3em6+Z77OcFLhY0OOYCGwW/Wam37OXYIirvY=; b=bW3tcylElOjW3odAZa+Eg6wiNvDzNXWxtUklfokbBcUz0xs0K8TKjDJ7hxO55qYPE8 MNLixnG/UhYkgFa562Q5Wt+QLh32EbxdKhtrvj//iqf/SBk43uilv4LvhqD4V0I5vqog j/+vohTBp8PKTK5CnyerCJ8Mb5ohVSU9LNvo1qJuusaEkbNb6WJsREc+nRVY6NGV8map cFdc7Qs/4ppWYWZrpKc9I5+g78h6o27jYJMSd+OHQEeqLETdR4AdH6aQXDBh3Zl1xYcD wZjGF6PBW4O1Vom2tim73wnWsb2p3N8+3uGukkszNcflagG6R2pqdCLaP5jIlUuSim3R Gf8Q== X-Gm-Message-State: APf1xPB26llZfNkSGzbXdognlPi5t3A8f+1jjF3wPuk37A1vzIfcaI80 wGMC356GYxkwvThrhvlZbOxjmS9OZrleynQHjHI= X-Received: by 10.36.47.5 with SMTP id j5mr8610049itj.123.1518022902804; Wed, 07 Feb 2018 09:01:42 -0800 (PST) MIME-Version: 1.0 Received: by 10.107.59.196 with HTTP; Wed, 7 Feb 2018 09:01:42 -0800 (PST) In-Reply-To: <20180207145913.2703-1-kirill.shutemov@linux.intel.com> References: <20180207145913.2703-1-kirill.shutemov@linux.intel.com> From: Linus Torvalds Date: Wed, 7 Feb 2018 09:01:42 -0800 X-Google-Sender-Auth: MHccQzpXKeT4MB7vQSwdXzvM23Y Message-ID: Subject: Re: [RFC 0/3] x86: Patchable constants To: "Kirill A. Shutemov" Cc: "the arch/x86 maintainers" , Tom Lendacky , Peter Zijlstra , Dave Hansen , Andy Lutomirski , Borislav Petkov , linux-mm , Linux Kernel Mailing List , Peter Anvin Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Feb 7, 2018 at 6:59 AM, Kirill A. Shutemov wrote: > This patchset introduces concept of patchable constants: constant values > that can be adjusted at boot-time in response to system configuration or > user input (kernel command-line). > > Patchable constants can replace variables that never changes at runtime > (only at boot-time), but used in very hot path. So I actually wanted something very close to this, but I think your approach is much too simplistic. You force all constants into a register, which means that the resulting code is always going to be very far from non-optimal. You also force a big "movabsq" instruction, which really is huge, and almost never needed. Together with the "use a register", it just makes for big code. What I wanted was something that can take things like a shift by a variable that is set once, and turn it into a shift by a boot-time constant. Which means that you literally end up patching the 8-bit immediate in the shift instruction itself. In particular, was looking at the dcache hashing code, and (to quote an old email of mine), what I wanted was to simplify the run-time constant part of this: =E2=94=82 mov $0x20,%ecx =E2=94=82 sub 0xaf8bd5(%rip),%ecx # ffffffff81d34600 =E2=94=82 mov 0x8(%rsi),%r9 =E2=94=82 add %r14d,%eax =E2=94=82 imul $0x9e370001,%eax,%eax =E2=94=82 shr %cl,%eax and it was the expression "32-d_hash_shift" that is really a constant, and that sequence of =E2=94=82 mov $0x20,%ecx =E2=94=82 sub 0xaf8bd5(%rip),%ecx # ffffffff81d34600 =E2=94=82 shr %cl,%eax should be just a single =E2=94=82 shr $CONSTANT,%eax at runtime. Look - much smaller code, and register %rcx isn't used at all. And no D$ miss on loading that constant (that is a constant depending on boot-time setup only). It's rather more complex, but it actually gives a much bigger win. The code itself will be much better, and smaller. The *infrastructure* for the code gets pretty hairy, though. The good news is that the patch already existed to at least _some_ degree. Peter Anvin did it about 18 months ago. It was not really pursued all the way because it *is* a lot of extra complexity, and I think there was some other hold-up, but he did have skeleton code for the actual replacement. There was a thread on the x86 arch list with the subject line Disgusting pseudo-self-modifying code idea: "variable constants" but I'm unable to actually find the patch. I know there was at least a vert early prototype. Adding hpa to the cc in the hope that he has some prototype code still laying around.. Linus