From patchwork Sat Mar 20 13:36:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alex_Benn=C3=A9e?= X-Patchwork-Id: 405439 Delivered-To: patch@linaro.org Received: by 2002:a02:8562:0:0:0:0:0 with SMTP id g89csp2189757jai; Sat, 20 Mar 2021 06:44:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw3lRZ7JIGvDJR8Pg/jUzRE9/uTwqEi91TmbkR74/6bz8SIdui7vKEueE1CWTsSuW5Qr463 X-Received: by 2002:a25:25d7:: with SMTP id l206mr12838144ybl.43.1616247840918; Sat, 20 Mar 2021 06:44:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1616247840; cv=none; d=google.com; s=arc-20160816; b=jKyCTABcLjvTyOLj6lMHOlC0Y9qkxgroXQ1WRJyqCCdjQAjkF75OSB5R9RMFO0mQJN VTgU4lJovUdX41X2+3VWdHjYRKReePlFibp3FD0MMvYhyRJm4pF2+QpeQnNIRz3N9ZkW q9xfyU2iyR0KUTs83GJP2uow38v7WYQjhDtvyjmu+ub/D0cVPyuRTw7S8Q8PQqSDd4wz cofS6lfFOYY8RcQ3lMmBKk7OAkx9VkXP5qChKYLwN8tPc8mfranSIxqh7bkZEBfU4uQI UJy7iVpmnaqthJRsP8jKCeA1awSsuKEs6GfbD0kIKxbMhUqZm7uyzuPruVCEqy/lMDmE n/YA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc: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:to:from :dkim-signature; bh=IM1oBayvdunRojoA3Pc9uJPF3Nk+3/AuR0/Svwaw3qQ=; b=RgtomkjVQg+Hv6Jr+uBJ3yIi1BJt364U4pBb9weV4kWWNNNxTTMXArI6Q1JdVXGn5b kqRd63rsxcQaSUQbXN5K3eBeJIUKI8cusDbvWCMdjpxPEUkUi5mqaajjTncULSq+Tcvz Lw4Bfb9HA8srbkT9OxH73qs6i9TGEN/nke3jBnMZato/UttF2AyZrrijPIsKCAEwYMBB YeJY9NbhxIOnktp4ICjaFhCiUJLcI843Ts+bnFeUkKh75F++QK6kWbrmBDiueASYrp7v d/Zg0fs3DOfYfcMRwHKLWE2tghLJPgKOwPPEKpbh9N0BwklTKCL0RAJFMAl7EK4cT/s5 rVJw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=FMVl8+T8; 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 g36si5239302ybj.495.2021.03.20.06.44.00 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sat, 20 Mar 2021 06:44:00 -0700 (PDT) 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=FMVl8+T8; 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]:34010 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lNbtY-0001fh-Bd for patch@linaro.org; Sat, 20 Mar 2021 09:44:00 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:59322) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lNbn6-00035m-UA for qemu-devel@nongnu.org; Sat, 20 Mar 2021 09:37:20 -0400 Received: from mail-ed1-x535.google.com ([2a00:1450:4864:20::535]:41715) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lNbn0-0004Eo-1g for qemu-devel@nongnu.org; Sat, 20 Mar 2021 09:37:20 -0400 Received: by mail-ed1-x535.google.com with SMTP id z1so14055211edb.8 for ; Sat, 20 Mar 2021 06:37:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=IM1oBayvdunRojoA3Pc9uJPF3Nk+3/AuR0/Svwaw3qQ=; b=FMVl8+T8V14SRgiP193wldryBjbcHXTun8kfuvY17B9vv4WL2II/NLqmjeZV0vpCiz mMjH/xFGzkpTqvtx8Gwlv1oeZU52xPmHFjRh9Qi9Ud5usTz5sK6n/2YoQbUHe/UkUkwP hps8JJAKlwHWOjwYrM/XputGzoWtzx8uYTA8f5EZq2Yes0B0mp4gJz8x5rNh0PNB0LBg oj121kCwN0a7i5g9WyGxivOpPaaqcZ1ikmkzKnylGWSi5YShZoyi7VORRjkZguFkW/7r TfgMtGGHLsZEN1r7ChpDov8FqKIO6Q2aJvvwiniXP/euN9f/Doqdy20sVgpbLoUTh5zL HN5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=IM1oBayvdunRojoA3Pc9uJPF3Nk+3/AuR0/Svwaw3qQ=; b=f61KLt7FCzqKHv9hA4dFU8lpdL0EMBxYcGJyvHntxfAehtjC2eb72CoZO7u2hSLPQ0 XQPXUn5Ds2qkL2SY0WB31niGg/3zn6BSi+xEQgwtBU5Lm3QmWUAZ+UNr2MMlSGC9aS5s Qi+zkR2TGwicyHe7fVM9MK23CRrMaFBxZyGOH2PY8X5iTftJAu0IVb8zcDiL4VJoaZ1d a+mZTMCHLPTl4rsFkGCIQNxSn7TfVrpPXeWHU8Fc/Stn16yOAz+09ihFEUS4xdzrHTY0 GvTvU7Ym2kZA0HOJMHQv+2ojGxc2TFcOjPovZWTFYqIdci7573Q7EbQRV/vFiYoSNI8v sBtg== X-Gm-Message-State: AOAM532ApdKKuUaJZ/grMHw1IKuww3e8OTJw3Ik+Je6rtxw1E0n7Uqin O2ryd0xmVMVWRRp9K/EikXTQ779YPGJNM/xQ X-Received: by 2002:a05:6402:5255:: with SMTP id t21mr15585751edd.91.1616247431818; Sat, 20 Mar 2021 06:37:11 -0700 (PDT) Received: from zen.linaroharston ([51.148.130.216]) by smtp.gmail.com with ESMTPSA id z9sm6395435edr.75.2021.03.20.06.37.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 20 Mar 2021 06:37:09 -0700 (PDT) Received: from zen.lan (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id 0010D1FF8F; Sat, 20 Mar 2021 13:37:06 +0000 (GMT) From: =?utf-8?q?Alex_Benn=C3=A9e?= To: qemu-devel@nongnu.org Subject: [PATCH v1 03/14] docs/devel: expand style section of memory management Date: Sat, 20 Mar 2021 13:36:55 +0000 Message-Id: <20210320133706.21475-4-alex.bennee@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210320133706.21475-1-alex.bennee@linaro.org> References: <20210320133706.21475-1-alex.bennee@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::535; envelope-from=alex.bennee@linaro.org; helo=mail-ed1-x535.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.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: =?utf-8?q?Alex_Benn=C3=A9e?= Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" This aims to provide a bit more guidance for those who take on one of our "clean up memory allocation" bite-sized tasks. Signed-off-by: Alex Bennée --- docs/devel/style.rst | 46 ++++++++++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 12 deletions(-) -- 2.20.1 Reviewed-by: Richard Henderson diff --git a/docs/devel/style.rst b/docs/devel/style.rst index 8b0bdb3570..260e3263fa 100644 --- a/docs/devel/style.rst +++ b/docs/devel/style.rst @@ -385,17 +385,37 @@ avoided. Low level memory management =========================== -Use of the malloc/free/realloc/calloc/valloc/memalign/posix_memalign +Use of the ``malloc/free/realloc/calloc/valloc/memalign/posix_memalign`` APIs is not allowed in the QEMU codebase. Instead of these routines, -use the GLib memory allocation routines g_malloc/g_malloc0/g_new/ -g_new0/g_realloc/g_free or QEMU's qemu_memalign/qemu_blockalign/qemu_vfree -APIs. - -Please note that g_malloc will exit on allocation failure, so there -is no need to test for failure (as you would have to with malloc). -Calling g_malloc with a zero size is valid and will return NULL. - -Prefer g_new(T, n) instead of g_malloc(sizeof(T) ``*`` n) for the following +use the GLib memory allocation routines +``g_malloc/g_malloc0/g_new/g_new0/g_realloc/g_free`` +or QEMU's ``qemu_memalign/qemu_blockalign/qemu_vfree`` APIs. + +Please note that ``g_malloc`` will exit on allocation failure, so +there is no need to test for failure (as you would have to with +``malloc``). Generally using ``g_malloc`` on start-up is fine as the +result of a failure to allocate memory is going to be a fatal exit +anyway. There may be some start-up cases where failing is unreasonable +(for example speculatively loading a large debug symbol table). + +Care should be taken to avoid introducing places where the guest could +trigger an exit by causing a large allocation. For small allocations, +of the order of 4k, a failure to allocate is likely indicative of an +overloaded host and allowing ``g_malloc`` to ``exit`` is a reasonable +approach. However for larger allocations where we could realistically +fall-back to a smaller one if need be we should use functions like +``g_try_new`` and check the result. For example this is valid approach +for a time/space trade-off like ``tlb_mmu_resize_locked`` in the +SoftMMU TLB code. + +If the lifetime of the allocation is within the function and there are +multiple exist paths you can also improve the readability of the code +by using ``g_autofree`` and related annotations. See :ref:`autofree-ref` +for more details. + +Calling ``g_malloc`` with a zero size is valid and will return NULL. + +Prefer ``g_new(T, n)`` instead of ``g_malloc(sizeof(T) * n)`` for the following reasons: * It catches multiplication overflowing size_t; @@ -409,8 +429,8 @@ Declarations like are acceptable, though. -Memory allocated by qemu_memalign or qemu_blockalign must be freed with -qemu_vfree, since breaking this will cause problems on Win32. +Memory allocated by ``qemu_memalign`` or ``qemu_blockalign`` must be freed with +``qemu_vfree``, since breaking this will cause problems on Win32. String manipulation =================== @@ -485,6 +505,8 @@ In addition, QEMU assumes that the compiler does not use the latitude given in C99 and C11 to treat aspects of signed '<<' as undefined, as documented in the GNU Compiler Collection manual starting at version 4.0. +.. _autofree-ref: + Automatic memory deallocation =============================