Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp601261ybz; Wed, 15 Apr 2020 14:56:11 -0700 (PDT) X-Google-Smtp-Source: APiQypK4tMN8jvJPFj4AUpAsPl48NZTe8LZ4OkE4IJqYM5QvxU5C4EZDiZxHIBzS0wrbaE+U9NFb X-Received: by 2002:a50:8d02:: with SMTP id s2mr27968346eds.81.1586987771576; Wed, 15 Apr 2020 14:56:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586987771; cv=none; d=google.com; s=arc-20160816; b=z+TMLv9xJANhR39jH/B86ow5lSAACoFt4s7qZeOI5rQB43OfHyyEi96WVcJKURhQ9W u8lIYQr1XUvrjpK9tfP/yE5TRp4aSvcZf1LzGeb2zRClj3Ej0o6wwLU5O+i1S5HErb/K Ixk+yxOrCxx4ilMaq/Hwvl8sUfUU4iKkpcIkRu3+P3e2Xx2eud7t6SqKzKU64aoaPhcu miv8mgC940gMrsaSYbtoupTvBwAjE8n7KP1aWzIWU7ukPZnxugL9bwwRAgb5Soq088G7 VupsvIv8AHCD37Qmba1z3LuKkkumD+e929+wasrSZ7cD+NPBQDL2dPNET7ppvYzvX/px mM7w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=3UvKGtfoV0Ab32vRdr+A6Zs78juH/jsZy4Qb8qV2VBM=; b=FmaDLO12lwK1VgRT7CjnYnsu9XHXALaxpuJ5lLMY+rW0VfzDx/U8aExk40y0CE32qF FG38w2Ww+jKcpB9doM2MdK9u6h+0u+iKqNloIPkiF0jDjXGjyit32t29KDMuN2CzskOz mfHDYCFu7vtZ3Wo20sk19M4fGyzMtX+jtMTzj9CgxRV6S7cSvdnbIN4qKxzLA2XdGu68 1p7oinbc2/qDV7VtOEBnTDmVK7DEeMa32nnLKTkISiQuPYapzGLREHoX38g1xNxwdrMT INNl2pxFfKoao2eJhfGd7jgd7ZdzKc2pGxpBKyFDXuhaUt4NCSK9B5Bohi9YdZxMwF89 obnQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=NmI1aKh3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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. [23.128.96.18]) by mx.google.com with ESMTP id dn12si8096097ejc.52.2020.04.15.14.55.47; Wed, 15 Apr 2020 14:56:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=NmI1aKh3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S2633702AbgDNVGf (ORCPT + 99 others); Tue, 14 Apr 2020 17:06:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46372 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S2633695AbgDNVGX (ORCPT ); Tue, 14 Apr 2020 17:06:23 -0400 Received: from mail-pg1-x544.google.com (mail-pg1-x544.google.com [IPv6:2607:f8b0:4864:20::544]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A488DC061A0C for ; Tue, 14 Apr 2020 14:06:23 -0700 (PDT) Received: by mail-pg1-x544.google.com with SMTP id h69so467851pgc.8 for ; Tue, 14 Apr 2020 14:06:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=3UvKGtfoV0Ab32vRdr+A6Zs78juH/jsZy4Qb8qV2VBM=; b=NmI1aKh3fNiZ7q6mGa1CY2Xphm7W/86aLKU/xiEPRgHYCvdtOF4npN5ImLGt5z7zS1 bjui72TB7vCN3Ihfic9xLlS0qkRQ/s7RC6OgNDLko2SlWQcQiLoaxLzRySZrCcKGyNdZ c2ohqf48YZAO7+LaPru0/QPOc+Ob4IKBbu71kWvjOATeDL0FoDQNMHQuZodPCep5mMdC siBnJb1DZ4qrZrcQqLwXA+3HKXS+eZ9bEfXDBzUF1+cpQOVU8FcWhqxZTjZqPp71Z83n OYNa7tBorgTtSaafNbRdxZ1nkXcP1LSE2ln/uPRtXgfXz+gjhY89gV+wN5GhFKnQBot2 DEfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=3UvKGtfoV0Ab32vRdr+A6Zs78juH/jsZy4Qb8qV2VBM=; b=r5EnezOFeMtGt4Wad+P+RLUW9+zIja5THGYGzix/1NJrgnFCYHcd3SqnjZ9E74VxIZ X1Og6Zk67bjf54zi192lzP1zZtMm5p2ee0XQwLoGF1PE7nDUOJkvHwVunwxRYNDQrSz9 PpreYjq8rv0RXpcYMtThEzhlgEQrcnEu9hHASvOAsJAgNEGaUVseHD4xzOWV2+E38Tc5 1wdgYVY9Qxz7OwTxaf9A33SA2EjrQxuDKxEd9NkO7V9F8v1AvaMJAVdhnhhDgTlfPDNC wH/Kl79kNqDoXcXFonTX+go73Ng4m4cNIY0eYwkSRJJ12XLFZ9HtnxSasbgHDj7X/F/O obwQ== X-Gm-Message-State: AGi0PuYrv2i5hMvdabpSnN+bOw9on2xqwe7aV0GZkNjtj6y1as7ISM2J CqUtEVq7bsKSet2eTPPv8Ypl9/6AlY+azlJLfB4+Ww== X-Received: by 2002:a62:8343:: with SMTP id h64mr26164739pfe.166.1586898382787; Tue, 14 Apr 2020 14:06:22 -0700 (PDT) MIME-Version: 1.0 References: <20200305223350.GA2852@mara.localdomain> <20200306120525.GC68079@kuha.fi.intel.com> <20200310111837.GA1368052@kuha.fi.intel.com> <20200414081513.GD2828150@kuha.fi.intel.com> In-Reply-To: From: Brendan Higgins Date: Tue, 14 Apr 2020 14:06:11 -0700 Message-ID: Subject: Re: BUG: kernel NULL pointer dereference, address: 00 - ida_free+0x76/0x140 To: Heikki Krogerus Cc: Naresh Kamboju , Sakari Ailus , Andy Shevchenko , Hans de Goede , "rafael.j.wysocki" , open list , "open list:KERNEL SELFTEST FRAMEWORK" , Steven Rostedt , Sergey Senozhatsky , Andy Shevchenko , Shuah Khan , Anders Roxell , lkft-triage@lists.linaro.org, Rasmus Villemoes Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Apr 14, 2020 at 12:27 PM Brendan Higgins wrote: > > On Tue, Apr 14, 2020 at 12:18 PM Brendan Higgins > wrote: > > > > On Tue, Apr 14, 2020 at 1:15 AM Heikki Krogerus > > wrote: > > > > > > Hi, > > > > > > On Tue, Apr 07, 2020 at 01:56:16PM -0700, Brendan Higgins wrote: > > > > On Tue, Apr 7, 2020 at 2:25 AM Naresh Kamboju wrote: > > > > > > > > > > On Wed, 11 Mar 2020 at 02:16, Brendan Higgins wrote: > > > > > > > > > > > > > > > Steps reproduce by using kselftests, > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > - lsmod || true > > > > > > > > > > > > > > > - cd /opt/kselftests/default-in-kernel/lib/ > > > > > > > > > > > > > > > - export PATH=/opt/kselftests/default-in-kernel/kselftest:$PATH > > > > > > > > > > > > > > > - ./printf.sh || true > > > > > > > > > > > > > > > - ./bitmap.sh || true > > > > > > > > > > > > > > > - ./prime_numbers.sh || true > > > > > > > > > > > > > > > - ./strscpy.sh || true > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > x86_64 kernel BUG dump. > > > > > > > > > > > > > > > + ./printf.sh > > > > > > > > > > > > > > > > > > > > > > > > > > Oops, I am wondering if I broke this with my change "Revert "software > > > > > > > > > > > > > node: Simplify software_node_release() function"": > > > > > > > > > > > > > > > > > > > > > > > > > > https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next.git/commit/?id=d1c19322388d6935b534b494a2c223dd089e30dd > > > > > > > > > > > > > > > > > > > > > > > > > > I am still investigating, will update later. > > > > > > > > > > > > > > > > > > > > > > > > Okay, yeah, I am pretty sure I caused the breakage. I got an email > > > > > > > > > > > > from kernel test robot a couple days ago that I didn't see: > > > > > > > > > > > > > > > > > > > > > > > > https://lists.01.org/hyperkitty/list/lkp@lists.01.org/thread/N3ZN5XH7HK24JVEJ5WSQD2SK6YCDRILR/ > > > > > > > > > > > > > > > > > > > > > > > > It shows the same breakage after applying this change. > > > > > > > > > > > > > > > > > > > > > > > > I am still investigating how my change broke it, nevertheless. > > > > > > > > > > > > > > > > > > > > > > As nodes in the tree are being removed, the code before the patch that > > > > > > > > > > > "simplified" the software_node_release() function accessed the node's parent > > > > > > > > > > > in its release function. > > > > > > > > > > > > > > > > > > > > > > And if CONFIG_DEBUG_KOBJECT_RELEASE is defined, the release functions are no > > > > > > > > > > > longer necessarily called in order, leading to referencing released memory. > > > > > > > > > > > Oops! > > > > > > > > > > > > > > > > > > > > > > So Heikki's patch actually fixed a bug. :-) > > > > > > > > > > > > > > > > > > > > Well, I think it just hid the problem. It looks like the core > > > > > > > > > > (lib/kobject.c) allows the parent kobject to be released before the > > > > > > > > > > last child kobject is released. To be honest, that does not sound > > > > > > > > > > right to me... > > > > > > > > > > > > > > > > > > > > I think we can workaround this problem by taking reference to the > > > > > > > > > > parent when the child is added, and then releasing it when the child > > > > > > > > > > is released, and in that way be guaranteed that the parent will not > > > > > > > > > > disappear before the child is fully released, but that still does not > > > > > > > > > > feel right. It feels more like the core is not doing it's job to me. > > > > > > > > > > The parent just should not be released before its children. > > > > > > > > > > > > > > > > > > > > Either I'm wrong about that, and we still should take the reference on > > > > > > > > > > the parent, or we revert my patch like Brendan proposed and then fix > > > > > > > > > > > > > > > > > > Either way, isn't it wrong to release the node ID before deleting the > > > > > > > > > sysfs entry? I am not sure that my fix was the correct one, but I > > > > > > > > > believe the bug that Heidi and I found is actually a bug. > > > > > > > > > > > > > > I agree. > > > > > > > > > > > > > > > > > the core with something like this (warning, I did not even try to > > > > > > > > > > compile that): > > > > > > > > > > > > > > > > > > I will try it out. > > > > > > > > > > > > > > > > > > > diff --git a/lib/kobject.c b/lib/kobject.c > > > > > > > > > > index 83198cb37d8d..ec5774992337 100644 > > > > > > > > > > --- a/lib/kobject.c > > > > > > > > > > +++ b/lib/kobject.c > > > > > > > > > > @@ -680,6 +680,12 @@ static void kobject_cleanup(struct kobject *kobj) > > > > > > > > > > kobject_uevent(kobj, KOBJ_REMOVE); > > > > > > > > > > } > > > > > > > > > > > > > > > > > > > > + if (t && t->release) { > > > > > > > > > > + pr_debug("kobject: '%s' (%p): calling ktype release\n", > > > > > > > > > > + kobject_name(kobj), kobj); > > > > > > > > > > + t->release(kobj); > > > > > > > > > > + } > > > > > > > > > > + > > > > > > > > > > /* remove from sysfs if the caller did not do it */ > > > > > > > > > > if (kobj->state_in_sysfs) { > > > > > > > > > > pr_debug("kobject: '%s' (%p): auto cleanup kobject_del\n", > > > > > > > > > > @@ -687,12 +693,6 @@ static void kobject_cleanup(struct kobject *kobj) > > > > > > > > > > kobject_del(kobj); > > > > > > > > > > } > > > > > > > > > > > > > > > > > > > > - if (t && t->release) { > > > > > > > > > > - pr_debug("kobject: '%s' (%p): calling ktype release\n", > > > > > > > > > > - kobject_name(kobj), kobj); > > > > > > > > > > - t->release(kobj); > > > > > > > > > > - } > > > > > > > > > > - > > > > > > > > > > /* free name if we allocated it */ > > > > > > > > > > if (name) { > > > > > > > > > > pr_debug("kobject: '%s': free name\n", name); > > > > > > > > > > > > > > > > Alright, so I tried it and it looks like Heikki's suggestion worked. > > > > > > > > > > > > > > > > Is everyone comfortable going this route? > > > > > > > > > > > > > > Hold on. Another way to fix the problem is to increment the parent's > > > > > > > reference count before that kobject_del(kobj) is called, and then > > > > > > > decrementing it after t->release(kobj) is called. It may be safer to > > > > > > > fix the problem like that. > > > > > > > > > > > > Right, this was your first suggestion above, right? That actually made > > > > > > more sense to me, but you seemed skeptical of it due to it being > > > > > > messier, which makes sense. > > > > > > > > > > > > Nevertheless, having children take a reference seems like the right > > > > > > thing to do because the children need to degregister themselves from > > > > > > the parent. Calling t->release() ahead of kobject_del() seems to > > > > > > reintroduce the problem that I pointed out, albeit *much* more > > > > > > briefly. If I understand correctly, it is always wrong to have a sysfs > > > > > > entry that points to a partially deallocated kobject. Please correct > > > > > > me if I am wrong. > > > > > > > > > > > > So I think there are two solutions: Either we have to ensure that each > > > > > > child is deallocated first so we can preserve the kobject_del() and > > > > > > then t->release() ordering, or we have to add some sort of "locking" > > > > > > mechanism to prevent the kobject from being accessed by anything other > > > > > > than the deallocation code until it is fully deallocated; well, it > > > > > > would have to prevent any access at all :-). I think it goes without > > > > > > saying that this "locking" idea is pretty flawed. > > > > > > > > > > > > The problem with just having children take a reference is that the > > > > > > kobject children already take a reference to their parent, so it seems > > > > > > like the kobject should be smart enough to deallocate children rather > > > > > > than having swnode have to keep a separate tally of children, no? > > > > > > > > > > > > Sorry if this all seems obvious, I am not an expert on this part of the kernel. > > > > > > > > > > > > > My example above proofs that there is the problem, but it changes the > > > > > > > order of execution which I think can always have other consequences. > > > > > > > > > > > > > > > Also, should I send this fix as a separate patch? Or do people want me > > > > > > > > to send an updated revision of my revert patch with the fix? > > > > > > > > > > > > > > This needs to be send in its own separate patch. Ideally it could be > > > > > > > send together with the revert in the same series, but I'm not sure > > > > > > > that's possible anymore. Didn't Greg pick the revert already? > > > > > > > > > > > > Sounds good. > > > > > > > > > > > > I did already let Greg know when he emailed us on backporting the > > > > > > patch to stable, and he acked saying he removed them. So as long as > > > > > > these are not in the queue for 5.6 (it is not in Linus' tree yet), we > > > > > > should be good. > > > > > > > > > > The reported bug is still noticed on Linux mainline master branch > > > > > The Kernel BUG noticed on x86_64 and i386 running selftest on Linux > > > > > mainline kernel 5.6.0. > > > > > > > > Oh sorry, I thought that this patch was dropped from the maintainer's > > > > for-next branch. > > > > > > > > Heikki, what do you think about my suggestion of having kobject > > > > deallocate its children? > > > > > > I'm not sure what was this suggestion? > > > > > > > In the meantime, are people cool with the patch that Heikki proposed > > > > as a temporary mitigation? I think my solution might be a bit more > > > > involved. If I don't hear anything back, I will send out Heikki's > > > > suggestion as a patch. > > > > > > Why not just take the reference to the parent like I proposed? > > > > > > diff --git a/lib/kobject.c b/lib/kobject.c > > > index 83198cb37d8d..173046c423f8 100644 > > > --- a/lib/kobject.c > > > +++ b/lib/kobject.c > > > @@ -663,6 +663,7 @@ EXPORT_SYMBOL(kobject_get_unless_zero); > > > */ > > > static void kobject_cleanup(struct kobject *kobj) > > > { > > > + struct kobject *parent = kobj->parent; > > > struct kobj_type *t = get_ktype(kobj); > > > const char *name = kobj->name; > > > > > > @@ -680,6 +681,9 @@ static void kobject_cleanup(struct kobject *kobj) > > > kobject_uevent(kobj, KOBJ_REMOVE); > > > } > > > > > > + /* make sure the parent is not released before the (last) child */ > > > + kobject_get(parent) > > > + > > > /* remove from sysfs if the caller did not do it */ > > > if (kobj->state_in_sysfs) { > > > pr_debug("kobject: '%s' (%p): auto cleanup kobject_del\n", > > > @@ -693,6 +697,8 @@ static void kobject_cleanup(struct kobject *kobj) > > > t->release(kobj); > > > } > > > > > > + kobject_put(parent); > > > + > > > /* free name if we allocated it */ > > > if (name) { > > > pr_debug("kobject: '%s': free name\n", name); > > > > Ah, I think that will work. Sorry, I just thought that was a bit > > kludgy, but if you are okay with doing it that way, I think it will > > work. > > > > I will try it and send out a patch. > > I think I am also going to send out a new test case, in a separate > patch of course, that exercises this logic. Kind of annoying having > the printf test catching this. For anyone interested, I sent out an RFC for this test case here: https://lore.kernel.org/lkml/20200414210142.191327-1-brendanhiggins@google.com/T/#u