Received: from list by lists.gnu.org with archive (Exim 4.71) id 1aWNbZ-0000xk-IE for mharc-grub-devel@gnu.org; Thu, 18 Feb 2016 07:22:45 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51281) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aWNbW-0000x4-Af for grub-devel@gnu.org; Thu, 18 Feb 2016 07:22:44 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aWNbU-0008Lw-Qc for grub-devel@gnu.org; Thu, 18 Feb 2016 07:22:42 -0500 Received: from mail-wm0-x229.google.com ([2a00:1450:400c:c09::229]:38428) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aWNbU-0008Lf-DK for grub-devel@gnu.org; Thu, 18 Feb 2016 07:22:40 -0500 Received: by mail-wm0-x229.google.com with SMTP id a4so22525207wme.1 for ; Thu, 18 Feb 2016 04:22:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-type:content-disposition:content-transfer-encoding :in-reply-to:user-agent; bh=6dJaVwKYV61y1esBC3F3ZKDWZUndPjz4Ve+RHi2Uldw=; b=R4H33g9CgeRBgvKvU6nDUwwoPsbi1GTJ5xcIfWvENNQQQn6x/Pmf1BWktTmfrxv6ZZ PVfdU0H7h7282A6Erc6S8HyBpAvRH8G1Mc5pm66GrHeff6qGZvwomzHqLqNpi3Ny25v9 yMwmZK6sUFL9V0gnEMr6UZq9Rzt7t/3BKT/CY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-type:content-disposition :content-transfer-encoding:in-reply-to:user-agent; bh=6dJaVwKYV61y1esBC3F3ZKDWZUndPjz4Ve+RHi2Uldw=; b=L+Zlzcii1nv/IZKvHVebwYI0nx+PZNP+k5mlZz4I6AyBXnF6kZEuu5XsThYu3NlclH xNuLY1CzFVwV2a+dH52cmpJJ7qmXdmdS4/fYKNbP5JhBg3VCKfZJIdnPLf65xNldWQW2 0auydhVKP4BM88nuXLhec5E/sGBt+0xjUS085AB0AhmhYm46pkhxo1qKRZIJ0SJbYr8r aUDUi4vd6PkcgAS7r8hw0NjReMbQtIcr+oLmWuHEeXso/qexagtVBcjW2NvQNXX+AaA+ S/gkSzZ86MMWST5nFsjWJTzU+o4PvP068/Pzo1kSdzjsAjOXJ8ZWiqNB8+FfSV0wJrgX lwQQ== X-Gm-Message-State: AG10YOQB+6xWsZXqmSlIl4YrF1lK6YkMy+AHqi0O36jcSdeTwMok22Uc1aqarFaCBNMzbNgG X-Received: by 10.28.63.200 with SMTP id m191mr2972311wma.21.1455798159590; Thu, 18 Feb 2016 04:22:39 -0800 (PST) Received: from bivouac.eciton.net (bivouac.eciton.net. [2a00:1098:0:86:1000:23:0:2]) by smtp.gmail.com with ESMTPSA id u9sm2777595wmd.4.2016.02.18.04.22.38 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 18 Feb 2016 04:22:38 -0800 (PST) Date: Thu, 18 Feb 2016 12:22:36 +0000 From: Leif Lindholm To: The development of GNU GRUB Subject: Re: [PATCH] efidisk: Respect block_io_protocol buffer alignment Message-ID: <20160218122236.GG1159@bivouac.eciton.net> References: <56C49E90.3070307@gmail.com> <1455738274-20469-1-git-send-email-leif.lindholm@linaro.org> <56C53C26.5000406@gmail.com> <20160218102110.GF1159@bivouac.eciton.net> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.5.23 (2014-03-12) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::229 Cc: Jeremy Linton X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list Reply-To: The development of GNU GRUB List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 18 Feb 2016 12:22:44 -0000 On Thu, Feb 18, 2016 at 03:00:38PM +0300, Andrei Borzenkov wrote: > On Thu, Feb 18, 2016 at 1:21 PM, Leif Lindholm wrote: > > On Thu, Feb 18, 2016 at 06:36:06AM +0300, Andrei Borzenkov wrote: > >> 17.02.2016 22:44, Leif Lindholm пишет: > >> > Returned from the OpenProtocol operation, the grub_efi_block_io_media > >> > structure contains the io_align field, specifying the minimum alignment > >> > required for buffers used in any data transfers with the device. > >> > > >> > Make grub_efidisk_readwrite() allocate a temporary buffer, aligned to > >> > this boundary, if the buffer passed to it does not already meet the > >> > requirements. > >> > > >> > Reported-by: Jeremy Linton > >> > --- > >> > > >> > This modified version is contained entirely within the efidisk driver. > >> > There is some excessive copying going on, but it removes the risk of > >> > the changes interfering with other disk drivers. > >> > > >> > grub-core/disk/efi/efidisk.c | 37 ++++++++++++++++++++++++++++++++----- > >> > 1 file changed, 32 insertions(+), 5 deletions(-) > >> > > >> > diff --git a/grub-core/disk/efi/efidisk.c b/grub-core/disk/efi/efidisk.c > >> > index 1c00e3e..901133f 100644 > >> > --- a/grub-core/disk/efi/efidisk.c > >> > +++ b/grub-core/disk/efi/efidisk.c > >> > @@ -524,15 +524,42 @@ grub_efidisk_readwrite (struct grub_disk *disk, grub_disk_addr_t sector, > >> > { > >> > struct grub_efidisk_data *d; > >> > grub_efi_block_io_t *bio; > >> > + grub_efi_status_t status; > >> > + grub_size_t io_align, num_bytes; > >> > + char *aligned_buf; > >> > > >> > d = disk->data; > >> > bio = d->block_io; > >> > > >> > - return efi_call_5 ((wr ? bio->write_blocks : bio->read_blocks), bio, > >> > - bio->media->media_id, > >> > - (grub_efi_uint64_t) sector, > >> > - (grub_efi_uintn_t) size << disk->log_sector_size, > >> > - buf); > >> > + /* Set alignment to 1 if 0 specified */ > >> > + io_align = bio->media->io_align ? bio->media->io_align : 1; > >> > >> We probably should sanity check that it is power of two in > >> grub_efidisk_open. > > > > The UEFI specification says: > > "IoAlign values of 0 and 1 mean that the buffer can be placed anywhere > > in memory. Otherwise, IoAlign must be a power of 2, and the > > requirement is that the start address of a buffer must be evenly > > divisible by IoAlign with no remainder." > > > > So we certainly could sanity check it, but what's the fallback if > > firmware presents an invalid value? Scream loudly that things are > > likely to break and then bail out, or silently round up to the > > nearest power of two? > > grub_efidisk_open() fails if sector size is bad, I guess we should > follow the suite. Please also add alignment print to grub_dprintf > there. Good call, that would be interesting to have. I'll also add a grub_error() message, given it is a fatal system error. > It may be interesting to optionally keep stats how often we hit > non-aligned buffer. I wonder what alignment requirement your system > has - is it sector size? In this instance it was (/happened to coincide with?) page size. > > Of the two, the former seems more palatable. > > > >> > + num_bytes = size << disk->log_sector_size; > >> > + > >> > + if ((unsigned long) buf & (io_align - 1)) > >> > >> grub_addr_t? > > > > Yeah, sorry, got lost in refactoring. > > > >> > + { > >> > + aligned_buf = grub_memalign (io_align, num_bytes); > >> > + if (! aligned_buf) > >> > + return GRUB_EFI_OUT_OF_RESOURCES; > >> > + if (wr) > >> > + grub_memcpy (aligned_buf, buf, num_bytes); > >> > + } > >> > + else > >> > + { > >> > + aligned_buf = buf; > >> > + } > >> > + > >> > + status = efi_call_5 ((wr ? bio->write_blocks : bio->read_blocks), bio, > >> > + bio->media->media_id, (grub_efi_uint64_t) sector, > >> > + (grub_efi_uintn_t) num_bytes, aligned_buf); > >> > + > >> > + if ((unsigned long) buf & (io_align - 1)) > > And here too of course. Already in my tree :) New version coming up. / Leif