From patchwork Sat Feb 25 16:31:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 1747986 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=PAbAfw+z; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4PPC3V3bqCz2469 for ; Sun, 26 Feb 2023 03:32:36 +1100 (AEDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pVxSt-0006wr-Mk; Sat, 25 Feb 2023 11:32:03 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pVxSs-0006uZ-Av for qemu-devel@nongnu.org; Sat, 25 Feb 2023 11:32:02 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pVxSo-0007lM-Mh for qemu-devel@nongnu.org; Sat, 25 Feb 2023 11:32:00 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1677342712; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ZVbXKBk+5YoJXuxsPRNLuFZCQaYMraNe+LiCnLIuD5I=; b=PAbAfw+zo6o64/h+EvsqlALLgeQcZktmPf+U2MJ3mwKqRGaMHwHqubYT9DUm85mgETkZ2D Zpk3QRL1FrUQbjYJzZHxm+XZsO0jhBj6IQkAJxRaCYjeSFqJ7jPvX2SWT+1ykvy8uigpUF OdmjhhLxMnZYbOW9Gc+lhHqEEoZ/1r8= Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-510-0pzsIGMXOfSNCEuv-HPS3w-1; Sat, 25 Feb 2023 11:31:50 -0500 X-MC-Unique: 0pzsIGMXOfSNCEuv-HPS3w-1 Received: by mail-qv1-f70.google.com with SMTP id jy22-20020a0562142b5600b005710b856106so1069494qvb.0 for ; Sat, 25 Feb 2023 08:31:50 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1677342707; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ZVbXKBk+5YoJXuxsPRNLuFZCQaYMraNe+LiCnLIuD5I=; b=vxF0HeSCFtzZiRlZ93ffiWT0FlCAvA5rrhDKjlHTweSfRN18ehVid73jma0rTxv2O/ JtlPwhJxF2Pnv+xD0mjWaPfOLuHp9ZdXmuGcwOf8zBDJt0ut73vMVICaYCTdZAO2pnhM oO9TOZAFyds5GK33qih8PKMoziet8AKQ/aTyjMCLTnEKy/fj+pV5ojREkhNTLnbp63tH 3ff/7Vg9EcVzVz901cErxaDgpgOlHe0b3XU4npf7q2rrgD/aHV9G3yCwKy+yYxTOyi7N Sx5kTPmTYwYE2d3CQKbQ6x9HVqdfutwkKg0tc9wtsAd0Iw1T0f/d0FUlN/PPTdsomsM6 EPVw== X-Gm-Message-State: AO0yUKVyPstY+BntrOP72b37IbAuYwiKu+EcroCPLaknp31mNuVYfFjz kbFFlPs6pccb8u3rqPG2x3ddqG9YqXHEkzqqbeJDqOQJ3IXWTsKoICDVqz+0Ru9mwv8C48Byd0G Tj9AlqD+djMnrJRYqI+FpRV2KarJ19bLqfsBC+n8RQzV35/5q9IaKtPGuN5TqMCiMUmaprA== X-Received: by 2002:ac8:5995:0:b0:3bd:1c0f:74f3 with SMTP id e21-20020ac85995000000b003bd1c0f74f3mr37663025qte.2.1677342707412; Sat, 25 Feb 2023 08:31:47 -0800 (PST) X-Google-Smtp-Source: AK7set9hJZ6h0JepQGiVNijPwhwXgorVfOEVrqze3rqEYiDmpFG2Azp5t7kSO8f3fPFnKOEjDubc+g== X-Received: by 2002:ac8:5995:0:b0:3bd:1c0f:74f3 with SMTP id e21-20020ac85995000000b003bd1c0f74f3mr37662956qte.2.1677342706927; Sat, 25 Feb 2023 08:31:46 -0800 (PST) Received: from x1n.redhat.com (bras-base-aurron9127w-grc-56-70-30-145-63.dsl.bell.ca. [70.30.145.63]) by smtp.gmail.com with ESMTPSA id t8-20020ac85888000000b003bb8c60cdf1sm1558142qta.78.2023.02.25.08.31.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 25 Feb 2023 08:31:45 -0800 (PST) From: Peter Xu To: qemu-devel@nongnu.org Cc: Maxim Levitsky , Stefan Hajnoczi , Juan Quintela , Paolo Bonzini , David Hildenbrand , "Dr . David Alan Gilbert" , Chuang Xu , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= , peterx@redhat.com Subject: [PATCH RFC 1/4] memory: Make memory_listeners RCU-safe for real Date: Sat, 25 Feb 2023 11:31:38 -0500 Message-Id: <20230225163141.1209368-2-peterx@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230225163141.1209368-1-peterx@redhat.com> References: <20230225163141.1209368-1-peterx@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.133.124; envelope-from=peterx@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org I think the plan was making memory_listeners rcu-safe, but maybe not really. This patch does it for real, by using RCU variances of qtailq helpers when modifying memory_listeners. The modification should be serialized by BQL, add assertions to register/unregister functions. Wait for a quiecent period before returning from unregister of memory listener to make sure any rcu reader is accessing valid listeners. AddressSpace.listeners are protected in the same way, so do the same. Signed-off-by: Peter Xu --- softmmu/memory.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/softmmu/memory.c b/softmmu/memory.c index 9d64efca26..a63e0bcbb7 100644 --- a/softmmu/memory.c +++ b/softmmu/memory.c @@ -3016,30 +3016,32 @@ void memory_listener_register(MemoryListener *listener, AddressSpace *as) /* Only one of them can be defined for a listener */ assert(!(listener->log_sync && listener->log_sync_global)); + /* Ownership of memory_listeners & as->listeners for modifications */ + assert(qemu_mutex_iothread_locked()); listener->address_space = as; if (QTAILQ_EMPTY(&memory_listeners) || listener->priority >= QTAILQ_LAST(&memory_listeners)->priority) { - QTAILQ_INSERT_TAIL(&memory_listeners, listener, link); + QTAILQ_INSERT_TAIL_RCU(&memory_listeners, listener, link); } else { QTAILQ_FOREACH(other, &memory_listeners, link) { if (listener->priority < other->priority) { break; } } - QTAILQ_INSERT_BEFORE(other, listener, link); + QTAILQ_INSERT_BEFORE_RCU(other, listener, link); } if (QTAILQ_EMPTY(&as->listeners) || listener->priority >= QTAILQ_LAST(&as->listeners)->priority) { - QTAILQ_INSERT_TAIL(&as->listeners, listener, link_as); + QTAILQ_INSERT_TAIL_RCU(&as->listeners, listener, link_as); } else { QTAILQ_FOREACH(other, &as->listeners, link_as) { if (listener->priority < other->priority) { break; } } - QTAILQ_INSERT_BEFORE(other, listener, link_as); + QTAILQ_INSERT_BEFORE_RCU(other, listener, link_as); } listener_add_address_space(listener, as); @@ -3051,9 +3053,14 @@ void memory_listener_unregister(MemoryListener *listener) return; } + /* Ownership of memory_listeners & as->listeners for modifications */ + assert(qemu_mutex_iothread_locked()); + listener_del_address_space(listener, listener->address_space); - QTAILQ_REMOVE(&memory_listeners, listener, link); - QTAILQ_REMOVE(&listener->address_space->listeners, listener, link_as); + QTAILQ_REMOVE_RCU(&memory_listeners, listener, link); + QTAILQ_REMOVE_RCU(&listener->address_space->listeners, listener, link_as); + /* Wait for RCU readers to finish. NOTE! this may release BQL */ + drain_call_rcu(); listener->address_space = NULL; } From patchwork Sat Feb 25 16:31:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 1747985 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=IYr0SK2N; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4PPC3V2bqDz245s for ; Sun, 26 Feb 2023 03:32:36 +1100 (AEDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pVxSt-0006wV-0a; Sat, 25 Feb 2023 11:32:03 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pVxSq-0006uG-ER for qemu-devel@nongnu.org; Sat, 25 Feb 2023 11:32:01 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pVxSo-0007mb-Fg for qemu-devel@nongnu.org; Sat, 25 Feb 2023 11:32:00 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1677342715; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=PXvHcGHMrV8Q2tJ9gSCW5uYa04vLjGWzQ6ZoHYh7V+c=; b=IYr0SK2N2AVDk+YO+mwpPEUu5eahl9rsC0xsLDZu4tib+YwRJiWNAfBGtl5JcxqnnTKD2g 3ghP/zxM5H6xTzL8xQ249NtF3PXq5+7FoQQRxOP3O3M3gzAQmt4PFEGs6p8EyW4SI7lGEh 9VNzWMYL9QR3hhGwFnmdXpCSl43Ys8g= Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-607-4nzEZmT8P1yykUF5FnqPyw-1; Sat, 25 Feb 2023 11:31:54 -0500 X-MC-Unique: 4nzEZmT8P1yykUF5FnqPyw-1 Received: by mail-qv1-f71.google.com with SMTP id pv11-20020ad4548b000000b0056e96f4fd64so1020711qvb.15 for ; Sat, 25 Feb 2023 08:31:54 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1677342712; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=PXvHcGHMrV8Q2tJ9gSCW5uYa04vLjGWzQ6ZoHYh7V+c=; b=nMrGJiGPiH9F3JX00m16i4ew0QkRVo+oZePSU4tdEl8J2D7S7DH6LxRKuidIlalu0R 3vH3w9y8/G8puqTG9OOY1uHcVN+G4GfqLnXsKtBFTRhb+DVpyP8NsC8508W+8qo1oUfg QrON+x8NwFI/g5RVV4NmaDV08gHzU644H+e3G+W2ZG8xY9O/FNHFFybk/DEcKu8T8Q3b B2yMH+L1keFRToplOHNYoQ6tXSD3LKcgOi2NO9ubHNVbuLnsxC3OvTYT2t6QJCHOnGZD fWD2WNtxzfEYRWD/56ZL5x6Q1U2aHz0Lmw3V7mXV/bEvcAgPCsJe1Vhlk8D6psOHDOEn mXtw== X-Gm-Message-State: AO0yUKVZESuUcof+fa13S/FBQW37Wv77LRdyGaQPLIUFlD6XkrStt0zE s1jlzXe4U/Fhu8IgDz/AonIAF1DVizJ+uGcoaz6HJn6VVxM37NsDDHQhI6MGb4KpvYXhjLh2vET 1XnXhCFWXB2dXT+R0/6VSsEDaDNEBA/daXkSQQn/To3fvTuallQLML6DwT2n3dgDRlBUHqw== X-Received: by 2002:a05:622a:13cc:b0:3bf:bfde:91bd with SMTP id p12-20020a05622a13cc00b003bfbfde91bdmr9887516qtk.5.1677342712525; Sat, 25 Feb 2023 08:31:52 -0800 (PST) X-Google-Smtp-Source: AK7set8zySBkn8zfm26XmuLD0n+i7CUdBXd/vsR8DA3A6MnDj3AmewZmyWjya03UsZ+cCn2Z4tF8ww== X-Received: by 2002:a05:622a:13cc:b0:3bf:bfde:91bd with SMTP id p12-20020a05622a13cc00b003bfbfde91bdmr9887454qtk.5.1677342712088; Sat, 25 Feb 2023 08:31:52 -0800 (PST) Received: from x1n.redhat.com (bras-base-aurron9127w-grc-56-70-30-145-63.dsl.bell.ca. [70.30.145.63]) by smtp.gmail.com with ESMTPSA id t8-20020ac85888000000b003bb8c60cdf1sm1558142qta.78.2023.02.25.08.31.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 25 Feb 2023 08:31:49 -0800 (PST) From: Peter Xu To: qemu-devel@nongnu.org Cc: Maxim Levitsky , Stefan Hajnoczi , Juan Quintela , Paolo Bonzini , David Hildenbrand , "Dr . David Alan Gilbert" , Chuang Xu , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= , peterx@redhat.com Subject: [PATCH RFC 2/4] memory: Use rcu list variance for address_spaces modifications Date: Sat, 25 Feb 2023 11:31:39 -0500 Message-Id: <20230225163141.1209368-3-peterx@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230225163141.1209368-1-peterx@redhat.com> References: <20230225163141.1209368-1-peterx@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.129.124; envelope-from=peterx@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org AddressSpace should be safe to RCU since it's released with call_rcu. Change the list insert/removal to use RCU variances. Signed-off-by: Peter Xu --- softmmu/memory.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/softmmu/memory.c b/softmmu/memory.c index a63e0bcbb7..c48e9cc6ed 100644 --- a/softmmu/memory.c +++ b/softmmu/memory.c @@ -3079,7 +3079,7 @@ void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name) as->ioeventfd_nb = 0; as->ioeventfds = NULL; QTAILQ_INIT(&as->listeners); - QTAILQ_INSERT_TAIL(&address_spaces, as, address_spaces_link); + QTAILQ_INSERT_TAIL_RCU(&address_spaces, as, address_spaces_link); as->name = g_strdup(name ? name : "anonymous"); address_space_update_topology(as); address_space_update_ioeventfds(as); @@ -3103,7 +3103,7 @@ void address_space_destroy(AddressSpace *as) memory_region_transaction_begin(); as->root = NULL; memory_region_transaction_commit(); - QTAILQ_REMOVE(&address_spaces, as, address_spaces_link); + QTAILQ_REMOVE_RCU(&address_spaces, as, address_spaces_link); /* At this point, as->dispatch and as->current_map are dummy * entries that the guest should never use. Wait for the old From patchwork Sat Feb 25 16:31:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 1747988 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=DBArSuTN; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4PPC4D41Wdz240f for ; Sun, 26 Feb 2023 03:33:16 +1100 (AEDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pVxSt-0006ws-NK; Sat, 25 Feb 2023 11:32:03 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pVxSs-0006ub-D2 for qemu-devel@nongnu.org; Sat, 25 Feb 2023 11:32:02 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pVxSp-0007n5-Am for qemu-devel@nongnu.org; Sat, 25 Feb 2023 11:32:01 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1677342718; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=4KgT2+S1We7LJEei51AwIQia0/3Tv3edo38yyoS6jKc=; b=DBArSuTNRlxhjLZB6z524GGx2dwm7ps5gTGjafKf+n/gZl//csJTd9a8jcRM4JS6UVKVR1 lTz35pqdxoiX2Ok2YNqrel4esqG7X/bsuloV10B0OugTmsL6FwlaZYZIxxUl8+MU368OuX I/PlWYJEO7G7MOwLHl+TDZk8xYMXOEI= Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-258-Ivh0_SzkOh6tinrDkOUX4A-1; Sat, 25 Feb 2023 11:31:57 -0500 X-MC-Unique: Ivh0_SzkOh6tinrDkOUX4A-1 Received: by mail-qt1-f197.google.com with SMTP id c5-20020ac85185000000b003bfae3b8051so1345981qtn.0 for ; Sat, 25 Feb 2023 08:31:57 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1677342715; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=4KgT2+S1We7LJEei51AwIQia0/3Tv3edo38yyoS6jKc=; b=1KqrHlVmjbJdoU5pJxfpH0JGk2b9z8LsZCgbH63KREpgW7cprynm1SceRx0OpfRTCc SOh+wuUQuiNF7d7/wKLxRmSKTy2A7GwJaK1Ey3rj/UKqxdVnE3wsjEUXFPOU1VspOb7o ue90I3/ktfQ023i6wAGni+vY67mgOOdEUTcD6LUpnCxON3YjR3XGinhYkBXGl/wFWW6o RslR2BCXrG1MZUkv/XH4GuYxaNTqedfQ0o0DoHOD+z2C8LbqlFpWF9twu5YnaG3AqvY1 p5zE7SAXLPvyxX1RPClaYXyWuH4V7MbRKls5/CyckFXLchwLbO7u+5OoHV+uJiWpqy1B SWcA== X-Gm-Message-State: AO0yUKUn2OwPsM106ivlHw51WWZgDHH23ovFfQ9RmTRQMCMPMcqir3jP 4BMeCHLlqPhql2061VuZYaMt1E7ywGJLb/MdTWwK+WzDf6Fkm6dZWjFjFmg4OqkuuU9at2ivoNI M5Z1oe6zdLyxllXc+zpgvpIcEh020oWN7oLJRXiPrfPrPcyFqblDeeprnfq4VdQsFVKJuUQ== X-Received: by 2002:a05:622a:1822:b0:3bf:be4b:8094 with SMTP id t34-20020a05622a182200b003bfbe4b8094mr9562678qtc.0.1677342715622; Sat, 25 Feb 2023 08:31:55 -0800 (PST) X-Google-Smtp-Source: AK7set/y9tanwG7m3Xcj+W8VLLN07nI6CjBKP5IF6+zwazX6mm22KVySBFh1ai/Z93lZAZbIW7qksw== X-Received: by 2002:a05:622a:1822:b0:3bf:be4b:8094 with SMTP id t34-20020a05622a182200b003bfbe4b8094mr9562632qtc.0.1677342715233; Sat, 25 Feb 2023 08:31:55 -0800 (PST) Received: from x1n.redhat.com (bras-base-aurron9127w-grc-56-70-30-145-63.dsl.bell.ca. [70.30.145.63]) by smtp.gmail.com with ESMTPSA id t8-20020ac85888000000b003bb8c60cdf1sm1558142qta.78.2023.02.25.08.31.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 25 Feb 2023 08:31:54 -0800 (PST) From: Peter Xu To: qemu-devel@nongnu.org Cc: Maxim Levitsky , Stefan Hajnoczi , Juan Quintela , Paolo Bonzini , David Hildenbrand , "Dr . David Alan Gilbert" , Chuang Xu , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= , peterx@redhat.com Subject: [PATCH RFC 3/4] memory: Protect memory_region_clear_dirty_bitmap with RCU Date: Sat, 25 Feb 2023 11:31:40 -0500 Message-Id: <20230225163141.1209368-4-peterx@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230225163141.1209368-1-peterx@redhat.com> References: <20230225163141.1209368-1-peterx@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.133.124; envelope-from=peterx@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Clear dirty bitmap operation needs to walk memory_listeners but the context may not hold BQL. These callers hold BQL for it: cpu_physical_memory_sync_dirty_bitmap dirtyrate_manual_reset_protect These callers hold RCU for it: migration_clear_memory_region_dirty_bitmap [1] cpu_physical_memory_test_and_clear_dirty The above case [1] is extremely unobvious and probably still buggy, because: - Either the RCU read lock was taken very high from the stack in ram_save_iterate() or ram_save_queue_pages() where the RCU lock was probably taken for the sake of ramblock references (which is also protected by RCU), or, - I _think_ there's path that leaks taking any lock (e.g. the other path migration_clear_memory_region_dirty_bitmap_range which is used by virtio-mem or virtio-balloon that may or may not really take RCU at all, neither BQL). Add the RCU read lock in memory_region_clear_dirty_bitmap() to make sure it's not missed. The RCU is also needed for address_space_get_flatview(), so this will generally making the RCU section larger to cover the whole walking process when not taken, but wanted. This should be the only place that we referenced memory_listeners (or as->listeners) without guaranteed to hold BQL nor RCU. Signed-off-by: Peter Xu --- softmmu/memory.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/softmmu/memory.c b/softmmu/memory.c index c48e9cc6ed..95cdcaeccf 100644 --- a/softmmu/memory.c +++ b/softmmu/memory.c @@ -2270,7 +2270,8 @@ void memory_region_clear_dirty_bitmap(MemoryRegion *mr, hwaddr start, FlatRange *fr; hwaddr sec_start, sec_end, sec_size; - QTAILQ_FOREACH(listener, &memory_listeners, link) { + RCU_READ_LOCK_GUARD(); + QTAILQ_FOREACH_RCU(listener, &memory_listeners, link) { if (!listener->log_clear) { continue; } From patchwork Sat Feb 25 16:31:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Xu X-Patchwork-Id: 1747984 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=F/TE1+49; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4PPC3V1zzgz240f for ; Sun, 26 Feb 2023 03:32:36 +1100 (AEDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pVxT0-0006xx-3X; Sat, 25 Feb 2023 11:32:10 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pVxSt-0006wq-K0 for qemu-devel@nongnu.org; Sat, 25 Feb 2023 11:32:03 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pVxSs-0007pL-3N for qemu-devel@nongnu.org; Sat, 25 Feb 2023 11:32:03 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1677342721; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3nMIHYNGqj7vLSxK7AMfLxIZi6naTf38R3aAeG349Ag=; b=F/TE1+492oAuu6gm/1qyQI4PDa8vle/N4DTWsOcWodY1UTq+iS9sXUQtKAQMAAPFda//tZ sqzyBY8FFAnk3REwWuCkEyhDi2HSPvWd3xKM13syo9SBKCKKZGLKVUJBalkZuZLkCpqBYI aM58425HeRCXC++NvWiUENClthcTuD8= Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-645-4QOT8huHP5urebqR2WVUMg-1; Sat, 25 Feb 2023 11:31:59 -0500 X-MC-Unique: 4QOT8huHP5urebqR2WVUMg-1 Received: by mail-qt1-f199.google.com with SMTP id k19-20020ac86053000000b003bd0d4e3a50so1297431qtm.9 for ; Sat, 25 Feb 2023 08:31:59 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3nMIHYNGqj7vLSxK7AMfLxIZi6naTf38R3aAeG349Ag=; b=nB/3Is3mpmbhr5yiUGSu1SAUdNIQXl+HPlY4EsRlT3UIKxFWDu+tcJeGkKuvkdJNzH LxLQA+Y42Uxg/tNqZGVxq/YuCeOTQrgioTwhuSIZ1XVdEWDsernW9+D7U9xxzzFc2hay oQdqcyXAXB3a3q6lA6p8kwqAvg6V0nYPlrx23UdTxbx3vR+Kf7Xpspln0echsKMBVbTs 543TWsPsrz3RCjeLyKLK7Jz7BjcRi7/McgeBoHWPPG4WzjR6MQNLkgPCFMuqsLfyXDfu d1dg+4IYhDxDl6MInCI/vNw8L55zQqjAXmcI2rKPm5mToXy62MCxVyoM33xyo0ltJhPo QjSg== X-Gm-Message-State: AO0yUKWrssx5U71yKcx9/E/zvDHXs1L6QcdetMrR6gfX2vOlGLZVJX9N 6rqXX8M/4tIyRHVGy+oxsaDVExLoOUc7xkbt2Be5sXak1Qkc1ArzdT28rhYuI1g4uJOuOZK+K2f /jVpYHWcaKKHJoZTThm1Ypr2XFo9i9QTHyGXbfrXBGanOEJS5QQ/tF9yMrebM9PgKIE+2AA== X-Received: by 2002:ac8:4e95:0:b0:3bd:142d:64dd with SMTP id 21-20020ac84e95000000b003bd142d64ddmr38418067qtp.3.1677342718704; Sat, 25 Feb 2023 08:31:58 -0800 (PST) X-Google-Smtp-Source: AK7set/lSZKXf7vWwi/Zzxa116T4q2pNIIs8HJZOGIQGZnmfMGocBg6pV1BIV0QE8zaptUMUqy+pbg== X-Received: by 2002:ac8:4e95:0:b0:3bd:142d:64dd with SMTP id 21-20020ac84e95000000b003bd142d64ddmr38418014qtp.3.1677342718305; Sat, 25 Feb 2023 08:31:58 -0800 (PST) Received: from x1n.redhat.com (bras-base-aurron9127w-grc-56-70-30-145-63.dsl.bell.ca. [70.30.145.63]) by smtp.gmail.com with ESMTPSA id t8-20020ac85888000000b003bb8c60cdf1sm1558142qta.78.2023.02.25.08.31.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 25 Feb 2023 08:31:57 -0800 (PST) From: Peter Xu To: qemu-devel@nongnu.org Cc: Maxim Levitsky , Stefan Hajnoczi , Juan Quintela , Paolo Bonzini , David Hildenbrand , "Dr . David Alan Gilbert" , Chuang Xu , =?utf-8?q?Philippe_Mathieu-Daud?= =?utf-8?q?=C3=A9?= , peterx@redhat.com Subject: [PATCH RFC 4/4] memory: Use rcu traversal in memory_region_to_address_space Date: Sat, 25 Feb 2023 11:31:41 -0500 Message-Id: <20230225163141.1209368-5-peterx@redhat.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230225163141.1209368-1-peterx@redhat.com> References: <20230225163141.1209368-1-peterx@redhat.com> MIME-Version: 1.0 Received-SPF: pass client-ip=170.10.129.124; envelope-from=peterx@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org memory_region_to_address_space() is the only function that walks the address_spaces list using RCU read lock only. Use the RCU walker to reflect that. Signed-off-by: Peter Xu --- softmmu/memory.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/softmmu/memory.c b/softmmu/memory.c index 95cdcaeccf..0b652d9597 100644 --- a/softmmu/memory.c +++ b/softmmu/memory.c @@ -566,7 +566,7 @@ static AddressSpace *memory_region_to_address_space(MemoryRegion *mr) while (mr->container) { mr = mr->container; } - QTAILQ_FOREACH(as, &address_spaces, address_spaces_link) { + QTAILQ_FOREACH_RCU(as, &address_spaces, address_spaces_link) { if (mr == as->root) { return as; }