From patchwork Tue Nov 13 05:52:09 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sasha Levin X-Patchwork-Id: 10679619 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4DBE113BB for ; Tue, 13 Nov 2018 05:52:36 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3CEFF2A385 for ; Tue, 13 Nov 2018 05:52:36 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 30ACD2A3B9; Tue, 13 Nov 2018 05:52:36 +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=-3.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,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 0C3F32A385 for ; Tue, 13 Nov 2018 05:52:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7C1756B0290; Tue, 13 Nov 2018 00:52:30 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 771EE6B0292; Tue, 13 Nov 2018 00:52:30 -0500 (EST) 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 63A7A6B0293; Tue, 13 Nov 2018 00:52:30 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pf1-f198.google.com (mail-pf1-f198.google.com [209.85.210.198]) by kanga.kvack.org (Postfix) with ESMTP id 258906B0290 for ; Tue, 13 Nov 2018 00:52:30 -0500 (EST) Received: by mail-pf1-f198.google.com with SMTP id a24-v6so9545540pfn.12 for ; Mon, 12 Nov 2018 21:52:30 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:from:to:cc:subject:date :message-id:in-reply-to:references; bh=CX6BECc3YcJQEi8S/ZlTy6BYQ6aQ3aKhCAQ1V6xWn6Q=; b=Xs7tp0HMmE8tj+EsaKrjgnoxkmg7cvm6q6QM+mPstp4WPRqLTAZgaJLPl+UC3qIeMr 0iqR+YYV43OHxFOJqkPGQ/TOdWPavmQZxFDwgmBQT7GxJiJbaz5FPh7hb8HIE8Cav0/L 3Tin4+tx5JLMNUXT0ouPxzVXgcSZBcv9WbqZRBZaXR3P3sUY8pPkaWEozaYzesslbHfR /I7FhZBZ5GKSB6oNDnyoidCn+vGNaa1sg8wb9qOwJUq3XGZz/9eYe8moVFtuFalBK4z1 LCiAgX19pUYoJ/OGauRv2og7CjgaCUl7TyVcRIx2WhEkkakjk6Lz2rlHxnwgpxZNsHzf feTg== X-Gm-Message-State: AGRZ1gLStfBnJRbF5mcPEZBpddSd5jW3dqnZexUk1ssPBIpCje09NA43 S7Rl8IO5vHhI9JTp6JVvRuj/cfLiQ39fz6KbuZx79S43a1GLPv4k0DVaE+OBjGEoo/1PntqT/ls WoVZiCevBm6LlFZnhVhHH1ea+jEHCJJETOseOPxoMPnTpxqxVtZjjRvV7yo9QabR9mg== X-Received: by 2002:a63:42c1:: with SMTP id p184mr3504317pga.202.1542088349779; Mon, 12 Nov 2018 21:52:29 -0800 (PST) X-Google-Smtp-Source: AJdET5cRTQqAWc7Gvkoxsu0qtBvy/zrYr1qbuEb9shRhbjr5kDZy4u68t60B7nOAhovLP6s829pa X-Received: by 2002:a63:42c1:: with SMTP id p184mr3504300pga.202.1542088349011; Mon, 12 Nov 2018 21:52:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1542088348; cv=none; d=google.com; s=arc-20160816; b=GvqZle4rLB6iDMqIvwa4KrIwEw4NhqSD3NyPofmKSkIyNQ55lfqNthY+Uuh+R6IaUC tnH0Tj4zEmlUthl0mioUeKS1qjVSfnDLXrY2bpCy+sa8NZP7fSjp0aoCrP9feASezEW/ XL/XSXPrFQ2wrS2br7pVz7GfGV0n72SqjPQ7XeXTrEviIqmzENv5JynTleB1C8Rz4wEe 8FfOM6a+L3qRAEvYIUHePV+8AXdD8/ImSoNR8cdor3JHHLQUueuSwsJb+j+rXS8jrFjq aJhT6lF+4U++3ySvRvqVA4mqwKn/Q95mFVcLMYSAem3KcVAy4/SvlF0/lXaGMVTpX0qg aGSg== 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 :dkim-signature; bh=CX6BECc3YcJQEi8S/ZlTy6BYQ6aQ3aKhCAQ1V6xWn6Q=; b=O+OyPu2bxhfaW/FEzDrOLJuTUE7gZgNI2gj58B8Yi8fYgZhN8vcKmmps3/LZw5o6Uh UaqzB8Oj00ykywoYOUoSeZRTkMrK4AWsWWPK36WLRZlt0HndRg7AlMQKR/8UIAA19D7P tHeWi2E88esYubipfgis0KLfdBmmVAjo74RWgX8Js5baqbVPerEW/CFXJStx4ak4ss3W Aoe/N83eb0GKUB3HzCOOO0qfl2kzsN3MZTcSa9Fzl/2xNKOJw6VxtnYYfL6/ZicAz54E 3IfA5DmQs9H+oeNM5pOBfu0jjsGCZM8s2PXBlLt/vdTTu4HxO98QCHCX1nVkV8D+Mlsc qeIg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=YENUOlNn; spf=pass (google.com: domain of sashal@kernel.org designates 198.145.29.99 as permitted sender) smtp.mailfrom=sashal@kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from mail.kernel.org (mail.kernel.org. [198.145.29.99]) by mx.google.com with ESMTPS id m187-v6si22173768pfm.159.2018.11.12.21.52.28 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 12 Nov 2018 21:52:28 -0800 (PST) Received-SPF: pass (google.com: domain of sashal@kernel.org designates 198.145.29.99 as permitted sender) client-ip=198.145.29.99; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=YENUOlNn; spf=pass (google.com: domain of sashal@kernel.org designates 198.145.29.99 as permitted sender) smtp.mailfrom=sashal@kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from sasha-vm.mshome.net (unknown [64.114.255.114]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 4573222511; Tue, 13 Nov 2018 05:52:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1542088348; bh=+qH6clvJVT4tnjAFvF5NdR1an8frzDqL5LWjnCYzNrc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=YENUOlNnedmPvz1DS7Ilr7Jlg1SpvicNzvjsJGyyKvAesqXf/uRoTK7v8O/Q3mpX3 xFlSB/K3UA8mlzWpyryR5WvPGCu2SJea73w/YWw8zEwRKQUld+cmIUwi63LrCLtzu4 i1HcObtH+7gm6Nuckr5ytdiGf+obrppf5yWhZUpc= From: Sasha Levin To: stable@vger.kernel.org, linux-kernel@vger.kernel.org Cc: David Hildenbrand , Benjamin Herrenschmidt , Paul Mackerras , Michael Ellerman , "Rafael J. Wysocki" , Len Brown , Greg Kroah-Hartman , Boris Ostrovsky , Juergen Gross , Nathan Fontenot , John Allen , Michal Hocko , Dan Williams , Joonsoo Kim , Vlastimil Babka , Mathieu Malaterre , Pavel Tatashin , YASUAKI ISHIMATSU , Balbir Singh , Haiyang Zhang , Heiko Carstens , Jonathan Corbet , Kate Stewart , "K. Y. Srinivasan" , Martin Schwidefsky , Michael Neuling , Philippe Ombredanne , Stephen Hemminger , Thomas Gleixner , Andrew Morton , Linus Torvalds , Sasha Levin , linuxppc-dev@lists.ozlabs.org, linux-acpi@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH AUTOSEL 4.9 03/17] mm/memory_hotplug: make add_memory() take the device_hotplug_lock Date: Tue, 13 Nov 2018 00:52:09 -0500 Message-Id: <20181113055223.79060-3-sashal@kernel.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181113055223.79060-1-sashal@kernel.org> References: <20181113055223.79060-1-sashal@kernel.org> 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 From: David Hildenbrand [ Upstream commit 8df1d0e4a265f25dc1e7e7624ccdbcb4a6630c89 ] add_memory() currently does not take the device_hotplug_lock, however is aleady called under the lock from arch/powerpc/platforms/pseries/hotplug-memory.c drivers/acpi/acpi_memhotplug.c to synchronize against CPU hot-remove and similar. In general, we should hold the device_hotplug_lock when adding memory to synchronize against online/offline request (e.g. from user space) - which already resulted in lock inversions due to device_lock() and mem_hotplug_lock - see 30467e0b3be ("mm, hotplug: fix concurrent memory hot-add deadlock"). add_memory()/add_memory_resource() will create memory block devices, so this really feels like the right thing to do. Holding the device_hotplug_lock makes sure that a memory block device can really only be accessed (e.g. via .online/.state) from user space, once the memory has been fully added to the system. The lock is not held yet in drivers/xen/balloon.c arch/powerpc/platforms/powernv/memtrace.c drivers/s390/char/sclp_cmd.c drivers/hv/hv_balloon.c So, let's either use the locked variants or take the lock. Don't export add_memory_resource(), as it once was exported to be used by XEN, which is never built as a module. If somebody requires it, we also have to export a locked variant (as device_hotplug_lock is never exported). Link: http://lkml.kernel.org/r/20180925091457.28651-3-david@redhat.com Signed-off-by: David Hildenbrand Reviewed-by: Pavel Tatashin Reviewed-by: Rafael J. Wysocki Reviewed-by: Rashmica Gupta Reviewed-by: Oscar Salvador Cc: Benjamin Herrenschmidt Cc: Paul Mackerras Cc: Michael Ellerman Cc: "Rafael J. Wysocki" Cc: Len Brown Cc: Greg Kroah-Hartman Cc: Boris Ostrovsky Cc: Juergen Gross Cc: Nathan Fontenot Cc: John Allen Cc: Michal Hocko Cc: Dan Williams Cc: Joonsoo Kim Cc: Vlastimil Babka Cc: Mathieu Malaterre Cc: Pavel Tatashin Cc: YASUAKI ISHIMATSU Cc: Balbir Singh Cc: Haiyang Zhang Cc: Heiko Carstens Cc: Jonathan Corbet Cc: Kate Stewart Cc: "K. Y. Srinivasan" Cc: Martin Schwidefsky Cc: Michael Neuling Cc: Philippe Ombredanne Cc: Stephen Hemminger Cc: Thomas Gleixner Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- .../platforms/pseries/hotplug-memory.c | 2 +- drivers/acpi/acpi_memhotplug.c | 2 +- drivers/base/memory.c | 9 ++++++-- drivers/xen/balloon.c | 3 +++ include/linux/memory_hotplug.h | 1 + mm/memory_hotplug.c | 22 ++++++++++++++++--- 6 files changed, 32 insertions(+), 7 deletions(-) diff --git a/arch/powerpc/platforms/pseries/hotplug-memory.c b/arch/powerpc/platforms/pseries/hotplug-memory.c index c0a0947f43bb..656bbbd731d0 100644 --- a/arch/powerpc/platforms/pseries/hotplug-memory.c +++ b/arch/powerpc/platforms/pseries/hotplug-memory.c @@ -616,7 +616,7 @@ static int dlpar_add_lmb(struct of_drconf_cell *lmb) nid = memory_add_physaddr_to_nid(lmb->base_addr); /* Add the memory */ - rc = add_memory(nid, lmb->base_addr, block_sz); + rc = __add_memory(nid, lmb->base_addr, block_sz); if (rc) { dlpar_remove_device_tree_lmb(lmb); dlpar_release_drc(lmb->drc_index); diff --git a/drivers/acpi/acpi_memhotplug.c b/drivers/acpi/acpi_memhotplug.c index 6b0d3ef7309c..2ccfbb61ca89 100644 --- a/drivers/acpi/acpi_memhotplug.c +++ b/drivers/acpi/acpi_memhotplug.c @@ -228,7 +228,7 @@ static int acpi_memory_enable_device(struct acpi_memory_device *mem_device) if (node < 0) node = memory_add_physaddr_to_nid(info->start_addr); - result = add_memory(node, info->start_addr, info->length); + result = __add_memory(node, info->start_addr, info->length); /* * If the memory block has been used by the kernel, add_memory() diff --git a/drivers/base/memory.c b/drivers/base/memory.c index c5cdd190b781..9f96f1b43c15 100644 --- a/drivers/base/memory.c +++ b/drivers/base/memory.c @@ -500,15 +500,20 @@ memory_probe_store(struct device *dev, struct device_attribute *attr, if (phys_addr & ((pages_per_block << PAGE_SHIFT) - 1)) return -EINVAL; + ret = lock_device_hotplug_sysfs(); + if (ret) + goto out; + nid = memory_add_physaddr_to_nid(phys_addr); - ret = add_memory(nid, phys_addr, - MIN_MEMORY_BLOCK_SIZE * sections_per_block); + ret = __add_memory(nid, phys_addr, + MIN_MEMORY_BLOCK_SIZE * sections_per_block); if (ret) goto out; ret = count; out: + unlock_device_hotplug(); return ret; } diff --git a/drivers/xen/balloon.c b/drivers/xen/balloon.c index e4db19e88ab1..a10c64fee9ac 100644 --- a/drivers/xen/balloon.c +++ b/drivers/xen/balloon.c @@ -358,7 +358,10 @@ static enum bp_state reserve_additional_memory(void) * callers drop the mutex before trying again. */ mutex_unlock(&balloon_mutex); + /* add_memory_resource() requires the device_hotplug lock */ + lock_device_hotplug(); rc = add_memory_resource(nid, resource, memhp_auto_online); + unlock_device_hotplug(); mutex_lock(&balloon_mutex); if (rc) { diff --git a/include/linux/memory_hotplug.h b/include/linux/memory_hotplug.h index 134a2f69c21a..9469eef30095 100644 --- a/include/linux/memory_hotplug.h +++ b/include/linux/memory_hotplug.h @@ -272,6 +272,7 @@ static inline void remove_memory(int nid, u64 start, u64 size) {} extern int walk_memory_range(unsigned long start_pfn, unsigned long end_pfn, void *arg, int (*func)(struct memory_block *, void *)); +extern int __add_memory(int nid, u64 start, u64 size); extern int add_memory(int nid, u64 start, u64 size); extern int add_memory_resource(int nid, struct resource *resource, bool online); extern int zone_for_memory(int nid, u64 start, u64 size, int zone_default, diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c index 0f962cc3f1bf..1e8ba77f642d 100644 --- a/mm/memory_hotplug.c +++ b/mm/memory_hotplug.c @@ -1339,7 +1339,12 @@ static int online_memory_block(struct memory_block *mem, void *arg) return memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE); } -/* we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG */ +/* + * NOTE: The caller must call lock_device_hotplug() to serialize hotplug + * and online/offline operations (triggered e.g. by sysfs). + * + * we are OK calling __meminit stuff here - we have CONFIG_MEMORY_HOTPLUG + */ int __ref add_memory_resource(int nid, struct resource *res, bool online) { u64 start, size; @@ -1417,9 +1422,9 @@ int __ref add_memory_resource(int nid, struct resource *res, bool online) mem_hotplug_done(); return ret; } -EXPORT_SYMBOL_GPL(add_memory_resource); -int __ref add_memory(int nid, u64 start, u64 size) +/* requires device_hotplug_lock, see add_memory_resource() */ +int __ref __add_memory(int nid, u64 start, u64 size) { struct resource *res; int ret; @@ -1433,6 +1438,17 @@ int __ref add_memory(int nid, u64 start, u64 size) release_memory_resource(res); return ret; } + +int add_memory(int nid, u64 start, u64 size) +{ + int rc; + + lock_device_hotplug(); + rc = __add_memory(nid, start, size); + unlock_device_hotplug(); + + return rc; +} EXPORT_SYMBOL_GPL(add_memory); #ifdef CONFIG_MEMORY_HOTREMOVE From patchwork Tue Nov 13 05:52:21 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sasha Levin X-Patchwork-Id: 10679623 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E4D9113B5 for ; Tue, 13 Nov 2018 05:52:43 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D51A02A2B7 for ; Tue, 13 Nov 2018 05:52:43 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D36FA2A376; Tue, 13 Nov 2018 05:52:43 +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=-3.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,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 2A67C2A318 for ; Tue, 13 Nov 2018 05:52:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 308576B0292; Tue, 13 Nov 2018 00:52:41 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 28FC76B0294; Tue, 13 Nov 2018 00:52:41 -0500 (EST) 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 17EED6B0295; Tue, 13 Nov 2018 00:52:41 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pf1-f199.google.com (mail-pf1-f199.google.com [209.85.210.199]) by kanga.kvack.org (Postfix) with ESMTP id CBC086B0292 for ; Tue, 13 Nov 2018 00:52:40 -0500 (EST) Received: by mail-pf1-f199.google.com with SMTP id e89so1535319pfb.17 for ; Mon, 12 Nov 2018 21:52:40 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:from:to:cc:subject:date :message-id:in-reply-to:references; bh=SRqptq9XxiMFv5x7HTwy3kL/DSkrQFRQapmYlXuY7Cc=; b=X2JXCg0RzNM9lEHzb8mLhpGA18iyb5vLlBW8h+UFBMPcmar07hyK5Kk47FBJqwtZPH I3bL73lMUP+VMLr+j8nZNel1d8lRbMoTsQctrgQoY7hV7JMaAhZj+exmp/IPVDogQdDR RsQ6weU0FniV30y1N/FV3f4fGKKfrBbk+TqjGxa/fTiI5cVFhrkUn33elQEthYhahRwm rb0cdONRbMDPkXMcTIb3yjXT4U3M/w+QY7JPMrh147HWFlxA0OyaIfbisD8N+775F2MZ /rw+2sHDZRHfIwqHYHaw3BDzcA4lchTkI6IAGfd8/p6YmyqodJ8Ua6HDXy2T2MFm1vGu so4w== X-Gm-Message-State: AGRZ1gL/IvCLDuz0Ns5VsGKWei2IuXCyy4/QcOvZjs6kf+/dXJa2nUH3 U+5g+O5k6Cdg6Uff2UVFza49E/0rdn2PKUzpiV8MtAKB+ay0xo0id9lQRtPJ79T8auCeMUdYcrI aAOFzmIJ2Cnx/vY/Pd4O+LThCIC/RL0cB2dTtwnEuVDnDZxRusgcXaY/RO8lVIRcKOQ== X-Received: by 2002:a63:6906:: with SMTP id e6mr3481851pgc.144.1542088360519; Mon, 12 Nov 2018 21:52:40 -0800 (PST) X-Google-Smtp-Source: AJdET5dr5zEIzTLb9w+n0mEXbPELXB4kA0NSEnRxmK7mZZv0fnDuMl7QzntOununNZxY/O17OG0e X-Received: by 2002:a63:6906:: with SMTP id e6mr3481827pgc.144.1542088359912; Mon, 12 Nov 2018 21:52:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1542088359; cv=none; d=google.com; s=arc-20160816; b=Rh+oqVPp+jOuJN6KadJocSzktWYY4UCtdN3UOLPygPpUzdBFJnTgVSqyuaS7wrSrRs 6K980aS9Vz0lkT/mHYPP41pOvNjA3SY/0SMKAMxfgfJUTbgkUNOJPtUy+yY6/rFo/A2v iTdi+KtY7kEO0fZdoAjVhzBp/l3dYxjt4BbrPju++ZndLS/jmVoYfYg6VejMjA/WTRe6 QeQF37P3YXitAmAK4q5pgu3hMO+HmG/Up698x9JF4nS8gIlfGHybpL3qgQwEguB2SiqI lrk3yBmmjnQbNhZzmfP/OiXPLx1+hK21saQrTELWrP/eTTvZe0MtHJhh8eqzXAXkm3K/ Zv2w== 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 :dkim-signature; bh=SRqptq9XxiMFv5x7HTwy3kL/DSkrQFRQapmYlXuY7Cc=; b=Ab3AnYW2/FzIcp8SjE7aXmU4NhXOgBhlbeIv9+lzeL5gvcSYW1HiLOk3T6YsJlXrTF THgWF+we8Pps8+d9WTzjLXFmPiN4OJ2TZMdWhBQ6EvRl7x5/hNIsJFCgzjx3O+H06U4k UbxbjowCNGGZtYVbtJQAaYmloYg1htr0M3eDkqQ8rAHrHihQxD4lYFWlcWQ0DgKhMV9X th5RflZlpe39NJOSYNrJwxDwLpn3ntThQ0IyeYc/180T+eeYIdJz1b+77HoiR/ZRyQRA MMtNFcZ3JbYP2YQr46SGSTpyuBpqmkuHG3ECNU08VX3xUaPogvxtaYM+MVoT5jIw4HTd a2nA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=sshEF2Im; spf=pass (google.com: domain of sashal@kernel.org designates 198.145.29.99 as permitted sender) smtp.mailfrom=sashal@kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from mail.kernel.org (mail.kernel.org. [198.145.29.99]) by mx.google.com with ESMTPS id q8-v6si19701369pgc.347.2018.11.12.21.52.39 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 12 Nov 2018 21:52:39 -0800 (PST) Received-SPF: pass (google.com: domain of sashal@kernel.org designates 198.145.29.99 as permitted sender) client-ip=198.145.29.99; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=sshEF2Im; spf=pass (google.com: domain of sashal@kernel.org designates 198.145.29.99 as permitted sender) smtp.mailfrom=sashal@kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from sasha-vm.mshome.net (unknown [64.114.255.114]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 5D8EE2251A; Tue, 13 Nov 2018 05:52:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1542088359; bh=aV/4HrisMlXjVY7CtVc+csH7/miKpLYVJrIzZ/xx68M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sshEF2ImLe6Uxky55QJvBhsAuWHvtFqsspYjc9BRGQ8HPwmw9mZH8XoX/jVoX3uI6 wnkuS3ZCN9TXMcOccDaHmSXqKv57IUgp7ydMQnmEw3C9y0MmViyqPDxva3y2JLKp+0 dXJVi9wrL8n3Z7qyXBNMY9PTKnIavgPJ/7X0Pm+Q= From: Sasha Levin To: stable@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Jann Horn , Davidlohr Bueso , Oleg Nesterov , Christoph Lameter , Kemi Wang , Andy Lutomirski , Ingo Molnar , Andrew Morton , Linus Torvalds , Sasha Levin , linux-mm@kvack.org Subject: [PATCH AUTOSEL 4.9 15/17] mm/vmstat.c: assert that vmstat_text is in sync with stat_items_size Date: Tue, 13 Nov 2018 00:52:21 -0500 Message-Id: <20181113055223.79060-15-sashal@kernel.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181113055223.79060-1-sashal@kernel.org> References: <20181113055223.79060-1-sashal@kernel.org> 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 From: Jann Horn [ Upstream commit f0ecf25a093fc0589f0a6bc4c1ea068bbb67d220 ] Having two gigantic arrays that must manually be kept in sync, including ifdefs, isn't exactly robust. To make it easier to catch such issues in the future, add a BUILD_BUG_ON(). Link: http://lkml.kernel.org/r/20181001143138.95119-3-jannh@google.com Signed-off-by: Jann Horn Reviewed-by: Kees Cook Reviewed-by: Andrew Morton Acked-by: Roman Gushchin Acked-by: Michal Hocko Cc: Davidlohr Bueso Cc: Oleg Nesterov Cc: Christoph Lameter Cc: Kemi Wang Cc: Andy Lutomirski Cc: Ingo Molnar Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- mm/vmstat.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mm/vmstat.c b/mm/vmstat.c index 5e6a4d76659d..2bab38d5570c 100644 --- a/mm/vmstat.c +++ b/mm/vmstat.c @@ -1484,6 +1484,8 @@ static void *vmstat_start(struct seq_file *m, loff_t *pos) stat_items_size += sizeof(struct vm_event_state); #endif + BUILD_BUG_ON(stat_items_size != + ARRAY_SIZE(vmstat_text) * sizeof(unsigned long)); v = kmalloc(stat_items_size, GFP_KERNEL); m->private = v; if (!v) From patchwork Tue Nov 13 05:52:22 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sasha Levin X-Patchwork-Id: 10679625 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8E2D013BB for ; Tue, 13 Nov 2018 05:52:48 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 785772A2B7 for ; Tue, 13 Nov 2018 05:52:48 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 767552A3F9; Tue, 13 Nov 2018 05:52:48 +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=-3.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,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 BB5F42A402 for ; Tue, 13 Nov 2018 05:52:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4201B6B0294; Tue, 13 Nov 2018 00:52:43 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 37B576B0296; Tue, 13 Nov 2018 00:52:43 -0500 (EST) 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 269926B0297; Tue, 13 Nov 2018 00:52:43 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pf1-f200.google.com (mail-pf1-f200.google.com [209.85.210.200]) by kanga.kvack.org (Postfix) with ESMTP id D8EBF6B0294 for ; Tue, 13 Nov 2018 00:52:42 -0500 (EST) Received: by mail-pf1-f200.google.com with SMTP id o28-v6so6356753pfk.10 for ; Mon, 12 Nov 2018 21:52:42 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:from:to:cc:subject:date :message-id:in-reply-to:references; bh=4rM+zzo7/v/s+SHqVN2wEty6XNaOjBDtJGGjTvQ5XKc=; b=an8QcvksxBxWsirN9X1kT8nXH5qlgT1Me6HJb90cClc7Vr5epKFgNnY/+UQ5BSB4aL Mo5Y//Y0GenNKG/L3mkAJcA9wP+8pGQJBlceRyNiZFJ5PcsFazPaQM4c+nAAsNX+deRx O052tIenvf4U5QMAbFJrKzHj6/gwC4te5GmMchoKpigH4KEXhPQjs4nQapVgKn0PCmGe mqE4PtHrMwqhk9hQeYIlEUzTn5iDCX6X1Sf5AQ0BQ/vUhJwlparmkGr9BcDVIEvV9UpL Y3D+IZuy5yroxp7quFc9HiCXt9+zNx372UIrZDyA769M9qeBF3LwW3eUtaVaS0fdyjpi OLbw== X-Gm-Message-State: AGRZ1gKzjlbT3oY6QqNIS3kZUGmzPLioJSn2TpLIah1qNzCHheWpfVVu CYRyaBx4gqRdjxzplADkDE433PxDG3yybvgG2P5BnGTa9Q8mVAVdasEy7GCe2tl1yTQtusMuqPm YO5aefmQmKHSqYtBHzqopICxWuQf0fz0XnQcYUSjgXBhlSH+z3nlDPIpI5QDCJBQ5zA== X-Received: by 2002:a63:5407:: with SMTP id i7mr3503990pgb.413.1542088362523; Mon, 12 Nov 2018 21:52:42 -0800 (PST) X-Google-Smtp-Source: AJdET5fj5xTGcSgTOTWS3GD/qit4c1kVNQZBhHX/DSBs5WwzlMLDhOkrje21QUXN4TouyxlKZsN6 X-Received: by 2002:a63:5407:: with SMTP id i7mr3503960pgb.413.1542088361699; Mon, 12 Nov 2018 21:52:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1542088361; cv=none; d=google.com; s=arc-20160816; b=JQR8Tyfx94VOC2WpxExeXX6S48YAGBb1HdMGTzx9nfOS0AmHq+4MLb5of9gasKjdqa y6613fwRL6rv/Bcb/yjFi2Kar4cXB1CyLC+N+mPCMMXpgD4aTcGSEJQ0XcWSsp82eQhi HaJn3QS7A773SlRwQP/Y64OMfFhEJyVWuPkt2UhcvfiAVpulD5EGohDm8XO04q+Lh8XY xdvLHUeWZL/LbJvIkWRqx1NESqbR+upJe4QsW7cB8iN7/I6D8u4uvdvRWSsX8ghwCQnL S0NFOj6TpeKhxIsVbCclOfIeu0feZbPaUMtohYZS2XkHaYfS744tW3rK2bxn1InfGkR9 3jEA== 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 :dkim-signature; bh=4rM+zzo7/v/s+SHqVN2wEty6XNaOjBDtJGGjTvQ5XKc=; b=CkHWZGMGAGzuMB8hDraqScwR/yx2+3eWUkQueClb8dqLDZrFRkJ51PYEIuiGxYIB+a stN48BZkpmEcRmBv64WvcSz7GaQqzTxLHKZT9/FO31YzMYjhM9YcawL9BPGGwwhj8Qbb r2ZIjlpCZRvU3SVS4ZilyisMNdoFJrJNhzZZpYwryM5R3g1NJKVOc/0uByTsICvDB6ha iEBcok6rS4LJsVH98JnmejDBoPLN33G2PykxkBpkLsjEE7iOkhXZoUAcMjFbXOQqAMty Iiwgkzy6c+pxROlb7B5+BPUsJvvSe1yUnSl/Oz8vELSq4AD3sI0CiNLRHcL8DqYc7DhF AdaA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=qX4G7JJq; spf=pass (google.com: domain of sashal@kernel.org designates 198.145.29.99 as permitted sender) smtp.mailfrom=sashal@kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from mail.kernel.org (mail.kernel.org. [198.145.29.99]) by mx.google.com with ESMTPS id h3si19307674pgi.391.2018.11.12.21.52.41 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 12 Nov 2018 21:52:41 -0800 (PST) Received-SPF: pass (google.com: domain of sashal@kernel.org designates 198.145.29.99 as permitted sender) client-ip=198.145.29.99; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=qX4G7JJq; spf=pass (google.com: domain of sashal@kernel.org designates 198.145.29.99 as permitted sender) smtp.mailfrom=sashal@kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from sasha-vm.mshome.net (unknown [64.114.255.114]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 3EEC42251D; Tue, 13 Nov 2018 05:52:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1542088361; bh=P/0AuPw3ZpCOOANV96befJtEvWk9R1zto4pWPIT1cfg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qX4G7JJqPnDds1PRqeqTqx7l3MD2ulbnFvqk+tRQARfDfreWN5sjOOeP99xGlpnue Lq/JXEEqARMidFDkceTdqDa5ZRX4u2sBb45M+GnYaPBOi8BP4XZuh3+ZYverSBHius zp0BjoDd3tSmHiyBMGzf1p03lfBrFakzTm7YzTns= From: Sasha Levin To: stable@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Andrea Arcangeli , Andrew Morton , Linus Torvalds , Sasha Levin , linux-mm@kvack.org Subject: [PATCH AUTOSEL 4.9 16/17] userfaultfd: allow get_mempolicy(MPOL_F_NODE|MPOL_F_ADDR) to trigger userfaults Date: Tue, 13 Nov 2018 00:52:22 -0500 Message-Id: <20181113055223.79060-16-sashal@kernel.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181113055223.79060-1-sashal@kernel.org> References: <20181113055223.79060-1-sashal@kernel.org> 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 From: Andrea Arcangeli [ Upstream commit 3b9aadf7278d16d7bed4d5d808501065f70898d8 ] get_mempolicy(MPOL_F_NODE|MPOL_F_ADDR) called a get_user_pages that would not be waiting for userfaults before failing and it would hit on a SIGBUS instead. Using get_user_pages_locked/unlocked instead will allow get_mempolicy to allow userfaults to resolve the fault and fill the hole, before grabbing the node id of the page. If the user calls get_mempolicy() with MPOL_F_ADDR | MPOL_F_NODE for an address inside an area managed by uffd and there is no page at that address, the page allocation from within get_mempolicy() will fail because get_user_pages() does not allow for page fault retry required for uffd; the user will get SIGBUS. With this patch, the page fault will be resolved by the uffd and the get_mempolicy() will continue normally. Background: Via code review, previously the syscall would have returned -EFAULT (vm_fault_to_errno), now it will block and wait for an userfault (if it's waken before the fault is resolved it'll still -EFAULT). This way get_mempolicy will give a chance to an "unaware" app to be compliant with userfaults. The reason this visible change is that becoming "userfault compliant" cannot regress anything: all other syscalls including read(2)/write(2) had to become "userfault compliant" long time ago (that's one of the things userfaultfd can do that PROT_NONE and trapping segfaults can't). So this is just one more syscall that become "userfault compliant" like all other major ones already were. This has been happening on virtio-bridge dpdk process which just called get_mempolicy on the guest space post live migration, but before the memory had a chance to be migrated to destination. I didn't run an strace to be able to show the -EFAULT going away, but I've the confirmation of the below debug aid information (only visible with CONFIG_DEBUG_VM=y) going away with the patch: [20116.371461] FAULT_FLAG_ALLOW_RETRY missing 0 [20116.371464] CPU: 1 PID: 13381 Comm: vhost-events Not tainted 4.17.12-200.fc28.x86_64 #1 [20116.371465] Hardware name: LENOVO 20FAS2BN0A/20FAS2BN0A, BIOS N1CET54W (1.22 ) 02/10/2017 [20116.371466] Call Trace: [20116.371473] dump_stack+0x5c/0x80 [20116.371476] handle_userfault.cold.37+0x1b/0x22 [20116.371479] ? remove_wait_queue+0x20/0x60 [20116.371481] ? poll_freewait+0x45/0xa0 [20116.371483] ? do_sys_poll+0x31c/0x520 [20116.371485] ? radix_tree_lookup_slot+0x1e/0x50 [20116.371488] shmem_getpage_gfp+0xce7/0xe50 [20116.371491] ? page_add_file_rmap+0x1a/0x2c0 [20116.371493] shmem_fault+0x78/0x1e0 [20116.371495] ? filemap_map_pages+0x3a1/0x450 [20116.371498] __do_fault+0x1f/0xc0 [20116.371500] __handle_mm_fault+0xe2e/0x12f0 [20116.371502] handle_mm_fault+0xda/0x200 [20116.371504] __get_user_pages+0x238/0x790 [20116.371506] get_user_pages+0x3e/0x50 [20116.371510] kernel_get_mempolicy+0x40b/0x700 [20116.371512] ? vfs_write+0x170/0x1a0 [20116.371515] __x64_sys_get_mempolicy+0x21/0x30 [20116.371517] do_syscall_64+0x5b/0x160 [20116.371520] entry_SYSCALL_64_after_hwframe+0x44/0xa9 The above harmless debug message (not a kernel crash, just a dump_stack()) is shown with CONFIG_DEBUG_VM=y to more quickly identify and improve kernel spots that may have to become "userfaultfd compliant" like this one (without having to run an strace and search for syscall misbehavior). Spots like the above are more closer to a kernel bug for the non-cooperative usages that Mike focuses on, than for for dpdk qemu-cooperative usages that reproduced it, but it's still nicer to get this fixed for dpdk too. The part of the patch that caused me to think is only the implementation issue of mpol_get, but it looks like it should work safe no matter the kind of mempolicy structure that is (the default static policy also starts at 1 so it'll go to 2 and back to 1 without crashing everything at 0). [rppt@linux.vnet.ibm.com: changelog addition] http://lkml.kernel.org/r/20180904073718.GA26916@rapoport-lnx Link: http://lkml.kernel.org/r/20180831214848.23676-1-aarcange@redhat.com Signed-off-by: Andrea Arcangeli Reported-by: Maxime Coquelin Tested-by: Dr. David Alan Gilbert Reviewed-by: Mike Rapoport Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- mm/mempolicy.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/mm/mempolicy.c b/mm/mempolicy.c index 69c4a0c92ebb..625122adefe0 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -845,16 +845,19 @@ static void get_policy_nodemask(struct mempolicy *p, nodemask_t *nodes) } } -static int lookup_node(unsigned long addr) +static int lookup_node(struct mm_struct *mm, unsigned long addr) { struct page *p; int err; - err = get_user_pages(addr & PAGE_MASK, 1, 0, &p, NULL); + int locked = 1; + err = get_user_pages_locked(addr & PAGE_MASK, 1, 0, &p, &locked); if (err >= 0) { err = page_to_nid(p); put_page(p); } + if (locked) + up_read(&mm->mmap_sem); return err; } @@ -865,7 +868,7 @@ static long do_get_mempolicy(int *policy, nodemask_t *nmask, int err; struct mm_struct *mm = current->mm; struct vm_area_struct *vma = NULL; - struct mempolicy *pol = current->mempolicy; + struct mempolicy *pol = current->mempolicy, *pol_refcount = NULL; if (flags & ~(unsigned long)(MPOL_F_NODE|MPOL_F_ADDR|MPOL_F_MEMS_ALLOWED)) @@ -905,7 +908,16 @@ static long do_get_mempolicy(int *policy, nodemask_t *nmask, if (flags & MPOL_F_NODE) { if (flags & MPOL_F_ADDR) { - err = lookup_node(addr); + /* + * Take a refcount on the mpol, lookup_node() + * wil drop the mmap_sem, so after calling + * lookup_node() only "pol" remains valid, "vma" + * is stale. + */ + pol_refcount = pol; + vma = NULL; + mpol_get(pol); + err = lookup_node(mm, addr); if (err < 0) goto out; *policy = err; @@ -940,7 +952,9 @@ static long do_get_mempolicy(int *policy, nodemask_t *nmask, out: mpol_cond_put(pol); if (vma) - up_read(¤t->mm->mmap_sem); + up_read(&mm->mmap_sem); + if (pol_refcount) + mpol_put(pol_refcount); return err; } From patchwork Tue Nov 13 05:52:23 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sasha Levin X-Patchwork-Id: 10679669 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 0058D13B5 for ; Tue, 13 Nov 2018 05:56:46 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E298C29FAC for ; Tue, 13 Nov 2018 05:56:45 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D6B752A264; Tue, 13 Nov 2018 05:56:45 +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=-3.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,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 4634B29FAC for ; Tue, 13 Nov 2018 05:56:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4DBC56B02A0; Tue, 13 Nov 2018 00:56:44 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 488D76B02A2; Tue, 13 Nov 2018 00:56:44 -0500 (EST) 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 32B8B6B02A3; Tue, 13 Nov 2018 00:56:44 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pg1-f198.google.com (mail-pg1-f198.google.com [209.85.215.198]) by kanga.kvack.org (Postfix) with ESMTP id DF61F6B02A0 for ; Tue, 13 Nov 2018 00:56:43 -0500 (EST) Received: by mail-pg1-f198.google.com with SMTP id q62so6007777pgq.9 for ; Mon, 12 Nov 2018 21:56:43 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:from:to:cc:subject:date :message-id:in-reply-to:references; bh=zuQPdsDPXCbo+Uo4hKVYxA7KU6CtPFJlnlj+4TAb2ng=; b=NmK7Mqmdr2eo+k6jcZF2Q13z+0KCqC0mk4iSMlGxejavjx0oMU6zRZTJoe57WERqF6 jPoOvRU2YoKH2wkp3MC2/TtoPt0LJ9GlXZcMgIUT+NPPilnWMvhnScCblyRn47zds9z/ p/jiQFeh3rTx+tQtbk4HhvtOQqwCu8301KLKcwrYJuLFVjXFidJupd/VWqQDADQL4BVi D1HNu/uY6t87yIk69QFGFQxztHhtzYWPs1tsDe0r6IsXW/dWXhbpRKvgxZmbdI3IJ+iL o9taRdWaWm4EHPomtSD6LbqHmFzevDiyl72dizeJ5tDGygpIuCEP6GfbK0nEdZTQBtKa 3dOg== X-Gm-Message-State: AGRZ1gIUKQRTL/v1HJT5OkyjKT/JKzlMO6C7rJ/Xcr5Y89DGcmMo1Tz/ Bok/XlP+x7F7Y9+ZCEcjFdO1YhMAckV/AEdxySWAeVgJ5yOZYiB534NcaEFk380jm0L+duJZNCO pPI3mw1HdIks9blB68tWhxOssaJITLGI7H6i14vBYBNJwiGJ5jc2W/0r6iR7Kp2X3Sdm/X7AscQ EzaApY0lGM+xWwUl3Nw0icjAE+7lAQ/T6bMxXaBhb8DekhFLacg7Zhp3nTHcdKBBR3b8Wgu8bBF qVIOWAHF6eXeb4XDEHuEK1n7J/AiV/OvygADxkK4MPZwAXk4DpStA+jcE/T6nGzqcPQOgUWweO/ PYhGwf8kpfetdolWkVOF1ZkgIfIh0AByaOq5BXg+RGwpHY/Ol+Iqr/EMGmebfTQBVh6yArjImzY g X-Received: by 2002:a17:902:76cc:: with SMTP id j12-v6mr3783670plt.339.1542088363454; Mon, 12 Nov 2018 21:52:43 -0800 (PST) X-Received: by 2002:a17:902:76cc:: with SMTP id j12-v6mr3783665plt.339.1542088363332; Mon, 12 Nov 2018 21:52:43 -0800 (PST) X-Google-Smtp-Source: AJdET5fSlel7HSeVgHwGb2X+o8igrdoHaUjnRCUAcDRLR3SpuThd9ulRnxTCfoRGbXtypMmanmCo X-Received: by 2002:a17:902:76cc:: with SMTP id j12-v6mr3783608plt.339.1542088362074; Mon, 12 Nov 2018 21:52:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1542088362; cv=none; d=google.com; s=arc-20160816; b=eKqMrZz+CIYkZQw39N8VhGjqQwXdM/LTwtwSYDdCa8Lh8m/8nLA3PfHNFfk/1rwL2s KkyFRmqC2GOgImkp9oxXcxLV1i7/mcHIbFMSLyi8UVU7G6NnwtyL1mrnsc+yYT3lDqEH oFX/XWxhIeaiSt/ds1oAA7ZjBF3RFuT4i88oK4xC4gMEIzzfSwMhx9fxeVjWQ8IV0Xkg ojb4x74x/dFRwgy2/qjbAQURkmLaexrNVymX75Aju2IzGkFDFdOrBB98TkAuGtrTXmJ4 rraSYfyccf9I3wwxyma9BWksAqSmsE/jRgLMz78XbUsD3GU3Ve3OD7eyqjTXeeaINv1H y5Kg== 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 :dkim-signature; bh=zuQPdsDPXCbo+Uo4hKVYxA7KU6CtPFJlnlj+4TAb2ng=; b=cXVoFXKgSI61rdfV1hK19DN7XBIvJYsV47kKSvj+qJw7ysj2bSPImodNrOE4c24/8i 9z7PRD6plq9Q0Peh7jUFii48pM1+cGMhkhXZZWnlOXmF25R+BtX3OtMKZ4uO9HthkFb5 eHiYvrvJSrphWMNU9QOGNNsXJ6XRQj4tcIl+vrAEj4AfDEFfFMit80rT4Yc9nAHd8tsS HzcVsIipEmFGUyqelPZzi4lGg6l2ToRdBB0Z9Cnis9XVy6nXTGmLEO9Id1i+SjT9pz8e oWaLly+57pSEoc+EmI/M+87tNxsJvL5/aj+LTxrP+UDiCmp97F0Ww8ncGYJdU2S4RxCx OW1w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=wge2nkRb; spf=pass (google.com: domain of sashal@kernel.org designates 198.145.29.99 as permitted sender) smtp.mailfrom=sashal@kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from mail.kernel.org (mail.kernel.org. [198.145.29.99]) by mx.google.com with ESMTPS id k13si13373990pgb.343.2018.11.12.21.52.41 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 12 Nov 2018 21:52:42 -0800 (PST) Received-SPF: pass (google.com: domain of sashal@kernel.org designates 198.145.29.99 as permitted sender) client-ip=198.145.29.99; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=wge2nkRb; spf=pass (google.com: domain of sashal@kernel.org designates 198.145.29.99 as permitted sender) smtp.mailfrom=sashal@kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from sasha-vm.mshome.net (unknown [64.114.255.114]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 9AA9C2251F; Tue, 13 Nov 2018 05:52:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1542088361; bh=UeyeAhZLNpqh0cMAYMGzyy2/lL7C+oynF3ZSNb/JERY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=wge2nkRb36lj+V6VNIUrgkpzSIjMA3mU/2WKrFn65bnk4HXLMPAu8Ol0JEPEgLmMi /NCBueTpg78f4NxyfqvTjHM3jHTPr2Yohcs23+mTGt1C+YJM4UZhrUjgh2nDrqPkR5 rp38TYTN0MJ4RoeebDsaAQO21glk3FWm+ZM9q/VM= From: Sasha Levin To: stable@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Dmitry Vyukov , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Linus Torvalds , Sasha Levin , linux-mm@kvack.org Subject: [PATCH AUTOSEL 4.9 17/17] mm: don't warn about large allocations for slab Date: Tue, 13 Nov 2018 00:52:23 -0500 Message-Id: <20181113055223.79060-17-sashal@kernel.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181113055223.79060-1-sashal@kernel.org> References: <20181113055223.79060-1-sashal@kernel.org> 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 From: Dmitry Vyukov [ Upstream commit 61448479a9f2c954cde0cfe778cb6bec5d0a748d ] Slub does not call kmalloc_slab() for sizes > KMALLOC_MAX_CACHE_SIZE, instead it falls back to kmalloc_large(). For slab KMALLOC_MAX_CACHE_SIZE == KMALLOC_MAX_SIZE and it calls kmalloc_slab() for all allocations relying on NULL return value for over-sized allocations. This inconsistency leads to unwanted warnings from kmalloc_slab() for over-sized allocations for slab. Returning NULL for failed allocations is the expected behavior. Make slub and slab code consistent by checking size > KMALLOC_MAX_CACHE_SIZE in slab before calling kmalloc_slab(). While we are here also fix the check in kmalloc_slab(). We should check against KMALLOC_MAX_CACHE_SIZE rather than KMALLOC_MAX_SIZE. It all kinda worked because for slab the constants are the same, and slub always checks the size against KMALLOC_MAX_CACHE_SIZE before kmalloc_slab(). But if we get there with size > KMALLOC_MAX_CACHE_SIZE anyhow bad things will happen. For example, in case of a newly introduced bug in slub code. Also move the check in kmalloc_slab() from function entry to the size > 192 case. This partially compensates for the additional check in slab code and makes slub code a bit faster (at least theoretically). Also drop __GFP_NOWARN in the warning check. This warning means a bug in slab code itself, user-passed flags have nothing to do with it. Nothing of this affects slob. Link: http://lkml.kernel.org/r/20180927171502.226522-1-dvyukov@gmail.com Signed-off-by: Dmitry Vyukov Reported-by: syzbot+87829a10073277282ad1@syzkaller.appspotmail.com Reported-by: syzbot+ef4e8fc3a06e9019bb40@syzkaller.appspotmail.com Reported-by: syzbot+6e438f4036df52cbb863@syzkaller.appspotmail.com Reported-by: syzbot+8574471d8734457d98aa@syzkaller.appspotmail.com Reported-by: syzbot+af1504df0807a083dbd9@syzkaller.appspotmail.com Acked-by: Christoph Lameter Acked-by: Vlastimil Babka Cc: Pekka Enberg Cc: David Rientjes Cc: Joonsoo Kim Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- mm/slab.c | 4 ++++ mm/slab_common.c | 12 ++++++------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/mm/slab.c b/mm/slab.c index c59844dbd034..263dcda6897b 100644 --- a/mm/slab.c +++ b/mm/slab.c @@ -3690,6 +3690,8 @@ __do_kmalloc_node(size_t size, gfp_t flags, int node, unsigned long caller) struct kmem_cache *cachep; void *ret; + if (unlikely(size > KMALLOC_MAX_CACHE_SIZE)) + return NULL; cachep = kmalloc_slab(size, flags); if (unlikely(ZERO_OR_NULL_PTR(cachep))) return cachep; @@ -3725,6 +3727,8 @@ static __always_inline void *__do_kmalloc(size_t size, gfp_t flags, struct kmem_cache *cachep; void *ret; + if (unlikely(size > KMALLOC_MAX_CACHE_SIZE)) + return NULL; cachep = kmalloc_slab(size, flags); if (unlikely(ZERO_OR_NULL_PTR(cachep))) return cachep; diff --git a/mm/slab_common.c b/mm/slab_common.c index 622f6b6ae844..13f1926f8fcd 100644 --- a/mm/slab_common.c +++ b/mm/slab_common.c @@ -883,18 +883,18 @@ struct kmem_cache *kmalloc_slab(size_t size, gfp_t flags) { int index; - if (unlikely(size > KMALLOC_MAX_SIZE)) { - WARN_ON_ONCE(!(flags & __GFP_NOWARN)); - return NULL; - } - if (size <= 192) { if (!size) return ZERO_SIZE_PTR; index = size_index[size_index_elem(size)]; - } else + } else { + if (unlikely(size > KMALLOC_MAX_CACHE_SIZE)) { + WARN_ON(1); + return NULL; + } index = fls(size - 1); + } #ifdef CONFIG_ZONE_DMA if (unlikely((flags & GFP_DMA)))