From patchwork Thu Sep 27 09:25:54 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 10617715 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 2A15614BD for ; Thu, 27 Sep 2018 09:26:48 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1AA132AFF0 for ; Thu, 27 Sep 2018 09:26:48 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0E6792AFFB; Thu, 27 Sep 2018 09:26:48 +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=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 737542AFF0 for ; Thu, 27 Sep 2018 09:26:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 847D58E0008; Thu, 27 Sep 2018 05:26:45 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 81D508E0001; Thu, 27 Sep 2018 05:26:45 -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 6C41F8E0009; Thu, 27 Sep 2018 05:26:45 -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 3B6748E0008 for ; Thu, 27 Sep 2018 05:26:45 -0400 (EDT) Received: by mail-qt1-f198.google.com with SMTP id b12-v6so1591243qtp.16 for ; Thu, 27 Sep 2018 02:26:45 -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=Rd2I9G2Ic96FJbyZT2bUkX3kp820Ave9MgHnT+kuWYFaZ9lew6QlL8DTMraEnrsTEy WrS+FEYT4gNGRTaO/29VF7SSkJKmlobrdPLXF73LT9UYfPBdd1BnEbj3w0yB4D9jF7dd 58XO8B1NPSnMFyX4MnjpTp41axWdrjc0YM/PQ3Hl6bCaO5D7vA9nyaEJQ2i8YQzg5L6M V9oN3yXfhrJPXFH80K3zIEtKLgExqY1SgQpML3/UIUJyjlJI47+HFIay8qdfIz26GacL 6ImSEQyKLXxCGyl89pBC1KxUOeiCvKM2ryB3Z5d+FQnFoA7aLeyYwEFo2q4t7AIs/ufF 66Gw== 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: ABuFfoittoHOXoeMVlTg9RKfmF4W2ws8Oj/v9S68G6W1qRiPJzGuaOUl 6RQlW/3itowmkKIXmTu1jjD9L+OJxt0+KKi9oJ7YPJGbzgINb1Fn44F0crpX4gKu/VFeGfZ8q1j MvwB044cJt6sffNhDptsiLZt5nqnj/VD6+kZtUHQElNd5tYDaB/buMp5NL8QJGtXWHA== X-Received: by 2002:a0c:b88f:: with SMTP id y15-v6mr7267140qvf.203.1538040404998; Thu, 27 Sep 2018 02:26:44 -0700 (PDT) X-Google-Smtp-Source: ACcGV61vS7QreCR83itm/wcynIGrSRmyesw0N8/VZbJvMe9Z2UGn36Szwmoa1GNqDfSWwtrQapkt X-Received: by 2002:a0c:b88f:: with SMTP id y15-v6mr7267123qvf.203.1538040404514; Thu, 27 Sep 2018 02:26:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1538040404; cv=none; d=google.com; s=arc-20160816; b=LCF+VkpqNbFRxdG/9FAQnGOPi1JwOmJnLvk77SC2nX59u8gZzIWp370bXP82+E4v+C j6Icg7gdcIdPZBbzCN34DFL8mQ/CRJ3vCMtweaAPqspet/gde9yKiI9/1v0SfdtOjsxO zwpacNiiX8FqA0inEzVCsEwiNT2YIMw/MtR186i0SmUJqHGE6b3BmF4U06PV5c9k4EsL iMr+vFQrplakY1LhT36u/B9ccaSEjAzD6NRUGfGzXx/Cf2syDI3+fYU1xI3EQL0n6rjB Z6SikqS4HoOkfjON+OILwbQ7SuY6tXHvBP6ZpSVhEzqVWPP5PHBQhpN08yM/32Ds4F0k Y9mA== 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=JrEkLg6vczKlXKmfOZuSuCqGZ72VbdAPgN74eSVT2Tc8WEcKzxdD8lNcBAbvv9ElK2 3G15EOk3S4Ei6xTRd8J4M6arGJlFrkKxMl0kANBV6pkUpOXyMmqoJQSIpU1s2k9rEWIg Oa2bQb8QzdriMgq6KGP6GBds+yRjqF6fO5cPX2ZQS6zKpHRwtUEZL2r8Fl79Acqiw8+j ts5f+MR8+FIKc1x0OSqMnO6SWRZYGkBhuICw2Mh2UsY5qiDRN4CVPOBo8/PxW7gpD2kh dBUr868rlNEoHF3btjYSXibCo5T1RPwFwjdOL/rqTWypgVYRWX41+fhG3ehpS9aFVQiP FfOg== 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 d1-v6si919600qtl.321.2018.09.27.02.26.44 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 27 Sep 2018 02:26:44 -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-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.25]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 939AAA6E04; Thu, 27 Sep 2018 09:26:43 +0000 (UTC) Received: from t460s.redhat.com (ovpn-116-205.ams2.redhat.com [10.36.116.205]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3D8C62015AD1; Thu, 27 Sep 2018 09:26:41 +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 v3 6/6] memory-hotplug.txt: Add some details about locking internals Date: Thu, 27 Sep 2018 11:25:54 +0200 Message-Id: <20180927092554.13567-7-david@redhat.com> In-Reply-To: <20180927092554.13567-1-david@redhat.com> References: <20180927092554.13567-1-david@redhat.com> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.25 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Thu, 27 Sep 2018 09:26:43 +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 Reviewed-by: Oscar Salvador --- 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 ===========