From patchwork Fri Oct 20 15:23:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yong Huang X-Patchwork-Id: 13430876 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org 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 smtp.lore.kernel.org (Postfix) with ESMTPS id 45D2EC00A98 for ; Fri, 20 Oct 2023 15:27:32 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qtrO8-0002qj-IE; Fri, 20 Oct 2023 11:26:12 -0400 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 1qtrO4-0002gw-Tz for qemu-devel@nongnu.org; Fri, 20 Oct 2023 11:26:08 -0400 Received: from mail-pl1-x62b.google.com ([2607:f8b0:4864:20::62b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qtrO1-00060c-S5 for qemu-devel@nongnu.org; Fri, 20 Oct 2023 11:26:08 -0400 Received: by mail-pl1-x62b.google.com with SMTP id d9443c01a7336-1ca74e77aecso15647035ad.1 for ; Fri, 20 Oct 2023 08:24:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=smartx-com.20230601.gappssmtp.com; s=20230601; t=1697815443; x=1698420243; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=IQwXPhv+suUODSVaOFT/uvjWZ0giK1V2oP5SwGbONBE=; b=RqJP9v+772MdWdjHpO4nozJWSE9vQ5J+xhrNy4ptLNqHjDY0hj0/2Z0YCDut2Xv1AJ aB/pw7Wz2w23WZq3C9GFdncRiOZQowaAt1B1H13MNxitoNM3FOKsvoZQ0h/ciwrNDGRS 9/9LWdZZhg3koKIceYZ7CH5Ti+D6kZZUX08AurjYlit+ze0oNCOLspEQTc1tywvmTCdy uyZBvtB9EeW3CwAXcnoSrm39GBfvHZLzEy4ITQNWv21aJPIFv/q+oFE7XzxqA5Rm7t0A aEkwrmNkwnHOu5RY6RTgvcU2Sy+IelDLat5y++jCiVzvcl5oP0nsbMwQ5FsjtVT4yfyO cYug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697815443; x=1698420243; 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=IQwXPhv+suUODSVaOFT/uvjWZ0giK1V2oP5SwGbONBE=; b=DRfxeYvJDj+Yx9RHBuBqDalBC3EVGC9cICmoExUrxNmrmL2qEiNk/UiCsvkdoAiNbQ mqCZcBvKQY6970NIxIrvuGp87BhdnGXqbWIZT4FGoM1Eiu6WHXIZUo8b7pBazaGdvLGZ Dx/GmDbrngJBTEMf9bBNsg1nAHaR43AgL9agMbqADnHq4JUvnjPzkateTx+JlqM2AufJ I7vSxMjMgjFdnE8fECJrOhiPLhnVTnCEe0/vCPQzck+TMpL9KVP7sW91QCcleIrxs/CE 4BS3rFgwqmRGbXsIR2U3Hg3WX1Ok47FGpkVkKH4AvaoOLYMewcmIInZTu48bF6JOsnOV oQfA== X-Gm-Message-State: AOJu0Yxk9bgXGpR2IFSkaagUWhK3dKvhFP6cxp771Xr6c20Napwv5lt7 ElX3szUnFimyfOLu+dPPD3npIycTpL5Pt5EsroyPcE4V X-Google-Smtp-Source: AGHT+IHombyZ6D0T+fsB2qGSszQFtb8Ux1CIfsamDAyWx1cmZqAeFOuQ9q1d5NshrXxkcxOjAnD8/g== X-Received: by 2002:a17:903:1c3:b0:1c7:22ae:4080 with SMTP id e3-20020a17090301c300b001c722ae4080mr2878261plh.0.1697815443264; Fri, 20 Oct 2023 08:24:03 -0700 (PDT) Received: from localhost.localdomain ([171.216.79.181]) by smtp.gmail.com with ESMTPSA id d4-20020a170902cec400b001c60d0a6d84sm1644824plg.127.2023.10.20.08.24.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 20 Oct 2023 08:24:03 -0700 (PDT) From: Hyman Huang To: qemu-devel@nongnu.org Cc: Juan Quintela , Peter Xu , Fabiano Rosas , Leonardo Bras , Thomas Huth , Laurent Vivier , Paolo Bonzini , Hyman Huang Subject: [v2 1/6] system/dirtylimit: Fix a race situation Date: Fri, 20 Oct 2023 23:23:43 +0800 Message-Id: X-Mailer: git-send-email 2.39.1 In-Reply-To: References: MIME-Version: 1.0 Received-SPF: none client-ip=2607:f8b0:4864:20::62b; envelope-from=yong.huang@smartx.com; helo=mail-pl1-x62b.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=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+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Fix a race situation for global variable dirtylimit_state. Also, replace usleep by g_usleep to increase platform accessibility to the sleep function. Signed-off-by: Hyman Huang Reviewed-by: Fabiano Rosas --- system/dirtylimit.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/system/dirtylimit.c b/system/dirtylimit.c index fa959d7743..3666c4cb7c 100644 --- a/system/dirtylimit.c +++ b/system/dirtylimit.c @@ -411,12 +411,20 @@ void dirtylimit_set_all(uint64_t quota, void dirtylimit_vcpu_execute(CPUState *cpu) { - if (dirtylimit_in_service() && - dirtylimit_vcpu_get_state(cpu->cpu_index)->enabled && - cpu->throttle_us_per_full) { - trace_dirtylimit_vcpu_execute(cpu->cpu_index, - cpu->throttle_us_per_full); - usleep(cpu->throttle_us_per_full); + if (cpu->throttle_us_per_full) { + dirtylimit_state_lock(); + + if (dirtylimit_in_service() && + dirtylimit_vcpu_get_state(cpu->cpu_index)->enabled) { + dirtylimit_state_unlock(); + trace_dirtylimit_vcpu_execute(cpu->cpu_index, + cpu->throttle_us_per_full); + + g_usleep(cpu->throttle_us_per_full); + return; + } + + dirtylimit_state_unlock(); } }