Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp2294135imm; Fri, 7 Sep 2018 14:06:13 -0700 (PDT) X-Google-Smtp-Source: ANB0VdZwyocKAzc5h5Uc5Joeq0SCtcSH5m82eNinFDcuFEs6qnuB8EnTRaZ4LVBVuM8GTbR2TpH8 X-Received: by 2002:a63:b95e:: with SMTP id v30-v6mr10102793pgo.221.1536354373110; Fri, 07 Sep 2018 14:06:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536354373; cv=none; d=google.com; s=arc-20160816; b=Xp0HlROzkNUDLJumiRnUIEop+FrMrGRCKIpoJ84aqhNM50ynm7QCGQoO3UJOEk9vRZ PESMSMK9VYsD7dehyoSuZYiinFCtD0Z7bNAKNbOx65GU/6IfLkHkVMOS3Z4DnfGC6VZi i4S1X8ddtQDSUlWiidFJI3QfiStkAa3aRHO/OeDXFDz0AwJSwtyNXc1vUzlDhDfspaRD 9dYzGxZWQhdlJkGwl7h3Wz3lEuTKhqXVFt6aLmyR2xRGSJUvMy6ML2NeHfdgNbgU4GbH FGmTKOx3lWAzTLzw7HHPMSrb+RP+aSG+uSnEcDZVhLaECQ3AYb7O8NzTQXdtVPJeRqoK j9ow== 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:mime-version :references:in-reply-to:date:cc:to:from:subject:message-id; bh=gA5vF0MyDzIfRonglcq4I7eXP2GwysHm0einB12KidQ=; b=qw9uRRywZoByE+6sTjM/AdNihotH/HnS28S0Fjx8JeO6V2aetSikpldyfJBpcEcDxT LpsM4YljazVmbLf8Pn9R6g7QKvwRbpJrmMr3GAtnVVkHeel4aBQmRgkmZ8AT4xPZc0EZ muevS8Vns8QYUlU43TIgrVQTeb85HHt92a8GFQdJHFVYOaGW7vt5qGZ1rjPuUYXEx4j1 XZpRE4D1szDNlG/PJyszFYKEuIPSIMacMXmaikDdJE8ncDMEXErQDnaL+us7C+s1nJ6p aRv0Yc9wAFhUHueqcU6VzsMHGVa2V6pYuKRaUJ4yoy82TZzSjzuXrglb9vUDvkwsMoRS HOyA== 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=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a61-v6si9449220plc.80.2018.09.07.14.05.57; Fri, 07 Sep 2018 14:06:13 -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=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727245AbeIHBrf (ORCPT + 99 others); Fri, 7 Sep 2018 21:47:35 -0400 Received: from mga04.intel.com ([192.55.52.120]:53670 "EHLO mga04.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725999AbeIHBrf (ORCPT ); Fri, 7 Sep 2018 21:47:35 -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 fmsmga104.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 07 Sep 2018 14:04:51 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.53,343,1531810800"; d="scan'208";a="68392367" Received: from sjchrist-coffee.jf.intel.com ([10.54.74.20]) by fmsmga007.fm.intel.com with ESMTP; 07 Sep 2018 14:04:40 -0700 Message-ID: <1536354280.11460.28.camel@intel.com> Subject: Re: [RFC][PATCH 5/8] x86/mm: fix exception table comments From: Sean Christopherson To: Dave Hansen , linux-kernel@vger.kernel.org Cc: peterz@infradead.org, tglx@linutronix.de, x86@kernel.org, luto@kernel.org Date: Fri, 07 Sep 2018 14:04:40 -0700 In-Reply-To: <20180907194900.DF3B41C0@viggo.jf.intel.com> References: <20180907194852.3C351B82@viggo.jf.intel.com> <20180907194900.DF3B41C0@viggo.jf.intel.com> Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.18.5.2-0ubuntu3.2 Mime-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, 2018-09-07 at 12:49 -0700, Dave Hansen wrote: > From: Dave Hansen > > The comments here are wrong.  They are too absolute about where > faults can occur when running in the kernel.  The comments are > also a bit hard to match up with the code. > > Trim down the comments, and make them more precise. > > Also add a comment explaining why we are doing the > bad_area_nosemaphore() path here. > > Signed-off-by: Dave Hansen > Cc: Sean Christopherson > Cc: "Peter Zijlstra (Intel)" > Cc: Thomas Gleixner > Cc: x86@kernel.org > Cc: Andy Lutomirski > --- > >  b/arch/x86/mm/fault.c |   27 ++++++++++++++------------- >  1 file changed, 14 insertions(+), 13 deletions(-) > > diff -puN arch/x86/mm/fault.c~pkeys-fault-warnings-03 arch/x86/mm/fault.c > --- a/arch/x86/mm/fault.c~pkeys-fault-warnings-03 2018-09-07 11:21:47.696751898 -0700 > +++ b/arch/x86/mm/fault.c 2018-09-07 11:21:47.700751898 -0700 > @@ -1349,24 +1349,25 @@ void do_user_addr_space_fault(struct pt_ >   flags |= FAULT_FLAG_INSTRUCTION; >   >   /* > -  * When running in the kernel we expect faults to occur only to > -  * addresses in user space.  All other faults represent errors in > -  * the kernel and should generate an OOPS.  Unfortunately, in the > -  * case of an erroneous fault occurring in a code path which already > -  * holds mmap_sem we will deadlock attempting to validate the fault > -  * against the address space.  Luckily the kernel only validly > -  * references user space from well defined areas of code, which are > -  * listed in the exceptions table. > +  * Kernel-mode access to the user address space should only occur > +  * inside well-defined areas of code listed in the exception > +  * tables.  But, an erroneous kernel fault occurring outside one of > +  * those areas which also holds mmap_sem might deadlock attempting > +  * to validate the fault against the address space. >    * > -  * As the vast majority of faults will be valid we will only perform > -  * the source reference check when there is a possibility of a > -  * deadlock. Attempt to lock the address space, if we cannot we then > -  * validate the source. If this is invalid we can skip the address > -  * space check, thus avoiding the deadlock: > +  * Only do the expensive exception table search when we might be at > +  * risk of a deadlock: > +  * 1. We failed to acquire mmap_sem, and > +  * 2. The access was an explicit kernel-mode access > +  *    (X86_PF_USER=0). Might be worth reminding the reader that X86_PF_USER will be set in sw_error_code for implicit accesses.  I saw "explicit" and my mind immediately jumped to hw_error_code for whatever reason.  E.g.: * 2. The access was an explicit kernel-mode access (we set X86_PF_USER *    in sw_error_code for implicit kernel-mode accesses). >    */ >   if (unlikely(!down_read_trylock(&mm->mmap_sem))) { >   if (!(sw_error_code & X86_PF_USER) && >       !search_exception_tables(regs->ip)) { > + /* > +  * Fault from code in kernel from > +  * which we do not expect faults. > +  */ >   bad_area_nosemaphore(regs, sw_error_code, address, NULL); >   return; >   } > _