From patchwork Mon Feb 1 19:43:27 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 12059867 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 83B63C433E0 for ; Mon, 1 Feb 2021 19:45:49 +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 E665664EA9 for ; Mon, 1 Feb 2021 19:45:48 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org E665664EA9 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=eZLwp5500oR/A7fEcxgZBUh6PKqo1GZEf5PrlSB3sqU=; b=z7grGXuy/IaDL2poA58yZ01zY DIbLiWXY/hEtMMWCVDcMKBXv12p59fMen1EX5CpSiyT4amHUPA9H7AMRvTBMe7g5cPMDwPaRMM3e1 yU93Db1sQ9+yKOYxFqPcsipAlTs1zPws/4OfNYutR+L0Ss9BviTgZjOQhV1KSXfzwggb0dbhluhTm 0/+wGzB9KIrpXIFwagLUfzSg5eyFI/G7D7ZXmskziZXnxYDin3bd1FhFZ2Pnj1JcQaNAv2BhgPtie DuAyC0WSAh7HR8/h06rSA53tf0WrAKZkXQkpl5PamU3BDjgfgGi731OXKP//WS0e3LDiiMR/E08VO c9zALJvpw==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1l6f7S-00026g-06; Mon, 01 Feb 2021 19:44:18 +0000 Received: from mail-qv1-xf49.google.com ([2607:f8b0:4864:20::f49]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1l6f71-0001rZ-Fs for linux-arm-kernel@lists.infradead.org; Mon, 01 Feb 2021 19:43:53 +0000 Received: by mail-qv1-xf49.google.com with SMTP id v13so8738526qvm.10 for ; Mon, 01 Feb 2021 11:43:48 -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=5os4NdBbDONsbLVxABgdrPD9x64VnOptbOzgU3pEDKs=; b=icvI25ft44tZNJLareqZm2eV6LU3rjKFoSzXua9cf+7734+1fWcCmnYf7hEl5Qbz+D X1HGWJkAzxbfnIv7aZkYjPhjP0onDk8oLebvt5EFUyH9inK3uQ5w7A/juLiNsTrOpW6D 4QB/f/PJ605/JfJndxR6OL1twXraBxmad0by++3RFa1uvStOQIe+cLBvnXzyUaTfu+2v IDNsEAuTbXwUW2bZwOGOX6UO1kpsjI9ybzJEZjTQQpoYleLdUk5xqzFbAzvVhdKy/Qr8 WEoqEGKqEd0hnV7F9350O0q+eRFF2yKHGuEA4AGGamvMh48mEenN5FlPFbNm7at+3K3H B7wg== 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=5os4NdBbDONsbLVxABgdrPD9x64VnOptbOzgU3pEDKs=; b=CtQB9Zbsq0d+8DiAB1y0T/voysbylwGrhaM8vqcQz88qDomRdi+u36+gfHvK/h2Fvr 0zGHUOo+L17+z3cUaYyjc/o6shQ7EXeKqwlunwgway8WqTfyLAn4gAQINL51uvQBV/Rw wOnnoqc+EsGT9bWZmAdkQjDojtTLbTf81BYVu1hAUPWk7vtrwxQ/1Vw392dj4gRMI0GJ euKLrbmHWyPyGxKOr17VlLJ9Cfo0C/yfqzZV4iKMwNtUq20nT0ewufJbTTfhwZ8qTPKk N0Sc2dUuSBsatkJLWSBVj6YHO1uvTfvxDIIU2IkrEcACZXP/XjUcTxTNoRLxS5UYI8qn V4tw== X-Gm-Message-State: AOAM533o/hn8Q6ez+7LoahhqcwQdbXTr/DOVtdDjBj4cKtmmAp7kgcMo EHL5eY3HuHTBaSjZjn9e/svju0nsFO8tNLFI X-Google-Smtp-Source: ABdhPJzR+YEpFc2Ch11Rl6BTXMIoXfaJyZzPai737uCYCgWg2fJznC+a5+pVgD+X3paVHk7gPsjQcrXSotCeIBAj X-Received: from andreyknvl3.muc.corp.google.com ([2a00:79e0:15:13:7220:84ff:fe09:7e9d]) (user=andreyknvl job=sendgmr) by 2002:a0c:c488:: with SMTP id u8mr16899450qvi.9.1612208627196; Mon, 01 Feb 2021 11:43:47 -0800 (PST) Date: Mon, 1 Feb 2021 20:43:27 +0100 In-Reply-To: Message-Id: <8fdbf86842f4eaf2458ecd23d0844058dbc2c7a2.1612208222.git.andreyknvl@google.com> Mime-Version: 1.0 References: X-Mailer: git-send-email 2.30.0.365.g02bc693789-goog Subject: [PATCH 03/12] kasan: optimize large kmalloc poisoning From: Andrey Konovalov To: 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-20210201_144351_589583_966E65D5 X-CRM114-Status: GOOD ( 15.11 ) 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 , Andrew Morton , 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. Signed-off-by: Andrey Konovalov Reviewed-by: Marco Elver --- 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 128cb330ca73..a7eb553c8e91 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);