From patchwork Mon Jun 29 17:15:29 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Thompson X-Patchwork-Id: 191993 Delivered-To: patches@linaro.org Received: by 2002:a92:d244:0:0:0:0:0 with SMTP id v4csp3067572ilg; Mon, 29 Jun 2020 10:15:44 -0700 (PDT) X-Received: by 2002:a1c:6006:: with SMTP id u6mr17208697wmb.111.1593450943888; Mon, 29 Jun 2020 10:15:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1593450943; cv=none; d=google.com; s=arc-20160816; b=WXzoZ/8cHx+vmQRG01iivR6+Xl1KrufzdiqTOn9JqDwbzaOS9fwbUDbwYr5+u55dHr 4PHxm/E3ZO4F4XzDL+xZkF7zLlVIHyJ1NdSLJBeEub4uI0BgZq1SaPBO/uF3fL5662HE HkhMT6/S4U3XTSMIawOohOMQ69HzU+CH4gYilv9L3f7Ymnk94x0xXlVm9Q6rXDHuJtl/ hFh2iEi9/SSzZVkPrs3hcNlG2pnnXcJQUWEBVBvFD4vPC7nAVwGYDZVKdkSlzwWBbMJI LX2b+LvTrJbFsNccBzfjhXCiZMUalIASzYGXg5UKa/4hk+ReEdths1P4thsLO0bq+iZo H1Iw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:dkim-signature; bh=LdmyDXtA8EIM1jxYalqYHcGVicYKfXEl1rpwhzYY7yY=; b=nTjCuRu/KzD7nvjmY+dpXEyOguF4zgUiyarWnX8s+o7Mcm6AAAiSiOnM7j2S6Bfdu9 Ha9sLrtu+AyObBurZ6FinCSHc2Hg4CbO/RGgpbNEjgg5gtf5Ll5EN3JwdkK/1GrOA1QJ KVvBTRclOHjcO1AI46YjAEqdVcRN8xAQAh+Tsyi9aZb2mtxtguVLQrt0TeoNDtuYlEki EBdxZStb8vl+cflkuvmZi5ESm00EpxkDFDAd8zNXWHp89PqkiM21Zu+NRIW/B7KDKvs8 dpCdkYTJva+ipI/rpO/1Xm6tgKmblxNxO+oFj2HFKNzURLNVYV0Ev/10f8M+UweclKWV PoTQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=c9p9Gar7; spf=pass (google.com: domain of daniel.thompson@linaro.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=daniel.thompson@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id v12sor359306wrp.5.2020.06.29.10.15.43 for (Google Transport Security); Mon, 29 Jun 2020 10:15:43 -0700 (PDT) Received-SPF: pass (google.com: domain of daniel.thompson@linaro.org designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=c9p9Gar7; spf=pass (google.com: domain of daniel.thompson@linaro.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=daniel.thompson@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=LdmyDXtA8EIM1jxYalqYHcGVicYKfXEl1rpwhzYY7yY=; b=c9p9Gar7D2nXzpz2YR/ib39Abgv4y6hEa91cr786gH8hhBcc2XHPgejdMauKVSCXyw EgAsOWswu0yM60MBbUpqEjXeok6FXbZK3cY12YvSpijtvD0BGihf4HyPCLh9I970OF+w 78MwbI9PqGOns9VkUov0ZQ9RbuCV+fwXeAcEtPspxUT9Uk7UyYJvZDXRhVlIPAhOgy1r 0ebCtAluvcdaAZtdvp/iG7aZAmwyeaGzf8o6royVXgBp6bmZYTZ+EEf7K6vitzPvHvWn wVU1xmaE5kyGW65gr+wCRjIx7La2Aae9t0Re8V+Heu0ZC4EarsLzwXx5hOPENYGaf8DM nFPg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=LdmyDXtA8EIM1jxYalqYHcGVicYKfXEl1rpwhzYY7yY=; b=N69fEle0a7A7gJQ/U5/4yXw5x6Yga4ZhP1MXH8QF49hALyByKxGCF6R4vIlRWuwrFe Op48GHMDzsN5r4fP9/3A4NpXzFXk8NQd6SMp61IXTDWl+7R6LJQ3WkRqA+Gsg35Btqjg FX4QEn9jcXZwIosujRGcSaRKimXyk+2p6G7q6Fx6ldfNH5zD9dWumw09W7k1VRPb3Xoj lYJNfL5ARIiUw0wqNR3mFvVAsDXpAlbfQKbrp1v0VjowP4GXkCQV9O+/pFrhrh0KflN5 e1MCgdL6RwtyOLVh1Svc2vhQQIb0OA9Esm1PSq5ry66DFnePEa4B4KcV+KDriujbbPcn xbSA== X-Gm-Message-State: AOAM5318VyRcBYsnZtJrQx7XsbMA+go04ExDyi83gnD+VqrifzPHKA1Y bMjd2dWVmJP44ODFplFGiINEqWPh0Ce4ig== X-Google-Smtp-Source: ABdhPJzs30159xRqcvLpbN8z4ChYHhI0ngFXg57huo/LOl0+OMOWqLHYtRb1FBzKVGwD5ZJ8obYgyw== X-Received: by 2002:a5d:66ca:: with SMTP id k10mr7472015wrw.244.1593450943526; Mon, 29 Jun 2020 10:15:43 -0700 (PDT) Return-Path: Received: from wychelm.lan (cpc141214-aztw34-2-0-cust773.18-1.cable.virginm.net. [86.9.19.6]) by smtp.gmail.com with ESMTPSA id g195sm517482wme.38.2020.06.29.10.15.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Jun 2020 10:15:42 -0700 (PDT) From: Daniel Thompson To: Jason Wessel , Douglas Anderson Cc: Daniel Thompson , pmladek@suse.com, kgdb-bugreport@lists.sourceforge.net, linux-kernel@vger.kernel.org, patches@linaro.org Subject: [PATCH] kgdb: Resolve races during kgdb_io_register/unregister_module Date: Mon, 29 Jun 2020 18:15:29 +0100 Message-Id: <20200629171529.558003-1-daniel.thompson@linaro.org> X-Mailer: git-send-email 2.25.4 MIME-Version: 1.0 Currently kgdb_register_callbacks() and kgdb_unregister_callbacks() are called outside the scope of the kgdb_registration_lock. This allows them to race with each other. This could do all sorts of crazy things up to and including dbg_io_ops becoming NULL partway through the execution of the kgdb trap handler (which isn't allowed and would be fatal). Fix this by bringing the trap handler setup and teardown into the scope of the registration lock. Signed-off-by: Daniel Thompson --- kernel/debug/debug_core.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) base-commit: 9ebcfadb0610322ac537dd7aa5d9cbc2b2894c68 -- 2.25.4 diff --git a/kernel/debug/debug_core.c b/kernel/debug/debug_core.c index 9e5934780f41..9799f2c6dc94 100644 --- a/kernel/debug/debug_core.c +++ b/kernel/debug/debug_core.c @@ -1117,9 +1117,8 @@ int kgdb_register_io_module(struct kgdb_io *new_dbg_io_ops) dbg_io_ops = new_dbg_io_ops; - spin_unlock(&kgdb_registration_lock); - if (old_dbg_io_ops) { + spin_unlock(&kgdb_registration_lock); old_dbg_io_ops->deinit(); return 0; } @@ -1129,6 +1128,8 @@ int kgdb_register_io_module(struct kgdb_io *new_dbg_io_ops) /* Arm KGDB now. */ kgdb_register_callbacks(); + spin_unlock(&kgdb_registration_lock); + if (kgdb_break_asap && (!dbg_is_early || IS_ENABLED(CONFIG_ARCH_HAS_EARLY_DEBUG))) kgdb_initial_breakpoint(); @@ -1147,13 +1148,14 @@ void kgdb_unregister_io_module(struct kgdb_io *old_dbg_io_ops) { BUG_ON(kgdb_connected); + spin_lock(&kgdb_registration_lock); + /* * KGDB is no longer able to communicate out, so * unregister our callbacks and reset state. */ kgdb_unregister_callbacks(); - spin_lock(&kgdb_registration_lock); WARN_ON_ONCE(dbg_io_ops != old_dbg_io_ops); dbg_io_ops = NULL;