From patchwork Wed Apr 22 00:14:19 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michel Lespinasse X-Patchwork-Id: 11502555 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1A44B14DD for ; Wed, 22 Apr 2020 00:14:50 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id D1B1820575 for ; Wed, 22 Apr 2020 00:14:49 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="BHEsdusq" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D1B1820575 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 3DEAD8E000C; Tue, 21 Apr 2020 20:14:43 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 3930F8E0003; Tue, 21 Apr 2020 20:14:43 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1E2D08E000C; Tue, 21 Apr 2020 20:14:43 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0142.hostedemail.com [216.40.44.142]) by kanga.kvack.org (Postfix) with ESMTP id 04D698E0003 for ; Tue, 21 Apr 2020 20:14:43 -0400 (EDT) Received: from smtpin19.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id BEDCC4DD8 for ; Wed, 22 Apr 2020 00:14:42 +0000 (UTC) X-FDA: 76733570004.19.death14_4635a5f570e4e X-Spam-Summary: 2,0,0,f6789e66f2f88822,d41d8cd98f00b204,3cyyfxgykcaq0epoirksskpi.gsqpmry1-qqozego.svk@flex--walken.bounces.google.com,,RULES_HIT:41:69:152:355:379:541:800:960:973:988:989:1260:1277:1313:1314:1345:1359:1437:1516:1518:1535:1543:1593:1594:1711:1730:1747:1777:1792:2393:2553:2559:2562:2693:2901:3138:3139:3140:3141:3142:3152:3354:3865:3866:3867:3870:3871:3872:3874:4117:4250:4321:5007:6261:6653:6742:7875:7903:9969:10004:10400:11026:11473:11658:11914:12043:12114:12296:12297:12438:12555:12895:12986:14181:14394:14659:14721:21080:21324:21433:21444:21450:21451:21627:21740:21795:21987:21990:30046:30051:30054:30064:30090,0,RBL:209.85.215.202:@flex--walken.bounces.google.com:.lbl8.mailshell.net-66.100.201.100 62.18.0.100,CacheIP:none,Bayesian:0.5,0.5,0.5,Netcheck:none,DomainCache:0,MSF:not bulk,SPF:fp,MSBL:0,DNSBL:neutral,Custom_rules:0:0:0,LFtime:23,LUA_SUMMARY:none X-HE-Tag: death14_4635a5f570e4e X-Filterd-Recvd-Size: 6162 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.202]) by imf46.hostedemail.com (Postfix) with ESMTP for ; Wed, 22 Apr 2020 00:14:42 +0000 (UTC) Received: by mail-pg1-f202.google.com with SMTP id c22so221407pgb.7 for ; Tue, 21 Apr 2020 17:14:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=trhg9Yk7vVwhm5tzbCAJRNxhtIH/dkjCFeCx4usvKdo=; b=BHEsdusqub6ITSFVJH1OMaviWdq8JZTGu52eYcgWIfShAzJitgHwHUZS3IvSOm1BNK JoTHHdeOuTWVSp3Aou4Wk3xTIFTTRqDcwpW5wgRDbQQS4DxJIytHgthOHaGUvHwNjkQe oWa4Qmnw57wWVZ2tndrp4TK2iFfiZ92Halo7sVxnJxPaolU+K9JmS8I4jrq4jhbbgvzB JS1k9LoBdeRYsIdS5zOLVvh5XXfUBfumGNrf/bEdBVphpikbeiEhVjLZcTM799RvpEMp U4XZNdb62rmL8l+SiqDEJ+cKSBOrXG5vnp8CblZGvTiay4osZPNIOplUBnMK+AHVlXKu JQpw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=trhg9Yk7vVwhm5tzbCAJRNxhtIH/dkjCFeCx4usvKdo=; b=LqWE15QXycip+IbB8HD68pixAlPflroWRXlRs4HbpGQRRLV/t0/eJ6oe8IWOeg+36v XqqUXnUQAFdFBCzHH7fg27wiip2FOdHW2VRMI7dPrpayvYq3qYnRrdGJgHme9FKvKfLH cbN9r8kz3KhPKSlyFXQ4zg7kvTm0e9iTlKIS753+KlRE12jMwYILd1jKq69+fu09en+Y 7ycA8bRJs7mRYKzyfUZvTPf6Rzz0b7t2w+EqUkwC21I4IgCoSQDGnC8dPUoCjpNxLOVB XrY/CuUyRqy1OFd6SIv4gy8eAZsYpOZaxnFhmK0Zg8TyuniP1BXpXHfSO0GLxYemk0Jb qHWg== X-Gm-Message-State: AGi0Puat0LN6iX4er1sDxTMtvg56WKCXzg3uYckXfC4XrFCkxF8TT5Zc pZ2NsvcGzn7FL3o50YKTF7ZNW/6F9Eo= X-Google-Smtp-Source: APiQypKCBgqau9oBZcw41su0qMphAJhDDfKbrccQB8zcf2ElgGBtskpCaousaW7ZlmKXUWUS3kvrDn3koqM= X-Received: by 2002:a17:90a:dc83:: with SMTP id j3mr6004567pjv.6.1587514481568; Tue, 21 Apr 2020 17:14:41 -0700 (PDT) Date: Tue, 21 Apr 2020 17:14:19 -0700 In-Reply-To: <20200422001422.232330-1-walken@google.com> Message-Id: <20200422001422.232330-8-walken@google.com> Mime-Version: 1.0 References: <20200422001422.232330-1-walken@google.com> X-Mailer: git-send-email 2.26.1.301.g55bc3eb7cb9-goog Subject: [PATCH v5 07/10] mmap locking API: add mmap_read_trylock_non_owner() From: Michel Lespinasse To: Andrew Morton , linux-mm Cc: LKML , Peter Zijlstra , Laurent Dufour , Vlastimil Babka , Matthew Wilcox , Liam Howlett , Jerome Glisse , Davidlohr Bueso , David Rientjes , Hugh Dickins , Ying Han , Jason Gunthorpe , Daniel Jordan , Michel Lespinasse 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: 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 --- 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_); } }