From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.3 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C31D0C4338F for ; Wed, 28 Jul 2021 14:03:06 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 2DB7E60C3F for ; Wed, 28 Jul 2021 14:03:06 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 2DB7E60C3F Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=nongnu.org Received: from localhost ([::1]:44378 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1m8k9J-0001Qr-3U for qemu-devel@archiver.kernel.org; Wed, 28 Jul 2021 10:03:05 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:54436) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m8k8R-0000Zo-PI for qemu-devel@nongnu.org; Wed, 28 Jul 2021 10:02:11 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:52398) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1m8k8L-000765-8T for qemu-devel@nongnu.org; Wed, 28 Jul 2021 10:02:10 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1627480923; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=O4h9I1P9g5o3ADlfUVRX9qHGC0PMa/ZwZFmRwYxcFRk=; b=KWlqDdj7thOvWPmBoInICpmf6+hvXflLvyJ6QaD3zQIaOGGAwbsimZrHyq6AMOpPzx/Pi1 AYzHczEkr1btrcFzh8Yz+gni9gp7BsGmp13OMrqlezUFhUG+EzyqLCOJbg1/EGNMUFq5x0 5OFfWZYZZF5NK4FwC/4Nn10wWy3+OFU= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-236-IDTKza8wNfSyC-RUa0ujYw-1; Wed, 28 Jul 2021 10:02:01 -0400 X-MC-Unique: IDTKza8wNfSyC-RUa0ujYw-1 Received: by mail-wr1-f70.google.com with SMTP id d13-20020adfc3cd0000b02901531b0b7c89so987169wrg.23 for ; Wed, 28 Jul 2021 07:02:01 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:organization :message-id:date:user-agent:mime-version:in-reply-to :content-language:content-transfer-encoding; bh=O4h9I1P9g5o3ADlfUVRX9qHGC0PMa/ZwZFmRwYxcFRk=; b=mGqt65jiIM7qdMWpcRFKMZHG1JE1k0DNyUSokAIY13659HIKVfr3LUGygV6jqntliV k1O2NvN2lkFVJu6i9QTAhZ5IIHNI4VvS7/JYzKCUX8pYOogNqOuxXesoW0XbxbLkdo2Q /fnjF7rzs4r6WoFDNod+2y6v2a/Svm7Vo4y9F/fcJmF+Na/z5bdsW0CRAQC7cEV9KF6x FRdRDphwmZKcyin+bfDpOACQMdrCgv32PWcUuwrM/r2dAtjZGHPsU9h2TMXTbr1i77r6 BCOkLSVILaW7TeBB/5bd6bFkZXzXIMf7UBrtFlr0Bq1M0tUqyZan1Y5HZBzpgnzU4adE g06A== X-Gm-Message-State: AOAM532SC2en+xbQMMhnYmCK2lWzCIJx3EyuNzrq3wuBhy0rbSXmXU1a EBBxCuZu1pNthDvHsQysB+dXo0IXSRNsXK+/D0sgqWVw4L4MvBR145CbkQU/yPmcPh8qxXDXDGc uAVMjeQdJutqx9iw= X-Received: by 2002:a05:600c:1906:: with SMTP id j6mr9299641wmq.108.1627480920369; Wed, 28 Jul 2021 07:02:00 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyg67KaNdPFyqshf2+/dJEYIyViKMZeSqyJximGtipD+FPbVMzhaMoJGy7lCKx1j+kYy/0QuA== X-Received: by 2002:a05:600c:1906:: with SMTP id j6mr9299614wmq.108.1627480920092; Wed, 28 Jul 2021 07:02:00 -0700 (PDT) Received: from ?IPv6:2003:d8:2f0a:7f00:fad7:3bc9:69d:31f? (p200300d82f0a7f00fad73bc9069d031f.dip0.t-ipconnect.de. [2003:d8:2f0a:7f00:fad7:3bc9:69d:31f]) by smtp.gmail.com with ESMTPSA id s13sm6171183wmc.47.2021.07.28.07.01.59 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 28 Jul 2021 07:01:59 -0700 (PDT) Subject: Re: [PATCH v2 4/9] memory: Don't do topology update in memory finalize() To: Peter Xu References: <20210723193444.133412-1-peterx@redhat.com> <20210723193444.133412-5-peterx@redhat.com> <1ced8a81-18a2-85fe-0323-03dbc606f73e@redhat.com> <5fb63e79-7a75-d7bc-2c62-9149f91c5d83@redhat.com> From: David Hildenbrand Organization: Red Hat Message-ID: <8f60030d-fbea-7dac-af1d-e8c9f9f5d8b1@redhat.com> Date: Wed, 28 Jul 2021 16:01:58 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=david@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Received-SPF: pass client-ip=170.10.133.124; envelope-from=david@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -37 X-Spam_score: -3.8 X-Spam_bar: --- X-Spam_report: (-3.8 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.719, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, NICE_REPLY_A=-0.277, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=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.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Paolo Bonzini , qemu-devel@nongnu.org, Richard Henderson Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" On 28.07.21 15:56, Peter Xu wrote: > On Wed, Jul 28, 2021 at 02:13:17PM +0200, David Hildenbrand wrote: >> On 27.07.21 18:02, Peter Xu wrote: >>> On Tue, Jul 27, 2021 at 03:21:31PM +0200, David Hildenbrand wrote: >>>> On 23.07.21 21:34, Peter Xu wrote: >>>>> Topology update could be wrongly triggered in memory region finalize() if >>>>> there's bug somewhere else. It'll be a very confusing stack when it >>>>> happens (e.g., sending KVM ioctl within the RCU thread, and we'll observe it >>>>> only until it fails!). >>>>> >>>>> Instead of that, we use the push()/pop() helper to avoid memory transaction >>>>> commit, at the same time we use assertions to make sure there's no pending >>>>> updates or it's a nested transaction, so it could fail even earlier and in a >>>>> more explicit way. >>>>> >>>>> Suggested-by: Paolo Bonzini >>>>> Signed-off-by: Peter Xu >>>>> --- >>>>> softmmu/memory.c | 23 +++++++++++++++++++++-- >>>>> 1 file changed, 21 insertions(+), 2 deletions(-) >>>>> >>>>> diff --git a/softmmu/memory.c b/softmmu/memory.c >>>>> index 1a3e9ff8ad..dfce4a2bda 100644 >>>>> --- a/softmmu/memory.c >>>>> +++ b/softmmu/memory.c >>>>> @@ -170,6 +170,12 @@ struct MemoryRegionIoeventfd { >>>>> EventNotifier *e; >>>>> }; >>>>> +/* Returns whether there's any pending memory updates */ >>>>> +static bool memory_region_has_pending_update(void) >>>>> +{ >>>>> + return memory_region_update_pending || ioeventfd_update_pending; >>>>> +} >>>>> + >>>>> static bool memory_region_ioeventfd_before(MemoryRegionIoeventfd *a, >>>>> MemoryRegionIoeventfd *b) >>>>> { >>>>> @@ -1756,12 +1762,25 @@ static void memory_region_finalize(Object *obj) >>>>> * and cause an infinite loop. >>>>> */ >>>>> mr->enabled = false; >>>>> - memory_region_transaction_begin(); >>>>> + >>>>> + /* >>>>> + * Use push()/pop() instead of begin()/commit() to make sure below block >>>>> + * won't trigger any topology update (which should never happen, but it's >>>>> + * still a safety belt). >>>>> + */ >>>> >>>> Hmm, I wonder if we can just keep the begin/end semantics and just do an >>>> assertion before doing the commit? Does anything speak against that? >>> >>> That sounds working too for the case of run_on_cpu and similar, but I think >>> this patch should be able to cover more. For example, it's possible depth==0 >>> when enter memory_region_finalize(), but some removal of subregions could >>> further cause memory layout changes. IMHO we should also bail out early for >>> those cases too. Thanks, >>> >> >> Do we really have to switch to push/pop to catch these cases early? I'd >> assume we'd just have to formulate the right assertions :) > > The subject and commit message was trying to tell why. :) > > "memory: Don't do topology update in memory finalize()" > > The root reason is errors within memory commit can be very hard to digest, > however the assertion failure is easier to understand because any memory layout > change is not expected to happen. > > The push/pop (I renamed it after your other comment to depth_inc/dec) avoids > memory commit happening at all within finalize(), and make sure we always fail > at the assertion as long as there's any potential memory update (even if the > memory update won't crash qemu immediately). Okay, makes sense to me, thanks Acked-by: David Hildenbrand -- Thanks, David / dhildenb