From patchwork Fri Jul 12 17:00:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brendan Jackman X-Patchwork-Id: 13732030 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 625B417D371 for ; Fri, 12 Jul 2024 17:01:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720803713; cv=none; b=FYERidPYlQ3k7/d7TSuVPSthAapxfn1pfe75SZMiiypyKUrOCk2oeI5bArOSEZF9ChD4zNvxc7qDLfeAFHgovFmJqkY7ie92y4mCbVtZethC3t6p4bMATIBpmoZJKA4tKDqUJxur82NqgZKvGdQs3QOwxJp4NkRFDgLsyeaRQAg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1720803713; c=relaxed/simple; bh=1ErSrdkfI8yA/0cfu0Z+seRub2kxW1au/mENrkDG/Go=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=YZyhHEmI1v/OB45opyZQ0E1K96lCgFdhvPzLtaospHyR4DVGZgpzYRAHgyxk3kMWtC7MElxhmzoMylBq5+myt1Cur4NIQ8XSDiTlh+tHqpqsSWDfQbNY4RM+cl9NnZHQgZXXi7YKNqlhYbDKGysDVbQlBCcZDJ1JDy3fwr/FG+w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--jackmanb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=aQCPejgQ; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--jackmanb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="aQCPejgQ" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-65be82a3241so37988247b3.0 for ; Fri, 12 Jul 2024 10:01:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1720803710; x=1721408510; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=vODRaOCa3ahOVXhr5qs8R058obZ/hmyGBbEWzV1lmI8=; b=aQCPejgQm8djF229zLWYJbsA1g/PA60MtWOi/glveAdwjSrNlUnibbEAZu46hQ4Kcz KH7tJfF/gK77GISM6li5mmVOyBZs8CP/Si1K/3zDKUnkH8957Wlv4goMmQVg3uwezgfX N8nnzzhQIv1zMi3bMv6QXypfQi1IlZtasoC8iOHKTNoTC27flIgIdW0PqAVnvZywIF1X +TNcWk73B8T+0ioCnyE+Zdiu7xMKgiWQtPaNcJDnQTY5DdfyF2lE/wc2JOK9XpLTsK3o aKdepfAeuZQp06bA8byOapSjz3giUXGi5Ol4A4CBtWGEiePwASkhfg/wjKEVlHhVbUcL og3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720803710; x=1721408510; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=vODRaOCa3ahOVXhr5qs8R058obZ/hmyGBbEWzV1lmI8=; b=P2cBj57H0lfGDnE72UZIX1fAKgUHGPCu3iCCOjifUuduiv+VfXo0yzzCDLGjyt0KaO YQx/Ml7uGYB7Bvt1/FgSyZOpt+sYCoHq7VD2KMpD99tYiYeUMvYnvCpOtv1fNEUBw459 Fc2kt3zEqUAgBkcJQtczNMyq6gOq2gljfXzTM0geJoOsHPViwdt1R0mAuyHuiPgMBv2r +phCtfhF+LMvU77U6c5tr2839nynXQIMVXJsJJ6BQ2VNIu7brRi7z6W3ISIW69ZC7koG 62fbfAfMtPAhpbdG2yYZkaFUprPd164Xii13AL8xdKVwgLWC73EYzl6SChgF5qQ5oG7s /ihA== X-Forwarded-Encrypted: i=1; AJvYcCVhXHB6tLP8pGkig9ImjZjMxeB95A4jPpidg0KNrnmEYERXHZe1Ca1GKlaQ/rx4OvwxuD/lM8Dlf5zBBHaL+kfYE68J X-Gm-Message-State: AOJu0YwUKsn64wdpRxAZC7jFa88Io+c+9Drg9Ys2u37wi8ihmq71HoNW LpzwRIUzuG/wFibjIc3dDtEz4fpZ+vRJ4hO6bVG/veiMApDsDk4xQYs54aKrJShC2NuhtNcM7t/ zWSIyIIcbmw== X-Google-Smtp-Source: AGHT+IFZSEZj3VcvmAV2TZcIrShIr4wt15JBT1NWiP/B9JdX5vcjtoCXk1eU0WBA34EhNCZasboijrfP9ezs0Q== X-Received: from beeg.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:11db]) (user=jackmanb job=sendgmr) by 2002:a05:6902:70b:b0:e03:5144:1d48 with SMTP id 3f1490d57ef6-e041b142c52mr23629276.11.1720803710367; Fri, 12 Jul 2024 10:01:50 -0700 (PDT) Date: Fri, 12 Jul 2024 17:00:38 +0000 In-Reply-To: <20240712-asi-rfc-24-v1-0-144b319a40d8@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240712-asi-rfc-24-v1-0-144b319a40d8@google.com> X-Mailer: b4 0.14-dev Message-ID: <20240712-asi-rfc-24-v1-20-144b319a40d8@google.com> Subject: [PATCH 20/26] mm: asi: Map dynamic percpu memory as nonsensitive From: Brendan Jackman To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , Andy Lutomirski , Peter Zijlstra , Sean Christopherson , Paolo Bonzini , Alexandre Chartre , Liran Alon , Jan Setje-Eilers , Catalin Marinas , Will Deacon , Mark Rutland , Andrew Morton , Mel Gorman , Lorenzo Stoakes , David Hildenbrand , Vlastimil Babka , Michal Hocko , Khalid Aziz , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Valentin Schneider , Paul Turner , Reiji Watanabe , Junaid Shahid , Ofir Weisse , Yosry Ahmed , Patrick Bellasi , KP Singh , Alexandra Sandulescu , Matteo Rizzo , Jann Horn Cc: x86@kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, kvm@vger.kernel.org, Brendan Jackman From: Reiji Watanabe Currently, all dynamic percpu memory is implicitly (and unintentionally) treated as sensitive memory. Unconditionally map pages for dynamically allocated percpu memory as global nonsensitive memory, other than pages that are allocated for pcpu_{first,reserved}_chunk during early boot via memblock allocator (these will be taken care by the following patch). We don't support sensitive percpu memory allocation yet. Co-developed-by: Junaid Shahid Signed-off-by: Junaid Shahid Signed-off-by: Reiji Watanabe Signed-off-by: Brendan Jackman WIP: Drop VM_SENSITIVE checks from percpu code --- mm/percpu-vm.c | 50 ++++++++++++++++++++++++++++++++++++++++++++------ mm/percpu.c | 4 ++-- 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/mm/percpu-vm.c b/mm/percpu-vm.c index cd69caf6aa8d8..2935d7fbac415 100644 --- a/mm/percpu-vm.c +++ b/mm/percpu-vm.c @@ -132,11 +132,20 @@ static void pcpu_pre_unmap_flush(struct pcpu_chunk *chunk, pcpu_chunk_addr(chunk, pcpu_high_unit_cpu, page_end)); } -static void __pcpu_unmap_pages(unsigned long addr, int nr_pages) +static void ___pcpu_unmap_pages(unsigned long addr, int nr_pages) { vunmap_range_noflush(addr, addr + (nr_pages << PAGE_SHIFT)); } +static void __pcpu_unmap_pages(unsigned long addr, int nr_pages, + unsigned long vm_flags) +{ + unsigned long size = nr_pages << PAGE_SHIFT; + + asi_unmap(ASI_GLOBAL_NONSENSITIVE, (void *)addr, size); + ___pcpu_unmap_pages(addr, nr_pages); +} + /** * pcpu_unmap_pages - unmap pages out of a pcpu_chunk * @chunk: chunk of interest @@ -153,6 +162,8 @@ static void __pcpu_unmap_pages(unsigned long addr, int nr_pages) static void pcpu_unmap_pages(struct pcpu_chunk *chunk, struct page **pages, int page_start, int page_end) { + struct vm_struct **vms = (struct vm_struct **)chunk->data; + unsigned long vm_flags = vms ? vms[0]->flags : VM_ALLOC; unsigned int cpu; int i; @@ -165,7 +176,7 @@ static void pcpu_unmap_pages(struct pcpu_chunk *chunk, pages[pcpu_page_idx(cpu, i)] = page; } __pcpu_unmap_pages(pcpu_chunk_addr(chunk, cpu, page_start), - page_end - page_start); + page_end - page_start, vm_flags); } } @@ -190,13 +201,38 @@ static void pcpu_post_unmap_tlb_flush(struct pcpu_chunk *chunk, pcpu_chunk_addr(chunk, pcpu_high_unit_cpu, page_end)); } -static int __pcpu_map_pages(unsigned long addr, struct page **pages, - int nr_pages) +/* + * __pcpu_map_pages() should not be called during the percpu initialization, + * as asi_map() depends on the page allocator (which isn't available yet + * during percpu initialization). Instead, ___pcpu_map_pages() can be used + * during the percpu initialization. But, any pages that are mapped with + * ___pcpu_map_pages() will be treated as sensitive memory, unless + * they are explicitly mapped with asi_map() later. + */ +static int ___pcpu_map_pages(unsigned long addr, struct page **pages, + int nr_pages) { return vmap_pages_range_noflush(addr, addr + (nr_pages << PAGE_SHIFT), PAGE_KERNEL, pages, PAGE_SHIFT); } +static int __pcpu_map_pages(unsigned long addr, struct page **pages, + int nr_pages, unsigned long vm_flags) +{ + unsigned long size = nr_pages << PAGE_SHIFT; + int err; + + err = ___pcpu_map_pages(addr, pages, nr_pages); + if (err) + return err; + + /* + * If this fails, pcpu_map_pages()->__pcpu_unmap_pages() will call + * asi_unmap() and clean up any partial mappings. + */ + return asi_map(ASI_GLOBAL_NONSENSITIVE, (void *)addr, size); +} + /** * pcpu_map_pages - map pages into a pcpu_chunk * @chunk: chunk of interest @@ -214,13 +250,15 @@ static int __pcpu_map_pages(unsigned long addr, struct page **pages, static int pcpu_map_pages(struct pcpu_chunk *chunk, struct page **pages, int page_start, int page_end) { + struct vm_struct **vms = (struct vm_struct **)chunk->data; + unsigned long vm_flags = vms ? vms[0]->flags : VM_ALLOC; unsigned int cpu, tcpu; int i, err; for_each_possible_cpu(cpu) { err = __pcpu_map_pages(pcpu_chunk_addr(chunk, cpu, page_start), &pages[pcpu_page_idx(cpu, page_start)], - page_end - page_start); + page_end - page_start, vm_flags); if (err < 0) goto err; @@ -232,7 +270,7 @@ static int pcpu_map_pages(struct pcpu_chunk *chunk, err: for_each_possible_cpu(tcpu) { __pcpu_unmap_pages(pcpu_chunk_addr(chunk, tcpu, page_start), - page_end - page_start); + page_end - page_start, vm_flags); if (tcpu == cpu) break; } diff --git a/mm/percpu.c b/mm/percpu.c index 4e11fc1e6deff..d8309f2ea4e44 100644 --- a/mm/percpu.c +++ b/mm/percpu.c @@ -3328,8 +3328,8 @@ int __init pcpu_page_first_chunk(size_t reserved_size, pcpu_fc_cpu_to_node_fn_t pcpu_populate_pte(unit_addr + (i << PAGE_SHIFT)); /* pte already populated, the following shouldn't fail */ - rc = __pcpu_map_pages(unit_addr, &pages[unit * unit_pages], - unit_pages); + rc = ___pcpu_map_pages(unit_addr, &pages[unit * unit_pages], + unit_pages); if (rc < 0) panic("failed to map percpu area, err=%d\n", rc);