Received: by 10.192.165.148 with SMTP id m20csp579548imm; Wed, 25 Apr 2018 04:36:40 -0700 (PDT) X-Google-Smtp-Source: AIpwx49cIaZptXlsYjRVMRmdqu1Z0AIAqZqlRB7ZFHP1iY8ABatQCBdVtUdPS7IZ2rcxxQYBKHYK X-Received: by 10.98.89.89 with SMTP id n86mr27697307pfb.158.1524656200380; Wed, 25 Apr 2018 04:36:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524656200; cv=none; d=google.com; s=arc-20160816; b=ddCdeE3YI7ABU2dAUw+gj8TmSN2L2gklKUE3MmVDD5rIXA5ol3/CPvDNhf8idiwBJj we6NmarEtpGyMl3l7ulmqFQMiCZCHWR3hhlFPbU5i0bwYnhf8eFldoUBbR0SYAwTrh5w onXe0s0OAIOfpnOarKadkkiMQqfXTWESsd7aqSjfxYERc+hRcl7gJ5J/kcR/dphjIVUS 8ZeVOTLwttDpTiiPJTGtZNGMmbHbus2gCtezDL2Nr3Ae/1v2SEVWHcpTkBTznzQmMALu o9o5v9CtkLKRJk0/3iwQ/mLHNUFEmelxE9y8VKUsa1hcQ8tDMNwWQlscFBE6XcfSczzF ccbQ== 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=Dc66OEUFANV54DJ7VRMiM3LlkErSg1ZPocDFbgLVvDE=; b=B+usjX4rkkmWVRxfcUGIJEJrLYsKzmYsdJ7T9Oo8iJOoDzVuh/1HXyUAQ8wBfp3D/w w10yhHb0gkTA2qngmdGrlil1wBn1EJzwfVaitcsHwAi0soUEOz3ity5XoQPPlIGB+Y+e eoxbRrq3kUTIoLz5zuHaaAFEM5PkqLzhp2ZEuLagUOes0yArZw5VaPmDZcTH+3E7ZU8v 66tQ7CX6NDRtHWmNJH4lmkYutbp5rlefhf5HI+hWFl1wkIa3vo3ZgOc6zZBayd3ptfBr junWSJvmZSB5GpLPXA59hJDncl6lCRbzvYVJKe6aP2z+CVLMEkcjMkWMdz3X48DZ7Jq4 XvgQ== 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 o6si13695729pgs.51.2018.04.25.04.36.25; Wed, 25 Apr 2018 04:36:40 -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 S1753274AbeDYLfZ (ORCPT + 99 others); Wed, 25 Apr 2018 07:35:25 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:50764 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752461AbeDYKeN (ORCPT ); Wed, 25 Apr 2018 06:34:13 -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 8375E41C; Wed, 25 Apr 2018 10:34:12 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Tetsuo Handa , syzbot , Al Viro , Michal Hocko Subject: [PATCH 4.16 17/26] mm,vmscan: Allow preallocating memory for register_shrinker(). Date: Wed, 25 Apr 2018 12:33:26 +0200 Message-Id: <20180425103315.536235852@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180425103314.842517924@linuxfoundation.org> References: <20180425103314.842517924@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review 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: Tetsuo Handa commit 8e04944f0ea8b838399049bdcda920ab36ae3b04 upstream. syzbot is catching so many bugs triggered by commit 9ee332d99e4d5a97 ("sget(): handle failures of register_shrinker()"). That commit expected that calling kill_sb() from deactivate_locked_super() without successful fill_super() is safe, but the reality was different; some callers assign attributes which are needed for kill_sb() after sget() succeeds. For example, [1] is a report where sb->s_mode (which seems to be either FMODE_READ | FMODE_EXCL | FMODE_WRITE or FMODE_READ | FMODE_EXCL) is not assigned unless sget() succeeds. But it does not worth complicate sget() so that register_shrinker() failure path can safely call kill_block_super() via kill_sb(). Making alloc_super() fail if memory allocation for register_shrinker() failed is much simpler. Let's avoid calling deactivate_locked_super() from sget_userns() by preallocating memory for the shrinker and making register_shrinker() in sget_userns() never fail. [1] https://syzkaller.appspot.com/bug?id=588996a25a2587be2e3a54e8646728fb9cae44e7 Signed-off-by: Tetsuo Handa Reported-by: syzbot Cc: Al Viro Cc: Michal Hocko Signed-off-by: Al Viro Signed-off-by: Greg Kroah-Hartman --- fs/super.c | 9 ++++----- include/linux/shrinker.h | 7 +++++-- mm/vmscan.c | 21 ++++++++++++++++++++- 3 files changed, 29 insertions(+), 8 deletions(-) --- a/fs/super.c +++ b/fs/super.c @@ -166,6 +166,7 @@ static void destroy_unused_super(struct security_sb_free(s); put_user_ns(s->s_user_ns); kfree(s->s_subtype); + free_prealloced_shrinker(&s->s_shrink); /* no delays needed */ destroy_super_work(&s->destroy_work); } @@ -251,6 +252,8 @@ static struct super_block *alloc_super(s s->s_shrink.count_objects = super_cache_count; s->s_shrink.batch = 1024; s->s_shrink.flags = SHRINKER_NUMA_AWARE | SHRINKER_MEMCG_AWARE; + if (prealloc_shrinker(&s->s_shrink)) + goto fail; return s; fail: @@ -517,11 +520,7 @@ retry: hlist_add_head(&s->s_instances, &type->fs_supers); spin_unlock(&sb_lock); get_filesystem(type); - err = register_shrinker(&s->s_shrink); - if (err) { - deactivate_locked_super(s); - s = ERR_PTR(err); - } + register_shrinker_prepared(&s->s_shrink); return s; } --- a/include/linux/shrinker.h +++ b/include/linux/shrinker.h @@ -75,6 +75,9 @@ struct shrinker { #define SHRINKER_NUMA_AWARE (1 << 0) #define SHRINKER_MEMCG_AWARE (1 << 1) -extern int register_shrinker(struct shrinker *); -extern void unregister_shrinker(struct shrinker *); +extern int prealloc_shrinker(struct shrinker *shrinker); +extern void register_shrinker_prepared(struct shrinker *shrinker); +extern int register_shrinker(struct shrinker *shrinker); +extern void unregister_shrinker(struct shrinker *shrinker); +extern void free_prealloced_shrinker(struct shrinker *shrinker); #endif --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -258,7 +258,7 @@ unsigned long lruvec_lru_size(struct lru /* * Add a shrinker callback to be called from the vm. */ -int register_shrinker(struct shrinker *shrinker) +int prealloc_shrinker(struct shrinker *shrinker) { size_t size = sizeof(*shrinker->nr_deferred); @@ -268,10 +268,29 @@ int register_shrinker(struct shrinker *s shrinker->nr_deferred = kzalloc(size, GFP_KERNEL); if (!shrinker->nr_deferred) return -ENOMEM; + return 0; +} + +void free_prealloced_shrinker(struct shrinker *shrinker) +{ + kfree(shrinker->nr_deferred); + shrinker->nr_deferred = NULL; +} +void register_shrinker_prepared(struct shrinker *shrinker) +{ down_write(&shrinker_rwsem); list_add_tail(&shrinker->list, &shrinker_list); up_write(&shrinker_rwsem); +} + +int register_shrinker(struct shrinker *shrinker) +{ + int err = prealloc_shrinker(shrinker); + + if (err) + return err; + register_shrinker_prepared(shrinker); return 0; } EXPORT_SYMBOL(register_shrinker);