From patchwork Fri Feb 5 17:34:37 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 12070583 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 70C01C433DB for ; Fri, 5 Feb 2021 17:37:12 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 1EBBE60C3F for ; Fri, 5 Feb 2021 17:37:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 1EBBE60C3F Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:To:From:Subject:References:Mime-Version:Message-Id: In-Reply-To:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=Mehy53FT3eP+IUV0z8y/U7HPGF90Wf0dZrLAHwZ7Z2Y=; b=G0TxPlIQaC5GTN8P22NnYadev H2RfCRpfMcYpgnNX7vsKblLYk8cF4pYgadP4NxE4Bwt2gXr2kdLm22+Jwq+YIsNBIkInzCrs4Ru0E AedlpGubdi/WmZYufDKfMT55BiX6vCT/lv8uRo0nzrHXpmrOt0tLJIiBgq6R8wuRCbJUCL0+sTDWU SHXTlhwzR5lO19Z6YQ3qvi9bhKCBSX4r81VtQcmc+92T4C29+pzDj87PIty/zcVtybdmoVGIXRcAc op+mE/aVcVBzwXXOcy0lmWsZKJNLaH1k+EmPnp8lHMsaeewnvoCGRgKUf564cp7aDIMoS1Ik/QUHA B8oNr5Bcw==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1l850z-00053T-49; Fri, 05 Feb 2021 17:35:29 +0000 Received: from mail-wr1-x44a.google.com ([2a00:1450:4864:20::44a]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1l850X-0004p7-Ao for linux-arm-kernel@lists.infradead.org; Fri, 05 Feb 2021 17:35:09 +0000 Received: by mail-wr1-x44a.google.com with SMTP id p16so5723833wrx.10 for ; Fri, 05 Feb 2021 09:34:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=tljlSxhBzcxqidAVSdVdhM6oUJ+nJ3d1xitEmX2Ppf0=; b=EooVbzbfAmq8G1bWgvLByZcRTJzsGaZsM+gbsnttAHt59SZ03efBEYHdViNV3NOcBb AIV9WF6XYYWGw3mRwID1biNz4Xd/YnW9mVfWF3MqcCeTu8ZEhARSrRcHYLM1RcDt/U8i v3YvXKISbDMp6LV5R+hWQKWdH3XDXXiz6Y8YegizUaUeJw9yV1lF2WpCyTeZiZQMLUSY laKMOHx3s2jUXdJJtzLun9LBT3MD0Y52b8M44RMyqTJV6KG7pYS+FryEe+WFupRVaQW6 nT3pdbQDrilZswBy/TGLAEBB7NbLJO0EwX6HQFv7HTOCg1BW07zGmBMyOMgF1P3Aima1 hypg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=tljlSxhBzcxqidAVSdVdhM6oUJ+nJ3d1xitEmX2Ppf0=; b=JYfpGjWkwETY2yS24QCZIacgF6jibpHvFbEQbB0hxRsi847RLcvmYN6T4Urke5haaa xAqW9Fn2AXh9lchygecnb5ed71LK2GGLB8Qy9zwa14K0D4v09nts9iglW41BL/tzlHXC li2/yiyN5g6WEI8J960w9Jy9nqG7u2OoJTdO8tU8Wl0r6UcJp5oD8e2Ip5W9DFhCEUT/ U8tYijAKFfA5ch778UBtDJ8sHknidlFdCvVqdp6eDUDG8sOPtB/fDUQ2F9BX1yAVvm7z EIAsDOgSk5DXgOBolIiN1obtOiih43hRbGZmLjLypCJ/eqrAJET6/T+hGECZHmhgcDHi MrqA== X-Gm-Message-State: AOAM5313iILbXS3Hzh8AnUiU4Tx1YWOQC90nFnHdSoX0GQDjlnt03tuw sASKeFz3sdOytU+R1gZNNNFAXybxMapsMLyc X-Google-Smtp-Source: ABdhPJwsjA/F6RHWX96VY6maj/IIDJ50rvRshw+Hr4yR+j1mTxQLGy8N+h/cCmJm78A4s33ZYOd8+8tTyP3YonbL X-Received: from andreyknvl3.muc.corp.google.com ([2a00:79e0:15:13:edb8:b79c:2e20:e531]) (user=andreyknvl job=sendgmr) by 2002:a5d:53c3:: with SMTP id a3mr6032044wrw.43.1612546497774; Fri, 05 Feb 2021 09:34:57 -0800 (PST) Date: Fri, 5 Feb 2021 18:34:37 +0100 In-Reply-To: Message-Id: <33dee5aac0e550ad7f8e26f590c9b02c6129b4a3.1612546384.git.andreyknvl@google.com> Mime-Version: 1.0 References: X-Mailer: git-send-email 2.30.0.365.g02bc693789-goog Subject: [PATCH v3 mm 03/13] kasan: optimize large kmalloc poisoning From: Andrey Konovalov To: Andrew Morton , Catalin Marinas , Vincenzo Frascino , Dmitry Vyukov , Alexander Potapenko , Marco Elver X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210205_123501_571659_D486DA6A X-CRM114-Status: GOOD ( 15.03 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Branislav Rankov , Andrey Konovalov , Kevin Brodsky , Will Deacon , linux-kernel@vger.kernel.org, kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, Andrey Ryabinin , Peter Collingbourne , Evgenii Stepanov Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Similarly to kasan_kmalloc(), kasan_kmalloc_large() doesn't need to unpoison the object as it as already unpoisoned by alloc_pages() (or by ksize() for krealloc()). This patch changes kasan_kmalloc_large() to only poison the redzone. Reviewed-by: Marco Elver Signed-off-by: Andrey Konovalov --- mm/kasan/common.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/mm/kasan/common.c b/mm/kasan/common.c index 00edbc3eb32e..f2a6bae13053 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -494,7 +494,6 @@ EXPORT_SYMBOL(__kasan_kmalloc); void * __must_check __kasan_kmalloc_large(const void *ptr, size_t size, gfp_t flags) { - struct page *page; unsigned long redzone_start; unsigned long redzone_end; @@ -504,12 +503,23 @@ void * __must_check __kasan_kmalloc_large(const void *ptr, size_t size, if (unlikely(ptr == NULL)) return NULL; - page = virt_to_page(ptr); + /* + * The object has already been unpoisoned by kasan_alloc_pages() for + * alloc_pages() or by ksize() for krealloc(). + */ + + /* + * The redzone has byte-level precision for the generic mode. + * Partially poison the last object granule to cover the unaligned + * part of the redzone. + */ + if (IS_ENABLED(CONFIG_KASAN_GENERIC)) + kasan_poison_last_granule(ptr, size); + + /* Poison the aligned part of the redzone. */ redzone_start = round_up((unsigned long)(ptr + size), KASAN_GRANULE_SIZE); - redzone_end = (unsigned long)ptr + page_size(page); - - kasan_unpoison(ptr, size); + redzone_end = (unsigned long)ptr + page_size(virt_to_page(ptr)); kasan_poison((void *)redzone_start, redzone_end - redzone_start, KASAN_PAGE_REDZONE);