From patchwork Tue Aug 21 10:44:18 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 10571369 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 4D5A8139B for ; Tue, 21 Aug 2018 10:45:00 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4511129DA1 for ; Tue, 21 Aug 2018 10:45:00 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3989429DA4; Tue, 21 Aug 2018 10:45:00 +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 5C4B729DA1 for ; Tue, 21 Aug 2018 10:44:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4D9F76B1E50; Tue, 21 Aug 2018 06:44:55 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 45DBC6B1E51; Tue, 21 Aug 2018 06:44:55 -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 2FD4A6B1E52; Tue, 21 Aug 2018 06:44:55 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-qk0-f199.google.com (mail-qk0-f199.google.com [209.85.220.199]) by kanga.kvack.org (Postfix) with ESMTP id 06A4B6B1E51 for ; Tue, 21 Aug 2018 06:44:55 -0400 (EDT) Received: by mail-qk0-f199.google.com with SMTP id w8-v6so17429851qkf.8 for ; Tue, 21 Aug 2018 03:44:55 -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=nnfVyljX6cxkryibLmAWL2uYypuiCLeIZVe8KdBz6Ns=; b=fdl2U9ZkzaQ+0yFsERXNjJfHurTVgdMLd0Zl+DQj6oOrwtba606dbZPUaT3+jAnR94 +IUaFNXzpuFc18xjCn35s1GZfKLjPFRkijSeE9SiMCfhyAgC1Ag/z6e9BKC6TRhiKhRV +JCNxG7KcTuCC/UjdW9W8Ua7aK8fO+6honzkVc4mNIJLP0PlBh7l/RE93ckP9J9f9nVG iJXevYzk8CzIWk1VrRCUZft8t0BUKxThKrg0I8svc3z2jb3g35e9PouOXJTCzp6OGb8u 3OSmKTeiHpQEEtSnYG0NHywlSTWg7nymeWCtah2TXgGub9TbYMyeZIHQtpT0W/kTmk+Z 2xIQ== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of david@redhat.com designates 66.187.233.73 as permitted sender) smtp.mailfrom=david@redhat.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Gm-Message-State: AOUpUlG+hK4ebHYs04Bw1JPUrKWmmhDSY0WHD8VUls44iPMJJpHd9Vy+ pn20w9V2UoFZXupPmA8nZfPH653XDP769//Oj8fFhmeaxGB5KFgjNJ1MwxSloK5H0vlbHPvMgaB AospO7wexgxC67xj/NrlRjlVs5PLSVGzu5Pa2o7i/SrApOTWH0ARR+JhIwk80siAG0w== X-Received: by 2002:a0c:aa81:: with SMTP id f1-v6mr45047093qvb.80.1534848294760; Tue, 21 Aug 2018 03:44:54 -0700 (PDT) X-Google-Smtp-Source: AA+uWPxHnxOksGhBtowk26TcNYgj3Jq/JkE5KOD6gjXgfyF0DG1LvCKAn01VtqNhKBSBIRdIRZEl X-Received: by 2002:a0c:aa81:: with SMTP id f1-v6mr45047060qvb.80.1534848294259; Tue, 21 Aug 2018 03:44:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1534848294; cv=none; d=google.com; s=arc-20160816; b=dFOh36GYY48o4sHlKmKIZgvkzzIIl90F4Y6VVkr3Mv0vuI9s1m7xGdjFI01bvgdS7W dV53oCzxgdPXJNgVNNjvh7lnKi5IoABVEp/gqBj7K3xMneIgr44XwHkl/yk6NPlE3Cyo nYrw7NV5riuXG0yQY/C/FwEJEip5LavFqb6zw9aZ0tx1wTIzF4hmsB45/sZm6pfl2nx5 u95Z8GAh/Pj98jJ7bo4Cp/R0oxRElUniqv4x8r37VJdDF9IfdULjUC8Eb/5/z3kXP9jq p9wKrWShA8wBm9FpuWDHMyWeCCla+cFHrdo0+qvN6uZceHnRKvhCBrnGi+QKRg8mST6G kWZw== 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 :arc-authentication-results; bh=nnfVyljX6cxkryibLmAWL2uYypuiCLeIZVe8KdBz6Ns=; b=o80336NS8nnaQbxSmJmG/nqByLHAc/LQ+iira0tqJrhm877kcNdgezi7yynuvi686q w9Ly4HN1aoCfH05vCe99ikFj6VJsnfkVWqHK7m+5/nIDzboQjUwMb3MkMuVyQtlEbOnI Vuu0w0utqgOAuRK2i6/fz7oFMPkarueoZV7ai0/q8nyuaHFmsDVD5XPXhlV3zWk5s3d5 TNFtxmob1s8VcETE5VA/2ER5pKUYfWna7kpNhyb8n6+UW2oArts6jlb6GrfItRU+67bc y6YGyuDr+i8IBAnYzyy6NsUJnXdFl/s48X5EgCirRDCgJCs2ZQmKmXLkMNUf6WiSZ55F op+A== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of david@redhat.com designates 66.187.233.73 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 (mx3-rdu2.redhat.com. [66.187.233.73]) by mx.google.com with ESMTPS id v8-v6si3315525qtp.169.2018.08.21.03.44.54 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 21 Aug 2018 03:44:54 -0700 (PDT) Received-SPF: pass (google.com: domain of david@redhat.com designates 66.187.233.73 as permitted sender) client-ip=66.187.233.73; Authentication-Results: mx.google.com; spf=pass (google.com: domain of david@redhat.com designates 66.187.233.73 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-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id CC6F240216FA; Tue, 21 Aug 2018 10:44:53 +0000 (UTC) Received: from t460s.redhat.com (ovpn-117-96.ams2.redhat.com [10.36.117.96]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0B41E7D4C9; Tue, 21 Aug 2018 10:44:51 +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 RFCv2 6/6] memory-hotplug.txt: Add some details about locking internals Date: Tue, 21 Aug 2018 12:44:18 +0200 Message-Id: <20180821104418.12710-7-david@redhat.com> In-Reply-To: <20180821104418.12710-1-david@redhat.com> References: <20180821104418.12710-1-david@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Tue, 21 Aug 2018 10:44:53 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.5]); Tue, 21 Aug 2018 10:44:53 +0000 (UTC) for IP:'10.11.54.5' DOMAIN:'int-mx05.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'david@redhat.com' RCPT:'' 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 Signed-off-by: David Hildenbrand Reviewed-by: Pavel Tatashin --- 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 ===========