Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp4415696pxa; Mon, 10 Aug 2020 08:34:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxrguWZY/xJClMAl5Dqghv/JsnPsy046I+hX7ifFHoFYHVMcKDgLzg832rBrC958ByDWeKK X-Received: by 2002:a17:906:5902:: with SMTP id h2mr23410917ejq.423.1597073657090; Mon, 10 Aug 2020 08:34:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597073657; cv=none; d=google.com; s=arc-20160816; b=aWWp7Wkbbaw8vx0FEnr9Hr+uZT9KlsbNJ13JewlNk6cHy2VgiyoKhHSn03bgpwYdrr ovs1K/UVvKduobhqMkvt+M8YjAlFFzqKbmbMZf4Pa8eTXDVwBalvK2knFZanm8AYdkEN IQQ32uHlEc6o6ktdY4XiNlR5+LTn9tE3irqTxlnm0NYPJ5Sxt005C7KMH7h6UB4TTN8G Lf5U+e0YQZ6mvVW13J5jZ9tj6pum8ri8rw3zA8YYxRulrD78VidbJEnY/ZotblQkYttg UF6XP95hMjWFDsZGMe+P2zCNsh8Tk4/U8Suzrs2LqySMZtXvAV3UoFSPPSM7+0okXXRX RO2Q== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=aDZZtFMxRjUqzHomdjUjXJN8yZwhypWfL9+ArfFYF8M=; b=FCGlIdUj1TfIf2OL/ljJM9I8vUIe1uOb2njL1ir0L5b+OPKNayOajnVBxTKZBEMuVE st/J6VlqZJL/owA/bt2WPQBB1UsPwvrVbPYYMMjw4BLrTtqqrj15GhyJy2E/kn4CFv+l bJaLTFWGqbHORonkc4Ohpmr0cWNtm+UX0Nqw4b6XZS/WCiaCKzuDVDixcExhsq4qjV7h BCpY33UqanLeMajPbgqxID7XM+NtriiUo61VNxLpzwSWgpcRVJaTHz8J8vcrp7CCKE/v fywwOqJokbqwk47XCAUXPdoGCXKou/MwXfLDJcFo6py3r1K3LLwkiCPDHS2ScqZ4xDtP /4wA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="l/3Q5MR3"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id x24si10930304ejs.198.2020.08.10.08.33.53; Mon, 10 Aug 2020 08:34:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b="l/3Q5MR3"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729033AbgHJPc6 (ORCPT + 99 others); Mon, 10 Aug 2020 11:32:58 -0400 Received: from mail.kernel.org ([198.145.29.99]:36744 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729042AbgHJPaC (ORCPT ); Mon, 10 Aug 2020 11:30:02 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 1039E22D02; Mon, 10 Aug 2020 15:30:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1597073402; bh=l/JSZWTrxiPgtyVy4G9PmA8sZJAzsemOFxyR3InUXEA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=l/3Q5MR3MWAko1g4X3YJmIJk4N1jZ2meLxcb8EdwIA6jVZ+ca9Qpb83S2nUSzCPYO PKxNXnAABC7jHQBwqr7v4KFklaZ+2V/5y3wdFSBL+TL3Aw1G1+nl8jc5wq+rPbuJWr 8OapwX/16ECTEgH6476zqKOiDpjFHfoZF8WreTtA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot+7a0d9d0b26efefe61780@syzkaller.appspotmail.com, Suren Baghdasaryan , "Joel Fernandes (Google)" Subject: [PATCH 4.19 07/48] staging: android: ashmem: Fix lockdep warning for write operation Date: Mon, 10 Aug 2020 17:21:29 +0200 Message-Id: <20200810151804.577411967@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200810151804.199494191@linuxfoundation.org> References: <20200810151804.199494191@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Suren Baghdasaryan commit 3e338d3c95c735dc3265a86016bb4c022ec7cadc upstream. syzbot report [1] describes a deadlock when write operation against an ashmem fd executed at the time when ashmem is shrinking its cache results in the following lock sequence: Possible unsafe locking scenario: CPU0 CPU1 ---- ---- lock(fs_reclaim); lock(&sb->s_type->i_mutex_key#13); lock(fs_reclaim); lock(&sb->s_type->i_mutex_key#13); kswapd takes fs_reclaim and then inode_lock while generic_perform_write takes inode_lock and then fs_reclaim. However ashmem does not support writing into backing shmem with a write syscall. The only way to change its content is to mmap it and operate on mapped memory. Therefore the race that lockdep is warning about is not valid. Resolve this by introducing a separate lockdep class for the backing shmem inodes. [1]: https://lkml.kernel.org/lkml/0000000000000b5f9d059aa2037f@google.com/ Reported-by: syzbot+7a0d9d0b26efefe61780@syzkaller.appspotmail.com Signed-off-by: Suren Baghdasaryan Cc: stable Reviewed-by: Joel Fernandes (Google) Link: https://lore.kernel.org/r/20200730192632.3088194-1-surenb@google.com Signed-off-by: Greg Kroah-Hartman --- drivers/staging/android/ashmem.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) --- a/drivers/staging/android/ashmem.c +++ b/drivers/staging/android/ashmem.c @@ -95,6 +95,15 @@ static DEFINE_MUTEX(ashmem_mutex); static struct kmem_cache *ashmem_area_cachep __read_mostly; static struct kmem_cache *ashmem_range_cachep __read_mostly; +/* + * A separate lockdep class for the backing shmem inodes to resolve the lockdep + * warning about the race between kswapd taking fs_reclaim before inode_lock + * and write syscall taking inode_lock and then fs_reclaim. + * Note that such race is impossible because ashmem does not support write + * syscalls operating on the backing shmem. + */ +static struct lock_class_key backing_shmem_inode_class; + static inline unsigned long range_size(struct ashmem_range *range) { return range->pgend - range->pgstart + 1; @@ -395,6 +404,7 @@ static int ashmem_mmap(struct file *file if (!asma->file) { char *name = ASHMEM_NAME_DEF; struct file *vmfile; + struct inode *inode; if (asma->name[ASHMEM_NAME_PREFIX_LEN] != '\0') name = asma->name; @@ -406,6 +416,8 @@ static int ashmem_mmap(struct file *file goto out; } vmfile->f_mode |= FMODE_LSEEK; + inode = file_inode(vmfile); + lockdep_set_class(&inode->i_rwsem, &backing_shmem_inode_class); asma->file = vmfile; /* * override mmap operation of the vmfile so that it can't be