From patchwork Mon Feb 22 14:02:55 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Leif Lindholm X-Patchwork-Id: 62580 Delivered-To: patch@linaro.org Received: by 10.112.43.199 with SMTP id y7csp1254058lbl; Mon, 22 Feb 2016 06:03:22 -0800 (PST) X-Received: by 10.140.144.83 with SMTP id 80mr36097699qhq.102.1456149802778; Mon, 22 Feb 2016 06:03:22 -0800 (PST) Return-Path: Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id 188si29599742qhi.44.2016.02.22.06.03.22 for (version=TLS1 cipher=AES128-SHA bits=128/128); Mon, 22 Feb 2016 06:03:22 -0800 (PST) Received-SPF: pass (google.com: domain of grub-devel-bounces+patch=linaro.org@gnu.org designates 2001:4830:134:3::11 as permitted sender) client-ip=2001:4830:134:3::11; Authentication-Results: mx.google.com; spf=pass (google.com: domain of grub-devel-bounces+patch=linaro.org@gnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom=grub-devel-bounces+patch=linaro.org@gnu.org; dkim=neutral (body hash did not verify) header.i=@linaro.org Received: from localhost ([::1]:49291 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aXr58-0005XL-7a for patch@linaro.org; Mon, 22 Feb 2016 09:03:22 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:39124) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aXr4r-0005X7-Sf for grub-devel@gnu.org; Mon, 22 Feb 2016 09:03:07 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aXr4l-0007mb-TW for grub-devel@gnu.org; Mon, 22 Feb 2016 09:03:05 -0500 Received: from mail-wm0-x229.google.com ([2a00:1450:400c:c09::229]:36297) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aXr4l-0007mU-JA for grub-devel@gnu.org; Mon, 22 Feb 2016 09:02:59 -0500 Received: by mail-wm0-x229.google.com with SMTP id g62so174212831wme.1 for ; Mon, 22 Feb 2016 06:02:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=date:from:to:subject:message-id:references:mime-version :content-type:content-disposition:content-transfer-encoding :in-reply-to:user-agent; bh=OBRcE/HNTZAzV/l/UbyeLLLprM+Im+hTBcE3o2xKBwc=; b=bja1LC2Ovxx+oPUSRB2aLGKE2uOf2rI7zsi3GtOqzWVN/V5DXPCHhuLGfVSuvF1R/a OAejmBaZmTgcImM6W1/m6SQ80Af4/X5OFsC/bY2mtRv1Y1OWwJKgfRleXoEu2+Vd/lFP 1KDTFu3dQYwsWoPE4TFXVf0Jqiec6ql7t/v6U= 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:subject:message-id:references :mime-version:content-type:content-disposition :content-transfer-encoding:in-reply-to:user-agent; bh=OBRcE/HNTZAzV/l/UbyeLLLprM+Im+hTBcE3o2xKBwc=; b=gSj79aF5fcXTGFqAy7+UJ1wilv0N6kmf5DztnGQBTxCLdqYRa2W6CweWvMuhVZevI6 BT5LLm7BxfXlUYDNvCpsecnDePqyIAOGm7xufuS75vZw5moKCaB0zbmKClyQc2/Tp8/c fGZ0NuInJdJiIUPSzncLCn25Rs/tjrxj6UdfQdu6ZpMYbdShAqBMSjcRi8Ud3meZOPtH cozMr8VioSSAfxQ8lvdQ/0nGxfyHgHFbVHO+Egf0aejtmSDmMV2DxVuXkx7jBOpiOOYC 62x/meMSSoaOYDV1WN5Dq87YAL0QV5MRfPN4EVMuXuujOYkQIordX54AJAulpqqNQJoa msuw== X-Gm-Message-State: AG10YOQ1HiP8xvs6yI78A9glav6mVxd59JjeRLyFIeaTpj8Bt5bKi99QcHbAT6ROj4cgyxek X-Received: by 10.194.63.75 with SMTP id e11mr27685240wjs.79.1456149778207; Mon, 22 Feb 2016 06:02:58 -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 v1sm24717473wjf.49.2016.02.22.06.02.56 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 22 Feb 2016 06:02:57 -0800 (PST) Date: Mon, 22 Feb 2016 14:02:55 +0000 From: Leif Lindholm To: The development of GNU GRUB Subject: Re: [PATCH 1/2] disk: Add support for device-specific malloc function Message-ID: <20160222140255.GO1159@bivouac.eciton.net> References: <1455898714-25127-1-git-send-email-leif.lindholm@linaro.org> <1455898714-25127-2-git-send-email-leif.lindholm@linaro.org> <56CABF64.7020003@gmail.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <56CABF64.7020003@gmail.com> 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 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: , Errors-To: grub-devel-bounces+patch=linaro.org@gnu.org Sender: grub-devel-bounces+patch=linaro.org@gnu.org On Mon, Feb 22, 2016 at 10:57:24AM +0300, Andrei Borzenkov wrote: > 19.02.2016 19:18, Leif Lindholm пишет: > > Some disk types have allocation requirements beyond normal grub_malloc. > > Add a function pointer to grub_disk_t and a wrapper function in > > kern/disk.c making use of that function if available, to enable these > > disk drivers to implement their own malloc. > > The problem is not (only) grub_disk_read_small(), but this part in > grub_disk_read: > > if (agglomerate) > { > grub_disk_addr_t i; > > err = (disk->dev->read) (disk, transform_sector (disk, sector), > agglomerate << (GRUB_DISK_CACHE_BITS > + GRUB_DISK_SECTOR_BITS > - disk->log_sector_size), > buf); > > which reads directly into user supplied buffer. May be we can allocate > contiguous cache block here but put pointers to multiple chunks inside > it. Possible implementation is to have second layer of reference counted > memory blocks with cache entries containing pointer + offset into them. Whoops! Understood. So how about merging the two concepts? Including a patch to go with (after) the previous two to catch any remaining unaligned accesses in grub_efidisk_readwrite(). With this applied, I get no fixups from a normal Linux boot (linux + initrd), but see them when exploring filesystems from the command line. Whilst a bit clunky, this seems much short-term preferable to going back and redesigning the disk subsystem to understand that alignment matters. Although given the number of exceptions we seem to be amassing, that does not sound like a bad idea for future. And hopefully we can get rid of things like these: https://github.com/tianocore/edk2/blob/master/OvmfPkg/XenPvBlkDxe/BlockIo.c#L117 Regards, Leif >From 41bea6c3fdcbf97d05ce8f90e1300f7a347b8a34 Mon Sep 17 00:00:00 2001 From: Leif Lindholm Date: Mon, 22 Feb 2016 13:44:46 +0000 Subject: [PATCH] efidisk: handle unaligned buffers With a dedicated buffer allocator, the vast majority of efidisk accesses are now performed compliant to block_io_protocol. Implement temporary buffers to fix up any remaining unaligned buffers, such as refernces directly into the disk cache. --- grub-core/disk/efi/efidisk.c | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/grub-core/disk/efi/efidisk.c b/grub-core/disk/efi/efidisk.c index 9b42585..4c5caf2 100644 --- a/grub-core/disk/efi/efidisk.c +++ b/grub-core/disk/efi/efidisk.c @@ -539,15 +539,41 @@ 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; + io_align = bio->media->io_align ? bio->media->io_align : 1; + num_bytes = size << disk->log_sector_size; + + if ((unsigned long) buf & (io_align -1)) + { + grub_dprintf ("efidisk", "using temporary buffer to handle alignment\n"); + 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, + num_bytes, aligned_buf); + + if ((unsigned long) buf & (io_align -1)) + { + if (!wr) + grub_memcpy (buf, aligned_buf, num_bytes); + grub_free (aligned_buf); + } - 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); + return status; } static grub_err_t