From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.2 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_SANE_1 autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 43021C433DF for ; Fri, 22 May 2020 14:20:50 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id ED3502237B for ; Fri, 22 May 2020 14:20:49 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org ED3502237B Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=stgolabs.net Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 8107280008; Fri, 22 May 2020 10:20:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7E7D280007; Fri, 22 May 2020 10:20:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7253B80008; Fri, 22 May 2020 10:20:49 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0151.hostedemail.com [216.40.44.151]) by kanga.kvack.org (Postfix) with ESMTP id 5A9B080007 for ; Fri, 22 May 2020 10:20:49 -0400 (EDT) Received: from smtpin29.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 1C91C8245571 for ; Fri, 22 May 2020 14:20:49 +0000 (UTC) X-FDA: 76844566218.29.shame47_242e1af797c08 X-HE-Tag: shame47_242e1af797c08 X-Filterd-Recvd-Size: 5488 Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) by imf26.hostedemail.com (Postfix) with ESMTP for ; Fri, 22 May 2020 14:20:48 +0000 (UTC) X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id EE843B15F; Fri, 22 May 2020 14:20:48 +0000 (UTC) Date: Fri, 22 May 2020 07:15:14 -0700 From: Davidlohr Bueso To: Michel Lespinasse Cc: Andrew Morton , linux-mm , LKML , Peter Zijlstra , Laurent Dufour , Vlastimil Babka , Matthew Wilcox , Liam Howlett , Jerome Glisse , David Rientjes , Hugh Dickins , Ying Han , Jason Gunthorpe , Daniel Jordan , John Hubbard Subject: Re: [PATCH v6 07/12] mmap locking API: add mmap_read_trylock_non_owner() Message-ID: <20200522141514.ctpmu4ksw4lj6udu@linux-p48b> Mail-Followup-To: Michel Lespinasse , Andrew Morton , linux-mm , LKML , Peter Zijlstra , Laurent Dufour , Vlastimil Babka , Matthew Wilcox , Liam Howlett , Jerome Glisse , David Rientjes , Hugh Dickins , Ying Han , Jason Gunthorpe , Daniel Jordan , John Hubbard References: <20200520052908.204642-1-walken@google.com> <20200520052908.204642-8-walken@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <20200520052908.204642-8-walken@google.com> User-Agent: NeoMutt/20180716 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Tue, 19 May 2020, Michel Lespinasse wrote: >Add a couple APIs used by kernel/bpf/stackmap.c only: >- mmap_read_trylock_non_owner() >- mmap_read_unlock_non_owner() (may be called from a work queue). > >It's still not ideal that bpf/stackmap subverts the lock ownership >in this way. Thanks to Peter Zijlstra for suggesting this API as the >least-ugly way of addressing this in the short term. > >Signed-off-by: Michel Lespinasse >Reviewed-by: Daniel Jordan >Reviewed-by: Vlastimil Babka Sigh, bpf, but ok. Reviewed-by: Davidlohr Bueso >--- > include/linux/mmap_lock.h | 14 ++++++++++++++ > kernel/bpf/stackmap.c | 17 +++++------------ > 2 files changed, 19 insertions(+), 12 deletions(-) > >diff --git a/include/linux/mmap_lock.h b/include/linux/mmap_lock.h >index a757cb30ae77..d1826ce42f00 100644 >--- a/include/linux/mmap_lock.h >+++ b/include/linux/mmap_lock.h >@@ -56,4 +56,18 @@ static inline void mmap_read_unlock(struct mm_struct *mm) > up_read(&mm->mmap_sem); > } > >+static inline bool mmap_read_trylock_non_owner(struct mm_struct *mm) >+{ >+ if (down_read_trylock(&mm->mmap_sem)) { >+ rwsem_release(&mm->mmap_sem.dep_map, _RET_IP_); >+ return true; >+ } >+ return false; >+} >+ >+static inline void mmap_read_unlock_non_owner(struct mm_struct *mm) >+{ >+ up_read_non_owner(&mm->mmap_sem); >+} >+ > #endif /* _LINUX_MMAP_LOCK_H */ >diff --git a/kernel/bpf/stackmap.c b/kernel/bpf/stackmap.c >index 11d41f0c7005..998968659892 100644 >--- a/kernel/bpf/stackmap.c >+++ b/kernel/bpf/stackmap.c >@@ -33,7 +33,7 @@ struct bpf_stack_map { > /* irq_work to run up_read() for build_id lookup in nmi context */ > struct stack_map_irq_work { > struct irq_work irq_work; >- struct rw_semaphore *sem; >+ struct mm_struct *mm; > }; > > static void do_up_read(struct irq_work *entry) >@@ -44,8 +44,7 @@ static void do_up_read(struct irq_work *entry) > return; > > work = container_of(entry, struct stack_map_irq_work, irq_work); >- up_read_non_owner(work->sem); >- work->sem = NULL; >+ mmap_read_unlock_non_owner(work->mm); > } > > static DEFINE_PER_CPU(struct stack_map_irq_work, up_read_work); >@@ -317,7 +316,7 @@ static void stack_map_get_build_id_offset(struct bpf_stack_build_id *id_offs, > * with build_id. > */ > if (!user || !current || !current->mm || irq_work_busy || >- mmap_read_trylock(current->mm) == 0) { >+ !mmap_read_trylock_non_owner(current->mm)) { > /* cannot access current->mm, fall back to ips */ > for (i = 0; i < trace_nr; i++) { > id_offs[i].status = BPF_STACK_BUILD_ID_IP; >@@ -342,16 +341,10 @@ static void stack_map_get_build_id_offset(struct bpf_stack_build_id *id_offs, > } > > if (!work) { >- mmap_read_unlock(current->mm); >+ mmap_read_unlock_non_owner(current->mm); > } else { >- work->sem = ¤t->mm->mmap_sem; >+ work->mm = current->mm; > irq_work_queue(&work->irq_work); >- /* >- * The irq_work will release the mmap_sem with >- * up_read_non_owner(). The rwsem_release() is called >- * here to release the lock from lockdep's perspective. >- */ >- rwsem_release(¤t->mm->mmap_sem.dep_map, _RET_IP_); > } > } > >-- >2.26.2.761.g0e0b3e54be-goog >