From patchwork Sat Jul 7 00:35:42 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 10512633 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id B9675600CB for ; Sat, 7 Jul 2018 00:35:34 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AB70328701 for ; Sat, 7 Jul 2018 00:35:34 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9F70F2883A; Sat, 7 Jul 2018 00:35:34 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 73D1D28701 for ; Sat, 7 Jul 2018 00:35:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2D50F6B0005; Fri, 6 Jul 2018 20:35:30 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 280B96B0006; Fri, 6 Jul 2018 20:35:30 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 103336B0007; Fri, 6 Jul 2018 20:35:30 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pl0-f72.google.com (mail-pl0-f72.google.com [209.85.160.72]) by kanga.kvack.org (Postfix) with ESMTP id BC51C6B0005 for ; Fri, 6 Jul 2018 20:35:29 -0400 (EDT) Received: by mail-pl0-f72.google.com with SMTP id o7-v6so5642614pll.13 for ; Fri, 06 Jul 2018 17:35:29 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references; bh=n3OSEWeTA7IFV1sWn6EbYmum3lt1A7peN275Qocno1I=; b=XuomVD0wco/XGcyN/iSE5fqXdmOrDOXA8R0ro/zn70I35UhO+e39RZcDIXJoe9kClv FFz3V2jKfDYdcL4CiPQNwu0g+q0LqoKg2a1av5W/neWxEEXfMLWGFqETbM8Qe75zP4Di N0Jkbs/K20qhUp2AHn+TTxY/+4lyV4y8FOppc/astp9d3a/JyIFeA4iRu0bqJeenHcgD +Fs5fw8OrdrVFRpxd7KTABaKmmpnHch+E4Dht7mas6qNus4KPFXPqy3qjdiNodhhpIan 4k9tZKVxWSLrs6qRPcGAT15KRTH9cZLoQPQc2/JJ40B1dR9FrzLjpw4VH5tWNLZx/xBu tT7g== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.93 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Gm-Message-State: APt69E30zab6KcOAHjMXND5PnB8hX6sE9AvxHixy7vDx8ub4nZknEpaB kewdqVSretQFv+KgYlL2WIL3+DgRwDQgL/PpfwonVFk08UpSDpvSBRDPJXJG+lQtzn11ZufffY/ Z98QFpCq1Q6kvEng/9jp2DoxHwraCtGS9U0j3uIbn+BB7HKmhhnZeQHim0Ikglg32+w== X-Received: by 2002:a65:4541:: with SMTP id x1-v6mr10834703pgr.26.1530923729406; Fri, 06 Jul 2018 17:35:29 -0700 (PDT) X-Google-Smtp-Source: AAOMgpcydxVDaT/7A+A8mxW6cyNmpaBwfrjJztLyeI6asgnVFSSiUsGQw8zA+k4wYQlfTMYH/FQN X-Received: by 2002:a65:4541:: with SMTP id x1-v6mr10834674pgr.26.1530923728390; Fri, 06 Jul 2018 17:35:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530923728; cv=none; d=google.com; s=arc-20160816; b=H3wKyk+0UtqO69v4LNzrICtiibWFSOv2JGygFFnOS1Ug4AS8xICBHbjqRyYiSAFLRX 4OMFp+SdQl/OWi6NO6j9Stu3C7uiYzzPl5vYE7Lx3Fje1qkQLGTVPNo5nFmcLK5qjscc tQMcCCQKOIH4EQsKNnTStOvBap8M7MTZVlpSJywL1k0l6FsVpWqo/8XRvJy9o25sq0Px vr9t6GJsOnUZp1JJQ+pp3GaqDbGaslwWzv1gqvXpBS7qNQoNmy9WeAC58/mue9iVfCMn R2n285J391vinwk2FCIYoPF+SAHcM+G+/2Q+VmOK6OJyiY21DarCKfglq2UMxqiC68Jz SZjw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=n3OSEWeTA7IFV1sWn6EbYmum3lt1A7peN275Qocno1I=; b=Klqw+x/uejYgKHOzyJ8APblHN7GIalMmuVunTmRDmzshHebOFBYX4XKPtvR5GYkbRv wVfgU/Kw4O8kuRbCAURwtssh1+KAF45z0qE2o9K8smfI56KQygtQgyt8RWqhWyncM9QY BCLvtjoDG0atW58s02CHvnS7NQvMrN2YaUSkG3q5gyuCnNldAKeYllDcssu805hVcmVY GbUOx77OJurIbBAkt403S34j+hJxKGIxuu9+iRYZS4s7JGNP+JpLXzkd6SB6t647ATDl QxWQWhrX0CCv/mRfJlCKUMgt4A+I2yB1v5x251bBL3aDnBV3QASEUOiiJyKyiBIMOuva 77Dg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.93 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from mga11.intel.com (mga11.intel.com. [192.55.52.93]) by mx.google.com with ESMTPS id 1-v6si9618339pla.509.2018.07.06.17.35.28 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 06 Jul 2018 17:35:28 -0700 (PDT) Received-SPF: pass (google.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.93 as permitted sender) client-ip=192.55.52.93; Authentication-Results: mx.google.com; spf=pass (google.com: domain of rick.p.edgecombe@intel.com designates 192.55.52.93 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga005.jf.intel.com ([10.7.209.41]) by fmsmga102.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 06 Jul 2018 17:35:27 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.51,318,1526367600"; d="scan'208";a="238445481" Received: from linksys13920.jf.intel.com (HELO rpedgeco-HP-Z240-Tower-Workstation.jf.intel.com) ([10.7.197.56]) by orsmga005.jf.intel.com with ESMTP; 06 Jul 2018 17:35:27 -0700 From: Rick Edgecombe To: tglx@linutronix.de, mingo@redhat.com, hpa@zytor.com, x86@kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, kernel-hardening@lists.openwall.com Cc: kristen@linux.intel.com, dave.hansen@intel.com, arjan@linux.intel.com, Rick Edgecombe Subject: [PATCH 1/3] vmalloc: Add __vmalloc_node_try_addr function Date: Fri, 6 Jul 2018 17:35:42 -0700 Message-Id: <1530923744-25687-2-git-send-email-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1530923744-25687-1-git-send-email-rick.p.edgecombe@intel.com> References: <1530923744-25687-1-git-send-email-rick.p.edgecombe@intel.com> X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP Create __vmalloc_node_try_addr function that tries to allocate at a specific address and supports caller specified behavior for whether any lazy purging happens if there is a collision. Signed-off-by: Rick Edgecombe --- include/linux/vmalloc.h | 3 + mm/vmalloc.c | 174 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 177 insertions(+) diff --git a/include/linux/vmalloc.h b/include/linux/vmalloc.h index 398e9c9..c7712c8 100644 --- a/include/linux/vmalloc.h +++ b/include/linux/vmalloc.h @@ -82,6 +82,9 @@ extern void *__vmalloc_node_range(unsigned long size, unsigned long align, unsigned long start, unsigned long end, gfp_t gfp_mask, pgprot_t prot, unsigned long vm_flags, int node, const void *caller); +extern void *__vmalloc_node_try_addr(unsigned long addr, unsigned long size, + gfp_t gfp_mask, pgprot_t prot, unsigned long vm_flags, + int node, int try_purge, const void *caller); #ifndef CONFIG_MMU extern void *__vmalloc_node_flags(unsigned long size, int node, gfp_t flags); static inline void *__vmalloc_node_flags_caller(unsigned long size, int node, diff --git a/mm/vmalloc.c b/mm/vmalloc.c index cfea25b..b6f2449 100644 --- a/mm/vmalloc.c +++ b/mm/vmalloc.c @@ -1710,6 +1710,180 @@ static void *__vmalloc_area_node(struct vm_struct *area, gfp_t gfp_mask, } /** + * __vmalloc_try_addr - try to alloc at a specific address + * @addr: address to try + * @size: size to try + * @gfp_mask: flags for the page level allocator + * @prot: protection mask for the allocated pages + * @vm_flags: additional vm area flags (e.g. %VM_NO_GUARD) + * @node: node to use for allocation or NUMA_NO_NODE + * @try_purge: try to purge if needed to fulfill and allocation + * @caller: caller's return address + * + * Try to allocate at the specific address. If it succeeds the address is + * returned. If it fails NULL is returned. If try_purge is zero, it will + * return an EBUSY ERR_PTR if it could have allocated if it was allowed to + * purge. It may trigger TLB flushes if a purge is needed, and try_purge is + * set. + */ +void *__vmalloc_node_try_addr(unsigned long addr, unsigned long size, + gfp_t gfp_mask, pgprot_t prot, unsigned long vm_flags, + int node, int try_purge, const void *caller) +{ + struct vmap_area *va; + struct vm_struct *area; + struct rb_node *n; + struct vmap_area *cur_va = NULL; + struct vmap_area *first_before = NULL; + + int not_at_end = 0; + int need_purge = 0; + int blocked = 0; + int purged = 0; + + unsigned long real_size = size; + unsigned long addr_end; + + size = PAGE_ALIGN(size); + if (!size || (size >> PAGE_SHIFT) > totalram_pages) + return NULL; + + WARN_ON(in_interrupt()); + + va = kmalloc_node(sizeof(struct vmap_area), + gfp_mask & GFP_RECLAIM_MASK, node); + if (unlikely(!va)) { + warn_alloc(gfp_mask, NULL, + "kmalloc: allocation failure"); + return NULL; + } + + area = kzalloc_node(sizeof(*area), gfp_mask & GFP_RECLAIM_MASK, node); + if (unlikely(!area)) { + warn_alloc(gfp_mask, NULL, + "kmalloc: allocation failure"); + goto failva; + } + /* + * Only scan the relevant parts containing pointers to other objects + * to avoid false negatives. + */ + kmemleak_scan_area(&va->rb_node, SIZE_MAX, gfp_mask & GFP_RECLAIM_MASK); + + if (!(vm_flags & VM_NO_GUARD)) + size += PAGE_SIZE; + + addr_end = addr + size; + if (addr > addr_end) + return NULL; + +retry: + spin_lock(&vmap_area_lock); + + n = vmap_area_root.rb_node; + while (n) { + cur_va = rb_entry(n, struct vmap_area, rb_node); + if (addr < cur_va->va_end) { + not_at_end = 1; + if (cur_va->va_start == addr) { + first_before = cur_va; + break; + } + n = n->rb_left; + } else { + first_before = cur_va; + n = n->rb_right; + } + } + + /* + * Linearly search through to make sure there is a hole, unless we are + * at the end of the VA list. + */ + if (not_at_end) { + /* + * If there is no VA that starts before the + * target address, start the check from the closest VA. + */ + if (first_before) + cur_va = first_before; + + while (cur_va->va_start < addr_end) { + if (cur_va->va_end > addr) { + if (cur_va->flags & VM_LAZY_FREE) { + need_purge = 1; + } else { + blocked = 1; + break; + } + } + + if (list_is_last(&cur_va->list, &vmap_area_list)) + break; + + cur_va = list_next_entry(cur_va, list); + } + + if (blocked || (!try_purge && need_purge)) { + /* + * If a non-lazy free va blocks the allocation, or + * we are not supposed to purge, but we need to the + * allocation fails. + */ + goto fail; + } + if (try_purge && need_purge) { + if (purged) { + /* if purged once before, give up */ + goto fail; + } else { + /* + * If the va blocking the allocation is set to + * be purged then purge all vmap_areas that are + * set to purged since this will flush the TLBs + * anyway. + */ + spin_unlock(&vmap_area_lock); + purge_vmap_area_lazy(); + need_purge = 0; + purged = 1; + goto retry; + } + } + } + + va->va_start = addr; + va->va_end = addr_end; + va->flags = 0; + __insert_vmap_area(va); + + spin_unlock(&vmap_area_lock); + + setup_vmalloc_vm(area, va, vm_flags, caller); + + addr = (unsigned long)__vmalloc_area_node(area, gfp_mask, prot, node); + if (!addr) { + warn_alloc(gfp_mask, NULL, + "vmalloc: allocation failure: %lu bytes", real_size); + return NULL; + } + + clear_vm_uninitialized_flag(area); + + kmemleak_vmalloc(area, size, gfp_mask); + + return (void *)addr; +fail: + kfree(area); + spin_unlock(&vmap_area_lock); +failva: + kfree(va); + if (need_purge && !blocked) + return ERR_PTR(-EBUSY); + return NULL; +} + +/** * __vmalloc_node_range - allocate virtually contiguous memory * @size: allocation size * @align: desired alignment