From patchwork Tue Sep 25 09:14:57 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 10613775 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 B57F2913 for ; Tue, 25 Sep 2018 09:15:59 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AB28A299D5 for ; Tue, 25 Sep 2018 09:15:59 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9F26929B3C; Tue, 25 Sep 2018 09:15:59 +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=-2.9 required=2.0 tests=BAYES_00,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 32C24299D5 for ; Tue, 25 Sep 2018 09:15:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id ACD2A8E0088; Tue, 25 Sep 2018 05:15:52 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id A51728E0072; Tue, 25 Sep 2018 05:15:52 -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 8F4598E0088; Tue, 25 Sep 2018 05:15:52 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by kanga.kvack.org (Postfix) with ESMTP id 62CAD8E0072 for ; Tue, 25 Sep 2018 05:15:52 -0400 (EDT) Received: by mail-qt1-f198.google.com with SMTP id c14-v6so7974012qtc.7 for ; Tue, 25 Sep 2018 02:15:52 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references; bh=Q+AWfAQyFh1eB4temGrJ9RcRxkI6tEPl3FFKc4he+h8=; b=RWPBj/SYHO78p/poCyjaqcbVnKEt/fqUHP4ey+mzWJAvuvCQ28JHeMugcuKa8qkNXL 88LJxKWt4ZP0VUN2ciHsv2t7xt7fIs8C4itIcwDjQTkvkf8/JPs1kN9DR2GS+59dson2 UhACVrSXysyQhGh90Sl7ZitFIf4mxl0wz3vr2F9b6/sB52Tlru6QDk1ASMRHI31/Govd x8uk50O+SIbvrCFMZmPNTcWP02vv0sC4PO0qRDqQzNE0dc+1Q8ZYwqVT+I5KAGWP2Hot LmL9Zt6ODtSqah7amCFLA4fhYcmO1pOeFipTq0IvW9D33zrOySwINy4rsj1UjRnmfVcL 22Vg== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of david@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Gm-Message-State: ABuFfojqOpGsXoPyk6abgYcovkGra88gdzzoWEeZVDs4h6GIND9nHmWP OmVqqeZ8nbir1Y75/mKRWTWh1cJS1xvicNMBqZXVqwXIiPFyS1ngNd/D5/9HVgZVOm9n1jBTHw5 sPTseGBPAOC6kSiQk1Lz7+CDubq6fYUIIEAi4Zq68NrPaj5LQts0FGv73sWwifVzkVA== X-Received: by 2002:ae9:e314:: with SMTP id v20-v6mr58198qkf.69.1537866952157; Tue, 25 Sep 2018 02:15:52 -0700 (PDT) X-Google-Smtp-Source: ACcGV605KPadauPRPl2qA42QYJa20uocQ+Ih77mW+Ffc2Pswwa1WybhdrWwZj2pM7RegGBPoWDi2 X-Received: by 2002:ae9:e314:: with SMTP id v20-v6mr58179qkf.69.1537866951615; Tue, 25 Sep 2018 02:15:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537866951; cv=none; d=google.com; s=arc-20160816; b=CCdXfT19w7ouXuPdDThWq8bw7dmDzG8eNirivy/11D3qkQ3pWFqAjDc9Xw5Z832nGP Afg/xc/4DJCTL/3unOCCNPAgAggZuBS29nQCcsD21fbj8Yuf+hZy9DXWU/6PAedjINcG HFvPovV+ZIEk9ZbRZPoLo7ByE7oehtPqi6i7hDCofad78ygshjYADRBLvs5TaGH7Teq5 ljN1WLtMnNIkiku6DjXGzWLor/sGvN4SavVsu3CARuGrWLyDpNstU37kCgiArGnpg+Af BJsvULAtwdQkx5JKPL5zIwqf+wmuNEwviW1Umd4ji0wunV0PeDhgKD5M4b2ttUo0P17d KX7A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from; bh=Q+AWfAQyFh1eB4temGrJ9RcRxkI6tEPl3FFKc4he+h8=; b=O758zSN6evifY4jbgddxEP19+G6UTtJ2PTi9wH8YeKwkEltpegj1BO4KH4pkAFqTmr 7WhyYj5ma85QoMr6xn/qjgmnVGX5Q7KZz8h5MgUTpxBxXUa+GvtNfl3NkaMwiOwjNulB 9KqCCVeadM7N6br1WcDeD9HKk11qDcykoNEHYnfA1Yk8boUnl7zfSrRlTZSnw7+8Fq+w 1kOpEiRpbZovd9YtlDkdMvp9KUfIfor0nyiVqotA7H+8w2E4yGpg1lu15ayfLmnb3BAo 2pGCXFWu+rwTcFERX3vErqipOHVwFf0Rf4MjVTxm0G5SIpAYtvwggxVLzSkfgmhaSuqg 9cHg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of david@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from mx1.redhat.com (mx1.redhat.com. [209.132.183.28]) by mx.google.com with ESMTPS id m18-v6si1297488qka.155.2018.09.25.02.15.51 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 25 Sep 2018 02:15:51 -0700 (PDT) Received-SPF: pass (google.com: domain of david@redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; Authentication-Results: mx.google.com; spf=pass (google.com: domain of david@redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.24]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B32C4308213F; Tue, 25 Sep 2018 09:15:50 +0000 (UTC) Received: from t460s.redhat.com (ovpn-117-161.ams2.redhat.com [10.36.117.161]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5B832308BDBA; Tue, 25 Sep 2018 09:15:48 +0000 (UTC) From: David Hildenbrand To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-acpi@vger.kernel.org, xen-devel@lists.xenproject.org, devel@linuxdriverproject.org, David Hildenbrand , Jonathan Corbet , Michal Hocko , Andrew Morton Subject: [PATCH v2 6/6] memory-hotplug.txt: Add some details about locking internals Date: Tue, 25 Sep 2018 11:14:57 +0200 Message-Id: <20180925091457.28651-7-david@redhat.com> In-Reply-To: <20180925091457.28651-1-david@redhat.com> References: <20180925091457.28651-1-david@redhat.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.24 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.42]); Tue, 25 Sep 2018 09:15:50 +0000 (UTC) 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 Let's document the magic a bit, especially why device_hotplug_lock is required when adding/removing memory and how it all play together with requests to online/offline memory from user space. Cc: Jonathan Corbet Cc: Michal Hocko Cc: Andrew Morton Reviewed-by: Pavel Tatashin Reviewed-by: Rashmica Gupta Signed-off-by: David Hildenbrand --- Documentation/memory-hotplug.txt | 42 +++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/Documentation/memory-hotplug.txt b/Documentation/memory-hotplug.txt index 7f49ebf3ddb2..ce4faa5530fa 100644 --- a/Documentation/memory-hotplug.txt +++ b/Documentation/memory-hotplug.txt @@ -3,7 +3,7 @@ Memory Hotplug ============== :Created: Jul 28 2007 -:Updated: Add description of notifier of memory hotplug: Oct 11 2007 +:Updated: Add some details about locking internals: Aug 20 2018 This document is about memory hotplug including how-to-use and current status. Because Memory Hotplug is still under development, contents of this text will @@ -495,6 +495,46 @@ further processing of the notification queue. NOTIFY_STOP stops further processing of the notification queue. + +Locking Internals +================= + +When adding/removing memory that uses memory block devices (i.e. ordinary RAM), +the device_hotplug_lock should be held to: + +- synchronize against online/offline requests (e.g. via sysfs). This way, memory + block devices can only be accessed (.online/.state attributes) by user + space once memory has been fully added. And when removing memory, we + know nobody is in critical sections. +- synchronize against CPU hotplug and similar (e.g. relevant for ACPI and PPC) + +Especially, there is a possible lock inversion that is avoided using +device_hotplug_lock when adding memory and user space tries to online that +memory faster than expected: + +- device_online() will first take the device_lock(), followed by + mem_hotplug_lock +- add_memory_resource() will first take the mem_hotplug_lock, followed by + the device_lock() (while creating the devices, during bus_add_device()). + +As the device is visible to user space before taking the device_lock(), this +can result in a lock inversion. + +onlining/offlining of memory should be done via device_online()/ +device_offline() - to make sure it is properly synchronized to actions +via sysfs. Holding device_hotplug_lock is advised (to e.g. protect online_type) + +When adding/removing/onlining/offlining memory or adding/removing +heterogeneous/device memory, we should always hold the mem_hotplug_lock in +write mode to serialise memory hotplug (e.g. access to global/zone +variables). + +In addition, mem_hotplug_lock (in contrast to device_hotplug_lock) in read +mode allows for a quite efficient get_online_mems/put_online_mems +implementation, so code accessing memory can protect from that memory +vanishing. + + Future Work ===========