From owner-freebsd-hackers@FreeBSD.ORG Wed Feb 12 19:40:18 2014 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id DE4DD581; Wed, 12 Feb 2014 19:40:18 +0000 (UTC) Received: from mx12.netapp.com (mx12.netapp.com [216.240.18.77]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id A4F631601; Wed, 12 Feb 2014 19:40:18 +0000 (UTC) X-IronPort-AV: E=Sophos;i="4.95,834,1384329600"; d="scan'208";a="142359855" Received: from vmwexceht01-prd.hq.netapp.com ([10.106.76.239]) by mx12-out.netapp.com with ESMTP; 12 Feb 2014 11:40:16 -0800 Received: from SACEXCMBX04-PRD.hq.netapp.com ([169.254.6.187]) by vmwexceht01-prd.hq.netapp.com ([10.106.76.239]) with mapi id 14.03.0123.003; Wed, 12 Feb 2014 11:40:16 -0800 From: "Gumpula, Suresh" To: Ian Lepore Subject: RE: malloc(9) and its alignment Thread-Topic: malloc(9) and its alignment Thread-Index: Ac8nlkbcQYqIQNi5TDadKkROsoUT6QAqkNoAAAYt8KA= Date: Wed, 12 Feb 2014 19:40:15 +0000 Message-ID: References: <1392214788.1145.52.camel@revolution.hippie.lan> In-Reply-To: <1392214788.1145.52.camel@revolution.hippie.lan> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.106.53.53] Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Cc: "freebsd-hackers@freebsd.org" X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.17 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 12 Feb 2014 19:40:18 -0000 Thanks Ian for the reply. I will look at the ARM code, but I was thinkin= g why malloc(9) does not return bucket size aligned pointers.=20 Regards, Suresh -----Original Message----- From: Ian Lepore [mailto:ian@FreeBSD.org]=20 Sent: Wednesday, February 12, 2014 9:20 AM To: Gumpula, Suresh Cc: freebsd-hackers@freebsd.org Subject: Re: malloc(9) and its alignment On Wed, 2014-02-12 at 02:02 +0000, Gumpula, Suresh wrote: > Hi, > It appears the malloc(9) returns 8 byte aligned ( UMA_ALIGN_PTR)=20 > pointers, but in bus_dmamem_alloc we might end up checking for greater a= lignment if we take malloc(9) path instead contig_malloc. > Can someone please confirm if malloc(9) returns different alignment point= ers ? >=20 > bus_dmamem_alloc(bus_dma_tag_t dmat, void** vaddr, int flags, > bus_dmamap_t *mapp) > { > /* > * XXX: > * (dmat->alignment < dmat->maxsize) is just a quick hack; the ex= act > * alignment guarantees of malloc need to be nailed down, and the > * code below should be rewritten to take that into account. > * > * In the meantime, we'll warn the user if malloc gets it wrong. > */ > if ((dmat->maxsize <=3D PAGE_SIZE) && > (dmat->alignment < dmat->maxsize) && > dmat->lowaddr >=3D ptoa((vm_paddr_t)Maxmem)) { > *vaddr =3D malloc(dmat->maxsize, M_DEVBUF, mflags); > } else { >=20 > *vaddr =3D contigmalloc(dmat->maxsize, M_DEVBUF, mflags, > 0ul, dmat->lowaddr, dmat->alignment? dmat->alignment = : 1ul, > dmat->boundary); > }=20 > if (vtophys(*vaddr) & (dmat->alignment - 1)) { > NETAPP_MUTED_PRINTF("bus_dmamem_alloc failed to align=20 > memory properly.\n"); >=20 > Regards, > Suresh In my experience, the effective malloc(9) alignment ends up being the same = as MINALLOCSIZE, which is UMA_SMALLEST_UNIT, which is 16 bytes on a system = with 4K pages. At $work we overrode MINALLOCSIZE to 32 to work around cach= e line alignment problems in busdma for ARM systems a few years ago. There is a newer set of busdma allocator routines available in kern/subr_bu= sdma_bufalloc.c which are designed to give a platform more control over ali= gnment of busdma buffers smaller than a page. An example of using them can= be found in arm/busdma_machdep[-v6].c. As far as I know, they're only bei= ng used on ARM platforms right now. -- Ian