From patchwork Tue Sep 18 11:48:22 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 10604227 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 9FAC66CB for ; Tue, 18 Sep 2018 11:49:11 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8FD0B2A6F6 for ; Tue, 18 Sep 2018 11:49:11 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 83DA52A704; Tue, 18 Sep 2018 11:49:11 +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 119C82A6F6 for ; Tue, 18 Sep 2018 11:49:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 986268E0009; Tue, 18 Sep 2018 07:49:09 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 90B1C8E0001; Tue, 18 Sep 2018 07:49:09 -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 7AB2F8E0009; Tue, 18 Sep 2018 07:49:09 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-qt0-f198.google.com (mail-qt0-f198.google.com [209.85.216.198]) by kanga.kvack.org (Postfix) with ESMTP id 4A53D8E0001 for ; Tue, 18 Sep 2018 07:49:09 -0400 (EDT) Received: by mail-qt0-f198.google.com with SMTP id z44-v6so1127398qtg.5 for ; Tue, 18 Sep 2018 04:49:09 -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=mxKBX7msMlKovsOh7TxuiR1dmSGE2iQ2lKIYf05t38o=; b=OQwxgrM8ysZj20tm8VDwiSt7oHUGu8+TxxNRqe3pK9kHcFN+DdXRH9TUvWAEP9YZIY m+YWWwFilYi4CHaOCjtEIs6IjlJDBpG4YVMHhYhKFpAjj9S089EhK/4rWRossArpMA9O lyUuEoFw7LQf8NqBNgJp7HaVY+Jw9uPBiICNzxHkHQdm0laabf8Zf626NwIPZLmW2Srb vJqzQ0TvlmzIag52GVHV2NOqQbBVUOVAOtRiqxJzBctuMAB0/Qgdqx04yls3UgKleWnZ Ehp4mDnK5kx8DLbO1sHruJekclyWo7nMzdkwc0a/421Up9Sysmkgi6PPSMT71p5LhG+P HOZg== 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: APzg51Dh3/tOy5wnXcVIg1rEFwddk5t5fNAGrNars9aPjRIyY/2tMc3S wx7AOxkzN7ar0W9L4peKvBHjyr8DnQbUc0As71ElTiixGWt5vfs3eNM1fngeJVJT+4kjefFnjXH lG3yml08rFWYsyE/uZZZ+KBOgghVHrvNFhmLSbqEuQ7yKmqcoHpatB9/QYRbFbJRexg== X-Received: by 2002:a0c:8ab0:: with SMTP id 45-v6mr21278406qvv.49.1537271349065; Tue, 18 Sep 2018 04:49:09 -0700 (PDT) X-Google-Smtp-Source: ANB0VdYqzVhI4Rnt+nM91C6I1l6i9WYhEFIeN2UJf6+8w9i0EPEoywE76jC96TTZnll7BtvromA6 X-Received: by 2002:a0c:8ab0:: with SMTP id 45-v6mr21278367qvv.49.1537271348168; Tue, 18 Sep 2018 04:49:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537271348; cv=none; d=google.com; s=arc-20160816; b=yCpKYtD4cPL4P1m2ZSMvOW3ixE5PtS1HjjnmFvaycZ9C/gIrxuEVECL7rLzOPENtpH 19ZEwli6/MUUo7IJrUz9qZwmcQw5bjj2uZWek2ke3VIq7Q5FfPrCmI8tTupkwDHTua4t ru1A/vhFzR/TxaF394f6fVkIsOtH3tIx05HJigjD3O3488XdMsHyPiAKUsOSO2kg2aLz RxSwXSjIHZ/nIB01zlTujWD2zeIIkGxAltYAMGn49mqcc2dVKg1uocu5GKU9qSm9y3Ae taDr+xetvaiZGzrYBD8WHlOkI4380FZqLNaxi3C0dsmYISTOaT6cvAQjAkEDurIWGl3c 8Rsw== 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=mxKBX7msMlKovsOh7TxuiR1dmSGE2iQ2lKIYf05t38o=; b=EZCR2aNr/bcE51ngrG40cm2o0BcNR3rHt9EeUcbgrF3SRB2TB4yD2nYRn3WMCMR0pY 18kD20ArGfQyEOC/RjjcqH8hGnNmSJTGDBMKSSPdVPZzc28KaAU2R6cxYueUy6u7vsyx Z8wKJfCV9Gtxabh510YgAwMmea/0ZaXYAGw/MhdOSsK9wXLOIS3ERZ6kwIipuWS5xzo/ 29L/+q+kS+GR88yOKtZ1+TfsnWKRxC2vKbM7uuTlB/wWF1fiolBu3ZlLLM6OYHJu3N2R sFh5LVdeCAYFjqur/5EpVfmCogcLuBr1i/2SA86DPWcIv+cH02Vm7Qloe3bMCRo+Gers +Dvw== 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 41-v6si1710832qtr.28.2018.09.18.04.49.07 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 18 Sep 2018 04:49:08 -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-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 21F7B80E7E; Tue, 18 Sep 2018 11:49:07 +0000 (UTC) Received: from t460s.redhat.com (ovpn-116-248.ams2.redhat.com [10.36.116.248]) by smtp.corp.redhat.com (Postfix) with ESMTP id B43B417101; Tue, 18 Sep 2018 11:49:04 +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 v1 6/6] memory-hotplug.txt: Add some details about locking internals Date: Tue, 18 Sep 2018 13:48:22 +0200 Message-Id: <20180918114822.21926-7-david@redhat.com> In-Reply-To: <20180918114822.21926-1-david@redhat.com> References: <20180918114822.21926-1-david@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Tue, 18 Sep 2018 11:49:07 +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 Signed-off-by: David Hildenbrand --- Documentation/memory-hotplug.txt | 39 +++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/Documentation/memory-hotplug.txt b/Documentation/memory-hotplug.txt index 7f49ebf3ddb2..03aaad7d7373 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,43 @@ 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 to +serialise memory hotplug (e.g. access to global/zone variables). + +In addition, mem_hotplug_lock (in contrast to device_hotplug_lock) allows +for a quite efficient get_online_mems/put_online_mems implementation. + + Future Work ===========