From patchwork Sat Dec 24 15:18:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 636600 Delivered-To: patch@linaro.org Received: by 2002:a17:522:c983:b0:460:3032:e3c4 with SMTP id kr3csp779022pvb; Sat, 24 Dec 2022 07:19:36 -0800 (PST) X-Google-Smtp-Source: AMrXdXul9C9tgZu9PHq2K+UsfpYJECJ3vUgsCDblfKXX2ZiZoO4BbiPAhnFHfXFBmeU/XsVkS8sR X-Received: by 2002:a05:6214:3a02:b0:4c7:7f63:5fbf with SMTP id nw2-20020a0562143a0200b004c77f635fbfmr17819921qvb.4.1671895175868; Sat, 24 Dec 2022 07:19:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671895175; cv=none; d=google.com; s=arc-20160816; b=kPTm5kd0GHCW3WeFpjU+9cYwVQt97nna5RrYSRHH8EPpSIxNKqEPPcoxnjWCresN8G 24j2bkFT+yjfcHF2zoUKa8CjgiUWfrODMv7hdspI9vg0hwhgqGuUs6nyv//VvROLofK/ 0P8Wb5kdERnwYt+4X+kCiz28LEhyOVOHMIHxXpCm9a9VqsVDFILW84Gz1i1NveHVkWC2 q9FPEghnEB9LQm0v4Mu4em8RXb2tH0K/L7g8GdNklr2a8F7/bZeCnWPRegQqzJ6421yE 0N7+FiXRJgSDrzU0EOQ66Jgg5Lh9Qm/hiEsRNCvZ4U7VhlfZFBNJhRr7KNRhKpsR/9Wn fndw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=EogRULrLfJQmVZpCaE9KC8IG8I8XrMvRS8yWEac0lHY=; b=ikWSDhaYJE9UkKEeDFMyBP2+kMDb0Zsn44504f07bqEnELhrStuHCd2p5xsVqO89l4 6xGKVcKZX8iWlK+94ByzQV7I4W86Kp6CGEQKr0hpXH/S9lfIF1VdfTndlYti1gSO7BPK wm1qNFoPS+lrtVfTJvtk9ImbaL4XRiqZWUnuV+cqEKOsI5aOBSQDld4RBXyUjwwM4MYB +b5eki2DDTPfH3dcZQQY9Zj1E6XOfxEQ49y6YFCTUqbr+yXfifu8dNUhFvSSZAE6E4PY GNjUBQEKC/GJtWY539v/qlWaoAVekkVVVeyHkKCCnlV4nShqXlEcoHtnwVsJdTrm+duV 11Fw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=QefQAZ0V; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id o3-20020ad45c83000000b004bbc6765aa0si2268643qvh.294.2022.12.24.07.19.35 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sat, 24 Dec 2022 07:19:35 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=QefQAZ0V; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1p96I8-00056Y-Ew; Sat, 24 Dec 2022 10:18:28 -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 1p96I7-00055t-1i for qemu-devel@nongnu.org; Sat, 24 Dec 2022 10:18:27 -0500 Received: from mail-pg1-x532.google.com ([2607:f8b0:4864:20::532]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1p96I5-00016Z-Cp for qemu-devel@nongnu.org; Sat, 24 Dec 2022 10:18:26 -0500 Received: by mail-pg1-x532.google.com with SMTP id 79so4929729pgf.11 for ; Sat, 24 Dec 2022 07:18:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; 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=EogRULrLfJQmVZpCaE9KC8IG8I8XrMvRS8yWEac0lHY=; b=QefQAZ0Vlf6OZqv485bBrR4VBI563u/QNy3e9dlRZ7Rhl/ADexBgZX0evJ9/J+r6o+ /kX1rwdbpU/D7zroy9uZP3WhBJWXJ6X/pQ3vDOGl3dDFXNZMD1h5ERLcAc2TDyh1czuv Wq2x2kHo+d7z8Moe6r0bxEdEVRqFlZlmvit7a4575P4+23+TbTvzvEEXIvxKWWF1mUzr 0pL0F6sXDze85/LrTKCUYnZJu4GEpj1GeYEQ0OCQti6qUannTK7mpF8AqY7Gc3IYjpVn QMFWvWqYhRatQR6klfKWzMl/GSyJ0HW8xLGCPojNlLT8uougL9RjB2xjOGGNNVIuygut TWEw== 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=EogRULrLfJQmVZpCaE9KC8IG8I8XrMvRS8yWEac0lHY=; b=i/ROv1UYDHHvpcGAX4IjV92W0C73hrjevWvTl15NVjQqsFxG3qro+IlldWfopvNtBS Ng8VgXVaD641AfuwQZIDVL3iHOCJl8j0R/rHkqCwKc2g+su26fOavysDvRdd/RoB2tLW FBjYYGqbrdRNCCjyOmJU9suYxm01R8q/ErYdZU9rcue7qEHvr6gd9XEtV72ccXntqVBV rADTQvx/0Cjtv6wCBTGHqjQ96wlYwm4SVDv06cxeIsH7RoGUBlKXLlJaKE06R6WlabE7 pNv/D0e2+ggs+UkRj7mY6m8HTwPJsZZzorJ4R7MdhjEUJvZCax/ptYvbL6jdKh3bQk4l juvw== X-Gm-Message-State: AFqh2krSnuSFJWvhTdq4ZEUkmPrPICki8gQp1AKeur+a7qXGksMvNVuw a5Y3ZzLnsiGMeNnd/flrseaD2EzfKUcA38E+ X-Received: by 2002:aa7:9154:0:b0:57b:30b6:9e85 with SMTP id 20-20020aa79154000000b0057b30b69e85mr14201906pfi.22.1671895103731; Sat, 24 Dec 2022 07:18:23 -0800 (PST) Received: from stoup.. (76-14-210-194.or.wavecable.com. [76.14.210.194]) by smtp.gmail.com with ESMTPSA id t64-20020a625f43000000b005774f19b41csm4231124pfb.88.2022.12.24.07.18.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 24 Dec 2022 07:18:23 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Cc: iii@linux.ibm.com Subject: [PATCH 1/4] accel/tcg: Fix tb_invalidate_phys_page_unwind Date: Sat, 24 Dec 2022 07:18:18 -0800 Message-Id: <20221224151821.464455-2-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221224151821.464455-1-richard.henderson@linaro.org> References: <20221224151821.464455-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::532; envelope-from=richard.henderson@linaro.org; helo=mail-pg1-x532.google.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, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, 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+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org When called from syscall(), we are not within a TB and pc == 0. We can skip the check for invalidating the current TB. Signed-off-by: Richard Henderson Reviewed-by: Philippe Mathieu-Daudé Reviewed-by: Philippe Mathieu-Daudé --- accel/tcg/tb-maint.c | 78 ++++++++++++++++++++++++-------------------- 1 file changed, 43 insertions(+), 35 deletions(-) diff --git a/accel/tcg/tb-maint.c b/accel/tcg/tb-maint.c index 1b8e860647..b3d6529ae2 100644 --- a/accel/tcg/tb-maint.c +++ b/accel/tcg/tb-maint.c @@ -1024,43 +1024,51 @@ void tb_invalidate_phys_page(tb_page_addr_t addr) */ bool tb_invalidate_phys_page_unwind(tb_page_addr_t addr, uintptr_t pc) { - assert(pc != 0); -#ifdef TARGET_HAS_PRECISE_SMC - assert_memory_lock(); - { - TranslationBlock *current_tb = tcg_tb_lookup(pc); - bool current_tb_modified = false; - TranslationBlock *tb; - PageForEachNext n; + TranslationBlock *current_tb; + bool current_tb_modified; + TranslationBlock *tb; + PageForEachNext n; - addr &= TARGET_PAGE_MASK; - - PAGE_FOR_EACH_TB(addr, addr + TARGET_PAGE_SIZE, unused, tb, n) { - if (current_tb == tb && - (tb_cflags(current_tb) & CF_COUNT_MASK) != 1) { - /* - * If we are modifying the current TB, we must stop its - * execution. We could be more precise by checking that - * the modification is after the current PC, but it would - * require a specialized function to partially restore - * the CPU state. - */ - current_tb_modified = true; - cpu_restore_state_from_tb(current_cpu, current_tb, pc); - } - tb_phys_invalidate__locked(tb); - } - - if (current_tb_modified) { - /* Force execution of one insn next time. */ - CPUState *cpu = current_cpu; - cpu->cflags_next_tb = 1 | CF_NOIRQ | curr_cflags(current_cpu); - return true; - } + /* + * Without precise smc semantics, or when outside of a TB, + * we can skip to invalidate. + */ +#ifndef TARGET_HAS_PRECISE_SMC + pc = 0; +#endif + if (!pc) { + tb_invalidate_phys_page(addr); + return false; + } + + assert_memory_lock(); + current_tb = tcg_tb_lookup(pc); + + addr &= TARGET_PAGE_MASK; + current_tb_modified = false; + + PAGE_FOR_EACH_TB(addr, addr + TARGET_PAGE_SIZE, unused, tb, n) { + if (current_tb == tb && + (tb_cflags(current_tb) & CF_COUNT_MASK) != 1) { + /* + * If we are modifying the current TB, we must stop its + * execution. We could be more precise by checking that + * the modification is after the current PC, but it would + * require a specialized function to partially restore + * the CPU state. + */ + current_tb_modified = true; + cpu_restore_state_from_tb(current_cpu, current_tb, pc); + } + tb_phys_invalidate__locked(tb); + } + + if (current_tb_modified) { + /* Force execution of one insn next time. */ + CPUState *cpu = current_cpu; + cpu->cflags_next_tb = 1 | CF_NOIRQ | curr_cflags(current_cpu); + return true; } -#else - tb_invalidate_phys_page(addr); -#endif /* TARGET_HAS_PRECISE_SMC */ return false; } #else From patchwork Sat Dec 24 15:18:19 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 636598 Delivered-To: patch@linaro.org Received: by 2002:a17:522:c983:b0:460:3032:e3c4 with SMTP id kr3csp778898pvb; Sat, 24 Dec 2022 07:19:12 -0800 (PST) X-Google-Smtp-Source: AMrXdXtd+aNr4qlre0yA8K9gMgFcIeGTsichcRPbiJ3I5mSk43taK8QxXymwc1XXC6gdq8d3qv1z X-Received: by 2002:a05:6214:3a02:b0:4c7:6944:47b4 with SMTP id nw2-20020a0562143a0200b004c7694447b4mr17869409qvb.7.1671895152056; Sat, 24 Dec 2022 07:19:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671895152; cv=none; d=google.com; s=arc-20160816; b=raTUzLyVMptDZkBgAoTWJ1XKJyu179jtti37sAFex8blC98lK81IhQAMzbquayADX9 G7xpUcoqwISBewhvxlRLS8raAjEUSz/RqMYerGXXZ982/bg7A4K2fD5pZJRB6ehcbeuG P430N2PZFLQyKOIi15xwFerO5yQB9IVWHQmCwoAJuqyIST+zR3q6mqSA6YtcG6h5G5ka lvMFSbunbT5E3lXIjcIVyZy6uC7dye6BbW2qIEXtCie4YqUNDYEYkSRXei6ojK5YdBpN vG47r7InkclWqLuCCro2ncnIQz0t8c2tQdsKiMGwEhEh4Svcdc1wqZ4+walUFT7Y6CZr nZmQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=3ZdTKdjXZPsSu57R08aDOC5nHrf6U05QdlzIh/dh4Ps=; b=H+/umYT/uQb0ar9TrZ7ZOo5c588pq8gFCQlyE60fTc2wkAbBo6wmAk5MJ7Qmfd1WAY s38beirdjmQlm9Ns6gATu8o817JJb6Rv4kH27dcs+XwyoH7h1ZBD1YdB2OWLwfiWshtb pXzUzpV25hwRI0QGJhfJ/ggF06NFNK3E8kUm0vPEVoxkPh57BsHUxwIxJgqPLM5YzM4g POv2ZjMtEq0KsDl9KDOf0QD8SieislPNTmZmmKuucEYL8IFPovARfexTRB6ZovMGYHFZ qsYlta416U6Gx815AMdn4EHg8wfZDCYYgxANmUC44T1Zp50RaHiI7NsG1lLyWsUNLf5w cSFw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=aDUfRt60; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id j23-20020a37ef17000000b006fabaf6b03bsi2072899qkk.676.2022.12.24.07.19.11 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sat, 24 Dec 2022 07:19:12 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=aDUfRt60; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1p96I9-00056t-NV; Sat, 24 Dec 2022 10:18:29 -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 1p96I7-00056H-QE for qemu-devel@nongnu.org; Sat, 24 Dec 2022 10:18:27 -0500 Received: from mail-pf1-x42c.google.com ([2607:f8b0:4864:20::42c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1p96I6-00016e-4f for qemu-devel@nongnu.org; Sat, 24 Dec 2022 10:18:27 -0500 Received: by mail-pf1-x42c.google.com with SMTP id a14so4996047pfa.1 for ; Sat, 24 Dec 2022 07:18:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; 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=3ZdTKdjXZPsSu57R08aDOC5nHrf6U05QdlzIh/dh4Ps=; b=aDUfRt60uiyPxMbkGC3D0P9U7IT90che4HNEepZo/fMsGQAEyFZgM4EFkOOuDlV3BQ H1PJgpXz7+oUM+S17BszscIkYuFh0GqxSOD4ynhVZIvcra+4cmMzb1CY4p9qNJsMXCGx 8pk0e3hhE5YqjDOk69AewG5vTdI8WtgaKhKykEjWj2knjh89AteKA6pa6Sx52qUOxfzc SEVTKxiAocrBdsR1pgptHxPm12FRP5E9qAXe7F9BgdxrAPZmfwImYxfVsX8p8qsj3wnX zVX4b081Vmdwv8cQCl8Nwagn0yE6vQDwV+Q0z1XydaQs5NUjUnRB1Hj/I+HT2Fsf0VtO eQHg== 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=3ZdTKdjXZPsSu57R08aDOC5nHrf6U05QdlzIh/dh4Ps=; b=BxNcAI4oycqIUMV8BvRg6YxEz2bsN03db2ipx/L2ON2K6YQ/tpGkW8xUB7KrwdzI/l KV3uISFFcgrxcdFsazntrQDPyX7gBHtd4UwS8nTIMWzG5zVm+a7ec+i02IBJFk+Nqhdg orl65HzNfVKDsf9gwq2l0RAcWOUGlmFB4MHTvpP3Dfftw/X4fI36/NoCeLy2eCnQbWRy /Is4HnaFIhHfGQrQ1RApR5cyt9b6IE73Ec9lyuwt2qmKu8z9HHM+L+BDlKIrHVCAEHW7 6GenS7IzgRrZim765dYHzHsil6qe+agP14eWvRYSjcnx/tDYDbpRi0EvxuhkYSBTCLjZ GFlg== X-Gm-Message-State: AFqh2kpPHMsCC9yM/P0Rqbt9hEcnn8GHJPkle3Hg8q5Jqudpd8/wQBYf 1y2zSL4rd4f7LSyrzyyMDXgC/h4xGOTdyGzg X-Received: by 2002:aa7:83d1:0:b0:580:d71e:a2e5 with SMTP id j17-20020aa783d1000000b00580d71ea2e5mr3089327pfn.22.1671895104676; Sat, 24 Dec 2022 07:18:24 -0800 (PST) Received: from stoup.. (76-14-210-194.or.wavecable.com. [76.14.210.194]) by smtp.gmail.com with ESMTPSA id t64-20020a625f43000000b005774f19b41csm4231124pfb.88.2022.12.24.07.18.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 24 Dec 2022 07:18:24 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Cc: iii@linux.ibm.com Subject: [PATCH 2/4] accel/tcg: Use g_free_rcu for user-exec interval trees Date: Sat, 24 Dec 2022 07:18:19 -0800 Message-Id: <20221224151821.464455-3-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221224151821.464455-1-richard.henderson@linaro.org> References: <20221224151821.464455-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::42c; envelope-from=richard.henderson@linaro.org; helo=mail-pf1-x42c.google.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, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, 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+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org Because we allow lockless lookups, we have to be careful when it is freed. Use rcu to delay the free until safe. Signed-off-by: Richard Henderson Reviewed-by: Philippe Mathieu-Daudé --- accel/tcg/user-exec.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/accel/tcg/user-exec.c b/accel/tcg/user-exec.c index a3cecda405..2c5c10d2e6 100644 --- a/accel/tcg/user-exec.c +++ b/accel/tcg/user-exec.c @@ -22,6 +22,7 @@ #include "exec/exec-all.h" #include "tcg/tcg.h" #include "qemu/bitops.h" +#include "qemu/rcu.h" #include "exec/cpu_ldst.h" #include "exec/translate-all.h" #include "exec/helper-proto.h" @@ -136,6 +137,7 @@ bool handle_sigsegv_accerr_write(CPUState *cpu, sigset_t *old_set, } typedef struct PageFlagsNode { + struct rcu_head rcu; IntervalTreeNode itree; int flags; } PageFlagsNode; @@ -266,7 +268,7 @@ static bool pageflags_unset(target_ulong start, target_ulong last) } } else if (p_last <= last) { /* Range completely covers node -- remove it. */ - g_free(p); + g_free_rcu(p, rcu); } else { /* Truncate the node from the start. */ p->itree.start = last + 1; @@ -311,7 +313,7 @@ static void pageflags_create_merge(target_ulong start, target_ulong last, if (prev) { if (next) { prev->itree.last = next->itree.last; - g_free(next); + g_free_rcu(next, rcu); } else { prev->itree.last = last; } @@ -376,7 +378,7 @@ static bool pageflags_set_clear(target_ulong start, target_ulong last, p->flags = merge_flags; } else { interval_tree_remove(&p->itree, &pageflags_root); - g_free(p); + g_free_rcu(p, rcu); } goto done; } @@ -421,7 +423,7 @@ static bool pageflags_set_clear(target_ulong start, target_ulong last, p->flags = merge_flags; } else { interval_tree_remove(&p->itree, &pageflags_root); - g_free(p); + g_free_rcu(p, rcu); } if (p_last < last) { start = p_last + 1; @@ -462,7 +464,7 @@ static bool pageflags_set_clear(target_ulong start, target_ulong last, p->itree.start = last + 1; interval_tree_insert(&p->itree, &pageflags_root); } else { - g_free(p); + g_free_rcu(p, rcu); goto restart; } if (set_flags) { @@ -779,6 +781,7 @@ tb_page_addr_t get_page_addr_code_hostp(CPUArchState *env, target_ulong addr, #define TBD_MASK (TARGET_PAGE_MASK * TPD_PAGES) typedef struct TargetPageDataNode { + struct rcu_head rcu; IntervalTreeNode itree; char data[TPD_PAGES][TARGET_PAGE_DATA_SIZE] __attribute__((aligned)); } TargetPageDataNode; @@ -801,11 +804,11 @@ void page_reset_target_data(target_ulong start, target_ulong end) n = next, next = next ? interval_tree_iter_next(n, start, last) : NULL) { target_ulong n_start, n_last, p_ofs, p_len; - TargetPageDataNode *t; + TargetPageDataNode *t = container_of(n, TargetPageDataNode, itree); if (n->start >= start && n->last <= last) { interval_tree_remove(n, &targetdata_root); - g_free(n); + g_free_rcu(t, rcu); continue; } @@ -819,7 +822,6 @@ void page_reset_target_data(target_ulong start, target_ulong end) n_last = MIN(last, n->last); p_len = (n_last + 1 - n_start) >> TARGET_PAGE_BITS; - t = container_of(n, TargetPageDataNode, itree); memset(t->data[p_ofs], 0, p_len * TARGET_PAGE_DATA_SIZE); } } From patchwork Sat Dec 24 15:18:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 636601 Delivered-To: patch@linaro.org Received: by 2002:a17:522:c983:b0:460:3032:e3c4 with SMTP id kr3csp779041pvb; Sat, 24 Dec 2022 07:19:39 -0800 (PST) X-Google-Smtp-Source: AMrXdXujWh11s3djCTHZF6g/gMJ7AT2JIXzO3NIGDF6pYuqL2QwveDktyGmd847qvVq18t/C7VH8 X-Received: by 2002:ac8:67c2:0:b0:3a9:6fa7:38a8 with SMTP id r2-20020ac867c2000000b003a96fa738a8mr16295580qtp.66.1671895179510; Sat, 24 Dec 2022 07:19:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671895179; cv=none; d=google.com; s=arc-20160816; b=n1NcNYtkACFG/+5TIOTL16wuncd7dHhOa8XIcXz9tNuLFvY1rm0ks+EJS8xov0fh2g nfUF7pBDocX0WMksM1YT+RgVkV+mGzIg7XNuC0AHz3MXnm0NHinHrNQcWRz7ujvuZiDA MFceIEm1K5G2Tv/1HCnwIdQtVKNhl/K1q2FCr4GqwprZzh0shyNZMV3e1JBIh4VzZaxB Uy8yfUxAWvPq7zoI91Ry/L66vkSqAdDAwz9j4FRJEFr3HpKCVeKrgyqZpjrnvtunNj/L J6bFm+URUKG0OGWI/sUjmHzECY3OvwyqKR0QWyzSfrOR/aTOO0voFvRE7ch6dr9QE0Iy tsMw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=XUu9359dynbQsE7pTFU+n7TagGz84N6z5eJx4KKYfOM=; b=FOXSI4NcMELd5nAyAm2/Cfol/3wJbKOjxb1QTyiknoUOKo37r9AytP+4ZA4u3f6yVL GVtZQN5MOIqyE6JFl79vaG3lNP7DOs0ejKI8wVOHegV9ou8jrDf96mQXMVJkXm6xhahP XXHL3vXAJ69dycX4xiloi22Hmp3xv9HzKabuZ92mw7g3EOpPXkP7k7j46w1T5bUMy1qW gxfvOJrWPAlBxH5wR/2KvcCsLfSlQ2alozP/hOEYtJ8xfPbkQlxPLE35D2ny4jvps4CS QRiC+U2gtrDz2DIQaewgKsl1lBLM4VMF+DjWv1xKp/2HSA/ZhHBnbFl1xqpBAqneEHyw 9G6w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=p90cuqay; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id fe13-20020a05622a4d4d00b003ab6a097fc2si2238409qtb.382.2022.12.24.07.19.39 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sat, 24 Dec 2022 07:19:39 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=p90cuqay; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1p96IA-00057K-AK; Sat, 24 Dec 2022 10:18:30 -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 1p96I8-00056Q-6D for qemu-devel@nongnu.org; Sat, 24 Dec 2022 10:18:28 -0500 Received: from mail-pg1-x531.google.com ([2607:f8b0:4864:20::531]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1p96I6-00014M-Ju for qemu-devel@nongnu.org; Sat, 24 Dec 2022 10:18:27 -0500 Received: by mail-pg1-x531.google.com with SMTP id h192so110570pgc.7 for ; Sat, 24 Dec 2022 07:18:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; 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=XUu9359dynbQsE7pTFU+n7TagGz84N6z5eJx4KKYfOM=; b=p90cuqayz2D8C1fde3coYBN3FrySuRK9VpuL+CBMNb72WE1BoE3ruW5XhKmV9Rt6Be 5ThCYAOUclbmOGBL2keWz5dqIutOk4+XPBdvCBtW5Iqnd0VE8dRrhCUMxWms654eR72I Oy7E9Z7cKXEgqJ+06ZeWt/FrglAWGSk4uaqDXmgDmwAW+bo5W0mcN7FTupPMEVABlKm1 19ywGbecDEXqXfUTJ4kj0S3i/AriHvD1O2eaq0aIFiyZogFDz8aAGdhxO6+TVVgn1ygM KHPrnh2adQJYEi7Vnp67CGjhY5T9/Prw+cc2d+Iq32dd8/N20SZjeQ605oysAlg7BxEv JFQg== 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=XUu9359dynbQsE7pTFU+n7TagGz84N6z5eJx4KKYfOM=; b=K1vqAMzN8IjZs550P9M2QkkgX0TMNAKOenmevunP/G2K0QLdQgti2+/8HDLQEA0xRD 1r50oVCkFfQf8kAqorY1SjWF5lGj1zY0oq0K+QIsAxl3VBWD5+O/+pzYeAxX1IT6kc6W QxpDffDdWtqT9UfHhhxymBviwb0atytm+64UbaiwpDWviKN+XfUr2/Sh6a2Wt3U0W8dM xxqO3BUnqR4BCMC7TKgeauB+C/m5Vexp4yef1CvhAkwvQrL5ORQZj5wBQVJLDZpMEPK9 G0n8LzT37P1xIdpD5jy7itt6SNSuehB0d1tIkxp71YOpsP9hO6Rq/RSFj/pNMEmKthbj RnhA== X-Gm-Message-State: AFqh2koVTeEN7Z8FOz/upbkh3nS1yKCxHriM03Lk9/5vWS8GgzI52Wbu 78s9fwY9MF0VtLTlTL3Gv2y8RGWAl55VmjEX X-Received: by 2002:aa7:9143:0:b0:580:cfbd:3fd3 with SMTP id 3-20020aa79143000000b00580cfbd3fd3mr4557291pfi.26.1671895105737; Sat, 24 Dec 2022 07:18:25 -0800 (PST) Received: from stoup.. (76-14-210-194.or.wavecable.com. [76.14.210.194]) by smtp.gmail.com with ESMTPSA id t64-20020a625f43000000b005774f19b41csm4231124pfb.88.2022.12.24.07.18.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 24 Dec 2022 07:18:25 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Cc: iii@linux.ibm.com Subject: [PATCH 3/4] accel/tcg: Handle false negative lookup in page_check_range Date: Sat, 24 Dec 2022 07:18:20 -0800 Message-Id: <20221224151821.464455-4-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221224151821.464455-1-richard.henderson@linaro.org> References: <20221224151821.464455-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::531; envelope-from=richard.henderson@linaro.org; helo=mail-pg1-x531.google.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, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, 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+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org As in page_get_flags, we need to try again with the mmap lock held if we fail a page lookup. Signed-off-by: Richard Henderson Reviewed-by: Philippe Mathieu-Daudé --- accel/tcg/user-exec.c | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/accel/tcg/user-exec.c b/accel/tcg/user-exec.c index 2c5c10d2e6..c72a806203 100644 --- a/accel/tcg/user-exec.c +++ b/accel/tcg/user-exec.c @@ -525,6 +525,7 @@ void page_set_flags(target_ulong start, target_ulong end, int flags) int page_check_range(target_ulong start, target_ulong len, int flags) { target_ulong last; + int locked, ret; if (len == 0) { return 0; /* trivial length */ @@ -535,42 +536,66 @@ int page_check_range(target_ulong start, target_ulong len, int flags) return -1; /* wrap around */ } + locked = have_mmap_lock(); while (true) { PageFlagsNode *p = pageflags_find(start, last); int missing; if (!p) { - return -1; /* entire region invalid */ + if (!locked) { + /* + * Lockless lookups have false negatives. + * Retry with the lock held. + */ + mmap_lock(); + locked = -1; + p = pageflags_find(start, last); + } + if (!p) { + ret = -1; /* entire region invalid */ + break; + } } if (start < p->itree.start) { - return -1; /* initial bytes invalid */ + ret = -1; /* initial bytes invalid */ + break; } missing = flags & ~p->flags; if (missing & PAGE_READ) { - return -1; /* page not readable */ + ret = -1; /* page not readable */ + break; } if (missing & PAGE_WRITE) { if (!(p->flags & PAGE_WRITE_ORG)) { - return -1; /* page not writable */ + ret = -1; /* page not writable */ + break; } /* Asking about writable, but has been protected: undo. */ if (!page_unprotect(start, 0)) { - return -1; + ret = -1; + break; } /* TODO: page_unprotect should take a range, not a single page. */ if (last - start < TARGET_PAGE_SIZE) { - return 0; /* ok */ + ret = 0; /* ok */ + break; } start += TARGET_PAGE_SIZE; continue; } if (last <= p->itree.last) { - return 0; /* ok */ + ret = 0; /* ok */ + break; } start = p->itree.last + 1; } + + if (locked < 0) { + mmap_unlock(); + } + return ret; } void page_protect(tb_page_addr_t address) From patchwork Sat Dec 24 15:18:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 636599 Delivered-To: patch@linaro.org Received: by 2002:a17:522:c983:b0:460:3032:e3c4 with SMTP id kr3csp778910pvb; Sat, 24 Dec 2022 07:19:14 -0800 (PST) X-Google-Smtp-Source: AMrXdXtdywG46UeQG0K0ztHMS6Bk3HBh1CddcZwHQXj3BfwzPifrer0TzgWCzi/Vj3QUpPM4ugFU X-Received: by 2002:ac8:6e9b:0:b0:3a5:274c:611b with SMTP id c27-20020ac86e9b000000b003a5274c611bmr15801228qtv.62.1671895154209; Sat, 24 Dec 2022 07:19:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671895154; cv=none; d=google.com; s=arc-20160816; b=wxeIyfmhqMRCKNS8i9V2xFZqg/vYpIrwC94HEiCgOPFvtZxxu+Svbh3zdzOfTkQwJL waE04f0bdl1ehvGsu9C+ilyGlUfRNir4UNaXJzAFT1jcdHhQxqu6v3/uCpnZFAFfhkOs XRNjffnNtbOPdN36OXmRV1mP3vU/WqIYA2L0eNqC/Jr/S/zITFAZgQiGax4q77aVKgMf F7nyyx58Nz5jXP16eIkpM/G/Bp9xLpMVhdxiGccrXkPbZwZA7oodBdEtc7AqKf02yQ1J +VThepsk6PuRzunI99Qw0HhlPxTDcaWl6JoaCi4A0nwbx6fKHruiwWzqXn7uYHDUPLen SnYA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=F5ssAZN04Sr56jpLZ2qO5Ks4STp4zpt9vi68yM+h7Z4=; b=b489NloK3gHAhKH3Y/0J8DB1mf0rWYMkBJM2EQHVthGji0stAEu+0o1w5LdF4jRYV8 bg6uMUPU/8qZ+z11V5Sy1JRmnKjufWEa1FvDE1r7ATHXjMVuj1S5Qh/DGcy1mN2HDN/J eJozbqWH+WX8EQvlM7E+n1HxCMeL6POA05EFq1O/mJj6PXR5itGh4zmAiYuwK26dnbpS PDM9936wtnPXIIMFhJe5d5bNO8mn+s6Hq95lvvxT4+LjrmeNoukwa1JKBUzrzbp5pyUa lzUtxE2inPFa2fW72dNhemPcDhIjJslFB7421pO5R/lbRloogSacmwsw2lsWbusm+cwR FpPA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=RAINwKfx; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id bq8-20020a05620a468800b006ce230b6c15si2229874qkb.139.2022.12.24.07.19.14 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sat, 24 Dec 2022 07:19:14 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=RAINwKfx; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1p96IC-00057d-3r; Sat, 24 Dec 2022 10:18:32 -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 1p96IA-00057L-CI for qemu-devel@nongnu.org; Sat, 24 Dec 2022 10:18:30 -0500 Received: from mail-pl1-x636.google.com ([2607:f8b0:4864:20::636]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1p96I8-00017M-91 for qemu-devel@nongnu.org; Sat, 24 Dec 2022 10:18:30 -0500 Received: by mail-pl1-x636.google.com with SMTP id b2so7367680pld.7 for ; Sat, 24 Dec 2022 07:18:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; 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=F5ssAZN04Sr56jpLZ2qO5Ks4STp4zpt9vi68yM+h7Z4=; b=RAINwKfxv0lqznXvfAq4yUrDCXL98iH/8EjqYRF6qdNgSSDvimdzvhFdLjRpUfAahc vV2yHSAeoHXqjXok0yGEUc84ZmywJfy+xuekDZcvaRB17SMBUdHZ2JGruQE9CtVYTyqk LOuws7DezhpZ9AaLYCnzFalVxV+VNMOeoSl/59FByBw7zpe6esbmRLarWW1JL5bMDDeN AMP9fush2oIxg+DDLllZ7tbp3xnB1SGmFm2sKPUZsqIhnjmMMzr1xU9hvRonVPWguvpO DW2p5Tf9SqmSrhSpfRuWMoi2sluuJ56MtseGaBlHRbO3r6XYimN1CgkLPkdCzbEV56LG 4LjQ== 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=F5ssAZN04Sr56jpLZ2qO5Ks4STp4zpt9vi68yM+h7Z4=; b=vDQkC1UWQf44nKaR70cNGi81pvTSEWu09onn85ziGAh7rTlhStz58/xFuuWnGl/sc7 Qrypy8b0eT59WCcdbX7Z3o94mXCGxFYxWR8VdtbyIDal9r47XjbjHtF1s+sB/c9R19K9 yEKSYTgS7KAGQHQd6pq9gx/mA1OWgYEnjDmiWe8DN50I/MZC3xDITbkTTvSFyJ+R2tZm DVNYRZatq+3jWxSaZouapQpZhTC8hScAEk4g3xZhzUS6l5RiIs3DYi+8SJfQLuwej6JT XYZraUvSy8R8hTsig8aeDtoGCNA6D92OveX1/FVqXu7ShCA5J+k4aNDuYqAK5r/gW6vh wC1g== X-Gm-Message-State: AFqh2kp6LXnwWcc+d64COpX5z1hsajELGRygdMnWMq+98nFj02bPtbLB 8a9rveMBaZF4Q3mpsH71XWylBhA/1MV+4RP3 X-Received: by 2002:a05:6a20:2a9f:b0:a4:ac45:8a68 with SMTP id v31-20020a056a202a9f00b000a4ac458a68mr20752006pzh.11.1671895106734; Sat, 24 Dec 2022 07:18:26 -0800 (PST) Received: from stoup.. (76-14-210-194.or.wavecable.com. [76.14.210.194]) by smtp.gmail.com with ESMTPSA id t64-20020a625f43000000b005774f19b41csm4231124pfb.88.2022.12.24.07.18.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 24 Dec 2022 07:18:26 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Cc: iii@linux.ibm.com Subject: [PATCH 4/4] tests/tcg/multiarch: add vma-pthread.c Date: Sat, 24 Dec 2022 07:18:21 -0800 Message-Id: <20221224151821.464455-5-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221224151821.464455-1-richard.henderson@linaro.org> References: <20221224151821.464455-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::636; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x636.google.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, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, 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+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org From: Ilya Leoshkevich Add a test that locklessly changes and exercises page protection bits from various threads. This helps catch race conditions in the VMA handling. Signed-off-by: Ilya Leoshkevich Message-Id: <20221223120252.513319-1-iii@linux.ibm.com> Signed-off-by: Richard Henderson Acked-by: Alex Bennée --- tests/tcg/multiarch/nop_func.h | 25 ++++ tests/tcg/multiarch/munmap-pthread.c | 16 +-- tests/tcg/multiarch/vma-pthread.c | 207 +++++++++++++++++++++++++++ tests/tcg/multiarch/Makefile.target | 3 + 4 files changed, 236 insertions(+), 15 deletions(-) create mode 100644 tests/tcg/multiarch/nop_func.h create mode 100644 tests/tcg/multiarch/vma-pthread.c diff --git a/tests/tcg/multiarch/nop_func.h b/tests/tcg/multiarch/nop_func.h new file mode 100644 index 0000000000..f714d21000 --- /dev/null +++ b/tests/tcg/multiarch/nop_func.h @@ -0,0 +1,25 @@ +/* + * No-op functions that can be safely copied. + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ +#ifndef NOP_FUNC_H +#define NOP_FUNC_H + +static const char nop_func[] = { +#if defined(__aarch64__) + 0xc0, 0x03, 0x5f, 0xd6, /* ret */ +#elif defined(__alpha__) + 0x01, 0x80, 0xFA, 0x6B, /* ret */ +#elif defined(__arm__) + 0x1e, 0xff, 0x2f, 0xe1, /* bx lr */ +#elif defined(__riscv) + 0x67, 0x80, 0x00, 0x00, /* ret */ +#elif defined(__s390__) + 0x07, 0xfe, /* br %r14 */ +#elif defined(__i386__) || defined(__x86_64__) + 0xc3, /* ret */ +#endif +}; + +#endif diff --git a/tests/tcg/multiarch/munmap-pthread.c b/tests/tcg/multiarch/munmap-pthread.c index d7143b00d5..1c79005846 100644 --- a/tests/tcg/multiarch/munmap-pthread.c +++ b/tests/tcg/multiarch/munmap-pthread.c @@ -7,21 +7,7 @@ #include #include -static const char nop_func[] = { -#if defined(__aarch64__) - 0xc0, 0x03, 0x5f, 0xd6, /* ret */ -#elif defined(__alpha__) - 0x01, 0x80, 0xFA, 0x6B, /* ret */ -#elif defined(__arm__) - 0x1e, 0xff, 0x2f, 0xe1, /* bx lr */ -#elif defined(__riscv) - 0x67, 0x80, 0x00, 0x00, /* ret */ -#elif defined(__s390__) - 0x07, 0xfe, /* br %r14 */ -#elif defined(__i386__) || defined(__x86_64__) - 0xc3, /* ret */ -#endif -}; +#include "nop_func.h" static void *thread_mmap_munmap(void *arg) { diff --git a/tests/tcg/multiarch/vma-pthread.c b/tests/tcg/multiarch/vma-pthread.c new file mode 100644 index 0000000000..35e63cd6cd --- /dev/null +++ b/tests/tcg/multiarch/vma-pthread.c @@ -0,0 +1,207 @@ +/* + * Test that VMA updates do not race. + * + * SPDX-License-Identifier: GPL-2.0-or-later + * + * Map a contiguous chunk of RWX memory. Split it into 8 equally sized + * regions, each of which is guaranteed to have a certain combination of + * protection bits set. + * + * Reader, writer and executor threads perform the respective operations on + * pages, which are guaranteed to have the respective protection bit set. + * Two mutator threads change the non-fixed protection bits randomly. + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "nop_func.h" + +#define PAGE_IDX_BITS 10 +#define PAGE_COUNT (1 << PAGE_IDX_BITS) +#define PAGE_IDX_MASK (PAGE_COUNT - 1) +#define REGION_IDX_BITS 3 +#define PAGE_IDX_R_MASK (1 << 7) +#define PAGE_IDX_W_MASK (1 << 8) +#define PAGE_IDX_X_MASK (1 << 9) +#define REGION_MASK (PAGE_IDX_R_MASK | PAGE_IDX_W_MASK | PAGE_IDX_X_MASK) +#define PAGES_PER_REGION (1 << (PAGE_IDX_BITS - REGION_IDX_BITS)) + +struct context { + int pagesize; + char *ptr; + int dev_null_fd; + volatile int mutator_count; +}; + +static void *thread_read(void *arg) +{ + struct context *ctx = arg; + ssize_t sret; + size_t i, j; + int ret; + + for (i = 0; ctx->mutator_count; i++) { + char *p; + + j = (i & PAGE_IDX_MASK) | PAGE_IDX_R_MASK; + p = &ctx->ptr[j * ctx->pagesize]; + + /* Read directly. */ + ret = memcmp(p, nop_func, sizeof(nop_func)); + if (ret != 0) { + fprintf(stderr, "fail direct read %p\n", p); + abort(); + } + + /* Read indirectly. */ + sret = write(ctx->dev_null_fd, p, 1); + if (sret != 1) { + if (sret < 0) { + fprintf(stderr, "fail indirect read %p (%m)\n", p); + } else { + fprintf(stderr, "fail indirect read %p (%zd)\n", p, sret); + } + abort(); + } + } + + return NULL; +} + +static void *thread_write(void *arg) +{ + struct context *ctx = arg; + struct timespec *ts; + size_t i, j; + int ret; + + for (i = 0; ctx->mutator_count; i++) { + j = (i & PAGE_IDX_MASK) | PAGE_IDX_W_MASK; + + /* Write directly. */ + memcpy(&ctx->ptr[j * ctx->pagesize], nop_func, sizeof(nop_func)); + + /* Write using a syscall. */ + ts = (struct timespec *)(&ctx->ptr[(j + 1) * ctx->pagesize] - + sizeof(struct timespec)); + ret = clock_gettime(CLOCK_REALTIME, ts); + if (ret != 0) { + fprintf(stderr, "fail indirect write %p (%m)\n", ts); + abort(); + } + } + + return NULL; +} + +static void *thread_execute(void *arg) +{ + struct context *ctx = arg; + size_t i, j; + + for (i = 0; ctx->mutator_count; i++) { + j = (i & PAGE_IDX_MASK) | PAGE_IDX_X_MASK; + ((void(*)(void))&ctx->ptr[j * ctx->pagesize])(); + } + + return NULL; +} + +static void *thread_mutate(void *arg) +{ + size_t i, start_idx, end_idx, page_idx, tmp; + struct context *ctx = arg; + unsigned int seed; + int prot, ret; + + seed = (unsigned int)time(NULL); + for (i = 0; i < 50000; i++) { + start_idx = rand_r(&seed) & PAGE_IDX_MASK; + end_idx = rand_r(&seed) & PAGE_IDX_MASK; + if (start_idx > end_idx) { + tmp = start_idx; + start_idx = end_idx; + end_idx = tmp; + } + prot = rand_r(&seed) & (PROT_READ | PROT_WRITE | PROT_EXEC); + for (page_idx = start_idx & REGION_MASK; page_idx <= end_idx; + page_idx += PAGES_PER_REGION) { + if (page_idx & PAGE_IDX_R_MASK) { + prot |= PROT_READ; + } + if (page_idx & PAGE_IDX_W_MASK) { + /* FIXME: qemu syscalls check for both read+write. */ + prot |= PROT_WRITE | PROT_READ; + } + if (page_idx & PAGE_IDX_X_MASK) { + prot |= PROT_EXEC; + } + } + ret = mprotect(&ctx->ptr[start_idx * ctx->pagesize], + (end_idx - start_idx + 1) * ctx->pagesize, prot); + assert(ret == 0); + } + + __atomic_fetch_sub(&ctx->mutator_count, 1, __ATOMIC_SEQ_CST); + + return NULL; +} + +int main(void) +{ + pthread_t threads[5]; + struct context ctx; + size_t i; + int ret; + + /* Without a template, nothing to test. */ + if (sizeof(nop_func) == 0) { + return EXIT_SUCCESS; + } + + /* Initialize memory chunk. */ + ctx.pagesize = getpagesize(); + ctx.ptr = mmap(NULL, PAGE_COUNT * ctx.pagesize, + PROT_READ | PROT_WRITE | PROT_EXEC, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + assert(ctx.ptr != MAP_FAILED); + for (i = 0; i < PAGE_COUNT; i++) { + memcpy(&ctx.ptr[i * ctx.pagesize], nop_func, sizeof(nop_func)); + } + ctx.dev_null_fd = open("/dev/null", O_WRONLY); + assert(ctx.dev_null_fd >= 0); + ctx.mutator_count = 2; + + /* Start threads. */ + ret = pthread_create(&threads[0], NULL, thread_read, &ctx); + assert(ret == 0); + ret = pthread_create(&threads[1], NULL, thread_write, &ctx); + assert(ret == 0); + ret = pthread_create(&threads[2], NULL, thread_execute, &ctx); + assert(ret == 0); + for (i = 3; i <= 4; i++) { + ret = pthread_create(&threads[i], NULL, thread_mutate, &ctx); + assert(ret == 0); + } + + /* Wait for threads to stop. */ + for (i = 0; i < sizeof(threads) / sizeof(threads[0]); i++) { + ret = pthread_join(threads[i], NULL); + assert(ret == 0); + } + + /* Destroy memory chunk. */ + ret = close(ctx.dev_null_fd); + assert(ret == 0); + ret = munmap(ctx.ptr, PAGE_COUNT * ctx.pagesize); + assert(ret == 0); + + return EXIT_SUCCESS; +} diff --git a/tests/tcg/multiarch/Makefile.target b/tests/tcg/multiarch/Makefile.target index 5f0fee1aad..e7213af492 100644 --- a/tests/tcg/multiarch/Makefile.target +++ b/tests/tcg/multiarch/Makefile.target @@ -39,6 +39,9 @@ signals: LDFLAGS+=-lrt -lpthread munmap-pthread: CFLAGS+=-pthread munmap-pthread: LDFLAGS+=-pthread +vma-pthread: CFLAGS+=-pthread +vma-pthread: LDFLAGS+=-pthread + # We define the runner for test-mmap after the individual # architectures have defined their supported pages sizes. If no # additional page sizes are defined we only run the default test.