Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp1969646imm; Fri, 7 Sep 2018 08:51:52 -0700 (PDT) X-Google-Smtp-Source: ANB0VdbRKJhkKBofx7nTPuL3qsGhEqE/EaB4ndsfi+VR1g4AAe5fFticZFWqZO3b+gZB0e8nLTG8 X-Received: by 2002:a62:e412:: with SMTP id r18-v6mr9220562pfh.25.1536335512614; Fri, 07 Sep 2018 08:51:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536335512; cv=none; d=google.com; s=arc-20160816; b=iVSlI8p6K8c7Yvob435XBELU61Wtov50VgFxosFUFZk8kYxOCdm4twDKgcGgH5YzGh TceeGaTv8WhkZjoGYinf52DQNAHNzEisYQpAI0K7doGVsNvrEZFXQ7SveCjwOVh42X+F 94QpF06EJrRloLVHl3pz8awbbsRpTy3fHXSxeo4CeGvlec+97eoduHWL0nf2Gk4HZCfn a+ODHoDq+pQyyIGTZc2kw0D/G+c6+OHprGHl9xv1g8RVxRoLiw8DIfzBtnInLQNZT72x AK+1DNBTf1Ab3iNCIqXFRE9gxsRdA4BbkP/NKHRK4hhkESkXZN6Uk0GmX5T42/ZOkFmp Kd/g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=hhryv7i+6xW0/275E5fq0ZyomRtnBZUYgjT4BEdEMZE=; b=AylN0mFRvni0tWdQGvrK4gdgqZ9msmU4jmo/LLznsrwMTF/GQDHkC2MN+eWMIPLVXl p5hyrcKEnYF1QT1KSLo7m1ukuhdlPCn46cb7EKTZGdqLr/TjFJqWkRqna+bVwHtJ1aa9 Weey+ZvuDqLNcPOekMK3bvrqQrTr9xbUCms+SwVMpqtAahkMz0dU7kBygP9dRlX84Va9 KW8EFSww6C+zTNOoMEQPyC2beKV6NilTIztZq6y1tL9GST+0yFr7hONusDBasVIRGwtu Oyh63bgI5k1wjJ4B0S74kzn557uG6qlPVfJoTU/JrxlFWmweIkajHMxx6XeLbycJTmXt xnjg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@aepfle.de header.s=strato-dkim-0002 header.b=tK4uQtyX; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v21-v6si8397055plo.397.2018.09.07.08.51.36; Fri, 07 Sep 2018 08:51:52 -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=fail header.i=@aepfle.de header.s=strato-dkim-0002 header.b=tK4uQtyX; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728897AbeIGTMx (ORCPT + 99 others); Fri, 7 Sep 2018 15:12:53 -0400 Received: from mo4-p01-ob.smtp.rzone.de ([81.169.146.165]:19322 "EHLO mo4-p01-ob.smtp.rzone.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727659AbeIGTMx (ORCPT ); Fri, 7 Sep 2018 15:12:53 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1536330699; s=strato-dkim-0002; d=aepfle.de; h=Message-Id:Date:Subject:Cc:To:From:X-RZG-CLASS-ID:X-RZG-AUTH:From: Subject:Sender; bh=hhryv7i+6xW0/275E5fq0ZyomRtnBZUYgjT4BEdEMZE=; b=tK4uQtyXwRV3PUkfJwACCkMngkrFFBkLynZz+nXXZ/Hsto89p+D8+gYg67g6GdH4Pq G1og1JY/4G+yAwdiIu7zvs/xxAZltW49o5QdReEVGfVG+13O9qUXDvyxulRTaHuJ/SK5 aG1f9A+ZcB815xw+u19ZTyR6bidyE7vxkI4CNYKz7ot/0fkDPiU+Ha7jSiv2m2W7PD5M nSF8pUEPU/8BkfTs6XBKOX8NUSA4dKRVar16HMxCfFGSJs8X27p/8bd124fvbfNCrfhE 09KFAHTtHHbjuOwYeIzdmrx575jKE0got/CZzCrRJ8um5j2QAM+vunHfKjOXrLR1Wmg4 cvTg== X-RZG-AUTH: ":P2EQZWCpfu+qG7CngxMFH1J+3q8wa/QXkBR9MXjAuzpIG0mv9coXAgc0/lb4QMstw0mqi/C51hpW50R4vli8caicjAbM" X-RZG-CLASS-ID: mo00 Received: from sender by smtp.strato.de (RZmta 44.0 AUTH) with ESMTPSA id L0862bu87EVcFXy (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (curve secp521r1 with 521 ECDH bits, eq. 15360 bits RSA)) (Client did not present a certificate); Fri, 7 Sep 2018 16:31:38 +0200 (CEST) From: Olaf Hering To: xen-devel@lists.xenproject.org Cc: Olaf Hering , Boris Ostrovsky , Juergen Gross , linux-kernel@vger.kernel.org (open list) Subject: [PATCH v4] xen: avoid crash in disable_hotplug_cpu Date: Fri, 7 Sep 2018 16:31:35 +0200 Message-Id: <20180907143135.29608-1-olaf@aepfle.de> X-Mailer: git-send-email 2.16.4 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The command 'xl vcpu-set 0 0', issued in dom0, will crash dom0: BUG: unable to handle kernel NULL pointer dereference at 00000000000002d8 PGD 0 P4D 0 Oops: 0000 [#1] PREEMPT SMP NOPTI CPU: 7 PID: 65 Comm: xenwatch Not tainted 4.19.0-rc2-1.ga9462db-default #1 openSUSE Tumbleweed (unreleased) Hardware name: Intel Corporation S5520UR/S5520UR, BIOS S5500.86B.01.00.0050.050620101605 05/06/2010 RIP: e030:device_offline+0x9/0xb0 Code: 77 24 00 e9 ce fe ff ff 48 8b 13 e9 68 ff ff ff 48 8b 13 e9 29 ff ff ff 48 8b 13 e9 ea fe ff ff 90 66 66 66 66 90 41 54 55 53 87 d8 02 00 00 01 0f 85 88 00 00 00 48 c7 c2 20 09 60 81 31 f6 RSP: e02b:ffffc90040f27e80 EFLAGS: 00010203 RAX: 0000000000000000 RBX: 0000000000000000 RCX: 0000000000000000 RDX: ffff8801f3800000 RSI: ffffc90040f27e70 RDI: 0000000000000000 RBP: 0000000000000000 R08: ffffffff820e47b3 R09: 0000000000000000 R10: 0000000000007ff0 R11: 0000000000000000 R12: ffffffff822e6d30 R13: dead000000000200 R14: dead000000000100 R15: ffffffff8158b4e0 FS: 00007ffa595158c0(0000) GS:ffff8801f39c0000(0000) knlGS:0000000000000000 CS: e033 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 00000000000002d8 CR3: 00000001d9602000 CR4: 0000000000002660 Call Trace: handle_vcpu_hotplug_event+0xb5/0xc0 xenwatch_thread+0x80/0x140 ? wait_woken+0x80/0x80 kthread+0x112/0x130 ? kthread_create_worker_on_cpu+0x40/0x40 ret_from_fork+0x3a/0x50 This happens because handle_vcpu_hotplug_event is called twice. In the first iteration cpu_present is still true, in the second iteration cpu_present is false which causes get_cpu_device to return NULL. In case of cpu#0, cpu_online is apparently always true. Fix this crash by checking if the cpu can be hotplugged, which is false for a cpu that was just removed. Also check if the cpu was actually offlined by device_remove, otherwise leave the cpu_present state as it is. Rearrange to code to do all work with device_hotplug_lock held. Signed-off-by: Olaf Hering --- drivers/xen/cpu_hotplug.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/drivers/xen/cpu_hotplug.c b/drivers/xen/cpu_hotplug.c index d4265c8ebb22..b1357aa4bc55 100644 --- a/drivers/xen/cpu_hotplug.c +++ b/drivers/xen/cpu_hotplug.c @@ -19,15 +19,16 @@ static void enable_hotplug_cpu(int cpu) static void disable_hotplug_cpu(int cpu) { - if (cpu_online(cpu)) { - lock_device_hotplug(); + if (!cpu_is_hotpluggable(cpu)) + return; + lock_device_hotplug(); + if (cpu_online(cpu)) device_offline(get_cpu_device(cpu)); - unlock_device_hotplug(); - } - if (cpu_present(cpu)) + if (!cpu_online(cpu) && cpu_present(cpu)) { xen_arch_unregister_cpu(cpu); - - set_cpu_present(cpu, false); + set_cpu_present(cpu, false); + } + unlock_device_hotplug(); } static int vcpu_online(unsigned int cpu)