Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp4438237imm; Mon, 18 Jun 2018 15:11:48 -0700 (PDT) X-Google-Smtp-Source: ADUXVKIMe1gaHw9as9OXSrtVSkrlDWjqBALtTMPunRG8sUiIo3YMVM0OWM4nziYCHslHW4udDwlK X-Received: by 2002:a63:3807:: with SMTP id f7-v6mr12555422pga.446.1529359908531; Mon, 18 Jun 2018 15:11:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1529359908; cv=none; d=google.com; s=arc-20160816; b=Q1UUm+kaTFws7p1xlj9ZB2ghav0mmPJqRd6e67hWOLm9MJnt8l+r4uYMOysY1TcLmH mlpQuIBiNZycs7DsqN65bIx6zbv0r48RfZOZ0uYdUbO9rTbh76HOFNBgdKz5teMPoLxh UGdxDDsFdZDKCT+udhqo76gWXcT4tR5vspbkzURhab44eiYwF1rKonEe0zbp1GsRahGh eRs7mo1DCmz9iO44AQfgNEaafu/MWTtzedgzuM+kHbQoIqxZhDHrdaqlF6myvyKOGiX7 nm0NR+l7HIea2LBzFtPcPwBYu7eNzNukCOpSh6giHjd/tM0FelzSeQW5UzQpl8i6hZGS +yuA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:dkim-signature:dkim-signature :arc-authentication-results; bh=fnY/d62ljkEPQYxKqXry0rYSvQFo5/bLV3wUd9r5wKQ=; b=PtwHttpxmazMhHk6Ahvj/Rkse0Z5zVn1MYvgpwYqRg8YRyURD8Qf9ZaWWdksBRpL+T SW3rWec/6NP/o30MMSOdIdcpwUmlqVGvL6cefF32N2EesO6ba+bG1cVOPn9I5iAVA3JD MBnOkiv/C0fd3Du7IdTguYfmq1suAqCY8vF9bXZJ6f1IwjkstjpQ+VQsXB4bTLGS3cn8 M2MoXBIuEGxAbB5LrHnptwdNVNb1FAetXGAtMaYzRvqDqkXTyWGcX4qgWfMPY6ydkhVf mAxHN+QvNwbIZTTBsdADN6ktZ35ii9i+mYjaSKFD6rtqudQyYkst2Fgco9wTj47bKgAx MRcg== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@google.com header.s=20161025 header.b=urn1hTuY; dkim=fail header.i=@chromium.org header.s=google header.b=Bgh8gO8P; 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=fail (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z3-v6si13188099pgb.277.2018.06.18.15.11.34; Mon, 18 Jun 2018 15:11:48 -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; dkim=fail header.i=@google.com header.s=20161025 header.b=urn1hTuY; dkim=fail header.i=@chromium.org header.s=google header.b=Bgh8gO8P; 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=fail (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S935690AbeFRWKv (ORCPT + 99 others); Mon, 18 Jun 2018 18:10:51 -0400 Received: from mail-yb0-f195.google.com ([209.85.213.195]:40684 "EHLO mail-yb0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933036AbeFRWKu (ORCPT ); Mon, 18 Jun 2018 18:10:50 -0400 Received: by mail-yb0-f195.google.com with SMTP id v17-v6so6561983ybe.7 for ; Mon, 18 Jun 2018 15:10:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc; bh=fnY/d62ljkEPQYxKqXry0rYSvQFo5/bLV3wUd9r5wKQ=; b=urn1hTuY6WcQQm6GVMDWeLyRznNrAkTFYRVoS1Ahpd1EhMIa6I23+VqTlv5RMc7CxB iuTq4erqVDd/t25SGvyKvavR6sJzrXXJQ0Tox0DIV7OFWRTR1dzXEL8Foq10DHFaOmNg 6dBg2lv/4umVsQUoLp7+dXQitTYjY0sl9lEH0UoJtDD36KzFV94u7hnW5B08V+u/4Qya 6P9ETQ0BQSgGyF+jPXiumepXlZM4+iX4HYW+8nY2KwyXzILaFdEiAEu9joVZ8PNX9ecK IC5DKOJhMJyHsltJvKUBAmPS07+pvS53VVe52R1KL+E5GStErXraIiDTUE8uasG9IaiK iEig== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc; bh=fnY/d62ljkEPQYxKqXry0rYSvQFo5/bLV3wUd9r5wKQ=; b=Bgh8gO8P4o6vAkLd6aS/JT3fkEn9+pbEjyR/g/IGNjky1/18yGm4vx0wy+AEyd6qRq 8O84AkscIKwYyJ8t5FRDYLmQomhcqLrjV6SGt0j6syNo8F7CVU2dPpGpWp50TVjYvYRm 2lOhtym1NxdckMfe06q6JnHO0Z7QAUZoy1xSM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:sender:in-reply-to:references:from :date:message-id:subject:to:cc; bh=fnY/d62ljkEPQYxKqXry0rYSvQFo5/bLV3wUd9r5wKQ=; b=P3VfUwfKKYOxDrpYyorxQ9uEzSvQkR6xe5mZDuZVZkKYeTR0A6cqspgh21DDbP7x0r SC6MMZxIlale0U6hBTsAZY1Kdbpd7WzxkrnBa/Y5fJZWlB9+mb+itaQhzpDaVMFtrQjf IISpM0moFQCADE9nOAp8W57Oqe6pkbiGNHnLvUdYJpNTpoYtmTjwoeRDtUgQnalfxowt PVzsAb0RICK8crx5BsPEfkUVhaAU754ky5b3QBeeAbszZLpxK/wuMfAw280S0MWoL/bY U5Hzr/DMDcwYrvn4t5YwcbAKx/sA4EG8hull0z+JVP+SJ9mUwK1sMZRb87zAhbBTr76P rk2Q== X-Gm-Message-State: APt69E330Y/OSaN7QS4R3wXRNEXAdr3bJyUDARGyyqBTGZSjdssXz1zd +JzYBcr+R0bW6f0V/FCK5SkMk8J/+hEy0LlZXUWKUw== X-Received: by 2002:a25:a301:: with SMTP id d1-v6mr7526196ybi.193.1529359849211; Mon, 18 Jun 2018 15:10:49 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a25:d6c5:0:0:0:0:0 with HTTP; Mon, 18 Jun 2018 15:10:48 -0700 (PDT) In-Reply-To: References: <20180618175033.GA2627@beast> From: Kees Cook Date: Mon, 18 Jun 2018 15:10:48 -0700 X-Google-Sender-Auth: IMV0cYza_xQyHU0wzIPgbg3hOhY Message-ID: Subject: Re: [PATCH] dm: writecache: Use 2-factor allocator arguments To: Mikulas Patocka Cc: LKML , dm-devel@redhat.com, Dan Williams , Mike Snitzer , Alasdair Kergon 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 On Mon, Jun 18, 2018 at 2:12 PM, Mikulas Patocka wrote: > > > On Mon, 18 Jun 2018, Kees Cook wrote: > >> This adjusts the allocator calls to use the 2-factor argument style, as >> already done treewide for better defense against allocator overflows. >> Additionally adjusts style nit to avoid assignments in test expressions. >> >> Signed-off-by: Kees Cook >> --- >> drivers/md/dm-writecache.c | 16 ++++++++++------ >> 1 file changed, 10 insertions(+), 6 deletions(-) >> >> diff --git a/drivers/md/dm-writecache.c b/drivers/md/dm-writecache.c >> index 5961c7794ef3..7773f4c75701 100644 >> --- a/drivers/md/dm-writecache.c >> +++ b/drivers/md/dm-writecache.c >> @@ -259,7 +259,7 @@ static int persistent_memory_claim(struct dm_writecache *wc) >> if (da != p) { >> long i; >> wc->memory_map = NULL; >> - pages = kvmalloc(p * sizeof(struct page *), GFP_KERNEL); >> + pages = kvmalloc_array(p, sizeof(struct page *), GFP_KERNEL); >> if (!pages) { >> r = -ENOMEM; >> goto err2; >> @@ -859,7 +859,8 @@ static int writecache_alloc_entries(struct dm_writecache *wc) >> >> if (wc->entries) >> return 0; >> - wc->entries = vmalloc(sizeof(struct wc_entry) * wc->n_blocks); >> + wc->entries = vmalloc(array_size(sizeof(struct wc_entry), >> + wc->n_blocks)); >> if (!wc->entries) >> return -ENOMEM; >> for (b = 0; b < wc->n_blocks; b++) { >> @@ -1480,10 +1481,13 @@ static void __writecache_writeback_pmem(struct dm_writecache *wc, struct writeba >> bio_set_dev(&wb->bio, wc->dev->bdev); >> wb->bio.bi_iter.bi_sector = read_original_sector(wc, e); >> wb->page_offset = PAGE_SIZE; >> - if (max_pages <= WB_LIST_INLINE || >> - unlikely(!(wb->wc_list = kmalloc(max_pages * sizeof(struct wc_entry *), >> - GFP_NOIO | __GFP_NORETRY | >> - __GFP_NOMEMALLOC | __GFP_NOWARN)))) { >> + if (max_pages > WB_LIST_INLINE) >> + wb->wc_list = kmalloc_array(max_pages, >> + sizeof(struct wc_entry *), >> + GFP_NOIO | __GFP_NORETRY | >> + __GFP_NOMEMALLOC | >> + __GFP_NOWARN); >> + if (max_pages <= WB_LIST_INLINE || !wb->wc_list) { > > The rest of patch is OK - but you shouldn't duplicate the comparison > against WB_LIST_INLINE. I couldn't find a better way to avoid an assignment in a test... open to suggestions! :) -Kees -- Kees Cook Pixel Security