Received: by 10.213.65.68 with SMTP id h4csp658495imn; Fri, 6 Apr 2018 06:54:51 -0700 (PDT) X-Google-Smtp-Source: AIpwx49qOfzqF9IhYGVOkTUjUvmMEgRnbhwczvHfd9lNBSlblZwf/ZEnEVrDELUkI8pCSpBISMX5 X-Received: by 10.98.226.23 with SMTP id a23mr20469600pfi.157.1523022891675; Fri, 06 Apr 2018 06:54:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523022891; cv=none; d=google.com; s=arc-20160816; b=WNu0aoxzJuUu6ME/eJO4/g18WoYvddBdzEGQUF3+P3BmvHR4V57MnWfgQHSBYy/syt FubRcJ6lJnM5qJgMq+1by84hsPQLZBiRg6CSC+RP1/T2h0UpnyMLqQKJqkidUIGQyiTH MDbISCh3VTa3jPYJOxm7GiznAzpFzz2K1rQGAHHsZmrBV9IAkGFP+Lz1mffcQ8/9lwtt w0OF90kW6sE2/8GtirbzAKVKj9TYx/XQW10fuCXWiYS7+4AHlvATXPOzAjlqhpoEdaIB 5JVreo5lTi/RVUX290b+C70eLiLWR5z88/jhETPjmZJ2FPnfQ5SW5QAoV4FXN0pqBs3Y jpiQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=l7ur/g8yvEMNKGXmGIW5nIhD2azby/m1FzGq97Dmh+s=; b=KrLxoBdG3PPR0ho5iq/Nx+jxwVsgT0RIDdJfLrBIhX8RZQiC+z2PyAj+7bG3g3MIyb XphwRqo1bwU2wlZdDBkYA7PUeDXMSCVjxrB6NPmQKOKF+WGr6yWnoNAwLdcFauSR+rTp cXjQfcUm8nSRdYtb9ZGKNwfMEqTJo/mtyxjms9fYBvq9jJerpLeL86DTmd77BUJfjyd7 afj8eRRaCumKxmqGpbrqfeMO8J8uQaIOrYQx/GMhAxs9UMZMRZAUd4AAlS61VU9Vl0Fe oW5omY/HcfI5LIxixiOsTXK29C0cVj4LHKRX+ac7vwpj9zCWVYMg86Iurg0v5BKmxXEI U0Kw== 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 m21si8130853pfj.25.2018.04.06.06.54.38; Fri, 06 Apr 2018 06:54:51 -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 S1756897AbeDFNoU (ORCPT + 99 others); Fri, 6 Apr 2018 09:44:20 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:37622 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752904AbeDFNoP (ORCPT ); Fri, 6 Apr 2018 09:44:15 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 52551E08; Fri, 6 Apr 2018 13:44:14 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, stable@kernel.org, "Erhard F." , Matthew Wilcox , Rasmus Villemoes , Andrew Morton , Arnd Bergmann , Omar Sandoval , Linus Torvalds Subject: [PATCH 4.16 01/31] bitmap: fix memset optimization on big-endian systems Date: Fri, 6 Apr 2018 15:24:26 +0200 Message-Id: <20180406084341.141774205@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180406084340.999820380@linuxfoundation.org> References: <20180406084340.999820380@linuxfoundation.org> User-Agent: quilt/0.65 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.16-stable review patch. If anyone has any objections, please let me know. ------------------ From: Omar Sandoval commit 21035965f60b0502fc6537b232839389bb4ce664 upstream. Commit 2a98dc028f91 ("include/linux/bitmap.h: turn bitmap_set and bitmap_clear into memset when possible") introduced an optimization to bitmap_{set,clear}() which uses memset() when the start and length are constants aligned to a byte. This is wrong on big-endian systems; our bitmaps are arrays of unsigned long, so bit n is not at byte n / 8 in memory. This was caught by the Btrfs selftests, but the bitmap selftests also fail when run on a big-endian machine. We can still use memset if the start and length are aligned to an unsigned long, so do that on big-endian. The same problem applies to the memcmp in bitmap_equal(), so fix it there, too. Fixes: 2a98dc028f91 ("include/linux/bitmap.h: turn bitmap_set and bitmap_clear into memset when possible") Fixes: 2c6deb01525a ("bitmap: use memcmp optimisation in more situations") Cc: stable@kernel.org Reported-by: "Erhard F." Cc: Matthew Wilcox Cc: Rasmus Villemoes Cc: Andrew Morton Cc: Arnd Bergmann Signed-off-by: Omar Sandoval Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- include/linux/bitmap.h | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) --- a/include/linux/bitmap.h +++ b/include/linux/bitmap.h @@ -302,12 +302,20 @@ static inline void bitmap_complement(uns __bitmap_complement(dst, src, nbits); } +#ifdef __LITTLE_ENDIAN +#define BITMAP_MEM_ALIGNMENT 8 +#else +#define BITMAP_MEM_ALIGNMENT (8 * sizeof(unsigned long)) +#endif +#define BITMAP_MEM_MASK (BITMAP_MEM_ALIGNMENT - 1) + static inline int bitmap_equal(const unsigned long *src1, const unsigned long *src2, unsigned int nbits) { if (small_const_nbits(nbits)) return !((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits)); - if (__builtin_constant_p(nbits & 7) && IS_ALIGNED(nbits, 8)) + if (__builtin_constant_p(nbits & BITMAP_MEM_MASK) && + IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT)) return !memcmp(src1, src2, nbits / 8); return __bitmap_equal(src1, src2, nbits); } @@ -358,8 +366,10 @@ static __always_inline void bitmap_set(u { if (__builtin_constant_p(nbits) && nbits == 1) __set_bit(start, map); - else if (__builtin_constant_p(start & 7) && IS_ALIGNED(start, 8) && - __builtin_constant_p(nbits & 7) && IS_ALIGNED(nbits, 8)) + else if (__builtin_constant_p(start & BITMAP_MEM_MASK) && + IS_ALIGNED(start, BITMAP_MEM_ALIGNMENT) && + __builtin_constant_p(nbits & BITMAP_MEM_MASK) && + IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT)) memset((char *)map + start / 8, 0xff, nbits / 8); else __bitmap_set(map, start, nbits); @@ -370,8 +380,10 @@ static __always_inline void bitmap_clear { if (__builtin_constant_p(nbits) && nbits == 1) __clear_bit(start, map); - else if (__builtin_constant_p(start & 7) && IS_ALIGNED(start, 8) && - __builtin_constant_p(nbits & 7) && IS_ALIGNED(nbits, 8)) + else if (__builtin_constant_p(start & BITMAP_MEM_MASK) && + IS_ALIGNED(start, BITMAP_MEM_ALIGNMENT) && + __builtin_constant_p(nbits & BITMAP_MEM_MASK) && + IS_ALIGNED(nbits, BITMAP_MEM_ALIGNMENT)) memset((char *)map + start / 8, 0, nbits / 8); else __bitmap_clear(map, start, nbits);