Received: by 2002:ac0:a582:0:0:0:0:0 with SMTP id m2-v6csp1734118imm; Tue, 2 Oct 2018 13:05:07 -0700 (PDT) X-Google-Smtp-Source: ACcGV60oh+8YgjKQY2rvXVqURxAHI93uc3qsbR/vS86WD6g2EbTHjUrhgw5cSold9HBvfHQzk2Z9 X-Received: by 2002:a17:902:70c3:: with SMTP id l3-v6mr18348765plt.185.1538510707460; Tue, 02 Oct 2018 13:05:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1538510707; cv=none; d=google.com; s=arc-20160816; b=bwEyue7a8ruWDKDgnoxHaseZqLLS7tt3ENTLtKgJAnblIopzx9XdTcz+/BeD84NZau jfiQANVh+aHtKjpZObRqVYr0/DdsQ1QaBMBEW1XvN+s6QInW9CppHeKAFcoNnecQPp+T cJt+dL1gX9+rLprr5LsXQlUHsOldXNHEXzNRcTBoK3laqPxjdcbUvlnoFESIo31PWKih 1toc65vvHIoUrIHSALQqBtVPk/Zdj5wzDi3SZ4CN4IfsSo/2xgLxDnVLSz5Z7MsBD3NJ HKbWMecTXgPW5thrsFW6g75SJhUl/hyfsqsFXpZ+L9rJj/dRCCxm8lygykzYbMF5WpOX mktw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :message-id:in-reply-to:subject:cc:to:from:date; bh=bNmSgGA1+p28neRe4H/Eq4rDtYEZImVdxt7LsdRNd7A=; b=zo3w5m8+qC4ul+70+R5jmypJ6ShmWIWbrQw3FJa54bBql+ECJWKQMX3kMGlGPTASiR XMJTfzcQvH0lJR7++W8fvD6o03laSoUSdM1LDWe3BHejBKnN2gy9ufMJ8mXGlSk9pVCw uWBRpwcpMIe+3VLNIPkA4OYG4U/tntbtyieogEgJJgGWmbpfyUSiIFxkBff+dKBhjmzT NJMJjhSMPHXQIogCYsMXcfi9jCXEYzsDBotpzyefaNQfOsfxx1MFcUD2NXmTTq/5Gr3a rdaM5ysT2bpmYmZghcVsyvLKx9N8TmPT0TlZGRDDwUSIVhze3VY0UA0+KzNoI9Qhx1Uy yTaQ== 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a15-v6si3923138plm.216.2018.10.02.13.04.49; Tue, 02 Oct 2018 13:05:07 -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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727450AbeJCCt2 (ORCPT + 99 others); Tue, 2 Oct 2018 22:49:28 -0400 Received: from Galois.linutronix.de ([146.0.238.70]:60928 "EHLO Galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726194AbeJCCt2 (ORCPT ); Tue, 2 Oct 2018 22:49:28 -0400 Received: from p5492e4c1.dip0.t-ipconnect.de ([84.146.228.193] helo=nanos) by Galois.linutronix.de with esmtpsa (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1g7Qtq-0002K0-7H; Tue, 02 Oct 2018 22:04:06 +0200 Date: Tue, 2 Oct 2018 22:04:05 +0200 (CEST) From: Thomas Gleixner To: Tim Chen cc: Jiri Kosina , Tom Lendacky , Ingo Molnar , Peter Zijlstra , Josh Poimboeuf , Andrea Arcangeli , David Woodhouse , Andi Kleen , Dave Hansen , Casey Schaufler , Asit Mallick , Arjan van de Ven , Jon Masters , linux-kernel@vger.kernel.org, x86@kernel.org Subject: Re: [Patch v2 1/4] x86/speculation: Option to select app to app mitigation for spectre_v2 In-Reply-To: Message-ID: References: User-Agent: Alpine 2.21 (DEB 202 2017-01-01) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-Linutronix-Spam-Score: -1.0 X-Linutronix-Spam-Level: - X-Linutronix-Spam-Status: No , -1.0 points, 5.0 required, ALL_TRUSTED=-1,SHORTCIRCUIT=-0.0001 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, 25 Sep 2018, Tim Chen wrote: > +enum spectre_v2_app2app_mitigation { > + SPECTRE_V2_APP2APP_NONE, > + SPECTRE_V2_APP2APP_LITE, > + SPECTRE_V2_APP2APP_STRICT, > +}; > > static enum spectre_v2_mitigation spectre_v2_enabled __ro_after_init = > SPECTRE_V2_NONE; > > +static enum spectre_v2_mitigation spectre_v2_app2app_enabled __ro_after_init = Copy and paste. The enum type is wrong. > + SPECTRE_V2_APP2APP_NONE; > + > +static enum spectre_v2_mitigation_cmd __init spectre_v2_parse_app2app_cmdline(void) Ditto > +{ > + char arg[20]; > + int ret, i; > + enum spectre_v2_mitigation_cmd cmd = SPECTRE_V2_APP2APP_CMD_AUTO; Please use reverse fir tree ordering of the variables: enum spectre_v2_mitigation_cmd cmd = SPECTRE_V2_APP2APP_CMD_AUTO; char arg[20]; int ret, i; > + > + ret = cmdline_find_option(boot_command_line, "spectre_v2_app2app", arg, sizeof(arg)); Line break around 78 please > @@ -325,6 +383,9 @@ static enum spectre_v2_mitigation_cmd __init spectre_v2_parse_cmdline(void) > > static bool stibp_needed(void) > { > + if (static_branch_unlikely(&spectre_v2_app_lite)) > + return false; > + > if (spectre_v2_enabled == SPECTRE_V2_NONE) > return false; > > @@ -366,7 +427,9 @@ void arch_smt_update(void) > static void __init spectre_v2_select_mitigation(void) > { > enum spectre_v2_mitigation_cmd cmd = spectre_v2_parse_cmdline(); > + enum spectre_v2_app2app_mitigation_cmd app2app_cmd = spectre_v2_parse_app2app_cmdline(); Please avoid these overlong lines. Move the initialization to the code if it does not fit into the declaration part. > enum spectre_v2_mitigation mode = SPECTRE_V2_NONE; > + enum spectre_v2_app2app_mitigation app2app_mode = SPECTRE_V2_APP2APP_NONE; > /* > * If the CPU is not affected and the command line mode is NONE or AUTO > @@ -376,6 +439,17 @@ static void __init spectre_v2_select_mitigation(void) > (cmd == SPECTRE_V2_CMD_NONE || cmd == SPECTRE_V2_CMD_AUTO)) > return; > > + switch (app2app_cmd) { > + case SPECTRE_V2_APP2APP_CMD_LITE: > + case SPECTRE_V2_APP2APP_CMD_AUTO: > + app2app_mode = SPECTRE_V2_APP2APP_LITE; > + break; > + > + case SPECTRE_V2_APP2APP_CMD_STRICT: > + app2app_mode = SPECTRE_V2_APP2APP_STRICT; > + break; > + } > + > switch (cmd) { > case SPECTRE_V2_CMD_NONE: > return; > @@ -427,6 +501,11 @@ static void __init spectre_v2_select_mitigation(void) > } > > specv2_set_mode: > + spectre_v2_app2app_enabled = app2app_mode; > + pr_info("%s\n", spectre_v2_app2app_strings[app2app_mode]); > + if (app2app_mode == SPECTRE_V2_APP2APP_LITE) > + static_branch_enable(&spectre_v2_app_lite); > + > spectre_v2_enabled = mode; > pr_info("%s\n", spectre_v2_strings[mode]); > > @@ -441,8 +520,8 @@ static void __init spectre_v2_select_mitigation(void) > setup_force_cpu_cap(X86_FEATURE_RSB_CTXSW); > pr_info("Spectre v2 / SpectreRSB mitigation: Filling RSB on context switch\n"); > > - /* Initialize Indirect Branch Prediction Barrier if supported */ > - if (boot_cpu_has(X86_FEATURE_IBPB)) { > + /* Initialize Indirect Branch Prediction Barrier if supported and not disabled */ > + if (boot_cpu_has(X86_FEATURE_IBPB) && app2app_mode != SPECTRE_V2_APP2APP_NONE) { Line breaks exist for a reason. Applies to comments and code. > setup_force_cpu_cap(X86_FEATURE_USE_IBPB); > pr_info("Spectre v2 mitigation: Enabling Indirect Branch Prediction Barrier\n"); > } > @@ -875,8 +954,16 @@ static ssize_t cpu_show_common(struct device *dev, struct device_attribute *attr > > case X86_BUG_SPECTRE_V2: > mutex_lock(&spec_ctrl_mutex); > - ret = sprintf(buf, "%s%s%s%s%s%s\n", spectre_v2_strings[spectre_v2_enabled], > - boot_cpu_has(X86_FEATURE_USE_IBPB) ? ", IBPB" : "", > + if (static_branch_unlikely(&spectre_v2_app_lite)) > + ret = sprintf(buf, "%s%s%s%s%s%s\n", spectre_v2_strings[spectre_v2_enabled], > + boot_cpu_has(X86_FEATURE_USE_IBPB) ? ", IBPB-lite" : "", > + boot_cpu_has(X86_FEATURE_USE_IBRS_FW) ? ", IBRS_FW" : "", > + (x86_spec_ctrl_base & SPEC_CTRL_STIBP) ? ", STIBP" : "", > + boot_cpu_has(X86_FEATURE_RSB_CTXSW) ? ", RSB filling" : "", > + spectre_v2_module_string()); > + else > + ret = sprintf(buf, "%s%s%s%s%s%s\n", spectre_v2_strings[spectre_v2_enabled], > + boot_cpu_has(X86_FEATURE_USE_IBPB) ? ", IBPB-strict" : "", > boot_cpu_has(X86_FEATURE_USE_IBRS_FW) ? ", IBRS_FW" : "", > (x86_spec_ctrl_base & SPEC_CTRL_STIBP) ? ", STIBP" : "", > boot_cpu_has(X86_FEATURE_RSB_CTXSW) ? ", RSB filling" : "", Why do you need to copy the whole thing? What's wrong with using spectre_v2_app2app_enabled for getting the proper string for the IBPB mitigation? > - * Check if the current (previous) task has access to the memory > - * of the @tsk (next) task. If access is denied, make sure to > + * For lite protection mode, we only protect the non-dumpable > + * processes. > + * > + * Otherwise check if the current (previous) task has access to the memory > + * of the @tsk (next) task for strict app to app protection. > + * If access is denied, make sure to > * issue a IBPB to stop user->user Spectre-v2 attacks. > * > * Note: __ptrace_may_access() returns 0 or -ERRNO. > */ > - return (tsk && tsk->mm && tsk->mm->context.ctx_id != last_ctx_id && > - __ptrace_may_access(tsk, PTRACE_MODE_IBPB)); Needs to be updated to the latest code in tip x86/pti > + /* skip IBPB if no context changes */ > + if (!tsk || !tsk->mm || tsk->mm->context.ctx_id == last_ctx_id) > + return false; > + > + if (static_branch_unlikely(&spectre_v2_app_lite)) > + return (get_dumpable(tsk->mm) != SUID_DUMP_USER); > + else > + return (__ptrace_may_access(tsk, PTRACE_MODE_IBPB)); Neither of the branches needs braces for the return Thanks, tglx