From patchwork Sun Mar 18 12:53:36 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Abbott Liu X-Patchwork-Id: 10291151 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id EE26E60385 for ; Sun, 18 Mar 2018 13:30:55 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DCD7E28A05 for ; Sun, 18 Mar 2018 13:30:55 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D07E928FDB; Sun, 18 Mar 2018 13:30:55 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-1.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 5BF7A28A05 for ; Sun, 18 Mar 2018 13:30:55 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=tsIBtPkypbnPzO0HAEmvuxFoBUA1rtxJIKtlZeD98OY=; b=Hap73z7x8R8GA5 2hMT+8kNC0tyPP52HUxwd3fdPcI80M5ufvUifi47CTPhQho9ErjtqE5ZRv3wzjdbm9RVP8W+CnEJT peGLuCoOWLpnogqrcSAqMGDabT/I5cvdmMldbPBFhX3eK22QcHo3MtKymZ4Rlr0GERRdCOeIuiy1o y+PKqpmaEyj0ciq7LzyndB18vwnguBp9nnsy8cEARpwpRkIAjuuZ5VV51iohtBdWt/J0QLNfik8Ae ZrH/0zqf3yqlWWd2foZfNAJrM85Q/vDbdbzHzufF2eXEz6M1Mb7/JHuS3JpArjzWNCi0yAP0OMXYJ qcXuNb1DxOErpot7LHjA==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.90_1 #2 (Red Hat Linux)) id 1exYOh-0006BY-7l; Sun, 18 Mar 2018 13:30:51 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by bombadil.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1exYNm-0004mi-IW for linux-arm-kernel@bombadil.infradead.org; Sun, 18 Mar 2018 13:29:54 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Type:MIME-Version:References: In-Reply-To:Message-ID:Date:Subject:CC:To:From:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=lTxpIFFh58vmJxEWR02loWAsikfJ+s9CWRPwhE1dby8=; b=tXhJpF98TPKVzP4oKZjow89vC RrGPu92mXoGso85JegMm5ESewwjoIooU0phj+UCIeloXFR5TMzUx0ojq30jLegPFSxnk6QnJWy0EM K13C9YhFtWrZFsOKzSs3eXEk32VAlZ3XbIjnPcA70PAw1vbGYURIkaY6p0ap2tAWArHC9K7g10i5R eOVv1IAHmHK1x2y4c2YUFu5MDaYPu504KjterhXy3oFFhIlF4d2ZdxFADZXWD/Yq/Y5IQOmZhCEXD NKoZ05RVAzeFnQCAGFSCqZF3yALVtx+jml9BGqxbYA6IOXeUlb6qQwLzVW/mkztDsm6IW8XCP9ioY tpw3riC8g==; Received: from [45.249.212.35] (helo=huawei.com) by casper.infradead.org with esmtps (Exim 4.90_1 #2 (Red Hat Linux)) id 1exY8e-0005BQ-Nq for linux-arm-kernel@lists.infradead.org; Sun, 18 Mar 2018 13:14:21 +0000 Received: from DGGEMS405-HUB.china.huawei.com (unknown [172.30.72.59]) by Forcepoint Email with ESMTP id 213C1364F1FB9; Sun, 18 Mar 2018 21:13:45 +0800 (CST) Received: from linux.site (10.67.187.223) by DGGEMS405-HUB.china.huawei.com (10.3.19.205) with Microsoft SMTP Server id 14.3.361.1; Sun, 18 Mar 2018 21:13:40 +0800 From: Abbott Liu To: , , , , , , , , , Subject: [PATCH 1/7] 2 1-byte checks more safer for memory_is_poisoned_16 Date: Sun, 18 Mar 2018 20:53:36 +0800 Message-ID: <20180318125342.4278-2-liuwenliang@huawei.com> X-Mailer: git-send-email 2.9.0 In-Reply-To: <20180318125342.4278-1-liuwenliang@huawei.com> References: <20180318125342.4278-1-liuwenliang@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.67.187.223] X-CFilter-Loop: Reflected X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20180318_131418_569787_BB99FA34 X-CRM114-Status: GOOD ( 12.82 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: nicolas.pitre@linaro.org, tixy@linaro.org, grygorii.strashko@linaro.org, catalin.marinas@arm.com, linux@rasmusvillemoes.dk, dhowells@redhat.com, linux-mm@kvack.org, glider@google.com, mark.rutland@arm.com, kvmarm@lists.cs.columbia.edu, opendmb@gmail.com, mawilcox@microsoft.com, kasan-dev@googlegroups.com, geert@linux-m68k.org, linux-arm-kernel@lists.infradead.org, zhichao.huang@linaro.org, labbott@redhat.com, vladimir.murzin@arm.com, keescook@chromium.org, arnd@arndb.de, philip@cog.systems, jinb.park7@gmail.com, tglx@linutronix.de, dvyukov@google.com, ard.biesheuvel@linaro.org, linux-kernel@vger.kernel.org, james.morse@arm.com, kirill.shutemov@linux.intel.com, pombredanne@nexb.com, thgarnie@google.com, christoffer.dall@linaro.org Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP Because in some architecture(eg. arm) instruction set, non-aligned access support is not very well, so 2 1-byte checks is more safer than 1 2-byte check. The impact on performance is small because 16-byte accesses are not too common. Cc: Andrey Ryabinin Reviewed-by: Andrew Morton Reviewed-by: Russell King - ARM Linux Reviewed-by: Ard Biesheuvel Acked-by: Dmitry Vyukov Signed-off-by: Abbott Liu --- mm/kasan/kasan.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/mm/kasan/kasan.c b/mm/kasan/kasan.c index e13d911..104839a 100644 --- a/mm/kasan/kasan.c +++ b/mm/kasan/kasan.c @@ -151,13 +151,20 @@ static __always_inline bool memory_is_poisoned_2_4_8(unsigned long addr, static __always_inline bool memory_is_poisoned_16(unsigned long addr) { - u16 *shadow_addr = (u16 *)kasan_mem_to_shadow((void *)addr); - - /* Unaligned 16-bytes access maps into 3 shadow bytes. */ - if (unlikely(!IS_ALIGNED(addr, KASAN_SHADOW_SCALE_SIZE))) - return *shadow_addr || memory_is_poisoned_1(addr + 15); + u8 *shadow_addr = (u8 *)kasan_mem_to_shadow((void *)addr); - return *shadow_addr; + if (unlikely(shadow_addr[0] || shadow_addr[1])) { + return true; + } else if (likely(IS_ALIGNED(addr, KASAN_SHADOW_SCALE_SIZE))) { + /* + * If two shadow bytes covers 16-byte access, we don't + * need to do anything more. Otherwise, test the last + * shadow byte. + */ + return false; + } else { + return memory_is_poisoned_1(addr + 15); + } } static __always_inline unsigned long bytes_is_nonzero(const u8 *start,