Received: by 10.192.165.148 with SMTP id m20csp4124175imm; Mon, 23 Apr 2018 19:48:57 -0700 (PDT) X-Google-Smtp-Source: AIpwx4+xlVKecjYy28Xjl1hctQdWt34pLFozFw06Xg0fbgoS21PNdt2oWDtlwNRDzobpVWGZc+Vi X-Received: by 10.98.59.203 with SMTP id w72mr14620932pfj.129.1524538137082; Mon, 23 Apr 2018 19:48:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524538137; cv=none; d=google.com; s=arc-20160816; b=Etzj2bIEsKht5KnVWZ3yEl8eVLDA9/TXM39VX9voS5tQJCcoDgg0PFMB7gzViIWFxr pw5EWy8+08bbkXKjW8WyoIxM+JZML1zpd2AZDW+8/9S3qVO+/LxDirYW3K4STLGVWYUa WT3MLeKWlxtDzxG8i+JK2iY8DAwny/NLvm9EIGB6bQF4YLdc07J11UHzXdKkxmyeVyZM WohNTk1DZVC9XBJruG9twqUI0yU0BO9zNNyC/dANIp1prUcuSVntoSd+X2TtFqhdoME9 F+1xkOllpo9n+iepciw8qNWPr6Vp43iW3gX+bsb3zXPS8cWtU0HaIdwHWP+ZzPaC3Hur j3CQ== 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:dkim-signature :arc-authentication-results; bh=/+jCmOhOjbEIDI9U5oaV2GbmZKJWgEH6FnTlInmPSa8=; b=CXkPaIDUb8B/D4YukKhsvL5Ng7bOWxpfxZeTG5idaO0TXMjBEYvjqNtg1catgbnrf3 wbHnFtbeBVIR39+cbADOJWyUyXmzaX65CuhEiHWNMOAo8FyzF2U0hwucUK2yvPjNNJ4L 22HkXXp8M9HERti5QLeNAuVw7t8GY9Lgd/8jjYAymFftDYdjvlm9hXS4f1wcTJtQ0haL dLewrJ/j9gwLbmekHjIvAww1igtGSyperEVmkPdXsokBGzCgQejc8YHTwRGGzYSBWk7f f2lpO4pUeMk7trkereJhVQpOWsmkFqR+s5fq02pj/hfFg3+S5dQejB+YIA0IoNO32/nK +7JA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=aSh6NAEn; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z3si10769763pgr.171.2018.04.23.19.48.42; Mon, 23 Apr 2018 19:48:57 -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; dkim=pass header.i=@google.com header.s=20161025 header.b=aSh6NAEn; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932753AbeDXCr2 (ORCPT + 99 others); Mon, 23 Apr 2018 22:47:28 -0400 Received: from mail-pg0-f66.google.com ([74.125.83.66]:33797 "EHLO mail-pg0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932721AbeDXCrZ (ORCPT ); Mon, 23 Apr 2018 22:47:25 -0400 Received: by mail-pg0-f66.google.com with SMTP id p10so9736985pgn.1 for ; Mon, 23 Apr 2018 19:47:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:in-reply-to:message-id:references :user-agent:mime-version; bh=/+jCmOhOjbEIDI9U5oaV2GbmZKJWgEH6FnTlInmPSa8=; b=aSh6NAEn6rVF739MrQQz6QJbIrNuGm4IsTerEcKeeIA37g93zsC+HZdMDiKk3TyxWZ GNIxnMSwc6Eypsan2Xbx1WoXyFManqdZPMvO5gQJTqGnRa1zGLR3v3DconFMgx3fM+TS 6ZlZFwzZi4Nv03LzOMtSoLmoQ9dBzGdp9GjzdSkf1dA76++E0nO0UMfrdEzQUm07nz9a KMHeBu/XuCrNEep4aNzRppriHIX4cm4AxkbHpr6AIaSdbKXRS1tIR/ikI8kDm2rwZpYq LL5rTnaXckwRVBLo9NlprGy4R9WibUt6paK56F4zRMBAWK6yf/B0Ri1vJUoljyh42EGb GbjQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:in-reply-to:message-id :references:user-agent:mime-version; bh=/+jCmOhOjbEIDI9U5oaV2GbmZKJWgEH6FnTlInmPSa8=; b=bdJU1zY0bo4sNU0V91dj4xxvw03vJi9wLAiOIC9ijcSv8IswJQ6jZST2N06hM+clWg KzG+n9rg6p1B4M07k9jwDdNjDLayOw9Y+ywU8c2GmQjml39zDqJPR03X6xr0NQLMNvGj GONw9cBnUVQnzkRZCAMENx7eteP3YrlZyYtJSzHCZcgaA2rXLneAm+uoo+j7dPoW2HxN y78Nwt6g75SsC3Phh3CmZZTYIc17+0X8+H3ai3SE8pLDPi9RwfayJtdspCELOKq0ZWLH p8KR3KM7puXCwfbKx3vWlaF+CNVxh9eQMnkFY35J/4jobamwQBsRHQzmYwA/xKt/jHj2 PQ3A== X-Gm-Message-State: ALQs6tDwbyz9x6hSEgrQ6MTevyhQvTh7VAvX4NlspvPW5OBXKjrCGie1 6s6J/rfVEEMWDJK5DKf5gpMWhg== X-Received: by 10.99.139.202 with SMTP id j193mr17971046pge.300.1524538044215; Mon, 23 Apr 2018 19:47:24 -0700 (PDT) Received: from [2620:15c:17:3:3a5:23a7:5e32:4598] ([2620:15c:17:3:3a5:23a7:5e32:4598]) by smtp.gmail.com with ESMTPSA id 204sm22394784pgb.67.2018.04.23.19.47.22 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 23 Apr 2018 19:47:23 -0700 (PDT) Date: Mon, 23 Apr 2018 19:47:22 -0700 (PDT) From: David Rientjes X-X-Sender: rientjes@chino.kir.corp.google.com To: Mikulas Patocka cc: Michal Hocko , 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: Re: [PATCH v3] kvmalloc: always use vmalloc if CONFIG_DEBUG_SG In-Reply-To: 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.21 (DEB 202 2017-01-01) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 23 Apr 2018, Mikulas Patocka wrote: > 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. > Why not just do it unconditionally? Sounds better than "50% of the time this will catch bugs". > 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 You can just do do_vmalloc: __maybe_unused > return __vmalloc_node_flags_caller(size, node, flags, > __builtin_return_address(0)); > } > >