From patchwork Sun Apr 21 01:44:28 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pasha Tatashin X-Patchwork-Id: 10910443 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8EE5A1515 for ; Sun, 21 Apr 2019 01:44:39 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 80AB028950 for ; Sun, 21 Apr 2019 01:44:39 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 747DA28965; Sun, 21 Apr 2019 01:44:39 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0FEC228950 for ; Sun, 21 Apr 2019 01:44:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9B0A56B0006; Sat, 20 Apr 2019 21:44:35 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 960B86B0007; Sat, 20 Apr 2019 21:44:35 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7DC526B0008; Sat, 20 Apr 2019 21:44:35 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-qt1-f200.google.com (mail-qt1-f200.google.com [209.85.160.200]) by kanga.kvack.org (Postfix) with ESMTP id 582CE6B0006 for ; Sat, 20 Apr 2019 21:44:35 -0400 (EDT) Received: by mail-qt1-f200.google.com with SMTP id b1so8345677qtk.11 for ; Sat, 20 Apr 2019 18:44:35 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:from:to:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=CYN5iyU5BivsuLuaj0p6OjCQw7Id7vPwrKGL/YIcXb0=; b=KNYiHrLaIyWG9Z/8JFZQ3PiWCe04q+IC0EmwvEm+ySMP4R/CjEKrDVBM7HNg4/8QUV TLn0pWE1gKS3knWVqcWC816vvX7ONk6NTPs2lhiBc85jkpXecLsgO8RFHqgD299VWi58 tMTty6ZAF3hLnLzwgmMQ18L8mNJRqV37FgF3xuSJy8j38ZGzpZR2+6LwY09lMoNoBX/c tG+iZielFA1irwzQewE8Ow9RT6MFL3gvPqirn7wHzg5/kmI7te9OvxBl0kRZVapTqIVp QqOc4IdyL+bzQZHa8M6NNfOA1gfPcK5cXGd1chcbGP8H42dfhvWXJqq4cZ+ysO7pWDc2 UHlA== X-Gm-Message-State: APjAAAWFOGWUmGKbDwy0prnlRMfzl4OInoM83eGz4UNBrAxMm8nPJevO qW+GbQMcaiyKw2yXX0BV1BsFuK3U7ZvvkfZp1MN7cR/tvP320bfYtK7aKJyoj1JILdZXtmBLOaJ whED1WFZHiTXWUD0w0scN/OjRkbrKjegs9U4u936BkfKNnFAMnCzEnWw3MuLd49kGaw== X-Received: by 2002:a0c:8738:: with SMTP id 53mr10151513qvh.210.1555811075147; Sat, 20 Apr 2019 18:44:35 -0700 (PDT) X-Received: by 2002:a0c:8738:: with SMTP id 53mr10151481qvh.210.1555811074261; Sat, 20 Apr 2019 18:44:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555811074; cv=none; d=google.com; s=arc-20160816; b=DFppvLYU6mgraWZp1Ua3d6H+d/zUObnCqau9azASFziOMKUZysHPdl/TSHKSezVdox EprWWErizQrhQX30XieuDbPL2l4y/2mzzXfNLxfkpe8KQJXS3B9zJXwa4ounbuH7GBsv 5mOi7EUvC+ewFUCllrV+BRTZmgv3ip6M0icErotLkBpbVxk4Zmnib2X7K8fAiVf/LTMJ nG2M+8Ig6j1mL4F54hZwjQB+4FsbJvQz/d37N5puGbiVgvJSUKTpNLrfUmJrlBfnH/ro 3/ZMgAYjuM8rKXu4Uox6aNcGf5bjfpmI0DPRgPGZ6eK+KSuFbbc+n5nX13DWAV+9FFBt IoOA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:dkim-signature; bh=CYN5iyU5BivsuLuaj0p6OjCQw7Id7vPwrKGL/YIcXb0=; b=iFORQ5WVWEH/l33M49OZ6DTGHbUZmyB1/BragLy4VtBscvMKNkhL3+5PbtB1O4HcCh oqgDXnKdlaoMBR80T0TrH9nNDqumBK/urSGbeEOr23T3cICkTt22q+6vl7QWQ87lz4ZI yZPJK7ao/MOkGKFJGRs2Qmz+x4CteaoHnpvx5r59dEy+duvqAEIaPJBo9PHwNYNnfgBS Gq9LFijO5O4Rps8/rVzAP7WSoh8h28dxA+4Ic8t9wF83+SLdWf5moSbvuOO5L/qTkGmK F/ZzHO5VgEBQqfdwQ76yrf/0bGnrSAKYJqWILaQibXRXeje1p6ESzeKihaA/F7VzkTxs p/JA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@soleen.com header.s=google header.b=Lx4n3eeQ; spf=pass (google.com: domain of pasha.tatashin@soleen.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=pasha.tatashin@soleen.com Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id f20sor7941451qvd.19.2019.04.20.18.44.34 for (Google Transport Security); Sat, 20 Apr 2019 18:44:34 -0700 (PDT) Received-SPF: pass (google.com: domain of pasha.tatashin@soleen.com designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@soleen.com header.s=google header.b=Lx4n3eeQ; spf=pass (google.com: domain of pasha.tatashin@soleen.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=pasha.tatashin@soleen.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=soleen.com; s=google; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=CYN5iyU5BivsuLuaj0p6OjCQw7Id7vPwrKGL/YIcXb0=; b=Lx4n3eeQsd3DKBZFt1X+n0tHyvdxX9eMXlbGkCZXa5jPDl+R+bxvhnePCzuiVFH+jk di+Mp6Mfiv5tBZu3YjQsIuC9JNhQV5AoV/J9rAuT1n2Pzo9ukHrKDSQHkIHF92Mqzxql YvxHUk9CCjLHPwU2JfPC6Jb4K3eYzQaEOCweZDDMP3zNxsjvacM4Si4fbi0xJj8A+ZHM HRrrp8lKjP5UFpftK/lphzl+xQk7hG0nsmdRNOw9TAxyhOTDr3q5eE7aOoZRIevE2SJ1 IyWD1DfUU79PI1VB99cVVKjDHJ6TbNTIQlFTSgKgHQNPdqcPC2df8BsTeVhpEsSjlzIL cz1g== X-Google-Smtp-Source: APXvYqyGwfhqvlrszPLC8qN9m2XQDrZ+wKGZWzLF7GM7scRLtBDlMXL92I0fVQaBdczXZtyMg5jLVQ== X-Received: by 2002:ad4:430c:: with SMTP id c12mr10017920qvs.109.1555811074063; Sat, 20 Apr 2019 18:44:34 -0700 (PDT) Received: from localhost.localdomain (c-73-69-118-222.hsd1.nh.comcast.net. [73.69.118.222]) by smtp.gmail.com with ESMTPSA id u1sm1385218qtj.50.2019.04.20.18.44.32 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 20 Apr 2019 18:44:33 -0700 (PDT) From: Pavel Tatashin To: pasha.tatashin@soleen.com, jmorris@namei.org, sashal@kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-nvdimm@lists.01.org, akpm@linux-foundation.org, mhocko@suse.com, dave.hansen@linux.intel.com, dan.j.williams@intel.com, keith.busch@intel.com, vishal.l.verma@intel.com, dave.jiang@intel.com, zwisler@kernel.org, thomas.lendacky@amd.com, ying.huang@intel.com, fengguang.wu@intel.com, bp@suse.de, bhelgaas@google.com, baiyaowei@cmss.chinamobile.com, tiwai@suse.de, jglisse@redhat.com Subject: [v2 1/2] device-dax: fix memory and resource leak if hotplug fails Date: Sat, 20 Apr 2019 21:44:28 -0400 Message-Id: <20190421014429.31206-2-pasha.tatashin@soleen.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190421014429.31206-1-pasha.tatashin@soleen.com> References: <20190421014429.31206-1-pasha.tatashin@soleen.com> MIME-Version: 1.0 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: X-Virus-Scanned: ClamAV using ClamSMTP When add_memory() function fails, the resource and the memory should be freed. Fixes: c221c0b0308f ("device-dax: "Hotplug" persistent memory for use like normal RAM") Signed-off-by: Pavel Tatashin --- drivers/dax/kmem.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/drivers/dax/kmem.c b/drivers/dax/kmem.c index a02318c6d28a..4c0131857133 100644 --- a/drivers/dax/kmem.c +++ b/drivers/dax/kmem.c @@ -66,8 +66,11 @@ int dev_dax_kmem_probe(struct device *dev) new_res->name = dev_name(dev); rc = add_memory(numa_node, new_res->start, resource_size(new_res)); - if (rc) + if (rc) { + release_resource(new_res); + kfree(new_res); return rc; + } return 0; } From patchwork Sun Apr 21 01:44:29 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pasha Tatashin X-Patchwork-Id: 10910445 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id F21091515 for ; Sun, 21 Apr 2019 01:44:41 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E312228950 for ; Sun, 21 Apr 2019 01:44:41 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D6B8B28965; Sun, 21 Apr 2019 01:44:41 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 50ABC28950 for ; Sun, 21 Apr 2019 01:44:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E93F56B0008; Sat, 20 Apr 2019 21:44:37 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id E46D06B000A; Sat, 20 Apr 2019 21:44:37 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D31756B000C; Sat, 20 Apr 2019 21:44:37 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-qt1-f200.google.com (mail-qt1-f200.google.com [209.85.160.200]) by kanga.kvack.org (Postfix) with ESMTP id B541D6B0008 for ; Sat, 20 Apr 2019 21:44:37 -0400 (EDT) Received: by mail-qt1-f200.google.com with SMTP id o34so8396651qte.5 for ; Sat, 20 Apr 2019 18:44:37 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:from:to:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=KL328kJp7uYop/CaXZcmGRKfoBkXK0v1G+nukTp5itg=; b=XimIRWqOjAbFTXxpuAQDMSUWE0VSgDT1FLPyCZt6nt5sqr09DcQ9TWGZ3EP6m66A16 PNUorDf6T9DBtBs+QQvXJ5qkJgSlz6h+qmyF/EJwezOBxrFj9fxB2XcSoR9jYSiFfCUo kUO4eO8396iEGmEe1atFhbNJ0pjp6V21qz7m92sz391ogUdRXlyBTdFbY79XFI70grFX vf4QMpgd/HPW+wkBHKBm2/AF3owa+sZS/cIrBb8yWJWCqRav+sFUpbbUeUPMmQcPiMOc utBog76zyq1Fw4y3xEwjhgxThlr4G/QDCr0VdMvWewiL//oY4vo3wLWUFDY9aUTYdsm6 g05w== X-Gm-Message-State: APjAAAWbnFUb3FxEH1YT0B2nhoX99xQuFQvp7kRonvnB8aOkCuBXcgyn DExJmEjSm6uzG3aLpNwx8kQ5aToICehRWHGXqotQTIK/hSyrsU/oQC/iN4JMyoGb+OWn3Qcd38/ p++A98YW28CNKFmMmZP8egN/LiZhs6ysLkmaoMyRkrjgouXVj+9afO3ZsYy4IzUBvHw== X-Received: by 2002:a37:5ac4:: with SMTP id o187mr9621544qkb.356.1555811077465; Sat, 20 Apr 2019 18:44:37 -0700 (PDT) X-Received: by 2002:a37:5ac4:: with SMTP id o187mr9621501qkb.356.1555811076083; Sat, 20 Apr 2019 18:44:36 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555811076; cv=none; d=google.com; s=arc-20160816; b=GwndR9z4AANqaNYAM2KlgV9+OaVOgkFJV0Lo60y0oJrLPugE0yZJ/WLWeNXj7Hro0p mIMKsIXrRBmCI2unDpiW8gSTGo7JquHB13QcO2pUQx1FH2403G4c3xNiGsSXtEwMoD0V ORenKBNHu90Ku3kdp0Hz/F1mva7wbSbG9+G8v4nT4DcodKb3W1gDOlydsjjkbZqSBIxL UUGjLNrYM5e2WkxC3YsL5hXkTozpWHsUyvN5HaSQ8XrrR/qiNX1HLGxU1eLIhHmBJt0r 7sFEK6DFnXBaVCJKDoBdXiqO4Y0A/KcOPa97z+9PPRdT3jHuVNNv6UaejSwbo5uMCI7P 8cRw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:dkim-signature; bh=KL328kJp7uYop/CaXZcmGRKfoBkXK0v1G+nukTp5itg=; b=caJAuZ5rD0xCv3UBjrrHJ3JZ/6UUTrrvnztz+zieNBsOwsMDCuKHfJRK6e/jFEc9mq WgogqzVktBF5gpk0lZsmsf72uSC6X1Y9Wy+CxySGece9tOdbTF9b5ih49vztKyWIGoAd qxFG71sqi6+jQ+7LjhGLWJf8n104IRddfoKY5Qsm1oDJoqS8WK7WhSN1qcnbpX3aGY4F Tz7CJ3lb1Xw8h6BEZdFMwa8P+1Kfi1w9OVRWIm6dtbYSXbzVbU5FWS7BmBB+CXcxeW2E A+s19UAfqceVoibi7dfDhZhinaA+crRkzd+KB9OGn2nnsxufM42hN5GWJ5D//e2+AAe+ 9x0g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@soleen.com header.s=google header.b="ZcOj6/tW"; spf=pass (google.com: domain of pasha.tatashin@soleen.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=pasha.tatashin@soleen.com Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id m4sor10901215qte.56.2019.04.20.18.44.35 for (Google Transport Security); Sat, 20 Apr 2019 18:44:36 -0700 (PDT) Received-SPF: pass (google.com: domain of pasha.tatashin@soleen.com designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@soleen.com header.s=google header.b="ZcOj6/tW"; spf=pass (google.com: domain of pasha.tatashin@soleen.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=pasha.tatashin@soleen.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=soleen.com; s=google; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=KL328kJp7uYop/CaXZcmGRKfoBkXK0v1G+nukTp5itg=; b=ZcOj6/tWBZki8VSTYWIx2hC0zATUWkFU1OTXUPmLys73YN2QUPVmWMkCAzisCIphb4 lagFcRl0UAa1+nKpZPFW6/ywAODOoC2ElJuMxdMZK+n/MVj6NLUv0dZbEJuUtuqfRbAG 2vQO7zgIlXc6foQWwNvVaXJ4cmOF6Nv1Mbrym5SdV+qbCVa1vCnbCbAIPeojIxSuiyzv alULIAAU7iGCTj9LS/VnbEzAzTbYgUAZW4UQycLNdEUtMMs7BATfDuCFjGJGPJtHwATa X1SGiIiiccfzYurY/yFrS0TMcM5+2hTPgZVtuHjsxwv960+2M26QfeQOg2ZSfR+OrX9P Nihw== X-Google-Smtp-Source: APXvYqwLrbFXWXl4ushQLhaJbuTDu4grdgvxRuAMcypo1JTsKGB8Q7Xxy+bv6W6dd9hWBd51O4WhFg== X-Received: by 2002:ac8:1776:: with SMTP id u51mr436168qtk.151.1555811075671; Sat, 20 Apr 2019 18:44:35 -0700 (PDT) Received: from localhost.localdomain (c-73-69-118-222.hsd1.nh.comcast.net. [73.69.118.222]) by smtp.gmail.com with ESMTPSA id u1sm1385218qtj.50.2019.04.20.18.44.34 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 20 Apr 2019 18:44:34 -0700 (PDT) From: Pavel Tatashin To: pasha.tatashin@soleen.com, jmorris@namei.org, sashal@kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-nvdimm@lists.01.org, akpm@linux-foundation.org, mhocko@suse.com, dave.hansen@linux.intel.com, dan.j.williams@intel.com, keith.busch@intel.com, vishal.l.verma@intel.com, dave.jiang@intel.com, zwisler@kernel.org, thomas.lendacky@amd.com, ying.huang@intel.com, fengguang.wu@intel.com, bp@suse.de, bhelgaas@google.com, baiyaowei@cmss.chinamobile.com, tiwai@suse.de, jglisse@redhat.com Subject: [v2 2/2] device-dax: "Hotremove" persistent memory that is used like normal RAM Date: Sat, 20 Apr 2019 21:44:29 -0400 Message-Id: <20190421014429.31206-3-pasha.tatashin@soleen.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190421014429.31206-1-pasha.tatashin@soleen.com> References: <20190421014429.31206-1-pasha.tatashin@soleen.com> MIME-Version: 1.0 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: X-Virus-Scanned: ClamAV using ClamSMTP It is now allowed to use persistent memory like a regular RAM, but currently there is no way to remove this memory until machine is rebooted. This work expands the functionality to also allows hotremoving previously hotplugged persistent memory, and recover the device for use for other purposes. To hotremove persistent memory, the management software must first offline all memory blocks of dax region, and than unbind it from device-dax/kmem driver. So, operations should look like this: echo offline > echo offline > /sys/devices/system/memory/memoryN/state ... echo dax0.0 > /sys/bus/dax/drivers/kmem/unbind Note: if unbind is done without offlining memory beforehand, it won't be possible to do dax0.0 hotremove, and dax's memory is going to be part of System RAM until reboot. Signed-off-by: Pavel Tatashin --- drivers/dax/dax-private.h | 2 + drivers/dax/kmem.c | 91 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 89 insertions(+), 4 deletions(-) diff --git a/drivers/dax/dax-private.h b/drivers/dax/dax-private.h index a45612148ca0..999aaf3a29b3 100644 --- a/drivers/dax/dax-private.h +++ b/drivers/dax/dax-private.h @@ -53,6 +53,7 @@ struct dax_region { * @pgmap - pgmap for memmap setup / lifetime (driver owned) * @ref: pgmap reference count (driver owned) * @cmp: @ref final put completion (driver owned) + * @dax_mem_res: physical address range of hotadded DAX memory */ struct dev_dax { struct dax_region *region; @@ -62,6 +63,7 @@ struct dev_dax { struct dev_pagemap pgmap; struct percpu_ref ref; struct completion cmp; + struct resource *dax_kmem_res; }; static inline struct dev_dax *to_dev_dax(struct device *dev) diff --git a/drivers/dax/kmem.c b/drivers/dax/kmem.c index 4c0131857133..d4896b281036 100644 --- a/drivers/dax/kmem.c +++ b/drivers/dax/kmem.c @@ -71,21 +71,104 @@ int dev_dax_kmem_probe(struct device *dev) kfree(new_res); return rc; } + dev_dax->dax_kmem_res = new_res; return 0; } +#ifdef CONFIG_MEMORY_HOTREMOVE +/* + * Check that device-dax's memory_blocks are offline. If a memory_block is not + * offline a warning is printed and an error is returned. dax hotremove can + * succeed only when every memory_block is offlined beforehand. + */ +static int +offline_memblock_cb(struct memory_block *mem, void *arg) +{ + struct device *mem_dev = &mem->dev; + bool is_offline; + + device_lock(mem_dev); + is_offline = mem_dev->offline; + device_unlock(mem_dev); + + if (!is_offline) { + struct device *dev = (struct device *)arg; + unsigned long spfn = section_nr_to_pfn(mem->start_section_nr); + unsigned long epfn = section_nr_to_pfn(mem->end_section_nr); + phys_addr_t spa = spfn << PAGE_SHIFT; + phys_addr_t epa = epfn << PAGE_SHIFT; + + dev_warn(dev, "memory block [%pa-%pa] is not offline\n", + &spa, &epa); + + return -EBUSY; + } + + return 0; +} + +static int dev_dax_kmem_remove(struct device *dev) +{ + struct dev_dax *dev_dax = to_dev_dax(dev); + struct resource *res = dev_dax->dax_kmem_res; + resource_size_t kmem_start; + resource_size_t kmem_size; + unsigned long start_pfn; + unsigned long end_pfn; + int rc; + + /* + * dax kmem resource does not exist, means memory was never hotplugged. + * So, nothing to do here. + */ + if (!res) + return 0; + + kmem_start = res->start; + kmem_size = resource_size(res); + start_pfn = kmem_start >> PAGE_SHIFT; + end_pfn = start_pfn + (kmem_size >> PAGE_SHIFT) - 1; + + /* + * Walk and check that every singe memory_block of dax region is + * offline + */ + lock_device_hotplug(); + rc = walk_memory_range(start_pfn, end_pfn, dev, offline_memblock_cb); + unlock_device_hotplug(); + + /* + * If admin has not offlined memory beforehand, we cannot hotremove dax. + * Unfortunately, because unbind will still succeed there is no way for + * user to hotremove dax after this. + */ + if (rc) + return rc; + + /* Hotremove memory, cannot fail because memory is already offlined */ + remove_memory(dev_dax->target_node, kmem_start, kmem_size); + + /* Release and free dax resources */ + release_resource(res); + kfree(res); + dev_dax->dax_kmem_res = NULL; + + return 0; +} +#else static int dev_dax_kmem_remove(struct device *dev) { /* - * Purposely leak the request_mem_region() for the device-dax - * range and return '0' to ->remove() attempts. The removal of - * the device from the driver always succeeds, but the region - * is permanently pinned as reserved by the unreleased + * Without hotremove purposely leak the request_mem_region() for the + * device-dax range and return '0' to ->remove() attempts. The removal + * of the device from the driver always succeeds, but the region is + * permanently pinned as reserved by the unreleased * request_mem_region(). */ return 0; } +#endif /* CONFIG_MEMORY_HOTREMOVE */ static struct dax_device_driver device_dax_kmem_driver = { .drv = {