Received: by 2002:a25:31c3:0:0:0:0:0 with SMTP id x186csp919173ybx; Wed, 30 Oct 2019 07:13:42 -0700 (PDT) X-Google-Smtp-Source: APXvYqyx5Qeblzc2eNHJ2bhN99H/GuhXVNBwC3pm1peIDLArYIaGffpwC4d9/B3pJlqXpNj0UTYg X-Received: by 2002:a50:cc43:: with SMTP id n3mr32661907edi.287.1572444822007; Wed, 30 Oct 2019 07:13:42 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1572444821; cv=none; d=google.com; s=arc-20160816; b=AuyoLsX4k/VLoBbk0H4Wv6Kiv339Wj9zHMneXBRF0olls4KwxZyFjYcoItEbc1aGc4 sHsDsej7ziP03YEYkJUugZiuetjNOE2uoPj6ycgSNiUhPb9+J3YHvf1EIawnWFJHmBiM CtDfzj68fng8zPTLSUPQu6m4kdSVH+cIYgrJ9PPyHFew5R9DdDAoyT35NHqqTDixkG4l I2/0iZuIGMnufQLUMZwErJqjt2CmY0khRCVAYnIozt27JvEEYt5Vqk4UrK0pGl+4JZZl XuGeUDWZI2qeJEoIiXDVark2WdokycenzLrbezs7jCwcyE/N+bulr6e97ZrLETdAQpg9 xZHw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=7wNlHbpGfe5SuMrfffApDUey9WSzzoUupSY6SDw2fZk=; b=TPaJewcbctbc+GEVB2fnqEvazihgdAfnUokWuM1GF6hlHIj9UHwUAXX3V5ZbjBghtU QwD1mkN4EhBzeS0XkrOOUwlEk3vCia/EmE4UUpOQhvjZ4Eub/WszDLmpCssWJjQzK/zz w7wvHc9X1KeMGFPV5X7WUziUP6NDMoNMlBIRFWGJTDO48dUoutg4nVm5jzSRVILiRfNG LW/KZgSiiOvniYYr8P5ILgBNNfx343g/MoyZ5LQQVfovguF2LkqUdjAuozAvrNF1KzJy uhe6QJBlZ+G0MPG2z2njfMdghB1RyBH5xspkM88BhPo7C/HuJZr9a300Ruhm4N+KKnpW sNPg== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=virtuozzo.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v26si1328165ejg.55.2019.10.30.07.13.17; Wed, 30 Oct 2019 07:13:41 -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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=virtuozzo.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726359AbfJ3OMh (ORCPT + 99 others); Wed, 30 Oct 2019 10:12:37 -0400 Received: from relay.sw.ru ([185.231.240.75]:39106 "EHLO relay.sw.ru" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726284AbfJ3OMh (ORCPT ); Wed, 30 Oct 2019 10:12:37 -0400 Received: from [172.16.25.5] by relay.sw.ru with esmtp (Exim 4.92.2) (envelope-from ) id 1iPohu-0005hC-HE; Wed, 30 Oct 2019 17:12:18 +0300 Subject: Re: [PATCH v10 4/5] x86/kasan: support KASAN_VMALLOC To: Daniel Axtens , kasan-dev@googlegroups.com, linux-mm@kvack.org, x86@kernel.org, glider@google.com, luto@kernel.org, linux-kernel@vger.kernel.org, mark.rutland@arm.com, dvyukov@google.com, christophe.leroy@c-s.fr Cc: linuxppc-dev@lists.ozlabs.org, gor@linux.ibm.com, Andrew Morton References: <20191029042059.28541-1-dja@axtens.net> <20191029042059.28541-5-dja@axtens.net> <87sgnamjg2.fsf@dja-thinkpad.axtens.net> From: Andrey Ryabinin Message-ID: Date: Wed, 30 Oct 2019 17:12:00 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.9.0 MIME-Version: 1.0 In-Reply-To: <87sgnamjg2.fsf@dja-thinkpad.axtens.net> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 10/30/19 4:50 PM, Daniel Axtens wrote: > Andrey Ryabinin writes: > >> On 10/29/19 7:20 AM, Daniel Axtens wrote: >>> In the case where KASAN directly allocates memory to back vmalloc >>> space, don't map the early shadow page over it. >>> >>> We prepopulate pgds/p4ds for the range that would otherwise be empty. >>> This is required to get it synced to hardware on boot, allowing the >>> lower levels of the page tables to be filled dynamically. >>> >>> Acked-by: Dmitry Vyukov >>> Signed-off-by: Daniel Axtens >>> >>> --- >> >>> +static void __init kasan_shallow_populate_pgds(void *start, void *end) >>> +{ >>> + unsigned long addr, next; >>> + pgd_t *pgd; >>> + void *p; >>> + int nid = early_pfn_to_nid((unsigned long)start); >> >> This doesn't make sense. start is not even a pfn. With linear mapping >> we try to identify nid to have the shadow on the same node as memory. But >> in this case we don't have memory or the corresponding shadow (yet), >> we only install pgd/p4d. >> I guess we could just use NUMA_NO_NODE. > > Ah wow, that's quite the clanger on my part. > > There are a couple of other invocations of early_pfn_to_nid in that file > that use an address directly, but at least they reference actual memory. > I'll send a separate patch to fix those up. I see only one incorrect, in kasan_init(): early_pfn_to_nid(__pa(_stext)) It should be wrapped with PFN_DOWN(). Other usages in map_range() seems to be correct, range->start,end is pfns. > >> The rest looks ok, so with that fixed: >> >> Reviewed-by: Andrey Ryabinin > > Thanks heaps! I've fixed up the nit you identifed in the first patch, > and I agree that the last patch probably isn't needed. I'll respin the > series shortly. > Hold on a sec, just spotted another thing to fix. > @@ -352,9 +397,24 @@ void __init kasan_init(void) > shadow_cpu_entry_end = (void *)round_up( > (unsigned long)shadow_cpu_entry_end, PAGE_SIZE); > > + /* > + * If we're in full vmalloc mode, don't back vmalloc space with early > + * shadow pages. Instead, prepopulate pgds/p4ds so they are synced to > + * the global table and we can populate the lower levels on demand. > + */ > +#ifdef CONFIG_KASAN_VMALLOC > + kasan_shallow_populate_pgds( > + kasan_mem_to_shadow((void *)PAGE_OFFSET + MAXMEM), This should be VMALLOC_START, there is no point to allocate pgds for the hole between linear mapping and vmalloc, just waste of memory. It make sense to map early shadow for that hole, because if code dereferences address in that hole we will see the page fault on that address instead of fault on the shadow. So something like this might work: kasan_populate_early_shadow( kasan_mem_to_shadow((void *)PAGE_OFFSET + MAXMEM), kasan_mem_to_shadow((void *)VMALLOC_START)); if (IS_ENABLED(CONFIG_KASAN_VMALLOC) kasan_shallow_populate_pgds(kasan_mem_to_shadow(VMALLOC_START), kasan_mem_to_shadow((void *)VMALLOC_END)) else kasan_populate_early_shadow(kasan_mem_to_shadow(VMALLOC_START), kasan_mem_to_shadow((void *)VMALLOC_END)); kasan_populate_early_shadow( kasan_mem_to_shadow((void *)VMALLOC_END + 1), shadow_cpu_entry_begin);