Received: by 10.192.165.148 with SMTP id m20csp4009726imm; Mon, 23 Apr 2018 17:08:32 -0700 (PDT) X-Google-Smtp-Source: AIpwx49Vjb7B9Z34ieU0mg7UB3JpmnYurp9OUz09rYfxuQCPwS9w4wK+Gzv31BxSDtKTnpQ5OTdX X-Received: by 2002:a17:902:aa46:: with SMTP id c6-v6mr22080843plr.154.1524528512056; Mon, 23 Apr 2018 17:08:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524528512; cv=none; d=google.com; s=arc-20160816; b=JIyIy14L7OEbQv8c+uXQMyGs5oQkqjSjCHH7Gk7+u0/uMyPqeXxkbjgetsRGVqwyIw 4YtTLPsQQIhNvxb1KZ5r3juVM/4l/r8lvVrWiO1YHqLSGSmSzQ3xZM6r+T11MYY5aY1r am6NkRFSLMbnm/b83pmnHThTNfZx+NkEr8vyNtjY+kwIykVd/99SMXv3+RZfd0w5xJVZ 1vDePVueswFIon3g09+XxJeVJq8mM2Ayc+NjY+LavDeAzqgN07hMD7bL7VIVI01EahkN kY5ObexcZ99PtfE3ghHo7IyKZ96wRh+9Ptjj5S9N0AtnIGZsVz9P8sgdQbrKt9rfaDZe Xhbg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :message-id:in-reply-to:subject:cc:to:from:date :arc-authentication-results; bh=rZMUGmRnKgI666xO1CsdNlfLan4224/R7/2XWWrr3Z8=; b=gD6nK+RmGN2uo9JWQ0OKlJm+w+qRaXXjIebuDA0XBDvyImThzI9VvNHUVAdE3SiEZL 7ADAUcS7hORTwm3HaseoDB7ca+DnWHV0LxkmfpjY4de94jfZ4Qw8/FeTZVKgHkcrtEF/ PQgyO7AZ9ySdZ79oKd7kGxLs4g5zwJRUBr6b7rbKQz0UK2CWC41vpY2LNtZEDG1bBKWE +FxfckzTpV1QeuM+ty063/fwU76AelWXYL69rQnLAlFvxUVKuOdSFJ4Wv+j5Diaf/B3D vefckNTfZnxAAaFjsJ+feiSHWM8z6CfCPbpAYuxtEIsJ+8Yv5p0j4ROSa5gGVRvCMLq8 a11A== 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=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r7-v6si10756028ple.585.2018.04.23.17.08.04; Mon, 23 Apr 2018 17:08:32 -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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932664AbeDXAGU (ORCPT + 99 others); Mon, 23 Apr 2018 20:06:20 -0400 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:33222 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S932615AbeDXAGR (ORCPT ); Mon, 23 Apr 2018 20:06:17 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 229E381A88B3; Tue, 24 Apr 2018 00:06:17 +0000 (UTC) Received: from file01.intranet.prod.int.rdu2.redhat.com (file01.intranet.prod.int.rdu2.redhat.com [10.11.5.7]) by smtp.corp.redhat.com (Postfix) with ESMTPS id EFF4F21568B2; Tue, 24 Apr 2018 00:06:16 +0000 (UTC) Received: from file01.intranet.prod.int.rdu2.redhat.com (localhost [127.0.0.1]) by file01.intranet.prod.int.rdu2.redhat.com (8.14.4/8.14.4) with ESMTP id w3O06GV4009851; Mon, 23 Apr 2018 20:06:16 -0400 Received: from localhost (mpatocka@localhost) by file01.intranet.prod.int.rdu2.redhat.com (8.14.4/8.14.4/Submit) with ESMTP id w3O06Go2009847; Mon, 23 Apr 2018 20:06:16 -0400 X-Authentication-Warning: file01.intranet.prod.int.rdu2.redhat.com: mpatocka owned process doing -bs Date: Mon, 23 Apr 2018 20:06:16 -0400 (EDT) From: Mikulas Patocka X-X-Sender: mpatocka@file01.intranet.prod.int.rdu2.redhat.com To: Michal Hocko cc: Matthew Wilcox , David Miller , Andrew Morton , linux-mm@kvack.org, eric.dumazet@gmail.com, edumazet@google.com, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, mst@redhat.com, jasowang@redhat.com, virtualization@lists.linux-foundation.org, dm-devel@redhat.com, Vlastimil Babka Subject: [PATCH v3] kvmalloc: always use vmalloc if CONFIG_DEBUG_SG In-Reply-To: <20180423151545.GU17484@dhcp22.suse.cz> Message-ID: References: <20180418.134651.2225112489265654270.davem@davemloft.net> <20180420130852.GC16083@dhcp22.suse.cz> <20180420210200.GH10788@bombadil.infradead.org> <20180421144757.GC14610@bombadil.infradead.org> <20180423151545.GU17484@dhcp22.suse.cz> User-Agent: Alpine 2.02 (LRH 1266 2009-07-14) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Tue, 24 Apr 2018 00:06:17 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Tue, 24 Apr 2018 00:06:17 +0000 (UTC) for IP:'10.11.54.6' DOMAIN:'int-mx06.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'mpatocka@redhat.com' RCPT:'' Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The kvmalloc function tries to use kmalloc and falls back to vmalloc if kmalloc fails. Unfortunatelly, some kernel code has bugs - it uses kvmalloc and then uses DMA-API on the returned memory or frees it with kfree. Such bugs were found in the virtio-net driver, dm-integrity or RHEL7 powerpc-specific code. These bugs are hard to reproduce because kvmalloc falls back to vmalloc only if memory is fragmented. In order to detect these bugs reliably I submit this patch that changes kvmalloc to fall back to vmalloc with 1/2 probability if CONFIG_DEBUG_SG is turned on. CONFIG_DEBUG_SG is used, because it makes the DMA API layer verify the addresses passed to it, and so the user will get a reliable stacktrace. Some bugs (such as buffer overflows) are better detected with kmalloc code, so we must test the kmalloc path too. Signed-off-by: Mikulas Patocka --- mm/util.c | 10 ++++++++++ 1 file changed, 10 insertions(+) Index: linux-2.6/mm/util.c =================================================================== --- linux-2.6.orig/mm/util.c 2018-04-23 00:12:05.000000000 +0200 +++ linux-2.6/mm/util.c 2018-04-23 17:57:02.000000000 +0200 @@ -14,6 +14,7 @@ #include #include #include +#include #include #include @@ -404,6 +405,12 @@ void *kvmalloc_node(size_t size, gfp_t f */ WARN_ON_ONCE((flags & GFP_KERNEL) != GFP_KERNEL); +#ifdef CONFIG_DEBUG_SG + /* Catch bugs when the caller uses DMA API on the result of kvmalloc. */ + if (!(prandom_u32_max(2) & 1)) + goto do_vmalloc; +#endif + /* * We want to attempt a large physically contiguous block first because * it is less likely to fragment multiple larger blocks and therefore @@ -427,6 +434,9 @@ void *kvmalloc_node(size_t size, gfp_t f if (ret || size <= PAGE_SIZE) return ret; +#ifdef CONFIG_DEBUG_SG +do_vmalloc: +#endif return __vmalloc_node_flags_caller(size, node, flags, __builtin_return_address(0)); }