Received: by 2002:a5b:505:0:0:0:0:0 with SMTP id o5csp6045578ybp; Tue, 15 Oct 2019 08:49:00 -0700 (PDT) X-Google-Smtp-Source: APXvYqyg9U4AvlSEEOHFu/ykkaCTgB+0vwkBQkRfBG1MiF+3dj1f5hbkFM+O1yzFhP6xzy6ezYc/ X-Received: by 2002:a17:906:8391:: with SMTP id p17mr33636785ejx.216.1571154540018; Tue, 15 Oct 2019 08:49:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1571154540; cv=none; d=google.com; s=arc-20160816; b=sBS5tU+L0NurIeKk/1FH1grG7KWZg9h5moskfVWMI2HJZvuW15TTFZeDLlmLzb0Xkg giu3uzDhUpyMFztX30Lqx/448GcQTH5/zIZU7HFT+h1YZ9ZFgD2LVGAvqzocKFN9163s 7y5LD3dSdBcMned4aulXtDZ1jjmwjQ3GdxrdBt7fPpb9czNQzUHjfvunTIlUe6rzKqAs MyXT9pm1vQt6AdWA4nBe/r0UvldDCPCFr/aZLpaxmUwYdq86DdEKVId7hM8qZlPYD542 7t5wp+7XLNVjw8mv3rZkc74Dca6n46McFyfVcaXDn+PxJ26AsHISUwvnla3nGzJxKWGB Xy7w== 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:dkim-signature; bh=4UV/vP6Ak7zlZSSUsFjQtQPToq6GnlOm1PHifs316U4=; b=XzOIkLzUp7TjByvs39zo/hQPK1ggI5aZmAKknGpYvOcHCpvmWD5ZipH1DPQezjqEWM R5e+GWDTjcwufj1Ihpf2GSCx7yilzl+16b/ErLEmMqVEtxo+j/fU4eJRkszv34bLnZiM kraK4ZYkQy22Gj74IKYM9oN1p94Oec9tilR4bpfknBUowA21hU2ZoLuZQqh1r7YKoCd1 SCumlKUZyh2LWhKEJbDxlcg97l/4D+VS2ql03nJdEYM6C9BuaU6H7wLaZIqNzR0EM4kA ajrPtAYzeBf+VtUj/eznoMeN8/wSMszjLREThyjrqZO7rlx7nGOb5rp3GdA6Dg8sKYPQ ayzw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b=LsoAypu+; 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=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g9si15402710edg.58.2019.10.15.08.48.36; Tue, 15 Oct 2019 08:49:00 -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=@gmail.com header.s=20161025 header.b=LsoAypu+; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732167AbfJOPsb (ORCPT + 99 others); Tue, 15 Oct 2019 11:48:31 -0400 Received: from mail-qk1-f196.google.com ([209.85.222.196]:34393 "EHLO mail-qk1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726422AbfJOPsb (ORCPT ); Tue, 15 Oct 2019 11:48:31 -0400 Received: by mail-qk1-f196.google.com with SMTP id q203so19626475qke.1; Tue, 15 Oct 2019 08:48:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=4UV/vP6Ak7zlZSSUsFjQtQPToq6GnlOm1PHifs316U4=; b=LsoAypu+QZFDZOOMgWj57DzO9NJX1FgAGXdYolZJAF3A/bnTKstO7wULawxUrkeLnF LIZALF6eFW4JRe91stBT6ts8IaIkGyHBFFDmQnJfq2yT/aAqEAZSmvDelPxyuKUqHiRE VEwh2YBOJgmKRUXKRKcELaCCvsJquUZJXkI3j2Lut0LhIZ6BmmRPQ0juk5aiZifh2sKB BuM8uI/lEYzA7uFjLvNJU3wOK31R6M8dqKpmvI5hACGwcYhs7HzPViT4DiEGTY1DLndY CGuRlYOGOjQ4Xosg93QM8Wpixg799Rm48Zf7pRmxjbeVnB5ewpnrDMl2kKbzbTbqENgt RRYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:from:to:cc:subject:message-id :references:mime-version:content-disposition:in-reply-to:user-agent; bh=4UV/vP6Ak7zlZSSUsFjQtQPToq6GnlOm1PHifs316U4=; b=GrZalv11NN+/wA0njRs2vKNHTW1S/iJnkexzABWjDgaGK5/wpz8UAteCPPVdo5hwWs sNmrGXFsZmuhi1Lav0ZwIIBLwOenUT/jMMosY7t1Kyx9KDT3H/1pcrkGbaE4FnfqFgnq 7pULvn10XYdvLvkqc+lTPiIxt/xKOciWM+3O1/wTKEu1owZOp1eKgV5BAF0eD2C1bsa0 /mG+nBTP5lOAVmlFRoFmWCKJWLlBu2i9r+lfggtDhVY9wXmJx2B6fwU8SsFIayOdknbl C5L6ZFx8LOMGIaHJ+e6nHdFtmHGyjSQqjbmSCYFRZbUwBwg0XZ5sdNsUakJ176krKHkZ 38sw== X-Gm-Message-State: APjAAAW1QYFuk8MDjzbr3aYC5x8NjjKaLbU6D461vugrE7XmVEbXsZHW +5rCuotivBBGHVebtEnIB/w= X-Received: by 2002:a37:a50a:: with SMTP id o10mr35684316qke.115.1571154509896; Tue, 15 Oct 2019 08:48:29 -0700 (PDT) Received: from localhost ([2620:10d:c091:500::3:2a53]) by smtp.gmail.com with ESMTPSA id p77sm10954411qke.6.2019.10.15.08.48.28 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 15 Oct 2019 08:48:29 -0700 (PDT) Date: Tue, 15 Oct 2019 08:48:27 -0700 From: Tejun Heo To: Julia Lawall Cc: Jens Axboe , kbuild-all@lists.01.org, linux-kernel@vger.kernel.org, kernel-team@fb.com, linux-block@vger.kernel.org Subject: [PATCH block/for-linus] blkcg: fix botched pd_prealloc error handling in blkcg_activate_policy() Message-ID: <20191015154827.GK18794@devbig004.ftw2.facebook.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org While fixing ->pd_alloc_fn() bug, ab94b0382d81 ("blkcg: Fix ->pd_alloc_fn() being called with the wrong blkcg on policy activation") broke the pd_prealloc error handling. * pd's were freed using kfree(). They should be freed with ->pd_free_fn(). * pd_prealloc could be kfree()'d and then ->pd_free_fn()'d again. * When GFP_KERNEL allocation fails, pinned_blkg wasn't put. There are also a couple existing issues. * Each pd is initialized as they get allocated. If alloc fails, the policy will get freed with pd's initialized on it. * After the above partial failure, the partial pds are not freed. This patch fixes all of the above issues. Signed-off-by: Tejun Heo Reported-by: Julia Lawall Fixes: ab94b0382d81 ("blkcg: Fix ->pd_alloc_fn() being called with the wrong blkcg on policy activation") --- block/blk-cgroup.c | 44 +++++++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 13 deletions(-) --- a/block/blk-cgroup.c +++ b/block/blk-cgroup.c @@ -1373,13 +1373,14 @@ int blkcg_activate_policy(struct request retry: spin_lock_irq(&q->queue_lock); - /* blkg_list is pushed at the head, reverse walk to init parents first */ + /* blkg_list is pushed at the head, reverse walk to allocate parents first */ list_for_each_entry_reverse(blkg, &q->blkg_list, q_node) { struct blkg_policy_data *pd; if (blkg->pd[pol->plid]) continue; + /* If prealloc matches, use it; otherwise try GFP_NOWAIT */ if (blkg == pinned_blkg) { pd = pd_prealloc; pd_prealloc = NULL; @@ -1389,6 +1390,10 @@ retry: } if (!pd) { + /* + * GFP_NOWAIT failed. Free the existing one and + * prealloc for @blkg w/ GFP_KERNEL. + */ if (pinned_blkg) blkg_put(pinned_blkg); blkg_get(blkg); @@ -1396,38 +1401,51 @@ retry: spin_unlock_irq(&q->queue_lock); - kfree(pd_prealloc); + if (pd_prealloc) + pol->pd_free_fn(pd_prealloc); pd_prealloc = pol->pd_alloc_fn(GFP_KERNEL, q, blkg->blkcg); - if (pd_prealloc) { + if (pd_prealloc) goto retry; - } else { - ret = -ENOMEM; - goto out_bypass_end; - } + else + goto enomem; } blkg->pd[pol->plid] = pd; pd->blkg = blkg; pd->plid = pol->plid; - if (pol->pd_init_fn) - pol->pd_init_fn(pd); } - if (pinned_blkg) - blkg_put(pinned_blkg); - kfree(pd_prealloc); + /* all allocated, init in the same order */ + if (pol->pd_init_fn) + list_for_each_entry_reverse(blkg, &q->blkg_list, q_node) + pol->pd_init_fn(blkg->pd[pol->plid]); __set_bit(pol->plid, q->blkcg_pols); ret = 0; spin_unlock_irq(&q->queue_lock); -out_bypass_end: +out: if (queue_is_mq(q)) blk_mq_unfreeze_queue(q); + if (pinned_blkg) + blkg_put(pinned_blkg); if (pd_prealloc) pol->pd_free_fn(pd_prealloc); return ret; + +enomem: + /* alloc failed, nothing's initialized yet, free everything */ + spin_lock_irq(&q->queue_lock); + list_for_each_entry(blkg, &q->blkg_list, q_node) { + if (blkg->pd[pol->plid]) { + pol->pd_free_fn(blkg->pd[pol->plid]); + blkg->pd[pol->plid] = NULL; + } + } + spin_unlock_irq(&q->queue_lock); + ret = -ENOMEM; + goto out; } EXPORT_SYMBOL_GPL(blkcg_activate_policy);