Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp8067847ybl; Thu, 16 Jan 2020 10:04:59 -0800 (PST) X-Google-Smtp-Source: APXvYqwHP+6CN5LE1YXDaar6KJxVuM7HqUizk4b9cX7TPmLP1IzZiI6Nl1KIv8ELVZcAkIT/Rpmr X-Received: by 2002:a05:6830:1d59:: with SMTP id p25mr3096784oth.308.1579197899154; Thu, 16 Jan 2020 10:04:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1579197899; cv=none; d=google.com; s=arc-20160816; b=SISKuF/niwtIMUXknKSLQCQYQBPRbASKCJIEAn29VMx65VgMFJFTv4MrddcpE9Xd6P mapU0DTNLBz+0QAfWdiKx9CvRS8B1c1fwhVBWVYMZs/S7udh+ZxguWqVc6X5isqaasXh QhYV27/0wyNteHnLQh3zKt216/uFIaW94rvSZxGGq0Kl6DDkR9SgGOC050zTtW7csY7f Bk942sOYpEMq1N8ejYBUcSHmTtcY9ApewVj+mp4NE4rUcpNO+GAJRI6QA7sREVeNgFnh cZisb3lAFmkMkXwrbbzGDVIVA0snOFzG+UvjvsMKnNt+jn8DriNuSgYAhuP/lnznSbii L8wA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=fffikSWlMkrixzH/uisvFy/PnFykvV3omwSOcLyfw8Q=; b=yHAK4QjlhJcm+0IEpXO0Bj9ERJB0D6IF4SbJ6eumRY5dhcoi+L0wlDAsun2a0mQoU6 CBx+PoLDtkehUHLoD8FmdhTMhUUce18SKINk7WMVHq8vgO343BX9wIlDxkuNpgVj6hZx tYrDFJneBrtWu/BpNERvKJellta5UuepXebObQtV/dyipQSntNN7zmXXfg81RfdHD9PQ HW/Oi++8qcJYEqLI3Nx4A6DWQ2I2+txxVfveLn+gPeaLRiNAuZGt6HX8gG2IXre+9ocX DbV/CLZ7wLwaVAnRQpMkxrfw8mEZa+lI0cjpbhvGXEHzjbFXDdIkc6aP1e46YzSFqAa+ sTmw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=VOgx0pKI; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q2si13778876oti.273.2020.01.16.10.04.46; Thu, 16 Jan 2020 10:04:59 -0800 (PST) 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; dkim=pass header.i=@kernel.org header.s=default header.b=VOgx0pKI; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2393596AbgAPRlQ (ORCPT + 99 others); Thu, 16 Jan 2020 12:41:16 -0500 Received: from mail.kernel.org ([198.145.29.99]:57420 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2393608AbgAPRkT (ORCPT ); Thu, 16 Jan 2020 12:40:19 -0500 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id F397F24718; Thu, 16 Jan 2020 17:40:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1579196419; bh=NH7YmaENed7733PiOVCPiTH7DV/SfeLsD2y7DmbM7Ug=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VOgx0pKIUgwMnYxNz2Nkx+vLHAZ55zodK+qnEHQmm5jXNnqjbMYRMVO+f8fCzKY4E rgPM25/jxGM7UEkMOt7j7GlTTG9IJOcHOt1c/DcL4BzOG3sDBOrwX6MmmyYLHOmKKC TjsC5ZJOp6kj5IfXHFqt5WcdYBD6ToxWxXQKykPc= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Linus Torvalds , laokz , Stefani Seibold , Andrew Morton , Dan Carpenter , Greg KH , Kees Cook , Will Deacon , Sasha Levin Subject: [PATCH AUTOSEL 4.9 189/251] Partially revert "kfifo: fix kfifo_alloc() and kfifo_init()" Date: Thu, 16 Jan 2020 12:35:38 -0500 Message-Id: <20200116173641.22137-149-sashal@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200116173641.22137-1-sashal@kernel.org> References: <20200116173641.22137-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Linus Torvalds [ Upstream commit ab9bb6318b0967671e0c9b6537c1537d51ca4f45 ] Commit dfe2a77fd243 ("kfifo: fix kfifo_alloc() and kfifo_init()") made the kfifo code round the number of elements up. That was good for __kfifo_alloc(), but it's actually wrong for __kfifo_init(). The difference? __kfifo_alloc() will allocate the rounded-up number of elements, but __kfifo_init() uses an allocation done by the caller. We can't just say "use more elements than the caller allocated", and have to round down. The good news? All the normal cases will be using power-of-two arrays anyway, and most users of kfifo's don't use kfifo_init() at all, but one of the helper macros to declare a KFIFO that enforce the proper power-of-two behavior. But it looks like at least ibmvscsis might be affected. The bad news? Will Deacon refers to an old thread and points points out that the memory ordering in kfifo's is questionable. See https://lore.kernel.org/lkml/20181211034032.32338-1-yuleixzhang@tencent.com/ for more. Fixes: dfe2a77fd243 ("kfifo: fix kfifo_alloc() and kfifo_init()") Reported-by: laokz Cc: Stefani Seibold Cc: Andrew Morton Cc: Dan Carpenter Cc: Greg KH Cc: Kees Cook Cc: Will Deacon Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- lib/kfifo.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/kfifo.c b/lib/kfifo.c index 90ba1eb1df06..a94227c55551 100644 --- a/lib/kfifo.c +++ b/lib/kfifo.c @@ -82,7 +82,8 @@ int __kfifo_init(struct __kfifo *fifo, void *buffer, { size /= esize; - size = roundup_pow_of_two(size); + if (!is_power_of_2(size)) + size = rounddown_pow_of_two(size); fifo->in = 0; fifo->out = 0; -- 2.20.1