From patchwork Fri May 4 13:55:34 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Rutland X-Patchwork-Id: 134980 Delivered-To: patch@linaro.org Received: by 10.46.151.6 with SMTP id r6csp82099lji; Fri, 4 May 2018 06:56:08 -0700 (PDT) X-Google-Smtp-Source: AB8JxZppOpr8lLEx92WlNcuXpT+5YCmqZDH9999jpcy34FcuSEMYC4mD7++xcIFp+B4K5cKARwRx X-Received: by 10.98.65.132 with SMTP id g4mr27188351pfd.51.1525442167867; Fri, 04 May 2018 06:56:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525442167; cv=none; d=google.com; s=arc-20160816; b=oOOOx+7f6ELYFChtqG6jJyfS036euVTOyhKMe9e252zHb95jo/wQlEk9Kvm2mYiEgH vt9/5JT3SRQrSuyf13a3i4Yp/jojVrz12kihYcagea0ExpIKAOOIk8Zw9GmEuDEkGG6x uJiocXkjXDuUkES+c1YRYLlzhq0HltDkefHsMxCj741CRkT6AOwVFhBBMXEtA1Fdd7nc NhUKPWXEDUNzin/jMnDjD7MZyZtbcZIyfs5NoYve0o0V9uIu8OsKXJzYEmqWNPfRPYui FKYjGr+nna3e6+Uj7KT3YOlFI8cSJAdgRq6TQWPLQRvd0xcQhAHJtRIQVKc7cMHxxAo0 yqoQ== 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=cTRUh9O/DbaSwfkMjNxEw3Fi6B/IMK0RM1SN8oys07w=; b=aTkxpn43zYdV6Zy2Yxs+DbejkSGMoPuaxhDC/0jsgiUmTcYdOzreMLOJXCmd4Gmy/z 1U3LQfLx5WtcgCwiiJ8UajxD8JXMOmIecgoeQ/8spj1W6mB4py1aBB+ZK8egOEyVBao2 B9H94v4GBIFsWUhrL0yi3etlNl39in9cphslHn3eWNRmIc0SAhMOFzAxxdKJIwYgYANh DpIUg4Gsha7eOUpiBMyoT7PjmXjw4x8W7uyPEdTAXkgvslpqG2O+/HHAKzeB7IIalPzm wRsZLKzaj1JkPofL9O9d62MhCwObWXheH1lSYSO1Atpk7DQrElIOqIZrb5A41X1xy9c9 3txQ== 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 b2-v6si4766492pgc.569.2018.05.04.06.56.07; Fri, 04 May 2018 06:56: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 S1752054AbeEDN4F (ORCPT + 29 others); Fri, 4 May 2018 09:56:05 -0400 Received: from foss.arm.com ([217.140.101.70]:53662 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752026AbeEDNzp (ORCPT ); Fri, 4 May 2018 09:55:45 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.72.51.249]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 6A570164F; Fri, 4 May 2018 06:55:45 -0700 (PDT) Received: from lakrids.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.72.51.249]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 161B23F487; Fri, 4 May 2018 06:55:43 -0700 (PDT) From: Mark Rutland To: linux-kernel@vger.kernel.org Cc: akpm@linux-foundation.org, aryabinin@virtuozzo.com, dvyukov@google.com, mark.rutland@arm.com, mingo@redhat.com, peterz@infradead.org Subject: [PATCH 2/3] kcov: prefault the kcov_area Date: Fri, 4 May 2018 14:55:34 +0100 Message-Id: <20180504135535.53744-3-mark.rutland@arm.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20180504135535.53744-1-mark.rutland@arm.com> References: <20180504135535.53744-1-mark.rutland@arm.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On many architectures the vmalloc area is lazily faulted in upon first access. This is problematic for KCOV, as __sanitizer_cov_trace_pc accesses the (vmalloc'd) kcov_area, and fault handling code may be instrumented. If an access to kcov_area faults, this will result in mutual recursion through the fault handling code and __sanitizer_cov_trace_pc(), eventually leading to stack corruption and/or overflow. We can avoid this by faulting in the kcov_area before __sanitizer_cov_trace_pc() is permitted to access it. Once it has been faulted in, it will remain present in the process page tables, and will not fault again. Signed-off-by: Mark Rutland Cc: Andrew Morton Cc: Andrey Ryabinin Cc: Dmitry Vyukov --- kernel/kcov.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) -- 2.11.0 diff --git a/kernel/kcov.c b/kernel/kcov.c index 5be9a60a959f..3b82f8e258da 100644 --- a/kernel/kcov.c +++ b/kernel/kcov.c @@ -324,6 +324,17 @@ static int kcov_close(struct inode *inode, struct file *filep) return 0; } +static void kcov_fault_in_area(struct kcov *kcov) +{ + unsigned long stride = PAGE_SIZE / sizeof(unsigned long); + unsigned long *area = kcov->area; + unsigned long offset; + + for (offset = 0; offset < kcov->size; offset += stride) { + READ_ONCE(area[offset]); + } +} + static int kcov_ioctl_locked(struct kcov *kcov, unsigned int cmd, unsigned long arg) { @@ -372,6 +383,7 @@ static int kcov_ioctl_locked(struct kcov *kcov, unsigned int cmd, #endif else return -EINVAL; + kcov_fault_in_area(kcov); /* Cache in task struct for performance. */ t->kcov_size = kcov->size; t->kcov_area = kcov->area;