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 588F3C54EE9 for ; Fri, 16 Sep 2022 12:32:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D38D58D0003; Fri, 16 Sep 2022 08:32:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CC0EC8D0001; Fri, 16 Sep 2022 08:32:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B3B5F8D0003; Fri, 16 Sep 2022 08:32:12 -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 9FB938D0001 for ; Fri, 16 Sep 2022 08:32:12 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 782A54029A for ; Fri, 16 Sep 2022 12:32:12 +0000 (UTC) X-FDA: 79917886104.30.64BEB75 Received: from mail-ej1-f43.google.com (mail-ej1-f43.google.com [209.85.218.43]) by imf19.hostedemail.com (Postfix) with ESMTP id 2DFB91A008A for ; Fri, 16 Sep 2022 12:32:11 +0000 (UTC) Received: by mail-ej1-f43.google.com with SMTP id u9so49172584ejy.5 for ; Fri, 16 Sep 2022 05:32:11 -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=vgm/siFgZ/KNP7qOkPjqizMwEBxYLnORvZ56mqOAwLM=; b=IzCJcgmspXAn5/f98WSp3lVBwQarQN40ro1YfD5wQc06u1DokK/xvkItlkONumojDg KMa8PfulFVW2XeAXW1Bgw74kuFaHoSUnqfRz0bIscKNo99cbrehh4vRCOrkRx2eb50Nd iZ5v+V755T/BdUTwLQh4q6xQZxK5zeexHMDbAQdRfwSVNA5gPhzf86XlilVjGewBUoGd Ib8sbSldr6bZXDBCEC7aCkRPgR3QQgXap9Z27waz0oCZ4HOqbpEi8cPi3C0sI+l6lr8l odZk/0abCMlrT9ZhdkI/D8RRKF85iWhuNb1wIyR0b/fTa6xBaLzaFa90K+qbFXMWKIfX eb6w== 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=vgm/siFgZ/KNP7qOkPjqizMwEBxYLnORvZ56mqOAwLM=; b=URWGa/JlqRcod+CfGSom1vwtPHwbgUakeGkaEA13KhS2SCH3k22MkE6d1KP3/NwCJf qdKbbCeeeOJ3FZmN/i1jLb2v7ZAjOxfahqg9O98ibXFMuRnbsLYd6R+Qkq8PEcBzYLm5 5BCaaMGUf2Rn9no+LIzidvQ9zWcKpbvJi5RbRgnFDidHVAilu6NQUu16gsLvjOHugYfN RrFBiTBfPYJ51S3mmXrwHW5hN0PlHMDF9CEqniUJMufYRtmp7pF8A7kJ8jGNqrekkIVi HvJj94VLY7qERl+ZKevDIu/x2xErOIVNqjsf0O3DbpvQ0tBIdrlBHGJe1GrLu4i7uMrB vASg== X-Gm-Message-State: ACrzQf3SewAs0Y0+61SxzU4JKO2uRTkODJpm1iGGUKd1ch7nSJH9zlea npiMREF/MpA5AFyV0pxD79A= X-Google-Smtp-Source: AMsMyM6vKYBb3gQ6Aj2lPOZB/BcRR0lNsMA5QRANjycol9FfBjf0WPzWWhwhZ00aaeuHReU/jfimeQ== X-Received: by 2002:a17:907:2bdd:b0:77d:e0f3:81e5 with SMTP id gv29-20020a1709072bdd00b0077de0f381e5mr3484155ejc.328.1663331530777; Fri, 16 Sep 2022 05:32:10 -0700 (PDT) Received: from pc636 (host-90-235-8-56.mobileonline.telia.com. [90.235.8.56]) by smtp.gmail.com with ESMTPSA id rl2-20020a170907216200b007415f8ffcbbsm10425196ejb.98.2022.09.16.05.32.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 16 Sep 2022 05:32:10 -0700 (PDT) From: Uladzislau Rezki X-Google-Original-From: Uladzislau Rezki Date: Fri, 16 Sep 2022 14:32:07 +0200 To: Kees Cook , Matthew Wilcox , Yu Zhao , Andrew Morton Cc: Kees Cook , Matthew Wilcox , Yu Zhao , Andrew Morton , dev@der-flo.net, Linux-MM , 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: ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1663331532; a=rsa-sha256; cv=none; b=QfDcj380XRM9sWSPvKf1y9EBaTL5o9cmXcuIIHj2jY8XYwAkjAFuJ1Ck7frpftIGCE7rUU FCB/f06ymRF60QrYCfrQrZAtg6/OtK75FxrkK0D4UDj0ze4SCp4qTQ2LQGOsu3nbKjDTy6 pCvBmc7AqHU7gKQYyugcbTCeM7lrho0= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=IzCJcgms; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf19.hostedemail.com: domain of urezki@gmail.com designates 209.85.218.43 as permitted sender) smtp.mailfrom=urezki@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1663331532; 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=vgm/siFgZ/KNP7qOkPjqizMwEBxYLnORvZ56mqOAwLM=; b=hKT3SpndCqEi6CC/k4geX383UJmq4GkxGy+rwxlEXOwvpaP4tVN9yBJroFtzwKzD/sdCOK 5GAR14U7HWKhHmWvpPRIyFSH2E9/Y8M2u++HvpnNEPjBh6ufLsCeiwW+oYlAjXeqEFBifw OviovBEwV+RPkMSKxFfPAMCVXpwNGv4= X-Rspam-User: Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=IzCJcgms; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf19.hostedemail.com: domain of urezki@gmail.com designates 209.85.218.43 as permitted sender) smtp.mailfrom=urezki@gmail.com X-Stat-Signature: 9mi4pqnrxd7f8z3huthnm8u4zogk9fab X-Rspamd-Queue-Id: 2DFB91A008A X-Rspamd-Server: rspam09 X-HE-Tag: 1663331531-22830 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:28:08PM +0200, Uladzislau Rezki wrote: > 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. > Another thing that i should mention is, using sleepable locks(it is for PREEMPT_RT) is not allowed in any atomic. So for PREEMPT_RT point of view it is broken. -- Uladzislau Rezki