Received: by 2002:ac0:a594:0:0:0:0:0 with SMTP id m20-v6csp4724048imm; Mon, 14 May 2018 11:53:48 -0700 (PDT) X-Google-Smtp-Source: AB8JxZr3FjX3Ss1YqpxZ61K0t9F6Nnyag8uBXhEZf0jMkT/0OTSgT3N8Y5oo8/ICZMB6UBg6wXVV X-Received: by 2002:a62:c95c:: with SMTP id k89-v6mr11583613pfg.47.1526324028667; Mon, 14 May 2018 11:53:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1526324028; cv=none; d=google.com; s=arc-20160816; b=Pul4nTG1/8p5mrNlB3Id0jw5b6WiiWw1+9Nfoi+8AUFeCZ3T2Ao1juzhQbzmD4oYEH qpjZTOgv+WlzFUnxZhWKITP/cSQpLr7xpBuFMkPKk1OPCbn2h27n8aiegXR+I5f99hTd 2xEyV3i1Urdgu5HwcweHGZyYDLUwquxSFuZaZjYmWWAg0Qwi2vZ8UCRkkp4Ex+6fwHRk Q7gpnZTEeSE5EAJtC+A3/v/lv99ODRNRgUi7PF8H74OERl1cNpWQhU7GjMBxdRl0s7SY tGd9rvtqGVnREzevEhGxsxSMs9A52qXCZwOsWWxWFjykg5bJqJfqt2dZ8urEbIFAgkah z+Aw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=3we+uipFCeg+m/1uLgDdCpqINipzJlgdFKMZWaRjMGA=; b=uodd3dP18gdmY6YR1kHlQQekfHssQa9PYcaH/dkl6vJ9BrrWDv1sLFp7L1qyMhOHJD a3tSn2eCWXtL2XBUpT7qFxcHHp/23KE21kD6aTAvjN22DBKkDkKOxS0nEJLUbUeXLBiu 8QFCQE0elDmTaFxYXbAnPNuLw6s/hBEUDzJ9IQHmdTw71L72KCvHUZY+pKGivLZIjSbH 8wUKmZGyjJvWo2GUz+yOB914fH2KF87la4M1RdagoNwCM+PRmvy2TeUMevcaDcxj1okY yNi26w45EeyY+IV0NM4Ysiu62cXkaURGvIAYGttUOCuv15yBVhzXO46ZdQVXvjnkxPnJ iqKA== 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 l81-v6si10078595pfj.127.2018.05.14.11.53.34; Mon, 14 May 2018 11:53:48 -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 S1752251AbeENSws (ORCPT + 99 others); Mon, 14 May 2018 14:52:48 -0400 Received: from mga06.intel.com ([134.134.136.31]:55035 "EHLO mga06.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752087AbeENSwb (ORCPT ); Mon, 14 May 2018 14:52:31 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by orsmga104.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 14 May 2018 11:52:28 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.49,401,1520924400"; d="scan'208";a="39244653" Received: from romley-ivt3.sc.intel.com ([172.25.110.60]) by fmsmga007.fm.intel.com with ESMTP; 14 May 2018 11:52:28 -0700 From: Fenghua Yu To: "Thomas Gleixner" , "Ingo Molnar" , "H. Peter Anvin" , "Ashok Raj" , "Ravi V Shankar" , "Tony Luck" , "Dave Hansen" , "Rafael Wysocki" , "Arjan van de Ven" , "Alan Cox" Cc: "x86" , "linux-kernel" , Fenghua Yu Subject: [PATCH 11/15] x86/split_lock: Add sysfs interface to control user mode behavior Date: Mon, 14 May 2018 11:52:21 -0700 Message-Id: <1526323945-211107-12-git-send-email-fenghua.yu@intel.com> X-Mailer: git-send-email 2.5.0 In-Reply-To: <1526323945-211107-1-git-send-email-fenghua.yu@intel.com> References: <1526323945-211107-1-git-send-email-fenghua.yu@intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add the interface /sys/kernel/split_lock/user_mode to allow user to choose to either generate SIGBUS (default) when hitting split lock in user or re-execute the user faulting instruction without generating SIGBUS signal. Signed-off-by: Fenghua Yu --- arch/x86/kernel/cpu/split_lock.c | 89 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/arch/x86/kernel/cpu/split_lock.c b/arch/x86/kernel/cpu/split_lock.c index 5d399b09c1c8..02b461c48b3c 100644 --- a/arch/x86/kernel/cpu/split_lock.c +++ b/arch/x86/kernel/cpu/split_lock.c @@ -32,6 +32,19 @@ static DECLARE_DELAYED_WORK(delayed_work, delayed_reenable_split_lock); static DEFINE_MUTEX(split_lock_mutex); +enum { + USER_MODE_SIGBUS, + USER_MODE_RE_EXECUTE, + USER_MODE_LAST +}; + +static int user_mode_reaction = USER_MODE_SIGBUS; + +static const char * const user_modes[USER_MODE_LAST] = { + [USER_MODE_SIGBUS] = "sigbus", + [USER_MODE_RE_EXECUTE] = "re-execute", +}; + /* * On processors not supporting #AC exception for split lock feature, * MSR_TEST_CTL may not exist or MSR_TEST_CTL exists but the bit 29 is @@ -214,6 +227,16 @@ static void delayed_reenable_split_lock(struct work_struct *w) _setup_split_lock(ENABLE_SPLIT_LOCK_AC); } +static unsigned long eflags_ac(struct pt_regs *regs) +{ + return regs->flags & X86_EFLAGS_AC; +} + +static unsigned long cr0_am(struct pt_regs *regs) +{ + return read_cr0() & X86_CR0_AM; +} + /* Will the faulting instruction be re-executed? */ static bool re_execute(struct pt_regs *regs) { @@ -224,6 +247,24 @@ static bool re_execute(struct pt_regs *regs) if (!user_mode(regs)) return true; + /* + * Now check if the user faulting instruction can be re-executed. + * + * If both CR0.AM (Alignment Mask) and EFLAGS.AC (Alignment Check) + * are set in user space, any misalignment including split lock + * can trigger #AC. In this case, we just issue SIGBUS as standard + * #AC handler to the user process because split lock is not the + * definite reason for triggering this #AC. + * + * If either CR0.AM or EFLAGS.AC is zero, the only reason for + * triggering this #AC is split lock. So the faulting instruction + * can be re-executed if required by user. + */ + if (cr0_am(regs) == 0 || eflags_ac(regs) == 0) + /* User faulting instruction will be re-executed if required. */ + if (user_mode_reaction == USER_MODE_RE_EXECUTE) + return true; + return false; } @@ -323,8 +364,56 @@ static ssize_t enable_store(struct kobject *kobj, struct kobj_attribute *attr, static struct kobj_attribute split_lock_ac_enable = __ATTR_RW(enable); +static ssize_t +user_mode_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) +{ + char *s = buf; + int reaction; + + for (reaction = 0; reaction < USER_MODE_LAST; reaction++) { + if (reaction == user_mode_reaction) + s += sprintf(s, "[%s] ", user_modes[reaction]); + else + s += sprintf(s, "%s ", user_modes[reaction]); + } + + if (s != buf) + /* convert the last space to a newline */ + *(s - 1) = '\n'; + + return s - buf; +} + +static ssize_t +user_mode_store(struct kobject *kobj, struct kobj_attribute *attr, + const char *buf, size_t count) +{ + int reaction, len, error = -EINVAL; + const char * const *s, *p; + + p = memchr(buf, '\n', count); + len = p ? p - buf : count; + + mutex_lock(&split_lock_mutex); + reaction = USER_MODE_SIGBUS; + for (s = &user_modes[reaction]; reaction <= USER_MODE_LAST; + s++, reaction++) { + if (*s && len == strlen(*s) && !strncmp(buf, *s, len)) { + user_mode_reaction = reaction; + error = 0; + break; + } + } + mutex_unlock(&split_lock_mutex); + + return error ? error : count; +} + +static struct kobj_attribute split_lock_ac_user = __ATTR_RW(user_mode); + static struct attribute *split_lock_attrs[] = { &split_lock_ac_enable.attr, + &split_lock_ac_user.attr, NULL, }; -- 2.5.0