From nobody Wed Apr 24 20:05:39 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1562085603; cv=none; d=zoho.com; s=zohoarc; b=czWAFYT1nerRxC382HYvnlFXrzuJLybuW5ZRVfvt1cpByb1SX3ru233MpNJbsWgM99JLzv3nazwnu6i4nFKIcObNfWwCwK0Dsk52rPrQF53yzlQc3UonjNmsizaaResXOlEx2NVw4UW693U9gNKvhjC9CJlDLUbj8aXy7SfF4vo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1562085603; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=ULWXC2MU3Fh47U6zpqbEhXks1AeKDyBQoXyZOtuM5bo=; b=bSBEoZwKoX0K1IJKNJYj6Z93M4Un0CbxSckQOIke/EfFX9AmvI//u2V181stkEb7bS2cKLnkVUtoo87qPSF89aaBPSlv0o+UsUrmKOkuH55eTZZn0nIaoAMDKZbAHT+15Y+c4gXs0Dz6+MS88d+PcpqdSoY8Lumx8OyNgXORalY= ARC-Authentication-Results: i=1; mx.zoho.com; spf=none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1562085602912257.75505082586835; Tue, 2 Jul 2019 09:40:02 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1hiLnu-0001iw-0q; Tue, 02 Jul 2019 16:38:50 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1hiLnt-0001iQ-4s for xen-devel@lists.xenproject.org; Tue, 02 Jul 2019 16:38:49 +0000 Received: from esa2.hc3370-68.iphmx.com (unknown [216.71.145.153]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id dd51bbb4-9ce7-11e9-8f5e-e796baa0a0a8; Tue, 02 Jul 2019 16:38:47 +0000 (UTC) X-Inumbo-ID: dd51bbb4-9ce7-11e9-8f5e-e796baa0a0a8 Authentication-Results: esa2.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none; spf=None smtp.pra=paul.durrant@citrix.com; spf=Pass smtp.mailfrom=Paul.Durrant@citrix.com; spf=None smtp.helo=postmaster@mail.citrix.com Received-SPF: none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Received-SPF: None (esa2.hc3370-68.iphmx.com: no sender authenticity information available from domain of paul.durrant@citrix.com) identity=pra; client-ip=162.221.158.21; receiver=esa2.hc3370-68.iphmx.com; envelope-from="Paul.Durrant@citrix.com"; x-sender="paul.durrant@citrix.com"; x-conformance=sidf_compatible Received-SPF: Pass (esa2.hc3370-68.iphmx.com: domain of Paul.Durrant@citrix.com designates 162.221.158.21 as permitted sender) identity=mailfrom; client-ip=162.221.158.21; receiver=esa2.hc3370-68.iphmx.com; envelope-from="Paul.Durrant@citrix.com"; x-sender="Paul.Durrant@citrix.com"; x-conformance=sidf_compatible; x-record-type="v=spf1"; x-record-text="v=spf1 ip4:209.167.231.154 ip4:178.63.86.133 ip4:195.66.111.40/30 ip4:85.115.9.32/28 ip4:199.102.83.4 ip4:192.28.146.160 ip4:192.28.146.107 ip4:216.52.6.88 ip4:216.52.6.188 ip4:162.221.158.21 ip4:162.221.156.83 ~all" Received-SPF: None (esa2.hc3370-68.iphmx.com: no sender authenticity information available from domain of postmaster@mail.citrix.com) identity=helo; client-ip=162.221.158.21; receiver=esa2.hc3370-68.iphmx.com; envelope-from="Paul.Durrant@citrix.com"; x-sender="postmaster@mail.citrix.com"; x-conformance=sidf_compatible IronPort-SDR: v14isdYEz+3Z7zP9Fvqo2WJDcQ4IDsS4awEXOKQkKY8K14Z+Vy61XFAS9EoH89HlGxrAPhy1BV 1/RdCa35yEppB9ED5zTybYqa1YmTRDXqWNiKgnoXxNRh2KaNNypW9itXGcYFq3GFPCDF3WSeMb sy1oxxY4mLDho8JiWpla/EHw3V9oRXQhVFnbOfdy6tTVgVY2yqkGDtmzbsDaZFBixAlw01sx0k WDpFv5G2OxTG4vvktxngAEIhjqGFtM6ONvRinNEN8j9b2nhgKnmb7fF2EuKif5vdEHayB0Xg+/ yB4= X-SBRS: 2.7 X-MesageID: 2501601 X-Ironport-Server: esa2.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED X-IronPort-AV: E=Sophos;i="5.63,443,1557201600"; d="scan'208";a="2501601" From: Paul Durrant To: Date: Tue, 2 Jul 2019 17:38:38 +0100 Message-ID: <20190702163840.2107-2-paul.durrant@citrix.com> X-Mailer: git-send-email 2.20.1.2.gb21ebb671 In-Reply-To: <20190702163840.2107-1-paul.durrant@citrix.com> References: <20190702163840.2107-1-paul.durrant@citrix.com> MIME-Version: 1.0 Subject: [Xen-devel] [PATCH 1/3] xmalloc: stop using a magic '1' in alignment padding X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Stefano Stabellini , Wei Liu , Konrad Rzeszutek Wilk , George Dunlap , Andrew Cooper , Ian Jackson , Tim Deegan , Julien Grall , Paul Durrant , Jan Beulich Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" Alignment padding inserts a pseudo block header in front of the allocation, sets its size field to the pad size and then ORs in 1, which is equivalent to marking it as a free block, so that xfree() can distinguish it from a real block header. This patch simply replaces the magic '1' with the defined 'FREE_BLOCK' to make it more obvious what's going on. Also, whilst in the neighbourhood, it removes a stray space after a cast. Signed-off-by: Paul Durrant Reviewed-by: Jan Beulich --- Cc: Andrew Cooper Cc: George Dunlap Cc: Ian Jackson Cc: Jan Beulich Cc: Julien Grall Cc: Konrad Rzeszutek Wilk Cc: Stefano Stabellini Cc: Tim Deegan Cc: Wei Liu --- xen/common/xmalloc_tlsf.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/xen/common/xmalloc_tlsf.c b/xen/common/xmalloc_tlsf.c index 2076953ac4..6d889b7bdc 100644 --- a/xen/common/xmalloc_tlsf.c +++ b/xen/common/xmalloc_tlsf.c @@ -595,7 +595,7 @@ void *_xmalloc(unsigned long size, unsigned long align) char *q =3D (char *)p + pad; struct bhdr *b =3D (struct bhdr *)(q - BHDR_OVERHEAD); ASSERT(q > (char *)p); - b->size =3D pad | 1; + b->size =3D pad | FREE_BLOCK; p =3D q; } =20 @@ -638,12 +638,12 @@ void xfree(void *p) } =20 /* Strip alignment padding. */ - b =3D (struct bhdr *)((char *) p - BHDR_OVERHEAD); - if ( b->size & 1 ) + b =3D (struct bhdr *)((char *)p - BHDR_OVERHEAD); + if ( b->size & FREE_BLOCK ) { p =3D (char *)p - (b->size & ~1u); b =3D (struct bhdr *)((char *)p - BHDR_OVERHEAD); - ASSERT(!(b->size & 1)); + ASSERT(!(b->size & FREE_BLOCK)); } =20 xmem_pool_free(p, xenpool); --=20 2.20.1.2.gb21ebb671 _______________________________________________ Xen-devel mailing list Xen-devel@lists.xenproject.org https://lists.xenproject.org/mailman/listinfo/xen-devel From nobody Wed Apr 24 20:05:39 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1562085604; cv=none; d=zoho.com; s=zohoarc; b=ldKEe8rNqIRz01JLsFMM/jL7dVnTH2yweaAXw3+0PKxaDfG018sf9tBiuPofg+NShDh2dqYR6pH7QZSu2HZnPLG54xqVfk+9gq481ymcQJv4739dwSOsTCTHBVr6SX2+lyI/3f1jolsIWbDr0G15NbjkqoGEVtnglVbUkso4+AA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1562085604; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=0V+psFSZXFIwfPNAlHO34kI1cOZOi4UPw3BR3bbvhVU=; b=g2c5lshQvIyBsnf37E5osTDiU1lI9+NlM/el6Db7WjDciWUmNCI5JOmimsOEdOXkLXZE9C4UqYcaQeM6chIQ+QKELbPopJV4ZlLGnls1kaEQumPJHdTC/BQDgtG4Q8/AGhj4uUMLxOykchwxQvzhkTOowpMSlmHpaFuQFP0NGW8= ARC-Authentication-Results: i=1; mx.zoho.com; spf=none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1562085604887209.71293107036433; Tue, 2 Jul 2019 09:40:04 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1hiLnt-0001ig-IH; Tue, 02 Jul 2019 16:38:49 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1hiLnr-0001iH-UO for xen-devel@lists.xenproject.org; Tue, 02 Jul 2019 16:38:47 +0000 Received: from esa2.hc3370-68.iphmx.com (unknown [216.71.145.153]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id dc2bda94-9ce7-11e9-83a6-5349a050a6a1; Tue, 02 Jul 2019 16:38:46 +0000 (UTC) X-Inumbo-ID: dc2bda94-9ce7-11e9-83a6-5349a050a6a1 Authentication-Results: esa2.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none; spf=None smtp.pra=paul.durrant@citrix.com; spf=Pass smtp.mailfrom=Paul.Durrant@citrix.com; spf=None smtp.helo=postmaster@mail.citrix.com Received-SPF: none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Received-SPF: None (esa2.hc3370-68.iphmx.com: no sender authenticity information available from domain of paul.durrant@citrix.com) identity=pra; client-ip=162.221.158.21; receiver=esa2.hc3370-68.iphmx.com; envelope-from="Paul.Durrant@citrix.com"; x-sender="paul.durrant@citrix.com"; x-conformance=sidf_compatible Received-SPF: Pass (esa2.hc3370-68.iphmx.com: domain of Paul.Durrant@citrix.com designates 162.221.158.21 as permitted sender) identity=mailfrom; client-ip=162.221.158.21; receiver=esa2.hc3370-68.iphmx.com; envelope-from="Paul.Durrant@citrix.com"; x-sender="Paul.Durrant@citrix.com"; x-conformance=sidf_compatible; x-record-type="v=spf1"; x-record-text="v=spf1 ip4:209.167.231.154 ip4:178.63.86.133 ip4:195.66.111.40/30 ip4:85.115.9.32/28 ip4:199.102.83.4 ip4:192.28.146.160 ip4:192.28.146.107 ip4:216.52.6.88 ip4:216.52.6.188 ip4:162.221.158.21 ip4:162.221.156.83 ~all" Received-SPF: None (esa2.hc3370-68.iphmx.com: no sender authenticity information available from domain of postmaster@mail.citrix.com) identity=helo; client-ip=162.221.158.21; receiver=esa2.hc3370-68.iphmx.com; envelope-from="Paul.Durrant@citrix.com"; x-sender="postmaster@mail.citrix.com"; x-conformance=sidf_compatible IronPort-SDR: 1HVVlpBeEyqFY8ue3C4/YfjNRVbEb+HZuPKDv/1luvh/ZkjLiWOFyH5ggji4vddcvDj0msDkW3 7/QbVmFO/gyX6b/o/QkytEcYlSztTizZ4hZHsEC7JY9/cFVNNPhiJ+K9zuJKXL8E2HceVYefgY 9JQu06DMojCgLWNNOVYwfwNnkLa0nrFGWEuztZD50UWoQ+EvmZ9rWFa83pD4kLP7WHRFDYFB7v 2spnYLX9nUL6dmRhP/5a9hcVWrrFWZI/X+RbqZSl6fpISdhgv2SH/9WV1Ph1sj3/QhhorMPN4y NIU= X-SBRS: 2.7 X-MesageID: 2501599 X-Ironport-Server: esa2.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED X-IronPort-AV: E=Sophos;i="5.63,443,1557201600"; d="scan'208";a="2501599" From: Paul Durrant To: Date: Tue, 2 Jul 2019 17:38:39 +0100 Message-ID: <20190702163840.2107-3-paul.durrant@citrix.com> X-Mailer: git-send-email 2.20.1.2.gb21ebb671 In-Reply-To: <20190702163840.2107-1-paul.durrant@citrix.com> References: <20190702163840.2107-1-paul.durrant@citrix.com> MIME-Version: 1.0 Subject: [Xen-devel] [PATCH 2/3] xmalloc: don't evaluate ADD_REGION without holding the pool lock X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Stefano Stabellini , Wei Liu , Konrad Rzeszutek Wilk , George Dunlap , Andrew Cooper , Ian Jackson , Tim Deegan , Julien Grall , Paul Durrant , Jan Beulich Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" It's not safe to add a region without holding the lock, but this is exactly what may happen if two threads race entering xmem_pool_alloc() before the init_region is set. This patch instead checks for init_region under lock, drops the lock if it needs to allocate a page, takes the lock, adds the region and then confirms init_region is still unset before pointing it at the newly added region. Thus, it is possible that a race may cause an extra region to be added, but there will be no pool metadata corruption. Signed-off-by: Paul Durrant --- Cc: Andrew Cooper Cc: George Dunlap Cc: Ian Jackson Cc: Jan Beulich Cc: Julien Grall Cc: Konrad Rzeszutek Wilk Cc: Stefano Stabellini Cc: Tim Deegan Cc: Wei Liu --- xen/common/xmalloc_tlsf.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/xen/common/xmalloc_tlsf.c b/xen/common/xmalloc_tlsf.c index 6d889b7bdc..71597c3590 100644 --- a/xen/common/xmalloc_tlsf.c +++ b/xen/common/xmalloc_tlsf.c @@ -380,18 +380,22 @@ void *xmem_pool_alloc(unsigned long size, struct xmem= _pool *pool) int fl, sl; unsigned long tmp_size; =20 + spin_lock(&pool->lock); if ( pool->init_region =3D=3D NULL ) { + spin_unlock(&pool->lock); if ( (region =3D pool->get_mem(pool->init_size)) =3D=3D NULL ) goto out; + spin_lock(&pool->lock); ADD_REGION(region, pool->init_size, pool); - pool->init_region =3D region; + /* Re-check since the lock was dropped */ + if ( pool->init_region =3D=3D NULL ) + pool->init_region =3D region; } =20 size =3D (size < MIN_BLOCK_SIZE) ? MIN_BLOCK_SIZE : ROUNDUP_SIZE(size); /* Rounding up the requested size and calculating fl and sl */ =20 - spin_lock(&pool->lock); retry_find: MAPPING_SEARCH(&size, &fl, &sl); =20 --=20 2.20.1.2.gb21ebb671 _______________________________________________ Xen-devel mailing list Xen-devel@lists.xenproject.org https://lists.xenproject.org/mailman/listinfo/xen-devel From nobody Wed Apr 24 20:05:39 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org ARC-Seal: i=1; a=rsa-sha256; t=1562085611; cv=none; d=zoho.com; s=zohoarc; b=XY/uiJ/ulXY3PPS4WJ3I2gze/aulaakOIeiMmm9gDB6J0xOQtrfkc2Wn/62kSFYBDcgMlkqZzN+azYegYom8f4ZaEIzKUfCxX2zZRDloDxmKtuL6Ar+0bD8BCj6gB4i8pH+3DfStpoUq0pBhBfSR6nUKqLpOvN2GO/C1Hvfq93Y= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1562085611; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=RWGgylFc4vaml4NrsH+QO6q+6aRKzHLSMjmSdcNedqg=; b=CLSMCmGSVu0GEgIEKgvTDMfjM4VH4hCinmK5ZcOg725V25C3WN58J3sx/22EJfL/EJbmR5UFquSoouZaji0tZPl6w8dpYGQaS7lgim7ouCTy+mOtzeuFO/fIziN1N82w2E8cRWoOuO3m/TnkmQZLkQNiLyVDg8VHLhsx8epUwV0= ARC-Authentication-Results: i=1; mx.zoho.com; spf=none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 15620856108631021.9344316498457; Tue, 2 Jul 2019 09:40:10 -0700 (PDT) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1hiLnu-0001j6-BL; Tue, 02 Jul 2019 16:38:50 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1hiLnt-0001iR-51 for xen-devel@lists.xenproject.org; Tue, 02 Jul 2019 16:38:49 +0000 Received: from esa2.hc3370-68.iphmx.com (unknown [216.71.145.153]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id dd53dc5a-9ce7-11e9-8782-4f699680660f; Tue, 02 Jul 2019 16:38:47 +0000 (UTC) X-Inumbo-ID: dd53dc5a-9ce7-11e9-8782-4f699680660f Authentication-Results: esa2.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none; spf=None smtp.pra=paul.durrant@citrix.com; spf=Pass smtp.mailfrom=Paul.Durrant@citrix.com; spf=None smtp.helo=postmaster@mail.citrix.com Received-SPF: none (zoho.com: 192.237.175.120 is neither permitted nor denied by domain of lists.xenproject.org) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Received-SPF: None (esa2.hc3370-68.iphmx.com: no sender authenticity information available from domain of paul.durrant@citrix.com) identity=pra; client-ip=162.221.158.21; receiver=esa2.hc3370-68.iphmx.com; envelope-from="Paul.Durrant@citrix.com"; x-sender="paul.durrant@citrix.com"; x-conformance=sidf_compatible Received-SPF: Pass (esa2.hc3370-68.iphmx.com: domain of Paul.Durrant@citrix.com designates 162.221.158.21 as permitted sender) identity=mailfrom; client-ip=162.221.158.21; receiver=esa2.hc3370-68.iphmx.com; envelope-from="Paul.Durrant@citrix.com"; x-sender="Paul.Durrant@citrix.com"; x-conformance=sidf_compatible; x-record-type="v=spf1"; x-record-text="v=spf1 ip4:209.167.231.154 ip4:178.63.86.133 ip4:195.66.111.40/30 ip4:85.115.9.32/28 ip4:199.102.83.4 ip4:192.28.146.160 ip4:192.28.146.107 ip4:216.52.6.88 ip4:216.52.6.188 ip4:162.221.158.21 ip4:162.221.156.83 ~all" Received-SPF: None (esa2.hc3370-68.iphmx.com: no sender authenticity information available from domain of postmaster@mail.citrix.com) identity=helo; client-ip=162.221.158.21; receiver=esa2.hc3370-68.iphmx.com; envelope-from="Paul.Durrant@citrix.com"; x-sender="postmaster@mail.citrix.com"; x-conformance=sidf_compatible IronPort-SDR: P8DlWrgjUXbq7h/Aj4uxKaFCre30NiQS6MFXQte0CQZqPBpzhWoZlO0JVidhnti2AJ1dYmeBAO YgJp75ZLQL5bmOzY1A+Jd1XnD6PztcVEleS4EEdGNF0IgRP2gjx9pcy/U2fm6lpA7A81QRsRgw g4kH5vA3j4jJWjmkyX+fqJ+3tP54rxR5orEJZchqfJXhybqxpvFfNESmLSfniIRw7U4xZ+I2Gy ZX22nkQz5CLqmiAc0dBWlHzDp5pxNnvlQSES3FDw/758w1kpsXL5QgQXAHZtFtf/kQh6vEvL0R JZU= X-SBRS: 2.7 X-MesageID: 2501602 X-Ironport-Server: esa2.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED X-IronPort-AV: E=Sophos;i="5.63,443,1557201600"; d="scan'208";a="2501602" From: Paul Durrant To: Date: Tue, 2 Jul 2019 17:38:40 +0100 Message-ID: <20190702163840.2107-4-paul.durrant@citrix.com> X-Mailer: git-send-email 2.20.1.2.gb21ebb671 In-Reply-To: <20190702163840.2107-1-paul.durrant@citrix.com> References: <20190702163840.2107-1-paul.durrant@citrix.com> MIME-Version: 1.0 Subject: [Xen-devel] [PATCH 3/3] xmalloc: add a Kconfig option to poison free pool memory X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Stefano Stabellini , Wei Liu , Konrad Rzeszutek Wilk , George Dunlap , Andrew Cooper , Ian Jackson , Tim Deegan , Julien Grall , Paul Durrant , Jan Beulich Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" This patch adds POOL_POISON to the Kconfig DEBUG options. If set, free blocks (greater than MIN_BLOCK_SIZE) will be poisoned with 0xAA bytes which will then be verified when memory is subsequently allocated. This can help in spotting heap corruption, particularly use-after-free. Signed-off-by: Paul Durrant --- Cc: Andrew Cooper Cc: George Dunlap Cc: Ian Jackson Cc: Jan Beulich Cc: Julien Grall Cc: Konrad Rzeszutek Wilk Cc: Stefano Stabellini Cc: Tim Deegan Cc: Wei Liu --- xen/Kconfig.debug | 7 +++++++ xen/common/xmalloc_tlsf.c | 13 +++++++++++++ 2 files changed, 20 insertions(+) diff --git a/xen/Kconfig.debug b/xen/Kconfig.debug index daacf85141..4f18a1144e 100644 --- a/xen/Kconfig.debug +++ b/xen/Kconfig.debug @@ -105,6 +105,13 @@ config DEBUG_TRACE either directly to the console or are printed to console in case of a system crash. =20 +config POOL_POISON + bool "Poison free xenpool blocks" + default DEBUG + ---help--- + Poison free blocks with 0xAA bytes and verify them when a block is + allocated in order to spot use-after-free issues. + endif # DEBUG || EXPERT =20 endmenu diff --git a/xen/common/xmalloc_tlsf.c b/xen/common/xmalloc_tlsf.c index 71597c3590..a12dbc8e11 100644 --- a/xen/common/xmalloc_tlsf.c +++ b/xen/common/xmalloc_tlsf.c @@ -223,6 +223,10 @@ static inline void EXTRACT_BLOCK_HDR(struct bhdr *b, s= truct xmem_pool *p, int fl static inline void EXTRACT_BLOCK(struct bhdr *b, struct xmem_pool *p, int = fl, int sl) { +#ifdef CONFIG_POOL_POISON + unsigned int i; +#endif /* CONFIG_POOL_POISON */ + if ( b->ptr.free_ptr.next ) b->ptr.free_ptr.next->ptr.free_ptr.prev =3D b->ptr.free_ptr.prev; @@ -240,6 +244,10 @@ static inline void EXTRACT_BLOCK(struct bhdr *b, struc= t xmem_pool *p, int fl, } } b->ptr.free_ptr =3D (struct free_ptr) {NULL, NULL}; +#ifdef CONFIG_POOL_POISON + for ( i =3D MIN_BLOCK_SIZE; i < (b->size & BLOCK_SIZE_MASK); i++ ) + ASSERT(b->ptr.buffer[i] =3D=3D 0xAA); +#endif /* CONFIG_POOL_POISON */ } =20 /** @@ -247,6 +255,11 @@ static inline void EXTRACT_BLOCK(struct bhdr *b, struc= t xmem_pool *p, int fl, */ static inline void INSERT_BLOCK(struct bhdr *b, struct xmem_pool *p, int f= l, int sl) { +#ifdef CONFIG_POOL_POISON + if ( (b->size & BLOCK_SIZE_MASK) > MIN_BLOCK_SIZE ) + memset(b->ptr.buffer + MIN_BLOCK_SIZE, 0xAA, + (b->size & BLOCK_SIZE_MASK) - MIN_BLOCK_SIZE); +#endif /* CONFIG_POOL_POISON */ b->ptr.free_ptr =3D (struct free_ptr) {NULL, p->matrix[fl][sl]}; if ( p->matrix[fl][sl] ) p->matrix[fl][sl]->ptr.free_ptr.prev =3D b; --=20 2.20.1.2.gb21ebb671 _______________________________________________ Xen-devel mailing list Xen-devel@lists.xenproject.org https://lists.xenproject.org/mailman/listinfo/xen-devel