Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1is7xL-0005TM-8S for pgsql-hackers@arkaria.postgresql.org; Thu, 16 Jan 2020 16:25:15 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1is7xI-0006ey-Rb for pgsql-hackers@arkaria.postgresql.org; Thu, 16 Jan 2020 16:25:12 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1is7xI-0006er-E5 for pgsql-hackers@lists.postgresql.org; Thu, 16 Jan 2020 16:25:12 +0000 Received: from mail-wr1-x443.google.com ([2a00:1450:4864:20::443]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1is7xD-00066p-AQ for pgsql-hackers@postgresql.org; Thu, 16 Jan 2020 16:25:11 +0000 Received: by mail-wr1-x443.google.com with SMTP id d16so19786714wre.10 for ; Thu, 16 Jan 2020 08:25:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=rDzqlHFFxRFFeTIutH9RlHoTFFhiieOe0eKE78Dbzns=; b=D1ni9IbKwO0PnTga81Bug7tycvigXt/OQUy4D/JV3aUyOnr3BZoHSn+rkAbHN2zTsR 1bbzWwMxqnYE2MMVa0bQ8wRb4L3LFf3rQ+aj9ELoeQX9vPUv7uwGOd1inlMRUV3C/FYX P9MqBhb1cFzrQciRKOIalUt0DVSyMw77FoCROacT9af1uYEdT+AixnNCU65O96aO5TfN IUKNaZWHE96FPmQaTS1jMqNB9zeOfxPqqqIUtivxL2k6GcgNEVPkv0XrzTpmWUksyLz4 soLq5Ii6LI3kFF+BdPzvsiInJPqrVYsvv11YKMh88CmIR4PbH7yuVU0weeBoks8vY27g eE8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=rDzqlHFFxRFFeTIutH9RlHoTFFhiieOe0eKE78Dbzns=; b=rtGRv8TX0cIMciM33ioOBtNq3HxGesBq7+e0pMuc1Mur8CNHYUiauHbFuN0FUen23a qeO/8ubTRvGmKWupZFAL3CmC2QWEvFU4sUZQk8v7Ye74t5M28eqNFmqIQbmiSYFqJSu7 meOP5UfPvN2MEWRuWlCao+iQEPc7Ei7aE/tsLsmf71aUCukPKrBcHmxJvCCnFSv9LxLW 8Vngox4TeKOJk4UKrdm8yU1tpQn1NjHhpKcRnIatF0WTekfQf/7ORDi8b7je2Fe8Ee9j 9N+n1K8AM8QSrzB6yfCv/8KLVdj5lGlDPkg/3Y1RUfu8l/06XNplVDiZh9x+0ScxsOd5 5Kfg== X-Gm-Message-State: APjAAAWUQ7OYJSDEcvynb+27bqBa+Mr90PCNU0Pn4Xxv+g5yC8ZEUS+E pV25U2IXYlrUMP8n/TV3SQ9kkQ== X-Google-Smtp-Source: APXvYqw+ObCaCdAzxshVftbfXKSsDsYfJ7oQLNhw9gK3MZsEMjPEI7A2InTG65YXewLCMdgPLSpICQ== X-Received: by 2002:adf:d0c1:: with SMTP id z1mr4279153wrh.371.1579191905199; Thu, 16 Jan 2020 08:25:05 -0800 (PST) Received: from localhost (static-84-42-175-93.net.upcbroadband.cz. [84.42.175.93]) by smtp.gmail.com with ESMTPSA id h2sm30382747wrv.66.2020.01.16.08.25.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Jan 2020 08:25:04 -0800 (PST) Date: Thu, 16 Jan 2020 17:25:00 +0100 From: Tomas Vondra To: Tom Lane Cc: Andres Freund , Tomas Vondra , pgsql-hackers@postgresql.org Subject: Re: SlabCheck leaks memory into TopMemoryContext Message-ID: <20200116162500.pcgxmykirm677mt6@development> References: <20200116044119.g45f7pmgz4jmodxj@alap3.anarazel.de> <23952.1579151393@sss.pgh.pa.us> <20200116061732.vvpyexenaortpu5y@alap3.anarazel.de> <27213.1579155900@sss.pgh.pa.us> <20200116064143.opcu2vhcktddpzbh@alap3.anarazel.de> <19743.1579188421@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="uzdmsenfrfcozwl4" Content-Disposition: inline In-Reply-To: <19743.1579188421@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --uzdmsenfrfcozwl4 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline On Thu, Jan 16, 2020 at 10:27:01AM -0500, Tom Lane wrote: >Andres Freund writes: >> ... I thought you were asking whether >> any additional memory could just be avoided... > >Well, I was kind of wondering that, but if it's not practical then >preallocating the space instead will do. > I don't think it's practical to rework the checks in a way that would not require allocations. Maybe it's possible, but I think it's not worth the extra complexity. The attached fix should do the trick - it pre-allocates the space when creating the context. There is a bit of complexity because we want to allocate the space as part of the context header, but nothin too bad. We might optimize it a bit by using a regular bitmap (instead of just an array of bools), but I haven't done that. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services --uzdmsenfrfcozwl4 Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="slab-leak-fix.patch" diff --git a/src/backend/utils/mmgr/slab.c b/src/backend/utils/mmgr/slab.c index c5866d9cc3..7f9749a73f 100644 --- a/src/backend/utils/mmgr/slab.c +++ b/src/backend/utils/mmgr/slab.c @@ -70,6 +70,9 @@ typedef struct SlabContext int chunksPerBlock; /* number of chunks per block */ int minFreeChunks; /* min number of free chunks in any block */ int nblocks; /* number of blocks allocated */ +#ifdef MEMORY_CONTEXT_CHECKING + char *freechunks; /* bitmap of free chunks on a block */ +#endif /* blocks with free space, grouped by number of free chunks: */ dlist_head freelist[FLEXIBLE_ARRAY_MEMBER]; } SlabContext; @@ -229,6 +232,15 @@ SlabContextCreate(MemoryContext parent, /* Size of the memory context header */ headerSize = offsetof(SlabContext, freelist) + freelistSize; +#ifdef MEMORY_CONTEXT_CHECKING + /* + * With memory checking, we need to allocate extra space for the bitmap + * of free chunks. The space is allocated at the end, and we need proper + * alignment (it's an array of bools, so maybe MAXALIGN is not needed). + */ + headerSize = MAXALIGN(headerSize) + chunksPerBlock * sizeof(bool); +#endif + slab = (SlabContext *) malloc(headerSize); if (slab == NULL) { @@ -258,6 +270,12 @@ SlabContextCreate(MemoryContext parent, for (i = 0; i < (slab->chunksPerBlock + 1); i++) dlist_init(&slab->freelist[i]); +#ifdef MEMORY_CONTEXT_CHECKING + /* set the freechunks pointer after the freelists array (aligned) */ + slab->freechunks = (char *) slab + + MAXALIGN(offsetof(SlabContext, freelist) + freelistSize); +#endif + /* Finally, do the type-independent part of context creation */ MemoryContextCreate((MemoryContext) slab, T_SlabContext, @@ -701,14 +719,10 @@ SlabCheck(MemoryContext context) int i; SlabContext *slab = castNode(SlabContext, context); const char *name = slab->header.name; - char *freechunks; Assert(slab); Assert(slab->chunksPerBlock > 0); - /* bitmap of free chunks on a block */ - freechunks = palloc(slab->chunksPerBlock * sizeof(bool)); - /* walk all the freelists */ for (i = 0; i <= slab->chunksPerBlock; i++) { @@ -731,7 +745,7 @@ SlabCheck(MemoryContext context) name, block->nfree, block, i); /* reset the bitmap of free chunks for this block */ - memset(freechunks, 0, (slab->chunksPerBlock * sizeof(bool))); + memset(slab->freechunks, 0, (slab->chunksPerBlock * sizeof(bool))); idx = block->firstFreeChunk; /* @@ -748,7 +762,7 @@ SlabCheck(MemoryContext context) /* count the chunk as free, add it to the bitmap */ nfree++; - freechunks[idx] = true; + slab->freechunks[idx] = true; /* read index of the next free chunk */ chunk = SlabBlockGetChunk(slab, block, idx); @@ -759,7 +773,7 @@ SlabCheck(MemoryContext context) for (j = 0; j < slab->chunksPerBlock; j++) { /* non-zero bit in the bitmap means chunk the chunk is used */ - if (!freechunks[j]) + if (!slab->freechunks[j]) { SlabChunk *chunk = SlabBlockGetChunk(slab, block, j); --uzdmsenfrfcozwl4--