Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp1583792yba; Sun, 21 Apr 2019 10:14:53 -0700 (PDT) X-Google-Smtp-Source: APXvYqzPjmbQ1aoiEmZ+Sibyd/8bnRdhVE4+P20Ilq6i2j8v1KS06rpgonP8dZVpdYGueN3fJ7as X-Received: by 2002:a17:902:be18:: with SMTP id r24mr15233993pls.69.1555866893629; Sun, 21 Apr 2019 10:14:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1555866893; cv=none; d=google.com; s=arc-20160816; b=j/Y8yVfs/YoXHGoN3r+fjtYBtPbR00Vr11H6JzfhEQ6bqnwa537WOGGvM1qgg2nIfe cVbciJoTWLgIg4dKp5zFmO2TYjO6JR6vJAN0rqa/Qz4Ws1OKX1797P1FLqVC9hijmMwB 8+xjEcHbAQrrEGGH5oosHWmBXMh8s7ayLlgoR5QEnO4j4FRG4qvoIQtpoqKqIHM2+10X mgOXx4Qh+iY0XxL9fBFbTpNECie1TTLLJaE8UCR0MvM0ah5xMx6oQs+g+pwjT5Bbd3+8 mpwH74pcdy1iR48VuAmkK7cdZ7ErQCJ5qMD7/WOUWfwCZoGzs2o7lVdMqyMV95H9x1Gt 52Ug== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=0O87q8p37rEuXIjRwv/KboXHu3gfrR/wWelzFw+Iu5s=; b=xHmfJXmH8PsyiAvwUeU/ZKC7u7+YYhJ6FFBYsTk3NigN+RTXmNA8NY84QOvGkPuh5k cYyfJxo8uM5aW3MlmILJ6fXu9X9nq6YapnkIJ8n99QZXhYcy+exd8l4hUhWVFSkRb1Nm Ix8zFnXuhfR3aqebto2KaRrm2KypZb9bnumwE/V4QM5SWvcmdnA37lzEczf5gibqsb5J VxURbHf8Kr7gngCgEhtjFm9MUjlo6092D1LhVDPhZ8X3UwlXx5xUruKouHBajMZJXj3U Idk6BplFxzrk/GUU8vazPrDAMrr2SlWdLxCtnCM3zVEQt08EAZ5fHWsYl5nIq7v0OiRZ 9g3Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k4si9974930pgq.208.2019.04.21.10.14.23; Sun, 21 Apr 2019 10:14:53 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727676AbfDURCk (ORCPT + 99 others); Sun, 21 Apr 2019 13:02:40 -0400 Received: from zeniv.linux.org.uk ([195.92.253.2]:32940 "EHLO ZenIV.linux.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727228AbfDURCj (ORCPT ); Sun, 21 Apr 2019 13:02:39 -0400 Received: from viro by ZenIV.linux.org.uk with local (Exim 4.92 #3 (Red Hat Linux)) id 1hIFrP-0005K2-A3; Sun, 21 Apr 2019 17:02:35 +0000 Date: Sun, 21 Apr 2019 18:02:35 +0100 From: Al Viro To: Andrew Morton Cc: Bharath Vedartham , jannh@google.com, reiserfs-devel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] reiserfs: Force type conversion in xattr_hash Message-ID: <20190421170235.GI2217@ZenIV.linux.org.uk> References: <20190417115200.GA10168@bharath12345-Inspiron-5559> <20190418155019.ab5189e4e317df2b36861012@linux-foundation.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190418155019.ab5189e4e317df2b36861012@linux-foundation.org> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Apr 18, 2019 at 03:50:19PM -0700, Andrew Morton wrote: > On Wed, 17 Apr 2019 17:22:00 +0530 Bharath Vedartham wrote: > > > This patch fixes the sparse warning: > > > > fs/reiserfs//xattr.c:453:28: warning: incorrect type in return > > expression (different base types) > > fs/reiserfs//xattr.c:453:28: expected unsigned int > > fs/reiserfs//xattr.c:453:28: got restricted __wsum > > fs/reiserfs//xattr.c:453:28: warning: incorrect type in return > > expression (different base types) > > fs/reiserfs//xattr.c:453:28: expected unsigned int > > fs/reiserfs//xattr.c:453:28: got restricted __wsum > > > > csum_partial returns restricted integer __wsum whereas xattr_hash > > expects a return type of __u32. > > > > ... > > > > --- a/fs/reiserfs/xattr.c > > +++ b/fs/reiserfs/xattr.c > > @@ -450,7 +450,7 @@ static struct page *reiserfs_get_page(struct inode *dir, size_t n) > > > > static inline __u32 xattr_hash(const char *msg, int len) > > { > > - return csum_partial(msg, len, 0); > > + return (__force __u32)csum_partial(msg, len, 0); > > } > > > > int reiserfs_commit_write(struct file *f, struct page *page, > > hm. Conversion from int to __u32 should be OK - why is sparse being so > picky here? Because csum_partial() returns __wsum_t, not int. > Why is the __force needed, btw? So that accidental mixing of those csums (both 16bit and 32bit) with host- or net-endian would be caught. And I'm not at all sure reiserfs xattr_hash() doesn't bugger it up, actually. Recall that 16bit inet csum is the sum of 16bit words (treated as host-endian) modulo 0xffff, i.e. the entire buffer interpreted as host-endian integer taken modulo 0xffff. That has a lovely property - memory representation of that value is the same whether we'd done calculations on b-e or l-e host; the reason is that modulo 65535 byteswap is the same as multiplying by 256, so the sum of byteswapped 16bit values modulo 65535 is byteswapped sum of original values. csum_partial() is sum of 32bit words (treated as host-endian) modulo 0xffffffff, i.e. the entire buffer treated as host-endian number modulo 0xffffffff. It is convenient when we want to calculate the 16bit csum - 0xffffffff is a multiple of 0xffff, so residue modulo 0xffffffff determines the residue modulo 0xffff; that's what csum_fold() is. However, result of csum_partial() on big- and little-endian hosts does *not* have the same property. Consider e.g. an array {0, 0, 0, 128, 0, 0, 0, 128}. csum_partial of that on l-e will be (2^31 + 2^31)mod(2^32 - 1), i.e. 1, with {1, 0, 0, 0} as memory representation. 16bit csum will again be 1, with {1, 0} as memory representation. On big-endian we get (128 + 128)mod(2^32 - 1), i.e. 256, with {0, 0, 1, 0} as memory representation. 16bit csum is again 256, stored as {1, 0}, i.e. the same as if we'd done everything on l-e; however, raw csum_partial() values have different memory representations. They certainly are different as host-endian (and so are 16bit csums). Reiserfs takes csum_partial() on buffer, interprets it as host-endian and stores it little-endian on disk. When fetching those it does the same calculation and fails on mismatch. However, if the store had been done on little-endian host and load - on big-endian one we *will* get mismatch almost all the time. Treating ->rx_hash as __wsum_t (and not doing that cpu_to_le32()) would lower the frequency of mismatches, but still would be broken. Storing a 16bit csum (declared as __sum16_t, again, without cpu_to_le...()) would be endian-safe, but that's not what reiserfs folks wanted (16 bits of csum instead of 32, for starters). IOW, what sparse has caught here is a genuine endianness bug; images created on little-endian host and mounted on big-endian (or vice versa) will see csum mismatches when trying to fetch xattrs. Broken since commit 0b1a6a8ca8a78c2e068b04acf97479ee89a024ac Author: Andrew Morton Date: Sun May 9 23:59:13 2004 -0700 [PATCH] reiserfs: xattr support From: Chris Mason From: jeffm@suse.com reiserfs support for xattrs ISTR some discussions of reiserfs layout endianness problems, but that had been many years ago and I could be wrong; I _think_ the conclusion had been "it sucks, but we can't do anything without breaking existing filesystem images". Not sure if that was the same bug or something different, though.