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=-5.3 required=3.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_PASS, USER_AGENT_MUTT autolearn=ham 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 8AC5EC282D7 for ; Wed, 30 Jan 2019 21:05:42 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 3BBFE20881 for ; Wed, 30 Jan 2019 21:05:42 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="R6PgKMDj" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727068AbfA3VFl (ORCPT ); Wed, 30 Jan 2019 16:05:41 -0500 Received: from bombadil.infradead.org ([198.137.202.133]:55126 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725613AbfA3VFk (ORCPT ); Wed, 30 Jan 2019 16:05:40 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20170209; h=In-Reply-To:Content-Type:MIME-Version :References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=6uwUwA8q50lQ77DpD8G6uWu0q1qNbqZqUPtAWwBRMC4=; b=R6PgKMDjX/XFWOAjYY9F9feob lEm6RVggXSGVjQk3BRObTnGre34Ytms8J+NhGtYErTvZ9muVqFpx0mNkY9VUCVEO3f5l2SVLag/J5 YmxW/xsmRQJXGVlFR5YvRBEapINBWt8b+t/QvF/MfiHiI7ZuD4XH9/PT/vN4oH7V59BUkrBs9pvqC lx6EVQfzQkiR57UmSw7XQcYHp+DY3fUj4L/RROo3T1DIkLs//WTDb2+ts/83TfEXM+Wbd2sGIQlDl qfGVj4gg1mU9g3jrcJ42747EJi3RUM7sjLhZ6tts0HY4bUV8g4kWOfT88L/Za9nq9xFAkI14cv/UR V0HGp1ViA==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=hirez.programming.kicks-ass.net) by bombadil.infradead.org with esmtpsa (Exim 4.90_1 #2 (Red Hat Linux)) id 1gox35-0002E4-7c; Wed, 30 Jan 2019 21:05:31 +0000 Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 1B45120D0E322; Wed, 30 Jan 2019 22:05:29 +0100 (CET) Date: Wed, 30 Jan 2019 22:05:29 +0100 From: Peter Zijlstra To: Alexei Starovoitov Cc: davem@davemloft.net, daniel@iogearbox.net, jannh@google.com, paulmck@linux.ibm.com, will.deacon@arm.com, mingo@redhat.com, netdev@vger.kernel.org, kernel-team@fb.com Subject: Re: [PATCH v5 bpf-next 1/9] bpf: introduce bpf_spin_lock Message-ID: <20190130210529.GI2278@hirez.programming.kicks-ass.net> References: <20190128025010.342241-1-ast@kernel.org> <20190128025010.342241-2-ast@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190128025010.342241-2-ast@kernel.org> User-Agent: Mutt/1.10.1 (2018-07-13) Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org On Sun, Jan 27, 2019 at 06:50:02PM -0800, Alexei Starovoitov wrote: > diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c > index a74972b07e74..e1d6aefbab50 100644 > --- a/kernel/bpf/helpers.c > +++ b/kernel/bpf/helpers.c > @@ -221,6 +221,63 @@ const struct bpf_func_proto bpf_get_current_comm_proto = { > .arg2_type = ARG_CONST_SIZE, > }; > > +#ifndef CONFIG_QUEUED_SPINLOCKS > +struct dumb_spin_lock { > + atomic_t val; > +}; > +#endif > + > +notrace BPF_CALL_1(bpf_spin_lock, struct bpf_spin_lock *, lock) > +{ > +#if defined(CONFIG_SMP) > +#ifdef CONFIG_QUEUED_SPINLOCKS > + struct qspinlock *qlock = (void *)lock; > + > + BUILD_BUG_ON(sizeof(*qlock) != sizeof(*lock)); > + queued_spin_lock(qlock); > +#else > + struct dumb_spin_lock *qlock = (void *)lock; > + > + BUILD_BUG_ON(sizeof(*qlock) != sizeof(*lock)); > + do { > + while (atomic_read(&qlock->val) != 0) > + cpu_relax(); > + } while (atomic_cmpxchg(&qlock->val, 0, 1) != 0); > +#endif > +#endif > + return 0; > +} > + > +const struct bpf_func_proto bpf_spin_lock_proto = { > + .func = bpf_spin_lock, > + .gpl_only = false, > + .ret_type = RET_VOID, > + .arg1_type = ARG_PTR_TO_SPIN_LOCK, > +}; > + > +notrace BPF_CALL_1(bpf_spin_unlock, struct bpf_spin_lock *, lock) > +{ > +#if defined(CONFIG_SMP) > +#ifdef CONFIG_QUEUED_SPINLOCKS > + struct qspinlock *qlock = (void *)lock; > + > + queued_spin_unlock(qlock); > +#else > + struct dumb_spin_lock *qlock = (void *)lock; > + > + atomic_set_release(&qlock->val, 0); > +#endif > +#endif > + return 0; > +} > + > +const struct bpf_func_proto bpf_spin_unlock_proto = { > + .func = bpf_spin_unlock, > + .gpl_only = false, > + .ret_type = RET_VOID, > + .arg1_type = ARG_PTR_TO_SPIN_LOCK, > +}; > + > #ifdef CONFIG_CGROUPS > BPF_CALL_0(bpf_get_current_cgroup_id) > { Would something like the below work for you instead? I find it easier to read, and the additional CONFIG symbol would give architectures (say ARM) an easy way to force the issue. --- a/kernel/bpf/helpers.c +++ b/kernel/bpf/helpers.c @@ -221,6 +221,72 @@ const struct bpf_func_proto bpf_get_curr .arg2_type = ARG_CONST_SIZE, }; +#if defined(CONFIG_QUEUED_SPINLOCKS) || defined(CONFIG_BPF_ARCH_SPINLOCK) + +static inline void __bpf_spin_lock(struct bpf_spin_lock *lock) +{ + arch_spinlock_t *l = (void *)lock; + BUILD_BUG_ON(sizeof(*l) != sizeof(__u32)); + if (1) { + union { + __u32 val; + arch_spinlock_t lock; + } u = { .lock = __ARCH_SPIN_LOCK_UNLOCKED }; + compiletime_assert(u.val == 0, "__ARCH_SPIN_LOCK_UNLOCKED not 0"); + } + arch_spin_lock(l); +} + +static inline void __bpf_spin_unlock(struct bpf_spin_lock *lock) +{ + arch_spinlock_t *l = (void *)lock; + arch_spin_unlock(l); +} + +#else + +static inline void __bpf_spin_lock(struct bpf_spin_lock *lock) +{ + atomic_t *l = (void *)lock; + do { + atomic_cond_read_relaxed(l, !VAL); + } while (atomic_xchg(l, 1)); +} + +static inline void __bpf_spin_unlock(struct bpf_spin_lock *lock) +{ + atomic_t *l = (void *)lock; + atomic_set_release(l, 0); +} + +#endif + +notrace BPF_CALL_1(bpf_spin_lock, struct bpf_spin_lock *, lock) +{ + __bpf_spin_lock(lock); + return 0; +} + +const struct bpf_func_proto bpf_spin_lock_proto = { + .func = bpf_spin_lock, + .gpl_only = false, + .ret_type = RET_VOID, + .arg1_type = ARG_PTR_TO_SPIN_LOCK, +}; + +notrace BPF_CALL_1(bpf_spin_unlock, struct bpf_spin_lock *, lock) +{ + __bpf_spin_unlock(lock); + return 0; +} + +const struct bpf_func_proto bpf_spin_unlock_proto = { + .func = bpf_spin_unlock, + .gpl_only = false, + .ret_type = RET_VOID, + .arg1_type = ARG_PTR_TO_SPIN_LOCK, +}; + #ifdef CONFIG_CGROUPS BPF_CALL_0(bpf_get_current_cgroup_id) {