From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pf0-f171.google.com ([209.85.192.171]:33112 "EHLO mail-pf0-f171.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751945AbdLFPvh (ORCPT ); Wed, 6 Dec 2017 10:51:37 -0500 Received: by mail-pf0-f171.google.com with SMTP id y89so2441499pfk.0 for ; Wed, 06 Dec 2017 07:51:37 -0800 (PST) Date: Wed, 6 Dec 2017 23:51:29 +0800 From: Eryu Guan To: Matthew Wilcox Cc: Harish Kasiviswanathan , linux-fsdevel@vger.kernel.org, Felix.Kuehling@amd.com Subject: Re: [PATCH v2] direct-io: Fix unsigned comparison overflow Message-ID: <20171206155129.GC7106@eguan.usersys.redhat.com> References: <1512510027-7101-1-git-send-email-Harish.Kasiviswanathan@amd.com> <20171205221927.GG26021@bombadil.infradead.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20171205221927.GG26021@bombadil.infradead.org> Sender: linux-fsdevel-owner@vger.kernel.org List-ID: On Tue, Dec 05, 2017 at 02:19:27PM -0800, Matthew Wilcox wrote: > On Tue, Dec 05, 2017 at 04:40:27PM -0500, Harish Kasiviswanathan wrote: > > create = dio->op == REQ_OP_WRITE; > > - if (dio->flags & DIO_SKIP_HOLES) { > > + if (dio->flags & DIO_SKIP_HOLES && > > + i_size_read(dio->inode) > 0) { > > if (fs_startblk <= ((i_size_read(dio->inode) - 1) >> > > i_blkbits)) > > i_size_read() isn't cheap on 32-bit SMP ... do we actually need to sample > it at all here, or is it enough to use the i_size that was sampled earlier? > IOW: > > create = dio->op == REQ_OP_WRITE; > - if (dio->flags & DIO_SKIP_HOLES) { > - if (fs_startblk <= ((i_size_read(dio->inode) - 1) >> > - i_blkbits)) > + if (dio->flags & DIO_SKIP_HOLES && dio->i_size) { > + if (fs_startblk <= (dio->i_size - 1) >> i_blkbits)) I think using dio->i_size should be fine. I tested ext3/4 with LTP (aio-)dio tests and fstests and didn't see any regression introduced with this change. Thanks, Eryu > > Another possibility would be to tweak the comparison slightly ... > > if (dio->flags & DIO_SKIP_HOLES) { > - if (fs_startblk <= ((i_size_read(dio->inode) - 1) >> > - i_blkbits)) > + if (fs_startblk < ((i_size_read(dio->inode) + > + (1UL << i_blkbits) - 1) >> i_blkbits)) > > Or we could use a temporary variable to avoid reading i_size twice.