From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 12990ECAAD8 for ; Fri, 16 Sep 2022 12:28:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3723E8D0002; Fri, 16 Sep 2022 08:28:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 322EB8D0001; Fri, 16 Sep 2022 08:28:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1E9968D0002; Fri, 16 Sep 2022 08:28:14 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 0F9D28D0001 for ; Fri, 16 Sep 2022 08:28:14 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id D4C89AC0F4 for ; Fri, 16 Sep 2022 12:28:13 +0000 (UTC) X-FDA: 79917876066.23.BBAF3E7 Received: from mail-ej1-f47.google.com (mail-ej1-f47.google.com [209.85.218.47]) by imf21.hostedemail.com (Postfix) with ESMTP id 8600A1C00B7 for ; Fri, 16 Sep 2022 12:28:13 +0000 (UTC) Received: by mail-ej1-f47.google.com with SMTP id dv25so49012153ejb.12 for ; Fri, 16 Sep 2022 05:28:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:date:from:from:to:cc:subject:date; bh=GhoO7cy8j9xPQ2XLDgZlBTRXGFivLRpZc/Yxx0oSHrM=; b=kP5WTaaonq6qchMkibZuA1UuhDmCfBT1LzKymnyDJDm0aKqhWn/L03wQU4SvQf0wjr vmBxd5tfVjMEqW4yy0TL9FemEdkpCTew+ZdjvsW0/BzCveFADH323MlJf+EaDcYo4tBO h5q/YViQyo5trUgJAZWvCJEYkWVYY8a2FM89fykGrFm60yYV52eHMD2o6YvrumZX97u5 4BvInfF3Bvd/NMDA4ksfYEj8e4Uqw+vczEiEcHu0eu0JKE1YkObas2/nOdFq57iEo7CQ sFIDy2m+a8K2lRKP21IAbsSvQ2H4VNpZMXcP/8dZeeuaju4JB2uQEqFfdONXugt8WsLb ROrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:date:from:x-gm-message-state:from:to:cc:subject:date; bh=GhoO7cy8j9xPQ2XLDgZlBTRXGFivLRpZc/Yxx0oSHrM=; b=J+/c2+hTxj3iKXjqffHGtxArXWMcckFGjDX4hWVoSB0hwvt6gAl+HCkUgL9yv2MLsh 3ByskQeYkGikL3ziZcn5l8mJNC2/h35moeDp6jJcolzh+CDM8JHztMtPnBt31BuU3Nfs A6jRm1Dfw8Y+hyfASvvr15HA8eywTbO56ES+RxztdojifE46RKTImacApTyeYerT0XV9 ANVNhfHfG5lF9ZAI/H9XiuD/hZXWc9CuHW5PrdaN9psNTM6Q2OOPcvnlQOztPL6XGJyD TTbXONcH1asyBOd3tTA/9WTBbwOu6K2Pv9NWIa673f0OIBTqZPrO6zhVXdnmrrnDZaxc Wv/g== X-Gm-Message-State: ACrzQf0pUYaemd4d16ySB0wBlYga0X43DOSgWwrOeXbkcfohLTtFc09t afiga70ZFxt26ZjgzlL+mvQ= X-Google-Smtp-Source: AMsMyM6I9E1pH2PXuw4PHep7njP0cUciZwiJzT62gfGRpLqR4moXdwqHGq45qpQPFvs8fdT55PuT9w== X-Received: by 2002:a17:907:2c77:b0:77b:4445:a852 with SMTP id ib23-20020a1709072c7700b0077b4445a852mr3467112ejc.582.1663331292071; Fri, 16 Sep 2022 05:28:12 -0700 (PDT) Received: from pc636 (host-90-235-8-56.mobileonline.telia.com. [90.235.8.56]) by smtp.gmail.com with ESMTPSA id cx24-20020a05640222b800b0044ec3285b23sm13516745edb.58.2022.09.16.05.28.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Sep 2022 05:28:11 -0700 (PDT) From: Uladzislau Rezki X-Google-Original-From: Uladzislau Rezki Date: Fri, 16 Sep 2022 14:28:08 +0200 To: Kees Cook , Matthew Wilcox , Yu Zhao , Andrew Morton Cc: Matthew Wilcox , Yu Zhao , Andrew Morton , dev@der-flo.net, Linux-MM , Uladzislau Rezki , bugzilla-daemon@kernel.org Subject: Re: [Bug 216489] New: Machine freezes due to memory lock Message-ID: References: <20220915133931.ee0a6c8a86c59a144828eb60@linux-foundation.org> <202209160230.CE9E0E51@keescook> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <202209160230.CE9E0E51@keescook> ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1663331293; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=GhoO7cy8j9xPQ2XLDgZlBTRXGFivLRpZc/Yxx0oSHrM=; b=qXhJRqpQ/W457VGVD1vcgUTIwymKzAM2hvdXbEXLMSLuf1zsqd58eVI/smUfqqihqyu93A +gvtZGbqvRh1Og7BL1jGY74rs3P/Jl4OStCb3xa8BsfOvTt7lycM2AZSiuOjkVobATZlAn NDa6HYL7nIOLzSdtzqONzJY6UJCKOUQ= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=kP5WTaao; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf21.hostedemail.com: domain of urezki@gmail.com designates 209.85.218.47 as permitted sender) smtp.mailfrom=urezki@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1663331293; a=rsa-sha256; cv=none; b=zMh92K7WHyn/AoykFNQwOUp84WW/kAO872NNetKVYIKrGTQswQotmEaWra/aM+fGMvx7L2 NpanX6l6rMq90y12dp2IQY0huy5ccxshxQ3shz3sdJuO7ShLUM4FJHd74tUkJ9MsmREgt3 1j9zXQE+dCtfvggsHCW6p2jPRiQZr78= Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=kP5WTaao; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf21.hostedemail.com: domain of urezki@gmail.com designates 209.85.218.47 as permitted sender) smtp.mailfrom=urezki@gmail.com X-Stat-Signature: qe5do57mtobkhz1pa6675f1iqwhzozzw X-Rspamd-Queue-Id: 8600A1C00B7 X-Rspamd-Server: rspam12 X-Rspam-User: X-HE-Tag: 1663331293-773146 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: On Fri, Sep 16, 2022 at 02:46:39AM -0700, Kees Cook wrote: > On Fri, Sep 16, 2022 at 09:38:33AM +0100, Matthew Wilcox wrote: > > On Thu, Sep 15, 2022 at 05:59:56PM -0600, Yu Zhao wrote: > > > I think this is a manifest of the lockdep warning I reported a couple > > > of weeks ago: > > > https://lore.kernel.org/r/CAOUHufaPshtKrTWOz7T7QFYUNVGFm0JBjvM700Nhf9qEL9b3EQ@mail.gmail.com/ > > > > That would certainly match the symptoms. > > > > Turning vmap_lock into an NMI-safe lock would be bad. I don't even know > > if we have primitives for that (it's not like you can disable an NMI > > ...) > > > > I don't quite have time to write a patch right now. Perhaps something > > like: > > > > struct vmap_area *find_vmap_area_nmi(unsigned long addr) > > { > > struct vmap_area *va; > > > > if (spin_trylock(&vmap_area_lock)) > > return NULL; > > va = __find_vmap_area(addr, &vmap_area_root); > > spin_unlock(&vmap_area_lock); > > > > return va; > > } > > > > and then call find_vmap_area_nmi() in check_heap_object(). I may have > > the polarity of the return value of spin_trylock() incorrect. > > I think we'll need something slightly tweaked, since this would > return NULL under any contention (and a NULL return is fatal in > check_heap_object()). It seems like we need to explicitly check > for being in nmi context in check_heap_object() to deal with it? > Like this (only build tested): > > > diff --git a/include/linux/vmalloc.h b/include/linux/vmalloc.h > index 096d48aa3437..c8a00f181a11 100644 > --- a/include/linux/vmalloc.h > +++ b/include/linux/vmalloc.h > @@ -216,6 +216,7 @@ void free_vm_area(struct vm_struct *area); > extern struct vm_struct *remove_vm_area(const void *addr); > extern struct vm_struct *find_vm_area(const void *addr); > struct vmap_area *find_vmap_area(unsigned long addr); > +struct vmap_area *find_vmap_area_try(unsigned long addr); > > static inline bool is_vm_area_hugepages(const void *addr) > { > diff --git a/mm/usercopy.c b/mm/usercopy.c > index c1ee15a98633..9f943c29e7ec 100644 > --- a/mm/usercopy.c > +++ b/mm/usercopy.c > @@ -173,7 +173,16 @@ static inline void check_heap_object(const void *ptr, unsigned long n, > } > > if (is_vmalloc_addr(ptr)) { > - struct vmap_area *area = find_vmap_area(addr); > + struct vmap_area *area; > + > + if (!in_nmi()) { > + area = find_vmap_area(addr); > + } else { > + area = find_vmap_area_try(addr); > + /* Give up under NMI to avoid deadlocks. */ > + if (!area) > + return; > + } > > if (!area) > usercopy_abort("vmalloc", "no area", to_user, 0, n); > diff --git a/mm/vmalloc.c b/mm/vmalloc.c > index dd6cdb201195..f14f1902c2f6 100644 > --- a/mm/vmalloc.c > +++ b/mm/vmalloc.c > @@ -1840,6 +1840,17 @@ struct vmap_area *find_vmap_area(unsigned long addr) > return va; > } > > +struct vmap_area *find_vmap_area_try(unsigned long addr) > +{ > + struct vmap_area *va = NULL; > + > + if (spin_trylock(&vmap_area_lock)) { > + va = __find_vmap_area(addr, &vmap_area_root); > + spin_unlock(&vmap_area_lock); > + } > + return va; > +} > + > /*** Per cpu kva allocator ***/ > > /* > OK. The problem is about using find_vmap_area() from the IRQ context. Indeed it can be dead-locked. It is not supposed to be used there. But if you want then we should have a helper. Please note that it might be a regular IRQ also so it is not limited to NMI context only, because somebody could decide later to use it from a regular IRQ. IMHO, it makes sense to make use of in_interrupt() helper instead so we cover here a hw-IRQ context including NMI one. It also would be aligned with deferred vfreeing: tatic void __vfree(const void *addr) { if (unlikely(in_interrupt())) __vfree_deferred(addr); else __vunmap(addr, 1); } so we handle here not only NMI scenario. I think we should align. -- Uladzislau Rezki