Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp4419102pxa; Mon, 10 Aug 2020 08:38:47 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw1Jx2OxPmQZVWc1dwt0fXNf2xjQHQaz0QYwpHY98JOmDb0dEsyUvdTqEeqVD3gDs1Umf9C X-Received: by 2002:a17:906:8595:: with SMTP id v21mr23004642ejx.347.1597073926922; Mon, 10 Aug 2020 08:38:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597073926; cv=none; d=google.com; s=arc-20160816; b=eJQhPgp4oaJAW9QM4c9q8k5WgIuCTgyJSaTHMcH0QnguYp3R/gHeQwQAHiS0PAXNCL MmY+bxjn4p4BlF84jzGoiZISQw6Xz1qySI4wF9GB7xnXjeKsflIuUjwGFrXES0H5u2Ji jH1ARbtks9UjxX9klgKfSbq8xRiPkner94MAAYt7/Zs/BfDRCxfDRpNB9DZEoIiVmS0Q gXPWOTpUQKudzSdZjqm7XKIcquAa2U0sN0DUWJIUfUsexkwOOfPcltdp8lqlVboWdUvp mbSHTRqI6e+gLFwwc/6dBTWDrUSlvbsXSk8KREJWs1XGUYSPaLsXeYZEL2GldztP0irq s5lQ== 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=tdasK8OR97xaGIdyuXVf5QHdMAKgJ1riqGLqoXKcqDs=; b=v5EYiwxHaaGea3wkWZX/zOeKivn/sfaOWOVVyPVDpiTzypeg5uQVnRnvprq3VZb/eZ OWjHQR3gdEbaC+vDxmIf6bIyLJRHKdCwKBCHY4I4Y+xN64Owf0icyzPG8NWFKcBs1Rxs oHT28IyppgmNkZAKD0Z50bnQdEac/q7voAzIBcEqa7SdsP283wKDjJUVy0ea+rliRQ2W 3qCxXv3tbG8fjQGKI33vktC4LUYqaNAIpK6mgi742f1kMJh+iu6v1oFGdlG67lAkQ64x qpLSDXjnt5rsc3n7AHLaVENAEgRsBVExN+gVc2giuIP1MGFCZ23vPMarw3ENkx6qwoiT Rq3Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=O19SBIeQ; 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 89si10739735edr.415.2020.08.10.08.38.23; Mon, 10 Aug 2020 08:38:46 -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=O19SBIeQ; 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 S1728920AbgHJPh4 (ORCPT + 99 others); Mon, 10 Aug 2020 11:37:56 -0400 Received: from mail.kernel.org ([198.145.29.99]:60202 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728616AbgHJP0S (ORCPT ); Mon, 10 Aug 2020 11:26:18 -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 AB05D20772; Mon, 10 Aug 2020 15:26:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1597073178; bh=x7ozp1mq3C1BY63eMKEcpS3peJBmk+UfyCtDfXhOuww=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=O19SBIeQk3JW4bUCq77m7CBQKbKnuvlgDD7yLnsyuBoOvQn8jXLEqXOO2zYdl33M+ EBbmyeL6qE55G1tB+DYKLgtVBpDTFXrNtCL+JvHFtFiNCJ2Pv0aH/FAgcIDr4pe+lF xjP1r5TiuEYf2Y6IFyys2ulkTOUEOCWnkeo+uPZI= 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 5.4 14/67] staging: android: ashmem: Fix lockdep warning for write operation Date: Mon, 10 Aug 2020 17:21:01 +0200 Message-Id: <20200810151810.131585891@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200810151809.438685785@linuxfoundation.org> References: <20200810151809.438685785@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; @@ -396,6 +405,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; @@ -407,6 +417,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