Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp1178417ybl; Fri, 16 Aug 2019 10:09:16 -0700 (PDT) X-Google-Smtp-Source: APXvYqzWD0TYOZfOBGXWuu4u3A4BoBwqSKEn8Ae+5Gf4sZaZBQefLyj5cxWJMmb3O/V/jkarD1d5 X-Received: by 2002:a17:90a:8991:: with SMTP id v17mr8050046pjn.120.1565975356058; Fri, 16 Aug 2019 10:09:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1565975356; cv=none; d=google.com; s=arc-20160816; b=lN5b7TG+pRzP97I3gR/TzWJ4uosg1eRoVtI1Zz8pMg4kgoXURXJxqFWToIu/jwYi+y j+ZmudU1NKYOnGbuQopCWe/I2CEqjzlr3xHuEdMTHHxx3TxdedINvN9nlPQE2jcuZ+WU iq0TkCwTG3pHuRpO+0Ih0UIccFZS1p112AvQ3LpvaDoj6WyAyiSXBSBogaZLxVVAx5Ga F2ECyW3m7A0iQVaaa3kZZeaLWWOMyI19TmXbqkksZnEI/+8sUyrFBoz7TBxZ5/P6zujL 4rT1v7ioaMpiv2nSfU0cXqMgc9DbLx+PZTHqaFY1Fl8bQX8Miv3uxPHLvtifT+QJyZ4S dyXQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-transfer-encoding:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=iQP6cTOXD2gZPjecCX7PieaZTobOpg7FNKc3tId4y28=; b=qMi/m8+Y32DUlPRLwsGoJWFjf6mDvZ6q0AVei13nDJRpfCpCXVbx1sZ5PQaLdjtIT3 5ZR6bQc5pQLb/uim+3xNbynpH8H4RtZnDUxHGNAAOyXFnSnpEaZx5QbzxrNn3a4Q2DsE HXs9FHwhZa7wy0F1ZoWAYr9UN4qqN93qMHMVy/YRI0RrejuovHNrDJZwL5JwVl8DWVb6 KvUGbCzq56jdK9HFuzVMz6spbTmHS2uTLZ3pKU72jLkjVg8mkm9NJNNjfjiGegWbUyKn dwfPu6KhGBBwLNDp9NA9fMXfuPWEN7F9RViO4z6LQ7ny/nS5qsw82USH90bV+J9+wMax qptw== 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 b6si4200484pge.44.2019.08.16.10.08.59; Fri, 16 Aug 2019 10:09:16 -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 S1727164AbfHPRIV (ORCPT + 99 others); Fri, 16 Aug 2019 13:08:21 -0400 Received: from foss.arm.com ([217.140.110.172]:59200 "EHLO foss.arm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726469AbfHPRIV (ORCPT ); Fri, 16 Aug 2019 13:08:21 -0400 Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 074A928; Fri, 16 Aug 2019 10:08:20 -0700 (PDT) Received: from lakrids.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.121.207.14]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 700803F694; Fri, 16 Aug 2019 10:08:18 -0700 (PDT) Date: Fri, 16 Aug 2019 18:08:13 +0100 From: Mark Rutland To: Christophe Leroy Cc: Daniel Axtens , kasan-dev@googlegroups.com, linux-mm@kvack.org, x86@kernel.org, aryabinin@virtuozzo.com, glider@google.com, luto@kernel.org, linux-kernel@vger.kernel.org, dvyukov@google.com, linuxppc-dev@lists.ozlabs.org, gor@linux.ibm.com Subject: Re: [PATCH v4 1/3] kasan: support backing vmalloc space with real shadow memory Message-ID: <20190816170813.GA7417@lakrids.cambridge.arm.com> References: <20190815001636.12235-1-dja@axtens.net> <20190815001636.12235-2-dja@axtens.net> <15c6110a-9e6e-495c-122e-acbde6e698d9@c-s.fr> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <15c6110a-9e6e-495c-122e-acbde6e698d9@c-s.fr> User-Agent: Mutt/1.11.1+11 (2f07cb52) (2018-12-01) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Christophe, On Fri, Aug 16, 2019 at 09:47:00AM +0200, Christophe Leroy wrote: > Le 15/08/2019 à 02:16, Daniel Axtens a écrit : > > Hook into vmalloc and vmap, and dynamically allocate real shadow > > memory to back the mappings. > > > > Most mappings in vmalloc space are small, requiring less than a full > > page of shadow space. Allocating a full shadow page per mapping would > > therefore be wasteful. Furthermore, to ensure that different mappings > > use different shadow pages, mappings would have to be aligned to > > KASAN_SHADOW_SCALE_SIZE * PAGE_SIZE. > > > > Instead, share backing space across multiple mappings. Allocate > > a backing page the first time a mapping in vmalloc space uses a > > particular page of the shadow region. Keep this page around > > regardless of whether the mapping is later freed - in the mean time > > the page could have become shared by another vmalloc mapping. > > > > This can in theory lead to unbounded memory growth, but the vmalloc > > allocator is pretty good at reusing addresses, so the practical memory > > usage grows at first but then stays fairly stable. > > I guess people having gigabytes of memory don't mind, but I'm concerned > about tiny targets with very little amount of memory. I have boards with as > little as 32Mbytes of RAM. The shadow region for the linear space already > takes one eighth of the RAM. I'd rather avoid keeping unused shadow pages > busy. I think this depends on how much shadow would be in constant use vs what would get left unused. If the amount in constant use is sufficiently large (or the residue is sufficiently small), then it may not be worthwhile to support KASAN_VMALLOC on such small systems. > Each page of shadow memory represent 8 pages of real memory. Could we use > page_ref to count how many pieces of a shadow page are used so that we can > free it when the ref count decreases to 0. > > > This requires architecture support to actually use: arches must stop > > mapping the read-only zero page over portion of the shadow region that > > covers the vmalloc space and instead leave it unmapped. > > Why 'must' ? Couldn't we switch back and forth from the zero page to real > page on demand ? > > If the zero page is not mapped for unused vmalloc space, bad memory accesses > will Oops on the shadow memory access instead of Oopsing on the real bad > access, making it more difficult to locate and identify the issue. I agree this isn't nice, though FWIW this can already happen today for bad addresses that fall outside of the usual kernel address space. We could make the !KASAN_INLINE checks resilient to this by using probe_kernel_read() to check the shadow, and treating unmapped shadow as poison. It's also worth noting that flipping back and forth isn't generally safe unless going via an invalid table entry, so there'd still be windows where a bad access might not have shadow mapped. We'd need to reuse the common p4d/pud/pmd/pte tables for unallocated regions, or the tables alone would consume significant amounts of memory (e..g ~32GiB for arm64 defconfig), and thus we'd need to be able to switch all levels between pgd and pte, which is much more complicated. I strongly suspect that the additional complexity will outweigh the benefit. [...] > > +#ifdef CONFIG_KASAN_VMALLOC > > +static int kasan_populate_vmalloc_pte(pte_t *ptep, unsigned long addr, > > + void *unused) > > +{ > > + unsigned long page; > > + pte_t pte; > > + > > + if (likely(!pte_none(*ptep))) > > + return 0; > > Prior to this, the zero shadow area should be mapped, and the test should > be: > > if (likely(pte_pfn(*ptep) != PHYS_PFN(__pa(kasan_early_shadow_page)))) > return 0; As above, this would need a more comprehensive redesign, so I don't think it's worth going into that level of nit here. :) If we do try to use common shadow for unallocate VA ranges, it probably makes sense to have a common poison page that we can use, so that we can report vmalloc-out-of-bounfds. Thanks, Mark.