Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752458AbdIXOUw (ORCPT ); Sun, 24 Sep 2017 10:20:52 -0400 Received: from mail-it0-f68.google.com ([209.85.214.68]:38903 "EHLO mail-it0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752276AbdIXOUu (ORCPT ); Sun, 24 Sep 2017 10:20:50 -0400 X-Google-Smtp-Source: AOwi7QDu987Y/WHIfTY51Dsrj8Lhl8d8INB74P8Mb+h+SJstQl88ybmDAd1wBuxlvY3md++W87rgJw== Date: Sun, 24 Sep 2017 10:20:46 -0400 From: Chuck Ebbert To: Timur Tabi Cc: linux-kernel@vger.kernel.org Subject: Re: Proper way to allocate DMA buffer within a single 4GB block? Message-ID: <20170924102046.357c00ca@gmail.com> In-Reply-To: References: Organization: Very little MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 900 Lines: 21 On Wed, 20 Sep 2017 17:18:09 -0500 Timur Tabi wrote: > I have a device that requires I allocated a few buffers for DMA. The > problem is that this device has only one register for the upper 32 > bits of all of the buffers. That is, all of buffers must reside > within the same 4GB block of memory. In order words, > > end = start + size - 1; > if (upper_32_bits(start) != upper_32_bits(end)) > // Oh no, the buffer spans across a 4GB boundary! > > The buffer is typically less than 16KB in size, so we've never seen > it actually span across a 4GB boundary. However, I want to ensure > that it's impossible. I wrote this function that re-tries the > allocation if the first one is invalid, but I suspect that it's too > hackish. Is there a better way? > Allocate a buffer twice as big as what you really need. If the first half doesn't work, the second half will.